Advertisement
Guest User

bk7252 ftdi232rl spi flash

a guest
Nov 6th, 2023
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | Software | 0 0
  1. from pylibftdi import BitBangDevice
  2. import time
  3.  
  4. # https://eblot.github.io/pyftdi/pinout.html
  5. # https://html.alldatasheet.com/html-pdf/1246138/FTDI/FT232RL/937/8/FT232RL.html
  6.  
  7. # FTDI GPIO pins mapped for SPI
  8. SPI_MOSI = 0x01  # TX pin used for MOSI
  9. SPI_MISO = 0x02  # RX pin used for MISO
  10. SPI_SCK = 0x04   # RTS pin used for SCK
  11. SPI_TMS = 0x8    # CTS pin used for CS
  12. CEN_PIN = 0x10  # DTR pin for CEN
  13.  
  14. def configure_spi_pins(bitbang):
  15.     # Set the direction of pins for SPI communication
  16.     bitbang.direction = SPI_MOSI | SPI_SCK | CEN_PIN | SPI_TMS
  17.  
  18. def sleep_ms(milliseconds):
  19.     time.sleep(milliseconds / 1000)
  20.  
  21. def sleep_us(useconds):
  22.     time.sleep(useconds / 1000 / 1000)
  23.  
  24. def toggle_dtr_cts(bitbang):
  25.     # Set DTR and CTS pins high
  26.     bitbang.port |= CEN_PIN
  27.  
  28.     # Wait or do other operations
  29.     time.sleep(4)
  30.  
  31.     # Set DTR and CTS pins low
  32.     bitbang.port &= ~(CEN_PIN)
  33.     time.sleep(4)
  34.  
  35. def bit_bang_spi_xfer(bitbang, data):
  36.     delay = 10
  37.     received_data = []
  38.     bitbang.port &= ~SPI_TMS # low
  39.     for byte in data:
  40.         received_byte = 0
  41.         for bit in range(8):
  42.  
  43.  
  44.             # Clock pulse
  45.             bitbang.port &= ~SPI_SCK # low
  46.             sleep_us(delay)
  47.  
  48.             # Set MOSI based on the bit of the byte to be sent
  49.             if byte & (1 << (7 - bit)):
  50.                 bitbang.port |= SPI_MOSI
  51.             else:
  52.                 bitbang.port &= ~SPI_MOSI
  53.  
  54.  
  55.             bitbang.port |= SPI_SCK # high
  56.             sleep_us(delay)
  57.  
  58.             # Read MISO
  59.             if bitbang.port & SPI_MISO:
  60.                 received_byte |= (1 << (7 - bit))
  61.             sleep_us(delay)
  62.  
  63.         received_data.append(received_byte)
  64.    
  65.     bitbang.port |= SPI_TMS # high
  66.    
  67.     return received_data
  68.  
  69. def ChipReset(bitbang):
  70.     # set CEN low for 1s
  71.     bitbang.port &= ~(CEN_PIN)
  72.     time.sleep(1)
  73.     bitbang.port |= CEN_PIN
  74.  
  75. def ModeSelect(bitbang):
  76.     #bitbang.port |= SPI_MOSI
  77.     #bitbang.port |= SPI_SCK
  78.     bitbang.port &= ~SPI_MOSI
  79.     bitbang.port &= ~SPI_SCK
  80.     sleep_ms(500);
  81.    
  82. def DD(bitbang):
  83.     len = 250
  84.     print("Send 250 'D2'")
  85.     send_buf = bytearray(len)
  86.     for x in range(len):
  87.         send_buf[x] = 0xD2
  88.     a = bit_bang_spi_xfer(bitbang, send_buf)
  89.  
  90.  
  91.     for x in range(len):
  92.         print(hex(a[x]), end = '')
  93.         print(" ", end = '')
  94.  
  95. def BK_EnterSPIMode(bitbang):
  96.     #ModeSelect(bitbang)
  97.     ChipReset(bitbang)
  98.    
  99.     DD(bitbang)
  100.    
  101. #    time.sleep(0.1)
  102. #    time.sleep(1.1)
  103.  
  104.     print("Test by sending get ID")
  105.     cmd_id = bytearray(4)
  106.     cmd_id[0] = 0x9F
  107.     cmd_id[1] = 0x0
  108.     cmd_id[2] = 0x0
  109.     cmd_id[3] = 0x0
  110.    
  111.     a = bit_bang_spi_xfer(bitbang, cmd_id)
  112.  
  113.     for x in range(4):
  114.         print(hex(a[x]), end = '')
  115.         print(" ", end = '')
  116.        
  117. #    if a[0] == 0x00 and a[1] == 0x1c and a[2] == 0x70 and a[3] == 0x15:
  118.     if a[0] == 0x00 and a[1] == 0xb and a[2] == 0x40 and a[3] == 0x15:
  119.         print("ID OK")
  120.         return 1
  121.        
  122.     print("ID bad")
  123.     return 0
  124.  
  125. def ReadStart(bitbang, startaddr, filename, readlen):
  126.     count = 0
  127.     addr = startaddr
  128.     f = open(filename, "wb")
  129.     size = readlen
  130.     size = (size+255)//256*256
  131.     print("Reading")
  132.  
  133.     while count < size:
  134.         print("count "+str(count) +"/"+str(size))
  135.         send_buf = bytearray(4+256)
  136.         send_buf[0] = 0x03
  137.         send_buf[1] = (addr & 0xFF0000) >> 16
  138.         send_buf[2] = (addr & 0xFF00) >> 8
  139.         send_buf[3] = addr & 0xFF
  140.         result = bit_bang_spi_xfer(bitbang, send_buf)
  141.         count += 256
  142.         addr += 256
  143.         part = bytearray(result[4:4+256])
  144.         for x in range(256):
  145.             print(hex(part[x]), end = '')
  146.             print(" ", end = '')
  147.         f.write(part)
  148.  
  149.     f.close()
  150.  
  151.     ChipReset(bitbang)
  152.     return True
  153.  
  154. if __name__ == "__main__":
  155.     with BitBangDevice() as bb:
  156.         bitbang = bb
  157.         configure_spi_pins(bitbang)
  158.        
  159.         if BK_EnterSPIMode(bitbang) == 0:
  160.           print("Failed to read flash id")
  161.         #  exit();
  162.        
  163.         ReadStart(bitbang, 0x11000,"tstReadS.bin", 0x1100)
  164.        
  165. #        while True:
  166. #           toggle_dtr_cts(bitbang)
  167.  
  168.         # Example SPI xfer2 function call
  169. #        data_out = [0xAA, 0xBB, 0xCC, 0xDD]  # Example data to send
  170. #        received_data = bit_bang_spi_xfer(data_out, bitbang)
  171. #        print("Received data:", received_data)
  172.  
  173.         # Example SPI xfer function call
  174. #        data_out = [0x12, 0x34, 0x56, 0x78]  # Example data to send
  175. #        received_data = bit_bang_spi_xfer(data_out, bitbang)
  176. #        print("Received data:", received_data)
  177.        
  178.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement