Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pylibftdi import BitBangDevice
- import time
- # https://eblot.github.io/pyftdi/pinout.html
- # https://html.alldatasheet.com/html-pdf/1246138/FTDI/FT232RL/937/8/FT232RL.html
- # FTDI GPIO pins mapped for SPI
- SPI_MOSI = 0x01 # TX pin used for MOSI
- SPI_MISO = 0x02 # RX pin used for MISO
- SPI_SCK = 0x04 # RTS pin used for SCK
- SPI_TMS = 0x8 # CTS pin used for CS
- CEN_PIN = 0x10 # DTR pin for CEN
- def configure_spi_pins(bitbang):
- # Set the direction of pins for SPI communication
- bitbang.direction = SPI_MOSI | SPI_SCK | CEN_PIN | SPI_TMS
- def sleep_ms(milliseconds):
- time.sleep(milliseconds / 1000)
- def sleep_us(useconds):
- time.sleep(useconds / 1000 / 1000)
- def toggle_dtr_cts(bitbang):
- # Set DTR and CTS pins high
- bitbang.port |= CEN_PIN
- # Wait or do other operations
- time.sleep(4)
- # Set DTR and CTS pins low
- bitbang.port &= ~(CEN_PIN)
- time.sleep(4)
- def bit_bang_spi_xfer(bitbang, data):
- delay = 10
- received_data = []
- bitbang.port &= ~SPI_TMS # low
- for byte in data:
- received_byte = 0
- for bit in range(8):
- # Clock pulse
- bitbang.port &= ~SPI_SCK # low
- sleep_us(delay)
- # Set MOSI based on the bit of the byte to be sent
- if byte & (1 << (7 - bit)):
- bitbang.port |= SPI_MOSI
- else:
- bitbang.port &= ~SPI_MOSI
- bitbang.port |= SPI_SCK # high
- sleep_us(delay)
- # Read MISO
- if bitbang.port & SPI_MISO:
- received_byte |= (1 << (7 - bit))
- sleep_us(delay)
- received_data.append(received_byte)
- bitbang.port |= SPI_TMS # high
- return received_data
- def ChipReset(bitbang):
- # set CEN low for 1s
- bitbang.port &= ~(CEN_PIN)
- time.sleep(1)
- bitbang.port |= CEN_PIN
- def ModeSelect(bitbang):
- #bitbang.port |= SPI_MOSI
- #bitbang.port |= SPI_SCK
- bitbang.port &= ~SPI_MOSI
- bitbang.port &= ~SPI_SCK
- sleep_ms(500);
- def DD(bitbang):
- len = 250
- print("Send 250 'D2'")
- send_buf = bytearray(len)
- for x in range(len):
- send_buf[x] = 0xD2
- a = bit_bang_spi_xfer(bitbang, send_buf)
- for x in range(len):
- print(hex(a[x]), end = '')
- print(" ", end = '')
- def BK_EnterSPIMode(bitbang):
- #ModeSelect(bitbang)
- ChipReset(bitbang)
- DD(bitbang)
- # time.sleep(0.1)
- # time.sleep(1.1)
- print("Test by sending get ID")
- cmd_id = bytearray(4)
- cmd_id[0] = 0x9F
- cmd_id[1] = 0x0
- cmd_id[2] = 0x0
- cmd_id[3] = 0x0
- a = bit_bang_spi_xfer(bitbang, cmd_id)
- for x in range(4):
- print(hex(a[x]), end = '')
- print(" ", end = '')
- # if a[0] == 0x00 and a[1] == 0x1c and a[2] == 0x70 and a[3] == 0x15:
- if a[0] == 0x00 and a[1] == 0xb and a[2] == 0x40 and a[3] == 0x15:
- print("ID OK")
- return 1
- print("ID bad")
- return 0
- def ReadStart(bitbang, startaddr, filename, readlen):
- count = 0
- addr = startaddr
- f = open(filename, "wb")
- size = readlen
- size = (size+255)//256*256
- print("Reading")
- while count < size:
- print("count "+str(count) +"/"+str(size))
- send_buf = bytearray(4+256)
- send_buf[0] = 0x03
- send_buf[1] = (addr & 0xFF0000) >> 16
- send_buf[2] = (addr & 0xFF00) >> 8
- send_buf[3] = addr & 0xFF
- result = bit_bang_spi_xfer(bitbang, send_buf)
- count += 256
- addr += 256
- part = bytearray(result[4:4+256])
- for x in range(256):
- print(hex(part[x]), end = '')
- print(" ", end = '')
- f.write(part)
- f.close()
- ChipReset(bitbang)
- return True
- if __name__ == "__main__":
- with BitBangDevice() as bb:
- bitbang = bb
- configure_spi_pins(bitbang)
- if BK_EnterSPIMode(bitbang) == 0:
- print("Failed to read flash id")
- # exit();
- ReadStart(bitbang, 0x11000,"tstReadS.bin", 0x1100)
- # while True:
- # toggle_dtr_cts(bitbang)
- # Example SPI xfer2 function call
- # data_out = [0xAA, 0xBB, 0xCC, 0xDD] # Example data to send
- # received_data = bit_bang_spi_xfer(data_out, bitbang)
- # print("Received data:", received_data)
- # Example SPI xfer function call
- # data_out = [0x12, 0x34, 0x56, 0x78] # Example data to send
- # received_data = bit_bang_spi_xfer(data_out, bitbang)
- # print("Received data:", received_data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement