Page 1 of 2

heap_caps_malloc() returning NULL on spiram

Posted: Mon Nov 22, 2021 5:31 pm
by Baldhead
Hi,

I am trying to allocate some memory from spiram, but heap_caps_malloc() is returning null.

What's the problem ?
In chip esp32-s3 is possible allocate dma memory from spiram !!!

chip: esp32-s3
chip revision: 0
ESP-ROM:esp32s3-20210327
idf version: v4.4-dev-3675-g35b20cadce
menuconfig: make ram allocatable using heap_caps_malloc(..., MALLOC_CAP_SPIRAM)
board: * ESP32-S3-DevKitC-1
* esp32-s3-wroom-1 D2N8R8

Code: Select all

uint16_t* p_buffer_a = (uint16_t*) heap_caps_malloc( 307200, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_SPIRAM );
uint16_t* p_buffer_b = (uint16_t*) heap_caps_malloc( 307200, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_SPIRAM );

if( p_buffer_a == NULL || p_buffer_b == NULL )
{    
    if( p_buffer_a == NULL )    ESP_LOGI("TAG", "p_buffer_a == NULL"); 
    if( p_buffer_b == NULL )    ESP_LOGI("TAG", "p_buffer_b == NULL");    
}
Thank's.

Re: heap_caps_malloc() returning NULL on spiram

Posted: Mon Nov 22, 2021 9:56 pm
by WiFive
I think dma is able to access spiram but it is not considered to be "dma ram" so try taking out MALLOC_CAP_DMA

Re: heap_caps_malloc() returning NULL on spiram

Posted: Wed Nov 24, 2021 6:04 pm
by Baldhead
Hi @WiFive,

Now "heap_caps_malloc()" dont returned null.

Code: Select all

uint16_t* p_buffer_a = (uint16_t*) heap_caps_malloc( 307200, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM );
Won't i have problems accessing the spiram with dma this way ?

Thank's.

Re: heap_caps_malloc() returning NULL on spiram

Posted: Wed Nov 24, 2021 6:41 pm
by WiFive
The EDMA hardware feature allows DMA buffers to be placed in external PSRAM, but there may be additional alignment constraints. Consult the {IDF_TARGET_NAME} Technical Reference Manual for details. To allocate a DMA-capable external memory buffer, use the ``MALLOC_CAP_SPIRAM`` capabilities flag together with :cpp:func:`heap_caps_aligned_alloc` with the necessary alignment specified.

Re: heap_caps_malloc() returning NULL on spiram

Posted: Thu Nov 25, 2021 11:45 pm
by Baldhead
Hi @WiFive,

Why p_buffer_b are returning null if the internal memory capacity are 512kB ?

What the capacity made available by esp-idf or hardware limit for heap_caps_malloc( ? , MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL) ?

Code: Select all

uint16_t* p_buffer_a = (uint16_t*) heap_caps_malloc( 76800, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
uint16_t* p_buffer_b = (uint16_t*) heap_caps_malloc( 76800, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL );
Thank's.

Re: heap_caps_malloc() returning NULL on spiram

Posted: Fri Nov 26, 2021 1:13 am
by ESP_Sprite
Possibly because your memory is fragmented: there is no continuous chunk of free memory of that size available for the allocator to return. (Also note that obviously other things will eat into the 512K of RAM available as well.)

Re: heap_caps_malloc() returning NULL on spiram

Posted: Fri Nov 26, 2021 10:56 am
by levente
Would concur, the problem was/is most likely that with the specific capabilities (especially DMA) *and* large requested size, the pool had no such allocation class available.
So with heap allocations, even thought the physical maximum size of available heap is larger than what is being requested, depending on the heap manager and the allocation classes + their availability at time of alloc, it may say it cannot allocate requested lump.
This granularity is used to manage, as much as possible, heap fragmentation - so it is never a single amorphous huge lump of memory.

Re: heap_caps_malloc() returning NULL on spiram

Posted: Fri Nov 26, 2021 4:37 pm
by Baldhead
Hi,

I would like to know how much internal memory is available for dynamic allocation provided by esp-idf in esp32-s3.
I would like to know how much internal memory is available for dma in esp32-s3 too.

I remember that in esp32 there was a limit in esp-idf (i think it was for static allocation), 192kB if i'm not mistaken.

Re: heap_caps_malloc() returning NULL on spiram

Posted: Fri Nov 26, 2021 6:09 pm
by WiFive

Re: heap_caps_malloc() returning NULL on spiram

Posted: Fri Nov 26, 2021 7:20 pm
by Baldhead
no information about dma capable memory.

"All" internal ram is dma capable ?

1kB, 2kB, 100kB..... ?