Los Frijoles
Posts: 13
Joined: Tue Jun 12, 2012 6:06 am

Anyone interested in testing my avrdude with SPI support?

Mon May 27, 2013 5:26 pm

I just finished my first Raspberry Pi project yesterday which was to modify avrdude so that it would use the built in SPI interface on the Raspberry Pi (and other linux distributions) to talk to an AVR. I have posted the modified avrdude code here: https://github.com/kcuzner/avrdude. It should be relatively easy to clone it to the raspberry pi and build it there. I did all the development on the Raspberry Pi itself (aside from actually viewing the files in an editor...I used an nfs share) and I didn't experience any problems building it (I am using the Arch Linux image).

My howto and description (which probably needs improvement) is here: http://kevincuzner.com/2013/05/27/raspb ... rogrammer/

The issue I have is that I only have a few AVRs and so I haven't been able to test this with more than just my Atmega48 that I had lying around. For all I know, I messed up somewhere and it only works on my particular AVR.

I understand that there is already a "linuxgpio" programmer type in avrdude, but I thought to myself, "Why use bitbanging on the GPIOs when I could use the built in hardware SPI which is probably faster?"

So, would anyone be interested in trying it out?

kadamski
Posts: 187
Joined: Fri Jun 08, 2012 10:56 pm

Re: Anyone interested in testing my avrdude with SPI support

Mon May 27, 2013 9:08 pm

Seems to be working with my atmega328p, using exacly the same wiring as for linuxgpio driver. I was able to properly read fuses and program some basic blink application. Great job!

The only problem I have found (but I believe it may be in spi driver, not in your code) is that if I use linuxgpio driver before using linuxspi driver, avrdude won't be able to communicate. I have to remove spi driver and insert it again so that it configures SPI pins properly. It might be possible to reconfigure the pins each time avrdude is started to prevent this (maybe this could be configured in config file?).

Are you going to try merging this driver upstream?

Los Frijoles
Posts: 13
Joined: Tue Jun 12, 2012 6:06 am

Re: Anyone interested in testing my avrdude with SPI support

Wed May 29, 2013 2:53 pm

I would like to merge it upstream, but I honestly don't know how :(

I'll see if something can be added to make the SPI module load properly before talking to the programmer. That is an interesting issue that the GPIO takes over the SPI and doesn't give it back. At the moment, the only configuration I needed before was sacrificing a GPIO as a reset which would be manipulated through the sysfs endpoints.

neu-rah
Posts: 6
Joined: Sun Jun 16, 2013 5:34 am

Re: Anyone interested in testing my avrdude with SPI support

Sun Jun 16, 2013 5:41 am

what am i missing?

sudo avrdude -c linuxspi -P /dev/spidev0.0 -U flash:w:led.hex -p m328p -F -t

avrdude: error: AVR device not responding
avrdude: initialization failed, rc=-1
avrdude: AVR device initialized and ready to accept instructions
avrdude: Device signature = 0x9042ef
avrdude: Expected signature for ATmega328P is 1E 95 0F

i'm using the same wiring and it works fine on my test c++ code using wiringPiSPI library, i was able to read the correct ID of my ATMega328p

any ideas?

p.s. on my test code i have control over my atx power supply and i was able to powerup with reset and sck properlly set (so the sync rest pulse part of the code was never used)

Los Frijoles
Posts: 13
Joined: Tue Jun 12, 2012 6:06 am

Re: Anyone interested in testing my avrdude with SPI support

Sun Jun 16, 2013 4:17 pm

I would guess it has to do with your reset pulse thing. I had some difficulty with getting the device to read the correct signature before I got the reset line functioning properly. The reset pulse that avrdude gives is actually timed in relation to everything else, so it is possible that your implementation is either letting it up while programming or pulling it down too soon/late?

Does it read the same signature each time? If not, it may not be being powered all the way. Another problem I ran into was that without the proper reset sequence and powering, the chip appeared to randomly reset itself, even during programming mode. If your clock frequency is too high for the 3.3V (e.g. if you are using a 16-20Mhz crystal), you may see similar issues.

Also, I found that using a DLA to scope it out was a great help (I used a Saleae Logic). Actually, without it I probably wouldn't have been able to get the code working to put the reset portion of the code in the right place.

neu-rah
Posts: 6
Joined: Sun Jun 16, 2013 5:34 am

Re: Anyone interested in testing my avrdude with SPI support

