I’m trying to figure out how to program a 328p from a RPi 3b/4 using the hardware UART (/dev/ttyS0) on the RPi. I think it should be possible as long as I can trigger an external reset on the 328p. Is it possible to configure an arbitrary GPIO on the RPi as a reset pin for the upload?
The board is a custom PCB with ATMega328p at 8MHz. It sends sensor data to an RPi using serial over the UART(s). I’ve been uploading from my workstation using PlatformIO within Visual Studio using an ICSP header. However, once the project is “done” and in-situ, it will be much more convenient to upload new firmware using the RPi.
So usually the “DTR” pin of a USB-UART adapter is connected via a 100nF capacitor to the RESET pin of the 328P (schematics). A pulse on DTR translates into a reset and the Uno board will very shorty be in bootloader mode, waiting t oaccept a new firmware.
This post says while it has hardware RTS + CTR pins, there is no hardware DTR pin. So you would have to implement that as a “software-defined” DTR pin. You could do so by registering a pre-upload hook using advanced scripting, during which you can use the RPi library to pulse the pin. Then, the upload method can be kept as default (upload_protocol = arduino = aka serial.)
avrdude natively supports the raspberry_pi_gpio upload protocol, through which you can connect the shown GPIO pins to the ISP header of the ATMega328P, aka, its SPI pins (+reset). See documentation on the general gist of how to configure upload using differing protocol.
I tested this by powering the Nano from the 5V on the RPi as well as external power. Both worked just fine.
Now I’m running into some trouble when trying to replicate this on my custom board. It seems that the main program is starting up too quickly and avrdude isn’t able to hit the bootloader. From what I can tell, optiboot can be configured to wait up to 8 seconds for a programmer to communicate with it, but I can’t see how to set that configuration via platformio.
Do I need to build a custom optiboot with a higher timeout?
Are you sure Optiboot was burned into your custom ATMega328P board beforehand? If it has no bootloader, UART upload is impossible. Also, the wait time in the bootloader is predetermined by the burned bootloader, that is not configurable from the outside (and thus in PlatformIO), you would need to reburn the bootloader.
Are you directly hooking into the RST pin of the custom board or is there a capacitor in between the Pi’s “7” pin and your board’s RST pin? (Like in the reference boads.)
I had a basic misunderstanding of upload mechanisms and boot loaders. I was burning a custom bootloader via ISP and and then uploading my main program also over ISP. I didn’t realize this wiped out the bootloader.
After realizing this, I am able to upload over serial with no issues. Here is what I did (for future readers)
Custom optiboot build (from optiboot repo)
make clean AVR_FREQ=8000000 BAUD_RATE=38400 LED=D7 LED_START_FLASHES=3 UART=0 TIMEOUT=2 atmega328
Are you directly hooking into the RST pin of the custom board or is there a capacitor in between the Pi’s “7” pin and your board’s RST pin? (Like in the reference boads.)
Just directly connected. Is a capacitor recommended? I’ll need to do a new PCB revision anyways to get the RST pin connected to my RPi header.
The capacitor is an AC coupler in this case. I think this post is right in that regard. If RESET doesn’t go anywhere else in the system and the Pi can control it freely through the script, I don’t see why the cap would be required though.
You are expected to install the necessary Python package inside PlatformIO’s python environment. If you installed PlatformIO globally via pip3, you need to do a pip3 install RPi.GPIO. If you installed PlatformIO regularly or with the installer script, find the relevant Python executable with pio system info → “Python executabl” and execute that binary with -m pip install RPi.GPIO at the end.