Now you're ready to program the AVR using the Pi's GPIO pins! Make sure you've followed all the steps to this point and have compiled, installed, and configured avrdude for GPIO programming.
To program the chip you'll need a .hex file that has the compiled code you wish to run. With the Arduino IDE installed on your computer, an easy way to generate this file is to turn on the verbose compile and upload output (under preferences). Configure Arduino for the board/chip you're using and compile the code. In the debug output look for the line at the end that calls avr-objcopy to prepare the .hex file, for example compiling a Blink example on my machine produced the following:
/home/tony/Programs/arduino-1.6.4/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom /tmp/build6154610255332504576.tmp/Blink.cpp.elf /tmp/build6154610255332504576.tmp/Blink.cpp.hex
The file /tmp/build6154610255332504576.tmp/Blink.cpp.hex is the output .hex file that can be written directly to the AVR/Arduino using avrdude. Grab the .hex file and copy it to your Raspberry Pi.
Another way to generate a .hex file is to setup your own avr-gcc toolchain and compile code for the desired chip. This can be a somewhat daunting process so I recommend using Arduino's prebuilt toolchain. However if you're going at it yourself take a look at the instructions on setting up avr-gcc here--good luck!
Now from the Raspberry Pi run an avrdude command to verify it can connect to the AVR chip:
sudo avrdude -p atmega328p -C ~/avrdude_gpio.conf -c pi_1 -v
Notice the chip type is specified with -p, the path to the custom avrdude.conf
is specified with -C, and the name of the programmer is specified with -c. If you're using a different chip, configuration file path, or programmer name be sure to change the values.
Once the command runs you should see output like the following with avrdude reading the memory & fuses of the AVR chip:
avrdude: Version 6.1, compiled on May 28 2015 at 18:41:48 Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/ Copyright (c) 2007-2014 Joerg Wunsch System wide configuration file is "/home/pi/avrdude_gpio.conf" User configuration file is "/root/.avrduderc" User configuration file does not exist or is not a regular file, skipping Using Port : unknown Using Programmer : pi_1 AVR Part : ATmega328P Chip Erase delay : 9000 us PAGEL : PD7 BS2 : PC2 RESET disposition : dedicated RETRY pulse : SCK serial program mode : yes parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 ByteDelay : 0 PollIndex : 3 PollValue : 0x53 Memory Detail : Block Poll Page Polled Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00 signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00 Programmer Type : linuxgpio Description : Use the Linux sysfs interface to bitbang GPIO lines Pin assignment : /sys/class/gpio/gpio{n} RESET = 12 SCK = 24 MOSI = 23 MISO = 18 avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e950f avrdude: safemode: lfuse reads as FF avrdude: safemode: hfuse reads as D6 avrdude: safemode: efuse reads as 5 avrdude: safemode: lfuse reads as FF avrdude: safemode: hfuse reads as D6 avrdude: safemode: efuse reads as 5 avrdude: safemode: Fuses OK (E:05, H:D6, L:FF) avrdude done. Thank you.
If you see an error make sure you're running the command as root with sudo. Also go back and ensure the custom avrdude configuration exists in the expected location, the configuration has the specified programmer, and the programmer configuration is using the GPIO pins you have connected to the AVR.
Also note if you receive an error that a GPIO pin is in use try changing that pin to use a different one in the programmer configuration.
Once you've verified avrdude can talk to the chip you're ready to program it! You'll need the .hex file that was compiled, then run a command like the following to program the chip:
sudo avrdude -p atmega328p -C ~/avrdude_gpio.conf -c pi_1 -v -U flash:w:Blink.cpp.hex:i
Again the -p option configures the chip to program, the -C option points to your custom avrdude configuration, and the -c option specifies the custom programmer configuration block to use. The -U flash... option tells avrdude to wipe and program the flash memory of the AVR chip, and to use the provided Blink.cpp.hex file. Change the name and path to the hex file to the appropriate value for the code you're writing to the chip.
If avrdude successfully programs the chip you should see output like the following:
avrdude: Version 6.1, compiled on May 28 2015 at 18:41:48 Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/ Copyright (c) 2007-2014 Joerg Wunsch System wide configuration file is "/home/pi/avrdude_gpio.conf" User configuration file is "/root/.avrduderc" User configuration file does not exist or is not a regular file, skipping Using Port : unknown Using Programmer : pi_1 AVR Part : ATmega328P Chip Erase delay : 9000 us PAGEL : PD7 BS2 : PC2 RESET disposition : dedicated RETRY pulse : SCK serial program mode : yes parallel program mode : yes Timeout : 200 StabDelay : 100 CmdexeDelay : 25 SyncLoops : 32 ByteDelay : 0 PollIndex : 3 PollValue : 0x53 Memory Detail : Block Poll Page Polled Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- --------- eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00 calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00 signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00 Programmer Type : linuxgpio Description : Use the Linux sysfs interface to bitbang GPIO lines Pin assignment : /sys/class/gpio/gpio{n} RESET = 12 SCK = 24 MOSI = 23 MISO = 18 avrdude: AVR device initialized and ready to accept instructions Reading | ################################################## | 100% 0.00s avrdude: Device signature = 0x1e950f avrdude: safemode: lfuse reads as FF avrdude: safemode: hfuse reads as D6 avrdude: safemode: efuse reads as 5 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 "Blink.cpp.hex" avrdude: writing flash (1030 bytes): Writing | ################################################## | 100% 0.89s avrdude: 1030 bytes of flash written avrdude: verifying flash memory against Blink.cpp.hex: avrdude: load data flash data from input file Blink.cpp.hex: avrdude: input file Blink.cpp.hex contains 1030 bytes avrdude: reading on-chip flash data: Reading | ################################################## | 100% 0.87s avrdude: verifying ... avrdude: 1030 bytes of flash verified avrdude: safemode: lfuse reads as FF avrdude: safemode: hfuse reads as D6 avrdude: safemode: efuse reads as 5 avrdude: safemode: Fuses OK (E:05, H:D6, L:FF) avrdude done. Thank you.
Congratulations, you've successfully programmed an AVR chip using the GPIO pins on a Raspberry Pi!
Arduino Bootloader Note
If you program an Arduino board like the Uno using the method in this guide be aware that you might remove or overwrite the USB/serial bootloader. This can actually be useful if you need just a little more space in your sketch (the bootloader takes a few kilobytes of memory at the end of flash), however it means that you won't be able to program the Arduino using the Arduino IDE's normal USB/serial programming interface. Don't worry though you can burn the original Arduino bootloader back on your Arduino board to have it work with the Arduino IDE again.
Check out this tutorial for information on burning an Arduino bootloader from a .hex file using avrdude. If you download the Arduino IDE look for a firmware .hex file in the hardware/arduino/avr/bootloaders/optiboot directory beneath the IDE's installation. For an Arduino Uno the optiboot_atmega328.hex file is the bootloader file to use. Program this hex file to the Arduino using avrdude on the Raspberry Pi to restore the Arduino to its normal state and make programming work with the Arduino IDE again.
Page last edited March 08, 2024
Text editor powered by tinymce.