mirror of
https://github.com/espressif/esp-idf.git
synced 2026-05-04 20:05:25 +02:00
Merge branch 'feature/proper_way_register_testapp_component' into 'master'
Several driver improvements according to coverify scan report See merge request espressif/esp-idf!17708
This commit is contained in:
+67
-66
@@ -100,27 +100,17 @@ static void gptimer_release_group_handle(gptimer_group_t *group);
|
||||
static esp_err_t gptimer_select_periph_clock(gptimer_t *timer, gptimer_clock_source_t src_clk, uint32_t resolution_hz);
|
||||
static void gptimer_default_isr(void *args);
|
||||
|
||||
esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *ret_timer)
|
||||
static esp_err_t gptimer_register_to_group(gptimer_t *timer)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
gptimer_group_t *group = NULL;
|
||||
gptimer_t *timer = NULL;
|
||||
int group_id = -1;
|
||||
int timer_id = -1;
|
||||
ESP_GOTO_ON_FALSE(config && ret_timer, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
|
||||
ESP_GOTO_ON_FALSE(config->resolution_hz, ESP_ERR_INVALID_ARG, err, TAG, "invalid timer resolution:%d", config->resolution_hz);
|
||||
|
||||
timer = heap_caps_calloc(1, sizeof(gptimer_t), GPTIMER_MEM_ALLOC_CAPS);
|
||||
ESP_GOTO_ON_FALSE(timer, ESP_ERR_NO_MEM, err, TAG, "no mem for gptimer");
|
||||
|
||||
for (int i = 0; (i < SOC_TIMER_GROUPS) && (timer_id < 0); i++) {
|
||||
for (int i = 0; i < SOC_TIMER_GROUPS; i++) {
|
||||
group = gptimer_acquire_group_handle(i);
|
||||
ESP_GOTO_ON_FALSE(group, ESP_ERR_NO_MEM, err, TAG, "no mem for group (%d)", i);
|
||||
ESP_RETURN_ON_FALSE(group, ESP_ERR_NO_MEM, TAG, "no mem for group (%d)", i);
|
||||
// loop to search free timer in the group
|
||||
portENTER_CRITICAL(&group->spinlock);
|
||||
for (int j = 0; j < SOC_TIMER_GROUP_TIMERS_PER_GROUP; j++) {
|
||||
if (!group->timers[j]) {
|
||||
group_id = i;
|
||||
timer_id = j;
|
||||
group->timers[j] = timer;
|
||||
break;
|
||||
@@ -130,12 +120,60 @@ esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *re
|
||||
if (timer_id < 0) {
|
||||
gptimer_release_group_handle(group);
|
||||
group = NULL;
|
||||
} else {
|
||||
timer->timer_id = timer_id;
|
||||
timer->group = group;
|
||||
break;;
|
||||
}
|
||||
}
|
||||
ESP_RETURN_ON_FALSE(timer_id != -1, ESP_ERR_NOT_FOUND, TAG, "no free timer");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void gptimer_unregister_from_group(gptimer_t *timer)
|
||||
{
|
||||
gptimer_group_t *group = timer->group;
|
||||
int timer_id = timer->timer_id;
|
||||
portENTER_CRITICAL(&group->spinlock);
|
||||
group->timers[timer_id] = NULL;
|
||||
portEXIT_CRITICAL(&group->spinlock);
|
||||
// timer has a reference on group, release it now
|
||||
gptimer_release_group_handle(group);
|
||||
}
|
||||
|
||||
static esp_err_t gptimer_destory(gptimer_t *timer)
|
||||
{
|
||||
if (timer->pm_lock) {
|
||||
ESP_RETURN_ON_ERROR(esp_pm_lock_delete(timer->pm_lock), TAG, "delete pm_lock failed");
|
||||
}
|
||||
if (timer->intr) {
|
||||
ESP_RETURN_ON_ERROR(esp_intr_free(timer->intr), TAG, "delete interrupt service failed");
|
||||
}
|
||||
if (timer->group) {
|
||||
gptimer_unregister_from_group(timer);
|
||||
}
|
||||
free(timer);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *ret_timer)
|
||||
{
|
||||
#if CONFIG_GPTIMER_ENABLE_DEBUG_LOG
|
||||
esp_log_level_set(TAG, ESP_LOG_DEBUG);
|
||||
#endif
|
||||
esp_err_t ret = ESP_OK;
|
||||
gptimer_t *timer = NULL;
|
||||
ESP_GOTO_ON_FALSE(config && ret_timer, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
|
||||
ESP_GOTO_ON_FALSE(config->resolution_hz, ESP_ERR_INVALID_ARG, err, TAG, "invalid timer resolution:%d", config->resolution_hz);
|
||||
|
||||
timer = heap_caps_calloc(1, sizeof(gptimer_t), GPTIMER_MEM_ALLOC_CAPS);
|
||||
ESP_GOTO_ON_FALSE(timer, ESP_ERR_NO_MEM, err, TAG, "no mem for gptimer");
|
||||
// register timer to the group (because one group can have several timers)
|
||||
ESP_GOTO_ON_ERROR(gptimer_register_to_group(timer), err, TAG, "register timer failed");
|
||||
gptimer_group_t *group = timer->group;
|
||||
int group_id = group->group_id;
|
||||
int timer_id = timer->timer_id;
|
||||
|
||||
ESP_GOTO_ON_FALSE(timer_id != -1, ESP_ERR_NOT_FOUND, err, TAG, "no free timer");
|
||||
timer->timer_id = timer_id;
|
||||
timer->group = group;
|
||||
// initialize HAL layer
|
||||
timer_hal_init(&timer->hal, group_id, timer_id);
|
||||
// stop counter, alarm, auto-reload
|
||||
@@ -165,49 +203,27 @@ esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *re
|
||||
|
||||
err:
|
||||
if (timer) {
|
||||
if (timer->pm_lock) {
|
||||
esp_pm_lock_delete(timer->pm_lock);
|
||||
}
|
||||
free(timer);
|
||||
}
|
||||
if (group) {
|
||||
gptimer_release_group_handle(group);
|
||||
gptimer_destory(timer);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t gptimer_del_timer(gptimer_handle_t timer)
|
||||
{
|
||||
gptimer_group_t *group = NULL;
|
||||
bool valid_state = true;
|
||||
ESP_RETURN_ON_FALSE(timer, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
||||
portENTER_CRITICAL(&timer->spinlock);
|
||||
if (timer->fsm != GPTIMER_FSM_STOP) {
|
||||
valid_state = false;
|
||||
}
|
||||
portEXIT_CRITICAL(&timer->spinlock);
|
||||
ESP_RETURN_ON_FALSE(valid_state, ESP_ERR_INVALID_STATE, TAG, "can't delete timer as it's not in stop state");
|
||||
group = timer->group;
|
||||
gptimer_group_t *group = timer->group;
|
||||
int group_id = group->group_id;
|
||||
int timer_id = timer->timer_id;
|
||||
|
||||
if (timer->intr) {
|
||||
esp_intr_free(timer->intr);
|
||||
ESP_LOGD(TAG, "uninstall interrupt service for timer (%d,%d)", group_id, timer_id);
|
||||
}
|
||||
if (timer->pm_lock) {
|
||||
esp_pm_lock_delete(timer->pm_lock);
|
||||
ESP_LOGD(TAG, "uninstall APB_FREQ_MAX lock for timer (%d,%d)", group_id, timer_id);
|
||||
}
|
||||
free(timer);
|
||||
bool valid_state = true;
|
||||
portENTER_CRITICAL(&timer->spinlock);
|
||||
valid_state = timer->fsm == GPTIMER_FSM_STOP;
|
||||
portEXIT_CRITICAL(&timer->spinlock);
|
||||
ESP_RETURN_ON_FALSE(valid_state, ESP_ERR_INVALID_STATE, TAG, "can't delete timer as it's not stop yet");
|
||||
|
||||
ESP_LOGD(TAG, "del timer (%d,%d)", group_id, timer_id);
|
||||
|
||||
portENTER_CRITICAL(&group->spinlock);
|
||||
group->timers[timer_id] = NULL;
|
||||
portEXIT_CRITICAL(&group->spinlock);
|
||||
// timer has a reference on group, release it now
|
||||
gptimer_release_group_handle(group);
|
||||
|
||||
// recycle memory resource
|
||||
ESP_RETURN_ON_ERROR(gptimer_destory(timer), TAG, "destory gptimer failed");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -275,18 +291,20 @@ esp_err_t gptimer_set_alarm_action(gptimer_handle_t timer, const gptimer_alarm_c
|
||||
bool valid_auto_reload = !config->flags.auto_reload_on_alarm || config->alarm_count != config->reload_count;
|
||||
ESP_RETURN_ON_FALSE_ISR(valid_auto_reload, ESP_ERR_INVALID_ARG, TAG, "reload count can't equal to alarm count");
|
||||
|
||||
portENTER_CRITICAL_SAFE(&timer->spinlock);
|
||||
timer->reload_count = config->reload_count;
|
||||
timer->alarm_count = config->alarm_count;
|
||||
timer->flags.auto_reload_on_alarm = config->flags.auto_reload_on_alarm;
|
||||
timer->flags.alarm_en = true;
|
||||
|
||||
portENTER_CRITICAL_SAFE(&timer->spinlock);
|
||||
timer_ll_set_reload_value(timer->hal.dev, timer->timer_id, config->reload_count);
|
||||
timer_ll_set_alarm_value(timer->hal.dev, timer->timer_id, config->alarm_count);
|
||||
portEXIT_CRITICAL_SAFE(&timer->spinlock);
|
||||
} else {
|
||||
portENTER_CRITICAL_SAFE(&timer->spinlock);
|
||||
timer->flags.auto_reload_on_alarm = false;
|
||||
timer->flags.alarm_en = false;
|
||||
portEXIT_CRITICAL_SAFE(&timer->spinlock);
|
||||
}
|
||||
|
||||
portENTER_CRITICAL_SAFE(&timer->spinlock);
|
||||
@@ -486,20 +504,3 @@ esp_err_t gptimer_get_pm_lock(gptimer_handle_t timer, esp_pm_lock_handle_t *ret_
|
||||
*ret_pm_lock = timer->pm_lock;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function will be called during start up, to check that gptimer driver is not running along with the legacy timer group driver
|
||||
*/
|
||||
__attribute__((constructor))
|
||||
static void check_gptimer_driver_conflict(void)
|
||||
{
|
||||
#if CONFIG_GPTIMER_ENABLE_DEBUG_LOG
|
||||
esp_log_level_set(TAG, ESP_LOG_DEBUG);
|
||||
#endif
|
||||
extern int timer_group_driver_init_count;
|
||||
timer_group_driver_init_count++;
|
||||
if (timer_group_driver_init_count > 1) {
|
||||
ESP_EARLY_LOGE(TAG, "CONFLICT! The gptimer driver can't work along with the legacy timer group driver");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user