Skip to content

Commit 5edcaa0

Browse files
author
Me No Dev
committed
Merge remote-tracking branch 'esp8266/master'
2 parents a331927 + cc0db8b commit 5edcaa0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3778
-698
lines changed

boards.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ wifinfo.build.f_cpu=80000000L
814814
wifinfo.build.core=esp8266
815815
wifinfo.build.variant=wifinfo
816816
wifinfo.build.flash_mode=qio
817+
wifinfo.build.board=ESP8266_ESP12
817818
wifinfo.build.spiffs_pagesize=256
818819

819820
#wifinfo.menu.ESPModule.ESP07512=ESP07 (1M/512K SPIFFS)

cores/esp8266/Esp.cpp

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,7 @@ uint32_t EspClass::getFlashChipSize(void)
177177
uint8_t * bytes = (uint8_t *) &data;
178178
// read first 4 byte (magic byte + flash config)
179179
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
180-
switch((bytes[3] & 0xf0) >> 4) {
181-
case 0x0: // 4 Mbit (512KB)
182-
return (512_kB);
183-
case 0x1: // 2 MBit (256KB)
184-
return (256_kB);
185-
case 0x2: // 8 MBit (1MB)
186-
return (1_MB);
187-
case 0x3: // 16 MBit (2MB)
188-
return (2_MB);
189-
case 0x4: // 32 MBit (4MB)
190-
return (4_MB);
191-
case 0x5: // 64 MBit (8MB)
192-
return (8_MB);
193-
case 0x6: // 128 MBit (16MB)
194-
return (16_MB);
195-
case 0x7: // 256 MBit (32MB)
196-
return (32_MB);
197-
default: // fail?
198-
return 0;
199-
}
180+
return magicFlashChipSize((bytes[3] & 0xf0) >> 4);
200181
}
201182
return 0;
202183
}
@@ -207,18 +188,7 @@ uint32_t EspClass::getFlashChipSpeed(void)
207188
uint8_t * bytes = (uint8_t *) &data;
208189
// read first 4 byte (magic byte + flash config)
209190
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
210-
switch(bytes[3] & 0x0F) {
211-
case 0x0: // 40 MHz
212-
return (40_MHz);
213-
case 0x1: // 26 MHz
214-
return (26_MHz);
215-
case 0x2: // 20 MHz
216-
return (20_MHz);
217-
case 0xf: // 80 MHz
218-
return (80_MHz);
219-
default: // fail?
220-
return 0;
221-
}
191+
return magicFlashChipSpeed(bytes[3] & 0x0F);
222192
}
223193
return 0;
224194
}
@@ -230,10 +200,53 @@ FlashMode_t EspClass::getFlashChipMode(void)
230200
uint8_t * bytes = (uint8_t *) &data;
231201
// read first 4 byte (magic byte + flash config)
232202
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
233-
mode = (FlashMode_t) bytes[2];
234-
if(mode > FM_DOUT) {
235-
mode = FM_UNKNOWN;
236-
}
203+
mode = magicFlashChipMode(bytes[2]);
204+
}
205+
return mode;
206+
}
207+
208+
uint32_t EspClass::magicFlashChipSize(uint8_t byte) {
209+
switch(byte & 0x0F) {
210+
case 0x0: // 4 Mbit (512KB)
211+
return (512_kB);
212+
case 0x1: // 2 MBit (256KB)
213+
return (256_kB);
214+
case 0x2: // 8 MBit (1MB)
215+
return (1_MB);
216+
case 0x3: // 16 MBit (2MB)
217+
return (2_MB);
218+
case 0x4: // 32 MBit (4MB)
219+
return (4_MB);
220+
case 0x5: // 64 MBit (8MB)
221+
return (8_MB);
222+
case 0x6: // 128 MBit (16MB)
223+
return (16_MB);
224+
case 0x7: // 256 MBit (32MB)
225+
return (32_MB);
226+
default: // fail?
227+
return 0;
228+
}
229+
}
230+
231+
uint32_t EspClass::magicFlashChipSpeed(uint8_t byte) {
232+
switch(byte & 0x0F) {
233+
case 0x0: // 40 MHz
234+
return (40_MHz);
235+
case 0x1: // 26 MHz
236+
return (26_MHz);
237+
case 0x2: // 20 MHz
238+
return (20_MHz);
239+
case 0xf: // 80 MHz
240+
return (80_MHz);
241+
default: // fail?
242+
return 0;
243+
}
244+
}
245+
246+
FlashMode_t EspClass::magicFlashChipMode(uint8_t byte) {
247+
FlashMode_t mode = (FlashMode_t) byte;
248+
if(mode > FM_DOUT) {
249+
mode = FM_UNKNOWN;
237250
}
238251
return mode;
239252
}
@@ -298,6 +311,24 @@ uint32_t EspClass::getFlashChipSizeByChipId(void) {
298311
}
299312
}
300313

