touch_senser: fixed ci issue timer expired after it is deleted

pull/9803/head
laokaiyao 2022-08-30 14:50:10 +08:00
rodzic 0ecf0af5a1
commit 045b65e85d
2 zmienionych plików z 42 dodań i 36 usunięć

Wyświetl plik

@ -15,7 +15,7 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/xtensa_api.h" #include "freertos/xtensa_api.h"
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "freertos/timers.h" #include "esp_timer.h"
#include "esp_intr_alloc.h" #include "esp_intr_alloc.h"
#include "driver/rtc_io.h" #include "driver/rtc_io.h"
#include "driver/touch_pad.h" #include "driver/touch_pad.h"
@ -32,7 +32,7 @@
#include "hal/touch_sensor_hal.h" #include "hal/touch_sensor_hal.h"
typedef struct { typedef struct {
TimerHandle_t timer; esp_timer_handle_t timer;
uint16_t filtered_val[TOUCH_PAD_MAX]; uint16_t filtered_val[TOUCH_PAD_MAX];
uint32_t filter_last_val[TOUCH_PAD_MAX]; uint32_t filter_last_val[TOUCH_PAD_MAX];
uint16_t raw_val[TOUCH_PAD_MAX]; uint16_t raw_val[TOUCH_PAD_MAX];
@ -103,7 +103,6 @@ static void touch_pad_filter_cb(void *arg)
} }
uint16_t val = 0; uint16_t val = 0;
touch_fsm_mode_t mode; touch_fsm_mode_t mode;
xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
touch_pad_get_fsm_mode(&mode); touch_pad_get_fsm_mode(&mode);
for (int i = 0; i < TOUCH_PAD_MAX; i++) { for (int i = 0; i < TOUCH_PAD_MAX; i++) {
if ((s_touch_pad_init_bit >> i) & 0x1) { if ((s_touch_pad_init_bit >> i) & 0x1) {
@ -117,8 +116,6 @@ static void touch_pad_filter_cb(void *arg)
(s_touch_pad_filter->filter_last_val[i] + TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT; (s_touch_pad_filter->filter_last_val[i] + TOUCH_PAD_SHIFT_ROUND_DEFAULT) >> TOUCH_PAD_SHIFT_DEFAULT;
} }
} }
xTimerReset(s_touch_pad_filter->timer, portMAX_DELAY);
xSemaphoreGive(rtc_touch_mux);
if (s_filter_cb) { if (s_filter_cb) {
//return the raw data and filtered data. //return the raw data and filtered data.
s_filter_cb(s_touch_pad_filter->raw_val, s_touch_pad_filter->filtered_val); s_filter_cb(s_touch_pad_filter->raw_val, s_touch_pad_filter->filtered_val);
@ -335,11 +332,17 @@ esp_err_t touch_pad_init(void)
esp_err_t touch_pad_deinit(void) esp_err_t touch_pad_deinit(void)
{ {
ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized"); ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_FAIL, TOUCH_TAG, "Touch pad not initialized");
if (s_touch_pad_filter) { esp_err_t ret = ESP_OK;
touch_pad_filter_stop();
touch_pad_filter_delete();
}
xSemaphoreTake(rtc_touch_mux, portMAX_DELAY); xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
if (s_touch_pad_filter) {
if (s_touch_pad_filter->timer) {
ESP_GOTO_ON_ERROR(esp_timer_stop(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to stop the timer");
ESP_GOTO_ON_ERROR(esp_timer_delete(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to delete the timer");
s_touch_pad_filter->timer = NULL;
}
free(s_touch_pad_filter);
s_touch_pad_filter = NULL;
}
s_touch_pad_init_bit = 0x0000; s_touch_pad_init_bit = 0x0000;
TOUCH_ENTER_CRITICAL(); TOUCH_ENTER_CRITICAL();
touch_hal_deinit(); touch_hal_deinit();
@ -348,6 +351,9 @@ esp_err_t touch_pad_deinit(void)
vSemaphoreDelete(rtc_touch_mux); vSemaphoreDelete(rtc_touch_mux);
rtc_touch_mux = NULL; rtc_touch_mux = NULL;
return ESP_OK; return ESP_OK;
err:
xSemaphoreGive(rtc_touch_mux);
return ret;
} }
static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode) static esp_err_t _touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value, touch_fsm_mode_t mode)
@ -420,13 +426,10 @@ esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms)
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
xSemaphoreTake(rtc_touch_mux, portMAX_DELAY); xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
if (s_touch_pad_filter) { ESP_GOTO_ON_ERROR(esp_timer_stop(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to stop the timer");
xTimerChangePeriod(s_touch_pad_filter->timer, new_period_ms / portTICK_PERIOD_MS, portMAX_DELAY); ESP_GOTO_ON_ERROR(esp_timer_start_periodic(s_touch_pad_filter->timer, new_period_ms), err, TOUCH_TAG, "failed to start the timer");
s_touch_pad_filter->period = new_period_ms; s_touch_pad_filter->period = new_period_ms;
} else { err:
ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
ret = ESP_ERR_INVALID_STATE;
}
xSemaphoreGive(rtc_touch_mux); xSemaphoreGive(rtc_touch_mux);
return ret; return ret;
} }
@ -462,17 +465,24 @@ esp_err_t touch_pad_filter_start(uint32_t filter_period_ms)
} }
} }
if (s_touch_pad_filter->timer == NULL) { if (s_touch_pad_filter->timer == NULL) {
s_touch_pad_filter->timer = xTimerCreate("filter_tmr", filter_period_ms / portTICK_PERIOD_MS, pdFALSE, esp_timer_create_args_t timer_cfg = {
NULL, (TimerCallbackFunction_t) touch_pad_filter_cb); .callback = touch_pad_filter_cb,
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "touch filter timer",
.skip_unhandled_events = true,
};
esp_timer_create(&timer_cfg, &(s_touch_pad_filter->timer));
if (s_touch_pad_filter->timer == NULL) { if (s_touch_pad_filter->timer == NULL) {
free(s_touch_pad_filter); free(s_touch_pad_filter);
s_touch_pad_filter = NULL; s_touch_pad_filter = NULL;
goto err_no_mem; goto err_no_mem;
} }
s_touch_pad_filter->period = filter_period_ms; s_touch_pad_filter->period = filter_period_ms;
esp_timer_start_periodic(s_touch_pad_filter->timer, filter_period_ms);
} }
xSemaphoreGive(rtc_touch_mux); xSemaphoreGive(rtc_touch_mux);
touch_pad_filter_cb(NULL);
return ESP_OK; return ESP_OK;
err_no_mem: err_no_mem:
@ -484,13 +494,10 @@ esp_err_t touch_pad_filter_stop(void)
{ {
ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized"); ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized"); ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
esp_err_t ret = ESP_OK;
xSemaphoreTake(rtc_touch_mux, portMAX_DELAY); xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
if (s_touch_pad_filter) { esp_err_t ret = esp_timer_stop(s_touch_pad_filter->timer);
xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY); if (ret != ESP_OK) {
} else { ESP_LOGE(TOUCH_TAG, "failed to stop the timer");
ESP_LOGE(TOUCH_TAG, "Touch pad filter deleted");
ret = ESP_ERR_INVALID_STATE;
} }
xSemaphoreGive(rtc_touch_mux); xSemaphoreGive(rtc_touch_mux);
return ret; return ret;
@ -500,16 +507,16 @@ esp_err_t touch_pad_filter_delete(void)
{ {
ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized"); ESP_RETURN_ON_FALSE(s_touch_pad_filter, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad filter not initialized");
ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized"); ESP_RETURN_ON_FALSE(rtc_touch_mux, ESP_ERR_INVALID_STATE, TOUCH_TAG, "Touch pad not initialized");
esp_err_t ret = ESP_OK;
xSemaphoreTake(rtc_touch_mux, portMAX_DELAY); xSemaphoreTake(rtc_touch_mux, portMAX_DELAY);
if (s_touch_pad_filter) { if (s_touch_pad_filter->timer) {
if (s_touch_pad_filter->timer) { ESP_GOTO_ON_ERROR(esp_timer_stop(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to stop the timer");
xTimerStop(s_touch_pad_filter->timer, portMAX_DELAY); ESP_GOTO_ON_ERROR(esp_timer_delete(s_touch_pad_filter->timer), err, TOUCH_TAG, "failed to delete the timer");
xTimerDelete(s_touch_pad_filter->timer, portMAX_DELAY); s_touch_pad_filter->timer = NULL;
s_touch_pad_filter->timer = NULL;
}
free(s_touch_pad_filter);
s_touch_pad_filter = NULL;
} }
free(s_touch_pad_filter);
s_touch_pad_filter = NULL;
err:
xSemaphoreGive(rtc_touch_mux); xSemaphoreGive(rtc_touch_mux);
return ESP_OK; return ret;
} }

Wyświetl plik

@ -237,7 +237,7 @@ static int test_touch_parameter(touch_pad_t pad_num, int meas_time, int slp_time
touch_pad_set_cnt_mode(pad_num, slope, TOUCH_PAD_TIE_OPT_DEFAULT); touch_pad_set_cnt_mode(pad_num, slope, TOUCH_PAD_TIE_OPT_DEFAULT);
// Initialize and start a software filter to detect slight change of capacitance. // Initialize and start a software filter to detect slight change of capacitance.
touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD); TEST_ESP_OK(touch_pad_filter_start(TOUCHPAD_FILTER_TOUCH_PERIOD));
vTaskDelay(500 / portTICK_PERIOD_MS); vTaskDelay(500 / portTICK_PERIOD_MS);
// Start task to read values sensed by pads // Start task to read values sensed by pads
@ -302,7 +302,6 @@ static bool s_pad_activated[TOUCH_PAD_MAX];
static void test_touch_intr_cb(void *arg) static void test_touch_intr_cb(void *arg)
{ {
uint32_t pad_intr = touch_pad_get_status(); uint32_t pad_intr = touch_pad_get_status();
esp_rom_printf("T%x ", pad_intr);
//clear interrupt //clear interrupt
touch_pad_clear_status(); touch_pad_clear_status();
for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) { for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {