gptimer: fix race condition between start and stop

Added state transition in gptimer_start/stop functions.
So that it's not possible to make a stopped timer continue to run
because of race condition.
This commit is contained in:
morris
2023-03-06 13:32:35 +08:00
parent ed97d230c8
commit 2d52334e5d
11 changed files with 153 additions and 74 deletions

View File

@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -7,6 +7,7 @@
#pragma once
#include <stdint.h>
#include <stdatomic.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "esp_err.h"
@@ -45,8 +46,11 @@ typedef struct gptimer_group_t {
} gptimer_group_t;
typedef enum {
GPTIMER_FSM_INIT,
GPTIMER_FSM_ENABLE,
GPTIMER_FSM_INIT, // Timer is initialized, but not enabled
GPTIMER_FSM_ENABLE, // Timer is enabled, but is not running
GPTIMER_FSM_ENABLE_WAIT, // Timer is in the middle of the enable process (Intermediate state)
GPTIMER_FSM_RUN, // Timer is in running
GPTIMER_FSM_RUN_WAIT, // Timer is in the middle of the run process (Intermediate state)
} gptimer_fsm_t;
struct gptimer_t {
@@ -57,7 +61,7 @@ struct gptimer_t {
uint64_t alarm_count;
gptimer_count_direction_t direction;
timer_hal_context_t hal;
gptimer_fsm_t fsm;
_Atomic gptimer_fsm_t fsm;
intr_handle_t intr;
portMUX_TYPE spinlock; // to protect per-timer resources concurrent accessed by task and ISR handler
gptimer_alarm_cb_t on_alarm;