Sun Jun 16, 2013 5:13 pm

sorry this is my first burn (complete noob), on my test code it looks ok, but using avrdude on command line it can't detect the atmega and -F makes it report random ID's, i'm assuming that i dont have to put the chip in programnming mode before i call avrdude on the command line, right? (using GPIO/BCM pin 25 as a reset)

device is at 3.3v using internal clock 8/8 Mhz i think, anyway it responds well when i use wiringPiSPI

neu-rah
Posts: 6
Joined: Sun Jun 16, 2013 5:34 am

Re: Anyone interested in testing my avrdude with SPI support

Sun Jun 16, 2013 5:56 pm

Ok solved it, in my test code i was using 50KHz because 500KHz was too mutch when running at 3.3V i guess, just changed that value in linuxspi.c and voila it is reconizing the device. Previous to that i was messing with baud parameter to try lowering the speed with no effect... maybe we should replace that code wired constant to accept the baud rate insted.

Thanks for this nice code

Los Frijoles
Posts: 13
Joined: Tue Jun 12, 2012 6:06 am

Re: Anyone interested in testing my avrdude with SPI support

Sun Jun 16, 2013 10:54 pm

If you would like, you can open an issue on github. I was planning on using the baudrate command line switch to set the bus frequency eventually, but I have been a little bogged down with work. I wasn't sure if 500Khz was going to be too much (admittedly, I only tested using an 8Mhz clocked AVR with no divide), but I guess in some situations it really is so that sort of option does need to be added.

Great to hear you got it to work.

neu-rah
Posts: 6
Joined: Sun Jun 16, 2013 5:34 am

Re: Anyone interested in testing my avrdude with SPI support

Mon Jun 17, 2013 3:27 am

Thanks again for this nice software!

i've changed the Hz speed line to be:
.speed_hz = pgm->baudrate==0?400000:pgm->baudrate,

and added
baudrate=400000;
to the avrdude.conf section linuxspi
it works like a charm

Los Frijoles
Posts: 13
Joined: Tue Jun 12, 2012 6:06 am

Re: Anyone interested in testing my avrdude with SPI support

Mon Jun 17, 2013 6:51 am

If you fork the project, check in your changes, and give me a pull request I will merge your changes in.

neu-rah
Posts: 6
Joined: Sun Jun 16, 2013 5:34 am

Re: Anyone interested in testing my avrdude with SPI support

Mon Jun 17, 2013 8:30 am

its another thing i'm a complte noob at

neu-rah
Posts: 6
Joined: Sun Jun 16, 2013 5:34 am

Re: Anyone interested in testing my avrdude with SPI support

Mon Jun 17, 2013 6:47 pm

ok, just done that

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Anyone interested in testing my avrdude with SPI support

Sat Sep 06, 2014 6:01 pm

Good job on this, nice to see the SPI hardware being used instead of bit-banging GPIOs!

I too was caught out with the default 400kHz clock being too high on the 3V3 powered AVR. The max SPI clock before failing was 120kHz on an ATmega8515L with internal 1MHz clock.

Maybe better to set a lower frequency as default?

