Any way to program AVR through Arduino IDE and Raspberry Pi's GPIO pins?

Hi there,

I have a problem with uploading sketches through Arduino IDE. When I hit Ctrl+Shift+U (upload using programmer) I get the following output:

Can't export GPIO 18. Already exported/busy?

I've already tried unexporting and it didn't help. With only avrdude though, it works just fine. I think I might have implemented it to programmers.txt wrong.

Setup:
-Computer: Raspberry Pi 3 B
-Programmer: GPIO line
-Chip: ATTiny84, ATTiny85, Arduino Nano(ATMega328P)

We need more details on what you're doing.

Are you following some tutorial? If so, post a link to it. Please use the chain links icon on the toolbar to make the link clickable.

Korso:
With only avrdude though, it works just fine. I think I might have implemented it to programmers.txt wrong.

Are you using sudo to run the avrdude command or are you running the exact same command as the Arduino IDE is running (which you can see in the console if you turn one verbose output during upload in Preferences)?

What exactly do you mean by details on what I'm doing?

I'm not following a tutorial, I added linuxgpio to programmers.txt following the syntax of the other programmers in an experimental way because I couldn't find any info on adding it.

If I run avrdude without sudo, I get the following output:

avrdude: Version 6.3
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/etc/avrdude.conf"
         User configuration file is "/home/pi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : unknown
         Using Programmer              : linuxgpio
Can't open gpioX/direction: Permission denied

avrdude done.  Thank you.

Arduino IDE gives the following:

avrdude: Version 6.3, compiled on Dec 16 2016 at 12:14:49
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/pi/Downloads/arduino-1.8.5/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/pi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : unknown
         Using Programmer              : linuxgpio
Can't export GPIO 12, already exported/busy?: Invalid argument
avrdude done.  Thank you.

I happen to have recently spent some time working on something very similar. The difference is I'm trying to add a linuxspi programmer rather than linuxgpio. The reason for this is that linuxgpio is a software SPI (bit banged) implementation that's slower than the hardware SPI of linuxspi (according to SparkFun).

The problem I've run into is that my "Pi_grammer" programmer only works every other time. The other 50% of the time it fails with this error:

avrdude: linuxspi_gpio_op_wr(): Unable to open file /sys/class/gpio/gpio26/direction

The same behavior occurs from the command line but if the command is run via sudo it works 100% of the time.

I'm working with GPIO in Linux so the problem has me stumped. So far there has been no solution from SparkFun either. If anyone could help with this I'd really appreciate it and it might be considered a solution to Korso's problem as well.

You can find the implementation of the Pi_grammer programmer as part of the SparkFun AVR Boards hardware package with complete instructions here:

This talks about running "raspi-config" from the terminal which sets the correct user and group for the files.

Thanks kprims but unfortunately that is not the problem in my case. The files are already set to the gpio user group.

There is another answer in that thread that was interesting to me. Although the main part of the answer was irrelevant to my situation, it mentioned that a delay is required after exporting to give time for the udev rule to fire to give access rights to the newly created GPIO files. I thought, maybe avrdude exports GPIO 26, then immediately tries to set the direction. But at that time the file is still only accessible to root because it didn't wait for the udev rule to take effect. That lead me to this:

So I had to apply that patch (with some improvements) to the AVRDUDE source code and then figure out how to compile it (never had done that before). Sure enough, it fixes the problem!
https://github.com/per1234/avrdude/commit/bd1d6dd432f8a910e67b2ec8561b1f72a654925f

Korso, I think you might be having much the same problem with your attempt to use the linuxgpio programmer. I haven't looked into its source code to see if it's also missing this important delay.

One solution is to use the linuxspi programmer instead. I have provided (updated) complete instructions for this at [WIP] Add Pi_grammer programmer by per1234 · Pull Request #1 · per1234/Arduino_Boards · GitHub. That is applied to SparkFun AVR Boards but you could just as easily apply it to Arduino AVR Boards instead if you need to use it with one of those boards.

Another workaround would be to run the Arduino IDE as root, though of course it's preferable to avoid that.

Glad you got it working.

Me too! I had pretty much given up on figuring it out on my own until your link gave me that little hint I needed, so thanks!

Now I just have to hope the linuxspi author will merge my PR:

And Arduino will then pull in that change. Preferrably before the next release of the Arduino IDE. Otherwise, the work I did won't really benefit the Arduino community. I'm actually surprised more people didn't discover this bug earlier. I think a lot of Linux people just have the kneejerk reflex to sudo everything, even though that should only be done when absolutely necessary.