Skip to content

Commit b246163

Browse files
committed
backup handling of overheat mode in system task
1 parent f465ffc commit b246163

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

main/http_server/http_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
static const char * TAG = "http_server";
4040
static const char * CORS_TAG = "CORS";
4141

42-
extern bool apply_preset(DeviceModel device_model, const char* preset_name);
42+
#include "tasks/power_management_task.h"
4343

4444
static GlobalState * GLOBAL_STATE;
4545
static httpd_handle_t server = NULL;

main/lvglDisplayBAP.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern GlobalState *GLOBAL_STATE;
3737
#define SEND_DELAY_AFTER_RECEIVE_MS 1000 // 1 second delay
3838

3939

40-
extern bool apply_preset(DeviceModel device_model, const char* preset_name);
40+
#include "tasks/power_management_task.h"
4141

4242
/* sent to the display
4343
Network:

main/system.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include "lvglDisplayBAP.h"
3333
#include "lvglDisplay.h"
3434
#include "mempoolAPI.h"
35+
#include "esp_system.h"
36+
#include "tasks/power_management_task.h"
37+
#include "dataBase.h"
3538

3639

3740

@@ -45,6 +48,7 @@ esp_netif_t * netif;
4548

4649
//local function prototypes
4750
static esp_err_t ensure_overheat_mode_config();
51+
static void handle_system_overheat_recovery(GlobalState * GLOBAL_STATE);
4852

4953

5054
static void _check_for_best_diff(GlobalState * GLOBAL_STATE, double diff, uint8_t job_id);
@@ -177,6 +181,47 @@ void SYSTEM_init_peripherals(GlobalState * GLOBAL_STATE) {
177181
netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
178182
};
179183

184+
// System-level overheat recovery function - mirrors power management approach
185+
static void handle_system_overheat_recovery(GlobalState * GLOBAL_STATE) {
186+
ESP_LOGE(TAG, "SYSTEM OVERHEAT RECOVERY: Overheat flag detected in system task");
187+
188+
// Log the system recovery event
189+
char recovery_data[256];
190+
snprintf(recovery_data, sizeof(recovery_data),
191+
"{\"source\":\"system_task\",\"recoveryType\":\"automatic_30s_recovery\"}");
192+
dataBase_log_event("power", "critical", "System task overheat recovery activated", recovery_data);
193+
194+
ESP_LOGE(TAG, "Entering system overheat recovery mode. Waiting 30 seconds before automatic recovery...");
195+
196+
// Wait 30 seconds for cooling (same as power management task)
197+
vTaskDelay(30000 / portTICK_PERIOD_MS);
198+
199+
ESP_LOGI(TAG, "System overheat recovery: Applying balanced preset and restarting...");
200+
201+
// Reset overheat mode and apply balanced preset
202+
nvs_config_set_u16(NVS_CONFIG_OVERHEAT_MODE, 0);
203+
204+
// Apply balanced preset for safe recovery
205+
if (apply_preset(GLOBAL_STATE->device_model, "balanced")) {
206+
ESP_LOGI(TAG, "Successfully applied balanced preset for system recovery");
207+
} else {
208+
ESP_LOGE(TAG, "Failed to apply balanced preset, using system failsafe defaults");
209+
// Set safe failsafe values
210+
nvs_config_set_u16(NVS_CONFIG_ASIC_VOLTAGE, 1100);
211+
nvs_config_set_u16(NVS_CONFIG_ASIC_FREQ, 400);
212+
nvs_config_set_u16(NVS_CONFIG_FAN_SPEED, 75);
213+
nvs_config_set_u16(NVS_CONFIG_AUTO_FAN_SPEED, 1);
214+
nvs_config_set_string(NVS_CONFIG_AUTOTUNE_PRESET, "balanced");
215+
nvs_config_set_u16(NVS_CONFIG_AUTOTUNE_FLAG, 1);
216+
}
217+
218+
// Log system recovery completion
219+
dataBase_log_event("power", "info", "System task overheat recovery completed - restarting system", "{}");
220+
221+
// Restart the ESP32
222+
esp_restart();
223+
}
224+
180225
void SYSTEM_task(void * pvParameters)
181226
{
182227
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;
@@ -200,6 +245,9 @@ void SYSTEM_task(void * pvParameters)
200245
}
201246
int current_screen = 0;
202247
TickType_t last_update_time = xTaskGetTickCount();
248+
249+
// Variables for overheat recovery tracking - mirrors power management approach
250+
static bool system_overheat_recovery_triggered = false;
203251

204252
while (1) {
205253
// Check for overheat mode
@@ -210,6 +258,15 @@ void SYSTEM_task(void * pvParameters)
210258
#endif
211259

212260
if (module->overheat_mode == 1) {
261+
// Trigger recovery only once per overheat event (mirrors power management approach)
262+
if (!system_overheat_recovery_triggered) {
263+
system_overheat_recovery_triggered = true;
264+
ESP_LOGI(TAG, "System task detected overheat mode, triggering recovery");
265+
handle_system_overheat_recovery(GLOBAL_STATE);
266+
// This function will restart the system, so code below won't execute
267+
return;
268+
}
269+
213270
gpio_set_level(GPIO_NUM_1, 0);
214271
#if LVGL_MODE_BAP == 1
215272
lvglOverheatLoopBAP(GLOBAL_STATE);
@@ -218,6 +275,12 @@ void SYSTEM_task(void * pvParameters)
218275
#endif
219276
// vTaskDelay(5000 / portTICK_PERIOD_MS); // Update every 5 seconds
220277
continue; // Skip the normal screen cycle
278+
} else {
279+
// Reset recovery flag when not in overheat mode
280+
if (system_overheat_recovery_triggered) {
281+
ESP_LOGI(TAG, "Exited overheat mode normally, resetting recovery flag");
282+
system_overheat_recovery_triggered = false;
283+
}
221284
}
222285
// Update the RGB display
223286
#if LVGL_MODE_BAP == 1

main/tasks/power_management_task.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ static const DevicePreset DEVICE_GAMMA_PRESETS[] = {
7272
};
7373

7474
// Simple function to apply a preset by name
75-
bool apply_preset(DeviceModel device_model, const char* preset_name) {
75+
bool apply_preset(int device_model, const char* preset_name) {
7676
const DevicePreset* presets = NULL;
7777
uint8_t preset_count = 0;
7878

7979
// Get the correct preset array for the device
80-
switch (device_model) {
80+
switch ((DeviceModel)device_model) {
8181
case DEVICE_MAX:
8282
presets = DEVICE_MAX_PRESETS;
8383
preset_count = sizeof(DEVICE_MAX_PRESETS) / sizeof(DevicePreset);

main/tasks/power_management_task.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ typedef struct {
5050
void POWER_MANAGEMENT_task(void * pvParameters);
5151

5252
// Simple preset function - applies preset immediately
53-
53+
bool apply_preset(int device_model, const char* preset_name);
5454

5555
#endif

0 commit comments

Comments
 (0)