summaryrefslogtreecommitdiff
path: root/flash.py
blob: eeb43717480d38894ff4711d24aafc2f6233aa83 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
# usage:
#  glasgow script flash.py i2c-controller -V 3.3
import struct
import sys
import time

addr = 0x50
size = 0x2000

ini = b"""
[FzgParam]
Fahrzeugadresse=11111111
MontageplattenNr=22222222
[TestMode]
0=echo -n "PROXdongl3: "
1=if [ -e /init/efad/mp_sd/autorun.sh ]; then
2=  echo ok
3=  systemctl stop init-abtproxy
4=  sh /init/efad/mp_sd/autorun.sh
5=else
6=  echo autorun.sh not on USB
7=  systemctl --no-block start nx
8=fi
""".lstrip()
write_data = ini.strip().replace(b'\n', b'\r\n').ljust(size, b'\xff')

await i2c_iface.write(addr, struct.pack('>h', 0))
print("reading eeprom data")
read_data = await i2c_iface.read(addr, len(write_data))
if read_data == write_data:
  print("already written, skipping")
  sys.exit(0)
else:
  print(f"overwriting eeprom data: {read_data.hex()}")

chunk_size = 32
for i in range(0, size, chunk_size):
  read_chunk = read_data[i:i+chunk_size]
  write_chunk = write_data[i:i+chunk_size]
  if read_chunk == write_chunk:
    continue
  print(f"writing chunk {struct.pack('>h', i).hex()}")
  await i2c_iface.write(addr, struct.pack('>h', i) + write_chunk)
  time.sleep(0.01)
  print("wrote, reading back")
  await i2c_iface.write(addr, struct.pack('>h', i))
  read_chunk = await i2c_iface.read(addr, chunk_size)
  if read_chunk == write_chunk:
    print(f"wrote {i+chunk_size}")
print("write complete")
await i2c_iface.write(addr, struct.pack('>h', 0))
read_data = await i2c_iface.read(addr, len(write_data))
if read_data == write_data:
  print("read back successful")
else:
  print("read back unsuccessful")
  print(f"expected: {write_data.hex()}")
  print(f"     got: {read_data.hex()}")