Skip to content

Commit

Permalink
samples: boards: esp32: spiram test
Browse files Browse the repository at this point in the history
Simplifies psram test by using smh API.

Signed-off-by: Lucas Tamborrino <[email protected]>
  • Loading branch information
LucasTambor committed Aug 12, 2024
1 parent db085c1 commit 5d073d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 76 deletions.
21 changes: 9 additions & 12 deletions samples/boards/esp32/spiram_test/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
**************
Expand All @@ -21,6 +18,7 @@ The following SoCs are supported by this sample code so far:

* ESP32
* ESP32-S2
* ESP32-S3

Building and Running
********************
Expand All @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion samples/boards/esp32/spiram_test/prj.conf
Original file line number Diff line number Diff line change
@@ -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
75 changes: 12 additions & 63 deletions samples/boards/esp32/spiram_test/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,30 @@
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <soc/soc_memory_layout.h>

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 <zephyr/multi_heap/shared_multi_heap.h>

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;
}

0 comments on commit 5d073d0

Please sign in to comment.