error handling and cleanup
This commit is contained in:
109
main/main.c
109
main/main.c
@ -1,3 +1,11 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @brief Main entry point for Smart Oil Heating Control System.
|
||||
*
|
||||
* This file initializes all system modules and handles initialization
|
||||
* errors by logging diagnostic information and triggering a reboot.
|
||||
*/
|
||||
|
||||
#include "safety.h"
|
||||
#include "metrics.h"
|
||||
#include "outputs.h"
|
||||
@ -9,30 +17,107 @@
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "smart-oil-heater-control-system";
|
||||
static const char *TAG = "main";
|
||||
|
||||
/** @brief Delay before reboot in milliseconds. */
|
||||
#define REBOOT_DELAY_MS 5000
|
||||
|
||||
/**
|
||||
* @brief Log error and trigger system reboot.
|
||||
* @param module Name of the module that failed.
|
||||
*/
|
||||
static void reboot_on_error(const char *module)
|
||||
{
|
||||
ESP_LOGE(TAG, "========================================");
|
||||
ESP_LOGE(TAG, "FATAL: %s initialization failed!", module);
|
||||
ESP_LOGE(TAG, "System will reboot in %d seconds...", REBOOT_DELAY_MS / 1000);
|
||||
ESP_LOGE(TAG, "========================================");
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(REBOOT_DELAY_MS));
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Application main entry point.
|
||||
*/
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "starting ...");
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
ESP_LOGI(TAG, "Smart Oil Heating Control System");
|
||||
ESP_LOGI(TAG, "Starting initialization...");
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
|
||||
// Initialize NVS
|
||||
/* Initialize NVS */
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_LOGW(TAG, "NVS partition needs erase, erasing...");
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "NVS init failed: %s", esp_err_to_name(ret));
|
||||
reboot_on_error("NVS");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] NVS initialized");
|
||||
|
||||
// TODO: Error handling!
|
||||
initOutputs();
|
||||
initInputs();
|
||||
initSafety();
|
||||
initWifi();
|
||||
initSntp();
|
||||
initControl();
|
||||
initMetrics();
|
||||
/* Initialize Outputs */
|
||||
if (initOutputs() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("Outputs");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] Outputs initialized");
|
||||
|
||||
/* Initialize Inputs */
|
||||
if (initInputs() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("Inputs");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] Inputs initialized");
|
||||
|
||||
/* Initialize Safety */
|
||||
if (initSafety() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("Safety");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] Safety initialized");
|
||||
|
||||
/* Initialize WiFi */
|
||||
if (initWifi() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("WiFi");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] WiFi initialized");
|
||||
|
||||
/* Initialize SNTP */
|
||||
if (initSntp() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("SNTP");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] SNTP initialized");
|
||||
|
||||
/* Initialize Control */
|
||||
if (initControl() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("Control");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] Control initialized");
|
||||
|
||||
/* Initialize Metrics */
|
||||
if (initMetrics() != ESP_OK)
|
||||
{
|
||||
reboot_on_error("Metrics");
|
||||
}
|
||||
ESP_LOGI(TAG, "[OK] Metrics initialized");
|
||||
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
ESP_LOGI(TAG, "All modules initialized successfully!");
|
||||
ESP_LOGI(TAG, "System is now running.");
|
||||
ESP_LOGI(TAG, "========================================");
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user