Linuxspi atmega2560

Setup

Hi I am trying to use linuxspi with the Arduino MEGA (atmega2560) using a Raspberry Pi 3 Model B+. I have followed the steps in this [thread](https://community.platformio.org/t/uploading-to-avr-using-raspberry-pi-gpio-avrdude-gpio-reset/360?page=2) about the same topic. Here is how my .ini file looks
[env:megaatmega2560]
platform = atmelavr
framework = arduino
board = megaatmega2560

upload_flags = -C /etc/avrdude.conf -P$UPLOAD_PORT -b 10000
upload_protocol = linuxspi
upload_port = /dev/spidev0.0
extra_scripts = extra_script.py

And the extra_script.py looks like the following

Import("env")

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

def before_upload(source, target, env):
    _rpi_sysgpio("/sys/class/gpio/export", 22)
    _rpi_sysgpio("/sys/class/gpio/gpio22/direction", "out")
    _rpi_sysgpio("/sys/class/gpio/gpio22value", 0)


def after_upload(source, target, env):
    _rpi_sysgpio("/sys/class/gpio/gpio22/direction", "out")
    _rpi_sysgpio("/sys/class/gpio/gpio22value", 1)

env.AddPreAction("program", before_upload)
env.AddPostAction("program", after_upload)

I have connected the Arduino ICSP reset pin to GPIO25(pin 22), MOSI to SPI_MOSI GPIO10(pin 19), MISO to SPI_MISO GPIO09(pin 21), 3.3V and GND.

The errors I am dealing with

The current error I am getting is
*** [program] /sys/class/gpio/gpio22value: Permission denied

After running on boot

sudo pio run -t program

After the first time running this command, the error changes to the following when running the command

*** [program] Device or resource busy

What I have tried

Made sure I can communicate with the Arduino MEGA through ICSP with the following commands
sudo avrdude -p atmega2560 -c linuxspi -P /dev/spidev0.0 -U flash:r:back.hex.i

Which works, so most likely the Arduino is not faulty

I looked up the linuxspi configuration and configured the reset pin to GPIO25 such that the configuration looks like the following now

programmer
    id = "linuxspi";
    desc = "Use Linux SPI device in /dev/spidev*";
    type = "linuxspi";
    reset = 25
    baudrate = 40000;

Thank you in advance.

You’re getting that error because there is a / missing… it should be _rpi_sysgpio("/sys/class/gpio/gpio22/value", 1)

most likely, you also need to unexport the gpio so that it doesn’t exist at next run, or at least that’s the issue I’ve had when trying out the linuxgpio programmer method (as opposed to the linuxspi one).

i.e. add the following to the end of the after_upload function (and re-enable it… you need that function to run also):

_rpi_sysgpio("/sys/class/gpio/unexport", 22)

obviously change it to 25 if that’s the gpio you’re using. It also seems that because of how the delay in the udev rule applying with permissions after gpios are exported, that you’ll need to run pio as root (i.e. sudo) as otherwise whilst the user might technically have permissions, avrdude tries to use the gpio before the pins have their permissions updated.

1 Like

I configured the following

  • Changed the pin number to 25 from 22
  • Added / between gpio and value path
  • Added the env.Post such that the after_upload function is run
  • Added the unexport line to after_upload function
  • made a copy of avrdude.conf. The copy is located /home/pi/.avrduderc and is used in .ini and extra_script.py
  • upload_flags now has a sudo in the very beginning to avoid permission errors with avrdude conf file

The code now compiles but I am unable to get permission to load the .avrduderc file even after changing its right

The first error after reboot is now

sh: 1: /home/pi/.avrduderc: Permission denied

Error following the first error is then

*** [program] Device or resource busy

Any help would be appreciated

At least we’re getting warmer! :wink:

It sounds like from that error that something is wrong with either the permissions (ownership or access) of the /home/pi/.avrduderc directory, or perhaps the platformio.ini configuration isn’t referencing it correctly…

Unfortunately I have not been able to resolve what the issue might be. What rights do you think I should have for the /home/pi/.avrduderc directory? Do you see a problem with this referencing upload_flags = -C /etc/avrdude.conf -P$UPLOAD_PORT -b 10000 in the .ini file?