-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom.py
87 lines (72 loc) · 1.92 KB
/
eeprom.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
cs = 0
so = 1
si = 2
sck = 3
GPIO.setup(cs, GPIO.OUT)
GPIO.setup(si, GPIO.OUT)
GPIO.setup(sck, GPIO.OUT)
GPIO.setup(so, GPIO.IN)
def send(val):
bits = []
for _ in range(8):
bits.append(val & 1)
val = val >> 1
bits.reverse()
send_bits(bits)
def send_bits(bits):
for bit in bits:
GPIO.output(sck, 0)
GPIO.output(si, bit)
GPIO.output(sck, 1)
GPIO.output(sck, 0)
def rcv_bits(size):
bits = []
for _ in range(size):
GPIO.output(sck, 1)
bits.append(GPIO.input(so))
GPIO.output(sck, 0)
return bits
def enable_write():
GPIO.output(cs, 0)
send(0b00000110)
GPIO.output(cs, 1)
def write_bits(bits):
assert len(bits) == 1024
for addr in range(0, 128, 16):
enable_write()
GPIO.output(cs, 0)
send(0b00000010)
send(addr)
send_bits(bits[addr*8:(addr+16)*8])
GPIO.output(cs, 1)
sleep(10/1000)
assert read_bits(0, 16*8) == bits[:16*8]
def read_bits(addr, size):
GPIO.output(cs, 0)
send(0b00000011)
send(addr)
bits = rcv_bits(size)
GPIO.output(cs, 1)
return bits
r_cs = [1] + [0]*40 + [1] + [0]*72 + [1]*2 + [0]*40 + [1] + [0]*32 + [1]*2 + [0]*48 + [1]
r_cs = r_cs * 4
r_cs = r_cs + [1]*(1024 - len(r_cs))
sio_cs = [0]*114 + [1] + [0]*74 + [1] + [0]*49 + [1]
sio_cs = sio_cs * 4
sio_cs = sio_cs + [1]*(1024 - len(sio_cs))
sio = [0]*7 + [1]*2 + [0]*22 + [1] + [0]*24 + [1]*2 + [0]*14 + [1] + [0]*7 + [1] + [0]*14 + [1]*2 + [0] + [1] + [0]*6 + [1]*2 + [0]*13 + [1] + [0]*2
sio = sio * 4
sio = sio + [0]*20
sio = sio + sio
sio_hold = [1]*25 + [0]*17 + [1]*24 + [0]*49 + [1]*25 + [0]*17 + [1]*24 + [0]*9 + [1]*25 + [0]*25
sio_hold = sio_hold * 4
sio_hold = sio_hold + [0]*44 + [1]*20
try:
GPIO.output(cs, 1)
#write_bits(sio_hold)
print(read_bits(0, 1024))
finally:
GPIO.cleanup()