SPI on Raspberry Pi


18 posts
by natashenka » Mon Apr 23, 2012 1:33 am
Hi,

I was wondering where I can find information on SPI support on Raspberry Pi. I've seen diagrams with pins, but I can't figure out what the voltage levels and max supported speed are, and I'm not sure if it supports slave mode. Does anyone know where I could find info on this?

Thanks!
Posts: 5
Joined: Mon Apr 23, 2012 12:20 am
by jbeale » Mon Apr 23, 2012 4:19 am
All the GPIO data lines are 3.3V logic level (note: not 5V tolerant).  As far as I can tell, the SPI interface we have access to is master-mode only. There are two other SPI interfaces but I don"t think they are accessible in the current board. The maximum SPI clock rate is apparently that of the APB (Advanced Peripherals Bus) clock, but I don"t know what speed that is.

Page 10 of the datasheet mentions the UART goes up to a 250 MHz system clock and 31.25 Mbaud.

Page 21: …If the system clock is 250 MHz and the speed field is zero the [secondary SPI port] SPI clock frequency is 125 MHz. The practical SPI clock will be lower as the I/O pads can not transmit or receive signals at such high speed. The lowest SPI clock frequency with a 250 MHz system clock is 30.5 KHz.

GPIO pinout: http://elinux.org/Rpi_Low-leve…..

SoC datasheet: http://www.raspberrypi.org/wp-.....herals.pdf
User avatar
Posts: 2045
Joined: Tue Nov 22, 2011 11:51 pm
by gsh » Mon Apr 23, 2012 5:51 am
Ooh...

Didn't think of that, the APB clock speed is changing!  The GPU has a callback loop specifically for this clock change but this is not on the ARM

We may have to add something for this...

The SPI hardware does not support SPI slave mode, there is a specific SPI / BSC slave peripheral which will do this (right after the SPI in the docs)

Gordon
--
Gordon Hollingworth PhD
Raspberry Pi - Director of Software Engineering
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 788
Joined: Sat Sep 10, 2011 11:43 am
by rew » Mon Apr 23, 2012 6:24 am
Yes, put the divisor at 2, and you'll get about 125MHz of SPI speed. I can confirm that the SPI module runs off the "core clock" which runs at 250MHz (by default).

And yes, jbeale is right. It's master-only, and the other SPI interface pins are not accessible.
Check out our raspberry pi addons: http://www.bitwizard.nl/catalog/
User avatar
Posts: 396
Joined: Fri Aug 26, 2011 3:25 pm
by Petr » Mon Apr 23, 2012 11:14 am
Hi,

what is the easiest way to use SPI (or other peripherals) from Python?

I am trying to make some attempts using Python mmap module and it looks doable.

According to Gert examples written in C  I am able to read/write to the GPIO memory registers using the Python mmap.

It is not easy to find some info about  drivers on the forum and in the wiki.

Regards

Petr
Posts: 9
Joined: Sun Oct 09, 2011 8:51 am
by natashenka » Mon Apr 23, 2012 10:32 pm
Thanks! A bit bummed about slave mode tho ...
Posts: 5
Joined: Mon Apr 23, 2012 12:20 am
by jbeale » Mon Apr 23, 2012 10:59 pm
If SPI slave mode is required, the easiest way is probably to use a separate, secondary micro, like an Atmel part, to be the SPI slave and buffer the data. Could it be a slave to two masters on the same SPI bus (RasPi + whatever else)? I've never tried to make such a system, but as long as the RasPi respected a dedicated "busy" flag using another GPIO pin, it could work I guess.
User avatar
Posts: 2045
Joined: Tue Nov 22, 2011 11:51 pm
by natashenka » Mon Apr 23, 2012 11:07 pm
It's my understanding that SPI masters answer to no one, and having two masters on the same bus is a recipe for smoke. You might be more daring though ;)

Another thought would be to implement SPI myself, just driving the gpio pins, as the speed I need is much slower than the clock speed. Has the added benefit of allowing SPI waveform debugging, and allowing much more fine-grained tweaks to the protocol.
Posts: 5
Joined: Mon Apr 23, 2012 12:20 am
by hzrnbgy » Tue Apr 24, 2012 4:39 am
If you are going to bit bang SPI, why not use UART instead? Simpler to implement and Linux already has built in support for it. Are you thinking of a particular device that can only talk SPI?
Posts: 106
Joined: Mon Dec 26, 2011 10:55 pm
by rew » Tue Apr 24, 2012 5:37 am
What are you trying to do that you want SPI slave support for?

