-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDallasEPROM.cpp
454 lines (372 loc) · 9.58 KB
/
DallasEPROM.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
// Maxim/Dallas 1-Wire EPROM & EEPROM library for Arduino
// Copyright (C) 2011-2017 Eric Hokanson
// https://github.com/pceric
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
#include "DallasEPROM.h"
/** Supported chips. */
model_type _chip_model_list[] = {
// EPROMs
{ 0x09, "DS2502", 4, 2, true },
{ 0x0B, "DS2505", 64, 2, true },
// EEPROMs
{ 0x14, "DS2430", 1, 1, false },
{ 0x2D, "DS2431", 4, 2, false },
{ 0x23, "DS2433", 16, 2, false },
{ 0, 0, 0, 0, 0 }
};
DallasEPROM::DallasEPROM(OneWire* rWire) {
_wire = rWire;
_progPin = -1;
}
DallasEPROM::DallasEPROM(OneWire* rWire, int progPin) {
_wire = rWire;
_progPin = progPin;
pinMode(progPin, OUTPUT);
digitalWrite(progPin, LOW);
}
/*******************
* Static methods
*******************/
bool DallasEPROM::validAddress(uint8_t* deviceAddress) {
return (OneWire::crc8(deviceAddress, 7) == deviceAddress[7]);
}
bool DallasEPROM::isSupported(uint8_t* deviceAddress) {
int i = 0;
while (_chip_model_list[i].id) {
if (deviceAddress[0] == _chip_model_list[i].id)
return true;
++i;
}
return false;
}
/*******************
* Public methods
*******************/
bool DallasEPROM::search() {
int i;
_curModelIndex = -1;
if (!_wire->reset())
return false;
_wire->reset_search();
while (_wire->search(_addr)) {
i = 0;
while (_chip_model_list[i].id) {
if (_addr[0] == _chip_model_list[i].id) {
_curModelIndex = i;
_curModel = &_chip_model_list[i];
return true;
}
++i;
}
}
return false;
}
uint8_t* DallasEPROM::getAddress() {
return _addr;
}
void DallasEPROM::setAddress(uint8_t* pAddress) {
int i = 0;
_curModelIndex = -1;
memcpy(_addr, pAddress, 8);
while (_chip_model_list[i].id) {
if (_addr[0] == _chip_model_list[i].id) {
_curModelIndex = i;
_curModel = &_chip_model_list[i];
return;
}
++i;
}
}
const char* DallasEPROM::getDeviceName() {
if (_curModelIndex >= 0)
return _curModel->name;
else
return NULL;
}
bool DallasEPROM::isConnected() {
uint8_t tmpAddress[8];
if (!_wire->reset())
return false;
_wire->reset_search();
while (_wire->search(tmpAddress)) {
if (memcmp(_addr, tmpAddress, 8)==0)
return true;
}
return false;
}
int DallasEPROM::readPage(uint8_t* data, int page) {
unsigned int address = page * 32;
if (!isPageValid(page))
return INVALID_PAGE;
if (!isConnected())
return DEVICE_DISCONNECTED;
// check for page redirection
if (isEPROMDevice()) {
byte command[] = { READSTATUS, (byte)(page+1), 0x00 };
byte new_addr;
_wire->reset();
_wire->select(_addr);
_wire->write(command[0]);
_wire->write(command[1]);
_wire->write(command[2]);
if (OneWire::crc8(command, 3) != _wire->read())
return CRC_MISMATCH;
if ((new_addr = _wire->read()) != 0xFF)
address = new_addr;
}
byte command[] = { READMEMORY, (byte) address, (byte)(address >> 8) };
// send the command and starting address
_wire->reset();
_wire->select(_addr);
for (int i = 0; i <= _curModel->addrSize; i++) {
_wire->write(command[i]);
}
// Check CRC on EPROM devices
if (isEPROMDevice() && OneWire::crc8(command, 3) != _wire->read())
return CRC_MISMATCH;
// Read the entire page
for (int i = 0; i < 32; i++) {
data[i] = _wire->read();
}
// TODO: On EPROM device you can check the CRC post read
return 0;
}
int DallasEPROM::writePage(uint8_t* data, int page) {
unsigned int address = page * 32;
if (!isPageValid(page))
return INVALID_PAGE;
if (!isConnected())
return DEVICE_DISCONNECTED;
// EEPROMS have a difference write method than EPROMS
if (!isEPROMDevice()) {
int status;
// a page is 4 8-byte scratch writes
for (int i = 0; i < 32; i += 8) {
if ((status = scratchWrite(&data[i], 8, address + i)))
return status;
}
return 0;
}
byte command[] = { WRITEMEMORY, (byte) address, (byte)(address >> 8),
data[0] };
// send the command, address, and the first byte
_wire->reset();
_wire->select(_addr);
_wire->write(command[0]);
_wire->write(command[1]);
_wire->write(command[2]);
_wire->write(command[3]);
// Check CRC
if (OneWire::crc8(command, 4) != _wire->read())
return CRC_MISMATCH;
// Issue programming pulse for the first byte
if (_progPin >= 0) {
digitalWrite(_progPin, HIGH);
delayMicroseconds(500);
digitalWrite(_progPin, LOW);
}
delayMicroseconds(500);
// Check the first byte for proper burn
if (command[3] != _wire->read())
return COPY_FAILURE;
// write out the rest of the page
for (int i = 1; i < 32; i++) {
// Write byte
_wire->write(data[i]);
// Check CRC
_wire->read(); // FIXME: The EPROM calculates a CRC based on 9 bits, we can't do that with OneWire
//byte crc[] = { (byte)((address+i) & 0x01), data[i] };
//if (OneWire::crc8(crc, 2) != _wire->read())
// return CRC_MISMATCH;
// Issue programming pulse
if (_progPin >= 0) {
digitalWrite(_progPin, HIGH);
delayMicroseconds(500);
digitalWrite(_progPin, LOW);
}
delayMicroseconds(500);
// Check for proper burn
if (data[i] != _wire->read())
return COPY_FAILURE;
}
return 0;
}
int DallasEPROM::lockPage(int page) {
if (!isPageValid(page))
return INVALID_PAGE;
if (!isConnected())
return DEVICE_DISCONNECTED;
_wire->reset();
_wire->select(_addr);
if (isEPROMDevice()) {
byte command[] = { WRITESTATUS, 0x00, 0x00, (byte)(1 << page) };
_wire->write(command[0]);
_wire->write(command[1]);
_wire->write(command[2]);
_wire->write(command[3]);
// Check CRC
if (OneWire::crc8(command, 4) != _wire->read())
return CRC_MISMATCH;
// Issue programming pulse
if (_progPin >= 0) {
digitalWrite(_progPin, HIGH);
delayMicroseconds(500);
digitalWrite(_progPin, LOW);
}
delayMicroseconds(500);
// TODO: Verify data
} else {
unsigned int start;
byte data[] = { 0x55 }; // write protect
start = _curModel->pages * 32 + page;
scratchWrite(data, 1, start);
}
return 0;
}
bool DallasEPROM::isPageLocked(int page) {
byte status;
if (!isPageValid(page))
return INVALID_PAGE;
if (!isConnected())
return DEVICE_DISCONNECTED;
_wire->reset();
_wire->select(_addr);
if (isEPROMDevice()) {
byte command[] = { READSTATUS, 0x00, 0x00 };
_wire->write(command[0]);
_wire->write(command[1]);
_wire->write(command[2]);
// Check CRC on EPROM devices
if (OneWire::crc8(command, 3) != _wire->read())
return CRC_MISMATCH;
status = _wire->read();
_wire->reset();
return 1 & (status >> page);
} else {
unsigned int start;
start = _curModel->pages * 32 + page;
_wire->write(READMEMORY);
_wire->write((byte)start);
_wire->write((byte)(start >> 8));
if (_wire->read() == 0x55)
return true;
else
return false;
}
}
int DallasEPROM::readAppReg(uint8_t* data) {
if (!isConnected())
return DEVICE_DISCONNECTED;
if (_curModel->name != "DS2430")
return UNSUPPORTED_OPP;
_wire->reset();
_wire->select(_addr);
_wire->write(READMEMORYCRC);
_wire->write(0x00);
for (int i = 0; i < 8; i++) {
data[i] = _wire->read();
}
return 0;
}
int DallasEPROM::writeAppReg(uint8_t* data) {
if (!isConnected())
return DEVICE_DISCONNECTED;
if (_curModel->name != "DS2430")
return UNSUPPORTED_OPP;
// Are we locked?
if (isAppRegLocked()) {
return APP_REG_LOCKED;
}
_wire->reset();
_wire->select(_addr);
_wire->write(WRITEAPPREG);
_wire->write(0x00);
for (int i = 0; i < 8; i++) {
_wire->write(data[i]);
}
_wire->reset();
_wire->select(_addr);
_wire->write(COPYLOCK);
_wire->write(VERIFYRESUME);
// Need 10ms prog delay
delay(10);
return 0;
}
bool DallasEPROM::isAppRegLocked() {
if (_curModel->name != "DS2430")
return true;
_wire->reset();
_wire->select(_addr);
_wire->write(READSTATUSREG);
_wire->write(0x00);
if (_wire->read() == 0xFC) {
return true;
}
return false;
}
/*******************
* Private methods
*******************/
int DallasEPROM::scratchWrite(uint8_t* data, int length, unsigned int address) {
byte auth[3];
// send the command and address
_wire->reset();
_wire->select(_addr);
_wire->write(WRITEMEMORY);
_wire->write((byte) address);
if (_curModel->addrSize > 1) {
_wire->write((byte)(address >> 8));
}
// write "length" bytes to the scratchpad
for (int i = 0; i < length; i++)
_wire->write(data[i]);
if (_curModel->name == "DS2430") {
// Issue copy scratchpad with DS2430 auth byte
_wire->reset();
_wire->select(_addr);
_wire->write(WRITESTATUS);
_wire->write(VERIFYRESUME);
} else {
// Read the auth code from the scratchpad and verify integrity
_wire->reset();
_wire->select(_addr);
_wire->write(READSTATUS);
_wire->read_bytes(auth, 3);
for (int i = 0; i < length; i++) {
if (_wire->read() != data[i])
return BAD_INTEGRITY;
}
// Issue copy scratchpad with auth bytes
_wire->reset();
_wire->select(_addr);
_wire->write(WRITESTATUS);
_wire->write(auth[0]);
_wire->write(auth[1]);
_wire->write(auth[2], 1);
}
// Need 10ms prog delay
delay(10);
_wire->depower();
// Check for success
if (_curModel->name != "DS2430" && _wire->read() != 0xAA)
return COPY_FAILURE;
return 0;
}
bool DallasEPROM::isPageValid(int page) {
if (_curModelIndex >= 0 && page < _curModel->pages)
return true;
return false;
}
bool DallasEPROM::isEPROMDevice() {
if (_curModelIndex >= 0 && _curModel->isEPROM == true)
return true;
return false;
}
/** @file */