|
38 | 38 | #include "nvs_flash.h"
|
39 | 39 | #include "esp_task.h"
|
40 | 40 | #include "esp_event.h"
|
| 41 | +#include "esp_flash.h" |
41 | 42 | #include "esp_log.h"
|
42 | 43 | #include "esp_memory_utils.h"
|
43 | 44 | #include "esp_psram.h"
|
@@ -214,11 +215,38 @@ void boardctrl_startup(void) {
|
214 | 215 | nvs_flash_erase();
|
215 | 216 | nvs_flash_init();
|
216 | 217 | }
|
| 218 | + |
| 219 | + // Query the physical size of the SPI flash and store it in the size |
| 220 | + // variable of the global, default SPI flash handle. |
| 221 | + esp_flash_get_physical_size(NULL, &esp_flash_default_chip->size); |
| 222 | + |
| 223 | + // If there is no filesystem partition (no "vfs" or "ffat"), add a "vfs" partition |
| 224 | + // that extends from the end of the application partition up to the end of flash. |
| 225 | + if (esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "vfs") == NULL |
| 226 | + && esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "ffat") == NULL) { |
| 227 | + // No "vfs" or "ffat" partition, so try to create one. |
| 228 | + |
| 229 | + // Find the end of the last partition that exists in the partition table. |
| 230 | + size_t offset = 0; |
| 231 | + esp_partition_iterator_t iter = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL); |
| 232 | + while (iter != NULL) { |
| 233 | + const esp_partition_t *part = esp_partition_get(iter); |
| 234 | + offset = MAX(offset, part->address + part->size); |
| 235 | + iter = esp_partition_next(iter); |
| 236 | + } |
| 237 | + |
| 238 | + // If we found the application partition and there is some space between the end of |
| 239 | + // that and the end of flash, create a "vfs" partition taking up all of that space. |
| 240 | + if (offset > 0 && esp_flash_default_chip->size > offset) { |
| 241 | + size_t size = esp_flash_default_chip->size - offset; |
| 242 | + esp_partition_register_external(esp_flash_default_chip, offset, size, "vfs", ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL); |
| 243 | + } |
| 244 | + } |
217 | 245 | }
|
218 | 246 |
|
219 | 247 | void MICROPY_ESP_IDF_ENTRY(void) {
|
220 | 248 | // Hook for a board to run code at start up.
|
221 |
| - // This defaults to initialising NVS. |
| 249 | + // This defaults to initialising NVS and detecting the flash size. |
222 | 250 | MICROPY_BOARD_STARTUP();
|
223 | 251 |
|
224 | 252 | // Create and transfer control to the MicroPython task.
|
|
0 commit comments