Shop OBEX P1 Docs P2 Docs Learn Events
Is there code for a high speed SPI Slave? — Parallax Forums

Is there code for a high speed SPI Slave?

blittledblittled Posts: 681
edited 2012-04-17 17:24 in Propeller 1
I'm working on a project that needs a SPI slave that works at approx. 1MHz. I have the Chameleon PIC and that SPI Slave driver runs at 100KHz. Is there one I missed in the OBEX or has someone worked on one? Thanks!

Comments

  • jmgjmg Posts: 15,144
    edited 2012-04-14 15:04
    Full duplex or simplex ?

    It's a pity the Prop lacks a WAIT_Pedge opcode, (to allow avoid of WAITPEQ WAITPNE pairs) for the lowest overhead sync-to-edge.

    The classic pseudo minimal code for Serial RX, is
              Ctr_Shift = 0000000001    ; use shifter also as bit counter
    LOOP
              WAIT_For_Clock_Edge  ; Sync to external clock 
              CY = SPI_DI_Pin      ; receive mode
              RLC  Ctr_Shift       ; Shift and count 
              JC     LOOP          ; The preloaded 1 Bit appears in CY, when Required bits 1..32 are done.
    
    
    but the Prop needs a pair of WAITPEQ WAITPNE, so min is 5 lines, 2 waits ~ 3MHz 
    ( say <= 3.20MHz with a 1 clk wait adder  ~ 4% clock margin )
    

    I think there is a way to synth a WaitEdge, if you do not mind using some resource :
    Config a COG counter in feedback mode, and now we have two pins to mask.
    PinA= SPI CLK, and PinB = !PinA
     PinA    PinB
      1          0          PinA is Stable high 
      0          1          PinA is Stable Low 
      0          0          PinA has =\_
      1          1          PinA has _/=
    
    and the WAITxx mask, is 2 bits wide, and applies to BOTH PinA and PinB
    Wait is now a true edge wait, so even if applied when HI or LO, it will wait for the edge, and only
    one wait is needed per loop to sync to an external clock
     Min is  4 lines, 1 waits   ~ 4MHz  ( < 4.444'MHz)
    ( say <= 4.21MHz with a 1 clk wait adder  ~ 5% clock margin )
    

    This true edge approach could also be used for i2c, and might allow a prop to hit the 3.4MHz i2c speed bin ?
  • blittledblittled Posts: 681
    edited 2012-04-14 19:59
    jmg, Thanks for the suggestion.I was thinking of Full duplex operation but if that slows things down too much then I'll work around it. I'm trying to interface to an Arduino board via SPI and it's minimum speed is 1 MHz.
  • jmgjmg Posts: 15,144
    edited 2012-04-14 21:17
    blittled wrote: »
    I'm trying to interface to an Arduino board via SPI and it's minimum speed is 1 MHz.

    I can find this, re Ardunio SPI speeds : ["It's possible to set the SPI clock to 8, 4, 2, 1MHz, 500, 250, 125kHz (Assuming a 16MHz system clock). "]

    In Pseudo code, Duplex is along the lines of
    		  IO_Data = TxData 
    		  BitCtr  = 32
    LOOP
              WAIT_For_Clock_Rise  ; Sync to external clock 
              CY = SPI_DI_Pin      ; Capture data receive mode
              RLC  IO_Data         ; Shift In RX,and out TX
              WAIT_For_Clock_Fall  ; Sync to external clock 
              SPI_DO_Pin = CY      ; Send Data on opposite edge
              DJNZ  BitCtr,LOOP    ; Count Bits in-out
    

    Which should be good for ~ 2.75MHz, so should cover the 2MHz speed choice above.
    As always, start with the lowest speed for testing, and then increase.
  • blittledblittled Posts: 681
    edited 2012-04-15 19:26
    jmg Thank you again! I thought the divider for the Arduino SPI was only up to 16 and as you pointed out it goes to 128! I'm actually using a UNO32 which has a 80 MHz clock rather than a 16MHz and I was afraid of it being too fast for the 100 KHz slave I'm using. It turns out it has the same SPI frequencies as an Arduino and I believe 125KHz will be close enough since I want to use a known Propeller driver as a base so I know that any commuication problems are with the UNO32. After that I'll try speeding it up using your ideas. Thanks.
  • David BetzDavid Betz Posts: 14,511
    edited 2012-04-16 06:18
    blittled wrote: »
    jmg Thank you again! I thought the divider for the Arduino SPI was only up to 16 and as you pointed out it goes to 128! I'm actually using a UNO32 which has a 80 MHz clock rather than a 16MHz and I was afraid of it being too fast for the 100 KHz slave I'm using. It turns out it has the same SPI frequencies as an Arduino and I believe 125KHz will be close enough since I want to use a known Propeller driver as a base so I know that any commuication problems are with the UNO32. After that I'll try speeding it up using your ideas. Thanks.
    You say you're using the UNO32. What tool chain are you using with it? I'm wondering if there is a Mac-based tool chain for that board.

    Thanks,
    David

    Edit: I guess this is a dumb question. I realized after posting it that you can surely use MPLAB X on the Mac to develop code for this board and also their own modification of the Arduino IDE.
  • blittledblittled Posts: 681
    edited 2012-04-17 17:24
    Dave, there is no such thing as a dumb question. I'm using MIDE on Widows 7 and yes you can use MPLAB X. I also believe there is a MIDE for MAC X called MIDEX at https://github.com/rei-vilo/mpideXcode.
Sign In or Register to comment.