Using SPI with Python


3 posts
by sn0w » Sun Dec 23, 2012 6:32 pm
Hi folks,

I'm totally new to Python and I want to adapt all my C/C++ code to Python and atm I'm stuck with SPI.

The problem is, I need to control more than 2 SPI slaves but the RPi only offers 2 CS pins, in C I was just using SPI without the automated CS control and I set the needed pins by myself. But now i can't find any example without the usage of /dev/spidev0.x so I have no idea how to tell Python to use SPI but without it's CS control.

Maybe I wasn't searching hard enough, but do you guys have any link or code snippet for me to understand how to control SPI with my own CS?


Thanks,
sn0w
Posts: 4
Joined: Sun Dec 23, 2012 6:20 pm
by IanH2 » Mon Dec 24, 2012 8:09 am
Assuming you want the Po to be SPI master, CS will be an output, so you could always ignore it, and wire the slaves' CS to GPIO pins.

Personally, I'd be inclined to use a 74HC138 or similar, to demultiplex the Pi's CS between up to 8 devices; you'd drive the 'address' inputs from GPIOs to select the device you want. That way, the Pi's controller can frame output bytes properly by raising CS between commands.

Cheers
IH
-----
https://github.com/IanHarvey
Posts: 63
Joined: Tue Dec 18, 2012 10:17 am
by sn0w » Thu Dec 27, 2012 12:41 am
Thanks for the reply, but I think we talk about 2 different things. Maybe I wasn't really clear, sorry for that.

My point is that I want to use a fuction like I use in C, but i tell that function to not use any CS and I do it myself. (So I don't waste any GPIO pin.)

Lets say: spi_write($data, NO_CS);
Is there any python lib that can do exactly that, do I have to write one on my own or waste one CS pin?


Cheers
sn0w
Posts: 4
Joined: Sun Dec 23, 2012 6:20 pm