avrdude
avrdude copied to clipboard
Fix permission denied error in linuxgpio programmer
On Raspberry Pi using linuxgpio programmer without sudo
fails with
Can't open gpioX/direction: Permission denied
The next attempt fails, because gpio pin remains exported and not removed.
Open issue: #386
Solution
After exporting gpio pin wait, until GPIO directory appears. Retry in case of EACCESS error while delayed udev permission rule applies. Call unexport
, if export
failed for whatever reason.
Verified that it fixes the problem.
Thanks for the PR! I'm a pretty recent contributor, so @dl8dtl would have to have a look at this one.
When searching for linuxgpio specific Avrdude issues it looks like there are related issues we well. You mentioned #386, but it looks to me like #472 and perhaps #372 reports the same issues too.
This PR seems to help to avoid the issue.
pi@raspberrypi:~/Desktop/build/avr/avrdude_pr917/build_linux/src $ ./avrdude -C ./avrdude.conf -c linuxgpio
-P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "./blinky_m328p.hex"
avrdude: writing flash (32768 bytes):
Writing | ################################################## | 100% 7.75s
avrdude: 32768 bytes of flash written
avrdude: verifying flash memory against ./blinky_m328p.hex:
Reading | ################################################## | 100% 6.49s
avrdude: 32768 bytes of flash verified
avrdude done. Thank you.
But if the issue already happened (say using master without the fix), then it will not help.
pi@raspberrypi:~/Desktop/build/avr/avrdude/build_linux/src $ ./avrdude -C ./avrdude.conf -c linuxgpio
-P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
Can't open gpioX/direction: Permission denied
avrdude: opening programmer "linuxgpio" on port "GPIO" failed
avrdude done. Thank you.
pi@raspberrypi:~/Desktop/build/avr/avrdude/build_linux/src $ sudo ./avrdude -C ./avrdude.conf
-c linuxgpio -P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
Can't export GPIO 25, already exported/busy?: Device or resource busy
avrdude: opening programmer "linuxgpio" on port "GPIO" failed
avrdude done. Thank you.
In this case, PR #917 does not seem to be able to help any more.
pi@raspberrypi:~/Desktop/build/avr/avrdude_pr917/build_linux/src $ sudo ./avrdude -C ./avrdude.conf
-c linuxgpio -P GPIO -p m328p -U flash:w:./blinky_m328p.hex:i
Can't export GPIO 25, already exported/busy?: Device or resource busy
(Note: BTW, there is a missing return here.)
avrdude: opening programmer "linuxgpio" on port "GPIO" failed
avrdude done. Thank you.
All in all, I think this PR is good to be merged. It at least helps to avoid the issue in the first place.
Moving to libgpiod may be the ultimate solution.
- https://github.com/avrdudes/avrdude/issues/831
Moving to libgpiod may be the ultimate solution. https://github.com/avrdudes/avrdude/issues/831
Absolutely! Now that libgpiod is a part of the linux kernel, it looks like libgpiod is available under /linux/gpio.h, which is how linuxspi
interacts with the GPIO it uses as reset pin.
But I have no idea how to port the existing linuxgpio code over to libgpiod. Maybe the person who write linuxgpio in the first place would be interested to look into it?
@stefanrueger Maybe you can take a look at this one as well to see if this can be merged as it is. It seems to improve the situation of quite a few outstanding issues.
Not so sure who can review this and get this merged (or modified if necessary). My test results show it improves the situation.
I would be happy to merge the PR as @mcuee found it a useful improvement. The one request I have is to replace the printf()
with an appropriate avrdude_message()
.