-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSerialUploader.ino
422 lines (369 loc) · 10.4 KB
/
SerialUploader.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Serial uploader sketch for Arduino
// License: GNU GENERAL PUBLIC LICENSE Version 2
//
// Edit constants in SerialUploader.h
//
// Credits:
// https://github.com/robokoding/STK500
// https://github.com/Optiboot/optiboot
// https://github.com/nickgammon/arduino_sketches
#include <avr/wdt.h>
#include "SerialUploader.h"
#include "Stk500Client.h"
#include "UploaderUI.h"
#include "SerialUI.h"
#include "SDSketchSource.h"
#include "SDUI.h"
#include "Signatures.h"
#include "BaudRateChoices.h"
#include "LcdUI.h"
SdFat sd;
#ifdef LCD_ADDRESS
SDUI __sdui(sd);
UploaderUI &&ui = LcdUI(__sdui);
#elif defined(SERIAL_UI)
UploaderUI &&ui = SerialUI(SERIAL_UI);
#else
UploaderUI &&ui = SDUI(sd);
#endif
#ifdef SERIAL_TARGET
Stk500Client client(ui, SERIAL_TARGET);
#else
Stk500Client client(ui, Serial);
#endif
SketchSource &&sketch = SDSketchSource(ui, sd);
enum class UploadState
{
Start, CheckFlags, StartSketch, Syncing, Identify, Upload, Success, Error, ShowError, ShowSuccess
};
UploadState uploadState;
uint8_t statusFlags;
int autoBaud;
uint32_t baudRate;
SignatureType mcuSignature;
// Prototypes
// ==============
bool sdBegin();
bool uiBegin();
uint32_t getBaudRate();
void cleanup();
void resetTarget();
bool check(StkResponse response);
bool sync(uint32_t baudRate);
UploadState upload();
void blink(uint8_t count);
bool readSignature();
bool identifyMCU(byte *sig, size_t sigSize);
// Setup
// ===========
void setup()
{
statusFlags = MCUSR;
MCUSR = 0x00;
#if defined(DEBUG) && defined(SERIAL_UI)
SERIAL_UI.begin(115200);
#endif
wdt_enable(WDTO_8S);
#ifdef PIN_DTR
pinMode(PIN_DTR, OUTPUT);
digitalWrite(PIN_DTR, HIGH);
#endif
#ifdef PIN_RESET
pinMode(PIN_RESET, OUTPUT);
#endif
uploadState = UploadState::Start;
}
void loop()
{
wdt_reset();
switch (uploadState) {
case UploadState::Start:
uploadState = sdBegin() && uiBegin() ? UploadState::CheckFlags : UploadState::Error;
break;
case UploadState::CheckFlags:
if (statusFlags & _BV(WDRF)) {
ui.println(F("Uploader timed out, please reset"));
uploadState = UploadState::Error;
}
else {
uploadState = UploadState::StartSketch;
}
break;
case UploadState::StartSketch:
if (!sketch.begin()) {
uploadState = UploadState::Error;
} else {
baudRate = getBaudRate();
autoBaud = (baudRate == AUTO_BAUD_RATE) ? 0 : -1;
#ifndef SERIAL_TARGET
ui.println(F("No programming serial, done."));
uploadState = UploadState::Success;
#else
uploadState = UploadState::Syncing;
#endif
}
break;
case UploadState::Syncing:
if (autoBaud < 0) {
// Fixed
uploadState = sync(baudRate) ?
UploadState::Identify : UploadState::Error;
} else if (autoBaud < NUMITEMS(BAUD_RATES)) {
// Auto-detect
if (sync(BAUD_RATES[autoBaud])) {
uploadState = UploadState::Identify;
} else {
autoBaud++;
}
} else {
// auto-detect, out of options
ui.println(F("Could not synchronize w/ target"));
uploadState = UploadState::Error;
}
break;
case UploadState::Identify:
uploadState = readSignature() ? UploadState::Upload : UploadState::Error;
break;
case UploadState::Upload:
uploadState = upload();
break;
case UploadState::Error:
ui.println(F("Serial upload failed."));
cleanup();
uploadState = UploadState::ShowError;
break;
case UploadState::ShowError:
blink(5);
delay(800);
break;
case UploadState::Success:
cleanup();
uploadState = UploadState::ShowSuccess;
break;
case UploadState::ShowSuccess:
blink(2);
delay(800);
break;
}
}
uint32_t getBaudRate()
{
BaudRateChoices choices;
int8_t choice = ui.choose(choices);
return (choice <= 0) ? AUTO_BAUD_RATE : BAUD_RATES[choice - 1];
}
bool sdBegin()
{
sd.begin(SS_SD);
if (sd.cardErrorCode()) {
#if defined(DEBUG) && defined(SERIAL_UI)
SERIAL_UI.println(F("Initializing SD card"));
#endif
return false;
}
return true;
}
bool uiBegin()
{
if (!ui.begin()) {
#if defined(DEBUG) && defined(SERIAL_UI)
SERIAL_UI.println(F("UI failed to start!"));
#endif
return false;
}
ui.println(F("Serial uploader starting..."));
// ui.flush();
return true;
}
void cleanup()
{
ui.end();
SPI.end();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
bool sync(uint32_t baudRate)
{
wdt_reset();
client.begin(baudRate);
resetTarget();
ui.print(F("Trying "));
ui.print(baudRate);
ui.print(F(" bps..."));
// Sync with target
uint8_t syncCount = 0;
for (int i = 0; i < 8 && syncCount < 1; i++) {
wdt_reset();
Stk status = client.sync();
if (status == Stk::OK) {
syncCount++;
}
}
if (syncCount < 1) {
ui.println(F(" failed"));
client.end();
return false;
}
ui.println(F(" done."));
ui.println(F("AVR device initialized and ready"));
return true;
}
bool readSignature()
{
StkResponse response;
response = client.readSignature();
if (!check(response.status, F("Could not read device signature"))) {
return false;
}
ui.print(F("Device signature: "));
for (int j = 0; j < response.size; ++j) {
printByte(ui, response.data[j]);
}
ui.println();
if (! identifyMCU(response.data, response.size)) {
ui.println(F("Unknown MCU, need page size"));
return false;
}
ui.print(F("AVR part: ")); ui.println(mcuSignature.desc);
ui.print(F("Flash size: ")); ui.println(mcuSignature.flashSize);
ui.print(F("Page size: ")); ui.println(mcuSignature.pageSize);
// Get parameters, for fun
response = client.getParameter(StkParam::SW_MAJOR);
uint8_t major = response.data[0];
response = client.getParameter(StkParam::STK_SW_MINOR);
ui.print(F("Stk500 kit version: "));
ui.print(major, DEC);
ui.print('.');
ui.println(response.data[0], DEC);
wdt_reset();
return true;
}
UploadState upload()
{
StkResponse response;
Stk status;
// Program
status = client.enterProgramming();
if (!check(status, F("Could not enter programming mode"))) {
return UploadState::Error;
}
ui.println(F("Writing flash..."));
uint8_t hex[mcuSignature.pageSize];
// byte count
int count;
uint32_t byteAddress = 0;
do {
count = sketch.readBytes(hex, mcuSignature.pageSize);
if (count < 0) {
ui.println(F("SD read failure!"));
return UploadState::Error;
}
if (count > 0) {
status = client.loadAddress(byteAddress / 2);
if (!check(status, F("Could not load address."))) {
return UploadState::Error;
}
status = client.writeFlash((uint16_t) count, hex);
if (!check(status, F("Could not program page!"))) {
return UploadState::Error;
}
byteAddress += count;
}
wdt_reset();
} while (count > 0);
ui.print(byteAddress);
ui.println(F(" bytes of flash written."));
ui.println(F("Verifying flash..."));
sketch.reset();
byteAddress = 0;
client.resetAddress();
uint8_t targetBuf[mcuSignature.pageSize];
do {
count = sketch.readBytes(hex, mcuSignature.pageSize);
if (count < 0) {
ui.println(F("SD read failure!"));
return UploadState::Error;
}
if (count > 0) {
status = client.loadAddress(byteAddress / 2);
if (!check(status, F("Could not load address."))) {
return UploadState::Error;
}
byteAddress += count;
response = client.readFlash((uint16_t) count, targetBuf);
if (!check(status, F("Could not read page!"))) {
return UploadState::Error;
}
if (response.size != count) {
ui.println(F("Read flash error: got "));
ui.print(response.size);
ui.print(F(" bytes out of "));
ui.print(count);
ui.println(F(" requested"));
return UploadState::Error;
}
for (int i = 0; i < count; ++i) {
if (hex[i] != targetBuf[i]) {
ui.print(F("Vrfy error at byte "));
ui.println(byteAddress + i, HEX);
ui.print(F("Exp: 0x"));
ui.print(hex[i], HEX);
ui.print(F(", Got: 0x"));
ui.println(targetBuf[i], HEX);
return UploadState::Error;
}
}
}
wdt_reset();
} while (count > 0);
ui.print(byteAddress);
ui.println(F("b verified."));
status = client.leaveProgramming();
if (!check(status, F("Could not leave programming mode."))) {
return UploadState::Error;
}
ui.println(F("Uploading done."));
// client.end();
return UploadState::Success;
}
bool identifyMCU(byte *sig, size_t sigSize)
{
for (int j = 0; j < NUMITEMS (AVR_SIGNATURES); j++) {
memcpy_P(&mcuSignature, &AVR_SIGNATURES[j], sizeof mcuSignature);
if (memcmp(sig, mcuSignature.sig, sigSize) == 0) {
return true;
}
}
return false;
}
void blink(uint8_t count)
{
for (int i = 0; i < count; ++i) {
digitalWrite(LED_BUILTIN, HIGH);
delay(75);
digitalWrite(LED_BUILTIN, LOW);
delay(75);
}
}
bool check(Stk status, FString msg)
{
if (status != Stk::OK) {
ui.print(msg);
ui.print(F(": 0x"));
ui.println((uint8_t) status, HEX);
return false;
}
return true;
}
void resetTarget()
{
#ifdef PIN_RESET
ui.print(F("Resetting target board..."));
// Reset the target board
digitalWrite(PIN_RESET, LOW);
delay(DELAY_RESET);
digitalWrite(PIN_RESET, HIGH);
delay(DELAY_RESET);
ui.println(F(" done."));
#endif
}