Skip to content

Commit 84a7dfb

Browse files
committed
examples: improve firmware updater
1 parent 9df93c6 commit 84a7dfb

File tree

1 file changed

+98
-68
lines changed

1 file changed

+98
-68
lines changed

examples/Utilities/FirmwareUpdater/FirmwareUpdater.ino

Lines changed: 98 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,64 +6,93 @@
66
* SPDX-License-Identifier: MPL-2.0
77
*/
88

9-
#if defined(ARDUINO_UNOWIFIR4)
9+
#if defined(ARDUINO_UNOWIFIR4)
1010
#include "ArduinoGraphics.h"
1111
#include "Arduino_LED_Matrix.h"
1212
#endif
1313

1414
#include <Arduino_Modulino.h>
1515
#include "Wire.h"
1616
#include "fw.h"
17+
#include "fw_ledmatrix.h"
1718

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

2021
bool flash(const uint8_t* binary, size_t lenght, bool verbose = true);
22+
Module modulino;
23+
24+
// Change this to true if programming a blank Modulino LED Matrix
25+
bool force_led_matrix = false;
2126

2227
void setup() {
2328
Serial.begin(115200);
24-
Wire1.begin();
25-
Wire1.setClock(400000);
29+
Modulino.begin();
30+
modulino.getWire()->setClock(400000);
31+
32+
modulino.getWire()->beginTransmission(0x64);
33+
auto is_boot_mode = (modulino.getWire()->endTransmission() == 0);
34+
35+
if (is_boot_mode) {
36+
Serial.println("boot mode");
37+
}
38+
39+
bool is_led_matrix = false;
2640

2741
// Send reset to the module; remember, connect only ONE module at a time
28-
if (sendReset() != 0) {
29-
Serial.println("Send reset failed");
42+
if (!is_boot_mode) {
43+
modulino.getWire()->beginTransmission(0x39);
44+
is_led_matrix = (modulino.getWire()->endTransmission() == 0);
45+
46+
if (is_led_matrix) {
47+
Serial.println("led matrix mode");
48+
}
49+
50+
if (sendReset() != 0) {
51+
Serial.println("Send reset failed");
52+
}
3053
}
3154

32-
auto result = flash(node_base_bin, node_base_bin_len);
33-
34-
#if defined(ARDUINO_UNOWIFIR4)
55+
bool result;
56+
if (is_led_matrix || force_led_matrix) {
57+
result = flash(matrix_node_base_bin, matrix_node_base_bin_len);
58+
} else {
59+
result = flash(node_base_bin, node_base_bin_len);
60+
}
61+
62+
#if defined(ARDUINO_UNOWIFIR4)
3563
if (result) {
3664
matrixInitAndDraw("PASS");
3765
} else {
3866
matrixInitAndDraw("FAIL");
3967
}
40-
#endif
68+
#endif
4169
}
4270

4371
void loop() {
4472
// put your main code here, to run repeatedly:
4573
}
4674

4775
class SerialVerbose {
48-
public:
49-
SerialVerbose(bool verbose) : _verbose(verbose) {}
50-
int print(String s) {
51-
if (_verbose) {
52-
Serial.print(s);
53-
}
76+
public:
77+
SerialVerbose(bool verbose)
78+
: _verbose(verbose) {}
79+
int print(String s) {
80+
if (_verbose) {
81+
Serial.print(s);
5482
}
55-
int println(String s) {
56-
if (_verbose) {
57-
Serial.println(s);
58-
}
83+
}
84+
int println(String s) {
85+
if (_verbose) {
86+
Serial.println(s);
5987
}
60-
int println(int num, int base) {
61-
if (_verbose) {
62-
Serial.println(num, base);
63-
}
88+
}
89+
int println(int num, int base) {
90+
if (_verbose) {
91+
Serial.println(num, base);
6492
}
65-
private:
66-
bool _verbose;
93+
}
94+
private:
95+
bool _verbose;
6796
};
6897

6998
#if defined(ARDUINO_UNOWIFIR4)
@@ -120,7 +149,8 @@ bool flash(const uint8_t* binary, size_t lenght, bool verbose) {
120149
SerialDebug.println(resp_buf[i], HEX);
121150
}
122151

123-
for (int i = 0; i < lenght; i += 128) {
152+
int lenght_mod128 = ((lenght + 128) / 128) * 128;
153+
for (int i = lenght_mod128; i >= 0; i -= 128) {
124154
SerialDebug.print("WRITE_PAGE ");
125155
SerialDebug.println(i, HEX);
126156
uint8_t write_buf[5] = { 8, 0, i / 256, i % 256 };
@@ -131,7 +161,7 @@ bool flash(const uint8_t* binary, size_t lenght, bool verbose) {
131161
delay(10);
132162
}
133163
SerialDebug.println("GO");
134-
uint8_t jump_buf[5] = { 0x8, 0x00, 0x00, 0x00, 0x8 };
164+
uint8_t jump_buf[5] = { 0x8, 0x00, 0x00, 0x00, 0x8 };
135165
resp = command(0x21, jump_buf, 5, nullptr, 0, verbose);
136166
return true;
137167
}
@@ -144,32 +174,32 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
144174
uint8_t cmd[2];
145175
cmd[0] = opcode;
146176
cmd[1] = 0xFF ^ opcode;
147-
Wire1.beginTransmission(100);
148-
Wire1.write(cmd, 2);
177+
modulino.getWire()->beginTransmission(100);
178+
modulino.getWire()->write(cmd, 2);
149179
if (len_cmd > 0) {
150180
buf_cmd[len_cmd - 1] = 0;
151181
for (int i = 0; i < len_cmd - 1; i++) {
152182
buf_cmd[len_cmd - 1] ^= buf_cmd[i];
153183
}
154-
Wire1.endTransmission(true);
155-
Wire1.requestFrom(100, 1);
156-
auto c = Wire1.read();
184+
modulino.getWire()->endTransmission(true);
185+
modulino.getWire()->requestFrom(100, 1);
186+
auto c = modulino.getWire()->read();
157187
if (c != 0x79) {
158188
SerialDebug.print("error first ack: ");
159189
SerialDebug.println(c, HEX);
160190
return -1;
161191
}
162-
Wire1.beginTransmission(100);
163-
Wire1.write(buf_cmd, len_cmd);
192+
modulino.getWire()->beginTransmission(100);
193+
modulino.getWire()->write(buf_cmd, len_cmd);
164194
}
165-
Wire1.endTransmission(true);
166-
Wire1.requestFrom(100, 1);
167-
auto c = Wire1.read();
195+
modulino.getWire()->endTransmission(true);
196+
modulino.getWire()->requestFrom(100, 1);
197+
auto c = modulino.getWire()->read();
168198
if (c != 0x79) {
169199
while (c == 0x76) {
170200
delay(10);
171-
Wire1.requestFrom(100, 1);
172-
c = Wire1.read();
201+
modulino.getWire()->requestFrom(100, 1);
202+
c = modulino.getWire()->read();
173203
}
174204
if (c != 0x79) {
175205
SerialDebug.print("error second ack: ");
@@ -182,16 +212,16 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
182212
for (int i = 0; i < len_fw + 1; i++) {
183213
tmpbuf[len_fw + 1] ^= tmpbuf[i];
184214
}
185-
Wire1.beginTransmission(100);
186-
Wire1.write(tmpbuf, len_fw + 2);
187-
Wire1.endTransmission(true);
188-
Wire1.requestFrom(100, 1);
189-
c = Wire1.read();
215+
modulino.getWire()->beginTransmission(100);
216+
modulino.getWire()->write(tmpbuf, len_fw + 2);
217+
modulino.getWire()->endTransmission(true);
218+
modulino.getWire()->requestFrom(100, 1);
219+
c = modulino.getWire()->read();
190220
if (c != 0x79) {
191221
while (c == 0x76) {
192222
delay(10);
193-
Wire1.requestFrom(100, 1);
194-
c = Wire1.read();
223+
modulino.getWire()->requestFrom(100, 1);
224+
c = modulino.getWire()->read();
195225
}
196226
if (c != 0x79) {
197227
SerialDebug.print("error: ");
@@ -210,28 +240,28 @@ int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp,
210240
uint8_t cmd[2];
211241
cmd[0] = opcode;
212242
cmd[1] = 0xFF ^ opcode;
213-
Wire1.beginTransmission(100);
214-
Wire1.write(cmd, 2);
243+
modulino.getWire()->beginTransmission(100);
244+
modulino.getWire()->write(cmd, 2);
215245
if (len_cmd > 0) {
216-
Wire1.endTransmission(true);
217-
Wire1.requestFrom(100, 1);
218-
auto c = Wire1.read();
246+
modulino.getWire()->endTransmission(true);
247+
modulino.getWire()->requestFrom(100, 1);
248+
auto c = modulino.getWire()->read();
219249
if (c != 0x79) {
220250
Serial.print("error first ack: ");
221251
Serial.println(c, HEX);
222252
return -1;
223253
}
224-
Wire1.beginTransmission(100);
225-
Wire1.write(buf_cmd, len_cmd);
254+
modulino.getWire()->beginTransmission(100);
255+
modulino.getWire()->write(buf_cmd, len_cmd);
226256
}
227-
Wire1.endTransmission(true);
228-
Wire1.requestFrom(100, 1);
229-
auto c = Wire1.read();
257+
modulino.getWire()->endTransmission(true);
258+
modulino.getWire()->requestFrom(100, 1);
259+
auto c = modulino.getWire()->read();
230260
if (c != 0x79) {
231261
while (c == 0x76) {
232262
delay(100);
233-
Wire1.requestFrom(100, 1);
234-
c = Wire1.read();
263+
modulino.getWire()->requestFrom(100, 1);
264+
c = modulino.getWire()->read();
235265
SerialDebug.println("retry");
236266
}
237267
if (c != 0x79) {
@@ -244,14 +274,14 @@ int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp,
244274
if (len_resp == 0) {
245275
goto final_ack;
246276
}
247-
Wire1.requestFrom(100, len_resp);
248-
howmany = Wire1.read();
277+
modulino.getWire()->requestFrom(100, len_resp);
278+
howmany = modulino.getWire()->read();
249279
for (int j = 0; j < howmany + 1; j++) {
250-
buf_resp[j] = Wire1.read();
280+
buf_resp[j] = modulino.getWire()->read();
251281
}
252282

253-
Wire1.requestFrom(100, 1);
254-
c = Wire1.read();
283+
modulino.getWire()->requestFrom(100, 1);
284+
c = modulino.getWire()->read();
255285
if (c != 0x79) {
256286
SerialDebug.print("error: ");
257287
SerialDebug.println(c, HEX);
@@ -265,12 +295,12 @@ int sendReset() {
265295
uint8_t buf[3] = { 'D', 'I', 'E' };
266296
int ret;
267297
for (int i = 8; i < 0x78; i++) {
268-
Wire1.beginTransmission(i);
269-
ret = Wire1.endTransmission();
298+
modulino.getWire()->beginTransmission(i);
299+
ret = modulino.getWire()->endTransmission();
270300
if (ret != 2) {
271-
Wire1.beginTransmission(i);
272-
Wire1.write(buf, 40);
273-
ret = Wire1.endTransmission();
301+
modulino.getWire()->beginTransmission(i);
302+
modulino.getWire()->write(buf, 40);
303+
ret = modulino.getWire()->endTransmission();
274304
return ret;
275305
}
276306
}

0 commit comments

Comments
 (0)