Skip to content
10 changes: 6 additions & 4 deletions cores/esp8266/core_esp8266_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ extern "C" void __disableWiFiAtBootTime (void)

#if FLASH_MAP_SUPPORT
#include "flash_hal.h"
extern "C" void flashinit (void);
extern "C" const char *flashinit (void);
uint32_t __flashindex;
#endif

Expand Down Expand Up @@ -480,6 +480,11 @@ extern "C" void user_init(void) {

uart_div_modify(0, UART_CLK_FREQ / (115200));

#if FLASH_MAP_SUPPORT
const char *err_msg = flashinit();
if (err_msg) __unhandled_exception(err_msg);
#endif

init(); // in core_esp8266_wiring.c, inits hw regs and sdk timer

initVariant();
Expand All @@ -502,9 +507,6 @@ extern "C" void user_init(void) {

#if defined(MMU_IRAM_HEAP)
umm_init_iram();
#endif
#if FLASH_MAP_SUPPORT
flashinit();
#endif
preinit(); // Prior to C++ Dynamic Init (not related to above init() ). Meant to be user redefinable.
__disableWiFiAtBootTime(); // default weak function disables WiFi
Expand Down
21 changes: 16 additions & 5 deletions cores/esp8266/flash_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,32 @@ extern "C" {
#include <FlashMap.h>

extern uint32_t spi_flash_get_id (void); // <user_interface.h>
extern void flashinit(void);
extern const char *flashinit(void);
extern uint32_t __flashindex;
extern const flash_map_s __flashdesc[];

#ifndef QUOTE
#define QUOTE(a) __STRINGIFY(a)
#endif

#ifdef DEBUG_ESP_CORE
#define DEBUG_FLASH_MAP_NOT_FOUND " flashinit: configuration not found"
#else
#define DEBUG_FLASH_MAP_NOT_FOUND
#endif

#define FLASH_MAP_SETUP_CONFIG(conf) FLASH_MAP_SETUP_CONFIG_ATTR(,conf)
#define FLASH_MAP_SETUP_CONFIG_ATTR(attr, conf...) \
const flash_map_s __flashdesc[] PROGMEM = conf; \
void flashinit (void) attr; \
void flashinit (void) \
const char *flashinit (void) attr; \
const char *flashinit (void) \
{ \
uint32_t flash_chip_size_kb = 1 << (((spi_flash_get_id() >> 16) & 0xff) - 10); \
for (__flashindex = 0; __flashindex < sizeof(__flashdesc) / sizeof(__flashdesc[0]); __flashindex++) \
if (__flashdesc[__flashindex].flash_size_kb == flash_chip_size_kb) \
return; \
panic(); /* configuration not found */ \
return NULL; \
static const char fail_msg[] PROGMEM = __FILE__ ":" QUOTE(__LINE__) DEBUG_FLASH_MAP_NOT_FOUND ; \
return fail_msg; /* configuration not found */ \
}

#define EEPROM_start (__flashdesc[__flashindex].eeprom_start)
Expand Down