Blame view

qspi_loader-master/tools/enable_qspi_AT25SF641.py 1.08 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #!/usr/bin/env python3
  
  import serial
  import time
  import sys
  
  # serprog constants
  ACK = b'\x06'
  O_SPIOP = b'\x13'
  
  # flash chip constants
  READ_SR2 = b'\x35'
  WRITE_ENABLE = b'\x06'
  WRITE_SR2 = b'\x31'
  
  def send_recieve(ser, data_in, data_out_length):
    ser.write(O_SPIOP + len(data_in).to_bytes(3, 'little') + data_out_length.to_bytes(3, 'little') + data_in)
    ack = ser.read(1)
    if ack != ACK:
      print('Communication Error')
      exit(1)
    return ser.read(data_out_length)
  
  device = '/dev/ttyACM0'
  if len(sys.argv) >= 2:
    device = sys.argv[1]
  
  ser = serial.Serial(device)
  print('Using: ' + ser.name)
  
  old_sr2 = send_recieve(ser, READ_SR2, 1)
  print('Status Register 2 before = ' + str(old_sr2))
  
  new_sr2 = bytes([old_sr2[0] | 0x00000002])
  print('Programming Status Register 2 with ' + str(new_sr2))
  send_recieve(ser, WRITE_ENABLE, 0)
  send_recieve(ser, WRITE_SR2 + new_sr2, 0)
  
  time.sleep(1)
  programmed_sr2 = send_recieve(ser, READ_SR2, 1)
  print('Status Register 2 after = ' + str(programmed_sr2))
  
  if programmed_sr2 == new_sr2:
    print('Programming done')
  else:
    print('Programming error')
  
  ser.close()