-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeensyOTADemo.ino
393 lines (337 loc) · 12.6 KB
/
TeensyOTADemo.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
Name: TeensyOTADemo.ino
Created: 10/9/2021 11:18:19 AM
Author: FRANKNEWXPS15\Frank
This program demonstrates the use of 'board.txt' post-build instructions in
conjunction with a small C# command-line program for seamless 'over-the-air' (OTA)
firmware update for Teensy modules.
Implementation of OTA update requires that Joe Pasquariello's 'FlasherX'
code be added to the Teensy sketch as it is below, and some code to allow
an external serial communications program to trigger the update process.
In the demo sketch below, the 'FlasherX' additions are the '#include FlasherTxx.h'
line and the addition of the FlasherX specific code at the bottom. The trigger
mechanism is provided by the 'CheckForUserInput();' call in loop. If the ASCII
code for 'U' or 'u' is present on Serial1, the update process will be launched.
10/31/21 Replaced C# module with Tera Term Macro file TeensyOTA1.ttl. The 'board.txt'
post-build command file now calls tera term and to executed the above macro
*/
#include "FlashTxx.h" // TLC/T3x/T4x flash primitives
#include <elapsedMillis.h>
elapsedMillis MsecSinceLastLEDToggle; //used for LED blink timer
Stream* FTDIport = &Serial1; // Serial (USB) or Serial1, Serial2, etc. (UART)
//******************************************************************************
// hex_info_t struct for hex record and hex file info
//******************************************************************************
typedef struct { //
char* data; // pointer to array allocated elsewhere
unsigned int addr; // address in intel hex record
unsigned int code; // intel hex record type (0=data, etc.)
unsigned int num; // number of data bytes in intel hex record
uint32_t base; // base address to be added to intel hex 16-bit addr
uint32_t min; // min address in hex file
uint32_t max; // max address in hex file
int eof; // set true on intel hex EOF (code = 1)
int lines; // number of hex records received
} hex_info_t;
void read_ascii_line(Stream* serial, char* line, int maxbytes);
int parse_hex_line(const char* theline, char* bytes,
unsigned int* addr, unsigned int* num, unsigned int* code);
int process_hex_record(hex_info_t* hex);
void update_firmware(Stream* serial, uint32_t buffer_addr, uint32_t buffer_size);
uint32_t buffer_addr, buffer_size; //09/20/21 copied from FlasherX - loop()
void setup()
{
Serial.begin(115200);
//Serial.begin(230400);
//while (!Serial) {} //10/06/21 infinite wait hangs pgm when USB cable removed - oops!
delay(2000); //10/06/21 - just use fixed delay instead
Serial1.begin(115200);
// Serial1.begin(230400);
while (!Serial1) {}
pinMode(LED_BUILTIN, OUTPUT);
MsecSinceLastLEDToggle = 0;
}
void loop()
{
if (Serial1.available())
{
CheckForUserInput();
}
if (MsecSinceLastLEDToggle >= 200)
{
MsecSinceLastLEDToggle -= 200;
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}
void CheckForUserInput()
{
const int bufflen = 80;
char buff[bufflen];
memset(buff, 0, bufflen);
//const char s[2] = ",";
if (Serial1.available() > 0)
{
// read the incoming byte:
int incomingByte = Serial1.read();
// say what you got:
Serial1.print("I received: ");
Serial1.println(incomingByte, HEX); //chg to HEX 02/18/20
//02/18/20 experiment with multiple commands
switch (incomingByte)
{
case 0x55: //ASCII 'U'
case 0x75: //ASCII 'u'
Serial1.println(F("Start Program Update - Send new HEX file!"));
//09/20/21 copied from FlasherX - loop()
if (firmware_buffer_init(&buffer_addr, &buffer_size) == 0)
{
Serial1.printf("unable to create buffer\n");
Serial1.flush();
for (;;) {}
}
Serial1.printf("buffer = %1luK %s (%08lX - %08lX)\n",
buffer_size / 1024, IN_FLASH(buffer_addr) ? "FLASH" : "RAM",
buffer_addr, buffer_addr + buffer_size);
//09/20/21 clear the serial buffer
while (Serial1.available())
{
Serial1.read();
}
// receive hex file via serial, write new firmware to flash, clean up, reboot
update_firmware(&Serial1, buffer_addr, buffer_size); // no return if success
// return from update_firmware() means error or user abort, so clean up and
// reboot to ensure that static vars get boot-up initialized before retry
Serial1.printf("erase FLASH buffer / free RAM buffer...\n");
firmware_buffer_free(buffer_addr, buffer_size);
Serial1.flush();
REBOOT;
break;
}
}
}
//09/20/21 copied from FlasherX
//******************************************************************************
// update_firmware() read hex file and write new firmware to program flash
//******************************************************************************
void update_firmware(Stream* serial, uint32_t buffer_addr, uint32_t buffer_size)
{
static char line[96]; // buffer for hex lines
static char data[32] __attribute__((aligned(8))); // buffer for hex data
hex_info_t hex = { // intel hex info struct
data, 0, 0, 0, // data,addr,num,code
0, 0xFFFFFFFF, 0, // base,min,max,
0, 0 // eof,lines
};
serial->printf("waiting for hex lines...\n");
// read and process intel hex lines until EOF or error
//DEBUG!!
//int line_num = 0;
//DEBUG!!
long int start_msec = 0;
bool bFirstTime = true;
while (!hex.eof) {
read_ascii_line(serial, line, sizeof(line));
if (bFirstTime)
{
start_msec = millis();
bFirstTime = false;
}
//DEBUG!!
//serial->printf("%d: %s\n", line_num, line);
//line_num++;
//DEBUG!!
//10/02/21 gfp added for visual xfer confirmation
if (hex.lines % 10 == 0)
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
}
// reliability of transfer via USB is improved by this printf/flush
if (serial == (Stream*)&Serial)
{
serial->printf("%s\n", line);
serial->flush();
}
if (parse_hex_line((const char*)line, hex.data, &hex.addr, &hex.num, &hex.code) == 0) {
serial->printf("abort - bad hex line %s\n", line);
return;
}
else if (process_hex_record(&hex) != 0) { // error on bad hex code
serial->printf("abort - invalid hex code %d\n", hex.code);
return;
}
else if (hex.code == 0) { // if data record
uint32_t addr = buffer_addr + hex.base + hex.addr - FLASH_BASE_ADDR;
if (hex.max > (FLASH_BASE_ADDR + buffer_size)) {
serial->printf("abort - max address %08lX too large\n", hex.max);
return;
}
else if (!IN_FLASH(buffer_addr)) {
memcpy((void*)addr, (void*)hex.data, hex.num);
}
else if (IN_FLASH(buffer_addr)) {
int error = flash_write_block(addr, hex.data, hex.num);
if (error) {
serial->printf("abort - error %02X in flash_write_block()\n", error);
return;
}
}
}
hex.lines++;
}
//serial->printf("\nhex file: %1d lines %1lu bytes (%08lX - %08lX)\n",
// hex.lines, hex.max - hex.min, hex.min, hex.max);
serial->printf("\nhex file: %1d lines %1lu bytes (%08lX - %08lX), %lu Msec\n",
hex.lines, hex.max - hex.min, hex.min, hex.max, millis()-start_msec);
// check FSEC value in new code -- abort if incorrect
#if defined(KINETISK) || defined(KINETISL)
uint32_t value = *(uint32_t*)(0x40C + buffer_addr);
if (value == 0xfffff9de) {
serial->printf("new code contains correct FSEC value %08lX\n", value);
}
else {
serial->printf("abort - FSEC value %08lX should be FFFFF9DE\n", value);
return;
}
#endif
// check FLASH_ID in new code - abort if not found
if (check_flash_id(buffer_addr, hex.max - hex.min)) {
serial->printf("new code contains correct target ID %s\n", FLASH_ID);
}
else {
serial->printf("abort - new code missing string %s\n", FLASH_ID);
return;
}
// get user input to write to flash or abort
int user_lines = -1;
while (user_lines != hex.lines && user_lines != 0) {
serial->printf("enter %d to flash or 0 to abort\n", hex.lines);
read_ascii_line(serial, line, sizeof(line));
sscanf(line, "%d", &user_lines);
}
if (user_lines == 0) {
serial->printf("abort - user entered 0 lines\n");
return;
}
// move new program from buffer to flash, free buffer, and reboot
//serial->printf("user entered %d lines\n", user_lines);
flash_move(FLASH_BASE_ADDR, buffer_addr, hex.max - hex.min);
// should not return from flash_move(), but put REBOOT here as reminder
REBOOT;
}
//******************************************************************************
// read_ascii_line() read ascii characters until '\n', '\r', or max bytes
//******************************************************************************
void read_ascii_line(Stream* serial, char* line, int maxbytes)
{
int c = 0, nchar = 0;
while (nchar < maxbytes && !(c == '\n' || c == '\r')) {
if (serial->available()) {
c = serial->read();
line[nchar++] = c;
}
}
line[nchar - 1] = 0; // null-terminate
}
//******************************************************************************
// process_hex_record() process record and return okay (0) or error (1)
//******************************************************************************
int process_hex_record(hex_info_t* hex)
{
if (hex->code == 0) { // data -- update min/max address so far
if (hex->base + hex->addr + hex->num > hex->max)
hex->max = hex->base + hex->addr + hex->num;
if (hex->base + hex->addr < hex->min)
hex->min = hex->base + hex->addr;
}
else if (hex->code == 1) { // EOF (:flash command not received yet)
hex->eof = 1;
}
else if (hex->code == 2) { // extended segment address (top 16 of 24-bit addr)
hex->base = ((hex->data[0] << 8) | hex->data[1]) << 4;
}
else if (hex->code == 3) { // start segment address (80x86 real mode only)
return 1;
}
else if (hex->code == 4) { // extended linear address (top 16 of 32-bit addr)
hex->base = ((hex->data[0] << 8) | hex->data[1]) << 16;
}
else if (hex->code == 5) { // start linear address (32-bit big endian addr)
hex->base = (hex->data[0] << 24) | (hex->data[1] << 16)
| (hex->data[2] << 8) | (hex->data[3] << 0);
}
else {
return 1;
}
return 0;
}
//******************************************************************************
// Intel Hex record foramt:
//
// Start code: one character, ASCII colon ':'.
// Byte count: two hex digits, number of bytes (hex digit pairs) in data field.
// Address: four hex digits
// Record type: two hex digits, 00 to 05, defining the meaning of the data field.
// Data: n bytes of data represented by 2n hex digits.
// Checksum: two hex digits, computed value used to verify record has no errors.
//
// Examples:
// :10 9D30 00 711F0000AD38000005390000F5460000 35
// :04 9D40 00 01480000 D6
// :00 0000 01 FF
//******************************************************************************
/* Intel HEX read/write functions, Paul Stoffregen, [email protected] */
/* This code is in the public domain. Please retain my name and */
/* email address in distributed copies, and let me know about any bugs */
/* I, Paul Stoffregen, give no warranty, expressed or implied for */
/* this software and/or documentation provided, including, without */
/* limitation, warranty of merchantability and fitness for a */
/* particular purpose. */
// type modifications by Jon Zeeff
/* parses a line of intel hex code, stores the data in bytes[] */
/* and the beginning address in addr, and returns a 1 if the */
/* line was valid, or a 0 if an error occured. The variable */
/* num gets the number of bytes that were stored into bytes[] */
#include <stdio.h> // sscanf(), etc.
#include <string.h> // strlen(), etc.
int parse_hex_line(const char* theline, char* bytes,
unsigned int* addr, unsigned int* num, unsigned int* code)
{
unsigned sum, len, cksum;
const char* ptr;
int temp;
*num = 0;
if (theline[0] != ':')
return 0;
if (strlen(theline) < 11)
return 0;
ptr = theline + 1;
if (!sscanf(ptr, "%02x", &len))
return 0;
ptr += 2;
if (strlen(theline) < (11 + (len * 2)))
return 0;
if (!sscanf(ptr, "%04x", (unsigned int*)addr))
return 0;
ptr += 4;
/* Serial.printf("Line: length=%d Addr=%d\n", len, *addr); */
if (!sscanf(ptr, "%02x", code))
return 0;
ptr += 2;
sum = (len & 255) + ((*addr >> 8) & 255) + (*addr & 255) + (*code & 255);
while (*num != len)
{
if (!sscanf(ptr, "%02x", &temp))
return 0;
bytes[*num] = temp;
ptr += 2;
sum += bytes[*num] & 255;
(*num)++;
if (*num >= 256)
return 0;
}
if (!sscanf(ptr, "%02x", &cksum))
return 0;
if (((sum & 255) + (cksum & 255)) & 255)
return 0; /* checksum error */
return 1;
}