diff --git a/main/agent/agent_loop.c b/main/agent/agent_loop.c index 7ce5d36..7e5eae6 100644 --- a/main/agent/agent_loop.c +++ b/main/agent/agent_loop.c @@ -172,12 +172,13 @@ static void agent_loop_task(void *arg) { ESP_LOGI(TAG, "Agent loop started on core %d", xPortGetCoreID()); - char *system_prompt = calloc(1, MIMI_CONTEXT_BUF_SIZE); - char *history_json = calloc(1, MIMI_LLM_STREAM_BUF_SIZE); - char *tool_output = calloc(1, TOOL_OUTPUT_SIZE); + /* Allocate large buffers from PSRAM */ + char *system_prompt = heap_caps_calloc(1, MIMI_CONTEXT_BUF_SIZE, MALLOC_CAP_SPIRAM); + char *history_json = heap_caps_calloc(1, MIMI_LLM_STREAM_BUF_SIZE, MALLOC_CAP_SPIRAM); + char *tool_output = heap_caps_calloc(1, TOOL_OUTPUT_SIZE, MALLOC_CAP_SPIRAM); if (!system_prompt || !history_json || !tool_output) { - ESP_LOGE(TAG, "Failed to allocate runtime buffers"); + ESP_LOGE(TAG, "Failed to allocate PSRAM buffers"); vTaskDelete(NULL); return; } @@ -317,8 +318,8 @@ static void agent_loop_task(void *arg) free(msg.content); /* Log memory status */ - ESP_LOGI(TAG, "Free internal heap: %d bytes", - (int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); + ESP_LOGI(TAG, "Free PSRAM: %d bytes", + (int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); } } diff --git a/main/cli/serial_cli.c b/main/cli/serial_cli.c index 6758bd1..d2315c4 100644 --- a/main/cli/serial_cli.c +++ b/main/cli/serial_cli.c @@ -195,6 +195,8 @@ static int cmd_heap_info(int argc, char **argv) { printf("Internal free: %d bytes\n", (int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); + printf("PSRAM free: %d bytes\n", + (int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); printf("Total free: %d bytes\n", (int)esp_get_free_heap_size()); return 0; diff --git a/main/llm/llm_proxy.c b/main/llm/llm_proxy.c index 86febc6..dba2961 100644 --- a/main/llm/llm_proxy.c +++ b/main/llm/llm_proxy.c @@ -7,6 +7,7 @@ #include "esp_log.h" #include "esp_http_client.h" #include "esp_crt_bundle.h" +#include "esp_heap_caps.h" #include "nvs.h" #include "cJSON.h" @@ -90,7 +91,7 @@ typedef struct { static esp_err_t resp_buf_init(resp_buf_t *rb, size_t initial_cap) { - rb->data = calloc(1, initial_cap); + rb->data = heap_caps_calloc(1, initial_cap, MALLOC_CAP_SPIRAM); if (!rb->data) return ESP_ERR_NO_MEM; rb->len = 0; rb->cap = initial_cap; @@ -101,7 +102,7 @@ static esp_err_t resp_buf_append(resp_buf_t *rb, const char *data, size_t len) { while (rb->len + len >= rb->cap) { size_t new_cap = rb->cap * 2; - char *tmp = realloc(rb->data, new_cap); + char *tmp = heap_caps_realloc(rb->data, new_cap, MALLOC_CAP_SPIRAM); if (!tmp) return ESP_ERR_NO_MEM; rb->data = tmp; rb->cap = new_cap; diff --git a/main/mimi.c b/main/mimi.c index 65151b3..2eb2d48 100644 --- a/main/mimi.c +++ b/main/mimi.c @@ -99,7 +99,6 @@ void app_main(void) { /* Silence noisy components */ esp_log_level_set("esp-x509-crt-bundle", ESP_LOG_WARN); - esp_log_level_set("QRCODE", ESP_LOG_WARN); ESP_LOGI(TAG, "========================================"); ESP_LOGI(TAG, " MimiClaw - ESP32-S3 AI Agent"); @@ -108,6 +107,8 @@ void app_main(void) /* Print memory info */ ESP_LOGI(TAG, "Internal free: %d bytes", (int)heap_caps_get_free_size(MALLOC_CAP_INTERNAL)); + ESP_LOGI(TAG, "PSRAM free: %d bytes", + (int)heap_caps_get_free_size(MALLOC_CAP_SPIRAM)); /* Input */ button_Init(); diff --git a/main/tools/tool_web_search.c b/main/tools/tool_web_search.c index 4daaa34..d1b8ecf 100644 --- a/main/tools/tool_web_search.c +++ b/main/tools/tool_web_search.c @@ -7,6 +7,7 @@ #include "esp_log.h" #include "esp_http_client.h" #include "esp_crt_bundle.h" +#include "esp_heap_caps.h" #include "nvs.h" #include "cJSON.h" @@ -255,8 +256,9 @@ esp_err_t tool_web_search_execute(const char *input_json, char *output, size_t o snprintf(path, sizeof(path), "/res/v1/web/search?q=%s&count=%d", encoded_query, SEARCH_RESULT_COUNT); + /* Allocate response buffer from PSRAM */ search_buf_t sb = {0}; - sb.data = calloc(1, SEARCH_BUF_SIZE); + sb.data = heap_caps_calloc(1, SEARCH_BUF_SIZE, MALLOC_CAP_SPIRAM); if (!sb.data) { snprintf(output, output_size, "Error: Out of memory"); return ESP_ERR_NO_MEM;