-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.ino
142 lines (117 loc) · 3.5 KB
/
Program.ino
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
/*
* MIT License
* -----
*
* Copyright (c) 2023 Matt Borja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* - The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Helpers.h"
// TODO: Sanitize & release
#include "_Config.h"
#include "_ATECCX08A.h"
void setup()
{
Serial.begin(SERIAL_BAUD_RATE);
// Required by ATECCX08A.begin(), else board will fail with error: Error Status: 0x80FF013D Code: 317 Module: 255
Wire.begin();
Wire.setClock(WIRE_CLOCK_FREQ);
}
/*
* Executes arbitrary commands against ATECCX08 at I2C address specified
* input: [addr:1][opcode:1][param1:1][param2:2][outlen:1][datalen:1][data:datalen] (datalen=0-249)
*/
bool ateccx08_cmd(uint8_t input[256], bool debug = false)
{
Serial.println();
ATECCX08A atecc;
uint8_t addr = input[0];
if (debug)
{
Serial.print("ateccx08_cmd@0x");
Serial.print(addr, HEX);
Serial.print("> ");
}
if (!atecc.begin(addr))
{
if (debug)
Serial.println("CONNECT_FAIL");
return false;
}
if (debug)
Serial.println("CONNECT_OK");
// ===
uint8_t data[249] = {};
uint8_t res[128] = {}; // "up to 128 bytes"
uint8_t opcode = input[1];
uint8_t param1 = input[2];
uint16_t param2 = ((input[3] << 8) | input[4]);
size_t outlen = input[5] < 129 ? input[5] : 128;
size_t datalen = input[6] < 250 ? input[6] : 249;
memcpy(data, input+7, datalen);
if (debug)
{
Serial.print("_raw:\t\t");
Helpers::xxd(input, 256);
Serial.print("send:\topcode=0x");
Serial.print(opcode, HEX);
Serial.print(", param1=0x");
Serial.print(param1, HEX);
Serial.print(", param2=0x");
Serial.print(param2, HEX);
Serial.print(", outlen=0x");
Serial.print(outlen, HEX);
Serial.print(", datalen=0x");
Serial.print(datalen, HEX);
Serial.print(", data=");
Helpers::xxd(data, datalen);
}
// Protocol
bool status;
status = atecc.exec(opcode, param1, param2, (datalen > 0 ? data : NULL), datalen, outlen);
if (status)
memcpy(res, atecc.res, outlen);
if (debug)
{
Serial.print("result:\t\tstatus=");
Serial.print(status ? "OK" : "FAIL");
Serial.print(", res=");
}
if (status)
{
Helpers::xxd(res, outlen);
return true;
}
Serial.println();
return false;
}
void ateccx08_serial_loop()
{
while (Serial.available())
{
uint8_t line[512] = {};
uint8_t parsed[256] = {};
uint8_t count = Serial.readBytes(line, 512);
Helpers::parse_hex_bytes(parsed, line, 512);
ateccx08_cmd(parsed, true);
}
}
void loop() {
ateccx08_serial_loop();
}