While testing SPI vs bitbanging on my new B+, I got runtime errors with the linuxgpio device, "can`t export GPIO-0, already exported/busy". I think this may be because on the B+ GPIOs 0&1are reserved by the videocore for the HAT i2c eeprom? Got round that by changing line 260 of linuxgpio.c for(i=0;... to for(i=2;...

Many thanks Dave.

djplaquin
Posts: 3
Joined: Wed Sep 17, 2014 3:11 pm

Re: Anyone interested in testing my avrdude with SPI support

Wed Sep 17, 2014 3:16 pm

Is it safe to directly connect the ATMega to the raspberry PI as described in the post?
I was wondering if the ATMega could start executing its program when powered by the 3.3V and could potentially try to issue a high/low level on its MOSI pin at the same time as the PI is setting its SPI MOSI pin to low/high?
So, has anyone managed to use such a setup with an already programmed ATMega?

David.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Anyone interested in testing my avrdude with SPI support

Wed Sep 17, 2014 9:45 pm

I hadn't considered the possibility of the MOSI's fighting each other. The Gertduino uses resistor voltage dividers for logic-level conversion - I suppose such an arrangement offers some protection in case both are trying to output. So if using 3V3 powered AVR, instead of connecting directly, perhaps it's safer to link with a resistor?

djplaquin
Posts: 3
Joined: Wed Sep 17, 2014 3:11 pm

Re: Anyone interested in testing my avrdude with SPI support

Thu Sep 18, 2014 8:04 am

gregeric wrote:I hadn't considered the possibility of the MOSI's fighting each other. The Gertduino uses resistor voltage dividers for logic-level conversion - I suppose such an arrangement offers some protection in case both are trying to output. So if using 3V3 powered AVR, instead of connecting directly, perhaps it's safer to link with a resistor?
I wondered because I was trying to use this setup to program an attiny84. So, having never done this before, I looked at using the blinking LED example. But when I looked at the code I realised that, for the ATTINY84, the LED is supposed to be connected to the same pin as the MOSI pin of the ATTiny84. This then got me thinking about what would happen if the program on the attiny was running while I try to reprogram it.

I would guess that adding a resistor in serie to the MOSI and the SCLK pin would help to protect the PI. I'm assuming that the reset pin should be safe to drive to 0 at any time (otherwise, that would defeat the purpose of a rest pin :) ). I'll probably try to add those and see if it works.

I was also thinking that, if avrdude make sure that the SPI pins are only active when the reset pin is asserted, and then configure them back as input before the reset pin goes back to high, then it should be ok. I'll have a look at the code later to see if it's doing this.

Regards,
David.

gregeric
Posts: 1509
Joined: Mon Nov 28, 2011 10:08 am

Re: Anyone interested in testing my avrdude with SPI support

Thu Sep 18, 2014 9:36 am

I had a look at avrdude's linuxgpio.c source. There you can see that care *is* taken, when closing the programmer, to set the GPIOs used for MOSI & SCLK as inputs before allowing the reset to go high & the AVR run. It's not so clear to me that this is taken into account when opening the programmer. Only by good fortune of having the reset line assigned to a lower numbered GPIO does this happen?

Behaviour in linuxspi.c, using the kernel spi driver, isn't so obvious. Maybe better to use the GPIO bit-bang method after all?

djplaquin
Posts: 3
Joined: Wed Sep 17, 2014 3:11 pm

Re: Anyone interested in testing my avrdude with SPI support

Thu Sep 18, 2014 12:48 pm

It's indeed not clear what the best solution would be. It would be a shame to use bitbanging though.
One solution would be to not rely on the spidev driver and access the GPIOs and the SPI device directly (using /dev/mem like WireingPi does). By blacklisting the spidev module for instance, it should not be loaded and thus the GPIOs should be configured as input by default. linuxspi could then manage the SPI's GPIOs direction and function in accordance to the RESET cycle. This has however the disadvantage to become very Raspberry PI specific.

Another solution could be to add the list of GPIOs that needs special treatment and have linuxspi use the sysfs to force them as input once the programming is done. It would still require the user to configure these GPIOs as input before connecting the atmega to the PI. Which also means that we can't have a permanent connection between the atmega and the PI or else there is a risk to blow up the MOSI and SCLK pads the moment the spidev driver is loaded. It would also mean that once avrdude is done, the SPI port can not be used as normal without re-enabling the ALT_0 function on it (and I don't think the gpio utility from wiringpi can do it so, it would probably require a specific utility).

So, I think you are write, it doesn't look like there is a clean, fit all, solution. Maybe, in the end, using 2 resistors would be the easiest and cleanest option. After all, In System Programming makes the assumption that the system has been designed to allow for re-programming. So, it is not too far fetch to require the design to include those resistors to enable ISP.

Anyway, these were my 2 cents thoughts.
David.

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

SPI programming of Arduino without gertboard

Sun Dec 28, 2014 7:21 am

I have a ATMega328p sitting on a breadboard with all the assorted bells/whistles. Following the instructions over here

http://kevincuzner.com/2013/05/27/raspb ... rogrammer/

I have successfully interfaced with my Arduino using his modified avrdude file. However, I would like to integrate it into the arduino IDE. I modified the boards.txt to include the following (following the example Gordon used to be able to program an arduino using a gertboard)

Code: Select all

pi328.name=Pi with ATmega328 (LinuxSPI)

pi328.upload.using=linuxspi
pi328.upload.protocol=linuxspi
pi328.upload.maximum_size=32768
pi328.upload.speed=57600
pi328.upload.disable_flushing=true

pi328.bootloader.low_fuses=0xff
pi328.bootloader.high_fuses=0xde
pi328.bootloader.extended_fuses=0x05
pi328.bootloader.path=optiboot
pi328.bootloader.file=optiboot_atmega328.hex
pi328.bootloader.unlock_bits=0x3F
pi328.bootloader.lock_bits=0x0F

pi328.build.mcu=atmega328p
pi328.build.f_cpu=16000000L
pi328.build.core=arduino
pi328.build.variant=standard
And I modified programmers.txt to include

Code: Select all

linuxspi.name=LinuxSPI
linuxspi.communication=/dev/spidev0.0
linuxspi.protocol=linuxspi
I've also tried commenting out the communication bit as well as changing it to SPI or serial. No matter what I try the serial port option under tools remains greyed out and com1 remains stubbornly selected! :(

I've also made sure to disable serial console access via raspi-config and confirmed that the serial options are no longer present in the cmdline file or the inittab file.

Can anyone point me in the right direction for what I need to do to get the ability to program via SPI integrated into the IDE?
Dear forum: Play nice ;-)

