From b43efe884fbaf10127cb8601fa3cfdaf3b68fb80 Mon Sep 17 00:00:00 2001 From: ismail Date: Fri, 21 Mar 2025 15:27:06 +0300 Subject: [PATCH] Fix error handling in the init function. --- .vscode/settings.json | 13 ++++++++++++- src/relay_chn.c | 31 ++++++++++++++++--------------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c2ebba1..5367a72 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,16 @@ { "files.associations": { "relay_chn.h": "c" - } + }, + "idf.customExtraVars": { + "OPENOCD_SCRIPTS": "/home/ismail/.espressif/tools/openocd-esp32/v0.12.0-esp32-20240318/openocd-esp32/share/openocd/scripts", + "ESP_ROM_ELF_DIR": "/home/ismail/.espressif/tools/esp-rom-elfs/20240305/", + "IDF_TARGET": "esp32c3" + }, + "idf.openOcdConfigs": [ + "board/esp32c3-builtin.cfg" + ], + "idf.espIdfPath": "/disk/Depo/Developer/SDK/esp-idf/v5.4/esp-idf", + "idf.toolsPath": "/home/ismail/.espressif", + "idf.pythonInstallPath": "/usr/bin/python" } \ No newline at end of file diff --git a/src/relay_chn.c b/src/relay_chn.c index 64f28ed..eb882d0 100644 --- a/src/relay_chn.c +++ b/src/relay_chn.c @@ -15,6 +15,7 @@ #include #include #include "esp_err.h" +#include "esp_check.h" #include "esp_log.h" #include "esp_task.h" #include "driver/gpio.h" @@ -320,14 +321,14 @@ esp_err_t relay_chn_create(const gpio_num_t* gpio_map, uint8_t gpio_count) // Initialize the GPIOs ret = gpio_reset_pin(forward_pin); - ret |= gpio_set_direction(forward_pin, GPIO_MODE_OUTPUT); + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to reset GPIO forward pin for channel %d", i); + ret = gpio_set_direction(forward_pin, GPIO_MODE_OUTPUT); + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set GPIO direction for forward pin for channel %d", i); - ret |= gpio_reset_pin(reverse_pin); - ret |= gpio_set_direction(reverse_pin, GPIO_MODE_OUTPUT); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialize GPIOs relay channel %d!", i); - return ret; - } + ret = gpio_reset_pin(reverse_pin); + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to reset GPIO reverse pin for channel %d", i); + ret = gpio_set_direction(reverse_pin, GPIO_MODE_OUTPUT); + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to set GPIO direction for reverse pin for channel %d", i); // Initialize the GPIOs // Initialize the relay channel @@ -339,22 +340,22 @@ esp_err_t relay_chn_create(const gpio_num_t* gpio_map, uint8_t gpio_count) relay_chn->state = RELAY_CHN_STATE_FREE; relay_chn->pending_cmd = RELAY_CHN_CMD_NONE; relay_chn->run_info.last_run_cmd = RELAY_CHN_CMD_NONE; - ret |= relay_chn_init_timer(relay_chn); // Create direction change inertia timer + ret = relay_chn_init_timer(relay_chn); // Create direction change inertia timer + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to create relay channel timer for channel %d", i); #if RELAY_CHN_ENABLE_TILTING == 1 - ret |= relay_chn_init_tilt_control(relay_chn); + ret = relay_chn_init_tilt_control(relay_chn); + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize tilt control for channel %d", i); #endif - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialize relay channel %d!", i); - return ret; - } } // Create relay channel command event loop - ret |= relay_chn_create_event_loop(); + ret = relay_chn_create_event_loop(); + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to create relay channel event loop"); #if RELAY_CHN_ENABLE_TILTING == 1 // Must call after the event loop is initialized - ret |= relay_chn_tilt_init(); // Initialize tilt feature + ret = relay_chn_tilt_init(); // Initialize tilt feature + ESP_RETURN_ON_ERROR(ret, TAG, "Failed to initialize tilt feature"); #endif // Init the state listener manager