Skip to content

Commit 8987374

Browse files
committed
ESP class - move interpretation of magic bytes in function
1 parent a07838a commit 8987374

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

cores/esp8266/Esp.cpp

Lines changed: 49 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
}

cores/esp8266/Esp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ 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+
120124
bool checkFlashConfig(bool needsEquals = false);
121125

122126
bool flashEraseSector(uint32_t sector);

0 commit comments

Comments
 (0)