User avatar
DougieLawson
Posts: 35381
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: SPI programming of Arduino without gertboard

Sun Dec 28, 2014 1:06 pm

IIRC You'll need to change the Arduino boards.txt file.

It's probably better to post your question on http://forum.arduino.cc rather than here.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: SPI programming of Arduino without gertboard

Sun Dec 28, 2014 4:13 pm

abishur wrote:I've also tried commenting out the communication bit as well as changing it to SPI or serial. No matter what I try the serial port option under tools remains greyed out and com1 remains stubbornly selected! :(
It doesn't matter that 'com1' is still selected (but greyed out). You don't want to use the serial port to upload firmware. That'd work if there was a bootloader on your AVR. Have you tried to 'upload using programmer' in the Arduino IDE?

I've been using an unmodified (but configured/compiled with '--enable-linuxgpio=yes') upstream avrdude-6.1 to bitbang the SPI pins. That works (should work) about the same. Gordon's avrdude also needs 'upload using programmer'.
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Anyone interested in testing my avrdude with SPI support

Sun Dec 28, 2014 9:05 pm

Mod Edit: I have merged these two topics because this was the very thing I was discussing in the thread I started.
Dear forum: Play nice ;-)

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Anyone interested in testing my avrdude with SPI support

Sun Dec 28, 2014 9:09 pm

panik wrote:
abishur wrote:I've also tried commenting out the communication bit as well as changing it to SPI or serial. No matter what I try the serial port option under tools remains greyed out and com1 remains stubbornly selected! :(
It doesn't matter that 'com1' is still selected (but greyed out). You don't want to use the serial port to upload firmware. That'd work if there was a bootloader on your AVR. Have you tried to 'upload using programmer' in the Arduino IDE?

I've been using an unmodified (but configured/compiled with '--enable-linuxgpio=yes') upstream avrdude-6.1 to bitbang the SPI pins. That works (should work) about the same. Gordon's avrdude also needs 'upload using programmer'.
Using the Upload using programmer, I get an error message stating "error: No port specified. Port should point to an SPI interface"

Is it possible to use the version Gordon made without using the gertboard as well?
Dear forum: Play nice ;-)

User avatar
panik
Posts: 369
Joined: Fri Sep 23, 2011 12:29 pm
Location: Netherlands

Re: Anyone interested in testing my avrdude with SPI support

Mon Dec 29, 2014 12:02 am

abishur wrote:Is it possible to use the version Gordon made without using the gertboard as well?
Yes, Gordon's avrdude works with almost any AVR you throw at it, including the one on your breadboard (if it's listed in avrdude.conf, but the ATmega328p is).

Good topic merge! I've stuck with using the 'native' linuxgpio interface in the newer (6.1) avrdude because it's simple and works everytime. Also within the Arduino IDE if one would want to.

Hope you get it to work though. Native SPI sounds great! Sorry I can't be of any help with the port problem. I'd give it a try, but I'm not near a Pi and AVR until next week.

When using Gordon's avrdude, you can edit the 'gert328' entry in 'boards.txt' to change the crystal frequency. Or use a 12Mhz crystal and not change anything.
Microcontroller addon boards and software for Raspberry Pi A+/B+/Pi2:
- ARMinARM: ARM Cortex-M3 (STM32)
- AVRPi: ATmega32U4 & ATmega328 ("Arduino")
http://www.onandoffables.com

Return to “Interfacing (DSI, CSI, I2C, etc.)”