Update 15 May 2014
I wrote a much more detailed version of this in the May 2014 article of Linux Journal. That article goes into more detail about the device tree, how to build the hardware on a proto-cape, and how to add a webserver frontend.
We now return you back to your originally scheduled programming.
This tutorial will explain how to program an ATmega328P (the microcontroller used in the Arduino Uno) from a BeagleBone Black (BBB). For those who can’t wait for the Arduino Tre, I’ll walk you through how to breadboard your ATmega328P to your BBB so you can be ready for its release! The motivation for this tutorial is that I’m looking to put an ATmega328P on my CryptoCape, since there are few crypto libraries written for 8-bit AVRs, namely NaCl.
A few assumptions first. You’ll need an Arduino bootloader on your ATmega328P. If you have a blank ATmega328P, (I bought mine from SparkFun), you can flash the Arduino bootloader by following this tutorial. Scroll to the bottom section titled, “targeting an AVR on a breadboard.”
Secondly, I’m working in Debian, but Angstrom should be similar.
Ok, let’s get started.
- With the Power off wire up the ATmega328P as shown in the diagram. This setup is based on the Building an Arduino on a Breadboard tutorial.
- Power on the BBB.
- There are a few things we must do on the BBB. We have to enable the GPIO pin (HIGH) to power the RST pin. Then we have to enable the serial pins. Finally, we have to install avrdude on the BBB to be able to flash the ATmega328P. Proceed to the next step 🙂
- The Easy Way. You can go my github repo and just run the scripts once you have everything wired. It will perform all of the below steps for you. If you want to know how to do this manually, keep reading.
- Enabling the RST pin. (Most of these directions came from this Instructable). We will need the following script (run it as root or sudo). There is a little formula one has to know to map the GPIO pins with the Linux signal file descriptors:
. We want GPIO1-17, so:
. The following script will set the RST pin HIGH, which will let the ATmega328P run. Later, we will temporarily drop the value to reset the chip.
#!/bin/bash #gpio1_17 for arduino reset pin, pull high to turn on #GPIOb_p = b * 32 + p = N #enable the pin echo 49 > /sys/class/gpio/export #set for output echo "out" > /sys/class/gpio/gpio49/direction #set HIGH echo 1 > /sys/class/gpio/gpio49/value
- If you had a sketch running on the ATmega328P already, it should now be running. Specifically, if you had the blink example, the LED should now be blinking. If not, make sure the ATmega328P has an Arduino bootloader, the blink sketch is loaded, and double check your wiring. You’ll want to have the confidence that everything is wired correctly before continuing with the serial lines. Better to know that Alles in Ordung.
- Enable the BBB serial lines. In order to do that we need to create a device tree overlay and enable the pins. First, we need the Device Tree Compiler (dtc). Robert Nelson saves the day again, because we need a patched dtc for the BBB. Install as follows:
wget -c https://raw.github.com/RobertCNelson/tools/master/pkgs/dtc.sh chmod +x dtc.sh sudo ./dtc.sh
- With dtc installed, follow the directions here to enable your serial TTY on the BBB.
- Now we need the avr tools:
sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude
- Upload a sketch. If you are using the Arduino IDE, turn on verbose output for compilation (as shown in step three). You are looking for
Blink.cpp.hex
or whatever you are trying to upload. If you are using Blink, I recommend changing the value of the sleep so that you know that you’ve uploaded a different program. - Get that file to the BBB. I use
sftp
, others likescp
. Whatever. - Now we need a script to upload the sketch. As the original tutorial mentions, we first have to reset that ATmega328P, with the GPIO, then serial over the sketch. You can use the following script. If you’ve followed along, this should be the same GPIO pin and /dev/tty that I have.
#!/bin/bash if [ "$#" -ne 1 ] then echo "Usage: $0 sketch.cpp.hex" exit 1 fi (echo 0 > /sys/class/gpio/gpio49/value && sleep 0.8 && echo 1 > /sys/class/gpio/gpio49/value) & avrdude -c arduino -p m328p -v -v -v -v -P /dev/ttyO4 -U flash:w:$1

- Run as follows:
sudo ./upload.sh Blink.cpp.hex
. Assuming you call the script upload.sh and you want to upload Blink.cpp.hex, of course. - You should see a lot of output scroll past, but it should end with:
avrdude done. Thank you.
The ATmega328P should execute your sketch!

Thanks for the guide, it was very useful in bootstrapping me on a similar project, the only problem I encountered was your upload script did not work for me, however this simpler one did:
#!/bin/bash
if [ “$#” -ne 1 ]
then
echo “Usage: $0 sketch.cpp.hex”
exit 1
fi
echo 0 > /sys/class/gpio/gpio49/value
sleep 1
echo 1 > /sys/class/gpio/gpio49/value
avrdude -c arduino -p m328p -v -v -v -v -P /dev/ttyO4 -U flash:w:$1
Cool, glad it worked. The timing is kinda funky for the reset. I should look to see how long the uP needs to listen for the serial upload…
Anyway, I like your simplified script.