Skip to content

Commit b428681

Browse files
committed
Firmware updater: restructure code
1 parent d02c88b commit b428681

File tree

1 file changed

+127
-146
lines changed

1 file changed

+127
-146
lines changed

examples/node_flasher_raw/node_flasher_raw.ino renamed to examples/FirmwareUpdater/FirmwareUpdater.ino

Lines changed: 127 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,122 @@
66

77
// https://www.st.com/resource/en/application_note/an4221-i2c-protocol-used-in-the-stm32-bootloader-stmicroelectronics.pdf
88

9-
int howmany;
10-
int res;
9+
bool flash(const uint8_t* binary, size_t lenght, bool verbose = true);
10+
11+
void setup() {
12+
Serial.begin(115200);
13+
Wire1.begin();
14+
Wire1.setClock(400000);
15+
16+
// Send reset to the module; remember, connect only ONE module at a time
17+
if (sendReset() != 0) {
18+
Serial.println("Send reset failed");
19+
}
20+
21+
auto result = flash(node_base_bin, node_base_bin_len);
22+
if (result) {
23+
matrixInitAndDraw("PASS");
24+
} else {
25+
matrixInitAndDraw("FAIL");
26+
}
27+
}
28+
29+
void loop() {
30+
// put your main code here, to run repeatedly:
31+
}
32+
33+
class SerialVerbose {
34+
public:
35+
SerialVerbose(bool verbose) : _verbose(verbose) {}
36+
int print(String s) {
37+
if (_verbose) {
38+
Serial.print(s);
39+
}
40+
}
41+
int println(String s) {
42+
if (_verbose) {
43+
Serial.println(s);
44+
}
45+
}
46+
int println(int num, int base) {
47+
if (_verbose) {
48+
Serial.println(num, base);
49+
}
50+
}
51+
private:
52+
bool _verbose;
53+
};
1154

1255
ArduinoLEDMatrix matrix;
1356

57+
void matrixInitAndDraw(char* text) {
58+
matrix.begin();
59+
matrix.beginDraw();
60+
61+
matrix.stroke(0xFFFFFFFF);
62+
matrix.textFont(Font_4x6);
63+
matrix.beginText(0, 1, 0xFFFFFF);
64+
matrix.println(text);
65+
matrix.endText();
66+
67+
matrix.endDraw();
68+
}
69+
70+
bool flash(const uint8_t* binary, size_t lenght, bool verbose) {
71+
72+
SerialVerbose SerialDebug(verbose);
73+
74+
uint8_t resp_buf[255];
75+
int resp;
76+
SerialDebug.println("GET_COMMAND");
77+
resp = command(0, nullptr, 0, resp_buf, 20, verbose);
78+
79+
if (resp < 0) {
80+
SerialDebug.println("Failed :(");
81+
return false;
82+
}
83+
84+
for (int i = 0; i < resp; i++) {
85+
SerialDebug.println(resp_buf[i], HEX);
86+
}
87+
88+
SerialDebug.println("GET_ID");
89+
resp = command(2, nullptr, 0, resp_buf, 3, verbose);
90+
for (int i = 0; i < resp; i++) {
91+
SerialDebug.println(resp_buf[i], HEX);
92+
}
93+
94+
SerialDebug.println("GET_ID");
95+
resp = command(2, nullptr, 0, resp_buf, 3, verbose);
96+
for (int i = 0; i < resp; i++) {
97+
SerialDebug.println(resp_buf[i], HEX);
98+
}
99+
100+
SerialDebug.println("MASS_ERASE");
101+
uint8_t erase_buf[3] = { 0xFF, 0xFF, 0x0 };
102+
resp = command(0x44, erase_buf, 3, nullptr, 0, verbose);
103+
for (int i = 0; i < resp; i++) {
104+
SerialDebug.println(resp_buf[i], HEX);
105+
}
106+
107+
for (int i = 0; i < lenght; i += 128) {
108+
SerialDebug.print("WRITE_PAGE ");
109+
SerialDebug.println(i, HEX);
110+
uint8_t write_buf[5] = { 8, 0, i / 256, i % 256 };
111+
resp = command_write_page(0x32, write_buf, 5, &binary[i], 128, verbose);
112+
for (int i = 0; i < resp; i++) {
113+
SerialDebug.println(resp_buf[i], HEX);
114+
}
115+
delay(10);
116+
}
117+
return true;
118+
}
119+
120+
int howmany;
121+
int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const uint8_t* buf_fw, size_t len_fw, bool verbose) {
122+
123+
SerialVerbose SerialDebug(verbose);
14124

15-
int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const uint8_t* buf_fw, size_t len_fw) {
16125
uint8_t cmd[2];
17126
cmd[0] = opcode;
18127
cmd[1] = 0xFF ^ opcode;
@@ -27,8 +136,8 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
27136
Wire1.requestFrom(100, 1);
28137
auto c = Wire1.read();
29138
if (c != 0x79) {
30-
Serial.print("error first ack: ");
31-
Serial.println(c, HEX);
139+
SerialDebug.print("error first ack: ");
140+
SerialDebug.println(c, HEX);
32141
return -1;
33142
}
34143
Wire1.beginTransmission(100);
@@ -44,8 +153,8 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
44153
c = Wire1.read();
45154
}
46155
if (c != 0x79) {
47-
Serial.print("error second ack: ");
48-
Serial.println(c, HEX);
156+
SerialDebug.print("error second ack: ");
157+
SerialDebug.println(c, HEX);
49158
return -1;
50159
}
51160
}
@@ -66,17 +175,19 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
66175
c = Wire1.read();
67176
}
68177
if (c != 0x79) {
69-
Serial.print("error: ");
70-
Serial.println(c, HEX);
178+
SerialDebug.print("error: ");
179+
SerialDebug.println(c, HEX);
71180
return -1;
72181
}
73182
}
74183
final_ack:
75184
return howmany + 1;
76185
}
77186

187+
int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp, size_t len_resp, bool verbose) {
188+
189+
SerialVerbose SerialDebug(verbose);
78190

79-
int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp, size_t len_resp) {
80191
uint8_t cmd[2];
81192
cmd[0] = opcode;
82193
cmd[1] = 0xFF ^ opcode;
@@ -102,11 +213,11 @@ int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp,
102213
delay(100);
103214
Wire1.requestFrom(100, 1);
104215
c = Wire1.read();
105-
Serial.print("retry");
216+
SerialDebug.println("retry");
106217
}
107218
if (c != 0x79) {
108-
Serial.print("error second ack: ");
109-
Serial.println(c, HEX);
219+
SerialDebug.print("error second ack: ");
220+
SerialDebug.println(c, HEX);
110221
return -1;
111222
}
112223
}
@@ -123,8 +234,8 @@ int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp,
123234
Wire1.requestFrom(100, 1);
124235
c = Wire1.read();
125236
if (c != 0x79) {
126-
Serial.print("error: ");
127-
Serial.println(c, HEX);
237+
SerialDebug.print("error: ");
238+
SerialDebug.println(c, HEX);
128239
return -1;
129240
}
130241
final_ack:
@@ -145,134 +256,4 @@ int sendReset() {
145256
}
146257
}
147258
return ret;
148-
}
149-
150-
void setup() {
151-
Serial.begin(115200);
152-
Wire1.begin();
153-
Wire1.setClock(400000);
154-
155-
if (sendReset() != 0) {
156-
Serial.println("Send reset failed");
157-
}
158-
159-
uint8_t resp_buf[255];
160-
int resp;
161-
Serial.println("GET_COMMAND");
162-
resp = command(0, nullptr, 0, resp_buf, 20);
163-
164-
if (resp < 0) {
165-
Serial.println("Failed :(");
166-
matrix.begin();
167-
matrix.beginDraw();
168-
169-
matrix.stroke(0xFFFFFFFF);
170-
matrix.textScrollSpeed(100);
171-
172-
const char text[] = "FAIL";
173-
matrix.textFont(Font_4x6);
174-
matrix.beginText(0, 1, 0xFFFFFF);
175-
matrix.println(text);
176-
matrix.endText();
177-
178-
matrix.endDraw();
179-
while (1)
180-
;
181-
}
182-
183-
for (int i = 0; i < resp; i++) {
184-
Serial.println(resp_buf[i], HEX);
185-
}
186-
187-
Serial.println("GET_ID");
188-
resp = command(2, nullptr, 0, resp_buf, 3);
189-
for (int i = 0; i < resp; i++) {
190-
Serial.println(resp_buf[i], HEX);
191-
}
192-
193-
Serial.println("GET_ID");
194-
resp = command(2, nullptr, 0, resp_buf, 3);
195-
for (int i = 0; i < resp; i++) {
196-
Serial.println(resp_buf[i], HEX);
197-
}
198-
199-
Serial.println("MASS_ERASE");
200-
uint8_t erase_buf[3] = { 0xFF, 0xFF, 0x0 };
201-
resp = command(0x44, erase_buf, 3, nullptr, 0);
202-
for (int i = 0; i < resp; i++) {
203-
Serial.println(resp_buf[i], HEX);
204-
}
205-
206-
for (int i = 0; i < node_base_bin_len; i += 128) {
207-
Serial.print("WRITE_PAGE ");
208-
Serial.println(i, HEX);
209-
uint8_t write_buf[5] = { 8, 0, i / 256, i % 256 };
210-
resp = command_write_page(0x32, write_buf, 5, &node_base_bin[i], 128);
211-
for (int i = 0; i < resp; i++) {
212-
Serial.println(resp_buf[i], HEX);
213-
}
214-
delay(10);
215-
}
216-
217-
matrix.begin();
218-
matrix.beginDraw();
219-
220-
matrix.stroke(0xFFFFFFFF);
221-
matrix.textScrollSpeed(100);
222-
223-
const char text[] = "PASS";
224-
matrix.textFont(Font_4x6);
225-
matrix.beginText(0, 1, 0xFFFFFF);
226-
matrix.println(text);
227-
matrix.endText();
228-
229-
matrix.endDraw();
230-
231-
//Wire1.beginTransmission(100);
232-
//if (Wire1.endTransmission() == 0) {
233-
// Serial.println("Device found");
234-
//}
235-
/*
236-
237-
uint8_t buf[2] = {0, 0xFF};
238-
Wire1.beginTransmission(100);
239-
Wire1.write(buf, 2);
240-
Wire1.endTransmission(true);
241-
Wire1.requestFrom(100, 1);
242-
while (!Wire1.available()) {
243-
delay(1);
244-
}
245-
uint8_t res = Wire1.read();
246-
Serial.println(res, HEX);
247-
248-
Wire1.requestFrom(100, 20);
249-
uint8_t howmany = Wire1.read();
250-
Serial.println(howmany);
251-
for (int j = 0; j < howmany+1; j++) {
252-
Serial.println(Wire1.read(), HEX);
253-
}
254-
delay(100);
255-
buf[0] = 0x2; buf[1] = 0xFD;
256-
Wire1.beginTransmission(100);
257-
Wire1.write(buf, 2);
258-
Wire1.endTransmission(true);
259-
Wire1.requestFrom(100, 1);
260-
res = Wire1.read();
261-
Serial.println(res, HEX);
262-
if (res == 255) {
263-
Serial.println("error");
264-
while (1);
265-
}
266-
267-
Wire1.requestFrom(100, 3);
268-
howmany = Wire1.read();
269-
Serial.println(howmany);
270-
for (int j = 0; j < howmany + 1; j++) {
271-
Serial.println(Wire1.read(), HEX);
272-
}
273-
*/
274-
}
275-
276-
void loop() {
277-
// put your main code here, to run repeatedly:
278-
}
259+
}

0 commit comments

Comments
 (0)