Shop OBEX P1 Docs P2 Docs Learn Events
Sharing SPI bus between two cogs — Parallax Forums

Sharing SPI bus between two cogs

smbakersmbaker Posts: 164
edited 2009-02-09 08:49 in Propeller 1
Although 32 IO pins sounded reasonable at the start of my project, I've finally managed to run out of them. I have an additional SPI device that I need to connect, and only one pin left.

What I'm thinking is this -- I can share the SCLK, DI, and DO pins for the two SPI devices (this is after all how the SPI bus was designed to work).·Each SPI peripheral·would of course still need a unique CS pin, but I have enough pin budget for that.

Now on to my question. The two SPI devices will be used by two different cogs. Am I correct in assuming that if I protect the SPI code with a lock (LockSet/LockRet/etc), that I can successfully share the SCLK, DI, and DO pins between two cogs? The cog that is not using the SPI bus would set SCLK and DO to low before releasing it's lock, so that it would not interfere with the other cog that is using those same pins.

Does this all sound correct?

Thanks,
Scott

Comments

  • virtuPICvirtuPIC Posts: 193
    edited 2009-02-06 21:04
    Why don't you just use SPI as intended? With one bus master and each other device connected to the bus as slave. I would keep it like that - just for sure.

    The correct way for communication between the slave prop and the third device would be through the master prop. Yes, this would reduce data rate on the bus by more than a factor of two. But you can tune this mechanism. When the slave device sends data the master prop controls the transfer, receives the data, but drops it. The slave prop gets the data by listening to the transfer. When the slave prop wants to send data to a third device the mast prop operates the clock line while the slave prop listens to the clock and operates the data line.

    This approach does also not conform to the standard but I would consider it save. At least there is still only one master preventing clashes on the bus.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-02-07 04:31
    yes, you can share the clock with the I2C device. Also you can most likely share the SDA pin with the -CS pin. As for the DO and DI pins you should put them on seperate pins (because the W25X.. devices can be used as two read data pins). Don't forget you will need pullups on the DO and DI pins.

    For reference, see my schematic for Blades 1 & 4 in the SixBladeProb thread.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps (SixBladeProp)
    · Prop Tools under Development or Completed (Index)
    · Emulators (Micros eg Altair, and Terminals eg VT100) - index
    · Search the Propeller forums (via Google)

    My cruising website is: ·www.bluemagic.biz
  • smbakersmbaker Posts: 164
    edited 2009-02-08 18:56
    A little background that might make it clearer what I'm trying to do. My application needs both a ENC28J60 SPI ethernet controller and an SD card slot. These devices are being controller by two different cogs. The ENC28J60· is controlled by a cog running the tcp stack, and the SD slot is controlled by the spi code within the FSRW file system.

    My initial attempt at this was to share the DI, DO, and CLK pins. I placed locks around the code that used the SPI bus, so that both cogs could not use the bus at the same time. I had to be sure DI and CLK were set to low before a cog released the lock otherwise those two lines could be stuck high when the other cog got the lock. Generally, I seemed to be able to get this working with the spin version of the FSRW SPI code. The assembly version (sdpiqasm.spin) is another story. Unfortunately there's a few things going on in there that I don't understand yet, and I'm going to have to learn some prop assembly if I want to pursue this particular approach.
  • virtuPICvirtuPIC Posts: 193
    edited 2009-02-09 08:40
    Sorry, my post missed the point. I've been talking about several propellers instead of several cogs in a single propeller. Embarassing... blush.gif Yes, if you make each SPI-transaction a critical section and lock it then you should be safe.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Airspace V - international hangar flying!
    www.airspace-v.com/ggadgets for tools & toys
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-02-09 08:49
    You should have pullups on the pins and tristate them when not in use (dira is input).
    And to answer your original question, yes, use a lock.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps (SixBladeProp)
    · Prop Tools under Development or Completed (Index)
    · Emulators (Micros eg Altair, and Terminals eg VT100) - index
    · Search the Propeller forums (via Google)

    My cruising website is: ·www.bluemagic.biz
Sign In or Register to comment.