PDA

View Full Version : SPIN performance



ftkalcevic
12-09-2009, 07:36 AM
Sorry if this has been discussed elsewhere, but I just can't get this forum's search to work for me.

I'm trying to write an SPI slave implementation in SPIN, but the data I'm receiving isn't anything like what appears on the wire.и This is a stripped down version of my code...

и repeat
иии b := 0
иии repeat until ina[nCS_PIN] == 1
иии repeat until ina[nCS_PIN] == 0
иии repeat 32
иииии b <<= 1
иииии repeat until ina[SCK_PIN] == 0
иииии repeat until ina[SCK_PIN] == 1
иииии if ina[MOSI_PIN] == 1
иииииии b |= 1
иии vt100.PrintHex( b, 8 )
иии vt100.PrintString(string(13))
иии
I wait for CS to go low, then read the bits as the clock goes high.и

The SPI is clocked at 125kHz.и Is this too much for SPIN?и Do I need to use PASM (when I get it working I want to up the clock to 1MHz)

Thanks,
Frank
и

heater
12-09-2009, 07:45 AM
I'm pretty sure this is a job for PASM.
Spin is going at something like 100 instructions per statement, last I heard.
Perhaps using waitpeq and friends to wait for pin states rather than repeat loops would do it in Spin (see the manual).
For 1MHz definitely PASM.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.

ftkalcevic
12-09-2009, 07:48 AM
Thanks heater. I love how quickly people reply on this forum.

Originally I used waitpeq, but got the same results. I'll start playing around with PASM.

heater
12-09-2009, 07:55 AM
Just now I'm doing some funky serial communication protocol at 5Mbit/s.
That's just exactly 4 instructions for Tx and 4 for Rx on an 80MHz Prop. Just works nicely.

I guess there is a SPI driver or two in OBEX. You could look at those to see how the bit sampling goes even if they are the master end. Have a look at the SD card low level driver in FSRW.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.

lonesock
12-09-2009, 07:59 AM
ftkalcevic said...
...
repeat 32
b <<= 1
repeat until ina[SCK_PIN] == 0
repeat until ina[SCK_PIN] == 1
if ina[MOSI_PIN] == 1
b |= 1
...

Just a note: even using waitpeq and friends (and I definitely recommend switching to them), you have an IF, OR, Repeat, SHL all happening after the clock pin goes high. You can split those so that some of the action happens after the CLK went low. Better yet, you could combine the SHL IF and OR into a single command:


b += b + ina[MOSI_PIN]


b +=b is the same as multiplying by two, which is the same as SHL by 1. And adding the actual value of the bit adds a 1 if the bit is high, and adds a 0 if it's low. You may want to check for CLK hi, then do the above line, then check for CLK low, then let it repeat, that way the overhead of the repeat can be concurrent with the CLK pin being low.

Jonathan

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.