Shop Learn P1 Docs P2 Docs
SPI communication between Parallax P2 and Raspberry Pi — Parallax Forums

SPI communication between Parallax P2 and Raspberry Pi

I'm trying to transfer data from Raspberry Pi (As a master) to Parallax P2. I've attached Rpi code rpi_master_spi.cpp I have tried with Arduino Mega as a slave, I'm able to communicate bidirectionally from this code. Now I want to use Parallax P2 as a slave. I've checked shift_in() and shift_out() functions, but it's not working for me. I have used below pins of parallax P2

SCK 24
SS 26

So, is there any sample code, example or reference link available for P2 to work with SPI?


  • JonnyMacJonnyMac Posts: 8,163

    For the P2 you will want to look into smart pins. In Spin2, you would start with something like this

      SS   = 26
      MISO = 25
      SCLK = 24

    Assuming the RPi CPOL is 0, you could setup the SPI input like this:

      m := P_SYNC_RX                                                ' spi rx mode
      m |= ((SCLK-MISO) & %111) << 24                               ' add SCLK offset (B pin)
      x := %0_00000 | (8-1)                                         ' sample ahead of b pin rise, 8 bits
      pinstart(MISO, m, x, 0)                                       ' configure smart pin
      pinf(MISO)                                                    ' reset/disable until used

    This SUGGESTED code (two versions) is based on other things I've worked on, but I have not used the P2 as a SPI slave with another device. Ultimately, you'll probably want a cog that monitors the SPI input and updates a buffer on the fly (like serial objects do).

    pub shiftin(count, p_buf) | value
      repeat count
        pinl(MISO)                                                  ' enable MISO  
        repeat until pinr(MISO)                                     ' wait for a byte
        value := rdpin(MISO)                                        ' get the value
        pinf(MISO)                                                  ' reset MISO      
        value rev= 31                                               ' restore MSBFIRST order
        byte[p_buf++] := value 
    pub shiftin_pasm(count, p_buf) | value
    .loop                   drvl      #MISO                         ' enable MISO   
                            testp     #MISO                 wc      ' wait for a byte
            if_nc           jmp       #$-1
                            rdpin     pr0, #MISO                    ' get the value
                            fltl      #MISO                         ' reset MISO
                            rev       pr0                           ' restore MSBFIRST order
                            wrbyte    pr0, p_buf                    ' write to buffer
                            add       p_buf, #1                     ' update buffer pointer
                            djnz      count, #.loop
Sign In or Register to comment.