ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1
2#define LOG_TAG "SYSTEM"
3
4#include "system.h"
5#include "logger.h"
6
7#include <stdio.h>
8#include <string.h>
9
10#include "driver/temperature_sensor.h"
11#include "esp_app_desc.h"
12#include "esp_chip_info.h"
13#include "esp_heap_caps.h"
14#include "esp_mac.h"
15#include "esp_system.h"
16#include "esp_timer.h"
17
18#include "freertos/FreeRTOS.h"
19#include "freertos/task.h"
20#include "system_version.h"
21
23static temperature_sensor_handle_t temp_sensor = NULL;
25static float max_measured_temp = -100.0f;
26
27esp_err_t system_init(void) {
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);
31
32 if (err == ESP_OK) {
33 err = temperature_sensor_enable(temp_sensor);
34 if (err == ESP_OK) {
35 temperature_sensor_get_celsius(temp_sensor, &max_measured_temp);
36 LOGI("Component initialized successfully. Internal temp sensor active.");
37 } else {
38 LOGE("Failed to enable temperature sensor: %s", esp_err_to_name(err));
39 }
40 } else {
41 LOGW("Temperature sensor installation failed (maybe not supported on this "
42 "chip): %s",
43 esp_err_to_name(err));
44 // The component can still function without the temperature sensor
45 err = ESP_OK;
46 }
47 return err;
48}
49
51 if (!temp_sensor)
52 return 0.0f;
53
54 float current_temp = 0.0f;
55 if (temperature_sensor_get_celsius(temp_sensor, &current_temp) == ESP_OK) {
56 if (current_temp > max_measured_temp) {
57 max_measured_temp = current_temp;
58 }
59 return current_temp;
60 }
61 return 0.0f;
62}
63
68
69uint32_t system_get_free_heap(void) { return esp_get_free_heap_size(); }
70
72 return heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT);
73}
74
75uint8_t system_get_cpu_usage(void) {
76#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
77 static uint32_t last_idle_time = 0;
78 static uint64_t last_total_time = 0;
79
80 TaskStatus_t idle_status;
81 vTaskGetInfo(xTaskGetIdleTaskHandle(), &idle_status, pdFALSE, eRunning);
82
83 uint32_t current_idle_time = idle_status.ulRunTimeCounter;
84 uint64_t current_total_time = esp_timer_get_time();
85
86 uint32_t idle_diff = current_idle_time - last_idle_time;
87 uint64_t total_diff = current_total_time - last_total_time;
88
89 last_idle_time = current_idle_time;
90 last_total_time = current_total_time;
91
92 if (total_diff == 0)
93 return 0;
94
95 // Use 64-bit math for percentage to avoid overflow if diffs are large
96 uint8_t idle_percentage = (uint8_t)((idle_diff * 100ULL) / total_diff);
97 if (idle_percentage > 100)
98 idle_percentage = 100;
99
100 return (uint8_t)(100 - idle_percentage);
101#else
102 return 0;
103#endif
104}
105
106void system_get_tasks_list(char *buffer, size_t buffer_len) {
107#if CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
108 if (buffer && buffer_len > 0) {
109 vTaskList(buffer);
110 }
111#else
112 if (buffer && buffer_len > 0) {
113 snprintf(buffer, buffer_len, "vTaskList disabled in sdkconfig");
114 }
115#endif
116}
117
118const char *system_get_version(void) { return SYS_VERSION; }
119
120int64_t system_get_uptime_ms(void) { return esp_timer_get_time() / 1000; }
121
122int system_get_reset_reason(void) { return (int)esp_reset_reason(); }
123
125 if (!chip_info)
126 return;
127
128 esp_chip_info_t info;
129 esp_chip_info(&info);
130
131 chip_info->cores = info.cores;
132 chip_info->revision = info.revision;
133
134 switch (info.model) {
135 case CHIP_ESP32:
136 chip_info->model_name = "ESP32";
137 break;
138 case CHIP_ESP32S2:
139 chip_info->model_name = "ESP32-S2";
140 break;
141 case CHIP_ESP32S3:
142 chip_info->model_name = "ESP32-S3";
143 break;
144 case CHIP_ESP32C3:
145 chip_info->model_name = "ESP32-C3";
146 break;
147 case CHIP_ESP32C2:
148 chip_info->model_name = "ESP32-C2";
149 break;
150 case CHIP_ESP32C6:
151 chip_info->model_name = "ESP32-C6";
152 break;
153 case CHIP_ESP32H2:
154 chip_info->model_name = "ESP32-H2";
155 break;
156 default:
157 chip_info->model_name = "ESP32-Unknown";
158 break;
159 }
160}
161
163 sys_chip_info_t chip;
165 const esp_app_desc_t *app_desc = esp_app_get_description();
166
167 const char *reason_str;
168 switch ((esp_reset_reason_t)system_get_reset_reason()) {
169 case ESP_RST_POWERON:
170 reason_str = "Vat / Power-on reset";
171 break;
172 case ESP_RST_EXT:
173 reason_str = "External pin reset";
174 break;
175 case ESP_RST_SW:
176 reason_str = "Software reset via esp_restart";
177 break;
178 case ESP_RST_PANIC:
179 reason_str = "Software panic / crash reset";
180 break;
181 case ESP_RST_INT_WDT:
182 reason_str = "Interrupt watchdog reset";
183 break;
184 case ESP_RST_TASK_WDT:
185 reason_str = "Task watchdog reset";
186 break;
187 case ESP_RST_DEEPSLEEP:
188 reason_str = "Wakeup from deep sleep";
189 break;
190 case ESP_RST_BROWNOUT:
191 reason_str = "Brownout reset (voltage drop)";
192 break;
193 default:
194 reason_str = "Unknown reset reason";
195 break;
196 }
197 LOGI("==================================================");
198 LOGI("DEVICE SYSTEM INFO SNAPSHOT");
199 LOGI("==================================================");
200 LOGI("Firmware Version : %s", system_get_version());
201 LOGI("ESP-IDF Version : %s", app_desc->idf_ver);
202
203 LOGI("Hardware SoC : %s (Cores: %d, Rev: %lu)", chip.model_name,
204 chip.cores, chip.revision);
205 LOGI("Uptime : %lld ms", system_get_uptime_ms());
206 LOGI("Reset Reason : %s", reason_str);
207 LOGI("--------------------------------------------------");
208 LOGI("CPU Temperature : %.2f °C (Max: %.2f °C)", system_get_temperature(),
210#if CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
211 LOGI("CPU Usage : %d %%", system_get_cpu_usage());
212#endif
213 LOGI("Free Heap (RAM) : %lu Bytes", system_get_free_heap());
214 LOGI("Min Free Heap : %lu Bytes", system_get_min_free_heap());
215#if CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
216 char *tasks_buffer = malloc(2048);
217 if (tasks_buffer) {
218 system_get_tasks_list(tasks_buffer, 2048);
219
220 LOGI("==================================================");
221 LOGI("Task Name Status Prio Stack ID");
222 LOGI("--------------------------------------------------");
223
224 char *line = strtok(tasks_buffer, "\n");
225 while (line != NULL) {
226 if (strlen(line) > 0) {
227 LOGI("%s", line);
228 }
229 line = strtok(NULL, "\n");
230 }
231 free(tasks_buffer);
232 }
233#endif
234 LOGI("==================================================");
235}
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGW(...)
Log a message at Warning level.
Definition logger.h:40
#define LOGI(...)
Log a message at Info level.
Definition logger.h:42
#define LOGE(...)
Log a message at Error level.
Definition logger.h:38
Structure to hold static chip configuration details.
Definition system.h:10
uint8_t cores
Definition system.h:12
const char * model_name
Definition system.h:11
uint32_t revision
Definition system.h:13
int system_get_reset_reason(void)
Gets the reason for the last system reset.
Definition system.c:122
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...
Definition system.c:106
void system_get_chip_info(sys_chip_info_t *chip_info)
Retrieves static chip hardware information.
Definition system.c:124
const char * system_get_version(void)
Gets the active firmware build version. Matches the Git commit short hash (appends '-d' if local modi...
Definition system.c:118
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...
Definition system.c:71
esp_err_t system_init(void)
Initializes the system monitoring component. Sets up the internal temperature sensor and tracks initi...
Definition system.c:27
uint32_t system_get_free_heap(void)
Gets the current available free heap size (RAM).
Definition system.c:69
void system_print_info(void)
Prints a complete, formatted overview of all system statistics to the console. Internally uses ESP_LO...
Definition system.c:162
static float max_measured_temp
Highest measured temperature since boot.
Definition system.c:25
float system_get_temperature(void)
Gets the current internal CPU temperature.
Definition system.c:50
uint8_t system_get_cpu_usage(void)
Calculates the total real-time CPU utilization. Requires CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS to b...
Definition system.c:75
static temperature_sensor_handle_t temp_sensor
Handle for the internal temperature sensor.
Definition system.c:23
int64_t system_get_uptime_ms(void)
Gets the system uptime since boot.
Definition system.c:120
float system_get_max_temperature(void)
Gets the highest recorded CPU temperature since boot.
Definition system.c:64