fix(wifi): Add PSRAM failure fallback in WiFi Queue API's

This commit is contained in:
Nachiket Kukade 2024-08-12 14:24:52 +05:30 committed by akshat
parent da9536dbab
commit 8e06b09d31
3 changed files with 66 additions and 72 deletions

View File

@ -44,6 +44,9 @@
#include "esp_rom_sys.h" #include "esp_rom_sys.h"
#include "esp32/rom/ets_sys.h" #include "esp32/rom/ets_sys.h"
#include "private/esp_modem_wrapper.h" #include "private/esp_modem_wrapper.h"
#if __has_include("esp_psram.h")
#include "esp_psram.h"
#endif
#define TAG "esp_adapter" #define TAG "esp_adapter"
@ -255,36 +258,31 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size) static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
{ {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) StaticQueue_t *queue_buffer = heap_caps_malloc_prefer(sizeof(StaticQueue_t) + (queue_len * item_size), 2,
/* MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM,
* Since release/v5.1, FreeRTOS has been updated to always use internal memory (i.e., DRAM) MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
* for dynamic memory allocation. Calling FreeRTOS creation functions (e.g., xTaskCreate(), xQueueCreate()) if (!queue_buffer) {
* will guarantee that the memory allocated for those tasks/objects is from internal memory. return NULL;
* For more details, please refer to the Migration Guide in release/v5.1. }
*/ QueueHandle_t queue_handle = xQueueCreateStatic(queue_len, item_size, (uint8_t *)queue_buffer + sizeof(StaticQueue_t),
#if CONFIG_SPIRAM_USE_MALLOC queue_buffer);
/* Use xQueueCreateWithCaps() to allocate from SPIRAM */ if (!queue_handle) {
return (void *)xQueueCreateWithCaps(queue_len, item_size, MALLOC_CAP_SPIRAM); free(queue_buffer);
#else return NULL;
return (void *)xQueueCreate(queue_len, item_size); }
#endif
#else return (void *)queue_handle;
return (void *)xQueueCreate(queue_len, item_size);
#endif
} }
static void queue_delete_wrapper(void *queue) static void queue_delete_wrapper(void *queue)
{ {
if (queue) { if (queue) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) StaticQueue_t *queue_buffer = NULL;
#if CONFIG_SPIRAM_USE_MALLOC xQueueGetStaticBuffers(queue, NULL, &queue_buffer);
vQueueDeleteWithCaps(queue);
#else
vQueueDelete(queue); vQueueDelete(queue);
#endif if (queue_buffer) {
#else free(queue_buffer);
vQueueDelete(queue); }
#endif
} }
} }

View File

@ -45,6 +45,9 @@
#include "esp_rom_sys.h" #include "esp_rom_sys.h"
#include "esp32s2/rom/ets_sys.h" #include "esp32s2/rom/ets_sys.h"
#include "private/esp_modem_wrapper.h" #include "private/esp_modem_wrapper.h"
#if __has_include("esp_psram.h")
#include "esp_psram.h"
#endif
#define TAG "esp_adapter" #define TAG "esp_adapter"
@ -246,36 +249,31 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size) static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
{ {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) StaticQueue_t *queue_buffer = heap_caps_malloc_prefer(sizeof(StaticQueue_t) + (queue_len * item_size), 2,
/* MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM,
* Since release/v5.1, FreeRTOS has been updated to always use internal memory (i.e., DRAM) MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
* for dynamic memory allocation. Calling FreeRTOS creation functions (e.g., xTaskCreate(), xQueueCreate()) if (!queue_buffer) {
* will guarantee that the memory allocated for those tasks/objects is from internal memory. return NULL;
* For more details, please refer to the Migration Guide in release/v5.1. }
*/ QueueHandle_t queue_handle = xQueueCreateStatic(queue_len, item_size, (uint8_t *)queue_buffer + sizeof(StaticQueue_t),
#if CONFIG_SPIRAM_USE_MALLOC queue_buffer);
/* Use xQueueCreateWithCaps() to allocate from SPIRAM */ if (!queue_handle) {
return (void *)xQueueCreateWithCaps(queue_len, item_size, MALLOC_CAP_SPIRAM); free(queue_buffer);
#else return NULL;
return (void *)xQueueCreate(queue_len, item_size); }
#endif
#else return (void *)queue_handle;
return (void *)xQueueCreate(queue_len, item_size);
#endif
} }
static void queue_delete_wrapper(void *queue) static void queue_delete_wrapper(void *queue)
{ {
if (queue) { if (queue) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) StaticQueue_t *queue_buffer = NULL;
#if CONFIG_SPIRAM_USE_MALLOC xQueueGetStaticBuffers(queue, NULL, &queue_buffer);
vQueueDeleteWithCaps(queue);
#else
vQueueDelete(queue); vQueueDelete(queue);
#endif if (queue_buffer) {
#else free(queue_buffer);
vQueueDelete(queue); }
#endif
} }
} }

View File

@ -46,6 +46,9 @@
#include "esp_rom_sys.h" #include "esp_rom_sys.h"
#include "esp32s3/rom/ets_sys.h" #include "esp32s3/rom/ets_sys.h"
#include "private/esp_modem_wrapper.h" #include "private/esp_modem_wrapper.h"
#if __has_include("esp_psram.h")
#include "esp_psram.h"
#endif
#define TAG "esp_adapter" #define TAG "esp_adapter"
@ -249,36 +252,31 @@ static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size) static void * queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
{ {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) StaticQueue_t *queue_buffer = heap_caps_malloc_prefer(sizeof(StaticQueue_t) + (queue_len * item_size), 2,
/* MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM,
* Since release/v5.1, FreeRTOS has been updated to always use internal memory (i.e., DRAM) MALLOC_CAP_DEFAULT | MALLOC_CAP_INTERNAL);
* for dynamic memory allocation. Calling FreeRTOS creation functions (e.g., xTaskCreate(), xQueueCreate()) if (!queue_buffer) {
* will guarantee that the memory allocated for those tasks/objects is from internal memory. return NULL;
* For more details, please refer to the Migration Guide in release/v5.1. }
*/ QueueHandle_t queue_handle = xQueueCreateStatic(queue_len, item_size, (uint8_t *)queue_buffer + sizeof(StaticQueue_t),
#if CONFIG_SPIRAM_USE_MALLOC queue_buffer);
/* Use xQueueCreateWithCaps() to allocate from SPIRAM */ if (!queue_handle) {
return (void *)xQueueCreateWithCaps(queue_len, item_size, MALLOC_CAP_SPIRAM); free(queue_buffer);
#else return NULL;
return (void *)xQueueCreate(queue_len, item_size); }
#endif
#else return (void *)queue_handle;
return (void *)xQueueCreate(queue_len, item_size);
#endif
} }
static void queue_delete_wrapper(void *queue) static void queue_delete_wrapper(void *queue)
{ {
if (queue) { if (queue) {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0) StaticQueue_t *queue_buffer = NULL;
#if CONFIG_SPIRAM_USE_MALLOC xQueueGetStaticBuffers(queue, NULL, &queue_buffer);
vQueueDeleteWithCaps(queue);
#else
vQueueDelete(queue); vQueueDelete(queue);
#endif if (queue_buffer) {
#else free(queue_buffer);
vQueueDelete(queue); }
#endif
} }
} }