View Full Version : 4-wire SPI Interface

03-17-2009, 08:56 AM
I have a device that uses a 4-wire SPI Interface. I'd like to be able to communicate with this device using the Propeller chip. Is there any object that is already written that can do this? I looked through the object list under the section titled "Protocol: SPI, 1-wire, 12C, asynchronous serial..." and although it mentions SPI in the title I wasn't able to find any objects that actually provide any SPI interface communications. Is one available?


03-17-2009, 08:57 AM
What is your device?

03-17-2009, 09:01 AM
The Acam TDC-GP2

03-17-2009, 09:10 AM
50pS resolution? Are you kidding me? I didn't even know time itself was quantized that low! That's just craziness!

As for the setup, I'm actually learning SPI, myself. I know the Propeller CAN do it - how is another story.

03-17-2009, 09:14 AM
Yes, I would think that if the Propeller can handle other communications standards it ought to be able to handle the 4-wire SPI. But I don't want to have to write it myself. I don't think I COULD write it myself. I just need a nice, existing, object that will let me do the little bit of communications I need to do with my TDC-GP2.

So, anyone know where I can get an object to handle that protocol?

Timothy D. Swieter
03-17-2009, 10:04 AM
I don't think there is a specific object for SPI. From what I know and used SPI is more general term than say I2C. SPI data packets and such vary and as you also may know there is three wire and four wire. A generic driver or routines can probably be created as an example, but I don't know that it would every be a good stand alone object.

I bet you could write simple SPI send/receive for your device in SPIN. Try it. We will watch and help.

Timothy D. Swieter, E.I.
www.brilldea.com (http://www.brilldea.com) - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com (http://www.tdswieter.com)

03-17-2009, 10:08 AM
SPI is simple enough ... 4 wires: data in, data out, a device select line, and a serial clock line. It is basically a high level hardware interface specification. After that things diverge. Every device has its own specific protocol in terms of timing, command structure, data bit width (8, 11, 12 etc). So you will not find and one "standard" SPI interface. It does not define a communication protocol as well like I2C or RS232, but a hardware interface. One alwaysHAS to look at the specific device's datasheets to figure out timing and I/O bit structure. There is a lot of commonality but not any single SPI interface. The best bet is to look at some of the simple interfaces (cause yours is relatively simple) for some of the SPI ADC's in OBEX (i.e. ADC0834) and try to convert that to your device. But you will still have to use the data sheets to determine correct timing and I/O bit structure.

A bit of work the first time, but once you do one the next one is easier by far. The propeller can easily handle your device (took a quick peek at the data sheet).

Not the answer you were hoping for I know, but that is SPI.


Timothy D. Swieter
03-17-2009, 10:27 AM
See here: http://forums.parallax.com/showthread.php?p=792520

Timothy D. Swieter, E.I.
www.brilldea.com (http://www.brilldea.com) - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com (http://www.tdswieter.com)

03-17-2009, 11:57 AM
Another alternative to taking on the learning curve, is to offer up a few of these devices to anyone who wants to code an object for it. It may seem lazy at first, but in my opinion, it's a fair trade - you get the software from someone, and they get some free hardware. That's how I got a driver written for an ADC I was eyeballing, and an accelerometer I bought. Another plus, is it adds yet another object to the Propeller OBEX library that everyone can use, further making the propeller more capable.

03-17-2009, 04:59 PM
Hi, I had the same problem. What I did is use the BS2 object and the shiftin/shiftout subroutines. It seems to work, though I am not quite done with this yet. Here is the code snipets I used. Hope it helps. This code compiles, however it is not fully debugged yet. However, it does communicate (sends and receives) data from the accelerometer.


_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
cs = 2
clk = 1
reset = 0
Dout = 3
Din = 4


byte v ,r,v2,v3
word reg, data, receive,what

bs2 : "BS2_Functions"

Pub Start
'dira[cs]~ set pin to input
dira[cs]~~ 'set chip select to out
dira[clk]~~ 'set chip select to out
dira[dout]~ 'set dout to input
dira[Din]~~ 'SET to din output

BS2.Debug_Str(string("Begining Program",13))

BS2.Debug_Str(string("enter register address then hit enter : ",13)) 'get the register address to query from the user

data := BS2.DEBUGIN_DEC 'get address to send to accel.

'now construct message to send to accelerometer
data := data << 8 'shift the address to the correct byte before sending
display(data,1) ' 1= sending data
waitcnt(clkfreq/10 + cnt)

receive := read_accel(data)
convert_2_g(recieve,2) 'for now just do 2's complement data

pub write_accel(send_data)
outa[cs]~~ 'set pin to high
outa[cs]~ 'set pin to low
outa[clk]~ 'set clk to low

bs2.pulsout_uS(clk, 210)
bs2.shiftout(Din, CLK, send_data, BS2#MSBFIRST,16) 'most significant bits first

pub read_accel(send_data) : results
outa[cs]~~ 'set pin to high
outa[cs]~ 'set pin to low
outa[clk]~ 'set clk to loW

bs2.pulsout_uS(clk, 210)
results := bs2.shiftin(Dout, clk, BS2#MSBpost,16) 'most significant bits first

{{ PUB SHIFTIN (Dpin, Cpin, Mode, Bits) : Value | InBit
Shift data in, master clock, for mode use BS2#MSBPRE, #MSBPOST, #LSBPRE, #LSBPOST
Clock rate is ~16Kbps. Use at 80MHz only is recommended.

PUB PULSOUT_uS(Pin,Duration) | ClkCycles

Produces an opposite pulse on the pin for the duration in 1uS increments
Smallest value is 10 at clkfreq = 80Mhz
Largest value is around 50 seconds at 80Mhz.
BS2.Pulsout_uS(500) ' 0.5 mS pulse

PUB SHIFTOUT (Dpin, Cpin, Value, Mode, Bits)| bitNum

Shift data out, master clock, for mode use ObjName#LSBFIRST, #MSBFIRST
Clock rate is ~16Kbps. Use at 80MHz only is recommended.

pub display(value,in_out)
If in_out == 1
bs2.debug_str(string("sending : "))
elseif in_out == 2
bs2.debug_str(string("receiving bin : "))
bs2.debug_str(string("receiving dec : "))
bs2.debug_str(string("dec : "))
bs2.debug_str(string("receiving new data : "))
bs2.debug_str(string("receiving alarm : "))

Tom C
03-17-2009, 06:34 PM
Hi all,

Attached is an I2C object written by Joe Lucia: http://irobotcreate.googlepages.com/home



If you are going to sendˇa Robotˇto save the world, youˇbetter make sure it likes it the way it is!

Ken Peterson
03-17-2009, 11:26 PM
This seems like a popular request lately. I would think an SPI object, especially a master, should be easy to do. It's synchronous communication, so timing precision isn't critical. It basically implements a shift register in software, with a couple of options for phase and polarity. I'm surprised there's no GP SPI object in the OBEX.

If I find myself needing SPI for anything in the near future, I'll write it as a GP object and publish it.

ˇ"I have always wished that my computer would be as easy to use as my telephone.ˇ My wish has come true.ˇ I no longer know how to use my telephone."

- Bjarne Stroustrup

Timothy D. Swieter
03-18-2009, 05:40 AM
Again, read this thread - Parallax is working on a general purpose SPI: http://forums.parallax.com/showthread.php?p=792656 with two different flavors. Should be ready this week.

Timothy D. Swieter, E.I.
www.brilldea.com (http://www.brilldea.com) - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com (http://www.tdswieter.com)