From 506bff240b91486d162bb267e4c962b7163a7b0f Mon Sep 17 00:00:00 2001 From: xuxiao Date: Fri, 23 Aug 2024 10:51:44 +0800 Subject: [PATCH] feat(wifi): add SPIRAM wifi support for esp32c5 and esp32c61 --- components/esp_wifi/esp32c5/esp_adapter.c | 63 +++++++++++++++++++++- components/esp_wifi/esp32c61/esp_adapter.c | 63 +++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/components/esp_wifi/esp32c5/esp_adapter.c b/components/esp_wifi/esp32c5/esp_adapter.c index c0c735e01a..0bc270b856 100644 --- a/components/esp_wifi/esp32c5/esp_adapter.c +++ b/components/esp_wifi/esp32c5/esp_adapter.c @@ -63,17 +63,29 @@ extern void wifi_apb80m_release(void); IRAM_ATTR void *wifi_malloc(size_t size) { +#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); +#else return malloc(size); +#endif } IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { +#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); +#else return realloc(ptr, size); +#endif } IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { +#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); +#else return calloc(n, size); +#endif } static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size) @@ -91,14 +103,47 @@ wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size) return NULL; } +#if CONFIG_SPIRAM_USE_MALLOC + /* Wi-Fi still use internal RAM */ + + queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len * item_size), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + if (!queue->storage) { + goto _error; + } + + queue->handle = xQueueCreateStatic(queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); + + if (!queue->handle) { + goto _error; + } + + return queue; + +_error: + if (queue) { + if (queue->storage) { + free(queue->storage); + } + + free(queue); + } + + return NULL; +#else queue->handle = xQueueCreate(queue_len, item_size); return queue; +#endif } void wifi_delete_queue(wifi_static_queue_t *queue) { if (queue) { vQueueDelete(queue->handle); +#if CONFIG_SPIRAM_USE_MALLOC + if (queue->storage) { + free(queue->storage); + } +#endif free(queue); } } @@ -207,7 +252,23 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex) static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size) { +#if CONFIG_SPIRAM_USE_MALLOC + /* Use xQueueCreateWithCaps() to allocate from SPIRAM */ + return (void *)xQueueCreateWithCaps(queue_len, item_size, MALLOC_CAP_SPIRAM); +#else return (void *)xQueueCreate(queue_len, item_size); +#endif +} + +static void queue_delete_wrapper(void *queue) +{ + if (queue) { +#if CONFIG_SPIRAM_USE_MALLOC + vQueueDeleteWithCaps(queue); +#else + vQueueDelete(queue); +#endif + } } static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_tick) @@ -587,7 +648,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._mutex_lock = mutex_lock_wrapper, ._mutex_unlock = mutex_unlock_wrapper, ._queue_create = queue_create_wrapper, - ._queue_delete = (void(*)(void *))vQueueDelete, + ._queue_delete = queue_delete_wrapper, ._queue_send = queue_send_wrapper, ._queue_send_from_isr = queue_send_from_isr_wrapper, ._queue_send_to_back = queue_send_to_back_wrapper, diff --git a/components/esp_wifi/esp32c61/esp_adapter.c b/components/esp_wifi/esp32c61/esp_adapter.c index 360d7ae6f7..8f6aac9216 100644 --- a/components/esp_wifi/esp32c61/esp_adapter.c +++ b/components/esp_wifi/esp32c61/esp_adapter.c @@ -63,17 +63,29 @@ extern void wifi_apb80m_release(void); IRAM_ATTR void *wifi_malloc(size_t size) { +#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); +#else return malloc(size); +#endif } IRAM_ATTR void *wifi_realloc(void *ptr, size_t size) { +#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + return heap_caps_realloc_prefer(ptr, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); +#else return realloc(ptr, size); +#endif } IRAM_ATTR void *wifi_calloc(size_t n, size_t size) { +#if CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP + return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL); +#else return calloc(n, size); +#endif } static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size) @@ -91,14 +103,47 @@ wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size) return NULL; } +#if CONFIG_SPIRAM_USE_MALLOC + /* Wi-Fi still use internal RAM */ + + queue->storage = heap_caps_calloc(1, sizeof(StaticQueue_t) + (queue_len * item_size), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + if (!queue->storage) { + goto _error; + } + + queue->handle = xQueueCreateStatic(queue_len, item_size, ((uint8_t*)(queue->storage)) + sizeof(StaticQueue_t), (StaticQueue_t*)(queue->storage)); + + if (!queue->handle) { + goto _error; + } + + return queue; + +_error: + if (queue) { + if (queue->storage) { + free(queue->storage); + } + + free(queue); + } + + return NULL; +#else queue->handle = xQueueCreate(queue_len, item_size); return queue; +#endif } void wifi_delete_queue(wifi_static_queue_t *queue) { if (queue) { vQueueDelete(queue->handle); +#if CONFIG_SPIRAM_USE_MALLOC + if (queue->storage) { + free(queue->storage); + } +#endif free(queue); } } @@ -207,7 +252,23 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex) static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size) { +#if CONFIG_SPIRAM_USE_MALLOC + /* Use xQueueCreateWithCaps() to allocate from SPIRAM */ + return (void *)xQueueCreateWithCaps(queue_len, item_size, MALLOC_CAP_SPIRAM); +#else return (void *)xQueueCreate(queue_len, item_size); +#endif +} + +static void queue_delete_wrapper(void *queue) +{ + if (queue) { +#if CONFIG_SPIRAM_USE_MALLOC + vQueueDeleteWithCaps(queue); +#else + vQueueDelete(queue); +#endif + } } static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_tick) @@ -587,7 +648,7 @@ wifi_osi_funcs_t g_wifi_osi_funcs = { ._mutex_lock = mutex_lock_wrapper, ._mutex_unlock = mutex_unlock_wrapper, ._queue_create = queue_create_wrapper, - ._queue_delete = (void(*)(void *))vQueueDelete, + ._queue_delete = queue_delete_wrapper, ._queue_send = queue_send_wrapper, ._queue_send_from_isr = queue_send_from_isr_wrapper, ._queue_send_to_back = queue_send_to_back_wrapper,