39
39
for the same purpose.
40
40
*/
41
41
42
- boolean ATECCX08A::begin (uint8_t i2caddr, TwoWire &wirePort, Stream &serialPort)
42
+ bool ATECCX08A::begin (uint8_t i2caddr, TwoWire &wirePort, Stream &serialPort)
43
43
{
44
44
// Bring in the user's choices
45
45
_i2cPort = &wirePort; // Grab which port the user wants us to use
@@ -68,7 +68,7 @@ boolean ATECCX08A::begin(uint8_t i2caddr, TwoWire &wirePort, Stream &serialPort)
68
68
respond with a status, we are gonna use wakeUp() for the same purpose.
69
69
*/
70
70
71
- boolean ATECCX08A::wakeUp ()
71
+ bool ATECCX08A::wakeUp ()
72
72
{
73
73
_i2cPort->beginTransmission (0x00 ); // set up to write to address "0x00",
74
74
// This creates a "wake condition" where SDA is held low for at least tWLO
@@ -104,11 +104,30 @@ boolean ATECCX08A::wakeUp()
104
104
Note, it will automatically go into sleep mode after watchdog timer has been reached (1.3-1.7sec).
105
105
*/
106
106
107
- void ATECCX08A::idleMode ()
107
+ bool ATECCX08A::idleMode ()
108
108
{
109
109
_i2cPort->beginTransmission (_i2caddr); // set up to write to address
110
110
_i2cPort->write (WORD_ADDRESS_VALUE_IDLE); // enter idle command (aka word address - the first part of every communication to the IC)
111
- _i2cPort->endTransmission (); // actually send it
111
+ return (_i2cPort->endTransmission () == 0 ); // actually send it
112
+ }
113
+
114
+ /* * \brief
115
+
116
+ sleep()
117
+
118
+ The ATECCX08A is forcefully put into sleep (LOW POWER) mode and ignores all subsequent I/O transitions
119
+ until the next wake flag. The contents of TempKey and RNG Seed registers are NOT retained.
120
+ Idle Power Supply Current: 150nA.
121
+ This helps avoid waiting for watchdog timer and immidiately puts the device in sleep mode.
122
+ With this sleep/wakeup cycle, RNG seed registers are updated from internal entropy.
123
+ */
124
+
125
+ bool ATECCX08A::sleep ()
126
+ {
127
+ idleMode ();
128
+ _i2cPort->beginTransmission (_i2caddr); // set up to write to address
129
+ _i2cPort->write (WORD_ADDRESS_VALUE_SLEEP); // enter sleep command (aka word address - the first part of every communication to the IC)
130
+ return (_i2cPort->endTransmission () == 0 ); // actually send it
112
131
}
113
132
114
133
/* * \brief
@@ -122,7 +141,7 @@ void ATECCX08A::idleMode()
122
141
silicon revision.
123
142
*/
124
143
125
- boolean ATECCX08A::getInfo ()
144
+ bool ATECCX08A::getInfo ()
126
145
{
127
146
if (!sendCommand (COMMAND_OPCODE_INFO, 0x00 , 0x0000 )) // param1 - 0x00 (revision mode).
128
147
{
@@ -156,7 +175,7 @@ boolean ATECCX08A::getInfo()
156
175
and listens for success response (0x00).
157
176
*/
158
177
159
- boolean ATECCX08A::lockConfig ()
178
+ bool ATECCX08A::lockConfig ()
160
179
{
161
180
return lock (LOCK_MODE_ZONE_CONFIG);
162
181
}
@@ -172,7 +191,7 @@ boolean ATECCX08A::lockConfig()
172
191
This function also updates global variables for these other things.
173
192
*/
174
193
175
- boolean ATECCX08A::readConfigZone (boolean debug)
194
+ bool ATECCX08A::readConfigZone (bool debug)
176
195
{
177
196
// read block 0, the first 32 bytes of config zone into inputBuffer
178
197
read (ZONE_CONFIG, ADDRESS_CONFIG_READ_BLOCK_0, CONFIG_ZONE_READ_SIZE);
@@ -236,7 +255,7 @@ boolean ATECCX08A::readConfigZone(boolean debug)
236
255
and listens for success response (0x00).
237
256
*/
238
257
239
- boolean ATECCX08A::lockDataAndOTP ()
258
+ bool ATECCX08A::lockDataAndOTP ()
240
259
{
241
260
return lock (LOCK_MODE_ZONE_DATA_AND_OTP);
242
261
}
@@ -249,7 +268,7 @@ boolean ATECCX08A::lockDataAndOTP()
249
268
and listens for success response (0x00).
250
269
*/
251
270
252
- boolean ATECCX08A::lockDataSlot0 ()
271
+ bool ATECCX08A::lockDataSlot0 ()
253
272
{
254
273
return lock (LOCK_MODE_SLOT0);
255
274
}
@@ -262,7 +281,7 @@ boolean ATECCX08A::lockDataSlot0()
262
281
and listens for success response (0x00).
263
282
*/
264
283
265
- boolean ATECCX08A::lock (uint8_t zone)
284
+ bool ATECCX08A::lock (uint8_t zone)
266
285
{
267
286
if (!sendCommand (COMMAND_OPCODE_LOCK, zone, 0x0000 ))
268
287
return false ;
@@ -289,7 +308,7 @@ boolean ATECCX08A::lock(uint8_t zone)
289
308
290
309
/* * \brief
291
310
292
- updateRandom32Bytes(boolean debug)
311
+ updateRandom32Bytes(bool debug)
293
312
294
313
This function pulls a complete random number (all 32 bytes)
295
314
It stores it in a global array called random32Bytes[]
@@ -300,7 +319,7 @@ boolean ATECCX08A::lock(uint8_t zone)
300
319
They are getRandomByte(), getRandomInt(), and getRandomLong().
301
320
*/
302
321
303
- boolean ATECCX08A::updateRandom32Bytes (boolean debug)
322
+ bool ATECCX08A::updateRandom32Bytes (bool debug)
304
323
{
305
324
if (!sendCommand (COMMAND_OPCODE_RANDOM, 0x00 , 0x0000 ))
306
325
return false ;
@@ -343,28 +362,28 @@ boolean ATECCX08A::updateRandom32Bytes(boolean debug)
343
362
344
363
/* * \brief
345
364
346
- getRandomByte(boolean debug)
365
+ getRandomByte(bool debug)
347
366
348
367
This function returns a random byte.
349
368
It calls updateRandom32Bytes(), then uses the first byte in that array for a return value.
350
369
*/
351
370
352
- byte ATECCX08A::getRandomByte (boolean debug)
371
+ byte ATECCX08A::getRandomByte (bool debug)
353
372
{
354
373
updateRandom32Bytes (debug);
355
374
return random32Bytes[0 ];
356
375
}
357
376
358
377
/* * \brief
359
378
360
- getRandomInt(boolean debug)
379
+ getRandomInt(bool debug)
361
380
362
381
This function returns a random Int.
363
382
It calls updateRandom32Bytes(), then uses the first 2 bytes in that array for a return value.
364
383
It bitwize ORS the first two bytes of the array into the return value.
365
384
*/
366
385
367
- int ATECCX08A::getRandomInt (boolean debug)
386
+ int ATECCX08A::getRandomInt (bool debug)
368
387
{
369
388
updateRandom32Bytes (debug);
370
389
int return_val;
@@ -376,14 +395,14 @@ int ATECCX08A::getRandomInt(boolean debug)
376
395
377
396
/* * \brief
378
397
379
- getRandomLong(boolean debug)
398
+ getRandomLong(bool debug)
380
399
381
400
This function returns a random Long.
382
401
It calls updateRandom32Bytes(), then uses the first 4 bytes in that array for a return value.
383
402
It bitwize ORS the first 4 bytes of the array into the return value.
384
403
*/
385
404
386
- long ATECCX08A::getRandomLong (boolean debug)
405
+ long ATECCX08A::getRandomLong (bool debug)
387
406
{
388
407
updateRandom32Bytes (debug);
389
408
long return_val;
@@ -430,7 +449,7 @@ long ATECCX08A::random(long min, long max)
430
449
431
450
/* * \brief
432
451
433
- receiveResponseData(uint8_t length, boolean debug)
452
+ receiveResponseData(uint8_t length, bool debug)
434
453
435
454
This function receives messages from the ATECCX08a IC (up to 128 Bytes)
436
455
It will return true if it receives the correct amount of data and good CRCs.
@@ -443,7 +462,7 @@ long ATECCX08A::random(long min, long max)
443
462
It needs length argument:
444
463
length: length of data to receive (includes count + DATA + 2 crc bytes)
445
464
*/
446
- boolean ATECCX08A::receiveResponseData (uint8_t length, boolean debug)
465
+ bool ATECCX08A::receiveResponseData (uint8_t length, bool debug)
447
466
{
448
467
449
468
// pull in data 32 bytes at at time. (necessary to avoid overflow on atmega328)
@@ -470,11 +489,11 @@ boolean ATECCX08A::receiveResponseData(uint8_t length, boolean debug)
470
489
requestAmount = length; // now we're ready to pull in the last chunk.
471
490
}
472
491
473
- uint32_t ret = _i2cPort->requestFrom (_i2caddr, requestAmount); // request bytes from slave
492
+ _i2cPort->requestFrom (_i2caddr, requestAmount); // request bytes from peripheral
474
493
475
494
requestAttempts++;
476
495
477
- while (_i2cPort->available ()) // slave may send less than requested
496
+ while (_i2cPort->available ()) // peripheral may send less than requested
478
497
{
479
498
uint8_t value = _i2cPort->read ();
480
499
@@ -506,14 +525,14 @@ boolean ATECCX08A::receiveResponseData(uint8_t length, boolean debug)
506
525
507
526
/* * \brief
508
527
509
- checkCount(boolean debug)
528
+ checkCount(bool debug)
510
529
511
530
This function checks that the count byte received in the most recent message equals countGlobal
512
531
Call receiveResponseData, and then imeeditately call this to check the count of the complete message.
513
532
Returns true if inputBuffer[0] == countGlobal.
514
533
*/
515
534
516
- boolean ATECCX08A::checkCount (boolean debug)
535
+ bool ATECCX08A::checkCount (bool debug)
517
536
{
518
537
if (debug)
519
538
{
@@ -535,13 +554,13 @@ boolean ATECCX08A::checkCount(boolean debug)
535
554
536
555
/* * \brief
537
556
538
- checkCrc(boolean debug)
557
+ checkCrc(bool debug)
539
558
540
559
This function checks that the CRC bytes received in the most recent message equals a calculated CRCs
541
560
Call receiveResponseData, then call immediately call this to check the CRCs of the complete message.
542
561
*/
543
562
544
- boolean ATECCX08A::checkCrc (boolean debug)
563
+ bool ATECCX08A::checkCrc (bool debug)
545
564
{
546
565
// Check CRC[0] and CRC[1] are good to go.
547
566
atca_calculate_crc (countGlobal - CRC_SIZE, inputBuffer); // first calculate it
@@ -622,7 +641,7 @@ void ATECCX08A::cleanInputBuffer()
622
641
Sparkfun Default Configuration Sketch calls this, and then locks the data/otp zones and slot 0.
623
642
*/
624
643
625
- boolean ATECCX08A::createNewKeyPair (uint16_t slot)
644
+ bool ATECCX08A::createNewKeyPair (uint16_t slot)
626
645
{
627
646
if (!sendCommand (COMMAND_OPCODE_GENKEY, GENKEY_MODE_NEW_PRIVATE, slot))
628
647
return false ;
@@ -651,7 +670,7 @@ boolean ATECCX08A::createNewKeyPair(uint16_t slot)
651
670
652
671
/* * \brief
653
672
654
- generatePublicKey(uint16_t slot, boolean debug)
673
+ generatePublicKey(uint16_t slot, bool debug)
655
674
656
675
This function uses the GENKEY command in "Public Key Computation" mode.
657
676
@@ -664,7 +683,7 @@ boolean ATECCX08A::createNewKeyPair(uint16_t slot)
664
683
a global variable named publicKey64Bytes for later use.
665
684
*/
666
685
667
- boolean ATECCX08A::generatePublicKey (uint16_t slot, boolean debug)
686
+ bool ATECCX08A::generatePublicKey (uint16_t slot, bool debug)
668
687
{
669
688
if (!sendCommand (COMMAND_OPCODE_GENKEY, GENKEY_MODE_PUBLIC, slot))
670
689
return false ;
@@ -711,22 +730,21 @@ boolean ATECCX08A::generatePublicKey(uint16_t slot, boolean debug)
711
730
712
731
/* * \brief
713
732
714
- read(uint8_t zone, uint16_t address, uint8_t length, boolean debug)
733
+ read(uint8_t zone, uint16_t address, uint8_t length, bool debug)
715
734
716
735
Reads data from the IC at a specific zone and address.
717
736
Your data response will be available at inputBuffer[].
718
737
719
738
For more info on address encoding, see datasheet pg 58.
720
739
*/
721
740
722
- boolean ATECCX08A::read (uint8_t zone, uint16_t address, uint8_t length, boolean debug)
741
+ bool ATECCX08A::read (uint8_t zone, uint16_t address, uint8_t length, bool debug)
723
742
{
724
743
return read_output (zone, address, length, NULL , debug);
725
744
}
726
745
727
- boolean ATECCX08A::read_output (uint8_t zone, uint16_t address, uint8_t length, uint8_t * output, boolean debug)
746
+ bool ATECCX08A::read_output (uint8_t zone, uint16_t address, uint8_t length, uint8_t * output, bool debug)
728
747
{
729
- int i;
730
748
// adjust zone as needed for whether it's 4 or 32 bytes length read
731
749
// bit 7 of zone needs to be set correctly
732
750
// (0 = 4 Bytes are read)
@@ -776,7 +794,7 @@ boolean ATECCX08A::read_output(uint8_t zone, uint16_t address, uint8_t length, u
776
794
For more info on zone and address encoding, see datasheet pg 58.
777
795
*/
778
796
779
- boolean ATECCX08A::write (uint8_t zone, uint16_t address, uint8_t *data, uint8_t length_of_data)
797
+ bool ATECCX08A::write (uint8_t zone, uint16_t address, uint8_t *data, uint8_t length_of_data)
780
798
{
781
799
// adjust zone as needed for whether it's 4 or 32 bytes length write
782
800
// bit 7 of param1 needs to be set correctly
@@ -833,7 +851,7 @@ boolean ATECCX08A::write(uint8_t zone, uint16_t address, uint8_t *data, uint8_t
833
851
receives the signature and copies it to signature[].
834
852
*/
835
853
836
- boolean ATECCX08A::createSignature (uint8_t *data, uint16_t slot)
854
+ bool ATECCX08A::createSignature (uint8_t *data, uint16_t slot)
837
855
{
838
856
if (!loadTempKey (data) || !signTempKey (slot))
839
857
return false ;
@@ -855,7 +873,7 @@ boolean ATECCX08A::createSignature(uint8_t *data, uint16_t slot)
855
873
when it requests data, and this will allow us to create a unique data + signature for every communication.
856
874
*/
857
875
858
- boolean ATECCX08A::loadTempKey (uint8_t *data)
876
+ bool ATECCX08A::loadTempKey (uint8_t *data)
859
877
{
860
878
if (!sendCommand (COMMAND_OPCODE_NONCE, NONCE_MODE_PASSTHROUGH, 0x0000 , data, 32 ))
861
879
return false ;
@@ -891,7 +909,7 @@ boolean ATECCX08A::loadTempKey(uint8_t *data)
891
909
The response from this command (the signature) is stored in global varaible signature[].
892
910
*/
893
911
894
- boolean ATECCX08A::signTempKey (uint16_t slot)
912
+ bool ATECCX08A::signTempKey (uint16_t slot)
895
913
{
896
914
if (!sendCommand (COMMAND_OPCODE_SIGN, SIGN_MODE_TEMPKEY, slot))
897
915
return false ;
@@ -939,7 +957,7 @@ boolean ATECCX08A::signTempKey(uint16_t slot)
939
957
Note, it acutally uses loadTempKey, then uses the verify command in "external public key" mode.
940
958
*/
941
959
942
- boolean ATECCX08A::verifySignature (uint8_t *message, uint8_t *signature, uint8_t *publicKey)
960
+ bool ATECCX08A::verifySignature (uint8_t *message, uint8_t *signature, uint8_t *publicKey)
943
961
{
944
962
uint8_t data_sigAndPub[128 ];
945
963
@@ -975,7 +993,7 @@ boolean ATECCX08A::verifySignature(uint8_t *message, uint8_t *signature, uint8_t
975
993
return true ;
976
994
}
977
995
978
- boolean ATECCX08A::sha256 (uint8_t * plain, size_t len, uint8_t * hash)
996
+ bool ATECCX08A::sha256 (uint8_t * plain, size_t len, uint8_t * hash)
979
997
{
980
998
int i;
981
999
size_t chunks = len / SHA_BLOCK_SIZE + !!(len % SHA_BLOCK_SIZE);
@@ -991,7 +1009,6 @@ boolean ATECCX08A::sha256(uint8_t * plain, size_t len, uint8_t * hash)
991
1009
for (i = 0 ; i < chunks; ++i)
992
1010
{
993
1011
size_t data_size = SHA_BLOCK_SIZE;
994
- uint8_t chunk[SHA_BLOCK_SIZE];
995
1012
996
1013
delay (9 );
997
1014
@@ -1051,11 +1068,11 @@ boolean ATECCX08A::sha256(uint8_t * plain, size_t len, uint8_t * hash)
1051
1068
Returns true if write commands were successful.
1052
1069
*/
1053
1070
1054
- boolean ATECCX08A::writeConfigSparkFun ()
1071
+ bool ATECCX08A::writeConfigSparkFun ()
1055
1072
{
1056
1073
// keep track of our write command results.
1057
- boolean result1;
1058
- boolean result2;
1074
+ bool result1;
1075
+ bool result2;
1059
1076
1060
1077
// set keytype on slot 0 and 1 to 0x3300
1061
1078
// Lockable, ECC, PuInfo set (public key always allowed to be generated), contains a private Key
@@ -1084,7 +1101,7 @@ boolean ATECCX08A::writeConfigSparkFun()
1084
1101
So those specific transmissions are handled in unique functions.
1085
1102
*/
1086
1103
1087
- boolean ATECCX08A::sendCommand (uint8_t command_opcode, uint8_t param1, uint16_t param2, uint8_t *data, size_t length_of_data)
1104
+ bool ATECCX08A::sendCommand (uint8_t command_opcode, uint8_t param1, uint16_t param2, uint8_t *data, size_t length_of_data)
1088
1105
{
1089
1106
// build packet array (total_transmission) to send a communication to IC, with opcode COMMAND
1090
1107
// It expects to see: word address, count, command opcode, param1, param2, data (optional), CRC[0], CRC[1]
@@ -1129,7 +1146,5 @@ boolean ATECCX08A::sendCommand(uint8_t command_opcode, uint8_t param1, uint16_t
1129
1146
1130
1147
_i2cPort->beginTransmission (_i2caddr);
1131
1148
_i2cPort->write (total_transmission, total_transmission_length);
1132
- _i2cPort->endTransmission ();
1133
-
1134
- return true ;
1149
+ return (_i2cPort->endTransmission () == 0 );
1135
1150
}
0 commit comments