mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-10 04:43:33 +00:00
Add option to allocate external RAM using heap_alloc_caps
This commit is contained in:
@@ -57,8 +57,7 @@ choice SPIRAM_USE
|
||||
config SPIRAM_USE_MEMMAP
|
||||
bool "Integrate RAM into ESP32 memory map"
|
||||
config SPIRAM_USE_CAPS_ALLOC
|
||||
bool "Make RAM allocatable using heap_caps_malloc(..., MALLOC_CAP_SPISRAM)"
|
||||
depends on TO_BE_DONE
|
||||
bool "Make RAM allocatable using heap_caps_malloc(..., MALLOC_CAP_SPIRAM)"
|
||||
config SPIRAM_USE_MALLOC
|
||||
bool "Make RAM allocatable using malloc as well"
|
||||
depends on TO_BE_DONE
|
||||
|
@@ -255,6 +255,15 @@ void start_cpu0_default(void)
|
||||
{
|
||||
esp_err_t err;
|
||||
esp_setup_syscall_table();
|
||||
|
||||
#if CONFIG_SPIRAM_BOOT_INIT && (CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MALLOC)
|
||||
esp_err_t r=esp_spiram_add_to_heapalloc();
|
||||
if (r != ESP_OK) {
|
||||
ESP_EARLY_LOGE(TAG, "External RAM could not be added to heap!");
|
||||
abort();
|
||||
}
|
||||
#endif
|
||||
|
||||
//Enable trace memory and immediately start trace.
|
||||
#if CONFIG_ESP32_TRAX
|
||||
#if CONFIG_ESP32_TRAX_TWOBANKS
|
||||
|
@@ -39,6 +39,12 @@ esp_err_t esp_spiram_init();
|
||||
bool esp_spiram_test();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Add the initialized SPI RAM to the heap allocator.
|
||||
*/
|
||||
esp_err_t esp_spiram_add_to_heapalloc();
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the size of the attached SPI RAM chip selected in menuconfig
|
||||
*
|
||||
|
@@ -28,6 +28,8 @@ we add more types of external RAM memory, this can be made into a more intellige
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/xtensa_api.h"
|
||||
#include "soc/soc.h"
|
||||
#include "esp_heap_caps_init.h"
|
||||
#include "soc/soc_memory_layout.h"
|
||||
#include "soc/dport_reg.h"
|
||||
#include "rom/cache.h"
|
||||
|
||||
@@ -120,6 +122,12 @@ esp_err_t esp_spiram_init()
|
||||
}
|
||||
|
||||
|
||||
esp_err_t esp_spiram_add_to_heapalloc()
|
||||
{
|
||||
//Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's
|
||||
//no need to explicitly specify them.
|
||||
return heap_caps_add_region((intptr_t)SOC_EXTRAM_DATA_LOW, (intptr_t)SOC_EXTRAM_DATA_LOW + CONFIG_SPIRAM_SIZE-1);
|
||||
}
|
||||
|
||||
size_t esp_spiram_get_size()
|
||||
{
|
||||
|
@@ -56,12 +56,14 @@ TEST_CASE("Spiram cache flush on mmap", "[spiram][ignore]")
|
||||
void *mem[2];
|
||||
res[0]=0; res[1]=0;
|
||||
#if CONFIG_SPIRAM_USE_CAPS_ALLOC
|
||||
mem[0]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
mem[1]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
mem[0]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
mem[1]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
#else
|
||||
mem[0]=(void*)0x3f800000;
|
||||
mem[1]=(void*)0x3f800000+TSTSZ;
|
||||
#endif
|
||||
assert(mem[0]);
|
||||
assert(mem[1]);
|
||||
TaskHandle_t th[2];
|
||||
err[0]=0; err[1]=0;
|
||||
printf("Creating tasks\n");
|
||||
@@ -99,12 +101,14 @@ TEST_CASE("Spiram cache flush on write/read", "[spiram][ignore]")
|
||||
void *mem[2];
|
||||
res[0]=0; res[1]=0;
|
||||
#if CONFIG_SPIRAM_USE_CAPS_ALLOC
|
||||
mem[0]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
mem[1]=pvPortMallocCaps(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
mem[0]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
mem[1]=heap_caps_malloc(TSTSZ, MALLOC_CAP_SPIRAM);
|
||||
#else
|
||||
mem[0]=(void*)0x3f800000;
|
||||
mem[1]=(void*)0x3f800000+TSTSZ;
|
||||
#endif
|
||||
assert(mem[0]);
|
||||
assert(mem[1]);
|
||||
TaskHandle_t th[2];
|
||||
const esp_partition_t* part = get_test_data_partition();
|
||||
assert(part!=NULL);
|
||||
@@ -138,4 +142,36 @@ TEST_CASE("Spiram cache flush on write/read", "[spiram][ignore]")
|
||||
#endif
|
||||
}
|
||||
|
||||
IRAM_ATTR TEST_CASE("Spiram memcmp weirdness at 80MHz", "[spiram][ignore]") {
|
||||
char *mem1=malloc(0x10000);
|
||||
#if CONFIG_SPIRAM_USE_CAPS_ALLOC
|
||||
char *mem2=heap_caps_malloc(0x10000, MALLOC_CAP_SPIRAM);
|
||||
#else
|
||||
char *mem2=(void*)0x3f800000;
|
||||
#endif
|
||||
|
||||
#if !CONFIG_SPIRAM_SPEED_80M
|
||||
printf("**** WARNING **** Spi memory isn't running at 80MHz, so this test is somewhat meaningless.\n");
|
||||
#endif
|
||||
|
||||
printf("RAM: Got %p and %p\n", mem1, mem2);
|
||||
assert(mem1);
|
||||
assert(mem2);
|
||||
for (int i=0; i<0x10000; i++) mem1[i]=i^0xAAAAAAAA;
|
||||
|
||||
for (int cycle=1; cycle<100; cycle++) {
|
||||
memcpy(mem2, mem1, 0x10000);
|
||||
if (memcmp(mem1, mem2, 0x10000)!=0) {
|
||||
printf("Memcmp failed! Cycle %d\n", cycle);
|
||||
for (int i=0; i<0x10000; i++) {
|
||||
if (mem1[i]!=mem2[i]) {
|
||||
printf("Found real difference at index %d: 0x%x vs 0x%x\n", i, mem1[i], mem2[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif //CONFIG_SPIRAM_USE_CAPS_ALLOC || CONFIG_SPIRAM_USE_MEMMAP
|
||||
|
Reference in New Issue
Block a user