Skip to content

Commit d2ca7d0

Browse files
committed
add functions to read out the flash config
Serial.print(F("Flash Chip Size: ")); Serial.println(ESP.getFlashChipSize()); Serial.print(F("Flash Chip Speed: ")); Serial.println(ESP.getFlashChipSpeed()); Serial.print(F("Flash Chip Mode: 0x")); Serial.println(ESP.getFlashChipMode(), HEX);
1 parent 8cc844f commit d2ca7d0

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

hardware/esp8266com/esp8266/cores/esp8266/Arduino.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ void yield(void);
8282
#define degrees(rad) ((rad)*RAD_TO_DEG)
8383
#define sq(x) ((x)*(x))
8484

85+
#define kHz (1000L)
86+
#define MHz (1000L*kHz)
87+
#define GHz (1000L*MHz)
88+
89+
#define kBit (1024L)
90+
#define MBit (1024L*kBit)
91+
#define GBit (1024L*kBit)
92+
93+
#define kB (1024L)
94+
#define MB (1024L*kB)
95+
#define GB (1024L*MB)
96+
8597
void ets_intr_lock();
8698
void ets_intr_unlock();
8799

hardware/esp8266com/esp8266/cores/esp8266/Esp.cpp

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ uint32_t EspClass::getChipId(void)
8989
return system_get_chip_id();
9090
}
9191

92-
uint32_t EspClass::getFlashChipId(void)
93-
{
94-
return spi_flash_get_id();
95-
}
96-
9792
const char * EspClass::getSDKversion(void)
9893
{
9994
return system_get_sdk_version();
@@ -113,3 +108,70 @@ uint8_t EspClass::getCPUfreqMHz(void)
113108
{
114109
return system_get_cpu_freq();
115110
}
111+
112+
113+
uint32_t EspClass::getFlashChipId(void)
114+
{
115+
return spi_flash_get_id();
116+
}
117+
118+
uint32_t EspClass::getFlashChipSize(void)
119+
{
120+
uint32_t data;
121+
uint8_t * bytes = (uint8_t *) &data;
122+
// read first 4 byte (magic byte + flash config)
123+
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
124+
switch((bytes[3] & 0xF0) >> 4) {
125+
case 0x0: // 4 Mbit (512KB)
126+
return (512 * kB);
127+
case 0x1: // 2 MBit (256KB)
128+
return (256 * kB);
129+
case 0x2: // 8 MBit (1MB)
130+
return (1 * MB);
131+
case 0x3: // 16 MBit (2MB)
132+
return (2 * MB);
133+
case 0x4: // 32 MBit (4MB)
134+
return (4 * MB);
135+
default: // fail?
136+
return 0;
137+
}
138+
}
139+
return 0;
140+
}
141+
142+
uint32_t EspClass::getFlashChipSpeed(void)
143+
{
144+
uint32_t data;
145+
uint8_t * bytes = (uint8_t *) &data;
146+
// read first 4 byte (magic byte + flash config)
147+
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
148+
switch(bytes[3] & 0x0F) {
149+
case 0x0: // 40 MHz
150+
return (40 * MHz);
151+
case 0x1: // 26 MHz
152+
return (26 * MHz);
153+
case 0x2: // 20 MHz
154+
return (20 * MHz);
155+
case 0xF: // 80 MHz
156+
return (80 * MHz);
157+
default: // fail?
158+
return 0;
159+
}
160+
}
161+
return 0;
162+
}
163+
164+
FlashMode_t EspClass::getFlashChipMode(void)
165+
{
166+
FlashMode_t mode = FM_FAILD;
167+
uint32_t data;
168+
uint8_t * bytes = (uint8_t *) &data;
169+
// read first 4 byte (magic byte + flash config)
170+
if(spi_flash_read(0x0000, &data, 4) == SPI_FLASH_RESULT_OK) {
171+
mode = (FlashMode_t) bytes[2];
172+
if(mode > FM_DOUT) {
173+
mode = FM_FAILD;
174+
}
175+
}
176+
return mode;
177+
}

hardware/esp8266com/esp8266/cores/esp8266/Esp.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ enum WakeMode {
4949
WAKE_RF_DISABLED = 4 // disable RF after deep-sleep wake up, just like modem sleep, there will be the smallest current.
5050
};
5151

52+
typedef enum {
53+
FM_QIO = 0x00,
54+
FM_QOUT = 0x01,
55+
FM_DIO = 0x02,
56+
FM_DOUT = 0x03,
57+
FM_FAILD = 0xFF
58+
} FlashMode_t;
59+
5260
class EspClass {
5361
public:
5462
EspClass();
@@ -69,7 +77,7 @@ class EspClass {
6977
uint32_t getFreeHeap(void);
7078

7179
uint32_t getChipId(void);
72-
uint32_t getFlashChipId(void);
80+
7381

7482
const char * getSDKversion(void);
7583

@@ -78,6 +86,11 @@ class EspClass {
7886

7987
uint8_t getCPUfreqMHz(void);
8088

89+
uint32_t getFlashChipId(void);
90+
uint32_t getFlashChipSize(void);
91+
uint32_t getFlashChipSpeed(void);
92+
FlashMode_t getFlashChipMode(void);
93+
8194
};
8295

8396
extern EspClass ESP;

0 commit comments

Comments
 (0)