Skip to content

Commit a4b10fa

Browse files
authored
Merge pull request #53 from pennam/counter-support-changes
Add monotonic counter support
2 parents aa3932f + 6b15f79 commit a4b10fa

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
ECCX08 Counter
3+
4+
This sketch uses the ECC508 or ECC608 to increment a monotonic
5+
counter at each startup
6+
7+
Circuit:
8+
- Any board with ECC508 or ECC608 on board
9+
10+
*/
11+
12+
#include <ArduinoECCX08.h>
13+
14+
const int keyId = 5;
15+
long counter = -1;
16+
17+
void setup() {
18+
Serial.begin(9600);
19+
while (!Serial);
20+
21+
if (!ECCX08.begin()) {
22+
Serial.println("Failed to communicate with ECC508/ECC608!");
23+
while (1);
24+
}
25+
26+
if (!ECCX08.incrementCounter(keyId, counter)) {
27+
Serial.println("Failed to increment counter");
28+
while (1);
29+
}
30+
}
31+
32+
void loop() {
33+
if (!ECCX08.readCounter(keyId, counter)) {
34+
Serial.println("Failed to read counter");
35+
while (1);
36+
}
37+
38+
Serial.print("Counter value = ");
39+
Serial.println(counter);
40+
41+
delay(1000);
42+
}
43+

src/ECCX08.cpp

+75-1
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,80 @@ int ECCX08Class::nonce(const byte data[])
548548
return challenge(data);
549549
}
550550

551+
int ECCX08Class::incrementCounter(int counterId, long& counter)
552+
{
553+
if (counterId < 0 || counterId > 1) {
554+
return 0;
555+
}
556+
557+
if (!wakeup()) {
558+
return 0;
559+
}
560+
561+
if (!sendCommand(0x24, 1, counterId)) {
562+
return 0;
563+
}
564+
565+
delay(20);
566+
567+
if (!receiveResponse(&counter, sizeof(counter))) {
568+
return 0;
569+
}
570+
571+
delay(1);
572+
idle();
573+
574+
return 1;
575+
}
576+
577+
long ECCX08Class::incrementCounter(int counterId)
578+
{
579+
long counter; // the counter can go up to 2,097,151
580+
581+
if(!incrementCounter(counterId, counter)) {
582+
return -1;
583+
}
584+
585+
return counter;
586+
}
587+
588+
int ECCX08Class::readCounter(int counterId, long& counter)
589+
{
590+
if (counterId < 0 || counterId > 1) {
591+
return 0;
592+
}
593+
594+
if (!wakeup()) {
595+
return 0;
596+
}
597+
598+
if (!sendCommand(0x24, 0, counterId)) {
599+
return 0;
600+
}
601+
602+
delay(20);
603+
604+
if (!receiveResponse(&counter, sizeof(counter))) {
605+
return 0;
606+
}
607+
608+
delay(1);
609+
idle();
610+
611+
return 1;
612+
}
613+
614+
long ECCX08Class::readCounter(int counterId)
615+
{
616+
long counter; // the counter can go up to 2,097,151
617+
618+
if(!readCounter(counterId, counter)) {
619+
return -1;
620+
}
621+
622+
return counter;
623+
}
624+
551625
int ECCX08Class::wakeup()
552626
{
553627
_wire->setClock(_wakeupFrequency);
@@ -892,4 +966,4 @@ uint16_t ECCX08Class::crc16(const byte data[], size_t length)
892966
ECCX08Class ECCX08(CRYPTO_WIRE, 0x60);
893967
#else
894968
ECCX08Class ECCX08(Wire, 0x60);
895-
#endif
969+
#endif

src/ECCX08.h

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class ECCX08Class
6666

6767
int nonce(const byte data[]);
6868

69+
int incrementCounter(int counterId, long& counter);
70+
long incrementCounter(int counterId);
71+
int readCounter(int counterId, long& counter);
72+
long readCounter(int counterId);
73+
6974
private:
7075
int wakeup();
7176
int sleep();

0 commit comments

Comments
 (0)