Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify ESP32 SPI flash driver to be compatible with stacks in PSRAM (CON-1518) #1249

Open
jonsmirl opened this issue Jan 18, 2025 · 2 comments

Comments

@jonsmirl
Copy link
Contributor

jonsmirl commented Jan 18, 2025

You can't do flash writes from tasks which have their stack in PSRAM. That's just a limitation of the ESP architecture. The work around for this is to make a worker task with a small stack in internal RAM and then queue the writes to this worker task.

The trouble with this is that now I have to hunt down every place which is doing flash writes and then change the code to queue them to the worker task. How about giving me an option in the ESP IDF SPI flash driver to do flash writes from a worker task? Then the IDF will automatically modify everything which does flash writes.

This is not as simple as it seems, for example the flash write currently bothering me is inside the SPIFFS file system code. Since I don't want to modify SPIFFS now I have to move my file system calls into a worker task.

@github-actions github-actions bot changed the title Useful option to add to ESP IDF - do flash writes from worker task. Useful option to add to ESP IDF - do flash writes from worker task. (CON-1518) Jan 18, 2025
@jonsmirl
Copy link
Contributor Author

jonsmirl commented Jan 18, 2025

Example failure when I move task stack into PSRAM. I wish there were an option to just make SPI flash use PSRAM safe without me having to search all over over people's code and needing to change things.

In this case it is my Lua interpreter and Lua needs a 50K stack. I would love to get that stack into PSRAM.

assert failed: spi_flash_disable_interrupts_caches_and_other_cpu cache_utils.c:156 (esp_task_stack_is_sane_cache_disabled())


Backtrace: 0x40375de6:0x3c2b27a0 0x40384411:0x3c2b27c0 0x403895f2:0x3c2b27e0 0x4037f5b5:0x3c2b2900 0x40380397:0x3c2b2920 0x4038039f:0x3c2b2940 0x403805d3:0x3c2b2960 0x4037fe35:0x3c2b2980 0x4216e842:0x3c2b29c0 0x420622e2:0x3c2b29f0 0x42065c92:0x3c2b2a20 0x42063d05:0x3c2b2a50 0x420627c4:0x3c2b2aa0 0x42061706:0x3c2b2ad0 0x420621a8:0x3c2b2b20 0x4202f5f5:0x3c2b2bf0 0x403847b2:0x3c2b2c40
--- Error: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
--- Waiting for the device to reconnect.
--- Error: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
--- Waiting for the device to reconnect.--- 0x40375de6: panic_abort at /home/jonsmirl/esp/esp-idf/components/esp_system/panic.c:466
0x40384411: esp_system_abort at /home/jonsmirl/esp/esp-idf/components/esp_system/port/esp_system_chip.c:93
0x403895f2: __assert_func at /home/jonsmirl/esp/esp-idf/components/newlib/assert.c:81
0x4037f5b5: spi_flash_disable_interrupts_caches_and_other_cpu at /home/jonsmirl/esp/esp-idf/components/spi_flash/cache_utils.c:164 (discriminator 1)
0x40380397: cache_disable at /home/jonsmirl/esp/esp-idf/components/spi_flash/spi_flash_os_func_app.c:70
0x4038039f: spi1_start at /home/jonsmirl/esp/esp-idf/components/spi_flash/spi_flash_os_func_app.c:122
0x403805d3: spiflash_start_default at /home/jonsmirl/esp/esp-idf/components/spi_flash/esp_flash_api.c:188
0x4037fe35: esp_flash_read at /home/jonsmirl/esp/esp-idf/components/spi_flash/esp_flash_api.c:888
0x4216e842: esp_partition_read at /home/jonsmirl/esp/esp-idf/components/esp_partition/partition_target.c:38
0x420622e2: spiffs_api_read at /home/jonsmirl/esp/esp-idf/components/spiffs/spiffs_api.c:27
0x42065c92: spiffs_phys_rd at /home/jonsmirl/esp/esp-idf/components/spiffs/spiffs/src/spiffs_cache.c:146
0x42063d05: spiffs_obj_lu_scan at /home/jonsmirl/esp/esp-idf/components/spiffs/spiffs/src/spiffs_nucleus.c:366 (discriminator 4)
0x420627c4: SPIFFS_mount at /home/jonsmirl/esp/esp-idf/components/spiffs/spiffs/src/spiffs_hydrogen.c:134
0x42061706: esp_spiffs_init at /home/jonsmirl/esp/esp-idf/components/spiffs/esp_spiffs.c:267
0x420621a8: esp_vfs_spiffs_register at /home/jonsmirl/esp/esp-idf/components/spiffs/esp_spiffs.c:406
0x4202f5f5: nodemcu_init at /home/jonsmirl/l6/components/base_nodemcu/base_nodemcu.c:119
 (inlined by) lua_task at /home/jonsmirl/l6/components/base_nodemcu/base_nodemcu.c:246
0x403847b2: vPortTaskWrapper at /home/jonsmirl/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134

@jonsmirl
Copy link
Contributor Author

So I fix the previous one, and then there's just another one... and I have no clue how many places I will need to fix this.

The best way to fix this is to modify the SPI flash driver to have a hidden internal task which switches stacks. Then I enable use of that hidden task via menuconfig.

assert failed: spi_flash_disable_interrupts_caches_and_other_cpu cache_utils.c:156 (esp_task_stack_is_sane_cache_disabled())


Backtrace: 0x40375de6:0x3c2b2920 0x40384411:0x3c2b2940 0x403895f2:0x3c2b2960 0x4037f5b5:0x3c2b2a80 0x4037f13f:0x3c2b2aa0 0x4216e29e:0x3c2b2ac0 0x4216eb22:0x3c2b2b00 0x42027b5b:0x3c2b2b30 0x420216e5:0x3c2b2b50 0x42023e16:0x3c2b2ba0 0x4201f579:0x3c2b2bd0 0x420240b0:0x3c2b2c50 0x4201e838:0x3c2b2c90 0x42025697:0x3c2b2cb0 0x4202f72a:0x3c2b2cd0 0x403847b2:0x3c2b2d10
--- 0x40375de6: panic_abort at /home/jonsmirl/esp/esp-idf/components/esp_system/panic.c:466
0x40384411: esp_system_abort at /home/jonsmirl/esp/esp-idf/components/esp_system/port/esp_system_chip.c:93
0x403895f2: __assert_func at /home/jonsmirl/esp/esp-idf/components/newlib/assert.c:81
0x4037f5b5: spi_flash_disable_interrupts_caches_and_other_cpu at /home/jonsmirl/esp/esp-idf/components/spi_flash/cache_utils.c:164 (discriminator 1)
0x4037f13f: s_vaddr_to_paddr at /home/jonsmirl/esp/esp-idf/components/esp_mm/esp_mmu_map.c:758
0x4216e29e: esp_mmu_vaddr_to_paddr at /home/jonsmirl/esp/esp-idf/components/esp_mm/esp_mmu_map.c:774
0x4216eb22: spi_flash_cache2phys at /home/jonsmirl/esp/esp-idf/components/spi_flash/flash_mmap.c:369
0x42027b5b: lfs_get_location at /home/jonsmirl/l6/components/lua/common/lfs.c:18
0x420216e5: luaN_init at /home/jonsmirl/l6/components/lua/lua-5.3/lnodemcu.c:544
0x42023e16: f_luaopen at /home/jonsmirl/l6/components/lua/lua-5.3/lstate.c:220
0x4201f579: luaD_rawrunprotected at /home/jonsmirl/l6/components/lua/lua-5.3/ldo.c:146 (discriminator 2)
0x420240b0: lua_newstate at /home/jonsmirl/l6/components/lua/lua-5.3/lstate.c:373
0x4201e838: luaL_newstate at /home/jonsmirl/l6/components/lua/lua-5.3/lauxlib.c:1119
0x42025697: lua_main at /home/jonsmirl/l6/components/lua/lua-5.3/lua.c:287
0x4202f72a: start_lua at /home/jonsmirl/l6/components/base_nodemcu/base_nodemcu.c:94
 (inlined by) lua_task at /home/jonsmirl/l6/components/base_nodemcu/base_nodemcu.c:218
0x403847b2: vPortTaskWrapper at /home/jonsmirl/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:134


ELF file SHA256: 52b853e0a6597c27

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4216f697
--- 0x4216f697: NVSHandleEntry::NVSHandleEntry(nvs::NVSHandleSimple*, char const*) at /home/jonsmirl/esp/esp-idf/components/nvs_flash/src/nvs_api.cpp:36
 (inlined by) nvs_open_from_partition at /home/jonsmirl/esp/esp-idf/components/nvs_flash/src/nvs_api.cpp:277
 (inlined by) nvs_open_from_partition at /home/jonsmirl/esp/esp-idf/components/nvs_flash/src/nvs_api.cpp:265

@jonsmirl jonsmirl changed the title Useful option to add to ESP IDF - do flash writes from worker task. (CON-1518) Modify ESP32 SPI flash driver to be compatible with stacks in PSRAM (CON-1518) Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant