Uploading to AVR using Raspberry Pi GPIO (avrdude GPIO reset)

Could you try with sudo ~/.local/bin/pio run -t upload?

Running with sudo reinstalls the complete platformio environment, now in /root directory and results in the same error:

...
Check program size...
text	   data	    bss	    dec	    hex	filename
18854	     94	    857	  19805	   4d5d	/root/data/pioenvs/emonpi/firmware.elf
avr-objcopy -O ihex -R .eeprom /root/data/pioenvs/emonpi/firmware.elf /root/data/pioenvs/emonpi/firmware.hex
BeforeUpload(["upload"], ["/root/data/pioenvs/emonpi/firmware.hex"])
Looking for upload port/disk...
Manually specified: /dev/ttyAMA0
scons: *** [upload] Device or resource busy
======================== [ ERROR ] Took 21.94 seconds ======================

Note, that I’m able to manually upload the firmware.hex without sudo using:

pi@emonpi:~/emonpi/firmware $ avrdude -c arduino -p ATMEGA328P -P /dev/ttyAMA0 -b 115200 -U flash:w:/home/pi/data/pioenvs/emonpi/firmware.hex 
avrdude-original: Using autoreset DTR on GPIO Pin 7

avrdude-original: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude-original: Device signature = 0x1e950f
avrdude-original: NOTE: "flash" memory has been specified, an erase cycle will be performed
                  To disable this feature, specify the -D option.
avrdude-original: erasing chip
avrdude-original: reading input file "/home/pi/data/pioenvs/emonpi/firmware.hex"
avrdude-original: input file /home/pi/data/pioenvs/emonpi/firmware.hex auto detected as Intel Hex
avrdude-original: writing flash (18948 bytes):

Writing | ################################################## | 100% 2.68s

avrdude-original: 18948 bytes of flash written
avrdude-original: verifying flash memory against /home/pi/data/pioenvs/emonpi/firmware.hex:
avrdude-original: load data flash data from input file /home/pi/data/pioenvs/emonpi/firmware.hex:
avrdude-original: input file /home/pi/data/pioenvs/emonpi/firmware.hex auto detected as Intel Hex
avrdude-original: input file /home/pi/data/pioenvs/emonpi/firmware.hex contains 18948 bytes
avrdude-original: reading on-chip flash data:

Reading | ################################################## | 100% 2.00s

avrdude-original: verifying ...
avrdude-original: 18948 bytes of flash verified

avrdude-original: safemode: Fuses OK (E:00, H:00, L:00)
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe
strace: |autoreset: Broken pipe

avrdude-original done.  Thank you.

strace: |autoreset: Broken pipe

As I learnt now, is that the avrdude in emonpi has been modified, so I also tried replacing the avrdude in ~/data/.platformio/packages/tool-avrdude executable by the modified one, but this results in the same error.

Have you stopped emonhub python service? This will be using the serial port

$ sudo service emonhub stop

Yes, I’ve stopped emonhub before runnig pio and before running avrdude manually.

Hi all,

Next, I tried after rebooting the emonpi. Now, sudo pio -t upload works, but pio -t upload as regular user still doesn’t. This is strange as both gpio and dialout are in groups:

pi@emonpi:~ $ groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi

I looked at the source code builder/scripts/atmelavr.py and notice the following:

    if env.subst("$BOARD") in ("raspduino", "emonpi"):

        def _rpi_sysgpio(path, value):
            with open(path, "w") as f:
                f.write(str(value))

        pin_num = 18 if env.subst("$BOARD") == "raspduino" else 4
        _rpi_sysgpio("/sys/class/gpio/export", pin_num)
        _rpi_sysgpio("/sys/class/gpio/gpio%d/direction" % pin_num, "out")
        _rpi_sysgpio("/sys/class/gpio/gpio%d/value" % pin_num, 1)
        sleep(0.1)
        _rpi_sysgpio("/sys/class/gpio/gpio%d/value" % pin_num, 0)
        _rpi_sysgpio("/sys/class/gpio/unexport", pin_n

However, my /sys/class/gpio doesn’t have 18 or 4, but:

pi@emonpi:/sys/class/gpio $ ls -al
total 0
drwxrwx---  2 root gpio    0 Jul 16 17:07 .
drwxr-xr-x 48 root root    0 Jul 16 17:17 ..
-rwxrwx---  1 root gpio 4096 Jul 16 17:07 export
lrwxrwxrwx  1 root gpio    0 Jul 16 17:07 gpio17 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpio17
lrwxrwxrwx  1 root gpio    0 Jul 16 17:07 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
lrwxrwxrwx  1 root gpio    0 Jul 16 17:07 gpiochip100 -> ../../devices/platform/soc/soc:virtgpio/gpio/gpiochip100
-rwxrwx---  1 root gpio 4096 Jul 16 17:07 unexport

Could this explains the issue?

@glynhudson I remember that you had the same problem. Have we resolved it?

I edited the post above, due to new user limit :wink:

I notice that when running as root, the gpio4 gets created during sudo pio run -t upload and disappears when command has finished.

@glynhudson is it true? You said me that GPIO 18 should be used.

Opsss. I’ve just changed your level manually. The system is very “smart” :slight_smile:

No, we never fixed this. We came to the conclusion that due to Raspbian root user is needed to access GPIO. I have been living with running pio as sudo to upload. Not ideal, but IMO is not the end of the world since the emonPi is a closed system on a RaspberryPi.

The emonPi uses GPIO 4 (pin 7) as the reset.

Hey :slight_smile: Would be glad for the feedback. I remember that you were interested in Library Managment.

Hi

I 'm trying to load firmware to an ATtiny85 with raspberry pi, it is that possible?

Thanks

It’s possible. PlatformIO works on Raspberry Pi too.

Thanks @ivankravets, the platformio.ini, would look like ?

    [env:attiny85]
    platform = atmelavr
    framework = arduino
    board = attiny85
    board_f_cpu = 1000000L
upload_port =/dev/ttyAMA0

Yes, that is the core idea of PlatformIO to work with the same project settings everywhere with the same manner.

Default use programmer “usbtiny” and I want to use “linuxspi” or GPIO

Is Possible?

Please read docs Redirecting...

You need upload_protocol.

I am try with

“upload_protocol = -P /dev/spidev0.0 -c linuxspi -b 10000”

but next error:
avrdude: Can't find programmer id "-P

Do you have any example of upload_protocol?

Thanks

What are you doing? Please read docs and examples below:

upload_protocol = linuxspi
upload_speed = 10000

and etc… See upload_flags

See program examples Atmel AVR — PlatformIO v6.1 documentation

[env:attiny85]
platform = atmelavr
framework = arduino
board = attiny85
board_f_cpu = 1000000
upload_flags = -P$UPLOAD_PORT

upload_protocol = linuxspi
upload_speed = 10000
upload_port = /dev/spidev0.0

and use pio run -t program.