23
23
#include " esp_deep_sleep.h"
24
24
#include " esp_spi_flash.h"
25
25
#include < memory>
26
-
27
- // #define DEBUG_SERIAL Serial
28
-
26
+ #include < soc/soc.h>
27
+ #include < soc/efuse_reg.h>
28
+
29
+ /* Main header of binary image */
30
+ typedef struct {
31
+ uint8_t magic;
32
+ uint8_t segment_count;
33
+ uint8_t spi_mode; /* flash read mode (esp_image_spi_mode_t as uint8_t) */
34
+ uint8_t spi_speed: 4 ; /* flash frequency (esp_image_spi_freq_t as uint8_t) */
35
+ uint8_t spi_size: 4 ; /* flash chip size (esp_image_flash_size_t as uint8_t) */
36
+ uint32_t entry_addr;
37
+ uint8_t encrypt_flag; /* encrypt flag */
38
+ uint8_t extra_header[15 ]; /* ESP32 additional header, unused by second bootloader */
39
+ } esp_image_header_t ;
40
+
41
+ #define ESP_IMAGE_HEADER_MAGIC 0xE9
29
42
30
43
/* *
31
44
* User-defined Literals
@@ -104,64 +117,56 @@ uint32_t EspClass::getFreeHeap(void)
104
117
return esp_get_free_heap_size ();
105
118
}
106
119
120
+ uint8_t EspClass::getChipRevision (void )
121
+ {
122
+ return (REG_READ (EFUSE_BLK0_RDATA3_REG) >> EFUSE_RD_CHIP_VER_RESERVE_S) && EFUSE_RD_CHIP_VER_RESERVE_V;
123
+ }
124
+
107
125
const char * EspClass::getSdkVersion (void )
108
126
{
109
127
return esp_get_idf_version ();
110
128
}
111
129
112
130
uint32_t EspClass::getFlashChipSize (void )
113
131
{
114
- uint32_t data;
115
- uint8_t * bytes = (uint8_t *) &data;
116
- // read first 4 byte (magic byte + flash config)
117
- if (flashRead (0x0000 , &data, 4 ) == ESP_OK) {
118
- return magicFlashChipSize ((bytes[3 ] & 0xf0 ) >> 4 );
132
+ esp_image_header_t fhdr;
133
+ if (flashRead (0x1000 , (uint32_t *)&fhdr, sizeof (esp_image_header_t )) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
134
+ return 0 ;
119
135
}
120
- return 0 ;
136
+ return magicFlashChipSize (fhdr. spi_size ) ;
121
137
}
122
138
123
139
uint32_t EspClass::getFlashChipSpeed (void )
124
140
{
125
- uint32_t data;
126
- uint8_t * bytes = (uint8_t *) &data;
127
- // read first 4 byte (magic byte + flash config)
128
- if (flashRead (0x0000 , &data, 4 ) == ESP_OK) {
129
- return magicFlashChipSpeed (bytes[3 ] & 0x0F );
141
+ esp_image_header_t fhdr;
142
+ if (flashRead (0x1000 , (uint32_t *)&fhdr, sizeof (esp_image_header_t )) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
143
+ return 0 ;
130
144
}
131
- return 0 ;
145
+ return magicFlashChipSpeed (fhdr. spi_speed ) ;
132
146
}
133
147
134
148
FlashMode_t EspClass::getFlashChipMode (void )
135
149
{
136
- FlashMode_t mode = FM_UNKNOWN;
137
- uint32_t data;
138
- uint8_t * bytes = (uint8_t *) &data;
139
- // read first 4 byte (magic byte + flash config)
140
- if (flashRead (0x0000 , &data, 4 ) == ESP_OK) {
141
- mode = magicFlashChipMode (bytes[2 ]);
150
+ esp_image_header_t fhdr;
151
+ if (flashRead (0x1000 , (uint32_t *)&fhdr, sizeof (esp_image_header_t )) && fhdr.magic != ESP_IMAGE_HEADER_MAGIC) {
152
+ return FM_UNKNOWN;
142
153
}
143
- return mode ;
154
+ return magicFlashChipMode (fhdr. spi_mode ) ;
144
155
}
145
156
146
157
uint32_t EspClass::magicFlashChipSize (uint8_t byte)
147
158
{
148
159
switch (byte & 0x0F ) {
149
- case 0x0 : // 4 Mbit (512KB)
150
- return (512_kB);
151
- case 0x1 : // 2 MBit (256KB)
152
- return (256_kB);
153
- case 0x2 : // 8 MBit (1MB)
160
+ case 0x0 : // 8 MBit (1MB)
154
161
return (1_MB);
155
- case 0x3 : // 16 MBit (2MB)
162
+ case 0x1 : // 16 MBit (2MB)
156
163
return (2_MB);
157
- case 0x4 : // 32 MBit (4MB)
164
+ case 0x2 : // 32 MBit (4MB)
158
165
return (4_MB);
159
- case 0x5 : // 64 MBit (8MB)
166
+ case 0x3 : // 64 MBit (8MB)
160
167
return (8_MB);
161
- case 0x6 : // 128 MBit (16MB)
168
+ case 0x4 : // 128 MBit (16MB)
162
169
return (16_MB);
163
- case 0x7 : // 256 MBit (32MB)
164
- return (32_MB);
165
170
default : // fail?
166
171
return 0 ;
167
172
}
@@ -186,7 +191,7 @@ uint32_t EspClass::magicFlashChipSpeed(uint8_t byte)
186
191
FlashMode_t EspClass::magicFlashChipMode (uint8_t byte)
187
192
{
188
193
FlashMode_t mode = (FlashMode_t) byte;
189
- if (mode > FM_DOUT ) {
194
+ if (mode > FM_SLOW_READ ) {
190
195
mode = FM_UNKNOWN;
191
196
}
192
197
return mode;
@@ -197,6 +202,7 @@ bool EspClass::flashEraseSector(uint32_t sector)
197
202
return spi_flash_erase_sector (sector) == ESP_OK;
198
203
}
199
204
205
+ // Warning: These functions do not work with encrypted flash
200
206
bool EspClass::flashWrite (uint32_t offset, uint32_t *data, size_t size)
201
207
{
202
208
return spi_flash_write (offset, (uint32_t *) data, size) == ESP_OK;
0 commit comments