Using the Raspberry Pi 0 as ATmega328P ISP programmer - Ty Harness Feb. 2017.


I'm using a 9 VDC switchmode PSU with a 5V regulater to power the ATmega328 and the Raspbery Pi 0. See the Breadboarduino tutorial, www.arduino.cc/en/Main/Standalone , but note I'm not using an external crystal in this setup.


I'm using a Raspberry Pi 0 and a Pi Hut wifi dongle with Raspbian OS installed: SSH enabled and RealVNC enabled. I've Disabled serial console.

sudo apt-get install ardunio (it would work alot better on the PI3) It's an old version 1.05 but it does come with ATmega328 8MHz board installed. You might have to install a 3rd party board in other versions.

I've installed the Ardunio IDE just to compile the blink example. Use file preferences and tick verbose compilation to locate and copy the hex file to (say) your home directory for example:

cp /tmp/build90asdasd42.tmp/Blink.cpp.hex Blink1000.hex

Follow this tutorial to install and confgure avrdude:
learn.adafruit.com/program-an-avr-or-arduino-using-raspberry-pi-gpio-pins/overview

sudo apt-get install avrdude

Test your configuration and wiring:
sudo avrdude -p ATmega328p -C ~/avrdude_gpio.conf -c pi_1 -v

and make a note of your fuse settings. I bought a "vanilla" ATmega328p and the internal clock speed is set to 1MHz (-U lfuse:w:0x62:m -U hfuse:w:0xd9:m -U efuse:w:0x07:m ) and if you then flash the blink hex with a 1000 delay it'll flash on and off for 8 seconds at a time.

If you've robbed the ATmega328 from another job it may have had the fuses blown for external 16MHz crystal and you might have trouble programming. Check your fuse settings in the datasheet.

I've blown the fuse for 8MHz internal clock:

sudo avrdude -p ATmega328p -C ~/avrdude_gpio.conf -c pi_1 -v -U lfuse:w:0xE2:m

avrdude: safemode: lfuse reads as E2
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as 7
avrdude: safemode: Fuses OK (E:07, H:D9, L:E2)


sudo avrdude -p ATmega328p -C ~/avrdude_gpio.conf -c pi_1 -v -U flash:w:Blink1000.hex:i
Using the pi for serial communication

I'm using Raspbian Jesse and serial communication is a little more tricky because it's all setup for bluetooth on the Pi3. A lot of the tutorials on the web don't work any more. Things will always change of course. This was the best help I found:

spellfoundry.com/2016/05/29/configuring-gpio-serial-port-raspbian-jessie-including-pi-3

I used raspi-config to disable serial console and then rebooted.

sudo nano /boot/cmdline.txt
just to check if console=serial0,115200 has been removed and it had.

sudo nano /boot/config.txt
changed enable_uart=0 to enable_uart=1


shutdown

connect tx directly to rx (loopback) and power up the pi again.

ssh pi@raspberrypi031
pw xxxx


dmesg | grep ttyAMA0
and you should get something like
[0.202037] 20201000.uart: ttyAMA0 at MMIO 0x20201000 (irq = 81, base_baud = 0) is a PL011 rev2

Use minicom to do some testing:

sudo apt-get install minicom

sudo minicom -b 9600 -o -D /dev/ttyAMA0

type any key into minicom and it echos straight back.

open another ssh connection and type:

echo -e 'helloworld\r' > /dev/ttyAMA0

to quit mini com Ctrl A and then press x.

That was the easy bit I now want to connect the ATmega328P serial pins to the Pi 0 serial pins. I've used a 5V - 3.3V logic level convertor made by adafruit. (Note to self Need to look at using ATmega328 with 3.3V and internal 8MHz fuse). Connect Rx to Tx and Tx to Rx via logic level convertor.
Write a nice simple Arduino program to test that we can trasmit data from the Ardunio to the Pi and read the data in the Minicom.

Blinkserialtest1.ino

int led = 13;


// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop(){
digitalWrite(led, HIGH);
Serial.println("Wax On\r");
delay(1000);
digitalWrite(led, LOW);
Serial.println("Wax Off\r");
delay(1000);
}

Write a nice simple program for the pi0 to transmit data to the ATmega328p

To do.
References
www.atmel.com/devices/ATmega328P.aspx

www.ladyada.net/learn/avr/avrdude.html

adafruit.com/product/757

thepihut.com/products/usb-wifi-adapter

mikaelpatel/Cosa/master/package_cosa_index.json