Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from hexdump import hexdump
  3. import array
  4. import struct
  5. import sys
  6. import usb1
  7. import usb.core
  8. import usb.util
  9. import binascii
  10. '''
  11. # DspiEnableEx: 3 6 0 <1: port request>
  12. # DspiDisable: 3 6 1 <1: port request>
  13. # DspiSetSpeed: 7 6 3 <1: active port> <4: speed>
  14. # DspiGetSpeed: 3 6 4 <1: active port>
  15. # DspiSetSpiMode: 4 6 5 <1: active port> <1: mode?>
  16. # DspiSetSelect: 4 6 6 <1: active port> <1: select state>
  17. # DspiPut: 10 6 7 <1: active port> <1: fSelStart> <1: fSelEnd>
  18. # <1: should receive?> <4: number of bytes>
  19. # (fSelStart, fSelEnd, rgbSnd, rgbRcv, cbSend, fOverlap)
  20.  
  21. self.ep_cmdout = usb.util.find_descriptor(intf, bEndpointAddress = 1)
  22. self.ep_cmdin = usb.util.find_descriptor(intf, bEndpointAddress = 0x82)
  23. self.ep_dataout = usb.util.find_descriptor(intf, bEndpointAddress = 3)
  24. self.ep_datain = usb.util.find_descriptor(intf, bEndpointAddress = 0x84)
  25. '''
  26.  
  27. ENABLE=0
  28. DISABLE=1
  29. GETSPEED=4
  30. SETSPEED=3
  31.  
  32. STAT_BUSY = 0x1
  33. STAT_WEL = 0x2
  34.  
  35. CMD_GET_STATUS = 0x05
  36. CMD_WRITE_ENABLE = 0x6
  37. CMD_READ_ID = 0x9F
  38. CMD_WAKEUP = 0xAB
  39. CMD_CHIP_ERASE = 0xC7
  40. CMD_PAGE_PROGRAM = 0x02
  41. CMD_FAST_READ = 0xB
  42. CMD_RFSR=0x70
  43. #flash protocols
  44. class SPI(object):
  45.  
  46. STAT_BUSY=1
  47. def __init__(self, port):
  48. self.port=port
  49. self.dev = usb.core.find(idVendor=0x1443, idProduct=0x0007)
  50. self.dev.reset()
  51. self.dev.set_configuration()
  52.  
  53.  
  54. cfg = self.dev.get_active_configuration()
  55. intf = usb.util.find_descriptor(cfg)
  56.  
  57.  
  58. self.ep_cmdout = usb.util.find_descriptor(intf, bEndpointAddress = 1)
  59. self.ep_cmdin = usb.util.find_descriptor(intf, bEndpointAddress = 0x82)
  60. self.ep_dataout = usb.util.find_descriptor(intf, bEndpointAddress = 3)
  61. self.ep_datain = usb.util.find_descriptor(intf, bEndpointAddress = 0x84)
  62.  
  63. self.ENABLED=0
  64.  
  65. def __return_bytes(self, num=16):
  66. return bytes(self.ep_cmdin.read(num))
  67.  
  68. def enable(self):
  69. self.ep_cmdout.write(struct.pack('BBBB', 3, 6, 0, self.port))
  70. self.ENABLED=1
  71. return self.__return_bytes()[1:]
  72. #talk to flash through spi protocols
  73. #talk to flash through spi protocols
  74.  
  75. def setSpeed(self, speed):
  76. self.ep_cmdout.write(struct.pack('BBBBI', 7,6,3,0, speed))
  77. return self.__return_bytes()[1:]
  78.  
  79. def getSpeed(self):
  80. self.ep_cmdout.write(struct.pack('BBBB', 3, 6, 4, self.port))
  81. return self.__return_bytes()[2:]
  82.  
  83. def setMode(self, mode,sh=False):
  84. self.ep_cmdout.write(struct.pack('BBBBBB', 4,6,5,self.port,mode,sh))
  85. return self.__return_bytes()
  86.  
  87.  
  88. def io(self, write_bytes, read_byte_count=0):
  89. write_bytes=list(write_bytes)
  90. if len(write_bytes) < read_byte_count:
  91. write_bytes.extend([0]*(read_byte_count-len(write_bytes)))
  92. write_b_count=len(write_bytes)
  93. read_bytes=[]
  94.  
  95.  
  96. # Do the IO
  97. while write_bytes or len(read_bytes) < read_byte_count:
  98. if write_bytes:
  99. self.ep_dataout.write(write_bytes[:64])
  100. write_bytes = write_bytes[64:]
  101.  
  102. if read_byte_count:
  103. to_read = min(64, read_byte_count)
  104. read_bytes.extend(self.ep_datain.read(to_read))
  105. return read_bytes
  106.  
  107.  
  108. def put(self, b, fSelStart=0, fSelEnd=1):
  109. rcv = 1
  110. self.ep_cmdout.write(struct.pack("BBBBBBBI", 10, 6, 7, 0, fSelStart, fSelEnd, rcv, len(b)))
  111. self.ep_dataout.write(b)
  112. #hexdump(self.ep_cmdin.read(0x10))
  113.  
  114. def get(self, nbytes, fSelStart=0, fSelEnd=1, bfill=1):
  115. rcv=1
  116. self.ep_cmdout.write(struct.pack("BBBBBBBI", 10,6,8,0,fSelStart, fSelEnd, rcv, nbytes))
  117. hexdump(self.ep_cmdin.read(0x10))
  118. self.__return_bytes(0x1000)
  119.  
  120. def bulk_erase(self):
  121. self.put(b'\x06')
  122.  
  123. #self.waitDone()
  124.  
  125. def write_to(self,fn_):
  126. data=fn_.read()
  127. for addr in range(0, len(data), 256):
  128. buf=data[addr:addr+256]
  129. pageProgram(addr, buf)
  130.  
  131. def pageProgram(self,addr,buf):
  132. assert len(buf) <= 256
  133. assert addr & 0xff==0
  134. #self.io([0x02, (addr>>16) & 0xFF, (addr>>8)&0xff, addr&0xff] + list(buf))
  135.  
  136. def getStatus(self):
  137. return self.io(b'/0x05',2)[1]
  138.  
  139. def write_enable(self):
  140. self.put(bytes(CMD_WRITE_ENABLE))
  141. def read(self, addr):
  142. inp=bytearray([CMD_FAST_READ, (addr>>16)&0xff, (addr>>8)& 0xFF, addr&0xff])
  143. self.put(inp)
  144. hexdump(self.ep_datain.read(100))
  145. def read_id(self):
  146. self.put(b'\x9e' + b'\x00' * 10)
  147. hexdump(self.ep_cmdin.read(0x10))
  148.  
  149. def waitDone(self):
  150. while self.getStatus() & self.STAT_BUSY:
  151. pass
  152.  
  153. def get_board(self):
  154. r=bytes(self.dev.ctrl_transfer(0xc4, 0xe2,0x00,0x00,16))#read 16 bytes over endpoint0
  155. return r[:r.index(b'\x00')].decode('utf-8')
  156.  
  157.  
  158. if __name__=="__main__":
  159. s=SPI(0)
  160. s.get_board()
  161. s.write_enable()
  162. s.setMode(0)
  163. s.read_id()
  164.  
  165. '''
  166. # 06 WREN p88 BULK ERASE c7 p79
  167. '''
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement