This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathESP32BootROM.cpp
681 lines (516 loc) · 16.7 KB
/
ESP32BootROM.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/****************************************************************************************************************************
ESP32BootROM.cpp - part of the Firmware Updater for the
Arduino Nano-33 IoT, MKR WiFi 1010, Arduino MKR Vidor 4000, and Arduino UNO WiFi Rev.2., Adafruit's nRF52 boards
Based on and modified from WiFiNINA library https://www.arduino.cc/en/Reference/WiFiNINA
to support nRF52, SAMD21/SAMD51, STM32F/L/H/G/WB/MP1, Teensy, etc. boards besides Nano-33 IoT, MKRWIFI1010, MKRVIDOR400, etc.
Built by Khoi Hoang https://github.com/khoih-prog/WiFiNINA_Generic
Licensed under MIT license
Copyright (c) 2018 Arduino SA. All rights reserved.
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************************************************************/
#ifdef ARDUINO_SAMD_MKRVIDOR4000
#include <VidorPeripherals.h>
#define NINA_GPIO0 FPGA_NINA_GPIO0
#define NINA_RESETN FPGA_SPIWIFI_RESET
#endif
#include <WiFiNINA_Pinout_Generic.h>
#include "ESP32BootROM.h"
ESP32BootROMClass::ESP32BootROMClass(HardwareSerial& serial, int gpio0Pin, int resetnPin) :
_serial(&serial),
_gpio0Pin(gpio0Pin),
_resetnPin(resetnPin)
{
}
int ESP32BootROMClass::begin(unsigned long baudrate)
{
#ifdef ARDUINO_SAMD_MKRVIDOR4000
FPGA.begin();
_serial->begin(119400);
FPGA.pinMode(_gpio0Pin, OUTPUT);
FPGA.pinMode(_resetnPin, OUTPUT);
FPGA.digitalWrite(_gpio0Pin, LOW);
FPGA.digitalWrite(_resetnPin, LOW);
delay(10);
FPGA.digitalWrite(_resetnPin, HIGH);
delay(100);
#elif defined(ARDUINO_AVR_UNO_WIFI_REV2)
_serial->begin(119400);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, LOW);
delay(100);
digitalWrite(_resetnPin, HIGH);
delay(100);
digitalWrite(_resetnPin, LOW);
#elif defined(NINA_B302_ublox)
// For NINA_B302_ublox boards. OK now
_serial->begin(115200);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, LOW);
delay(100);
digitalWrite(_resetnPin, HIGH);
delay(100);
// check if we need this
digitalWrite(_gpio0Pin, HIGH);
delay(100);
#elif defined(NRF52_SERIES)
// For NRF52_SERIES boards. To change accordingly
_serial->begin(115200);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, LOW);
delay(100);
digitalWrite(_resetnPin, HIGH);
delay(100);
// check if we need this
digitalWrite(_gpio0Pin, HIGH);
delay(100);
#elif ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
|| defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) \
|| defined(ARDUINO_SAMD_MKRWAN1310) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) \
|| defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) \
|| defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) || defined(__SAMD51G19A__) )
// For SAMD21 and SAMD51 boards. To change accordingly
_serial->begin(115200);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, HIGH);
delay(10);
digitalWrite(_resetnPin, LOW);
delay(100);
#if ( defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT) )
digitalWrite(_resetnPin, HIGH);
delay(100);
#endif
#elif ( defined(CORE_TEENSY) || defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__)\
|| defined(__MK20DX128__) )
// For Teensy boards. To change accordingly
_serial->begin(115200);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, HIGH);
delay(10);
digitalWrite(_resetnPin, LOW);
delay(100);
// To check if you need to do this
digitalWrite(_resetnPin, HIGH);
delay(100);
#elif defined(ARDUINO_NANO_RP2040_CONNECT) || ( defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) )
// For RP2040-based boards. To change accordingly
_serial->begin(115200);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, HIGH);
delay(10);
digitalWrite(_resetnPin, LOW);
delay(100);
#if defined(ARDUINO_NANO_RP2040_CONNECT)
digitalWrite(_resetnPin, HIGH);
delay(100);
#endif
#else
// For remaining boards. To change accordingly
_serial->begin(115200);
pinMode(_gpio0Pin, OUTPUT);
pinMode(_resetnPin, OUTPUT);
digitalWrite(_gpio0Pin, LOW);
digitalWrite(_resetnPin, HIGH);
delay(10);
digitalWrite(_resetnPin, LOW);
delay(100);
// To check if you need to do this
digitalWrite(_resetnPin, HIGH);
delay(100);
#endif
int synced = 0;
for (int retries = 0; !synced && (retries < 5); retries++)
{
synced = sync();
}
if (!synced)
{
return 0;
}
#if defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_NANO_RP2040_CONNECT)
(void)baudrate;
#else
if (baudrate != 115200)
{
if (!changeBaudrate(baudrate))
{
return 0;
}
delay(100);
_serial->end();
_serial->begin(baudrate);
}
#endif
if (!spiAttach())
{
return 0;
}
return 1;
}
void ESP32BootROMClass::end()
{
_serial->end();
}
int ESP32BootROMClass::sync()
{
const uint8_t data[] =
{
0x07, 0x07, 0x12, 0x20,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55
};
command(0x08, data, sizeof(data));
int results[8];
for (int i = 0; i < 8; i++)
{
results[i] = response(0x08, 100);
}
return (results[0] == 0);
}
int ESP32BootROMClass::changeBaudrate(unsigned long baudrate)
{
const uint32_t data[2] =
{
baudrate,
0
};
command(0x0f, data, sizeof(data));
return (response(0x0f, 3000) == 0);
}
int ESP32BootROMClass::spiAttach()
{
const uint8_t data[] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
command(0x0d, data, sizeof(data));
return (response(0x0d, 3000) == 0);
}
int ESP32BootROMClass::beginFlash(uint32_t offset, uint32_t size, uint32_t chunkSize)
{
const uint32_t data[4] =
{
size,
size / chunkSize,
chunkSize,
offset
};
command(0x02, data, sizeof(data));
_flashSequenceNumber = 0;
_chunkSize = chunkSize;
return (response(0x02, 120000) == 0);
}
int ESP32BootROMClass::dataFlash(const void* data, uint32_t length)
{
uint32_t cmdData[4 + (_chunkSize / 4)];
cmdData[0] = length;
cmdData[1] = _flashSequenceNumber++;
cmdData[2] = 0;
cmdData[3] = 0;
memcpy(&cmdData[4], data, length);
if (length < _chunkSize)
{
memset(&cmdData[4 + (length / 4)], 0xff, _chunkSize - length);
}
command(0x03, cmdData, sizeof(cmdData));
return (response(0x03, 3000) == 0);
}
int ESP32BootROMClass::endFlash(uint32_t reboot)
{
const uint32_t data[1] =
{
reboot
};
command(0x04, data, sizeof(data));
return (response(0x04, 3000) == 0);
}
int ESP32BootROMClass::md5Flash(uint32_t offset, uint32_t size, uint8_t* result)
{
const uint32_t data[4] =
{
offset,
size,
0,
0
};
command(0x13, data, sizeof(data));
uint8_t asciiResult[32];
if (response(0x13, 3000, asciiResult) != 0)
{
return 0;
}
char temp[3] = { 0, 0, 0 };
for (int i = 0; i < 16; i++)
{
temp[0] = asciiResult[i * 2];
temp[1] = asciiResult[i * 2 + 1];
result[i] = strtoul(temp, NULL, 16);
}
return 1;
}
void ESP32BootROMClass::command(int opcode, const void* data, uint16_t length)
{
uint32_t checksum = 0;
if (opcode == 0x03)
{
checksum = 0xef; // seed
for (uint16_t i = 16; i < length; i++)
{
checksum ^= ((const uint8_t*)data)[i];
}
}
_serial->write(0xc0);
_serial->write((uint8_t)0x00); // direction
_serial->write(opcode);
_serial->write((uint8_t*)&length, sizeof(length));
writeEscapedBytes((uint8_t*)&checksum, sizeof(checksum));
writeEscapedBytes((uint8_t*)data, length);
_serial->write(0xc0);
#ifdef ARDUINO_SAMD_MKRVIDOR4000
// _serial->flush(); // doesn't work!
#else
_serial->flush();
#endif
}
int ESP32BootROMClass::response(int opcode, unsigned long timeout, void* body)
{
uint8_t data[10 + 256];
uint16_t index = 0;
uint8_t responseLength = 4;
for (unsigned long start = millis(); (index < (uint16_t)(10 + responseLength)) && (millis() - start) < timeout;)
{
if (_serial->available())
{
data[index] = _serial->read();
if (index == 3)
{
responseLength = data[index];
}
index++;
}
}
#ifdef DEBUG
if (index)
{
for (int i = 0; i < index; i++)
{
byte b = data[i];
if (b < 0x10)
{
Serial.print('0');
}
Serial.print(b, HEX);
Serial.print(' ');
}
Serial.println();
}
#endif
if (index != (uint16_t)(10 + responseLength))
{
return -1;
}
if (data[0] != 0xc0 || data[1] != 0x01 || data[2] != opcode || data[responseLength + 5] != 0x00
|| data[responseLength + 6] != 0x00 || data[responseLength + 9] != 0xc0)
{
return -1;
}
if (body)
{
memcpy(body, &data[9], responseLength - 4);
}
return data[responseLength + 5];
}
void ESP32BootROMClass::writeEscapedBytes(const uint8_t* data, uint16_t length)
{
uint16_t written = 0;
while (written < length)
{
uint8_t b = data[written++];
if (b == 0xdb)
{
_serial->write(0xdb);
_serial->write(0xdd);
}
else if (b == 0xc0)
{
_serial->write(0xdb);
_serial->write(0xdc);
}
else
{
_serial->write(b);
}
}
}
/*
#define NINA_GPIO0 (12u) // 12, IO8, P1.00
#define NINA_RESETN (2u) // 2, IO21, P0.12
#define NINA_ACK (10u) // 10, IO2, P0.14
#define SPIWIFI_SS 4 //PIN_SPI1_SS // 4, IO1, P0.13
#define SPIWIFI_ACK 10 //NINA_ACK // 10, IO2, P0.14
#define SPIWIFI_RESET 2 //NINA_RESETN // 2, IO21, P0.12
*/
#if ( defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) \
|| defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_SAMD_MKRFox1200) || defined(ARDUINO_SAMD_MKRWAN1300) \
|| defined(ARDUINO_SAMD_MKRWAN1310) || defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) \
|| defined(ARDUINO_SAMD_MKRVIDOR4000) || defined(__SAMD21G18A__) || defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) \
|| defined(__SAMD51__) || defined(__SAMD51J20A__) || defined(__SAMD51J19A__) || defined(__SAMD51G19A__) )
// For SAMD21 and SAMD51 boards. To be changed and added as necessary
#warning To change the pin defintions to match actual assignment for your board
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (26u) //
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (27u) //
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (28u) //
#endif
#elif defined(NINA_B302_ublox) || defined(NRF52_SERIES)
#if defined(NINA_B302_ublox)
// For NINA_B302_ublox boards. To be changed and added as necessary
#warning To change the pin defintions to match actual assignment for NINA_B302_ublox
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (22u) // 12, IO8, P1.00
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (2u) // 2, IO21, P0.12
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (10u) // 10, IO2, P0.14
#endif
#elif defined(NRF52840_ITSYBITSY)
//Just a template for NRF52840_ITSYBITSY boards. Must be changed and added as necessary
#warning To change the pin defintions to match actual assignment for NRF52840_ITSYBITSY
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (12u) // 12, IO8, P1.00
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (2u) // 2, IO21, P0.12
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (10u) // 10, IO2, P0.14
#endif
#elif defined(NRF52_SERIES)
//Just a template for NRF52_SERIES boards. Must be changed and added as necessary
#warning To change the pin defintions to match actual assignment for NRF52_SERIES
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (12u) // 12, IO8, P1.00
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (2u) // 2, IO21, P0.12
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (10u) // 10, IO2, P0.14
#endif
#else
// For other nRF52 boards. To be changed and added as necessary
#warning To change the pin defintions to match actual assignment for nRF52
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (12u) // 12, IO8, P1.00
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (2u) // 2, IO21, P0.12
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (10u) // 10, IO2, P0.14
#endif
#endif //#if defined(NINA_B302_ublox)
#elif ( defined(CORE_TEENSY) || defined(__IMXRT1062__) || defined(__MK66FX1M0__) || defined(__MK64FX512__) || defined(__MK20DX256__)\
|| defined(__MK20DX128__) )
// For Teensy boards. To be changed and added as necessary
#warning To change the pin defintions to match actual assignment for your board
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (6u) //
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (2u) //
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (5u) //
#endif
#else
// For other boards. To be changed and added as necessary
#warning To change the pin defintions to match actual assignment for your board
#ifndef SerialNina
// Just need to do something such as
#define SerialNina Serial1 //Serial2, etc.
#endif
#ifndef NINA_GPIO0
// Just need assign some pin, such as
#define NINA_GPIO0 (12u) // 12, IO8, P1.00
#endif
#ifndef NINA_RESETN
// Just need assign some pin, such as
#define NINA_RESETN (2u) // 2, IO21, P0.12
#endif
#ifndef NINA_ACK
// Just need assign some pin, such as
#define NINA_ACK (10u) // 10, IO2, P0.14
#endif
#endif //#if defined(NINA_B302_ublox) || defined(NRF52_SERIES)
ESP32BootROMClass ESP32BootROM(SerialNina, NINA_GPIO0, NINA_RESETN);