tp_interrupt_main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Touch Pad Interrupt Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_log.h"
  11. #include "driver/touch_pad.h"
  12. #include "soc/rtc_cntl_reg.h"
  13. #include "soc/sens_reg.h"
  14. static const char* TAG = "Touch pad";
  15. static bool s_pad_activated[TOUCH_PAD_MAX];
  16. /*
  17. Read values sensed at all available touch pads.
  18. Use half of read value as the threshold
  19. to trigger interrupt when the pad is touched.
  20. Note: this routine demonstrates a simple way
  21. to configure activation threshold for the touch pads.
  22. Do not touch any pads when this routine
  23. is running (on application start).
  24. */
  25. static void tp_example_set_thresholds(void)
  26. {
  27. uint16_t touch_value;
  28. for (int i=0; i<TOUCH_PAD_MAX; i++) {
  29. ESP_ERROR_CHECK(touch_pad_read(i, &touch_value));
  30. ESP_ERROR_CHECK(touch_pad_config(i, touch_value/2));
  31. }
  32. }
  33. /*
  34. Check if any of touch pads has been activated
  35. by reading a table updated by rtc_intr()
  36. If so, then print it out on a serial monitor.
  37. Clear related entry in the table afterwards
  38. */
  39. static void tp_example_read_task(void *pvParameter)
  40. {
  41. static int show_message;
  42. while (1) {
  43. for (int i=0; i<TOUCH_PAD_MAX; i++) {
  44. if (s_pad_activated[i] == true) {
  45. ESP_LOGI(TAG, "T%d activated!", i);
  46. // Wait a while for the pad being released
  47. vTaskDelay(200 / portTICK_PERIOD_MS);
  48. // Clear information on pad activation
  49. s_pad_activated[i] = false;
  50. // Reset the counter triggering a message
  51. // that application is running
  52. show_message = 1;
  53. }
  54. }
  55. // If no pad is touched, every couple of seconds, show a message
  56. // that application is running
  57. if (show_message++ % 500 == 0) {
  58. ESP_LOGI(TAG, "Waiting for any pad being touched...");
  59. }
  60. vTaskDelay(10 / portTICK_PERIOD_MS);
  61. }
  62. }
  63. /*
  64. Handle an interrupt triggered when a pad is touched.
  65. Recognize what pad has been touched and save it in a table.
  66. */
  67. static void tp_example_rtc_intr(void * arg)
  68. {
  69. uint32_t pad_intr = READ_PERI_REG(SENS_SAR_TOUCH_CTRL2_REG) & 0x3ff;
  70. uint32_t rtc_intr = READ_PERI_REG(RTC_CNTL_INT_ST_REG);
  71. //clear interrupt
  72. WRITE_PERI_REG(RTC_CNTL_INT_CLR_REG, rtc_intr);
  73. SET_PERI_REG_MASK(SENS_SAR_TOUCH_CTRL2_REG, SENS_TOUCH_MEAS_EN_CLR);
  74. if (rtc_intr & RTC_CNTL_TOUCH_INT_ST) {
  75. for (int i = 0; i < TOUCH_PAD_MAX; i++) {
  76. if ((pad_intr >> i) & 0x01) {
  77. s_pad_activated[i] = true;
  78. }
  79. }
  80. }
  81. }
  82. void app_main()
  83. {
  84. // Initialize touch pad peripheral
  85. ESP_LOGI(TAG, "Initializing touch pad");
  86. touch_pad_init();
  87. tp_example_set_thresholds();
  88. touch_pad_isr_handler_register(tp_example_rtc_intr, NULL, 0, NULL);
  89. // Start a task to show what pads have been touched
  90. xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 2048, NULL, 5, NULL);
  91. }