@@ -29,6 +29,37 @@ void ECC508Class::end()
29
29
_wire->end ();
30
30
}
31
31
32
+ String ECC508Class::serialNumber ()
33
+ {
34
+ String result = (char *)NULL ;
35
+ byte sn[12 ];
36
+
37
+ if (!read (0 , 0 , &sn[0 ], 4 )) {
38
+ return result;
39
+ }
40
+
41
+ if (!read (0 , 2 , &sn[4 ], 4 )) {
42
+ return result;
43
+ }
44
+
45
+ if (!read (0 , 3 , &sn[8 ], 4 )) {
46
+ return result;
47
+ }
48
+
49
+ result.reserve (18 );
50
+
51
+ for (int i = 0 ; i < 8 ; i++) {
52
+ byte b = sn[i];
53
+
54
+ if (b < 16 ) {
55
+ result += " 0" ;
56
+ }
57
+ result += String (b, HEX);
58
+ }
59
+
60
+ return result;
61
+ }
62
+
32
63
int ECC508Class::random (byte data[], size_t length)
33
64
{
34
65
if (!wakeup ()) {
@@ -140,6 +171,64 @@ int ECC508Class::ecSign(int slot, const byte message[], byte signature[])
140
171
return 1 ;
141
172
}
142
173
174
+ int ECC508Class::locked ()
175
+ {
176
+ byte config[4 ];
177
+
178
+ if (!read (0 , 0x15 , config, sizeof (config))) {
179
+ return 0 ;
180
+ }
181
+
182
+ if (config[2 ] == 0x00 && config[3 ] == 0x00 ) {
183
+ return 1 ; // locked
184
+ }
185
+
186
+ return 0 ;
187
+ }
188
+
189
+ int ECC508Class::writeConfiguration (const byte data[])
190
+ {
191
+ // skip first 16 bytes, they are not writable
192
+ for (int i = 16 ; i < 128 ; i += 4 ) {
193
+ if (i == 84 ) {
194
+ // not writable
195
+ continue ;
196
+ }
197
+
198
+ if (!write (0 , i / 4 , &data[i], 4 )) {
199
+ return 0 ;
200
+ }
201
+ }
202
+
203
+ return 1 ;
204
+ }
205
+
206
+ int ECC508Class::readConfiguration (byte data[])
207
+ {
208
+ for (int i = 0 ; i < 128 ; i += 32 ) {
209
+ if (!read (0 , i / 4 , &data[i], 32 )) {
210
+ return 0 ;
211
+ }
212
+ }
213
+
214
+ return 1 ;
215
+ }
216
+
217
+ int ECC508Class::lock ()
218
+ {
219
+ // lock config
220
+ if (!lock (0 )) {
221
+ return 0 ;
222
+ }
223
+
224
+ // lock data and OTP
225
+ if (!lock (1 )) {
226
+ return 0 ;
227
+ }
228
+
229
+ return 1 ;
230
+ }
231
+
143
232
int ECC508Class::wakeup ()
144
233
{
145
234
_wire->beginTransmission (0x00 );
@@ -288,6 +377,100 @@ int ECC508Class::sign(int slot, byte signature[])
288
377
return 1 ;
289
378
}
290
379
380
+ int ECC508Class::read (int zone, int address, byte buffer[], int length)
381
+ {
382
+ if (!wakeup ()) {
383
+ return 0 ;
384
+ }
385
+
386
+ if (length != 4 && length != 32 ) {
387
+ return 0 ;
388
+ }
389
+
390
+ if (length == 32 ) {
391
+ zone |= 0x80 ;
392
+ }
393
+
394
+ if (!sendCommand (0x02 , zone, address)) {
395
+ return 0 ;
396
+ }
397
+
398
+ delay (1 );
399
+
400
+ if (!receiveResponse (buffer, length)) {
401
+ return 0 ;
402
+ }
403
+
404
+ delay (1 );
405
+ idle ();
406
+
407
+ return length;
408
+ }
409
+
410
+ int ECC508Class::write (int zone, int address, const byte buffer[], int length)
411
+ {
412
+ uint8_t status;
413
+
414
+ if (!wakeup ()) {
415
+ return 0 ;
416
+ }
417
+
418
+ if (length != 4 && length != 32 ) {
419
+ return 0 ;
420
+ }
421
+
422
+ if (length == 32 ) {
423
+ zone |= 0x80 ;
424
+ }
425
+
426
+ if (!sendCommand (0x12 , zone, address, buffer, length)) {
427
+ return 0 ;
428
+ }
429
+
430
+ delay (26 );
431
+
432
+ if (!receiveResponse (&status, sizeof (status))) {
433
+ return 0 ;
434
+ }
435
+
436
+ delay (1 );
437
+ idle ();
438
+
439
+ if (status != 0 ) {
440
+ return 0 ;
441
+ }
442
+
443
+ return 1 ;
444
+ }
445
+
446
+ int ECC508Class::lock (int zone)
447
+ {
448
+ uint8_t status;
449
+
450
+ if (!wakeup ()) {
451
+ return 0 ;
452
+ }
453
+
454
+ if (!sendCommand (0x17 , 0x80 | zone, 0x0000 )) {
455
+ return 0 ;
456
+ }
457
+
458
+ delay (32 );
459
+
460
+ if (!receiveResponse (&status, sizeof (status))) {
461
+ return 0 ;
462
+ }
463
+
464
+ delay (1 );
465
+ idle ();
466
+
467
+ if (status != 0 ) {
468
+ return 0 ;
469
+ }
470
+
471
+ return 1 ;
472
+ }
473
+
291
474
int ECC508Class::sendCommand (uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[], size_t dataLength)
292
475
{
293
476
byte command[8 + dataLength]; // 1 for type, 1 for length, 1 for opcode, 1 for param1, 2 for param2, 2 for crc
0 commit comments