I have a userspace SPI driver. This allows you to use the SPI bus, but of course only in master mode. This would be easy to port to python, or could be used directly from python. A kernel-level SPI driver is in the works. It's simple problems that can get you stuck. I spent the whole of yesterday to get my module to load. Just before dinner I got: "No such device". Succes! The kernel module loaded and somehow thought incorrectly that the device is not present. That's something that can of course be fixed. :-)
Check out our raspberry pi addons: http://www.bitwizard.nl/catalog/
User avatar
Posts: 396
Joined: Fri Aug 26, 2011 3:25 pm
by jbeale » Tue Apr 24, 2012 6:00 am
Great to hear of the SPI driver progress! Personally, I'd like to interface to an A/D chip via SPI. Unfortunately, I will need to wait some yet-unknown time to actually get a Pi, to be able to help test your code.  No doubt it will be well-polished by that time!
User avatar
Posts: 2045
Joined: Tue Nov 22, 2011 11:51 pm
by natashenka » Wed Apr 25, 2012 12:03 am
"What are you trying to do that you want SPI slave support for?"

"If you are going to bit bang SPI, why not use UART instead? Simpler to implement and Linux already has built in support for it. Are you thinking of a particular device that can only talk SPI?"

Ah ... we are getting into the long story :) . I'm hoping to use the Pi to reverse engineer an SPI interface.

My 'hobby' for the last while has been trying to get the code off a Tamagotchi so I can figure out how everything works. Modern Tamagotchis have contacts on the top that allow an SPI ROM to be attached (so you can play extra games on your Tamagotchi). I've dumped one of these ROMs, and I suspect it has functionality that might let me dump the code ROM, but I haven't been able to acquire a R/W chip that's compatible with the proprietary (GeneralPlus) ROM that the Tamagotchi uses. So I've been trying to use a MCU to 'simulate' this chip. I tried to us an Arduino, but it was just a hair too slow to do this. So I was thinking of using the Raspberry Pi for this, and hoping it might be useful for other reversing projects as well.

More info is here: http://www.kwartzlab.ca/author/natalies/, but the short story is yes, it must be SPI slave, because the Tamagotchi is (only) a master, and I'm hoping for a lot of fine-grained control so I can match the master as best as possible.
Posts: 5
Joined: Mon Apr 23, 2012 12:20 am
by jbeale » Wed Apr 25, 2012 12:24 am
Sounds like a fun project. You might try a Chipkit Uno32, it works like an Arduino and is about the same price, except it is a 80 MHz 32-bit CPU and works about 5x faster than a stock Arduino. It uses a Microchip PIC32MX320F128 processor which has both master and slave SPI modes.

http://www.digilentinc.com/Pro.....PKIT-UNO32

http://www.microchip.com/wwwpr.....e=en532434
User avatar
Posts: 2045
Joined: Tue Nov 22, 2011 11:51 pm
by TonyD » Wed Apr 25, 2012 3:12 pm
If you're just wanting to sniff a SPI bus then the Bus Pirate from Dangerous Prototypes might be a better solution ....
Tony
User avatar
Posts: 341
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
by Gert van Loo » Wed Apr 25, 2012 5:37 pm
It's again those &@#~%£ GPIO pins 19 and 20 which were dropped from the interface!

With those you would have access to SPI/I2S slave mode. Now all you could do is use the PCM port on the resistors R3..R10 as some sort of synchronous capture port. But only if you're desperate.

Sounds like the Chipkit Uno32 is a simpler solution.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2054
Joined: Tue Aug 02, 2011 7:27 am
by natashenka » Thu Apr 26, 2012 2:01 am
TonyD -- Can the bus pirate just sniff, or can it act as a generic slave as well? I've been looking for a board with this feature set (R/W to different bus types), but I didn't think the bus pirate allowed it ...

Yeah, i think I might be ordering a Chipkit Uno32 :D
Posts: 5
Joined: Mon Apr 23, 2012 12:20 am
by hzrnbgy » Thu Apr 26, 2012 4:32 am
If you are willing to whip out some codes, you can order a very cheap development board off mouser ($15 plus shipping)

http://www.mouser.com/ProductD.....N%252b9qEw

It's based on the newer STM32F4 (Cortex-M4) chip and can do some really amazing stuff. On SPI slave mode, it can run up to 21MHz, plenty enough for your application I supposed. And if you decide to keep it, it comes with a bunch of peripherals you can play with and satisfy your hacking desires.

Good luck!
Posts: 106
Joined: Mon Dec 26, 2011 10:55 pm
by bhensley » Tue Jun 26, 2012 7:21 pm
Many doing SPI on the beagleboard have found this to be helpful for doing SPI on the raspberry pi i've been told. I recommend checking it out.

Getting SPI working: http://www.brianhensley.net/2012/02/spi ... rev-c.html

Doing Python programming with SPI: http://www.brianhensley.net/2012/02/pyt ... us-on.html

Cheers,
Brian Hensley
Posts: 7
Joined: Tue Jun 26, 2012 7:18 pm