2#define LOG_TAG "SYSTEM"
10#include "driver/temperature_sensor.h"
11#include "esp_app_desc.h"
12#include "esp_chip_info.h"
13#include "esp_heap_caps.h"
15#include "esp_system.h"
18#include "freertos/FreeRTOS.h"
19#include "freertos/task.h"
20#include "system_version.h"
28 temperature_sensor_config_t temp_cfg =
29 TEMPERATURE_SENSOR_CONFIG_DEFAULT(-10, 80);
30 esp_err_t err = temperature_sensor_install(&temp_cfg, &
temp_sensor);
36 LOGI(
"Component initialized successfully. Internal temp sensor active.");
38 LOGE(
"Failed to enable temperature sensor: %s", esp_err_to_name(err));
41 LOGW(
"Temperature sensor installation failed (maybe not supported on this "
43 esp_err_to_name(err));
54 float current_temp = 0.0f;
55 if (temperature_sensor_get_celsius(
temp_sensor, ¤t_temp) == ESP_OK) {
72 return heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
76#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
77 static uint32_t last_idle_time = 0;
78 static uint64_t last_total_time = 0;
80 TaskStatus_t idle_status;
81 vTaskGetInfo(xTaskGetIdleTaskHandle(), &idle_status, pdFALSE, eRunning);
83 uint32_t current_idle_time = idle_status.ulRunTimeCounter;
84 uint64_t current_total_time = esp_timer_get_time();
86 uint32_t idle_diff = current_idle_time - last_idle_time;
87 uint64_t total_diff = current_total_time - last_total_time;
89 last_idle_time = current_idle_time;
90 last_total_time = current_total_time;
96 uint8_t idle_percentage = (uint8_t)((idle_diff * 100ULL) / total_diff);
97 if (idle_percentage > 100)
98 idle_percentage = 100;
100 return (uint8_t)(100 - idle_percentage);
107#if CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
108 if (buffer && buffer_len > 0) {
112 if (buffer && buffer_len > 0) {
113 snprintf(buffer, buffer_len,
"vTaskList disabled in sdkconfig");
128 esp_chip_info_t info;
129 esp_chip_info(&info);
131 chip_info->
cores = info.cores;
132 chip_info->
revision = info.revision;
134 switch (info.model) {
165 const esp_app_desc_t *app_desc = esp_app_get_description();
167 const char *reason_str;
169 case ESP_RST_POWERON:
170 reason_str =
"Vat / Power-on reset";
173 reason_str =
"External pin reset";
176 reason_str =
"Software reset via esp_restart";
179 reason_str =
"Software panic / crash reset";
181 case ESP_RST_INT_WDT:
182 reason_str =
"Interrupt watchdog reset";
184 case ESP_RST_TASK_WDT:
185 reason_str =
"Task watchdog reset";
187 case ESP_RST_DEEPSLEEP:
188 reason_str =
"Wakeup from deep sleep";
190 case ESP_RST_BROWNOUT:
191 reason_str =
"Brownout reset (voltage drop)";
194 reason_str =
"Unknown reset reason";
197 LOGI(
"==================================================");
198 LOGI(
"DEVICE SYSTEM INFO SNAPSHOT");
199 LOGI(
"==================================================");
201 LOGI(
"ESP-IDF Version : %s", app_desc->idf_ver);
206 LOGI(
"Reset Reason : %s", reason_str);
207 LOGI(
"--------------------------------------------------");
210#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
215#if CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
216 char *tasks_buffer = malloc(2048);
220 LOGI(
"==================================================");
221 LOGI(
"Task Name Status Prio Stack ID");
222 LOGI(
"--------------------------------------------------");
224 char *line = strtok(tasks_buffer,
"\n");
225 while (line != NULL) {
226 if (strlen(line) > 0) {
229 line = strtok(NULL,
"\n");
234 LOGI(
"==================================================");
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGW(...)
Log a message at Warning level.
#define LOGI(...)
Log a message at Info level.
#define LOGE(...)
Log a message at Error level.
Structure to hold static chip configuration details.
int system_get_reset_reason(void)
Gets the reason for the last system reset.
void system_get_tasks_list(char *buffer, size_t buffer_len)
Formats raw FreeRTOS statistics into a human-readable task list. Requires CONFIG_FREERTOS_USE_STATS_F...
void system_get_chip_info(sys_chip_info_t *chip_info)
Retrieves static chip hardware information.
const char * system_get_version(void)
Gets the active firmware build version. Matches the Git commit short hash (appends '-d' if local modi...
uint32_t system_get_min_free_heap(void)
Gets the minimum ever free heap size since boot (watermark). This is crucial for detecting close-to-O...
esp_err_t system_init(void)
Initializes the system monitoring component. Sets up the internal temperature sensor and tracks initi...
uint32_t system_get_free_heap(void)
Gets the current available free heap size (RAM).
void system_print_info(void)
Prints a complete, formatted overview of all system statistics to the console. Internally uses ESP_LO...
static float max_measured_temp
Highest measured temperature since boot.
float system_get_temperature(void)
Gets the current internal CPU temperature.
uint8_t system_get_cpu_usage(void)
Calculates the total real-time CPU utilization. Requires CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS to b...
static temperature_sensor_handle_t temp_sensor
Handle for the internal temperature sensor.
int64_t system_get_uptime_ms(void)
Gets the system uptime since boot.
float system_get_max_temperature(void)
Gets the highest recorded CPU temperature since boot.