forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Esp.cpp
443 lines (368 loc) · 11 KB
/
Esp.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
/*
Esp.cpp - ESP8266-specific APIs
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
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
*/
#include "Arduino.h"
#include "flash_utils.h"
#include "eboot_command.h"
#include <memory>
extern "C" {
#include "user_interface.h"
extern struct rst_info resetInfo;
}
// #define DEBUG_SERIAL Serial
//extern "C" void ets_wdt_init(uint32_t val);
extern "C" void ets_wdt_enable(void);
extern "C" void ets_wdt_disable(void);
extern "C" void wdt_feed(void) {
}
/**
* User-defined Literals
* usage:
*
* uint32_t = test = 10_MHz; // --> 10000000
*/
unsigned long long operator"" _kHz(unsigned long long x) {
return x * 1000;
}
unsigned long long operator"" _MHz(unsigned long long x) {
return x * 1000 * 1000;
}
unsigned long long operator"" _GHz(unsigned long long x) {
return x * 1000 * 1000 * 1000;
}
unsigned long long operator"" _kBit(unsigned long long x) {
return x * 1024;
}
unsigned long long operator"" _MBit(unsigned long long x) {
return x * 1024 * 1024;
}
unsigned long long operator"" _GBit(unsigned long long x) {
return x * 1024 * 1024 * 1024;
}
unsigned long long operator"" _kB(unsigned long long x) {
return x * 1024;
}
unsigned long long operator"" _MB(unsigned long long x) {
return x * 1024 * 1024;
}
unsigned long long operator"" _GB(unsigned long long x) {
return x * 1024 * 1024 * 1024;
}
EspClass ESP;
EspClass::EspClass()
{
}
void EspClass::wdtEnable(uint32_t timeout_ms)
{
//todo find doku for ets_wdt_init may set the timeout
ets_wdt_enable();
}
void EspClass::wdtEnable(WDTO_t timeout_ms)
{
wdtEnable((uint32_t) timeout_ms);
}
void EspClass::wdtDisable(void)
{
ets_wdt_disable();
}
void EspClass::wdtFeed(void)
{
wdt_feed();
}
void EspClass::deepSleep(uint32_t time_us, WakeMode mode)
{
system_deep_sleep_set_option(static_cast<int>(mode));
system_deep_sleep(time_us);
}
void EspClass::reset(void)
{
((void (*)(void))0x40000080)();
}
void EspClass::restart(void)
{
system_restart();
}
uint16_t EspClass::getVcc(void)
{
return system_get_vdd33();
}
uint32_t EspClass::getFreeHeap(void)
{
return system_get_free_heap_size();
}
uint32_t EspClass::getChipId(void)
{
return system_get_chip_id();
}
const char * EspClass::getSdkVersion(void)
{
return system_get_sdk_version();
}
uint8_t EspClass::getBootVersion(void)
{
return system_get_boot_version();
}
uint8_t EspClass::getBootMode(void)
{
return system_get_boot_mode();
}
uint8_t EspClass::getCpuFreqMHz(void)
{
return system_get_cpu_freq();
}
uint32_t EspClass::getFlashChipId(void)
{
return spi_flash_get_id();
}
uint32_t EspClass::getFlashChipRealSize(void)
{
return (1 << ((spi_flash_get_id() >> 16) & 0xFF));
}
uint32_t EspClass::getFlashChipSize(void)
{
uint32_t data;
uint8_t * bytes = (uint8_t *) &data;
// read first 4 byte (magic byte + flash config)
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
switch((bytes[3] & 0xf0) >> 4) {
case 0x0: // 4 Mbit (512KB)
return (512_kB);
case 0x1: // 2 MBit (256KB)
return (256_kB);
case 0x2: // 8 MBit (1MB)
return (1_MB);
case 0x3: // 16 MBit (2MB)
return (2_MB);
case 0x4: // 32 MBit (4MB)
return (4_MB);
case 0x5: // 64 MBit (8MB)
return (8_MB);
case 0x6: // 128 MBit (16MB)
return (16_MB);
case 0x7: // 256 MBit (32MB)
return (32_MB);
default: // fail?
return 0;
}
}
return 0;
}
uint32_t EspClass::getFlashChipSpeed(void)
{
uint32_t data;
uint8_t * bytes = (uint8_t *) &data;
// read first 4 byte (magic byte + flash config)
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
switch(bytes[3] & 0x0F) {
case 0x0: // 40 MHz
return (40_MHz);
case 0x1: // 26 MHz
return (26_MHz);
case 0x2: // 20 MHz
return (20_MHz);
case 0xf: // 80 MHz
return (80_MHz);
default: // fail?
return 0;
}
}
return 0;
}
FlashMode_t EspClass::getFlashChipMode(void)
{
FlashMode_t mode = FM_UNKNOWN;
uint32_t data;
uint8_t * bytes = (uint8_t *) &data;
// read first 4 byte (magic byte + flash config)
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
mode = (FlashMode_t) bytes[2];
if(mode > FM_DOUT) {
mode = FM_UNKNOWN;
}
}
return mode;
}
/**
* Infos from
* http://www.wlxmall.com/images/stock_item/att/A1010004.pdf
* http://www.gigadevice.com/product-series/5.html?locale=en_US
* http://www.elinux.org/images/f/f5/Winbond-w25q32.pdf
*/
uint32_t EspClass::getFlashChipSizeByChipId(void) {
uint32_t chipId = getFlashChipId();
/**
* Chip ID
* 00 - always 00 (Chip ID use only 3 byte)
* 17 - ? looks like 2^xx is size in Byte ? //todo: find docu to this
* 40 - ? may be Speed ? //todo: find docu to this
* C8 - manufacturer ID
*/
switch(chipId) {
// GigaDevice
case 0x1740C8: // GD25Q64B
return (8_MB);
case 0x1640C8: // GD25Q32B
return (4_MB);
case 0x1540C8: // GD25Q16B
return (2_MB);
case 0x1440C8: // GD25Q80
return (1_MB);
case 0x1340C8: // GD25Q40
return (512_kB);
case 0x1240C8: // GD25Q20
return (256_kB);
case 0x1140C8: // GD25Q10
return (128_kB);
case 0x1040C8: // GD25Q12
return (64_kB);
// Winbond
case 0x1640EF: // W25Q32
return (4_MB);
case 0x1540EF: // W25Q16
return (2_MB);
case 0x1440EF: // W25Q80
return (1_MB);
default:
return 0;
}
}
String EspClass::getResetInfo(void) {
if(resetInfo.reason != 0) {
char buff[200];
sprintf(&buff[0], "Fatal exception:%d flag:%d (%s) epc1:0x%08x epc2:0x%08x epc3:0x%08x excvaddr:0x%08x depc:0x%08x", resetInfo.exccause, resetInfo.reason, (resetInfo.reason == 0 ? "DEFAULT" : resetInfo.reason == 1 ? "WDT" : resetInfo.reason == 2 ? "EXCEPTION" : resetInfo.reason == 3 ? "SOFT_WDT" : resetInfo.reason == 4 ? "SOFT_RESTART" : resetInfo.reason == 5 ? "DEEP_SLEEP_AWAKE" : "???"), resetInfo.epc1, resetInfo.epc2, resetInfo.epc3, resetInfo.excvaddr, resetInfo.depc);
return String(buff);
}
return String("flag: 0");
}
struct rst_info * EspClass::getResetInfoPtr(void) {
return &resetInfo;
}
bool EspClass::eraseConfig(void) {
bool ret = true;
size_t cfgAddr = (ESP.getFlashChipSize() - 0x4000);
size_t cfgSize = (8*1024);
noInterrupts();
while(cfgSize) {
if(spi_flash_erase_sector((cfgAddr / SPI_FLASH_SEC_SIZE)) != SPI_FLASH_RESULT_OK) {
ret = false;
}
cfgSize -= SPI_FLASH_SEC_SIZE;
cfgAddr += SPI_FLASH_SEC_SIZE;
}
interrupts();
return ret;
}
uint32_t EspClass::getSketchSize() {
static uint32_t result = 0;
if (result)
return result;
image_header_t image_header;
uint32_t pos = APP_START_OFFSET;
if (spi_flash_read(pos, (uint32_t*) &image_header, sizeof(image_header))) {
return 0;
}
pos += sizeof(image_header);
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.printf("num_segments=%u\r\n", image_header.num_segments);
#endif
for (uint32_t section_index = 0;
section_index < image_header.num_segments;
++section_index)
{
section_header_t section_header = {0};
if (spi_flash_read(pos, (uint32_t*) §ion_header, sizeof(section_header))) {
return 0;
}
pos += sizeof(section_header);
pos += section_header.size;
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.printf("section=%u size=%u pos=%u\r\n", section_index, section_header.size, pos);
#endif
}
result = pos;
return result;
}
extern "C" uint32_t _SPIFFS_start;
uint32_t EspClass::getFreeSketchSpace() {
uint32_t usedSize = getSketchSize();
// round one sector up
uint32_t freeSpaceStart = (usedSize + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
uint32_t freeSpaceEnd = (uint32_t)&_SPIFFS_start - 0x40200000;
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.printf("usedSize=%u freeSpaceStart=%u freeSpaceEnd=%u\r\n", usedSize, freeSpaceStart, freeSpaceEnd);
#endif
return freeSpaceEnd - freeSpaceStart;
}
bool EspClass::updateSketch(Stream& in, uint32_t size) {
if (size > getFreeSketchSpace())
return false;
uint32_t usedSize = getSketchSize();
uint32_t freeSpaceStart = (usedSize + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.printf("erase @0x%x size=0x%x\r\n", freeSpaceStart, roundedSize);
#endif
noInterrupts();
int rc = SPIEraseAreaEx(freeSpaceStart, roundedSize);
interrupts();
if (rc)
return false;
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("erase done");
#endif
uint32_t addr = freeSpaceStart;
uint32_t left = size;
const uint32_t bufferSize = FLASH_SECTOR_SIZE;
std::unique_ptr<uint8_t> buffer(new uint8_t[bufferSize]);
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("writing");
#endif
while (left > 0) {
size_t willRead = (left < bufferSize) ? left : bufferSize;
size_t rd = in.readBytes(buffer.get(), willRead);
if (rd != willRead) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("stream read failed");
#endif
return false;
}
noInterrupts();
rc = SPIWrite(addr, buffer.get(), willRead);
interrupts();
if (rc) {
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("write failed");
#endif
return false;
}
addr += willRead;
left -= willRead;
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(".");
#endif
}
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println("\r\nrestarting");
#endif
eboot_command ebcmd;
ebcmd.action = ACTION_COPY_RAW;
ebcmd.args[0] = freeSpaceStart;
ebcmd.args[1] = 0x00000;
ebcmd.args[2] = size;
eboot_command_write(&ebcmd);
ESP.restart();
return true; // never happens
}