314+
/**
315+
* check the Flash settings from IDE against the Real flash size
316+
* @param needsEquals (return only true it equals)
317+
* @return ok or not
318+
*/
319+
bool EspClass::checkFlashConfig(bool needsEquals) {
320+
if(needsEquals) {
321+
if(getFlashChipRealSize() == getFlashChipSize()) {
322+
return true;
323+
}
324+
} else {
325+
if(getFlashChipRealSize() >= getFlashChipSize()) {
326+
return true;
327+
}
328+
}
329+
return false;
330+
}
331+
301332
String EspClass::getResetInfo(void) {
302333
if(resetInfo.reason != 0) {
303334
char buff[200];

cores/esp8266/Esp.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ class EspClass {
117117
FlashMode_t getFlashChipMode();
118118
uint32_t getFlashChipSizeByChipId();
119119

120+
uint32_t magicFlashChipSize(uint8_t byte);
121+
uint32_t magicFlashChipSpeed(uint8_t byte);
122+
FlashMode_t magicFlashChipMode(uint8_t byte);
123+
124+
bool checkFlashConfig(bool needsEquals = false);
125+
120126
bool flashEraseSector(uint32_t sector);
121127
bool flashWrite(uint32_t offset, uint32_t *data, size_t size);
122128
bool flashRead(uint32_t offset, uint32_t *data, size_t size);

cores/esp8266/Updater.cpp

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ bool UpdaterClass::begin(size_t size, int command) {
5757
return false;
5858
}
5959

60+
if(!ESP.checkFlashConfig(false)) {
61+
_error = UPDATE_ERROR_FLASH_CONFIG;
62+
#ifdef DEBUG_UPDATER
63+
printError(DEBUG_UPDATER);
64+
#endif
65+
return false;
66+
}
67+
6068
_reset();
6169
_error = 0;
6270

@@ -116,9 +124,13 @@ bool UpdaterClass::begin(size_t size, int command) {
116124
return true;
117125
}
118126

119-
void UpdaterClass::setMD5(const char * expected_md5){
120-
if(strlen(expected_md5) != 32) return;
127+
bool UpdaterClass::setMD5(const char * expected_md5){
128+
if(strlen(expected_md5) != 32)
129+
{
130+
return false;
131+
}
121132
_target_md5 = expected_md5;
133+
return true;
122134
}
123135

124136
bool UpdaterClass::end(bool evenIfRemaining){
@@ -152,13 +164,22 @@ bool UpdaterClass::end(bool evenIfRemaining){
152164
#ifdef DEBUG_UPDATER
153165
DEBUG_UPDATER.printf("MD5 Failed: expected:%s, calculated:%s\n", _target_md5.c_str(), _md5.toString().c_str());
154166
#endif
167+
_reset();
155168
return false;
156169
}
157170
#ifdef DEBUG_UPDATER
158171
else DEBUG_UPDATER.printf("MD5 Success: %s\n", _target_md5.c_str());
159172
#endif
160173
}
161174

175+
if(!_verifyEnd()) {
176+
#ifdef DEBUG_UPDATER
177+
printError(DEBUG_UPDATER);
178+
#endif
179+
_reset();
180+
return false;
181+
}
182+
162183
if (_command == U_FLASH) {
163184
eboot_command ebcmd;
164185
ebcmd.action = ACTION_COPY_RAW;
@@ -233,12 +254,70 @@ size_t UpdaterClass::write(uint8_t *data, size_t len) {
233254
return len;
234255
}
235256

257+
bool UpdaterClass::_verifyHeader(uint8_t data) {
258+
if(_command == U_FLASH) {
259+
// check for valid first magic byte (is always 0xE9)
260+
if(data != 0xE9) {
261+
_error = UPDATE_ERROR_MAGIC_BYTE;
262+
_currentAddress = (_startAddress + _size);
263+
return false;
264+
}
265+
return true;
266+
} else if(_command == U_SPIFFS) {
267+
// no check of SPIFFS possible with first byte.
268+
return true;
269+
}
270+
return false;
271+
}
272+
273+
bool UpdaterClass::_verifyEnd() {
274+
if(_command == U_FLASH) {
275+
276+
uint8_t buf[4];
277+
if(!ESP.flashRead(_startAddress, (uint32_t *) &buf[0], 4)) {
278+
_error = UPDATE_ERROR_READ;
279+
_currentAddress = (_startAddress);
280+
return false;
281+
}
282+
283+
// check for valid first magic byte
284+
if(buf[0] != 0xE9) {
285+
_error = UPDATE_ERROR_MAGIC_BYTE;
286+
_currentAddress = (_startAddress);
287+
return false;
288+
}
289+
290+
uint32_t bin_flash_size = ESP.magicFlashChipSize((buf[3] & 0xf0) >> 4);
291+
292+
// check if new bin fits to SPI flash
293+
if(bin_flash_size > ESP.getFlashChipRealSize()) {
294+
_error = UPDATE_ERROR_NEW_FLASH_CONFIG;
295+
_currentAddress = (_startAddress);
296+
return false;
297+
}
298+
299+
return true;
300+
} else if(_command == U_SPIFFS) {
301+
// SPIFFS is already over written checks make no sense any more.
302+
return true;
303+
}
304+
return false;
305+
}
306+
236307
size_t UpdaterClass::writeStream(Stream &data) {
237308
size_t written = 0;
238309
size_t toRead = 0;
239310
if(hasError() || !isRunning())
240311
return 0;
241312

313+
if(!_verifyHeader(data.peek())) {
314+
#ifdef DEBUG_UPDATER
315+
printError(DEBUG_UPDATER);
316+
#endif
317+
_reset();
318+
return 0;
319+
}
320+
242321
while(remaining()) {
243322
toRead = data.readBytes(_buffer + _bufferLen, (FLASH_SECTOR_SIZE - _bufferLen));
244323
if(toRead == 0) { //Timeout
@@ -250,8 +329,9 @@ size_t UpdaterClass::writeStream(Stream &data) {
250329
#ifdef DEBUG_UPDATER
251330
printError(DEBUG_UPDATER);
252331
#endif
332+
_reset();
333+
return written;
253334
}
254-
return written;
255335
}
256336
_bufferLen += toRead;
257337
if((_bufferLen == remaining() || _bufferLen == FLASH_SECTOR_SIZE) && !_writeBuffer())
@@ -270,6 +350,8 @@ void UpdaterClass::printError(Stream &out){
270350
out.println("Flash Write Failed");
271351
} else if(_error == UPDATE_ERROR_ERASE){
272352
out.println("Flash Erase Failed");
353+
} else if(_error == UPDATE_ERROR_READ){
354+
out.println("Flash Read Failed");
273355
} else if(_error == UPDATE_ERROR_SPACE){
274356
out.println("Not Enough Space");
275357
} else if(_error == UPDATE_ERROR_SIZE){
@@ -278,6 +360,12 @@ void UpdaterClass::printError(Stream &out){
278360
out.println("Stream Read Timeout");
279361
} else if(_error == UPDATE_ERROR_MD5){
280362
out.println("MD5 Check Failed");
363+
} else if(_error == UPDATE_ERROR_FLASH_CONFIG){
364+
out.printf("Flash config wrong real: %d IDE: %d\n", ESP.getFlashChipRealSize(), ESP.getFlashChipSize());
365+
} else if(_error == UPDATE_ERROR_NEW_FLASH_CONFIG){
366+
out.printf("new Flash config wrong real: %d\n", ESP.getFlashChipRealSize());
367+
} else if(_error == UPDATE_ERROR_MAGIC_BYTE){
368+
out.println("Magic byte is wrong, not 0xE9");
281369
} else {
282370
out.println("UNKNOWN");
283371
}

cores/esp8266/Updater.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
#include "flash_utils.h"
66
#include "MD5Builder.h"
77

8-
#define UPDATE_ERROR_OK 0
9-
#define UPDATE_ERROR_WRITE 1
10-
#define UPDATE_ERROR_ERASE 2
11-
#define UPDATE_ERROR_SPACE 3
12-
#define UPDATE_ERROR_SIZE 4
13-
#define UPDATE_ERROR_STREAM 5
14-
#define UPDATE_ERROR_MD5 6
8+
#define UPDATE_ERROR_OK (0)
9+
#define UPDATE_ERROR_WRITE (1)
10+
#define UPDATE_ERROR_ERASE (2)
11+
#define UPDATE_ERROR_READ (3)
12+
#define UPDATE_ERROR_SPACE (4)
13+
#define UPDATE_ERROR_SIZE (5)
14+
#define UPDATE_ERROR_STREAM (6)
15+
#define UPDATE_ERROR_MD5 (7)
16+
#define UPDATE_ERROR_FLASH_CONFIG (8)
17+
#define UPDATE_ERROR_NEW_FLASH_CONFIG (9)
18+
#define UPDATE_ERROR_MAGIC_BYTE (10)
19+
1520

1621
#define U_FLASH 0
1722
#define U_SPIFFS 100
@@ -63,7 +68,7 @@ class UpdaterClass {
6368
/*
6469
sets the expected MD5 for the firmware (hexString)
6570
*/
66-
void setMD5(const char * expected_md5);
71+
bool setMD5(const char * expected_md5);
6772

6873
/*
6974
returns the MD5 String of the sucessfully ended firmware
@@ -131,6 +136,9 @@ class UpdaterClass {
131136
void _reset();
132137
bool _writeBuffer();
133138

139+
bool _verifyHeader(uint8_t data);
140+
bool _verifyEnd();
141+
134142
uint8_t _error;
135143
uint8_t *_buffer;
136144
size_t _bufferLen;

cores/esp8266/core_esp8266_main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C" {
3131
#include "user_interface.h"
3232
#include "cont.h"
3333
}
34-
#define LOOP_TASK_PRIORITY 0
34+
#define LOOP_TASK_PRIORITY 1
3535
#define LOOP_QUEUE_SIZE 1
3636

3737
#define OPTIMISTIC_YIELD_TIME_US 16000
@@ -73,7 +73,7 @@ extern "C" void esp_yield() {
7373
}
7474

7575
extern "C" void esp_schedule() {
76-
system_os_post(LOOP_TASK_PRIORITY, 0, 0);
76+
ets_post(LOOP_TASK_PRIORITY, 0, 0);
7777
}
7878

7979
extern "C" void __yield() {
@@ -144,7 +144,7 @@ extern "C" void user_init(void) {
144144

145145
cont_init(&g_cont);
146146

147-
system_os_task(loop_task,
147+
ets_task(loop_task,
148148
LOOP_TASK_PRIORITY, g_loop_queue,
149149
LOOP_QUEUE_SIZE);
150150

cores/esp8266/core_esp8266_phy.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ static uint8_t phy_init_data[128] =
231231
// force_freq_offset
232232
// signed, unit is 8kHz
233233
[113] = 0,
234+
235+
// rf_cal_use_flash
236+
// 0: RF init no RF CAL, using all RF CAL data in flash, it takes about 2ms for RF init
237+
// 1: RF init only do TX power control CAL, others using RF CAL data in flash , it takes about 20ms for RF init
238+
// 2: RF init no RF CAL, using all RF CAL data in flash, it takes about 2ms for RF init (same as 0?!)
239+
// 3: RF init do all RF CAL, it takes about 200ms for RF init
240+
[114] = 2
234241
};
235242

236243
extern int __real_register_chipv6_phy(uint8_t* init_data);

0 commit comments

Comments
 (0)