Using SPI on SyPy



  • I just can't seem to get SPI working on the SyPy. I am trying to use I2C as well but due to the conflicting pins, I have shifted the I2C pins to P8 and P9 and it works fine.

    For CS I have tried P12 and P23. Using P12 causes problems when uploading code probably because it keeps the pin high, which causes a firmware downgrade. Pins 13-18 are input only so can't be used. Pins 5-7 are SPI but connected to the Sigfox module. I don't think I can use pins 0-4 either so it doesn't leave much room for manoeuvre.

    I am using the default pins for CLK, MOSI and MISO, i.e. 10, 11 & 14.

    Is there some other connectivity that I am missing?



  • @robert-hh Unfortunately not although I have converted the code to Arduino C++ and used it on an Adafruit Feather with no problem.



  • @alidaf Strange. Dou you have a means of testing the communication, like a logic analyzer (Saleae clone?)



  • I am testing with the following code:

    from machine import Pin
    from machine import SPI
    
    #import binascii
    
    #SPI  pins.
    SPI_CS   = 'P23'
    SPI_MOSI = 'P11'
    SPI_MISO = 'P22'
    SPI_CLK  = 'P10'
    
    REGISTER_TEST      = 0x00
    REGISTER_WRITE_BIT = 0x80
    REGISTER_READ_MASK = 0x7f
    
    SPI_LOW  = 0
    SPI_HIGH = 1
    
    spi_cs = Pin(SPI_CS, mode = Pin.OUT)
    spi_cs.value(SPI_HIGH)
    spi = SPI(0, mode=SPI.MASTER, baudrate=1000000, pins=(SPI_CLK, SPI_MOSI, SPI_MISO))
    
    # Test register should return whatever value is written to it.
    for data in range(0, 0x7e):
        spi_cs.value(SPI_LOW)
        print("Writing 0x{:02x} to register 0x{:02x}".format(data, REGISTER_TEST))
        wbuf = bytes([REGISTER_TEST | REGISTER_WRITE_BIT, data])
        # Write data into register.
        spi.write(wbuf)
        # Read data from register.
        #spi.write(bytes([REGISTER_TEST & REGISTER_READ_MASK]))
        #rbuf=spi.read(1)
        tbuf = bytes([REGISTER_TEST & REGISTER_READ_MASK])
        rbuf = spi.read(1, write=tbuf[0])
        #numRead = spi.readinto(rbuf, write=tbuf[0])
        spi_cs.value(SPI_HIGH)
        print("Read 0x{:02x} from register 0x{:02x}".format(rbuf[0], REGISTER_TEST))
    
    


  • @robert-hh I've moved MISO to pin 21 but still no joy.



  • @alidaf Pins 10,11,14 should work. Pin 14 has no internal pull-up, so you have to supply an external one.


Log in to reply
 

Pycom on Twitter