Fix esp_timer_start error handling

A helper function added for each module so that each module
handles errors by itself.

Fixes #1092
This commit is contained in:
2025-08-27 15:56:24 +03:00
parent 374647732c
commit 71b632737e
2 changed files with 40 additions and 12 deletions

View File

@@ -263,6 +263,18 @@ void relay_chn_update_state(relay_chn_ctl_t *chn_ctl, relay_chn_state_t new_stat
}
}
static void relay_chn_execute_idle(relay_chn_ctl_t *chn_ctl);
static void relay_chn_start_timer_or_idle(relay_chn_ctl_t *chn_ctl, esp_timer_handle_t timer, uint32_t time_ms, const char* timer_name)
{
if (relay_chn_start_esp_timer_once(timer, time_ms) != ESP_OK) {
ESP_LOGE(TAG, "Failed to start %s timer for ch %d", timer_name, chn_ctl->id);
// Attempt to go to a safe state.
// relay_chn_execute_idle is safe to call, it stops timers and sets state.
relay_chn_execute_idle(chn_ctl);
}
}
/**
* @brief The command issuer function.
*
@@ -374,7 +386,7 @@ void relay_chn_issue_cmd(relay_chn_ctl_t* chn_ctl, relay_chn_cmd_t cmd)
relay_chn_state_t new_state = cmd == RELAY_CHN_CMD_FORWARD
? RELAY_CHN_STATE_FORWARD_PENDING : RELAY_CHN_STATE_REVERSE_PENDING;
relay_chn_update_state(chn_ctl, new_state);
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "inertia");
break;
#if CONFIG_RELAY_CHN_ENABLE_TILTING
@@ -456,12 +468,13 @@ static void relay_chn_execute_stop(relay_chn_ctl_t *chn_ctl)
relay_chn_run_info_set_last_run_cmd_time_ms(chn_ctl->run_info, (uint32_t)(esp_timer_get_time() / 1000));
// Schedule a free command for the channel
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "idle");
} else {
// If the channel was not running one of the run or fwd, issue a free command immediately
// relay_chn_dispatch_cmd(chn_ctl, RELAY_CHN_CMD_IDLE);
relay_chn_execute_idle(chn_ctl);
}
ESP_LOGI(TAG, "execute_stop: #%d, state: %d", chn_ctl->id, chn_ctl->state); // TODO delete
}
static void relay_chn_execute_forward(relay_chn_ctl_t *chn_ctl)
@@ -474,7 +487,7 @@ static void relay_chn_execute_forward(relay_chn_ctl_t *chn_ctl)
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_FORWARD);
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000);
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000, "run limit");
#endif
}
@@ -488,7 +501,7 @@ static void relay_chn_execute_reverse(relay_chn_ctl_t *chn_ctl)
relay_chn_update_state(chn_ctl, RELAY_CHN_STATE_REVERSE);
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
relay_chn_start_esp_timer_once(chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000);
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->run_limit_timer, chn_ctl->run_limit_sec * 1000, "run limit");
#endif
}
@@ -497,7 +510,7 @@ static void relay_chn_execute_flip(relay_chn_ctl_t *chn_ctl)
relay_chn_output_flip(chn_ctl->output);
// Set an inertia on the channel to prevent any immediate movement
chn_ctl->pending_cmd = RELAY_CHN_CMD_IDLE;
relay_chn_start_esp_timer_once(chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS);
relay_chn_start_timer_or_idle(chn_ctl, chn_ctl->inertia_timer, CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS, "flip inertia");
}
// Dispatch relay channel command