Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. def read_from_crypto():
  2. data = []
  3. firsttwo = mcp2221.I2C_Read(ATECC608A_addr,0x02)
  4. sleep(0.02)
  5. if firsttwo == -1: #crytpo is not responding
  6. return(0,0xFE,False)
  7.  
  8. bytecount = firsttwo[0] #1 byte
  9. status = firsttwo[1]
  10. print("bytecount:",bytecount)
  11. print("firsttwo:",firsttwo)
  12.  
  13. if bytecount < 11: # we have read 2, can read up to another 8
  14. data_crc = mcp2221.I2C_Read(ATECC608A_addr, bytecount - 0x02)
  15. sleep(0.02)
  16.  
  17. else: # read 8 bytes
  18. data_crc = list(mcp2221.I2C_Read(ATECC608A_addr, 0x08))
  19. sleep(0.02)
  20. # this will loop 'i' times getting bytes at a time.
  21. for i in range((bytecount-10)//8):
  22. data_crc.extend(mcp2221.I2C_Read(ATECC608A_addr, 0x08))
  23. sleep(0.02)
  24. if ((bytecount-10) % 0x08) > 0:
  25. data_crc.extend(mcp2221.I2C_Read(ATECC608A_addr, (bytecount-2) % 0x08))
  26.  
  27. print(bytecount-2, data_crc)
  28. crc_msb = data_crc[len(data_crc)-1] << 8
  29. crc_lsb = data_crc[len(data_crc)-2]
  30. rx_crc = crc_msb + crc_lsb
  31. data = data_crc[0:-2]
  32. #print("bytecount:\t" + str(hex(bytecount[0])))
  33.  
  34.  
  35. ## compute teh CRC ##
  36.  
  37. computearray = firsttwo + data
  38. reversedarray = []
  39. for byte in computearray:
  40. reversedarray.append(reflect_byte(byte))
  41.  
  42. crc16 = crcmod.mkCrcFun(0x18005,0x00,False,0x00)
  43. crc_calc = crc16(bytearray(reversedarray))
  44.  
  45. if crc_calc == rx_crc:
  46. crc_state = True
  47. else:
  48. crc_state = False
  49.  
  50. return(status,data,crc_state)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement