MOD-IO programming with nothing but the A20 micro

Started by JerkoNikolic, July 13, 2022, 02:06:08 PM

Previous topic - Next topic

JerkoNikolic

Hi everyone,
I've noticed that the MOD-IO board has it's AVRISP pins (MOSI, MISO, SCK, RST) connected directly to the SPI pins of it's UEXT connector.

Since avrdude supports programming via SPI ("linuxspi" programmer on versions 6 and up), this would theoretically mean that a MOD-IO board connected to an A20 micro could be used normally and then re-programmed with a new firmware when needed. Something very useful for over-the-air updates of an IoT system using those components.

However, I am having issues running avrdude in SPI mode on the A20 micro.
Both SPI 1 and 2 are enabled in the overlays and available through `/dev/spidev`
The connector used on the A20 board is UEXT2 with the SPI1 interface and it's CS pin corresponding to the GPIO pin 272.
Hence the command to try and establish a connection with the MOD-IO board
sudo avrdude -P /dev/spidev1.0:/dev/gpiochip0:272 -c linuxspi -p m16a -n
This returns an error
avrdude: error: Unable to set SPI mode 40 on /dev/spidev1.0
avrdude: opening programmer "linuxspi" on port "/dev/spidev1.0" failed

Digging through the code a bit, I've found that the error most likely occurs at this line
https://github.com/avrdudes/avrdude/blob/c81f52ff1088310e77f74d11fa8ff4238ec4a7bc/src/linuxspi.c#L170
uint32_t mode = SPI_MODE_0 | SPI_NO_CS;
ret = ioctl(fd_spidev, SPI_IOC_WR_MODE32, &mode);
My guess is that it fails to set the mode register to 32 bit operation and this is what actually returns the -1.

Checking the sunxi driver code at https://linux-sunxi.org/SPIdev I see no mention of SPI_IOC_WR_MODE32 or SPI_IOC_RD_MODE32 and suspect this might be the cause of the issue.

Has anyone had any experience or luck with this kind of scenario? It would certainly be a great feature to have and a nice addition to the mod-io npm package.

LubOlimex

You need to mention which Linux image are you using. Not sure if the sunxi information applies to Olimage images which use mainline kernel.

Yet, SPI should work we've tested it successfully with few SPI-only UEXT modules. Definitions for SPI rules in Olimage can be found here:

https://github.com/OLIMEX/olinuxino-overlays/tree/master/sun7i-a20
Technical support and documentation manager at Olimex

JerkoNikolic

The image used is A20-OLinuXino-buster-base-202103
I'm not sure I understand you regarding SPI. Do you mean that the SPI works in general or specifically with avrdude?
SPI works for us as well in normal use cases, the trouble is specifically with avrdude.
Have you had cases where you used UEXT ports to program other boards?

LubOlimex

I meant that it works in general, we haven't used it with avrdude for programming, but with other boards that use SPI. In newer Olimage images different SPIs can be enabled or disabled via script "olinuxino-overlay".
Technical support and documentation manager at Olimex

JerkoNikolic

Yeah, the SPI worked from the start (tested with the 2,8" LCD).
The issue is specifically when avrdude tries to set the SPI mode to SPI_NO_CS
ioctl(fd_spidev, SPI_IOC_WR_MODE32, SPI_NO_CS)

JohnS

#5
Possibly the doc at
https://www.kernel.org/doc/html/latest/spi/spidev.html
will help.

It mentions SPI_IOC_WR_MODE32 and SPI_IOC_RD_MODE32 etc.

Some systems don't support SPI_NO_CS but whether the Linux version & board you're using does or not I don't know - hunt around to see.

Generally SPI uses a CS so...

Obviously (I think), without CS you can only have one SPI device and I suppose must hardwire its CS appropriately.

I'm a bit puzzled why avrdude does that? (If it does.)

John