-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRN2483Bootloader.cpp
284 lines (214 loc) · 7.63 KB
/
RN2483Bootloader.cpp
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
#include "RN2483Bootloader.h"
#include "Sodaq_wdt.h"
#include <math.h>
#define DEBUG_SYMBOLS_ON
#ifdef DEBUG_SYMBOLS_ON
#define debugPrintLn(...) { if (this->diagStream) this->diagStream->println(__VA_ARGS__); }
#define debugPrint(...) { if (this->diagStream) this->diagStream->print(__VA_ARGS__); }
#warning "Debug mode is ON"
#else
#define debugPrintLn(...)
#define debugPrint(...)
#endif
Sodaq_RN2483Bootloader::Sodaq_RN2483Bootloader():
loraStream(0),
diagStream(0),
inputBufferSize(RN2483_BOOTLOADER_INPUT_BUFFER_SIZE)
{
}
void Sodaq_RN2483Bootloader::initBootloader(Uart& stream)
{
debugPrintLn("[initBootloader]");
this->loraStream = &stream;
}
void Sodaq_RN2483Bootloader::eraseFirmware()
{
debugPrintLn("[eraseFirmware]");
this->loraStream->print("sys eraseFW");
this->loraStream->print("\r\n");
this->loraStream->flush();
}
bool Sodaq_RN2483Bootloader::getVersionInfo(BootloaderVersionInfo& versionInfo)
{
sendCommand(GetVersionInfoCommand);
BootloaderRecord response;
if (readBootloaderResponse(response, (uint8_t*)inputBuffer, inputBufferSize) > 0) {
memcpy((void*)&versionInfo, inputBuffer, min(sizeof(inputBuffer), sizeof(versionInfo)));
return true;
}
return false;
}
bool Sodaq_RN2483Bootloader::writeFlash(uint32_t startingAddress, const uint8_t* buffer, size_t size)
{
sendCommand(WriteFlashCommand, size, startingAddress);
for (size_t i = 0; i < size; i++) {
loraStream->write((uint8_t)buffer[i]);
}
BootloaderRecord response;
if (readBootloaderResponse(response, (uint8_t*)inputBuffer, inputBufferSize) > 0) {
if (inputBuffer[0] == 1) {
return true;
}
}
return false;
}
bool Sodaq_RN2483Bootloader::eraseFlash(uint32_t address, uint8_t blockCount)
{
sendCommand(EraseFlashCommand, blockCount, address);
BootloaderRecord response;
if (readBootloaderResponse(response, (uint8_t*)inputBuffer, inputBufferSize) > 0) {
if (inputBuffer[0] == 1) {
return true;
}
}
return false;
}
void Sodaq_RN2483Bootloader::getChecksum()
{
// TODO
// sendCommand(CalculateChecksumCommand, Length (from address), Address)
}
inline void printToLength(Stream& stream, const uint8_t* buffer, size_t length)
{
for (uint8_t i = 0; i < length; i++) {
stream.print(i, DEC);
if (i < 10) {
stream.print(" ");
}
stream.print(" ");
}
stream.println();
for (uint8_t i = 0; i < length; i++) {
stream.print("0x");
if (buffer[i] < 0x10) {
stream.print("0");
}
stream.print(buffer[i], HEX);
stream.print(" ");
}
stream.println();
}
void Sodaq_RN2483Bootloader::bootloaderReset()
{
debugPrintLn("[bootloaderReset]");
sendCommand(ResetDeviceCommand);
// no response
}
uint16_t Sodaq_RN2483Bootloader::readApplicationLn()
{
int len = this->loraStream->readBytesUntil('\n', this->inputBuffer, this->inputBufferSize);
if (len > 0) {
this->inputBuffer[len - 1] = 0; // bytes until \n always end with \r, so get rid of it (-1)
}
return len;
}
bool Sodaq_RN2483Bootloader::expectApplicationString(const char* str, uint16_t timeout)
{
debugPrint("[expectApplicationString] expecting ");
debugPrint(str);
unsigned long start = millis();
while (millis() < start + timeout) {
sodaq_wdt_reset();
debugPrint(".");
if (readApplicationLn() > 0) {
debugPrint("(");
debugPrint(this->inputBuffer);
debugPrint(")");
if (strstr(this->inputBuffer, str) != NULL) {
debugPrintLn(" found a match!");
return true;
}
return false;
}
}
return false;
}
bool Sodaq_RN2483Bootloader::applicationReset(char* deviceResponseBuffer, size_t size)
{
debugPrintLn("[applicationReset]");
this->loraStream->print("sys reset\r\n");
sodaq_wdt_safe_delay(100);
if (expectApplicationString("RN")) {
debugPrintLn("[RN Module]");
if ((strstr(this->inputBuffer, "RN2483") != NULL) || (strstr(this->inputBuffer, "RN2903") != NULL)) {
if (deviceResponseBuffer && (size > strlen(this->inputBuffer))) {
debugPrintLn("Copying the response to the given buffer.");
memcpy(deviceResponseBuffer, this->inputBuffer, min(size, inputBufferSize));
deviceResponseBuffer[min(size, inputBufferSize)] = '\0'; // make sure the string is terminated
}
return true;
}
else {
debugPrintLn("Unknown device type!");
return false;
}
}
return false;
}
// returns -2 in case of error, -1 if no response at all, 0 if only mainResponse, or the lenth of the secondary response otherwise
int16_t Sodaq_RN2483Bootloader::readBootloaderResponse(BootloaderRecord& mainResponse, uint8_t* secondaryResponse, uint8_t secondaryResponseSize)
{
debugPrintLn("[readBootloaderResponse]");
int len = this->loraStream->readBytes((uint8_t*)&mainResponse, sizeof(mainResponse));
#ifdef DEBUG_SYMBOLS_ON
if (this->diagStream) {
printToLength((Stream&)(*diagStream), (uint8_t*)&mainResponse, sizeof(mainResponse));
}
#endif
if (len <= 0) {
debugPrintLn("No response received at all!");
return -1;
}
uint8_t expectLen = 0;
switch (mainResponse.Command) {
case ReadFlashCommand :
case ReadEeCommand :
case ReadConfigurationWordsCommand :
expectLen = mainResponse.Length;
break;
case WriteFlashCommand :
case EraseFlashCommand :
case WriteEeCommand :
case WriteConfigurationWordsCommand :
expectLen = 1;
break;
// Documentation is unclear, it shows a 2 byte checksum in the
// repsonse, but also mentions a 'status'?
case CalculateChecksumCommand :
expectLen = 3;
break;
// These will read until a time out.
// GetVersionInfoCommand does not send the length of the
// response as per documentation.
case GetVersionInfoCommand :
case ResetDeviceCommand :
expectLen = secondaryResponseSize;
break;
}
if (expectLen > secondaryResponseSize) {
debugPrintLn("The secondary response cannot fit in the buffer!");
this->loraStream->flush();
return -2;
}
len = this->loraStream->readBytes(secondaryResponse, expectLen);
#ifdef DEBUG_SYMBOLS_ON
if (this->diagStream) {
printToLength((Stream&)(*diagStream), (uint8_t*)secondaryResponse, len);
}
#endif
return len;
}
void Sodaq_RN2483Bootloader::sendCommand(uint8_t command, uint8_t length, uint32_t address)
{
this->loraStream->write((uint8_t)0x55); // autobaud
this->loraStream->write((uint8_t)command); // command
this->loraStream->write((uint8_t)length); // length
this->loraStream->write((uint8_t)0x00); // unused part of length
this->loraStream->write((uint8_t)0x55); // Key1 = 0x55
this->loraStream->write((uint8_t)0xAA); // Key2 = 0xAA
this->loraStream->write((uint8_t)address); // address part 0 (LSB side)
this->loraStream->write((uint8_t)(address >> 8)); // address part 1
this->loraStream->write((uint8_t)(address >> 16)); // address part 2
this->loraStream->write((uint8_t)(address >> 24)); // address part 3 (MSB Side)
this->loraStream->flush();
}