touch_element.c 35 KB

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