|
@@ -110,24 +110,17 @@ const char *resultMessages[] = {
|
110
|
110
|
// 230400b always manages to connect.
|
111
|
111
|
static const uint32_t uploadBaudRates[] = { 460800, 230400, 115200, 74880 };
|
112
|
112
|
|
113
|
|
-
|
114
|
|
-
|
115
|
113
|
signed char IsReady() {
|
116
|
114
|
return esp_upload.state == upload_idle;
|
117
|
115
|
}
|
118
|
116
|
|
119
|
|
-
|
120
|
|
-
|
121
|
|
-
|
122
|
|
-
|
123
|
117
|
void uploadPort_write(const uint8_t *buf, size_t len) {
|
124
|
118
|
#if 0
|
125
|
119
|
int i;
|
126
|
120
|
|
127
|
|
- for(i = 0; i < len; i++) {
|
128
|
|
- while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
|
129
|
|
-
|
130
|
|
- USART_SendData(USART1, *(buf + i));
|
|
121
|
+ for (i = 0; i < len; i++) {
|
|
122
|
+ while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) { /* nada */ }
|
|
123
|
+ USART_SendData(USART1, *(buf + i));
|
131
|
124
|
}
|
132
|
125
|
#endif
|
133
|
126
|
}
|
|
@@ -138,28 +131,22 @@ char uploadPort_read() {
|
138
|
131
|
return retChar;
|
139
|
132
|
else
|
140
|
133
|
return 0;
|
141
|
|
-
|
142
|
134
|
}
|
143
|
135
|
|
144
|
136
|
int uploadPort_available() {
|
145
|
137
|
return usartFifoAvailable(&WifiRxFifo);
|
146
|
138
|
}
|
147
|
139
|
|
148
|
|
-
|
149
|
140
|
void uploadPort_begin() {
|
150
|
141
|
esp_port_begin(1);
|
151
|
142
|
}
|
152
|
143
|
|
153
|
144
|
void uploadPort_close() {
|
154
|
|
-
|
155
|
145
|
//WIFI_COM.end();
|
156
|
146
|
//WIFI_COM.begin(115200, true);
|
157
|
|
-
|
158
|
147
|
esp_port_begin(0);
|
159
|
|
-
|
160
|
148
|
}
|
161
|
149
|
|
162
|
|
-
|
163
|
150
|
void flushInput() {
|
164
|
151
|
while (uploadPort_available() != 0) {
|
165
|
152
|
(void)uploadPort_read();
|
|
@@ -304,7 +291,6 @@ EspUploadResult readPacket(uint8_t op, uint32_t *valp, size_t *bodyLen, uint32_t
|
304
|
291
|
|
305
|
292
|
*bodyLen = 0;
|
306
|
293
|
|
307
|
|
-
|
308
|
294
|
while (state != done) {
|
309
|
295
|
uint8_t c;
|
310
|
296
|
EspUploadResult stat;
|
|
@@ -322,73 +308,73 @@ EspUploadResult readPacket(uint8_t op, uint32_t *valp, size_t *bodyLen, uint32_t
|
322
|
308
|
}
|
323
|
309
|
|
324
|
310
|
// sufficient bytes have been received for the current state, process them
|
325
|
|
- switch(state) {
|
326
|
|
- case begin: // expecting frame start
|
327
|
|
- c = uploadPort_read();
|
328
|
|
- if (c != (uint8_t)0xC0) {
|
329
|
|
- break;
|
330
|
|
- }
|
331
|
|
- state = header;
|
332
|
|
- needBytes = 2;
|
|
311
|
+ switch (state) {
|
|
312
|
+ case begin: // expecting frame start
|
|
313
|
+ c = uploadPort_read();
|
|
314
|
+ if (c != (uint8_t)0xC0) {
|
|
315
|
+ break;
|
|
316
|
+ }
|
|
317
|
+ state = header;
|
|
318
|
+ needBytes = 2;
|
333
|
319
|
|
334
|
|
- break;
|
335
|
|
- case end: // expecting frame end
|
336
|
|
- c = uploadPort_read();
|
337
|
|
- if (c != (uint8_t)0xC0) {
|
338
|
|
- return slipFrame;
|
339
|
|
- }
|
340
|
|
- state = done;
|
|
320
|
+ break;
|
|
321
|
+ case end: // expecting frame end
|
|
322
|
+ c = uploadPort_read();
|
|
323
|
+ if (c != (uint8_t)0xC0) {
|
|
324
|
+ return slipFrame;
|
|
325
|
+ }
|
|
326
|
+ state = done;
|
341
|
327
|
|
342
|
|
- break;
|
|
328
|
+ break;
|
343
|
329
|
|
344
|
|
- case header: // reading an 8-byte header
|
345
|
|
- case body: // reading the response body
|
346
|
|
- {
|
347
|
|
- int rslt;
|
348
|
|
- // retrieve a byte with SLIP decoding
|
349
|
|
- rslt = ReadByte(&c, 1);
|
350
|
|
- if (rslt != 1 && rslt != 2) {
|
351
|
|
- // some error occurred
|
352
|
|
- stat = (rslt == 0 || rslt == -2) ? slipData : slipFrame;
|
353
|
|
- return stat;
|
354
|
|
- }
|
355
|
|
- else if (state == header) {
|
356
|
|
- //store the header byte
|
357
|
|
- hdr[hdrIdx++] = c;
|
358
|
|
- if (hdrIdx >= headerLength) {
|
359
|
|
- // get the body length, prepare a buffer for it
|
360
|
|
- *bodyLen = (uint16_t)getData(2, hdr, 2);
|
361
|
|
-
|
362
|
|
- // extract the value, if requested
|
363
|
|
- if (valp != 0) {
|
364
|
|
- *valp = getData(4, hdr, 4);
|
|
330
|
+ case header: // reading an 8-byte header
|
|
331
|
+ case body: // reading the response body
|
|
332
|
+ {
|
|
333
|
+ int rslt;
|
|
334
|
+ // retrieve a byte with SLIP decoding
|
|
335
|
+ rslt = ReadByte(&c, 1);
|
|
336
|
+ if (rslt != 1 && rslt != 2) {
|
|
337
|
+ // some error occurred
|
|
338
|
+ stat = (rslt == 0 || rslt == -2) ? slipData : slipFrame;
|
|
339
|
+ return stat;
|
|
340
|
+ }
|
|
341
|
+ else if (state == header) {
|
|
342
|
+ //store the header byte
|
|
343
|
+ hdr[hdrIdx++] = c;
|
|
344
|
+ if (hdrIdx >= headerLength) {
|
|
345
|
+ // get the body length, prepare a buffer for it
|
|
346
|
+ *bodyLen = (uint16_t)getData(2, hdr, 2);
|
|
347
|
+
|
|
348
|
+ // extract the value, if requested
|
|
349
|
+ if (valp != 0) {
|
|
350
|
+ *valp = getData(4, hdr, 4);
|
|
351
|
+ }
|
|
352
|
+
|
|
353
|
+ if (*bodyLen != 0) {
|
|
354
|
+ state = body;
|
|
355
|
+ }
|
|
356
|
+ else {
|
|
357
|
+ needBytes = 1;
|
|
358
|
+ state = end;
|
|
359
|
+ }
|
365
|
360
|
}
|
366
|
|
-
|
367
|
|
- if (*bodyLen != 0) {
|
368
|
|
- state = body;
|
|
361
|
+ }
|
|
362
|
+ else {
|
|
363
|
+ // Store the response body byte, check for completion
|
|
364
|
+ if (bodyIdx < ARRAY_SIZE(respBuf)) {
|
|
365
|
+ respBuf[bodyIdx] = c;
|
369
|
366
|
}
|
370
|
|
- else {
|
|
367
|
+ ++bodyIdx;
|
|
368
|
+ if (bodyIdx >= *bodyLen) {
|
371
|
369
|
needBytes = 1;
|
372
|
370
|
state = end;
|
373
|
371
|
}
|
374
|
372
|
}
|
375
|
373
|
}
|
376
|
|
- else {
|
377
|
|
- // Store the response body byte, check for completion
|
378
|
|
- if (bodyIdx < ARRAY_SIZE(respBuf)) {
|
379
|
|
- respBuf[bodyIdx] = c;
|
380
|
|
- }
|
381
|
|
- ++bodyIdx;
|
382
|
|
- if (bodyIdx >= *bodyLen) {
|
383
|
|
- needBytes = 1;
|
384
|
|
- state = end;
|
385
|
|
- }
|
386
|
|
- }
|
387
|
|
- }
|
388
|
|
- break;
|
|
374
|
+ break;
|
389
|
375
|
|
390
|
|
- default: // this shouldn't happen
|
391
|
|
- return slipState;
|
|
376
|
+ default: // this shouldn't happen
|
|
377
|
+ return slipState;
|
392
|
378
|
}
|
393
|
379
|
}
|
394
|
380
|
|
|
@@ -397,7 +383,7 @@ EspUploadResult readPacket(uint8_t op, uint32_t *valp, size_t *bodyLen, uint32_t
|
397
|
383
|
opRet = (uint8_t)getData(1, hdr, 1);
|
398
|
384
|
// Sync packets often provoke a response with a zero opcode instead of ESP_SYNC
|
399
|
385
|
if (resp != 0x01 || opRet != op) {
|
400
|
|
-//debug//printf("resp %02x %02x\n", resp, opRet);
|
|
386
|
+ //printf("resp %02x %02x\n", resp, opRet); //debug
|
401
|
387
|
return respHeader;
|
402
|
388
|
}
|
403
|
389
|
|
|
@@ -432,20 +418,19 @@ void _writePacket(const uint8_t *data, size_t len) {
|
432
|
418
|
// 0xC0 and 0xDB replaced by the two-byte sequences {0xDB, 0xDC} and {0xDB, 0xDD} respectively.
|
433
|
419
|
|
434
|
420
|
void writePacket(const uint8_t *hdr, size_t hdrLen, const uint8_t *data, size_t dataLen) {
|
435
|
|
-
|
436
|
|
- WriteByteRaw(0xC0); // send the packet start character
|
|
421
|
+ WriteByteRaw(0xC0); // send the packet start character
|
437
|
422
|
_writePacket(hdr, hdrLen); // send the header
|
438
|
|
- _writePacket(data, dataLen); // send the data block
|
439
|
|
- WriteByteRaw(0xC0); // send the packet end character
|
|
423
|
+ _writePacket(data, dataLen); // send the data block
|
|
424
|
+ WriteByteRaw(0xC0); // send the packet end character
|
440
|
425
|
}
|
441
|
426
|
|
442
|
427
|
// Send a packet to the serial port while performing SLIP framing. The packet data comprises a header and an optional data block.
|
443
|
428
|
// This is like writePacket except that it does a fast block write for both the header and the main data with no SLIP encoding. Used to send sync commands.
|
444
|
429
|
void writePacketRaw(const uint8_t *hdr, size_t hdrLen, const uint8_t *data, size_t dataLen) {
|
445
|
|
- WriteByteRaw(0xC0); // send the packet start character
|
446
|
|
- _writePacketRaw(hdr, hdrLen); // send the header
|
|
430
|
+ WriteByteRaw(0xC0); // send the packet start character
|
|
431
|
+ _writePacketRaw(hdr, hdrLen); // send the header
|
447
|
432
|
_writePacketRaw(data, dataLen); // send the data block in raw mode
|
448
|
|
- WriteByteRaw(0xC0); // send the packet end character
|
|
433
|
+ WriteByteRaw(0xC0); // send the packet end character
|
449
|
434
|
}
|
450
|
435
|
|
451
|
436
|
// Send a command to the attached device together with the supplied data, if any.
|
|
@@ -460,12 +445,10 @@ void sendCommand(uint8_t op, uint32_t checkVal, const uint8_t *data, size_t data
|
460
|
445
|
|
461
|
446
|
// send the packet
|
462
|
447
|
//flushInput();
|
463
|
|
- if (op == ESP_SYNC) {
|
|
448
|
+ if (op == ESP_SYNC)
|
464
|
449
|
writePacketRaw(hdr, sizeof(hdr), data, dataLen);
|
465
|
|
- }
|
466
|
|
- else {
|
|
450
|
+ else
|
467
|
451
|
writePacket(hdr, sizeof(hdr), data, dataLen);
|
468
|
|
- }
|
469
|
452
|
}
|
470
|
453
|
|
471
|
454
|
// Send a command to the attached device together with the supplied data, if any, and get the response
|
|
@@ -476,9 +459,8 @@ EspUploadResult doCommand(uint8_t op, const uint8_t *data, size_t dataLen, uint3
|
476
|
459
|
sendCommand(op, checkVal, data, dataLen);
|
477
|
460
|
|
478
|
461
|
stat = readPacket(op, valp, &bodyLen, msTimeout);
|
479
|
|
- if (stat == success && bodyLen != 2) {
|
|
462
|
+ if (stat == success && bodyLen != 2)
|
480
|
463
|
stat = badReply;
|
481
|
|
- }
|
482
|
464
|
|
483
|
465
|
return stat;
|
484
|
466
|
}
|
|
@@ -611,6 +593,8 @@ EspUploadResult flashWriteBlock(uint16_t flashParmVal, uint16_t flashParmMask) {
|
611
|
593
|
//printf("Upload %d\%\n", ftell(&esp_upload.uploadFile) * 100 / esp_upload.fileSize);
|
612
|
594
|
|
613
|
595
|
return stat;
|
|
596
|
+ #else
|
|
597
|
+ return success;
|
614
|
598
|
#endif
|
615
|
599
|
}
|
616
|
600
|
|
|
@@ -625,15 +609,14 @@ void upload_spin() {
|
625
|
609
|
esp_upload.uploadResult = connected;
|
626
|
610
|
esp_upload.state = done;
|
627
|
611
|
}
|
628
|
|
- else{
|
|
612
|
+ else {
|
629
|
613
|
|
630
|
614
|
// Reset the serial port at the new baud rate. Also reset the ESP8266.
|
631
|
|
- // const uint32_t baud = uploadBaudRates[esp_upload.connectAttemptNumber/esp_upload.retriesPerBaudRate];
|
|
615
|
+ // const uint32_t baud = uploadBaudRates[esp_upload.connectAttemptNumber/esp_upload.retriesPerBaudRate];
|
632
|
616
|
if (esp_upload.connectAttemptNumber % esp_upload.retriesPerBaudRate == 0) {
|
633
|
617
|
}
|
634
|
|
- // uploadPort.begin(baud);
|
635
|
|
- // uploadPort_close();
|
636
|
|
-
|
|
618
|
+ //uploadPort.begin(baud);
|
|
619
|
+ //uploadPort_close();
|
637
|
620
|
|
638
|
621
|
uploadPort_begin();
|
639
|
622
|
|
|
@@ -654,7 +637,7 @@ void upload_spin() {
|
654
|
637
|
esp_upload.lastAttemptTime = getWifiTick();
|
655
|
638
|
if (res == success) {
|
656
|
639
|
// Successful connection
|
657
|
|
-// //MessageF(" success on attempt %d\n", (connectAttemptNumber % retriesPerBaudRate) + 1);
|
|
640
|
+ //MessageF(" success on attempt %d\n", (connectAttemptNumber % retriesPerBaudRate) + 1);
|
658
|
641
|
//printf("connect success\n");
|
659
|
642
|
esp_upload.state = erasing;
|
660
|
643
|
}
|
|
@@ -675,14 +658,13 @@ void upload_spin() {
|
675
|
658
|
const uint32_t sectorSize = 4096;
|
676
|
659
|
const uint32_t numSectors = (esp_upload.fileSize + sectorSize - 1)/sectorSize;
|
677
|
660
|
const uint32_t startSector = esp_upload.uploadAddress/sectorSize;
|
|
661
|
+
|
678
|
662
|
uint32_t headSectors = sectorsPerBlock - (startSector % sectorsPerBlock);
|
|
663
|
+ NOMORE(headSectors, numSectors);
|
679
|
664
|
|
680
|
|
- if (numSectors < headSectors) {
|
681
|
|
- headSectors = numSectors;
|
682
|
|
- }
|
683
|
|
- eraseSize = (numSectors < 2 * headSectors)
|
684
|
|
- ? (numSectors + 1) / 2 * sectorSize
|
685
|
|
- : (numSectors - headSectors) * sectorSize;
|
|
665
|
+ eraseSize = (numSectors < 2 * headSectors)
|
|
666
|
+ ? (numSectors + 1) / 2 * sectorSize
|
|
667
|
+ : (numSectors - headSectors) * sectorSize;
|
686
|
668
|
|
687
|
669
|
//MessageF("Erasing %u bytes...\n", fileSize);
|
688
|
670
|
esp_upload.uploadResult = flashBegin(esp_upload.uploadAddress, eraseSize);
|
|
@@ -755,7 +737,7 @@ void SendUpdateFile(const char *file, uint32_t address) {
|
755
|
737
|
|
756
|
738
|
if (res != FR_OK) return;
|
757
|
739
|
|
758
|
|
- esp_upload.fileSize = f_size(&esp_upload.uploadFile);
|
|
740
|
+ esp_upload.fileSize = f_size(&esp_upload.uploadFile);
|
759
|
741
|
if (esp_upload.fileSize == 0) {
|
760
|
742
|
f_close(&esp_upload.uploadFile);
|
761
|
743
|
return;
|