Trying to understand the SPI object.
sharpie
Posts: 150
Beau, or anyone... Can someone provide some insight for me on how to use this SPI Engine object?· I'm totally lost, not familiar with SPI at all.· Googled it to death, and still lost..· My device has the MISO, MOSI, SCK, CSB, and DRDY lines like I see others that I read about... But the object doesn't seem to use the same names for the signals?· Dpin,Cpin,SLpin, MSBPRE,LSBPRE,MSBPOST,LSBPOST...· ?· I can guess that maybe Cpin is the clock...· MSB is most significant bit, etc...· I never really used shiftin or out for much when using stamps...·
All I want to do is read an 8bit·value from the device's eeprom at 0x1F..· Maybe later on, expand after I've learned a little about the protocol etc...··But I am stumped and at your mercy.
I guess I should mention I am looking at the demo code below in my attempts..·
Post Edited (sharpie) : 12/14/2006 6:37:41 AM GMT
All I want to do is read an 8bit·value from the device's eeprom at 0x1F..· Maybe later on, expand after I've learned a little about the protocol etc...··But I am stumped and at your mercy.
I guess I should mention I am looking at the demo code below in my attempts..·
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 #0,MSBPRE,LSBPRE,MSBPOST,LSBPOST 'Used for SHIFTIN routines #4,LSBFIRST,MSBFIRST 'Used for SHIFTOUT routines #0,Dpin,Cpin,SLpin,#8,Bits 'Set Dpin,Cpin,SLpin and Bit constant VAR long DataValue OBJ SPI : "SPI Engine" PUB start { Once called from Spin, SHIFTIN or SHIFTOUT remains running in its own COG. If SHIFTIN or SHIFTOUT are called with 'Bits' set to Zero, then the COG will shut down. Another way to shut the COG down is to call 'stop' from Spin. } '------------------------------------------------------------------------------------------------------------------------------ DataValue := %00011111 'DEBUG - Test value SPI.SHIFTOUT(Dpin, Cpin, MSBFIRST, Bits, DataValue) 'Send SHIFTOUT DataVAlue 'DEBUG - Tested with 74HCT164 ' for MSBFIRST and LSBFIRST functions '------------------------------------------------------------------------------------------------------------------------------ dira[noparse][[/noparse]23..16] := %11111111 'DEBUG - Make ALL LED's outputs dira[noparse][[/noparse]SLpin] := %1 'Set Shift / Load pin as an output repeat outa[noparse][[/noparse]SLpin] := %1 'Set Shift / Load pin in shift mode "1" DataValue := SPI.SHIFTIN(Dpin, Cpin, MSBPRE, Bits) 'Get SHIFTIN DataValue outa[noparse][[/noparse]SLpin] := %0 'Set Shift / Load pin in load mode "0" outa[noparse][[/noparse]23..16] := DataValue 'DEBUG - Lightup the LED's corresponding to the data ' LED23 = MSB ' LED16 = LSB
Post Edited (sharpie) : 12/14/2006 6:37:41 AM GMT
Comments
I guess part of what I'm not understanding is how I specify what address and bits I am writing..·
The datasheet provides an example of a read operation as:
Write 0x29 in direct register ADDPTR (0x02)
Write 0x05 in direct register OPERATION (0x03)
Wait 15ms(minimum)
Read direct register DATARD8 (0x1F), the register content is in bits [noparse][[/noparse]7:0]
Help!!! Please!!!
I'm at work, but can give you quick advice. Your eeprom will likely need 4 bytes. 1st is the control, and should at least have "%1010_xxxx", the 1010 part denotes EEproms. The second and third bytes sent are the address bytes of the location you want to read or write. The firth byte is the data you are reading, or writing.
What hardware (eeprom) are you using?
I think the I2C object might make it easier. Normally eeproms need to send an ACK bit between the bytes to alert the master (in your case, the Prop) that the previous/last byte was sent and recieved.
Think about those few important points for a bit. Wikipedia SPI. I was in your boat about 6 months ago, and now I feel like me and SPI are best friends!!! [noparse]:)[/noparse]
Someone else will come along here and give you more insight. I need to go scan something on an electron microscope now (never done that before!!)
-Parsko
I will think about your reply for a bit...
The datasheet is at http://www.vti.fi/midcom-serveattachmentguid-cc8aee01c2fdb69696289d17b704d3d8/SCP1000_Product_Family_Specification_rev_0.07.pdf
Oh and I've wiki'd it, googled it, I think I'm getting more understanding of the protocol.. just not the implementation on the pchip...
Thanks for your reply Parsko. That is at least more info than I have.. (I'll assume firth is fifth and not first?)
No, I meant forth.
Just checked the datasheet. Figure 9. Study that, along with the link above. If you want to bit-bang (write your own communication routine), it's not likely going to be ffigured out overnight.
You should be able to do this with the shiftout command in the BS2functions library. You only need to send 1 6bit command, and recieve your 8 bit back.
Again, figure 9 is going to be your friend!!!!
Success!
-Luke
Table 10 and 11 actually just helped me understand a little more about how it works.. I see now that you (I know I wrote this earlier but now I understand it) write 0x29 in the address 0x02, then write the value 0x05 at address 0x03, wait then read 0x1F.. Which sounds great, but now I need to figure out what you are trying to tell me Also, I'm not so bright with the hex/binary tidbits... am I correct in assuming 0x29 = 101001 ????
Thanks for the input, seriously...
I'm a little confused about the order of the bits, fig9 shows the order as A5,A4,A3,A2,A1,A0,RB/W and a Zero, then the Data...· And it says the MSB of the words are sent first...· My example above proves my ignorance, so I transposed the numbers...· and sent MSBFIRST..· Is that wrong?·
Thanks again for the help..· I think I might feel like I'm starting to catch on at least.. at 2.20am... =/
You're probably asleep right now. I'll post a few bits now so I can remember later when I'm home...
In order to recieve data, you must have three BS2.shiftout and one BS3.shiftin commands, for a total of 4 bytes:
1st. You need to write the control bit first. There are 4 modes, choose one, probably high speed, ($0x03 = %0000_0011)
When you write, you will end up sending 2 bytes, the first is the OPERATION bit, the second is the data
So, you would BS3.shiftout the OPERATION bit "$0x03"
Then you would BS2.shiftout the OPERATION data, for high speed continuous "$0x0A" (%0000_1010)
2nd. You need to tell it to send you your data, or the 3rd shiftout and 4th shiftin
Write to the DATARD8 byte: "0x1F" (%0001_1111)
Then read the data using BS2.shiftin
The result you get from the READ step will be your data.
There are timing issues with this thing, meaning you can't do too much too fast, it seems to want to read at around 1Hz, so if you set it up to do so, that would be best initially.
From what I read, this seems to be enough to get you reading data. All due respect, you've chosen some tricky hardware to work with.
I think this is the best I can do for ya!
-Parsko
Thanks, and I'll let you know if I make any headway! If anyone else has any insight I am all ears!!!
-thanks
The only thing I'm not so sure on is if you actually need to write to the thing to get it to work. It isn't explicit in the datasheet. I'm not sure if you could just let it "warm up" and then you can read stuff in, without the write. Worth a try...
-Parsko
There are apparently other measurement modes, but it looks like you have to configure it to use them..· The triggered mode seems ok, but the module from sparkfun that I bought doesn't give access to the trigger pin.
I'm going to start working on it now and see what I can find out.. Thanks again!!!· It is greatly appreciated.
But I am totally still stuck on the idea of the order of bits in the words. The Figure9 shows the order as A5, A4, A3, A2, A1, A0, the Read or Write bit, then a zero bit as the LSB....
In your example, you have first 0000_011_1... for high speed(0x03) Which is 8 bits, and a 1 on the end to indicate write... but what about the 0 bit for LSB? Also, the order.. is it inversed? Should it be 11000010??? Or 00001110?? Since the 7th bit is the read/write and the 8th is a zero bit...??? ACK! I'm confused!!!
second, long deep breathes!!!
Oh, and you're totally right, there is an additional bit of "0" for your LSB, sorry. I missed that one. So, shift everything I suggested left(only when writing the operation bits) and add the trailing "0"
AND, remember, I think you should need 4 commands (unless it can "warm up"). Regardless, for the reading part of it, you'll need two BS2.... commands:
TO READ THE DATA
BS2.shiftout(Operation bit)
BS2.shiftin(data from x-ducer)
You might want to try a 16 bit shiftin, since the x-ducer seems to like 16 bits. Cover-your-***, so you can't say you didn't try, that's what I do. In the past, I end up trying about 1000 different things, until that one time I get something right. SAVE-AS, perform a revision change, and keep debuggin!!
I'm no PRO, so i'm not sure that everything I have said is exactly correct, just my interpretation of the datasheet.
Oh, post some code, keep it simple. Maybe others will be able to help...
-Parsko
I've been studying your posts, and the code listed in this link http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
It's in C and meant for a pic... But it shows good examples of communications with the device.· Hasn't helped me figure it out yet, but maybe somebody who knows can see what I'm doing wrong...
Here's my simplified propeller interpretation..
All I get in return from this is 1000000011111111..· I swear, at this point I'd be happy to buy one of these sensors for somebody if they could help me figure this out.!
Post Edited (sharpie) : 12/15/2006 9:20:32 AM GMT
repeat
repeat while ina[noparse][[/noparse]DRDY] == low
tv.out(00)
if ina[noparse][[/noparse]DRDY] == low
tv.str(string("LOW",13))
if ina[noparse][[/noparse]DRDY] == high
tv.str(string("HIGH",13))
outa[noparse][[/noparse]CSB] := Low
bs2.shiftout(MOSI,SCK,%10000100,BS2#MSBFIRST,8)
bs2.shiftout(MOSI,SCK,%00000000,BS2#MSBFIRST,8)
tmp := bs2.shiftin(MISO,SCK,BS2#MSBFIRST,8)
tv.dec(tmp)
tv.str(string(13))
tv.bin(tmp,8)
tv.str(string(13))
outa[noparse][[/noparse]CSB] := High
waitcnt(10_000_000+cnt)
And it is returning some values and seems to be responding to air pressure changes... Although it looks a little jittery and the number it returns doesn't mean anything to me yet (I think it's in Pa, but I have to read more on that...).. Anyway... I know I'm not supposed to be converting all 8 bits to the value and the actual value is somewhere around bit123 or something like that..
-Luke
Thanks for the help Parsko..· I'm about·ready to sleep now.
Also, make your overall waitcnt longer, like 1 second. I remember reading the throughput to be 1.8Hz...
Why is there 8 SHIFTxxx commands in your loop?
-Luke
It certainly appears to be temp too, I hold my finger next to the sensor.. the value goes up quick.. pull away and it drops back to around what it was...· soldering iron about an inch away drives it up even quicker.. and then back to around 605..
Here's what I did to get this.. not much different, just different registers and values...