6
6
7
7
// https://www.st.com/resource/en/application_note/an4221-i2c-protocol-used-in-the-stm32-bootloader-stmicroelectronics.pdf
8
8
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
+ };
11
54
12
55
ArduinoLEDMatrix matrix;
13
56
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);
14
124
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) {
16
125
uint8_t cmd[2 ];
17
126
cmd[0 ] = opcode;
18
127
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
27
136
Wire1.requestFrom (100 , 1 );
28
137
auto c = Wire1.read ();
29
138
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);
32
141
return -1 ;
33
142
}
34
143
Wire1.beginTransmission (100 );
@@ -44,8 +153,8 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
44
153
c = Wire1.read ();
45
154
}
46
155
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);
49
158
return -1 ;
50
159
}
51
160
}
@@ -66,17 +175,19 @@ int command_write_page(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, const u
66
175
c = Wire1.read ();
67
176
}
68
177
if (c != 0x79 ) {
69
- Serial .print (" error: " );
70
- Serial .println (c, HEX);
178
+ SerialDebug .print (" error: " );
179
+ SerialDebug .println (c, HEX);
71
180
return -1 ;
72
181
}
73
182
}
74
183
final_ack:
75
184
return howmany + 1 ;
76
185
}
77
186
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);
78
190
79
- int command (uint8_t opcode, uint8_t * buf_cmd, size_t len_cmd, uint8_t * buf_resp, size_t len_resp) {
80
191
uint8_t cmd[2 ];
81
192
cmd[0 ] = opcode;
82
193
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,
102
213
delay (100 );
103
214
Wire1.requestFrom (100 , 1 );
104
215
c = Wire1.read ();
105
- Serial. print (" retry" );
216
+ SerialDebug. println (" retry" );
106
217
}
107
218
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);
110
221
return -1 ;
111
222
}
112
223
}
@@ -123,8 +234,8 @@ int command(uint8_t opcode, uint8_t* buf_cmd, size_t len_cmd, uint8_t* buf_resp,
123
234
Wire1.requestFrom (100 , 1 );
124
235
c = Wire1.read ();
125
236
if (c != 0x79 ) {
126
- Serial .print (" error: " );
127
- Serial .println (c, HEX);
237
+ SerialDebug .print (" error: " );
238
+ SerialDebug .println (c, HEX);
128
239
return -1 ;
129
240
}
130
241
final_ack:
@@ -145,134 +256,4 @@ int sendReset() {
145
256
}
146
257
}
147
258
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