Add memory diagnostics and optimize PSRAM usage

This commit is contained in:
2026-08-25 11:08:02 +02:00
parent f6a275c543
commit d779fb046e
7 changed files with 106 additions and 14 deletions
+43 -2
View File
@@ -3,13 +3,42 @@
#include "system_console.h"
#include <stdint.h>
#include <stdio.h>
#include "esp_console.h"
#include "esp_heap_caps.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
static void print_heap_region(const char *name, uint32_t capabilities)
{
printf("%s: free=%u minimum-free=%u largest-block=%u bytes\n",
name,
(unsigned int)heap_caps_get_free_size(capabilities),
(unsigned int)heap_caps_get_minimum_free_size(capabilities),
(unsigned int)heap_caps_get_largest_free_block(capabilities));
}
static int command_memory(int argc, char **argv)
{
(void)argv;
if (argc != 1) {
printf("Usage: memory\n");
return 1;
}
print_heap_region("Internal 8-bit heap",
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
print_heap_region("Internal DMA heap",
MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA | MALLOC_CAP_8BIT);
print_heap_region("External PSRAM",
MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
printf("Minimum-free is a conservative sum of each matching heap region's lifetime minimum.\n");
return 0;
}
static int command_reboot(int argc, char **argv)
{
(void)argv;
@@ -28,12 +57,24 @@ static int command_reboot(int argc, char **argv)
esp_err_t system_console_register_commands(void)
{
const esp_console_cmd_t command = {
const esp_console_cmd_t reboot_command = {
.command = "reboot",
.help = "Restart the ESP32; unsaved RAM-only configuration is lost",
.hint = NULL,
.func = &command_reboot,
.argtable = NULL,
};
return esp_console_cmd_register(&command);
esp_err_t error = esp_console_cmd_register(&reboot_command);
if (error != ESP_OK) {
return error;
}
const esp_console_cmd_t memory_command = {
.command = "memory",
.help = "Show internal and PSRAM heap availability/low-water marks",
.hint = NULL,
.func = &command_memory,
.argtable = NULL,
};
return esp_console_cmd_register(&memory_command);
}