-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjm_24xx512.spin
277 lines (163 loc) · 6.63 KB
/
jm_24xx512.spin
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
'' =================================================================================================
''
'' File....... jm_24xx512.spin
'' Purpose.... R/W routines for 24xx512 (64K) EEPROM
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2009-2014 Jon McPhalen
'' -- see below for terms of use
'' E-mail..... [email protected]
'' Started....
'' Updated.... 08 JUN 2014
''
'' =================================================================================================
con { i2c address }
EE_WRITE = %1010_000_0
EE_READ = %1010_000_1
PG_SIZE = 128
ACK = i2c#ACK
NAK = i2c#NAK
con { fixed io pins }
RX1 = 31 ' programming / debug port
TX1 = 30
EE_SDA = 29 ' eeprom
EE_SCL = 28
obj
i2c : "jm_i2c"
var
long scl
long sda
long devid
pub start(device)
'' Setup I2C using default (boot EEPROM) pins
'' -- device is the device address %000 - %111
'' * %000 is boot eeprom
startx(EE_SCL, EE_SDA, device)
pub startx(sclpin, sdapin, device)
'' Define I2C SCL (clock) and SDA (data) pins
i2c.setupx(sclpin, sdapin)
devid := EE_WRITE | ((%000 #> device <# %111) << 1)
pub wait
'' Wait for EEPROM to be ready for new command
i2c.wait(devid)
con
{ --------------------------- }
{ W R I T E M E T H O D S }
{ --------------------------- }
pub wr_byte(addr, b)
'' Write byte to eeprom
return wr_block(addr, 1, @b)
pub wr_word(addr, w)
'' Write word to eeprom
return wr_block(addr, 2, @w)
pub wr_long(addr, l)
'' Write long to eeprom
return wr_block(addr, 4, @l)
pub wr_block(addr, n, p_src) | ackbit
'' Write block of n bytes from p_src to eeprom
'' -- be mindful of address/page size in device to prevent page wrap-around
i2c.wait(devid)
i2c.write(addr.byte[1]) ' msb of address
i2c.write(addr.byte[0]) ' lsb of address
ackbit := i2c#ACK ' assume okay
repeat n
ackbit |= i2c.write(byte[p_src++]) ' write a byte
i2c.stop
return ackbit
pub wr_str(addr, p_zstr) | ackbit, b
'' Write z-string at p_zstr to eeprom
'' -- string can cross page boundary
ackbit := i2c#ACK ' assume okay
repeat
b := byte[p_zstr++] ' get byte from string
ackbit |= wr_byte(addr++, b) ' write
if (b == 0) ' end of string?
quit ' yes, we're done
return ackbit
pub fill(addr, n, b) | ackbit
'' Write byte b to eeprom n times, starting with addr
'' -- be mindful of address/page size in device to prevent page wrap-around
i2c.wait(devid)
i2c.write(addr.byte[1]) ' msb of address
i2c.write(addr.byte[0]) ' lsb of address
ackbit := i2c#ACK ' assume okay
repeat n
ackbit |= i2c.write(b) ' write the byte
i2c.stop
return ackbit
pub copy_to(addr, n, p_src)
'' Copies n bytes from p_src to eeprom addr
'' -- not affected by page boundaries
repeat n
wr_byte(addr++, byte[p_src++])
con
{ ------------------------- }
{ R E A D M E T H O D S }
{ ------------------------- }
pub rd_byte(addr) | b
'' Return byte value from eeprom
rd_block(addr, 1, @b)
return b & $FF ' clean-up { local var }
pub rd_word(addr) | w
'' Return word value eeprom
rd_block(addr, 2, @w)
return w & $FFFF
pub rd_long(addr) | l
'' Return long value from eeprom
rd_block(addr, 4, @l)
return l
pub rd_block(addr, n, p_dest)
'' Read block of n bytes from eeprom to address at p_dest
'' -- be mindful of address/page size in device to prevent page wrap-around
i2c.wait(devid)
i2c.write(addr.byte[1]) ' msb of address
i2c.write(addr.byte[0]) ' lsb of address
i2c.start ' restart for read
i2c.write(devid | $01) ' device read
repeat while (n > 1)
byte[p_dest++] := i2c.read(i2c#ACK)
--n
byte[p_dest] := i2c.read(i2c#NAK) ' last byte gets NAK
i2c.stop
pub rd_str(addr, p_dest) | b
'' Read (arbitrary-length) z-string, store at p_dest
'' -- string can cross page boundary
repeat
b := rd_byte(addr++) ' read byte from device
byte[p_dest++] := b ' write to destination
if (b == 0) ' at end?
quit ' if yes, we're done
pub copy_from(addr, n, p_dest)
'' Copies n bytes from eeprom to p_dest
'' -- not affected by page boundaries
repeat n
byte[p_dest++] := rd_byte(addr++)
con
{ --------------- }
{ S U P P O R T }
{ --------------- }
pub page_num(addr)
'' Returns page # of addr
return (addr / PG_SIZE)
pub page_ok(addr, len) | pg0, pg1
'' Returns true if len bytes will fit into current page
pg0 := page_num(addr)
pg1 := page_num(addr + len-1)
return (pg1 == pg0)
dat { license }
{{
Terms of Use: MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}