touch_element.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/semphr.h"
  9. #include "freertos/queue.h"
  10. #include "esp_timer.h"
  11. #include "esp_log.h"
  12. #include "hal/touch_sensor_hal.h" //TODO: remove hal
  13. #include "touch_element/touch_element_private.h"
  14. #define TE_CLASS_ITEM(cls, cls_type, cls_item) ((&((cls)[cls_type]))->cls_item)
  15. #define TE_CLASS_FOREACH(cls_var, cls_start, cls_end) \
  16. for ((cls_var) = (cls_start); \
  17. (cls_var) < (cls_end); \
  18. (cls_var)++)
  19. #define TE_CLS_METHODS_INITIALIZER(cls, cls_start, cls_end) do { \
  20. typeof(cls_start) cls_method; \
  21. TE_CLASS_FOREACH(cls_method, cls_start, cls_end) { \
  22. TE_CLASS_ITEM(cls, cls_method, handle) = NULL; \
  23. } \
  24. } while (0)
  25. #define TE_CLASS_FOREACH_CHECK_CHANNEL(cls, cls_start, cls_end, channel) ({ \
  26. bool ret = false; \
  27. typeof(cls_start) cls_method; \
  28. TE_CLASS_FOREACH(cls_method, cls_start, cls_end) { \
  29. if (TE_CLASS_ITEM(cls, cls_method, handle) != NULL) { \
  30. ret |= TE_CLASS_ITEM(cls, cls_method, check_channel(channel)); \
  31. } \
  32. } \
  33. ret; \
  34. })
  35. #define TE_CLASS_FOREACH_SET_THRESHOLD(cls, cls_start, cls_end) do { \
  36. typeof(cls_start) cls_method; \
  37. TE_CLASS_FOREACH(cls_method, cls_start, cls_end) { \
  38. if (TE_CLASS_ITEM(cls, cls_method, handle) != NULL) { \
  39. TE_CLASS_ITEM(cls, cls_method, set_threshold()); \
  40. } \
  41. } \
  42. } while (0)
  43. #define TE_CLASS_FOREACH_PROCESS_STATE(cls, cls_start, cls_end) do { \
  44. typeof(cls_start) cls_method; \
  45. TE_CLASS_FOREACH(cls_method, cls_start, cls_end) { \
  46. if (TE_CLASS_ITEM(cls, cls_method, handle) != NULL) { \
  47. TE_CLASS_ITEM(cls, cls_method, process_state()); \
  48. } \
  49. } \
  50. } while (0)
  51. #define TE_CLASS_FOREACH_UPDATE_STATE(cls, cls_start, cls_end, channel, state) do {\
  52. typeof(cls_start) cls_method; \
  53. TE_CLASS_FOREACH(cls_method, cls_start, cls_end) { \
  54. if (TE_CLASS_ITEM(cls, cls_method, handle) != NULL) { \
  55. TE_CLASS_ITEM(cls, cls_method, update_state(channel, state)); \
  56. } \
  57. } \
  58. } while (0)
  59. #define TE_PROCESSING_PERIOD(obj) ((obj)->global_config->software.processing_period)
  60. #define TE_WATERPROOF_DIVIDER(obj) ((obj)->global_config->software.waterproof_threshold_divider)
  61. typedef enum {
  62. TE_INTR_PRESS = 0, //Touch sensor press interrupt(TOUCH_PAD_INTR_MASK_ACTIVE)
  63. TE_INTR_RELEASE, //Touch sensor release interrupt(TOUCH_PAD_INTR_MASK_INACTIVE)
  64. TE_INTR_TIMEOUT, //Touch sensor scan timeout interrupt(TOUCH_PAD_INTR_MASK_TIMEOUT)
  65. TE_INTR_SCAN_DONE, //Touch sensor scan done interrupt(TOUCH_PAD_INTR_MASK_SCAN_DONE), now just use for setting threshold
  66. TE_INTR_MAX
  67. } te_intr_t;
  68. typedef struct {
  69. te_intr_t intr_type; //channel interrupt type
  70. te_state_t channel_state; //channel state
  71. touch_pad_t channel_num; //channel index
  72. } te_intr_msg_t;
  73. typedef struct {
  74. te_object_methods_t object_methods[TE_CLS_TYPE_MAX]; //Class(object) methods
  75. touch_elem_global_config_t *global_config; //Global initialization
  76. te_waterproof_handle_t waterproof_handle; //Waterproof configuration
  77. esp_timer_handle_t proc_timer; //Processing timer handle
  78. QueueHandle_t event_msg_queue; //Application event message queue (for user)
  79. QueueHandle_t intr_msg_queue; //Interrupt message (for internal)
  80. SemaphoreHandle_t mutex; //Global resource mutex
  81. bool is_set_threshold; //Threshold configuration state bit
  82. uint32_t denoise_channel_raw; //De-noise channel(TO) raw signal
  83. } te_obj_t;
  84. static te_obj_t *s_te_obj = NULL;
  85. /**
  86. * Internal de-noise channel(Touch channel 0) equivalent capacitance table, depends on hardware design
  87. *
  88. * Units: pF
  89. */
  90. static const float denoise_channel_equ_cap[TOUCH_PAD_DENOISE_CAP_MAX] = {5.0f, 6.4f, 7.8f, 9.2f, 10.6f, 12.0f, 13.4f, 14.8f};
  91. /**
  92. * Waterproof shield channel(Touch channel 14) equivalent capacitance table, depends on hardware design
  93. *
  94. * Units: pF
  95. */
  96. static const float shield_channel_ref_cap[TOUCH_PAD_SHIELD_DRV_MAX] = {40.0f, 80.0f, 120.0f, 160.0f, 200.0f, 240.0f, 280.0f, 320.0f};
  97. /* -------------------------------------------- Internal shared methods --------------------------------------------- */
  98. /* ------------------------------------------------- */
  99. /* ------------------------------------------------- System methods ------------------------------------------------- */
  100. static esp_err_t te_hw_init(const touch_elem_hw_config_t *hardware_init);
  101. static esp_err_t te_sw_init(const touch_elem_sw_config_t *software_init);
  102. static inline float te_get_internal_equ_cap(touch_pad_denoise_cap_t denoise_level);
  103. static float te_channel_get_equ_cap(touch_pad_t channel_num);
  104. static uint32_t te_read_raw_signal(touch_pad_t channel_num);
  105. static void te_intr_cb(void *arg);
  106. static void te_proc_timer_cb(void *arg);
  107. static inline esp_err_t te_object_set_threshold(void);
  108. static inline void te_object_process_state(void);
  109. static inline void te_object_update_state(te_intr_msg_t te_intr_msg);
  110. /* ----------------------------------------------- Waterproof methods ----------------------------------------------- */
  111. static inline bool waterproof_check_state(void);
  112. static inline bool waterproof_shield_check_state(void);
  113. static inline bool waterproof_guard_check_state(void);
  114. static bool waterproof_channel_check(touch_pad_t channel_num);
  115. static void waterproof_guard_set_threshold(void);
  116. static void waterproof_guard_update_state(touch_pad_t current_channel, te_state_t current_state);
  117. static touch_pad_shield_driver_t waterproof_get_shield_level(touch_pad_t guard_channel_num);
  118. /* ------------------------------------------------------------------------------------------------------------------ */
  119. esp_err_t touch_element_install(const touch_elem_global_config_t *global_config)
  120. {
  121. TE_CHECK(s_te_obj == NULL, ESP_ERR_INVALID_STATE);
  122. TE_CHECK(global_config != NULL, ESP_ERR_INVALID_ARG);
  123. s_te_obj = (te_obj_t *)calloc(1, sizeof(te_obj_t));
  124. TE_CHECK(s_te_obj != NULL, ESP_ERR_NO_MEM);
  125. esp_err_t ret = ESP_ERR_NO_MEM;
  126. s_te_obj->global_config = (touch_elem_global_config_t *)calloc(1, sizeof(touch_elem_global_config_t));
  127. s_te_obj->mutex = xSemaphoreCreateMutex();
  128. TE_CHECK_GOTO(s_te_obj->global_config != NULL && s_te_obj->mutex != NULL, cleanup);
  129. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  130. TE_CLS_METHODS_INITIALIZER(s_te_obj->object_methods, TE_CLS_TYPE_BUTTON, TE_CLS_TYPE_MAX);
  131. ret = te_hw_init(&global_config->hardware);
  132. if (ret != ESP_OK) {
  133. abort();
  134. }
  135. ret = te_sw_init(&global_config->software);
  136. if (ret != ESP_OK) {
  137. xSemaphoreGive(s_te_obj->mutex);
  138. goto cleanup;
  139. }
  140. xSemaphoreGive(s_te_obj->mutex);
  141. return ESP_OK;
  142. cleanup:
  143. TE_FREE_AND_NULL(s_te_obj->global_config);
  144. if (s_te_obj->mutex != NULL) {
  145. vSemaphoreDelete(s_te_obj->mutex);
  146. }
  147. TE_FREE_AND_NULL(s_te_obj);
  148. return ret;
  149. }
  150. esp_err_t touch_element_start(void)
  151. {
  152. TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE);
  153. esp_err_t ret;
  154. uint16_t inited_channel_mask;
  155. do {
  156. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  157. ret = touch_pad_get_channel_mask(&inited_channel_mask);
  158. if (inited_channel_mask == 0x0) {
  159. ESP_LOGE(TE_TAG, "Can not find Touch Sensor channel that has been initialized");
  160. ret = ESP_ERR_INVALID_STATE;
  161. break;
  162. }
  163. if (ret != ESP_OK) {
  164. break;
  165. }
  166. s_te_obj->is_set_threshold = false; //Threshold configuration will be set on touch sense start
  167. ret = esp_timer_start_periodic(s_te_obj->proc_timer, TE_PROCESSING_PERIOD(s_te_obj) * 1000);
  168. if (ret != ESP_OK) {
  169. break;
  170. }
  171. ret = touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_SCAN_DONE); //Use scan done interrupt to set threshold
  172. if (ret != ESP_OK) {
  173. break;
  174. }
  175. ret = touch_pad_fsm_start();
  176. if (ret != ESP_OK) {
  177. break;
  178. }
  179. xQueueReset(s_te_obj->event_msg_queue);
  180. xQueueReset(s_te_obj->intr_msg_queue);
  181. xSemaphoreGive(s_te_obj->mutex);
  182. return ESP_OK;
  183. } while (0);
  184. ESP_LOGE(TE_TAG, "Touch interface start failed:(%s)", __FUNCTION__ );
  185. xSemaphoreGive(s_te_obj->mutex);
  186. return ret;
  187. }
  188. esp_err_t touch_element_stop(void)
  189. {
  190. TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE);
  191. esp_err_t ret;
  192. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  193. ret = touch_pad_fsm_stop();
  194. if (ret != ESP_OK) {
  195. return ret;
  196. }
  197. ret = touch_pad_intr_disable(TOUCH_PAD_INTR_MASK_SCAN_DONE);
  198. if (ret != ESP_OK) {
  199. return ret;
  200. }
  201. ret = esp_timer_stop(s_te_obj->proc_timer);
  202. if (ret != ESP_OK) {
  203. return ret;
  204. }
  205. xSemaphoreGive(s_te_obj->mutex);
  206. return ESP_OK;
  207. }
  208. //TODO: add a new api that output system's run-time state
  209. void touch_element_uninstall(void)
  210. {
  211. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  212. if (s_te_obj == NULL) {
  213. xSemaphoreGive(s_te_obj->mutex);
  214. return;
  215. }
  216. esp_err_t ret;
  217. ret = touch_pad_deinit();
  218. if (ret != ESP_OK) {
  219. abort();
  220. }
  221. ret = esp_timer_delete(s_te_obj->proc_timer);
  222. if (ret != ESP_OK) {
  223. abort();
  224. }
  225. ret = touch_pad_intr_disable(TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT);
  226. if (ret != ESP_OK) {
  227. abort();
  228. }
  229. ret = touch_pad_isr_deregister(te_intr_cb, NULL);
  230. if (ret != ESP_OK) {
  231. abort();
  232. }
  233. vQueueDelete(s_te_obj->event_msg_queue);
  234. vQueueDelete(s_te_obj->intr_msg_queue);
  235. xSemaphoreGive(s_te_obj->mutex);
  236. vSemaphoreDelete(s_te_obj->mutex);
  237. free(s_te_obj->global_config);
  238. s_te_obj->global_config = NULL;
  239. free(s_te_obj);
  240. s_te_obj = NULL;
  241. }
  242. esp_err_t touch_element_message_receive(touch_elem_message_t *element_message, uint32_t ticks_to_wait)
  243. {
  244. //TODO: Use the generic data struct to refactor this api
  245. TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE);
  246. TE_CHECK(element_message != NULL, ESP_ERR_INVALID_ARG);
  247. TE_CHECK(s_te_obj->event_msg_queue != NULL, ESP_ERR_INVALID_STATE);
  248. int ret = xQueueReceive(s_te_obj->event_msg_queue, element_message, ticks_to_wait);
  249. return (ret == pdTRUE) ? ESP_OK : ESP_ERR_TIMEOUT;
  250. }
  251. static uint32_t te_read_raw_signal(touch_pad_t channel_num)
  252. {
  253. uint32_t raw_signal = 0;
  254. touch_pad_sleep_channel_t sleep_channel_info;
  255. touch_pad_sleep_channel_get_info(&sleep_channel_info);
  256. if (channel_num != sleep_channel_info.touch_num) {
  257. touch_pad_read_raw_data(channel_num, &raw_signal);
  258. } else {
  259. touch_pad_sleep_channel_read_data(channel_num, &raw_signal);
  260. }
  261. return raw_signal;
  262. }
  263. uint32_t te_read_smooth_signal(touch_pad_t channel_num)
  264. {
  265. uint32_t smooth_signal = 0;
  266. touch_pad_sleep_channel_t sleep_channel_info;
  267. touch_pad_sleep_channel_get_info(&sleep_channel_info);
  268. if (channel_num != sleep_channel_info.touch_num) {
  269. touch_pad_filter_read_smooth(channel_num, &smooth_signal);
  270. } else {
  271. touch_pad_sleep_channel_read_smooth(channel_num, &smooth_signal);
  272. }
  273. return smooth_signal;
  274. }
  275. esp_err_t te_event_give(touch_elem_message_t te_message)
  276. {
  277. //TODO: add queue overwrite here when the queue is full
  278. int ret = xQueueSend(s_te_obj->event_msg_queue, &te_message, 0);
  279. if (ret != pdTRUE) {
  280. ESP_LOGE(TE_TAG, "event queue send failed, event message queue is full");
  281. return ESP_ERR_TIMEOUT;
  282. }
  283. return ESP_OK;
  284. }
  285. /**
  286. * @brief Touch sensor interrupt service routine
  287. *
  288. * This function is touch sensor ISR, all the touch
  289. * sensor channel state will be updated here.
  290. */
  291. static void te_intr_cb(void *arg)
  292. {
  293. TE_UNUSED(arg);
  294. static int scan_done_cnt = 0;
  295. int task_awoken = pdFALSE;
  296. te_intr_msg_t te_intr_msg;
  297. /*< Figure out which touch sensor channel is triggered and the trigger type */
  298. uint32_t intr_mask = touch_pad_read_intr_status_mask();
  299. te_intr_msg.channel_num = touch_pad_get_current_meas_channel();
  300. if (intr_mask == 0x0) { //For dummy interrupt
  301. return;
  302. }
  303. bool need_send_queue = true;
  304. if (intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  305. te_intr_msg.channel_state = TE_STATE_PRESS;
  306. te_intr_msg.intr_type = TE_INTR_PRESS;
  307. } else if (intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  308. te_intr_msg.channel_state = TE_STATE_RELEASE;
  309. te_intr_msg.intr_type = TE_INTR_RELEASE;
  310. } else if (intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
  311. te_intr_msg.channel_state = TE_STATE_IDLE;
  312. te_intr_msg.intr_type = TE_INTR_TIMEOUT;
  313. } else if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) {
  314. te_intr_msg.channel_state = TE_STATE_IDLE;
  315. te_intr_msg.intr_type = TE_INTR_SCAN_DONE;
  316. need_send_queue = false;
  317. /*< Due to a hardware issue, all of the data read operation(read raw, read smooth, read benchmark) */
  318. /*< must be after the second times of measure_done interrupt. */
  319. if (++scan_done_cnt >= 5) {
  320. touch_hal_intr_disable(TOUCH_PAD_INTR_MASK_SCAN_DONE); //TODO: remove hal
  321. scan_done_cnt = 0;
  322. need_send_queue = true;
  323. }
  324. /*< De-noise channel signal must be read at the time between SCAN_DONE and next measurement beginning(sleep)!!! */
  325. touch_pad_denoise_read_data(&s_te_obj->denoise_channel_raw); //Update de-noise signal
  326. } else {
  327. te_intr_msg.intr_type = TE_INTR_MAX; // Unknown Exception
  328. }
  329. if (need_send_queue) {
  330. xQueueSendFromISR(s_te_obj->intr_msg_queue, &te_intr_msg, &task_awoken);
  331. }
  332. if (task_awoken == pdTRUE) {
  333. portYIELD_FROM_ISR();
  334. }
  335. }
  336. /**
  337. * @brief esp-timer callback routine
  338. *
  339. * This function is an esp-timer daemon routine, all the touch sensor
  340. * application(button, slider, etc...) will be processed in here.
  341. *
  342. */
  343. static void te_proc_timer_cb(void *arg)
  344. {
  345. TE_UNUSED(arg);
  346. te_intr_msg_t te_intr_msg;
  347. te_intr_msg.intr_type = TE_INTR_MAX;
  348. BaseType_t ret = xSemaphoreTake(s_te_obj->mutex, 0);
  349. if (ret != pdPASS) {
  350. return;
  351. }
  352. ret = xQueueReceive(s_te_obj->intr_msg_queue, &te_intr_msg, 0);
  353. if (ret == pdPASS) {
  354. if (te_intr_msg.intr_type == TE_INTR_PRESS || te_intr_msg.intr_type == TE_INTR_RELEASE) {
  355. te_object_update_state(te_intr_msg);
  356. } else if (te_intr_msg.intr_type == TE_INTR_SCAN_DONE) {
  357. if (s_te_obj->is_set_threshold != true) {
  358. s_te_obj->is_set_threshold = true;
  359. te_object_set_threshold(); //TODO: add set threshold error processing
  360. ESP_LOGD(TE_DEBUG_TAG, "Set threshold");
  361. }
  362. if (waterproof_check_state()) {
  363. te_waterproof_handle_t waterproof_handle = s_te_obj->waterproof_handle;
  364. if (waterproof_handle->is_shield_level_set != true) {
  365. waterproof_handle->is_shield_level_set = true;
  366. touch_pad_waterproof_t wp_conf;
  367. wp_conf.shield_driver = waterproof_get_shield_level(waterproof_handle->shield_channel);
  368. wp_conf.guard_ring_pad = (waterproof_guard_check_state() ? waterproof_handle->guard_device->channel : TOUCH_WATERPROOF_GUARD_NOUSE);
  369. touch_pad_waterproof_set_config(&wp_conf);
  370. touch_pad_waterproof_enable();
  371. ESP_LOGD(TE_DEBUG_TAG, "Set waterproof shield level");
  372. }
  373. }
  374. ESP_LOGD(TE_DEBUG_TAG, "read denoise channel %d", s_te_obj->denoise_channel_raw);
  375. } else if (te_intr_msg.intr_type == TE_INTR_TIMEOUT) { //Timeout processing
  376. touch_pad_timeout_resume();
  377. }
  378. }
  379. te_object_process_state();
  380. xSemaphoreGive(s_te_obj->mutex);
  381. }
  382. void te_object_method_register(te_object_methods_t *object_methods, te_class_type_t object_type)
  383. {
  384. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  385. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, handle) = object_methods->handle;
  386. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, check_channel) = object_methods->check_channel;
  387. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, set_threshold) = object_methods->set_threshold;
  388. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, process_state) = object_methods->process_state;
  389. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, update_state) = object_methods->update_state;
  390. xSemaphoreGive(s_te_obj->mutex);
  391. }
  392. void te_object_method_unregister(te_class_type_t object_type)
  393. {
  394. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  395. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, handle) = NULL;
  396. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, check_channel) = NULL;
  397. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, set_threshold) = NULL;
  398. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, process_state) = NULL;
  399. TE_CLASS_ITEM(s_te_obj->object_methods, object_type, update_state) = NULL;
  400. xSemaphoreGive(s_te_obj->mutex);
  401. }
  402. /**
  403. * @brief Touch Sense channel check
  404. *
  405. * This function will check the input channel whether is
  406. * associated with the Touch Sense Object
  407. *
  408. * @return
  409. * - true: Channel has been initialized, pls adjust the input channel
  410. * - false: Channel has not been initialized, pass
  411. */
  412. bool te_object_check_channel(const touch_pad_t *channel_array, uint8_t channel_sum)
  413. {
  414. touch_pad_t current_channel;
  415. for (int idx = 0; idx < channel_sum; idx++) {
  416. current_channel = channel_array[idx];
  417. if (waterproof_channel_check(current_channel)) {
  418. goto INITIALIZED;
  419. }
  420. if (TE_CLASS_FOREACH_CHECK_CHANNEL(s_te_obj->object_methods, TE_CLS_TYPE_BUTTON, TE_CLS_TYPE_MAX, current_channel)) {
  421. goto INITIALIZED;
  422. }
  423. }
  424. return false;
  425. INITIALIZED:
  426. ESP_LOGE(TE_TAG, "Current channel [%d] has been initialized:(%s)", current_channel, __FUNCTION__ );
  427. return true;
  428. }
  429. static inline esp_err_t te_object_set_threshold(void)
  430. {
  431. if (waterproof_guard_check_state() == true) { //TODO: add to object methods
  432. waterproof_guard_set_threshold();
  433. }
  434. TE_CLASS_FOREACH_SET_THRESHOLD(s_te_obj->object_methods, TE_CLS_TYPE_BUTTON, TE_CLS_TYPE_MAX);
  435. return ESP_OK;
  436. }
  437. static inline void te_object_process_state(void)
  438. {
  439. TE_CLASS_FOREACH_PROCESS_STATE(s_te_obj->object_methods, TE_CLS_TYPE_BUTTON, TE_CLS_TYPE_MAX);
  440. }
  441. static inline void te_object_update_state(te_intr_msg_t te_intr_msg)
  442. {
  443. if (waterproof_guard_check_state()) {
  444. waterproof_guard_update_state(te_intr_msg.channel_num, te_intr_msg.channel_state);
  445. }
  446. TE_CLASS_FOREACH_UPDATE_STATE(s_te_obj->object_methods, TE_CLS_TYPE_BUTTON, TE_CLS_TYPE_MAX,
  447. te_intr_msg.channel_num, te_intr_msg.channel_state);
  448. }
  449. uint8_t te_get_timer_period(void)
  450. {
  451. return (TE_PROCESSING_PERIOD(s_te_obj));
  452. }
  453. esp_err_t te_dev_init(te_dev_t **device, uint8_t device_num, te_dev_type_t type, const touch_pad_t *channel, const float *sens, float divider)
  454. {
  455. for (int idx = 0; idx < device_num; idx++) {
  456. device[idx]->channel = channel[idx];
  457. device[idx]->sens = sens[idx] * divider;
  458. device[idx]->type = type;
  459. device[idx]->state = TE_STATE_IDLE;
  460. esp_err_t ret = touch_pad_config(device[idx]->channel);
  461. TE_CHECK(ret == ESP_OK, ret);
  462. }
  463. return ESP_OK;
  464. }
  465. void te_dev_deinit(te_dev_t **device, uint8_t device_num)
  466. {
  467. for (int idx = 0; idx < device_num; idx++) {
  468. touch_pad_clear_channel_mask((1UL << device[idx]->channel));
  469. }
  470. }
  471. esp_err_t te_dev_set_threshold(te_dev_t *device)
  472. {
  473. uint32_t smo_val = te_read_smooth_signal(device->channel);
  474. esp_err_t ret = touch_pad_set_thresh(device->channel, device->sens * smo_val);
  475. ESP_LOGD(TE_DEBUG_TAG, "channel: %d, smo_val: %d", device->channel, smo_val);
  476. return ret;
  477. }
  478. /**
  479. * This function returns the s_te_obj whether is initialized
  480. *
  481. * @return
  482. * - true: initialized
  483. * - false: not initialized
  484. */
  485. bool te_system_check_state(void)
  486. {
  487. return (s_te_obj != NULL);
  488. }
  489. static inline float te_get_internal_equ_cap(touch_pad_denoise_cap_t denoise_level)
  490. {
  491. return denoise_channel_equ_cap[denoise_level];
  492. }
  493. /**
  494. * @brief Get channel equivalent capacitance
  495. *
  496. * This function calculates the equivalent capacitance of input channel by
  497. * using the Touch channel 0 equivalent capacitance. The formula is:
  498. *
  499. * Raw_N / Raw_0 = Cap_N / Cap_0
  500. *
  501. * Note that Raw_N and Raw_0 are the raw data of touch channel N and touch channel 0 respectively,
  502. * Cap_N and Cap_0 are the equivalent capacitance of touch channel N and touch channel 0.
  503. *
  504. * @param[in] channel_num Input touch sensor channel
  505. *
  506. * @note The unit is pF
  507. *
  508. * @return Specified channel equivalent capacitance.
  509. */
  510. static float te_channel_get_equ_cap(touch_pad_t channel_num)
  511. {
  512. //Fixme: add a mutex in here and prevent the system call this function
  513. TE_CHECK(channel_num > TOUCH_PAD_NUM0 && channel_num < TOUCH_PAD_MAX, 0);
  514. uint32_t tn_raw, t0_raw;
  515. float tn_ref_cap, t0_ref_cap;
  516. touch_pad_denoise_t denoise_channel_conf;
  517. touch_pad_denoise_get_config(&denoise_channel_conf);
  518. tn_raw = te_read_raw_signal(channel_num);
  519. t0_raw = s_te_obj->denoise_channel_raw;
  520. t0_ref_cap = te_get_internal_equ_cap(denoise_channel_conf.cap_level);
  521. if (t0_raw == 0) {
  522. return 0;
  523. }
  524. tn_ref_cap = (float)tn_raw / t0_raw * t0_ref_cap;
  525. return tn_ref_cap;
  526. }
  527. /**
  528. * @brief Touch sensor driver default init [ESP32S2 only]
  529. *
  530. * 1. Channel measure time: Raw_value / RTC_FAST_CLK ==> Raw_value / 8000 000
  531. * 2. Channel sleep time: TOUCH_PAD_SLEEP_CYCLE_DEFAULT / RTC_SLOW_CLK ==> 0xf / 90 000(default) = 0.16ms
  532. * 3. Channel charge voltage threshold(upper/lower): 2.7V upper voltage, 0.5V lower voltage, 0.5V attenuation voltage
  533. * 4. IDLE channel processing: Connecting to GND
  534. * 5. Interrupt type: ACTIVE, INACTIVE, TIMEOUT
  535. *
  536. * @note A touch sensor channel will spend the time = measure time + sleep time, RTC_FAST_CLK is 8M
  537. *
  538. */
  539. static esp_err_t te_hw_init(const touch_elem_hw_config_t *hardware_init)
  540. {
  541. esp_err_t ret;
  542. ret = touch_pad_init();
  543. TE_CHECK(ret == ESP_OK, ret);
  544. ret = touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
  545. TE_CHECK(ret == ESP_OK, ret);
  546. ret = touch_pad_set_measurement_interval(hardware_init->sleep_cycle);
  547. TE_CHECK(ret == ESP_OK, ret);
  548. ret = touch_pad_set_charge_discharge_times(hardware_init->sample_count);
  549. TE_CHECK(ret == ESP_OK, ret);
  550. ret = touch_pad_set_voltage(hardware_init->upper_voltage, hardware_init->lower_voltage,
  551. hardware_init->voltage_attenuation);
  552. TE_CHECK(ret == ESP_OK, ret);
  553. ret = touch_pad_set_idle_channel_connect(hardware_init->suspend_channel_polarity);
  554. TE_CHECK(ret == ESP_OK, ret);
  555. ret = touch_pad_isr_register(te_intr_cb, NULL,
  556. TOUCH_PAD_INTR_MASK_ACTIVE | TOUCH_PAD_INTR_MASK_INACTIVE |
  557. TOUCH_PAD_INTR_MASK_TIMEOUT | TOUCH_PAD_INTR_MASK_SCAN_DONE);
  558. TE_CHECK(ret == ESP_OK, ret);
  559. ret = touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE |
  560. TOUCH_PAD_INTR_MASK_INACTIVE | TOUCH_PAD_INTR_MASK_TIMEOUT);
  561. TE_CHECK(ret == ESP_OK, ret);
  562. /*< Internal de-noise configuration */
  563. touch_pad_denoise_t denoise_config;
  564. denoise_config.grade = hardware_init->denoise_level;
  565. denoise_config.cap_level = hardware_init->denoise_equivalent_cap;
  566. ret = touch_pad_denoise_set_config(&denoise_config);
  567. TE_CHECK(ret == ESP_OK, ret);
  568. ret = touch_pad_denoise_enable();
  569. TE_CHECK(ret == ESP_OK, ret);
  570. /*< benchmark filter configuration */
  571. touch_filter_config_t filter_config;
  572. filter_config.smh_lvl = hardware_init->smooth_filter_mode;
  573. filter_config.mode = hardware_init->benchmark_filter_mode;
  574. filter_config.debounce_cnt = hardware_init->benchmark_debounce_count;
  575. filter_config.noise_thr = hardware_init->benchmark_calibration_threshold;
  576. filter_config.jitter_step = hardware_init->benchmark_jitter_step;
  577. ret = touch_pad_filter_set_config(&filter_config);
  578. TE_CHECK(ret == ESP_OK, ret);
  579. ret = touch_pad_filter_enable();
  580. TE_CHECK(ret == ESP_OK, ret);
  581. memcpy(&s_te_obj->global_config->hardware, hardware_init, sizeof(touch_elem_hw_config_t));
  582. return ESP_OK;
  583. }
  584. static esp_err_t te_sw_init(const touch_elem_sw_config_t *software_init)
  585. {
  586. TE_CHECK(software_init->processing_period > 1, ESP_ERR_INVALID_ARG);
  587. TE_CHECK(software_init->waterproof_threshold_divider > 0, ESP_ERR_INVALID_ARG);
  588. TE_CHECK(software_init->intr_message_size >= (TOUCH_PAD_MAX - 1), ESP_ERR_INVALID_ARG);
  589. TE_CHECK(software_init->event_message_size > 0, ESP_ERR_INVALID_ARG);
  590. esp_err_t ret = ESP_ERR_NO_MEM;
  591. s_te_obj->intr_msg_queue = xQueueCreate(software_init->intr_message_size, sizeof(te_intr_msg_t));
  592. s_te_obj->event_msg_queue = xQueueCreate(software_init->event_message_size, sizeof(touch_elem_message_t));
  593. TE_CHECK_GOTO(s_te_obj->event_msg_queue != NULL && s_te_obj->intr_msg_queue != NULL, cleanup);
  594. const esp_timer_create_args_t te_proc_timer_args = {
  595. .name = "te_proc_timer_cb",
  596. .arg = NULL,
  597. .callback = &te_proc_timer_cb
  598. };
  599. ret = esp_timer_create(&te_proc_timer_args, &s_te_obj->proc_timer);
  600. TE_CHECK_GOTO(ret == ESP_OK, cleanup);
  601. memcpy(&s_te_obj->global_config->software, software_init, sizeof(touch_elem_sw_config_t));
  602. return ret;
  603. cleanup:
  604. if (s_te_obj->event_msg_queue != NULL) {
  605. vQueueDelete(s_te_obj->event_msg_queue);
  606. }
  607. if (s_te_obj->intr_msg_queue != NULL) {
  608. vQueueDelete(s_te_obj->intr_msg_queue);
  609. }
  610. return ret;
  611. }
  612. //TODO: add waterproof guard-lock hysteresis
  613. esp_err_t touch_element_waterproof_install(const touch_elem_waterproof_config_t *waterproof_config)
  614. {
  615. TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE);
  616. TE_CHECK(waterproof_config != NULL, ESP_ERR_INVALID_ARG);
  617. TE_CHECK(waterproof_config->guard_channel >= TOUCH_PAD_NUM0 &&
  618. waterproof_config->guard_channel < TOUCH_PAD_MAX,
  619. ESP_ERR_INVALID_ARG);
  620. te_waterproof_handle_t waterproof_handle = (te_waterproof_handle_t)calloc(1, sizeof(struct te_waterproof_s));
  621. TE_CHECK(waterproof_handle != NULL, ESP_ERR_NO_MEM);
  622. waterproof_handle->shield_channel = TOUCH_PAD_NUM14;
  623. esp_err_t ret;
  624. if (waterproof_config->guard_channel != TOUCH_WATERPROOF_GUARD_NOUSE) { //Use guard sensor
  625. if (te_object_check_channel(&waterproof_config->guard_channel, 1)) {
  626. ret = ESP_ERR_INVALID_ARG;
  627. goto cleanup;
  628. }
  629. ret = ESP_ERR_NO_MEM;
  630. waterproof_handle->mask_handle = (touch_elem_handle_t *) calloc(TOUCH_PAD_MAX, sizeof(touch_elem_handle_t));
  631. waterproof_handle->guard_device = (te_dev_t *)calloc(1, sizeof(te_dev_t));
  632. TE_CHECK_GOTO(waterproof_handle->mask_handle != NULL && waterproof_handle->guard_device, cleanup);
  633. ret = te_dev_init(&waterproof_handle->guard_device, 1, TOUCH_ELEM_TYPE_BUTTON,
  634. &waterproof_config->guard_channel, &waterproof_config->guard_sensitivity,
  635. TE_WATERPROOF_DIVIDER(s_te_obj));
  636. TE_CHECK_GOTO(ret == ESP_OK, cleanup);
  637. waterproof_handle->guard_device->state = TE_STATE_RELEASE;
  638. for (int idx = 0; idx < TOUCH_PAD_MAX; idx++) {
  639. waterproof_handle->mask_handle[idx] = NULL;
  640. }
  641. } else { //No use waterproof guard sensor
  642. waterproof_handle->guard_device = NULL;
  643. waterproof_handle->mask_handle = NULL;
  644. }
  645. waterproof_handle->is_shield_level_set = 0; //Set a state bit so as to configure the shield level at the run-time
  646. touch_pad_waterproof_t wp_conf;
  647. wp_conf.shield_driver = TOUCH_PAD_SHIELD_DRV_L0; //Set a default shield level
  648. wp_conf.guard_ring_pad = waterproof_config->guard_channel;
  649. ret = touch_pad_waterproof_set_config(&wp_conf);
  650. TE_CHECK_GOTO(ret == ESP_OK, cleanup);
  651. ret = touch_pad_waterproof_enable();
  652. TE_CHECK_GOTO(ret == ESP_OK, cleanup);
  653. s_te_obj->waterproof_handle = waterproof_handle; //Fixme: add mutex
  654. return ESP_OK;
  655. cleanup:
  656. TE_FREE_AND_NULL(waterproof_handle->mask_handle);
  657. TE_FREE_AND_NULL(waterproof_handle->guard_device);
  658. TE_FREE_AND_NULL(waterproof_handle);
  659. return ret;
  660. }
  661. esp_err_t touch_element_waterproof_add(touch_elem_handle_t element_handle)
  662. {
  663. TE_CHECK(s_te_obj->waterproof_handle != NULL, ESP_ERR_INVALID_STATE);
  664. TE_CHECK(s_te_obj->waterproof_handle->guard_device != NULL, ESP_ERR_INVALID_STATE);
  665. TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG);
  666. te_waterproof_handle_t waterproof_handle = s_te_obj->waterproof_handle;
  667. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  668. for (int idx = 0; idx < TOUCH_PAD_MAX; idx++) {
  669. if (waterproof_handle->mask_handle[idx] == NULL) {
  670. waterproof_handle->mask_handle[idx] = element_handle;
  671. break;
  672. }
  673. }
  674. xSemaphoreGive(s_te_obj->mutex);
  675. return ESP_OK;
  676. }
  677. esp_err_t touch_element_waterproof_remove(touch_elem_handle_t element_handle)
  678. {
  679. TE_CHECK(s_te_obj->waterproof_handle != NULL, ESP_ERR_INVALID_STATE);
  680. TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG);
  681. esp_err_t ret = ESP_ERR_NOT_FOUND;
  682. te_waterproof_handle_t waterproof_handle = s_te_obj->waterproof_handle;
  683. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  684. for (int idx = 0; idx < TOUCH_PAD_MAX; idx++) {
  685. if (waterproof_handle->mask_handle[idx] == element_handle) {
  686. waterproof_handle->mask_handle[idx] = NULL;
  687. ret = ESP_OK;
  688. break;
  689. }
  690. }
  691. xSemaphoreGive(s_te_obj->mutex);
  692. return ret;
  693. }
  694. void touch_element_waterproof_uninstall(void)
  695. {
  696. xSemaphoreTake(s_te_obj->mutex, portMAX_DELAY);
  697. touch_pad_waterproof_disable();
  698. free(s_te_obj->waterproof_handle->guard_device);
  699. free(s_te_obj->waterproof_handle->mask_handle);
  700. free(s_te_obj->waterproof_handle);
  701. s_te_obj->waterproof_handle = NULL;
  702. xSemaphoreGive(s_te_obj->mutex);
  703. }
  704. static touch_pad_shield_driver_t waterproof_get_shield_level(touch_pad_t guard_channel_num)
  705. {
  706. touch_pad_shield_driver_t shield_level = TOUCH_PAD_SHIELD_DRV_L7;
  707. float guard_ref_cap = te_channel_get_equ_cap(guard_channel_num);
  708. for (int level = 0; level < TOUCH_PAD_SHIELD_DRV_MAX; level++) {
  709. if (guard_ref_cap <= shield_channel_ref_cap[level]) {
  710. shield_level = (touch_pad_shield_driver_t)level;
  711. break;
  712. }
  713. }
  714. return shield_level;
  715. }
  716. /**
  717. * This function returns the waterproof_handle whether is initialized
  718. *
  719. * @return
  720. * - true: initialized
  721. * - false: not initialized
  722. */
  723. static inline bool waterproof_check_state(void)
  724. {
  725. return (s_te_obj->waterproof_handle != NULL);
  726. }
  727. static inline bool waterproof_shield_check_state(void)
  728. {
  729. return waterproof_check_state(); //Driver does not allow to disable shield sensor after waterproof enabling
  730. }
  731. static inline bool waterproof_guard_check_state(void)
  732. {
  733. if (waterproof_check_state() == false) {
  734. return false;
  735. }
  736. if (s_te_obj->waterproof_handle->guard_device == NULL || s_te_obj->waterproof_handle->mask_handle == NULL) {
  737. return false;
  738. }
  739. return true;
  740. }
  741. static bool waterproof_channel_check(touch_pad_t channel_num)
  742. {
  743. if (waterproof_check_state() == false) {
  744. return false;
  745. }
  746. te_waterproof_handle_t waterproof_handle = s_te_obj->waterproof_handle;
  747. if (waterproof_shield_check_state()) {
  748. if (channel_num == waterproof_handle->shield_channel) {
  749. ESP_LOGE(TE_TAG, "TOUCH_PAD_NUM%d has been used for waterproof shield channel,"
  750. " please change the touch sensor channel or disable waterproof", channel_num);
  751. return true;
  752. }
  753. }
  754. if (waterproof_guard_check_state()) {
  755. if (channel_num == waterproof_handle->guard_device->channel) {
  756. ESP_LOGE(TE_TAG, "TOUCH_PAD_NUM%d has been used for waterproof guard channel,"
  757. " please change the touch sensor channel or disable waterproof", channel_num);
  758. return true;
  759. }
  760. }
  761. return false;
  762. }
  763. static void waterproof_guard_set_threshold(void)
  764. {
  765. if (waterproof_check_state() == false) {
  766. return;
  767. }
  768. if (waterproof_guard_check_state() == false) {
  769. return;
  770. }
  771. te_dev_set_threshold(s_te_obj->waterproof_handle->guard_device);
  772. }
  773. /**
  774. * This function will figure out current handle whether is a masked channel
  775. * while guard channel is triggered.
  776. *
  777. * @param[in] te_handle Touch sensor application handle
  778. * @return
  779. * - true current handle is a masked channel
  780. * - false current handle is not a masked channel
  781. */
  782. bool waterproof_check_mask_handle(touch_elem_handle_t te_handle)
  783. {
  784. if (waterproof_check_state() == false) {
  785. return false;
  786. }
  787. if (waterproof_guard_check_state() == false) {
  788. return false;
  789. }
  790. te_waterproof_handle_t waterproof_handle = s_te_obj->waterproof_handle;
  791. bool ret = false;
  792. if (waterproof_handle->guard_device->state == TE_STATE_PRESS) {
  793. for (int idx = 0; idx < TOUCH_PAD_MAX; idx++) {
  794. if (waterproof_handle->mask_handle[idx] == NULL) {
  795. break;
  796. }
  797. if (waterproof_handle->mask_handle[idx] == te_handle) {
  798. ret = true;
  799. }
  800. }
  801. }
  802. return ret;
  803. }
  804. static void waterproof_guard_update_state(touch_pad_t current_channel, te_state_t current_state)
  805. {
  806. te_dev_t *guard_device = s_te_obj->waterproof_handle->guard_device;
  807. if (current_channel == guard_device->channel) {
  808. guard_device->state = current_state;
  809. }
  810. ESP_LOGD(TE_DEBUG_TAG, "waterproof guard state update %d", guard_device->state);
  811. }