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
- # 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
- 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
- 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):
- received_data = []
- for byte in data:
- received_byte = 0
- for bit in range(8):
- # 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
- # Clock pulse
- bitbang.port |= SPI_SCK
- bitbang.port &= ~SPI_SCK
- # Read MISO
- if bitbang.port & SPI_MISO:
- received_byte |= (1 << (7 - bit))
- received_data.append(received_byte)
- return received_data
- def ChipReset(bitbang):
- # set CEN low for 1s
- bitbang.port &= ~(CEN_PIN)
- time.sleep(1)
- bitbang.port |= CEN_PIN
- def BK_EnterSPIMode(bitbang):
- ChipReset(bitbang)
- print("Send 250 'D2'")
- send_buf = bytearray(250)
- for x in range(250):
- send_buf[x] = 0xD2
- a = bit_bang_spi_xfer(bitbang, send_buf)
- for x in range(250):
- print(hex(a[x]), end = '')
- print(" ", end = '')
- time.sleep(0.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:
- print("ID OK")
- return 1
- print("ID bad")
- return 0
- 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();
- # 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