heap_caps_malloc() returning NULL on spiram

Baldhead
Posts: 465
Joined: Sun Mar 31, 2019 5:16 am

heap_caps_malloc() returning NULL on spiram

Postby Baldhead » Mon Nov 22, 2021 5:31 pm

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: heap_caps_malloc() returning NULL on spiram

Postby WiFive » Mon Nov 22, 2021 9:56 pm

I think dma is able to access spiram but it is not considered to be "dma ram" so try taking out MALLOC_CAP_DMA

Baldhead
Posts: 465
Joined: Sun Mar 31, 2019 5:16 am

Re: heap_caps_malloc() returning NULL on spiram

Postby Baldhead » Wed Nov 24, 2021 6:04 pm

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.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: heap_caps_malloc() returning NULL on spiram

Postby WiFive » Wed Nov 24, 2021 6:41 pm

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.

Baldhead
Posts: 465
Joined: Sun Mar 31, 2019 5:16 am

Re: heap_caps_malloc() returning NULL on spiram

Postby Baldhead » Thu Nov 25, 2021 11:45 pm

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.

ESP_Sprite
Posts: 9709
Joined: Thu Nov 26, 2015 4:08 am

Re: heap_caps_malloc() returning NULL on spiram

Postby ESP_Sprite » Fri Nov 26, 2021 1:13 am

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.)

levente
Posts: 12
Joined: Tue Nov 16, 2021 1:04 pm

Re: heap_caps_malloc() returning NULL on spiram

Postby levente » Fri Nov 26, 2021 10:56 am

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.

Baldhead
Posts: 465
Joined: Sun Mar 31, 2019 5:16 am

Re: heap_caps_malloc() returning NULL on spiram

Postby Baldhead » Fri Nov 26, 2021 4:37 pm

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.


Baldhead
Posts: 465
Joined: Sun Mar 31, 2019 5:16 am

Re: heap_caps_malloc() returning NULL on spiram

Postby Baldhead » Fri Nov 26, 2021 7:20 pm

no information about dma capable memory.

"All" internal ram is dma capable ?

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

Who is online

Users browsing this forum: MicroController and 224 guests