Search code examples
python-3.xusbspiusbserialftdi

How to communicate with a spi slave connected via FTDI USB correctly through the pyftdi lib?


I have the following problem: I want to read out a motion sensor from Invensense (ICM-20602) through the spi interface with the FTDI USB Spi converter connected to a Debian linux system with a python3 program. I set up all the necessary libraries and drivers and I can set the CS line and also verified my MOSI, Clock and CS line outputs by an oscilloscope. There is also MISO input from the device but not what I would expect. I am sending the register address to the whois-register to get the device ID to verify that the read/write is working. I dont understand why the device is not responding properly. This is my code at the moment:

#pyspi - pyftdi

from pyftdi.spi import SpiController, SpiIOError
from struct import *



ctrl= SpiController()#spi
ctrl.configure('ftdi://ftdi:232h/1')  # Assuming there is only one FT232H.
spi = ctrl.get_port(cs=0, freq=1E6, mode=0)# Assuming D3 is used for chip select.


write_buf = b'\x75\0xdf'


spi.write(write_buf,True,False)

read_1= spi.read(2, start=False, stop=True).tobytes()

id = spi.exchange([0x75,0xff,],2).tobytes()
#ctrl.get_port(cs=1, freq=1E6, mode=1)
print(read_1)
print(id)

There is no code error - only the read buffer is 0x00 or sometimes 0x10 but not what I would expect: the device ID: 0xAF

Has someone an idea how to get the device to answer properly?

By the way: the device is working properly with the invensense evaluation board - so the device should work properly.


Solution

  • Finally I tested the FTDI USB device with an Infineon pressure sensor with the device ID 0x10. I managed to read out the ID and then I also found out that the ICM-20602 has a different device-ID ( 0x12) as specified (0xAF) in the datasheet. I also managed to read out the gyro-data so I am very confident that the device ID is different than specified. To read out the ID for of both sensors I also adjusted my program to only send one byte during one exchange cycle. Mode 0 works for the ICM device withouth problems - 1,2 is not working - it is also not necessary to put the device in spi-mode only to be able to use mode 0. this is the adjusted programm:

    from pyftdi.spi import SpiController, SpiIOError
    from struct import *
    
    
    
    ctrl= SpiController()#spi
    ctrl.configure('ftdi://ftdi:232h/1')  # Assuming there is only one FT232H.
    spi = ctrl.get_port(cs=0, freq=1E6, mode=0)# Assuming D3 is used for chip 
    select.    
    write_buf = b'\x75\'
    
    
    spi.write(write_buf,True,False)
    
    read_1= spi.read(2, start=False, stop=True).tobytes()
    
    id = spi.exchange([0x75],2).tobytes()
    print(read_1)
    print(id)