-
Notifications
You must be signed in to change notification settings - Fork 0
/
eeprom_93cx6.py
291 lines (251 loc) · 8.32 KB
/
eeprom_93cx6.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""eeprom_93cx6
A micropython library to interface with EEPROM 93cx6 series.
"""
from utime import sleep_us as usleep
from machine import Pin
__DEBUG = False
def debug(tag, content):
if __DEBUG:
print(f"{tag}: {content}")
DELAY_CS = 0
DELAY_READ = 1
DELAY_WRITE = 1
DELAY_WAIT = 1
EEPROM_MODE_8BIT = 1
EEPROM_MODE_16BIT = 2
class OP:
CONTROL: int = 0x00
WRITE: int = 0x01
READ: int = 0x02
ERASE: int = 0x03
class CC:
EW_DISABLE: int = 0x00
WRITE_ALL: int = 0x01
ERASE_ALL: int = 0x02
EW_ENABLE: int = 0x03
class Device:
"""A class represents 93cx6 chip.
VCC
| NC(Don't Care)
| | TEST(Don't Care)
| | | GND
| | | |
__|__|__|__|__
| | | | | |
| 8 7 6 5 |
| |
|.1 2 3 4 |
|_|__|__|__|_|
| | | |
| | | DO(Data Out)
| | DI(Data In)
| SK(Serial clocK)
CS(Chip Select)
"""
def __init__(self, model, cs, sk, di, do, org=EEPROM_MODE_16BIT):
self.model = model
if model not in [46, 56, 66, 76, 86]:
raise ValueError(
f"Device.model mismatch({model}): supported model are 46, 56, 66, 76, 86."
)
self.org = org
if org not in [EEPROM_MODE_8BIT, EEPROM_MODE_16BIT]:
raise ValueError(
f"Device.org must be `EEPROM_MODE_8BIT` or `EEPROM_MODE_16BIT`, given value: {org}"
)
self.cs = Pin(cs, Pin.OUT)
self.sk = Pin(sk, Pin.OUT)
self.di = Pin(di, Pin.OUT)
self.do = Pin(do, Pin.IN)
self.bytes = Device.get_bytes_by_model(org, model)
self.addr = Device.get_addr_by_model(org, model)
self.mask = Device.get_mask_by_model(org, model)
self._ew = False
@staticmethod
def get_bytes_by_model(org: int, model: int) -> int:
if org == EEPROM_MODE_8BIT:
byte = 128
elif org == EEPROM_MODE_16BIT:
byte = 64
if model == 56: # 256/128
byte *= 2
elif model == 66: # 512/256
byte *= 4
elif model == 76: # 1024/512
byte *= 8
elif model == 86: # 2048/1024
byte *= 16
return byte
@staticmethod
def get_addr_by_model(org: int, model: int) -> int:
if org == EEPROM_MODE_8BIT:
addr = 7
elif org == EEPROM_MODE_16BIT:
addr = 6
if model == 56 or model == 66: # 9/8
addr += 2
if model == 76 or model == 86: # 11/10
addr += 4
return addr
@staticmethod
def get_mask_by_model(org: int, model: int) -> int:
if org == EEPROM_MODE_8BIT:
mask = 0x7F
elif org == EEPROM_MODE_16BIT:
mask = 0x3F
if model == 56 or model == 66: # 0x1ff/0xff
mask = (mask << 2) + 0x03
if model == 76 or model == 86: # 0x7ff/0x3ff
mask = (mask << 4) + 0x0F
return mask
def validate_addr(self, addr):
"""Validate if given address is in range of memory space"""
if addr < 0 or addr > (self.bytes - 1): # address is 0-origin
raise ValueError(f"Address exceeds maximum address({self.addr:04x})")
def send_bits(self, value: int, length: int):
"""Send value through a DI pin"""
for i in range(length - 1, -1, -1):
send_bit = value & 1 << i
if send_bit:
self.di.on()
else:
self.di.off()
usleep(DELAY_WRITE)
self.sk.on()
usleep(DELAY_WRITE)
self.sk.off()
usleep(DELAY_WRITE)
def wait_ready(self):
"""Wait for EEPROM to verify a written value"""
debug("wait_ready", "setting cs to high")
self.cs.on()
debug("wait_ready", "set cs to high, waiting dev_do to low")
while self.do.value() != 1:
usleep(DELAY_WAIT)
self.cs.off()
def ew_enable(self):
"""Enable Erase/Write feature(EWEN)"""
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1)
self.send_bits(
OP.CONTROL << self.addr | CC.EW_ENABLE << (self.addr - 2), self.addr + 2
)
self.cs.off()
self._ew = True
def ew_disable(self) -> None:
"""Disable Erase/Write feature(EWDS)"""
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bit
self.send_bits(
OP.CONTROL << self.addr | CC.EW_ENABLE << (self.addr - 2), self.addr + 2
)
self.cs.off()
self._ew = False
def ew_enabled(self) -> bool:
"""Return if Erase/Write is enabled"""
return self._ew
def erase_all(self) -> None:
"""Perform a ERASE ALL feature"""
if not self.ew_enabled():
return
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bit
self.send_bits(
OP.CONTROL << self.addr | CC.ERASE_ALL << (self.addr - 2), self.addr + 2
)
self.cs.off()
self.wait_ready()
def erase(self, addr: int) -> None:
"""Erase a value of given address"""
if not self.ew_enabled():
return
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bit
self.send_bits(OP.ERASE << self.addr | (addr & self.mask), self.addr + 2)
self.cs.off()
self.wait_ready()
def write_all(self, value):
"""Perform a WRITE ALL feature with given value"""
if not self.ew_enabled():
return
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bit
self.send_bits(
OP.CONTROL << self.addr | CC.WRITE_ALL << (self.addr - 2), self.addr + 2
)
if self.org == EEPROM_MODE_16BIT:
self.send_bits(0xFFFF & value, 16)
elif self.org == EEPROM_MODE_8BIT:
self.send_bits(0xFF & value, 8)
self.cs.off()
self.wait_ready()
def write(self, addr: int, value: int) -> None:
"""Write value to given address"""
if not self.ew_enabled():
return
self.validate_addr(addr)
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bits
self.send_bits(OP.WRITE << self.addr | (addr & self.mask), self.addr + 2)
if self.org == EEPROM_MODE_16BIT:
self.send_bits(0xFFFF & value, 16)
elif self.org == EEPROM_MODE_8BIT:
self.send_bits(0xFF & value, 8)
self.cs.off()
self.wait_ready()
def read(self, addr: int) -> int:
"""Read a value of given address from EEPROM"""
self.validate_addr(addr)
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bit
self.send_bits(OP.READ << self.addr | (addr & self.mask), self.addr + 2)
if self.org == EEPROM_MODE_16BIT:
num_bits = 16
elif self.org == EEPROM_MODE_8BIT:
num_bits = 8
read_value = 0
for i in range(num_bits, 0, -1):
self.sk.on()
usleep(DELAY_READ)
read_bit = self.do.value()
self.sk.off()
usleep(DELAY_READ)
read_value = read_value | (read_bit << (i - 1))
self.cs.off()
return read_value
def read_sequential(self, addr: int, length: int):
"""Perform a sequential read of given address and length.
Returns a list of values.
If address exceeds maximum memory adderss, this function will stop and
return a list at that time."""
self.validate_addr(addr)
arr = []
self.cs.on()
usleep(DELAY_CS)
self.send_bits(1, 1) # start bits
self.send_bits(OP.READ << self.addr | (addr & self.mask), self.addr + 2)
if self.org == EEPROM_MODE_16BIT:
num_bits = 16
elif self.org == EEPROM_MODE_8BIT:
num_bits = 8
for count in range(length):
read_value = 0
if addr + count > self.bytes:
break
for i in range(num_bits, 0, -1):
self.sk.on()
usleep(DELAY_READ)
read_bit = self.do.value()
self.sk.off()
usleep(DELAY_READ)
read_value = read_value | (read_bit << (i - 1))
arr.append(read_value)
self.cs.off()
return arr