I2C SPI loop driver/objec
Erlend
Posts: 612
I am working on a project where the Propeller is communicatiing with 3 SPI devices (RTC, ADC, IrSensor). I addition I have a large number of sensors and devices connected - almost using up all Pins.
All three SPI sensors seem to need specialized SPI drivers, judging from what I can find on OBEX - causing 3 sets of i/o pins to be used. How can I put all sensors on one common SPI loop / network?
I am fairly good at high level programming, but not so at low level, such as communication protocols.
Can anyone help, or point me to some resources, please?
All three SPI sensors seem to need specialized SPI drivers, judging from what I can find on OBEX - causing 3 sets of i/o pins to be used. How can I put all sensors on one common SPI loop / network?
I am fairly good at high level programming, but not so at low level, such as communication protocols.
Can anyone help, or point me to some resources, please?
Comments
You should at least name or better set a link to or upload the drivers you are using, so that you can also get help from guys who did not have exactly this problem before.
As much as I know from SPI, it's a fairly easy protocol and should leave a lot of free space in a single COG. So it should be possible to create some code out of the 3 specialized drivers which runs in one COG. Easiest way is simply copy all into one DAT-block with some little changes in the command-interface. More sophisticated and maybe needed because of the limited COG-RAM would be to find out the common part of all 3 drivers and extract it into sub-functions.
In any case you would then have a SPIN to PASM interface where you have different "functions" for the different SPI drivers.
With some resistors or some glue-code which keep the pins in a defined state during takeover it would also be possible to exchange the drivers in one COG. So, if you want to read device 1 you'd load code 1, if you want to read device 2 you'd load code 2 .... Of course this wastes some time for reloading the COG, but here no changes are needed in the original drivers. So, it depends on your bandwidth-needs whether it is a practical solution or not.
PS: As far as I remember SPI devices have a chip select, right? So, starting from 3 attached devices it might make sense to add a shift-register to the propeller to create the CS-signals.
I think it would even be funnier to also add a spinneret which gets the messages from the internet. So everybody can add a message on the internet and you'll see messages you did not 'invent' by yourself. This increases the long-term-fun-factor.
The plan would be that the spinneret from time to time loads the latest messages with the main-board falling back to buildin messages if there is no alternative message at all.
"Evacuate the dolphins - water tank runs empty!"
If you list the devices we can give you code examples.
The IR, RTC, and ADC will be cyclically read (by one COG I hope), whereas the SD will only be read occasionally to get 'permanent' global variables, and to play wav files (the machine is speaking!). Speed is not an issue, except for the SD - which I think should be left alone.
My Gaspresso project is progressing nicely, and is growing more and more fun - and funny eventually. Sorry, I did write a full-report-post now, but it got lost in space, and so I need time before I want to write it again. Sorry II , I have not updated my web, but that's because SPIN has been a learning exercise which has taken priority.
About the spinneret idea, i do not even mean what it is, I have to read up on spinneret first.
E
The Gaspresso has grown too 'large' to be left alone in my cabin (where there is only threadwidth internet), so it will also create fun in a more urban setting. How fun it would be to let it speak on behalf of the whole world!
The list of 'core messages' is defined - like "Water low warning" - but they can be phrased wildly different. Similarily I have established a list of moods - like Sad, Grumpy, Indifferent,etc - but the list can be expanded a lot. The machine will decide a mood at power-up, but some events will later cause it to change mood.
Mainly, the fun part is in the speaking, but the 20x4 LCD will also show an occasional odd message. Normally though, it is used for 'rational' communication with the user.
E
If you have more than 2 devices on the bus it makes sense to drive the CS signals of the different devices with a shift register. Such a shift register has 2 inputs at least (a clock- and data-input and maybe an ENABLE also makes sense) for shifting in a byte or a word depending on the size of the shift register (connected to the propeller) and then has 8/16 output pins (connected to the CS pins of your devices). This way you can shift out the 8-16 CS signals which saves you some propeller pins.
As far as I remember the FSRW (SD-card driver) in a later version is eating up all the COG-RAM, so this will need to run in it's own COG anyway, so if you can afford those extra pins (CS is then not needed on propeller-side - you can simply fix it to low) it's fine. Otherwise you'd have to implement some takeover/release - code for the bus.
As regards the SD, you just gave me another reason to leave it alone - using it's own SPI and it's own COG.
E
Are you familiar with the C3? It has some nice tricks with its SPI devices. It uses a counter chip (I'm not sure what it's really called) to pull different CS lines low based on how many pulses it receives. I thought it was a cool way to save a few pins.
I'm pretty sure the C3 schematics are available from the product page.
I had a look at the C3. Maybe I should have used that one in the first place, or maybe I will use it when I port the prototype to a permanent solution (everything soldered), but right now I think it is too much for my needs. Already I've been given ideas that will free up quite a number of pins, so I don't think I will run out of IO for this project. For larger projects I would have preferred a single-wire bus to communicate with sensors and other devices (CAN bus?), but for now my Gaspresso project is plenty enough to keep me busy.
I also got the idea that if i keep 3 separate comms lines for the 3 SPI devices, I could permanently pull down the CS of each, saving 3 pins - right?
Erlend
Some (a lot?) of SPI chips use the CS line in the communication protocol. I'm pretty sure not all SPI devices will work with their CS lines tied low.
So, it's a better idea to have CS connected to the propeller (separate for each device) and MISO/MOSI tied toghether than tie CS to low and have separate MISO/MOSI on the propeller!
But the Wiki-entry is nice and shows several ways how to use SPI.
All in all it is very important to read the specs and find the differences of all devices attached to the same pins to find out if there is away to code a general-purpose driver for all - and to find out whether CS=low tricks will work.
You can share the SCK, MOSI, and MISO pins (the three above), but each device sharing that bus will require its own chip select.
nfortunately I obviously had a bit of foggy mind when I assumed the Ir sensor was also SPI. It's not, it is SMBus, needing two pins only.
Another thing I've learned is that I2C has nothing to do with SPI, they are entirely different interfaces. You guys should have flamed me for that surely?
Erlend
The drivers that share the input and output lines were likely done just to save a pin. I doubt it would be hard to modify the objects to use two separate pins. If you post a link the drivers, maybe someone here would change them for you.
Edit the top post. "Go Advanced". Select "Solved" from menu (I think it's on the left above the edit window).
Well if you have enough SPI devices to warrant collapsing CS pins via a shift register you are likely to have at least one you want to go fast - and clocking the shift reg then latching it will become a bit of a bottleneck - might be better strategy to use a 3 to 8 or 4 to 16 active-low decoder? Poss problem with output glitches though.
BTW preventing SPI bus clashes is a perfect match for a Prop lock...
This is how I understood SPI to work. If erlend uses a shift register he would need to read sequentially or add an additional pin to enable shift register to the /CS lines taking the count to 3 + shclk, shdat, and oen (optional if just cycling sequentially). Or use a 7438(?) 3-8 or 74154 4-16 line decoder for 8 or 16 devices.
@jonnymac do you know of a good thread to understand I2C? I thought it was a different method than SPI, but have not used this method yet.
These are the two sources that I learned from:
http://www.robot-electronics.co.uk/acatalog/I2C_Tutorial.html
http://www.esacademy.com/en/library/technical-articles-and-documents/miscellaneous/i2c-bus.html