Commit adbf2192 authored by Lucas Tamborrino's avatar Lucas Tamborrino Committed by Anas Nashif
Browse files

samples: boards: esp32: spiram test



Simplifies psram test by using smh API.

Signed-off-by: default avatarLucas Tamborrino <lucas.tamborrino@espressif.com>
parent 5dc54529
Loading
Loading
Loading
Loading
+9 −12
Original line number Diff line number Diff line
@@ -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
   *** Booting Zephyr OS build v3.7.0-446-g93c9da66944c ***
   SPIRAM mem test pass
    mem test ok! 194
   Internal mem test pass
+0 −1
Original line number Diff line number Diff line
CONFIG_ESP_SPIRAM=y
CONFIG_ESP_HEAP_MIN_EXTRAM_THRESHOLD=2048
CONFIG_HEAP_MEM_POOL_SIZE=98304
CONFIG_ESP_HEAP_SEARCH_ALL_REGIONS=n
+12 −63
Original line number Diff line number Diff line
@@ -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;
}