This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspEEPROM.cpp
521 lines (414 loc) · 10.4 KB
/
WaspEEPROM.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*
* Based on library by Rob Tillaart
* URL: http://arduino.cc/playground/Main/LibraryForI2CEEPROM
* VERSION: 1.0.05
*
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program 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 program 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.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.0
* Design: David Gascon
* Implementation: Yuri Carmona
*/
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
#include <WaspEEPROM.h>
WaspEEPROM::WaspEEPROM()
{
_deviceAddress = I2C_ADDRESS_EEPROM;
_lastWrite = 0;
TWBR = 12; // 12=400Khz 32=200 72=100 152=50 F_CPU/16+(2*TWBR)
}
uint8_t WaspEEPROM::ON()
{
uint8_t error;
// init I2C bus
if (!Wire.isON)
{
Wire.begin();
}
error = waitReady();
// check correct comm
error = readJEDEC();
if (error == 0)
{
if ((_response[0] == 0x00) && (_response[1] == 0x1F))
{
return 0;
}
return 1;
}
else
{
// set default I2C address
_deviceAddress = I2C_ADDRESS_EEPROM_DEFAULT;
// generate new register value
uint8_t new_reg_value = 0;
new_reg_value = I2C_ADDRESS_EEPROM << 1;
new_reg_value++;
// set new I2C address
eeprom.writeByte(0xF040, new_reg_value);
// reset eeprom chip
reset();
// go back to current I2C address
_deviceAddress = I2C_ADDRESS_EEPROM;
return error;
}
return error;
}
int WaspEEPROM::writeByte(uint16_t address, uint8_t data)
{
int rv = _WriteBlock(address, &data, 1);
return rv;
}
int WaspEEPROM::setBlock(uint16_t address, uint8_t data, uint16_t length)
{
uint8_t buffer[I2C_TWIBUFFERSIZE];
for (uint8_t i =0; i< I2C_TWIBUFFERSIZE; i++) buffer[i] = data;
int rv = _pageBlock(address, buffer, length, false); // todo check return value..
return rv;
}
int WaspEEPROM::writeBlock(uint16_t address, uint8_t* buffer, uint16_t length)
{
int rv = _pageBlock(address, buffer, length, true); // todo check return value..
return rv;
}
uint8_t WaspEEPROM::readByte(uint16_t address)
{
uint8_t rdata;
_ReadBlock(address, &rdata, 1);
return rdata;
}
uint16_t WaspEEPROM::readBlock(uint16_t address, uint8_t* buffer, uint16_t length)
{
uint16_t rv = 0;
while (length > 0)
{
uint8_t cnt = min(length, I2C_TWIBUFFERSIZE);
rv += _ReadBlock(address, buffer, cnt);
address += cnt;
buffer += cnt;
length -= cnt;
}
return rv;
}
#ifdef I2C_EEPROM_EXTENDED
// returns 64, 32, 16, 8, 4, 2, 1, 0
// 0 is smaller than 1K
uint8_t WaspEEPROM::determineSize()
{
uint8_t rv = 0; // unknown
uint8_t orgValues[8];
uint16_t addr;
// remember old values, non destructive
for (uint8_t i=0; i<8; i++)
{
addr = (512 << i) + 1;
orgValues[i] = readByte(addr);
}
// scan page folding
for (uint8_t i=0; i<8; i++)
{
rv = i;
uint16_t addr1 = (512 << i) + 1;
uint16_t addr2 = (512 << (i+1)) + 1;
writeByte(addr1, 0xAA);
writeByte(addr2, 0x55);
if (readByte(addr1) == 0x55) // folded!
{
break;
}
}
// restore original values
for (uint8_t i=0; i<8; i++)
{
uint16_t addr = (512 << i) + 1;
writeByte(addr, orgValues[i]);
}
return 0x01 << (rv-1);
}
#endif
////////////////////////////////////////////////////////////////////
//
// PRIVATE
//
// _pageBlock aligns buffer to page boundaries for writing.
// and to TWI buffer size
// returns 0 = OK otherwise error
int WaspEEPROM::_pageBlock(uint16_t address, uint8_t* buffer, uint16_t length, bool incrBuffer)
{
int rv = 0;
while (length > 0)
{
uint8_t bytesUntilPageBoundary = I2C_EEPROM_PAGESIZE - address%I2C_EEPROM_PAGESIZE;
uint8_t cnt = min(length, bytesUntilPageBoundary);
cnt = min(cnt, I2C_TWIBUFFERSIZE);
int rv = _WriteBlock(address, buffer, cnt); // todo check return value..
if (rv != 0) return rv;
address += cnt;
if (incrBuffer) buffer += cnt;
length -= cnt;
}
return rv;
}
// pre: length <= I2C_EEPROM_PAGESIZE && length <= I2C_TWIBUFFERSIZE;
// returns 0 = OK otherwise error
int WaspEEPROM::_WriteBlock(uint16_t address, uint8_t* buffer, uint8_t length)
{
waitReady();
Wire.beginTransmission(_deviceAddress);
Wire.send((int)(address >> 8));
Wire.send((int)(address & 0xFF));
for (uint8_t cnt = 0; cnt < length; cnt++)
Wire.send(buffer[cnt]);
int rv = Wire.endTransmission();
_lastWrite = millis();
return rv;
}
// pre: buffer is large enough to hold length bytes
// returns bytes written
uint8_t WaspEEPROM::_ReadBlock(uint16_t address, uint8_t* buffer, uint8_t length)
{
waitReady();
Wire.beginTransmission(_deviceAddress);
Wire.send((int)(address >> 8));
Wire.send((int)(address & 0xFF));
int rv = Wire.endTransmission();
if (rv != 0) return 0; // error
Wire.requestFrom(_deviceAddress, length);
uint8_t cnt = 0;
uint32_t before = millis();
while ((cnt < length) && ((millis() - before) < I2C_EEPROM_TIMEOUT))
{
if (Wire.available()) buffer[cnt++] = Wire.receive();
}
return cnt;
}
// pre: buffer is large enough to hold length bytes
// returns bytes written
uint8_t WaspEEPROM::readRegister(uint16_t address, uint8_t* buffer, uint8_t length)
{
// wait until chip is ready
waitReady();
Wire.beginTransmission(_deviceAddress);
Wire.send((int)(address >> 8));
Wire.send((int)(address & 0xFF));
Wire.requestFrom(_deviceAddress, length);
uint8_t cnt = 0;
uint32_t before = millis();
while ((cnt < length) && ((millis() - before) < I2C_EEPROM_TIMEOUT))
{
if (Wire.available()) buffer[cnt++] = Wire.receive();
}
uint8_t rv = Wire.endTransmission();
delay(10);
if (rv != 0) return 1; // error
return 0;
}
/*************************************************************
*
*
*
*
*
*
*************************************************************/
uint8_t WaspEEPROM::sendCommand(uint8_t* command)
{
int retries = 3;
int x;
uint8_t crc_tx[2];
uint8_t crc_rx[2];
uint8_t crc_aux[2];
uint8_t length = command[0]-2;
uint8_t rx_length = 16;
do
{
// calculate CRC
aes132c_calculate_crc(length, command, crc_tx);
// wait until chip is ready
waitReady();
Wire.beginTransmission(_deviceAddress);
Wire.send(0xFE); // CMD REGISTER ADDRESS
Wire.send(0x00);
for (uint8_t i = 0; i<length; i++)
{
Wire.send(command[i]); //COUNT
}
Wire.send(crc_tx[0]); //CRC 1
Wire.send(crc_tx[1]); //CRC 2
x = Wire.endTransmission();
if (x != 0)
{
//USB.println(F("Error endTransmission"));
}
delay(100);
retries--;
if (retries == 0) return 1;
}
while(x != 0);
// read response
x = Wire.requestFrom(_deviceAddress, rx_length);
if (x != 0)
{
return 1;
}
// Store response
uint16_t index = 0;
while (Wire.available() && (index < sizeof(_buffer)))
{
_buffer[index] = Wire.receive();
index++;
}
// Stop transmission
x = Wire.endTransmission();
if (x != 0)
{
return 1;
}
// Parse response
uint8_t count = _buffer[0];
//~ uint8_t success = _buffer[1];
_length = 0;
for (int i = 2; i < (count-2); i++)
{
_response[i-2] = _buffer[i];
_length++;
}
crc_rx[0] = _buffer[count-2];
crc_rx[1] = _buffer[count-1];
// check crc
aes132c_calculate_crc(count-2, _buffer, crc_aux);
delay(10);
if (memcmp( crc_rx, crc_aux, 2) == 0)
{
return 0;
}
else
{
return 1;
}
}
/*************************************************************
*
*
*
*
*
*
*************************************************************/
uint8_t WaspEEPROM::sleepCommand()
{
uint8_t crc[2];
int x;
uint8_t command[] = { 0x09, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00 };
uint8_t length = command[0]-2;
// calculate CRC
aes132c_calculate_crc(length, command, crc);
/*USB.printHex(crc[0]);
USB.printHex(crc[1]);
USB.println();*/
Wire.beginTransmission(_deviceAddress);
Wire.send(0xFE); // CMD REGISTER ADDRESS
Wire.send(0x00);
for (int x = 0; x<length; x++)
{
Wire.send(command[x]);
}
Wire.send(crc[0]); //CRC 1
Wire.send(crc[1]); //CRC 2
x = Wire.endTransmission();
if (x != 0)
{
//USB.println("Error endTransmission_sleep");
return 1;
}
delay(10);
return 0;
}
/*
*
*
*
*
*/
uint8_t WaspEEPROM::waitReady()
{
uint32_t I2C_WRITEDELAY = 5000;
// Wait until EEPROM gives ACK again.
// this is a bit faster than the hardcoded 5 milli
while ((millis() - _lastWrite) <= I2C_WRITEDELAY)
{
Wire.beginTransmission(_deviceAddress);
int x = Wire.endTransmission();
if (x == 0)
{
return 0;
}
}
return 1;
}
/* readSerialNumber() - reads the Waspmote unique serial identifier from eeprom
*
* It reads the Waspmote unique serial identifier
*/
uint8_t WaspEEPROM::readSerialNumber()
{
uint8_t error = 0;
// define command
uint8_t I2C_EEPROM_READ_SERIAL[] = {0x09, 0x10, 0x00, 0xF0, 0x00, 0x00, 0x08};
error = eeprom.sendCommand(I2C_EEPROM_READ_SERIAL);
return error;
}
/*
*
*/
uint8_t WaspEEPROM::readJEDEC()
{
uint8_t error = 0;
// define command
uint8_t I2C_EEPROM_READ_JEDEC[] = { 0x09, 0x10, 0x00, 0xF0, 0x10, 0x00, 0x02};
error = eeprom.sendCommand(I2C_EEPROM_READ_JEDEC);
return error;
}
/*
*
*/
uint8_t WaspEEPROM::reset()
{
uint8_t error = 0;
// define command
uint8_t I2C_EEPROM_RESET[] = { 0x09, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00};
error = eeprom.sendCommand(I2C_EEPROM_RESET);
return error;
}
/*
*
*/
uint8_t WaspEEPROM::blockRead(uint16_t address, uint8_t length)
{
uint8_t error = 0;
// define command
uint8_t I2C_EEPROM_BLOCK_READ[] = {0x09, 0x10, 0x00, 0xFF, 0xFF, 0x00, 0xFF};
I2C_EEPROM_BLOCK_READ[3] = (address>>8) & 0xFF;
I2C_EEPROM_BLOCK_READ[4] = address & 0xFF;
I2C_EEPROM_BLOCK_READ[6] = length;
error = eeprom.sendCommand(I2C_EEPROM_BLOCK_READ);
return error;
}
WaspEEPROM eeprom = WaspEEPROM();
//
// END OF FILE
//