Porting a P1 object to P2 - Issues with SPI
Greg LaPolla
Posts: 320
Below are my read and write routines converted from spin to spin 2. I can see activity on my oscilloscope but I cannot decode spi. I don't quite have a grasp on the smart pins yet. Is there anything obviously wrong ?
Read seems to work (PINK) but write doesnt do anything (TEAL). Yellow is clock and blue is Chip Select
pri read(): Value dira[dout]~ ' make dout input outa[sclk] := ClockState ' set initial clock state dira[sclk]~~ ' make clk output Value~ ' clear output repeat Bits pintoggle(sclk) waitct(getct() + ClockDelay) pintoggle(sclk) waitct(getct() + ClockDelay) Value := (Value << 1) | ina[dout] return Value
pri write(Value) dira[din]~~ ' make Data pin output outa[sclk] := ClockState ' set initial clock state dira[sclk]~~ ' make Clock pin output Value <<= (32 - Bits) ' pre-align msb repeat Bits outa[din] := (Value ROL 1) & 1 ' output data bit waitct(getct() + ClockDelay) pintoggle(sclk) waitct(getct() + ClockDelay) pintoggle(sclk)
Read seems to work (PINK) but write doesnt do anything (TEAL). Yellow is clock and blue is Chip Select
Comments
Suggestions:
-- don't use dira[], outa[], ina[], etc; P2 pinxxx instructions handle all that
-- don't use post set and clear operators; they're slower than direct assignments
-- you probably don't need to slow the clock bit
Try this:
BTW, I use different but similar routines for block SPI that reads and writes blocks in memory.
From the flexspin docs (yes, may be different from PNut Spin2...):
Just trying to understand!
Thx
dgately
But...
I think you meant:
THx!
I got it working. For some reason though I still cant decode it on my scope.
Suggestions:
In your start() method, change... to:
If you change the BITS constant to a parameter in read() and write() like this ... then bits like this: ...become: ...and...
Finally, using... ...is redundant -- unless you're modifying the return value in the last step, like this:
Weird my scope cant see it.