forked from SodaqMoja/RN2483FirmwareUpdater
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RN2483FirmwareUpdater.ino
352 lines (277 loc) · 10.4 KB
/
RN2483FirmwareUpdater.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "RN2483Bootloader.h"
#include "IntelHexParser.h"
#include "Utils.h"
#include "Sodaq_wdt.h"
#include "HexFileImage.h"
// TODO ask user if should erase blocks
// TODO investigate larger writing blocks for higher speed
#define DEBUG_SYMBOLS_ON
#if defined (ARDUINO_SAMD_ZERO)
#define CONSOLE_STREAM SerialUSB
#define DEBUG_STREAM SerialUSB
#elif defined (ARDUINO_ARCH_ESP32)
#define CONSOLE_STREAM Serial
#define DEBUG_STREAM Serial
#else
#define CONSOLE_STREAM SERIAL_PORT_MONITOR
#define DEBUG_STREAM SERIAL_PORT_MONITOR
#endif
// keep testing SODAQ boards at the begining s
#if defined(ARDUINO_SODAQ_EXPLORER)
#define LORA_STREAM Serial2
// keep testing SODAQ boards at the begining since they can also be Zero boards
#elif defined(ARDUINO_SODAQ_AUTONOMO) || defined(ARDUINO_SODAQ_ONE) || defined(ARDUINO_SODAQ_ONE_BETA)
#define LORA_STREAM Serial1
// Now other Zero boards
#elif defined(ARDUINO_SAMD_ZERO)
#define LORA_STREAM Serial
#define LORA_RESET 6
#elif defined(ARDUINO_ARCH_ESP32)
// Serial1 on ESP32 default Serial1 pins are used by SPI Flash
// can't be used as is without remap, here we'll use free GPIO
//HardwareSerial Serial1(1); // UART Serial1
// On my LOLIN32 board, I'm using this pin connected
// to RN2483 module, but you can use other, remember
// that GPIO >33 are input only, so only RN2483 TX pin can use them
#define LORA_RX_PIN 25 // connected to ESP32 TXD1
#define LORA_TX_PIN 35 // connected to ESP32 RXD1
#define LORA_STREAM Serial1
#define LORA_RESET 4
#endif
#ifdef DEBUG_SYMBOLS_ON
#define debugPrintln(...) { if (isDebugOn) DEBUG_STREAM.println(__VA_ARGS__); }
#define debugPrint(...) { if (isDebugOn) DEBUG_STREAM.print(__VA_ARGS__); }
#warning "Debug mode is ON"
#else
#define debugPrintLn(...)
#define debugPrint(...)
#endif
#define consolePrintln(...) { CONSOLE_STREAM.println(__VA_ARGS__); }
#define consolePrint(...) { CONSOLE_STREAM.print(__VA_ARGS__); }
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define HexFileImageName STR(HexFileImage)
const uint8_t VersionMajor = 1;
const uint8_t VersionMinor = 4;
const uint8_t PageSize = 64;
Sodaq_RN2483Bootloader bootloader;
IntelHexParser hexParser(PageSize);
bool isDebugOn = false;
bool shouldEraseBlocks = true;
int8_t lastHexParserProgressPercent = -1;
bool shouldUseBootloaderMode = false;
bool onPageStart(uint32_t startingAddress);
bool onPageComplete(uint32_t startingAddress, const uint8_t* buffer, size_t size);
void onHexParserProgress(size_t currentLine, size_t totalLines);
bool onPageStart(uint32_t startingAddress)
{
if (shouldEraseBlocks) {
if (bootloader.eraseFlash(startingAddress, 1)) {
debugPrint("Successfully erased block starting at 0x");
debugPrintln(startingAddress, HEX);
return true;
}
else {
debugPrint("Failed to erase block starting at 0x");
debugPrintln(startingAddress, HEX);
return false;
}
}
return true;
}
bool onPageComplete(uint32_t startingAddress, const uint8_t* buffer, size_t size)
{
if (bootloader.writeFlash(startingAddress, buffer, size)) {
debugPrint("Successfully wrote block starting at 0x");
debugPrintln(startingAddress, HEX);
return true;
}
else {
debugPrint("Failed to write block starting at 0x");
debugPrintln(startingAddress, HEX);
return false;
}
}
void onHexParserProgress(size_t currentLine, size_t totalLines)
{
const uint8_t progressBarStepPercent = 2; // 1 step every x% done
const uint8_t progressBarTextPercent = 25; // 1 text reference per x% done
const uint8_t percent = (currentLine * 100) / (totalLines - 1);
if (percent != lastHexParserProgressPercent) {
lastHexParserProgressPercent = percent;
if (percent % progressBarTextPercent == 0) {
consolePrint(" ");
consolePrint(percent);
consolePrint("% ")
}
else if (percent % progressBarStepPercent == 0) {
consolePrint("|");
}
if (percent == 100) {
consolePrintln();
}
}
}
void setup()
{
// Enable LoRaBee on Autonomo
#if defined(ARDUINO_SODAQ_AUTONOMO)
pinMode(BEE_VCC, OUTPUT);
digitalWrite(BEE_VCC, HIGH); //set input power BEE high
#endif
#if defined(LORA_RESET)
pinMode(LORA_RESET, OUTPUT);
digitalWrite(LORA_RESET, LOW);
sodaq_wdt_safe_delay(100);
digitalWrite(LORA_RESET, HIGH);
sodaq_wdt_safe_delay(100);
#endif
#if defined (ARDUINO_ARCH_ESP32)
CONSOLE_STREAM.begin(115200);
#else
sodaq_wdt_safe_delay(5000);
if (DEBUG_STREAM != CONSOLE_STREAM) {
CONSOLE_STREAM.begin(115200);
}
#endif
consolePrint("** ");
#if defined(HEXFILE_RN2483_101)
consolePrint("RN2483 V1.0.1");
#elif defined(HEXFILE_RN2483_103)
consolePrint("RN2483 V1.0.3");
#elif defined(HEXFILE_RN2483_104)
consolePrint("RN2483 V1.0.4");
#elif defined(HEXFILE_RN2483_104A)
consolePrint("RN2483 V1.0.4A");
#elif defined(HEXFILE_RN2483_105)
consolePrint("RN2483 V1.0.5");
#elif defined(HEXFILE_RN2903AU_097rc7)
consolePrint("RN2903 V0.9.7");
#elif defined(HEXFILE_RN2903_098)
consolePrint("RN2903 V0.9.8");
#elif defined(HEXFILE_RN2903_103)
consolePrint("RN2903 V1.0.3");
#elif defined(HEXFILE_RN2903_SA103)
consolePrint("RN2903 SA V1.0.3");
#elif defined(HEXFILE_RN2903_AS923_105)
consolePrint("RN2903 AS V1.0.5");
#else
consolePrint("Unknown");
#endif
consolePrintln(" Firmware Updater **");
consolePrint("Version ");
consolePrint(VersionMajor);
consolePrint(".");
consolePrintln(VersionMinor);
consolePrintln("\nPress:");
consolePrintln(" - \'b\' to enable bootloader mode");
consolePrintln(" - \'d\' to enable debug");
for (uint8_t i = 0; i < 5 * 4; i++) {
while (CONSOLE_STREAM.available() > 0) {
char c = CONSOLE_STREAM.read();
if (c == 'b') {
shouldUseBootloaderMode = true;
consolePrintln("\nBootloader mode is now enabled.");
}
if (c == 'd') {
isDebugOn = true;
consolePrintln("\nDebug is now enabled.");
}
}
sodaq_wdt_safe_delay(250);
consolePrint(".");
}
consolePrintln();
if (isDebugOn) {
DEBUG_STREAM.begin(115200);
bootloader.setDiag(DEBUG_STREAM);
hexParser.setDiag(DEBUG_STREAM);
}
hexParser.setPageStartCallback(onPageStart);
hexParser.setPageCompleteCallback(onPageComplete);
hexParser.setProgressCallback(onHexParserProgress);
consolePrintln("\n* Starting HEX File Image Verification...");
// verify image first
if (hexParser.verifyImageIntegrity()) {
consolePrintln("HEX File Image Verification Successful!");
}
else {
consolePrintln("HEX File Image Verification Failed!");
consolePrintln("Cannot continue with firmware update!");
while (true) { }
}
bootloader.initBootloader(LORA_STREAM);
}
void loop()
{
if (shouldUseBootloaderMode) {
uint32_t startMS = millis();
#if defined(ARDUINO_ARCH_ESP32)
// Remap RX1/TX1 on unused GPIO
LORA_STREAM.begin(bootloader.getDefaultApplicationBaudRate(), SERIAL_8N1, LORA_TX_PIN, LORA_RX_PIN);
#else
LORA_STREAM.begin(bootloader.getDefaultApplicationBaudRate());
#endif
sodaq_wdt_safe_delay(200);
BootloaderVersionInfo versionInfo;
if (bootloader.getVersionInfo(versionInfo)) {
consolePrintln("\n* The module is in Bootloader mode.");
consolePrint("Bootloader Version: ");
consolePrintln(versionInfo.BootloaderVersion, HEX);
consolePrint("Device ID: ");
consolePrintln(versionInfo.DeviceId, HEX);
consolePrintln("\n* Starting firmware update...");
if (hexParser.parseImage()) {
consolePrintln("Firmware update has finished successfully! Please unplug the module to restart.");
}
else {
consolePrintln("Failed to upload the firmware. Please unplug and restart.");
while (true) { }
}
// consolePrintln("Resetting the module...");
// bootloader.bootloaderReset();
shouldUseBootloaderMode = false;
}
else {
consolePrintln("The module did not respond in bootloader mode. Please unplug and retry in application mode.");
}
consolePrintln("Elapsed Time: " + String((float)(millis() - startMS) / 1000) + "s");
}
else {
#if defined(LORA_RESET)
consolePrint("Reseting module with PIN ");
consolePrintln(LORA_RESET);
digitalWrite(LORA_RESET, LOW);
sodaq_wdt_safe_delay(100);
digitalWrite(LORA_RESET, HIGH);
sodaq_wdt_safe_delay(1000);
#endif
LORA_STREAM.end();
LORA_STREAM.flush();
#if defined(ARDUINO_ARCH_ESP32)
// Remap RX1/TX1 on unused GPIO
LORA_STREAM.begin(bootloader.getDefaultApplicationBaudRate(), SERIAL_8N1, LORA_TX_PIN, LORA_RX_PIN);
#else
LORA_STREAM.begin(bootloader.getDefaultApplicationBaudRate());
#endif
sodaq_wdt_safe_delay(100);
char applicationResetResponse[64];
if (bootloader.applicationReset(applicationResetResponse, sizeof(applicationResetResponse))) {
consolePrintln("\n* The module is in Application mode: ");
consolePrintln(applicationResetResponse);
consolePrintln("\nReady to start firmware update...");
consolePrint("Firmware Image: ");
consolePrintln(HexFileImageName);
consolePrintln("\nPlease press \'c\' to continue...");
while (CONSOLE_STREAM.read() != 'c') { }
consolePrintln("Erasing firmware and attempting to start bootloader...");
bootloader.eraseFirmware();
sodaq_wdt_safe_delay(1000);
shouldUseBootloaderMode = true;
}
else {
consolePrintln("The module did not respond in application mode. Please unplug and retry in bootloader mode.");
consolePrintln(applicationResetResponse);
}
}
}