diff --git a/components/esp_driver_touch_sens/hw_ver3/touch_version_specific.c b/components/esp_driver_touch_sens/hw_ver3/touch_version_specific.c index e2da59d5ccc..c2aa224f98e 100644 --- a/components/esp_driver_touch_sens/hw_ver3/touch_version_specific.c +++ b/components/esp_driver_touch_sens/hw_ver3/touch_version_specific.c @@ -64,6 +64,10 @@ void IRAM_ATTR touch_priv_default_intr_handler(void *arg) touch_base_event_data_t data; touch_ll_get_active_channel_mask(&data.status_mask); int ch_offset = touch_ll_get_current_meas_channel() - TOUCH_MIN_CHAN_ID; + if (ch_offset < 0 || ch_offset >= (int)SOC_TOUCH_SENSOR_NUM) { + /* Not a valid channel */ + return; + } data.chan = g_touch->ch[ch_offset]; /* If the channel is not registered, return directly */ if (!data.chan) { diff --git a/components/esp_driver_touch_sens/test_apps/touch_sens/main/test_touch_sens_common.c b/components/esp_driver_touch_sens/test_apps/touch_sens/main/test_touch_sens_common.c index c5c0adc3cee..255f979133e 100644 --- a/components/esp_driver_touch_sens/test_apps/touch_sens/main/test_touch_sens_common.c +++ b/components/esp_driver_touch_sens/test_apps/touch_sens/main/test_touch_sens_common.c @@ -196,3 +196,53 @@ TEST_CASE("touch_sens_active_inactive_test", "[touch]") TEST_ASSERT_EQUAL_INT32(touch_cnt, cb_data.active_count); TEST_ASSERT_EQUAL_INT32(touch_cnt, cb_data.inactive_count); } + +#if SOC_TOUCH_SENSOR_VERSION > 1 +TEST_CASE("touch_sens_current_meas_channel_test", "[touch]") +{ + touch_sensor_handle_t touch = NULL; + touch_channel_handle_t touch_chan = NULL; + + touch_sensor_config_t sens_cfg = TOUCH_SENSOR_DEFAULT_BASIC_CONFIG(TOUCH_SAMPLE_CFG_NUM, s_sample_cfg); + TEST_ESP_OK(touch_sensor_new_controller(&sens_cfg, &touch)); + + /* Configuring the filter */ + touch_sensor_filter_config_t filter_cfg = TOUCH_SENSOR_DEFAULT_FILTER_CONFIG(); + TEST_ESP_OK(touch_sensor_config_filter(touch, &filter_cfg)); + + int err_chan[TOUCH_MAX_CHAN_ID - TOUCH_MIN_CHAN_ID + 1] = {[0 ...(TOUCH_MAX_CHAN_ID - TOUCH_MIN_CHAN_ID)] = -1}; + int scan_times = 100; + uint32_t curr_chan[scan_times]; + /* Loop all channels */ + for (int ch_id = TOUCH_MIN_CHAN_ID; ch_id <= TOUCH_MAX_CHAN_ID; ch_id++) { + /* New a channel */ + TEST_ESP_OK(touch_sensor_new_channel(touch, ch_id, &s_chan_cfg, &touch_chan)); + TEST_ESP_OK(touch_sensor_enable(touch)); + /* Trigger one-shot scanning to update the current measuring channel */ + touch_sensor_trigger_oneshot_scanning(touch, 2000); + + /* Read the current measuring channel for several times */ + for (int i = 0; i < scan_times; i++) { + curr_chan[i] = touch_ll_get_current_meas_channel(); + /* Check if the current measuring channel is the same as the channel id */ + if (curr_chan[i] != ch_id) { + err_chan[ch_id - TOUCH_MIN_CHAN_ID] = curr_chan[i]; + } + } + /* Check if there is any error */ + TEST_ESP_OK(touch_sensor_disable(touch)); + TEST_ESP_OK(touch_sensor_del_channel(touch_chan)); + } + TEST_ESP_OK(touch_sensor_del_controller(touch)); + + /* Check if there is any error in the current measuring channel from any channel */ + bool has_error = false; + for (int i = 0; i < TOUCH_MAX_CHAN_ID - TOUCH_MIN_CHAN_ID + 1; i++) { + if (err_chan[i] >= 0) { + ESP_LOGE("TOUCH_TEST", "actual channel is %d, but current measuring channel reads %d", i + TOUCH_MIN_CHAN_ID, err_chan[i]); + has_error = true; + } + } + TEST_ASSERT_FALSE(has_error); +} +#endif // SOC_TOUCH_SENSOR_VERSION > 1 diff --git a/components/hal/esp32p4/include/hal/touch_sensor_ll.h b/components/hal/esp32p4/include/hal/touch_sensor_ll.h index 5d47aa137ec..cc0339d5c8d 100644 --- a/components/hal/esp32p4/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32p4/include/hal/touch_sensor_ll.h @@ -482,11 +482,7 @@ static inline void touch_ll_set_idle_channel_connect(touch_pad_conn_type_t type) __attribute__((always_inline)) static inline uint32_t touch_ll_get_current_meas_channel(void) { - uint32_t curr_chan = LP_TOUCH.chn_status.scan_curr; - HAL_ASSERT(curr_chan < 14); - // Workaround: the curr channel read 0 when the actual channel is 14 - curr_chan = curr_chan == 0 ? 14 : curr_chan; - return curr_chan; + return LP_TOUCH.chn_status.scan_curr; } /**