Advertisement
Guest User

beken spi ftdi232rl

a guest
Nov 2nd, 2023
180
0
156 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | Software | 0 0
  1. from pylibftdi import BitBangDevice
  2. import time
  3.  
  4. #https://eblot.github.io/pyftdi/pinout.html
  5.  
  6. # FTDI GPIO pins mapped for SPI
  7. SPI_MOSI = 0x01  # TX pin used for MOSI
  8. SPI_MISO = 0x02  # RX pin used for MISO
  9. SPI_SCK = 0x04   # RTS pin used for SCK
  10. CEN_PIN = 0x10  # DTR pin for CEN
  11.  
  12. def configure_spi_pins(bitbang):
  13.     # Set the direction of pins for SPI communication
  14.     bitbang.direction = SPI_MOSI | SPI_SCK | CEN_PIN
  15.  
  16. def toggle_dtr_cts(bitbang):
  17.     # Set DTR and CTS pins high
  18.     bitbang.port |= CEN_PIN
  19.  
  20.     # Wait or do other operations
  21.     time.sleep(4)
  22.  
  23.     # Set DTR and CTS pins low
  24.     bitbang.port &= ~(CEN_PIN)
  25.     time.sleep(4)
  26.  
  27. def bit_bang_spi_xfer(bitbang, data):
  28.     received_data = []
  29.     for byte in data:
  30.         received_byte = 0
  31.         for bit in range(8):
  32.             # Set MOSI based on the bit of the byte to be sent
  33.             if byte & (1 << (7 - bit)):
  34.                 bitbang.port |= SPI_MOSI
  35.             else:
  36.                 bitbang.port &= ~SPI_MOSI
  37.  
  38.             # Clock pulse
  39.             bitbang.port |= SPI_SCK
  40.             bitbang.port &= ~SPI_SCK
  41.  
  42.             # Read MISO
  43.             if bitbang.port & SPI_MISO:
  44.                 received_byte |= (1 << (7 - bit))
  45.  
  46.         received_data.append(received_byte)
  47.    
  48.     return received_data
  49.  
  50. def ChipReset(bitbang):
  51.     # set CEN low for 1s
  52.     bitbang.port &= ~(CEN_PIN)
  53.     time.sleep(1)
  54.     bitbang.port |= CEN_PIN
  55.  
  56. def BK_EnterSPIMode(bitbang):
  57.     ChipReset(bitbang)
  58.     print("Send 250 'D2'")
  59.     send_buf = bytearray(250)
  60.     for x in range(250):
  61.         send_buf[x] = 0xD2
  62.     a = bit_bang_spi_xfer(bitbang, send_buf)
  63.  
  64.  
  65.     for x in range(250):
  66.         print(hex(a[x]), end = '')
  67.         print(" ", end = '')
  68.    
  69.     time.sleep(0.1)
  70.  
  71.     print("Test by sending get ID")
  72.     cmd_id = bytearray(4)
  73.     cmd_id[0] = 0x9F
  74.     cmd_id[1] = 0x0
  75.     cmd_id[2] = 0x0
  76.     cmd_id[3] = 0x0
  77.    
  78.     a = bit_bang_spi_xfer(bitbang, cmd_id)
  79.  
  80.     for x in range(4):
  81.         print(hex(a[x]), end = '')
  82.         print(" ", end = '')
  83.        
  84.     if a[0] == 0x00 and a[1] == 0x1c and a[2] == 0x70 and a[3] == 0x15:
  85.         print("ID OK")
  86.         return 1
  87.        
  88.     print("ID bad")
  89.     return 0
  90.  
  91.  
  92. if __name__ == "__main__":
  93.     with BitBangDevice() as bb:
  94.         bitbang = bb
  95.         configure_spi_pins(bitbang)
  96.         if BK_EnterSPIMode(bitbang) == 0:
  97.           print("Failed to read flash id")
  98.           exit();
  99.  
  100. #        while True:
  101. #           toggle_dtr_cts(bitbang)
  102.  
  103.         # Example SPI xfer2 function call
  104. #        data_out = [0xAA, 0xBB, 0xCC, 0xDD]  # Example data to send
  105. #        received_data = bit_bang_spi_xfer(data_out, bitbang)
  106. #        print("Received data:", received_data)
  107.  
  108.         # Example SPI xfer function call
  109. #        data_out = [0x12, 0x34, 0x56, 0x78]  # Example data to send
  110. #        received_data = bit_bang_spi_xfer(data_out, bitbang)
  111. #        print("Received data:", received_data)
  112.        
  113.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement