From 37a5ed39123bc0dbc6a2ff1c51bfa74d41da28de Mon Sep 17 00:00:00 2001 From: Lucas Tamborrino Date: Fri, 9 Aug 2024 09:43:33 -0300 Subject: [PATCH] samples: boards: esp32: spiram test Simplifies psram test by using smh API. Signed-off-by: Lucas Tamborrino --- samples/boards/esp32/spiram_test/README.rst | 21 +++--- samples/boards/esp32/spiram_test/prj.conf | 1 - samples/boards/esp32/spiram_test/src/main.c | 75 ++++----------------- 3 files changed, 21 insertions(+), 76 deletions(-) diff --git a/samples/boards/esp32/spiram_test/README.rst b/samples/boards/esp32/spiram_test/README.rst index b3dea1a682246f6..7250b5477903577 100644 --- a/samples/boards/esp32/spiram_test/README.rst +++ b/samples/boards/esp32/spiram_test/README.rst @@ -6,13 +6,10 @@ Espressif ESP32 SPIRAM test Overview ******** -This sample allocates memory from internal DRAM and SPIRAM by calling -:c:func:`k_malloc`, frees allocated memory by calling :c:func:`k_free` and -checks if memory can be allocated again. Capability of allocated memory is -decided by ESP_HEAP_MIN_EXTRAM_THRESHOLD. If size is less than -ESP_HEAP_MIN_EXTRAM_THRESHOLD, memory is allocated from internal DRAM. If -size is greater than ESP_HEAP_MIN_EXTRAM_THRESHOLD, memory is allocated from -SPIRAM. +This sample shows how to allocate memory from SPIRAM by using +:c:func:`shared_multi_heap_aligned_alloc` with `SMH_REG_ATTR_EXTERNAL` attribute. Checks if the +memory was correctly allocated then frees it by calling :c:func:`shared_multi_heap_free`. +It also allocates memory from internal memory and checks if the address range is correct. Supported SoCs ************** @@ -21,6 +18,7 @@ The following SoCs are supported by this sample code so far: * ESP32 * ESP32-S2 +* ESP32-S3 Building and Running ******************** @@ -29,7 +27,7 @@ Make sure you have your board connected over USB port. .. code-block:: console - west build -b esp32_devkitc_wrover samples/boards/esp32/spiram_test + west build -b esp32s3_devkitm/esp32s3/procpu samples/boards/esp32/spiram_test west flash If using another supported Espressif board, replace the argument in the above @@ -49,7 +47,6 @@ port at ``/dev/ttyUSB0``: .. code-block:: console - mem test ok! 209 - SPIRAM mem test pass - mem test ok! 194 - Internal mem test pass + *** Booting Zephyr OS build v3.7.0-446-g93c9da66944c *** + SPIRAM mem test pass + Internal mem test pass diff --git a/samples/boards/esp32/spiram_test/prj.conf b/samples/boards/esp32/spiram_test/prj.conf index 27e14aa73c161f3..c7dc9a2f6a9ea57 100644 --- a/samples/boards/esp32/spiram_test/prj.conf +++ b/samples/boards/esp32/spiram_test/prj.conf @@ -1,4 +1,3 @@ CONFIG_ESP_SPIRAM=y -CONFIG_ESP_HEAP_MIN_EXTRAM_THRESHOLD=2048 CONFIG_HEAP_MEM_POOL_SIZE=98304 CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS=n diff --git a/samples/boards/esp32/spiram_test/src/main.c b/samples/boards/esp32/spiram_test/src/main.c index b92c1ec25b7ca17..dcd3bad8983365f 100644 --- a/samples/boards/esp32/spiram_test/src/main.c +++ b/samples/boards/esp32/spiram_test/src/main.c @@ -7,81 +7,30 @@ #include #include #include - -static int check_allocated_memory(int *m1, size_t size) -{ - int ret = 0; - - if (size < CONFIG_ESP_HEAP_MIN_EXTRAM_THRESHOLD) { - if (!esp_ptr_internal(m1)) { - printk("Memory allocation is not within" - " specified bounds\n"); - ret = -1; - } - } else { - if (!esp_ptr_external_ram(m1)) { - printk("Memory allocation is not within" - " specified bounds\n"); - ret = -1; - } - } - return ret; -} - -static int test_heap_caps(size_t size) -{ - int *m1, *m2; - int cnt = 0, err = 0; - - m2 = NULL; - /* Allocated as much as we can, and create linked list */ - while ((m1 = k_malloc(size))) { - if (check_allocated_memory(m1, size) == -1) { - err = -1; - goto ret; - } - *(int **) m1 = m2; - m2 = m1; - cnt++; - } - - /* Free all allocated memory */ - while (m2) { - m1 = *(int **) m2; - k_free(m2); - m2 = m1; - } - /* Confirm that allocation can succeed now */ - m1 = k_malloc(size); - if (check_allocated_memory(m1, size) == -1) { - err = -1; - goto ret; - } - if (!m1) { - err = -1; - } else { - k_free(m1); - printk("mem test ok! %d\n", cnt); - } -ret: - return err; -} +#include int main(void) { - int err = test_heap_caps(10001); + int *m_ext, *m_int; + + m_ext = shared_multi_heap_aligned_alloc(SMH_REG_ATTR_EXTERNAL, 32, 4096); - if (err == -1) { + if (!esp_ptr_external_ram(m_ext)) { printk("SPIRAM mem test failed\n"); } else { printk("SPIRAM mem test pass\n"); } - err = test_heap_caps(1001); - if (err == -1) { + shared_multi_heap_free(m_ext); + + m_int = k_malloc(4096); + if (!esp_ptr_internal(m_int)) { printk("Internal mem test failed\n"); } else { printk("Internal mem test pass\n"); } + + k_free(m_int); + return 0; }