Accessing esp_idf functions /

Hi folks, I’m trying to get my code to identify how much flash memory is on the (ESP32-based) ESP-WROVER-B module I have (4MB, 8MB, other) and am running into issues calling the esp-idf functions to get it. There is a function in esp-idf to do this (esp_flash_get_chip_size(), declared in esp_spi_flash.h) and somewhere a data structure called g_rom_flashchip.chip_size which may also contain that information:

size_t chip_size = esp_flash_get_chip_size();
LOG(LL_INFO, ("%s - flash: %uM", __FUNCTION__, g_rom_flashchip.chip_size / 1048576));

It seems like the way to access ESP-IDF functions is to add the esp-idf component’s folder name to the mos.yml config - I couldn’t find any docs, but there is an example with bluetooth someone’s pointed to a couple of times. That doesn’t seem to work with spi_flash, it results in a “recursive inclusion” error and stops (see log below).

Perhaps someone can help me out – how can I access ESP-IDF functions (in general) and what am I doing wrong with spi_flash? Thanks!

Here’s my modification to mos.yml – I’ve only added the “spi_flash” part. Without that, it all works.

build_vars:
MGOS_ROOT_FS_SIZE: 0x70000 # 448kB
ESP_IDF_EXTRA_COMPONENTS: ${build_vars.ESP_IDF_EXTRA_COMPONENTS} libsodium spi_flash
ESP_IDF_SDKCONFIG_OPTS: >
${build_vars.ESP_IDF_SDKCONFIG_OPTS}
CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE=y
CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y
CONFIG_LOG_COLORS=n
CONFIG_TCPIP_TASK_STACK_SIZE=6144
CONFIG_SPIRAM_SUPPORT=y
CONFIG_SPIRAM_BOOT_INIT=y
CONFIG_SPIRAM_CACHE_WORKAROUND=y
CONFIG_SPI_MASTER_IN_IRAM=y
CONFIG_WIFI_LWIP_ALLOCATION_FROM_SPIRAM_FIRST=y
CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY=y
CONFIG_SPIRAM_USE_CAPS_ALLOC=y
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=8192
CONFIG_SPIRAM_MEMTEST=y

And here is the build failure:

/opt/Espressif/esp-idf/make/project.mk:552: warning: overriding recipe for target ‘component-spi_flash-build’
/opt/Espressif/esp-idf/make/project.mk:552: warning: ignoring old recipe for target ‘component-spi_flash-build’
/opt/Espressif/esp-idf/make/project.mk:552: warning: overriding recipe for target ‘component-spi_flash-clean’
/opt/Espressif/esp-idf/make/project.mk:552: warning: ignoring old recipe for target ‘component-spi_flash-clean’
/opt/Espressif/esp-idf/make/project.mk:552: warning: overriding recipe for target ‘/c/projects/the_proj/build/objs/spi_flash’
/opt/Espressif/esp-idf/make/project.mk:552: warning: ignoring old recipe for target ‘/c/projects/the_proj/build/objs/spi_flash’
/opt/Espressif/esp-idf/make/project.mk:552: warning: overriding recipe for target ‘/c/projects/the_proj/build/objs/spi_flash/libspi_flash.a’
/opt/Espressif/esp-idf/make/project.mk:552: warning: ignoring old recipe for target ‘/c/projects/the_proj/build/objs/spi_flash/libspi_flash.a’
/opt/Espressif/esp-idf/make/project.mk:552: warning: overriding recipe for target ‘/c/projects/the_proj/build/objs/spi_flash/component_project_vars.mk’
/opt/Espressif/esp-idf/make/project.mk:552: warning: ignoring old recipe for target ‘/c/projects/the_proj/build/objs/spi_flash/component_project_vars.mk’
/c/Projects/the_proj/deps/modules/mongoose-os/platforms/esp32/Makefile.build:186: warning: overriding recipe for target ‘defconfig’
/opt/Espressif/esp-idf/make/project_config.mk:82: warning: ignoring old recipe for target ‘defconfig’
/c/Projects/the_proj/deps/modules/mongoose-os/platforms/esp32/Makefile.build:186: warning: overriding recipe for target ‘menuconfig’
/opt/Espressif/esp-idf/make/project_config.mk:69: warning: ignoring old recipe for target ‘menuconfig’
/c/Projects/the_proj/deps/modules/mongoose-os/platforms/esp32/Makefile.build:228: warning: overriding recipe for target ‘clean’
/opt/Espressif/esp-idf/components/app_update/Makefile.projbuild:48: warning: ignoring old recipe for target ‘clean’
make: Warning: File ‘/c/projects/the_proj/build/gen/vars.mk’ has modification time 24248 s in the future
GENCONFIG
/opt/Espressif/esp-idf/Kconfig:206: recursive inclusion detected. Inclusion path:
current file : ‘/opt/Espressif/esp-idf/Kconfig’
included from: ‘/opt/Espressif/esp-idf/components/tcpip_adapter/Kconfig:0’
included from: ‘/opt/Espressif/esp-idf/components/vfs/Kconfig:0’
included from: ‘/opt/Espressif/esp-idf/components/spiffs/Kconfig:0’
included from: ‘/app/components/libsodium/Kconfig:0’
included from: ‘/opt/Espressif/esp-idf/components/spi_flash/Kconfig:2’
make: *** No rule to make target ‘/c/projects/the_proj/build/objs/include/config/auto.conf’, needed by ‘/c/projects/the_proj/build/objs/bootloader/bootloader.bin’. Stop.

You don’t need to add extra components if the component you need is already included by default. Just #include the specific .h file.

In your case esp_flash_get_chip_size is a ESP-IDF 4 function. Mongoose OS uses 3.3-r4 and the function is spi_flash_get_chip_size.

Code snippet:

#include "esp_spi_flash.h"

static void timer_cb(void *arg) {
  static bool s_tick_tock = false;
  size_t flash = spi_flash_get_chip_size();
  LOG(LL_INFO,
      ("%s uptime: %.2lf, free_heap: %lu, min_free_heap: %lu, flash: %u",
       (s_tick_tock ? "Tick" : "Tock"), mgos_uptime(),
       (unsigned long) mgos_get_free_heap_size(),
       (unsigned long) mgos_get_min_free_heap_size(), flash));
  s_tick_tock = !s_tick_tock;
#ifdef LED_PIN
  mgos_gpio_toggle(LED_PIN);
#endif
  (void) arg;
}

Output from a WROVER-B with 16MB of flash:

[Mar 21 10:32:40.045] main.c:28               Tock uptime: 11.09, free_heap: 231756, min_free_heap: 227484, flash: 16777216

The default config options.

Thank you very much nliviu, that worked for me.

Odd – the first thing I tried was simply including the header, but it gave me a “file not found” during the build process. But as you say I was trying trying to call an ESP-IDF 4 function, maybe that had something to do with it.

Thanks also for those two links, they’ll really help me out going forward.