|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#include "driver/touch_sensor_common.h" |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | +extern "C" { |
| 13 | +#endif |
| 14 | + |
| 15 | +/** |
| 16 | + * @brief Configure touch pad interrupt threshold. |
| 17 | + * |
| 18 | + * @note If FSM mode is set to TOUCH_FSM_MODE_TIMER, this function will be blocked for one measurement cycle and wait for data to be valid. |
| 19 | + * |
| 20 | + * @param touch_num touch pad index |
| 21 | + * @param threshold interrupt threshold, |
| 22 | + * |
| 23 | + * @return |
| 24 | + * - ESP_OK Success |
| 25 | + * - ESP_ERR_INVALID_ARG if argument wrong |
| 26 | + * - ESP_FAIL if touch pad not initialized |
| 27 | + */ |
| 28 | +esp_err_t touch_pad_config(touch_pad_t touch_num, uint16_t threshold); |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief get touch sensor counter value. |
| 32 | + * Each touch sensor has a counter to count the number of charge/discharge cycles. |
| 33 | + * When the pad is not 'touched', we can get a number of the counter. |
| 34 | + * When the pad is 'touched', the value in counter will get smaller because of the larger equivalent capacitance. |
| 35 | + * |
| 36 | + * @note This API requests hardware measurement once. If IIR filter mode is enabled, |
| 37 | + * please use 'touch_pad_read_raw_data' interface instead. |
| 38 | + * |
| 39 | + * @param touch_num touch pad index |
| 40 | + * @param touch_value pointer to accept touch sensor value |
| 41 | + * |
| 42 | + * @return |
| 43 | + * - ESP_OK Success |
| 44 | + * - ESP_ERR_INVALID_ARG Touch pad parameter error |
| 45 | + * - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0. |
| 46 | + * - ESP_FAIL Touch pad not initialized |
| 47 | + */ |
| 48 | +esp_err_t touch_pad_read(touch_pad_t touch_num, uint16_t *touch_value); |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief get filtered touch sensor counter value by IIR filter. |
| 52 | + * |
| 53 | + * @note touch_pad_filter_start has to be called before calling touch_pad_read_filtered. |
| 54 | + * This function can be called from ISR |
| 55 | + * |
| 56 | + * @param touch_num touch pad index |
| 57 | + * @param touch_value pointer to accept touch sensor value |
| 58 | + * |
| 59 | + * @return |
| 60 | + * - ESP_OK Success |
| 61 | + * - ESP_ERR_INVALID_ARG Touch pad parameter error |
| 62 | + * - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0. |
| 63 | + * - ESP_FAIL Touch pad not initialized |
| 64 | + */ |
| 65 | +esp_err_t touch_pad_read_filtered(touch_pad_t touch_num, uint16_t *touch_value); |
| 66 | + |
| 67 | +/** |
| 68 | + * @brief get raw data (touch sensor counter value) from IIR filter process. |
| 69 | + * Need not request hardware measurements. |
| 70 | + * |
| 71 | + * @note touch_pad_filter_start has to be called before calling touch_pad_read_raw_data. |
| 72 | + * This function can be called from ISR |
| 73 | + * |
| 74 | + * @param touch_num touch pad index |
| 75 | + * @param touch_value pointer to accept touch sensor value |
| 76 | + * |
| 77 | + * @return |
| 78 | + * - ESP_OK Success |
| 79 | + * - ESP_ERR_INVALID_ARG Touch pad parameter error |
| 80 | + * - ESP_ERR_INVALID_STATE This touch pad hardware connection is error, the value of "touch_value" is 0. |
| 81 | + * - ESP_FAIL Touch pad not initialized |
| 82 | + */ |
| 83 | +esp_err_t touch_pad_read_raw_data(touch_pad_t touch_num, uint16_t *touch_value); |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief Callback function that is called after each IIR filter calculation. |
| 87 | + * @note This callback is called in timer task in each filtering cycle. |
| 88 | + * @note This callback should not be blocked. |
| 89 | + * @param raw_value The latest raw data(touch sensor counter value) that |
| 90 | + * points to all channels(raw_value[0..TOUCH_PAD_MAX-1]). |
| 91 | + * @param filtered_value The latest IIR filtered data(calculated from raw data) that |
| 92 | + * points to all channels(filtered_value[0..TOUCH_PAD_MAX-1]). |
| 93 | + * |
| 94 | + */ |
| 95 | +typedef void (* filter_cb_t)(uint16_t *raw_value, uint16_t *filtered_value); |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Register the callback function that is called after each IIR filter calculation. |
| 99 | + * @note The 'read_cb' callback is called in timer task in each filtering cycle. |
| 100 | + * @param read_cb Pointer to filtered callback function. |
| 101 | + * If the argument passed in is NULL, the callback will stop. |
| 102 | + * @return |
| 103 | + * - ESP_OK Success |
| 104 | + * - ESP_ERR_INVALID_ARG set error |
| 105 | + */ |
| 106 | +esp_err_t touch_pad_set_filter_read_cb(filter_cb_t read_cb); |
| 107 | + |
| 108 | +/** |
| 109 | + * @brief Register touch-pad ISR. |
| 110 | + * The handler will be attached to the same CPU core that this function is running on. |
| 111 | + * @param fn Pointer to ISR handler |
| 112 | + * @param arg Parameter for ISR |
| 113 | + * @return |
| 114 | + * - ESP_OK Success ; |
| 115 | + * - ESP_ERR_INVALID_ARG GPIO error |
| 116 | + * - ESP_ERR_NO_MEM No memory |
| 117 | + */ |
| 118 | +esp_err_t touch_pad_isr_register(intr_handler_t fn, void *arg); |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Set the clock cycles of each measurement |
| 122 | + * @note This function will specify the clock cycles of each measurement |
| 123 | + * and the clock is sourced from SOC_MOD_CLK_RTC_FAST, its default frequency is SOC_CLK_RC_FAST_FREQ_APPROX |
| 124 | + * The touch sensor will record the charge and discharge times during these clock cycles as the final result (raw value) |
| 125 | + * @note If clock cycles is too small, it may lead to inaccurate results. |
| 126 | + * |
| 127 | + * @param clock_cycle The clock cycles of each measurement |
| 128 | + * measure_time = clock_cycle / SOC_CLK_RC_FAST_FREQ_APPROX, the maximum measure time is 0xffff / SOC_CLK_RC_FAST_FREQ_APPROX |
| 129 | + * @return |
| 130 | + * - ESP_OK Set the clock cycle success |
| 131 | + */ |
| 132 | +esp_err_t touch_pad_set_measurement_clock_cycles(uint16_t clock_cycle); |
| 133 | + |
| 134 | +/** |
| 135 | + * @brief Get the clock cycles of each measurement |
| 136 | + * |
| 137 | + * @param clock_cycle The clock cycles of each measurement |
| 138 | + * @return |
| 139 | + * - ESP_OK Get the clock cycle success |
| 140 | + * - ESP_ERR_INVALID_ARG The input parameter is NULL |
| 141 | + */ |
| 142 | +esp_err_t touch_pad_get_measurement_clock_cycles(uint16_t *clock_cycle); |
| 143 | + |
| 144 | +/** |
| 145 | + * @brief Set the interval between two measurements |
| 146 | + * @note The touch sensor will sleep between two measurements |
| 147 | + * This function is to set the interval cycle |
| 148 | + * And the interval is clocked from SOC_MOD_CLK_RTC_SLOW, its default frequency is SOC_CLK_RC_SLOW_FREQ_APPROX |
| 149 | + * |
| 150 | + * @param interval_cycle The interval between two measurements |
| 151 | + * sleep_time = interval_cycle / SOC_CLK_RC_SLOW_FREQ_APPROX. |
| 152 | + * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function. |
| 153 | + * @return |
| 154 | + * - ESP_OK Set interval cycle success |
| 155 | + */ |
| 156 | +esp_err_t touch_pad_set_measurement_interval(uint16_t interval_cycle); |
| 157 | + |
| 158 | +/** |
| 159 | + * @brief Get the interval between two measurements |
| 160 | + * |
| 161 | + * @param interval_cycle The interval between two measurements |
| 162 | + * @return |
| 163 | + * - ESP_OK Get interval cycle success |
| 164 | + * - ESP_ERR_INVALID_ARG The input parameter is NULL |
| 165 | + */ |
| 166 | +esp_err_t touch_pad_get_measurement_interval(uint16_t *interval_cycle); |
| 167 | + |
| 168 | +/** |
| 169 | + * @brief Set touch sensor measurement and sleep time. |
| 170 | + * Excessive total time will slow down the touch response. |
| 171 | + * Too small measurement time will not be sampled enough, resulting in inaccurate measurements. |
| 172 | + * @note The touch sensor will count the number of charge/discharge cycles over a fixed period of time (specified as the second parameter). |
| 173 | + * That means the number of cycles (raw value) will decrease as the capacity of the touch pad is increasing. |
| 174 | + * @note The greater the duty cycle of the measurement time, the more system power is consumed. |
| 175 | + * |
| 176 | + * @param sleep_cycle The touch sensor will sleep after each measurement. |
| 177 | + * sleep_cycle decide the interval between each measurement. |
| 178 | + * t_sleep = sleep_cycle / SOC_CLK_RC_SLOW_FREQ_APPROX. |
| 179 | + * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function. |
| 180 | + * @param meas_cycle The duration of the touch sensor measurement. |
| 181 | + * t_meas = meas_cycle / SOC_CLK_RC_FAST_FREQ_APPROX, the maximum measure time is 0xffff / SOC_CLK_RC_FAST_FREQ_APPROX |
| 182 | + * @return |
| 183 | + * - ESP_OK on success |
| 184 | + */ |
| 185 | +esp_err_t touch_pad_set_meas_time(uint16_t sleep_cycle, uint16_t meas_cycle) |
| 186 | +__attribute__((deprecated("please use 'touch_pad_set_measurement_clock_cycles' and 'touch_pad_set_measurement_interval' instead"))); |
| 187 | + |
| 188 | +/** |
| 189 | + * @brief Get touch sensor measurement and sleep time |
| 190 | + * @param sleep_cycle Pointer to accept sleep cycle number |
| 191 | + * @param meas_cycle Pointer to accept measurement cycle count. |
| 192 | + * @return |
| 193 | + * - ESP_OK on success |
| 194 | + * - ESP_ERR_INVALID_ARG The input parameter is NULL |
| 195 | + */ |
| 196 | +esp_err_t touch_pad_get_meas_time(uint16_t *sleep_cycle, uint16_t *meas_cycle) |
| 197 | +__attribute__((deprecated("please use 'touch_pad_get_measurement_clock_cycles' and 'touch_pad_get_measurement_interval' instead"))); |
| 198 | + |
| 199 | +/** |
| 200 | + * @brief Trigger a touch sensor measurement, only support in SW mode of FSM |
| 201 | + * @return |
| 202 | + * - ESP_OK on success |
| 203 | + */ |
| 204 | +esp_err_t touch_pad_sw_start(void); |
| 205 | + |
| 206 | +/** |
| 207 | + * @brief Set touch sensor interrupt threshold |
| 208 | + * @param touch_num touch pad index |
| 209 | + * @param threshold threshold of touchpad count, refer to touch_pad_set_trigger_mode to see how to set trigger mode. |
| 210 | + * @return |
| 211 | + * - ESP_OK on success |
| 212 | + * - ESP_ERR_INVALID_ARG if argument is wrong |
| 213 | + */ |
| 214 | +esp_err_t touch_pad_set_thresh(touch_pad_t touch_num, uint16_t threshold); |
| 215 | + |
| 216 | +/** |
| 217 | + * @brief Get touch sensor interrupt threshold |
| 218 | + * @param touch_num touch pad index |
| 219 | + * @param threshold pointer to accept threshold |
| 220 | + * @return |
| 221 | + * - ESP_OK on success |
| 222 | + * - ESP_ERR_INVALID_ARG if argument is wrong |
| 223 | + */ |
| 224 | +esp_err_t touch_pad_get_thresh(touch_pad_t touch_num, uint16_t *threshold); |
| 225 | + |
| 226 | +/** |
| 227 | + * @brief Set touch sensor interrupt trigger mode. |
| 228 | + * Interrupt can be triggered either when counter result is less than |
| 229 | + * threshold or when counter result is more than threshold. |
| 230 | + * @param mode touch sensor interrupt trigger mode |
| 231 | + * @return |
| 232 | + * - ESP_OK on success |
| 233 | + * - ESP_ERR_INVALID_ARG if argument is wrong |
| 234 | + */ |
| 235 | +esp_err_t touch_pad_set_trigger_mode(touch_trigger_mode_t mode); |
| 236 | + |
| 237 | +/** |
| 238 | + * @brief Get touch sensor interrupt trigger mode |
| 239 | + * @param mode pointer to accept touch sensor interrupt trigger mode |
| 240 | + * @return |
| 241 | + * - ESP_OK on success |
| 242 | + */ |
| 243 | +esp_err_t touch_pad_get_trigger_mode(touch_trigger_mode_t *mode); |
| 244 | + |
| 245 | +/** |
| 246 | + * @brief Set touch sensor interrupt trigger source. There are two sets of touch signals. |
| 247 | + * Set1 and set2 can be mapped to several touch signals. Either set will be triggered |
| 248 | + * if at least one of its touch signal is 'touched'. The interrupt can be configured to be generated |
| 249 | + * if set1 is triggered, or only if both sets are triggered. |
| 250 | + * @param src touch sensor interrupt trigger source |
| 251 | + * @return |
| 252 | + * - ESP_OK on success |
| 253 | + * - ESP_ERR_INVALID_ARG if argument is wrong |
| 254 | + */ |
| 255 | +esp_err_t touch_pad_set_trigger_source(touch_trigger_src_t src); |
| 256 | + |
| 257 | +/** |
| 258 | + * @brief Get touch sensor interrupt trigger source |
| 259 | + * @param src pointer to accept touch sensor interrupt trigger source |
| 260 | + * @return |
| 261 | + * - ESP_OK on success |
| 262 | + */ |
| 263 | +esp_err_t touch_pad_get_trigger_source(touch_trigger_src_t *src); |
| 264 | + |
| 265 | +/** |
| 266 | + * @brief Set touch sensor group mask. |
| 267 | + * Touch pad module has two sets of signals, 'Touched' signal is triggered only if |
| 268 | + * at least one of touch pad in this group is "touched". |
| 269 | + * This function will set the register bits according to the given bitmask. |
| 270 | + * @param set1_mask bitmask of touch sensor signal group1, it's a 10-bit value |
| 271 | + * @param set2_mask bitmask of touch sensor signal group2, it's a 10-bit value |
| 272 | + * @param en_mask bitmask of touch sensor work enable, it's a 10-bit value |
| 273 | + * @return |
| 274 | + * - ESP_OK on success |
| 275 | + * - ESP_ERR_INVALID_ARG if argument is wrong |
| 276 | + */ |
| 277 | +esp_err_t touch_pad_set_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask); |
| 278 | + |
| 279 | +/** |
| 280 | + * @brief Get touch sensor group mask. |
| 281 | + * @param set1_mask pointer to accept bitmask of touch sensor signal group1, it's a 10-bit value |
| 282 | + * @param set2_mask pointer to accept bitmask of touch sensor signal group2, it's a 10-bit value |
| 283 | + * @param en_mask pointer to accept bitmask of touch sensor work enable, it's a 10-bit value |
| 284 | + * @return |
| 285 | + * - ESP_OK on success |
| 286 | + */ |
| 287 | +esp_err_t touch_pad_get_group_mask(uint16_t *set1_mask, uint16_t *set2_mask, uint16_t *en_mask); |
| 288 | + |
| 289 | +/** |
| 290 | + * @brief Clear touch sensor group mask. |
| 291 | + * Touch pad module has two sets of signals, Interrupt is triggered only if |
| 292 | + * at least one of touch pad in this group is "touched". |
| 293 | + * This function will clear the register bits according to the given bitmask. |
| 294 | + * @param set1_mask bitmask touch sensor signal group1, it's a 10-bit value |
| 295 | + * @param set2_mask bitmask touch sensor signal group2, it's a 10-bit value |
| 296 | + * @param en_mask bitmask of touch sensor work enable, it's a 10-bit value |
| 297 | + * @return |
| 298 | + * - ESP_OK on success |
| 299 | + * - ESP_ERR_INVALID_ARG if argument is wrong |
| 300 | + */ |
| 301 | +esp_err_t touch_pad_clear_group_mask(uint16_t set1_mask, uint16_t set2_mask, uint16_t en_mask); |
| 302 | + |
| 303 | +/** |
| 304 | + * @brief To enable touch pad interrupt |
| 305 | + * @return |
| 306 | + * - ESP_OK on success |
| 307 | + */ |
| 308 | +esp_err_t touch_pad_intr_enable(void); |
| 309 | + |
| 310 | +/** |
| 311 | + * @brief To disable touch pad interrupt |
| 312 | + * @return |
| 313 | + * - ESP_OK on success |
| 314 | + */ |
| 315 | +esp_err_t touch_pad_intr_disable(void); |
| 316 | + |
| 317 | +/** |
| 318 | + * @brief To clear touch pad interrupt |
| 319 | + * @return |
| 320 | + * - ESP_OK on success |
| 321 | + */ |
| 322 | +esp_err_t touch_pad_intr_clear(void); |
| 323 | + |
| 324 | +/** |
| 325 | + * @brief set touch pad filter calibration period, in ms. |
| 326 | + * Need to call touch_pad_filter_start before all touch filter APIs |
| 327 | + * @param new_period_ms filter period, in ms |
| 328 | + * @return |
| 329 | + * - ESP_OK Success |
| 330 | + * - ESP_ERR_INVALID_STATE driver state error |
| 331 | + * - ESP_ERR_INVALID_ARG parameter error |
| 332 | + */ |
| 333 | +esp_err_t touch_pad_set_filter_period(uint32_t new_period_ms); |
| 334 | + |
| 335 | +/** |
| 336 | + * @brief get touch pad filter calibration period, in ms |
| 337 | + * Need to call touch_pad_filter_start before all touch filter APIs |
| 338 | + * @param p_period_ms pointer to accept period |
| 339 | + * @return |
| 340 | + * - ESP_OK Success |
| 341 | + * - ESP_ERR_INVALID_STATE driver state error |
| 342 | + * - ESP_ERR_INVALID_ARG parameter error |
| 343 | + */ |
| 344 | +esp_err_t touch_pad_get_filter_period(uint32_t *p_period_ms); |
| 345 | + |
| 346 | +/** |
| 347 | + * @brief start touch pad filter function |
| 348 | + * This API will start a filter to process the noise in order to prevent false triggering |
| 349 | + * when detecting slight change of capacitance. |
| 350 | + * Need to call touch_pad_filter_start before all touch filter APIs |
| 351 | + * |
| 352 | + * @note This filter uses FreeRTOS timer, which is dispatched from a task with |
| 353 | + * priority 1 by default on CPU 0. So if some application task with higher priority |
| 354 | + * takes a lot of CPU0 time, then the quality of data obtained from this filter will be affected. |
| 355 | + * You can adjust FreeRTOS timer task priority in menuconfig. |
| 356 | + * @param filter_period_ms filter calibration period, in ms |
| 357 | + * @return |
| 358 | + * - ESP_OK Success |
| 359 | + * - ESP_ERR_INVALID_ARG parameter error |
| 360 | + * - ESP_ERR_NO_MEM No memory for driver |
| 361 | + * - ESP_ERR_INVALID_STATE driver state error |
| 362 | + */ |
| 363 | +esp_err_t touch_pad_filter_start(uint32_t filter_period_ms); |
| 364 | + |
| 365 | +/** |
| 366 | + * @brief stop touch pad filter function |
| 367 | + * Need to call touch_pad_filter_start before all touch filter APIs |
| 368 | + * @return |
| 369 | + * - ESP_OK Success |
| 370 | + * - ESP_ERR_INVALID_STATE driver state error |
| 371 | + */ |
| 372 | +esp_err_t touch_pad_filter_stop(void); |
| 373 | + |
| 374 | +/** |
| 375 | + * @brief delete touch pad filter driver and release the memory |
| 376 | + * Need to call touch_pad_filter_start before all touch filter APIs |
| 377 | + * @return |
| 378 | + * - ESP_OK Success |
| 379 | + * - ESP_ERR_INVALID_STATE driver state error |
| 380 | + */ |
| 381 | +esp_err_t touch_pad_filter_delete(void); |
| 382 | + |
| 383 | +#ifdef __cplusplus |
| 384 | +} |
| 385 | +#endif |
0 commit comments