Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Arduino Development using the OpenBSD CLI

Introduction

This page is for those who want to develop software for the Arduino ATmega328-based microcontrollers using OpenBSD, but don't want to use (or cannot use) the Arduino IDE. In contrast to the instructions for FreeBSD, this page will set up your environment to be able to use and take advantage of the Arduino programming language. (These instructions may also work to some degree for other BSDs, but I have not tested on anything other than OpenBSD.)

Getting the Toolchain

There is a meta-package called "arduino" in the devel category using the arduino 1.0 release, which pulls in the following packages:

avr-binutils-2.19.1 Atmel AVR 8-bit RISC microcontrollers' GNU binutils
avr-gcc-4.2.2p2 Atmel AVR 8-bit RISC microcontrollers' XGCC
avr-libc-1.6.7 libc for Atmel AVR 8-bit RISC microcontrollers
avrdude-5.8p0 AVR microcontroller hardware in-system programmer

Note: The version numbers you see may be different than those listed here. If the versions you see are earlier than those listed, you should update to the latest version of each of these packages.

Note 2: For those who notice that the avr-gcc package is still at 4.2.2, it seems that the version of this package in ports does in fact recognize the ATmega328. At least, this post makes it sound like avr-gcc should not have compiled any of the code below, but I can attest that it did compile, and ran successfully, on a Duemilanove board with an ATmega328 microcontroller. It has also worked out fine for the Arduino Uno R3 using the ATmega 328p.

Install the package like this:

~ $ sudo pkg_add arduino

(Properly configuring the PKG_PATH with a suitable package mirror is left as an exercise for the reader. I suspect if you not only have OpenBSD installed, but want to use it as a development platform, you'll already have this set up.)

The Arduino package will pull down all required files and a few helper scripts that makes it all very easy.

Optional - vim Setup

For vim users, get a vim syntax highlighting file for PDE files. Read "Syntax file for Arduino .PDE files" for more information. I'm sure there are extensions for other editors as well. If you are not using vim, feel free to skip this section.

First, create the appropriate folder to hold the syntax file (if it doesn't already exist) and download it:

~ $ mkdir -p .vim/syntax
~ $ ftp -o .vim/syntax/arduino.vim \
"https://www.vim.org/scripts/download_script.php?src_id=10674"

Trying 216.34.181.97...
Requesting https://www.vim.org/scripts/download_script.php?src_id=10674
100% |**************************************************|  1595       00:00
1595 bytes received in 0.00 seconds (2.62 MB/s) 

Next, create the filetype.vim file (or add to it if you already have one). Mine looks like this:

~ $ cat ~/.vim/filetype.vim

" local filetype file
" loads various extra filetypes

if exists("did_load_filetypes")
    finish
endif
augroup filetypedetect
    au! BufRead,BufNewFile *.pde  setfiletype arduino
augroup END 

Optional - emacs Setup

For emacs users you can find instructions via the following link: Install Arduino mode

Creating, Compiling, and Uploading

Create a New Project (Sketch)

Now that you have the environment set up, the Arduino core files downloaded, and a place to put all your projects, let's create a project!

The arduino package came with a script called arduinoproject which sets everything up for you and supplies you with a default BSDMakefile.

~ $ cd ~/code/arduino/projects/
~/code/arduino/projects $ arduinoproject helloworld

Edit the BSDmakefile in your new project directory to suit your arduino board:

-->[@~/code/arduino/projects/helloworld $ vim BSDMakefile 

Open that file up in your favorite editor and ensure that the first few variables listed suit your environment. The stock file will work for an Arduino Duemilanove (ATMega328) board. You may have to play with the PORT variable, depending on your computer. Everything else should be okay if you're following these instructions. If you are using different paths for your projects directory, or if you have placed the Arduino core source files somewhere different, be sure to edit the file to reflect their placement in your system.

You will have a skeleton helloworld.ino file autocreated for you by the arduinoproject script. Edit helloworld.ino to look like the below example:

~/code/arduino/projects/helloworld $ vim helloworld.ino

// helloword.ino
// 
// A simple test sketch that blinks the on-board LED,
// or any LED connected to digital pin 13.

// The pin to manipulate.
int digitalPin = 13;

void setup() {
    // Set the pin as an output pin.
    pinMode(digitalPin, OUTPUT);
}

void loop() {
    // Set the pin HIGH
    digitalWrite(digitalPin, HIGH);
    // Wait for a second
    delay(1000);
    // Set the pin LOW
    digitalWrite(digitalPin, LOW);
    // Wait for another second
    delay(1000);
} 

Compile the Project

Now, compile the project. The makefile will compile the core.a Arduino library file as well, just like the Arduino IDE would.

~/code/arduino/projects/helloworld $ make
[lots of compiling, linking, etc., happens here] 

If there were any errors, correct them and re-run the make command.

"undefined reference to `__cxa_pure_virtual'"

If you get the error, please upgrade your system and the arduino package. This has been fixed in recent versions of the BSDMakefile and example files installed by the later versions of the arduinoproject script.

 applet/core.a(Print.o):(.data+0x6): undefined reference to `__cxa_pure_virtual'
*** Error code 1

Stop in /home/seth/code/arduino/projects/temperature (line 202 of BSDmakefile). 

you will need to add the following code to your PDE file:

 extern "C" void __cxa_pure_virtual(void) {
    while(1);
} 

Want more information about this error? Search the internet for the function name, or have a look at this post. The problem here is that, as someone put it, the avr-libc implementation is sort of "swiss-cheesed" in that it does not include all of the functions that the full libc/glibc does (in this case, the the __cxa_* functions defined in /usr/include/g++/cxxabi.h). This is not only an issue with OpenBSD, but happens on other platforms as well, whether using the stock Makefile or the modified BSDmakefile above.

Uploading to the Arduino

Once you have a working sketch (or at least one that compiles!), connect your Arduino to your computer and upload the new project. (Note the use of sudo in the following command):

~/code/arduino/projects/helloworld $ sudo make upload

/usr/local/bin/avrdude -V -F -C /etc/avrdude.conf -p atmega328p -P /dev/cuaU0 
-c stk500v1 -b 57600 -U flash:w:applet/helloworld.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.05s

avrdude: Device signature = 0x1e950f
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "applet/helloworld.hex"
avrdude: input file applet/helloworld.hex auto detected as Intel Hex
avrdude: writing flash (1184 bytes):

Writing | ################################################## | 100% 0.62s

avrdude: 1184 bytes of flash written

avrdude: safemode: Fuses OK

avrdude done.  Thank you. 

Note: The reason that you have to run the make upload command using sudo(8) (or as root) is because the avrdude command requires access to the COM port, which normal users cannot access. If you do not want to have to remember to run the command as root (or via sudo(8)) every time, you can set the setuid(2) bit on the avrdude executable, like so:

~ $ sudo chown root /usr/local/bin/avrdude
~ $ sudo chmod 4755 /usr/local/bin/avrdude 

Note that you should only run the above commands if you know what they do and are okay with another setuid(2) binary on your system. Standard disclaimers apply. Another option is to add yourself to the dialer group, since the USB device file is owned by uucp:dialer, so if you add your username last in the (possibly empty) list in /etc/group for "dialer:*:117:", you can run "make upload" without using sudo. Remember to log out and in again after adding yourself to the group for it to take effect. After that, it will be permanent.

Example Sketches

You can list the example sketches brought in by the arduino package here:

~ $ ls /usr/local/share/examples/arduino/

Conclusion

With this setup, you should now be able to download and run any of the Example sketches from Arduino's site, plus create and edit your own projects that utilize the Arduino programming language extensions to C/C++, using OpenBSD.