touch_wakeup.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <inttypes.h>
  8. #include "esp_sleep.h"
  9. #include "sdkconfig.h"
  10. #include "driver/rtc_io.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
  14. #include "driver/touch_pad.h"
  15. #if CONFIG_IDF_TARGET_ESP32
  16. #define TOUCH_THRESH_NO_USE 0
  17. static void calibrate_touch_pad(touch_pad_t pad)
  18. {
  19. int avg = 0;
  20. const size_t calibration_count = 128;
  21. for (int i = 0; i < calibration_count; ++i) {
  22. uint16_t val;
  23. touch_pad_read(pad, &val);
  24. avg += val;
  25. }
  26. avg /= calibration_count;
  27. const int min_reading = 300;
  28. if (avg < min_reading) {
  29. printf("Touch pad #%d average reading is too low: %d (expecting at least %d). "
  30. "Not using for deep sleep wakeup.\n", pad, avg, min_reading);
  31. touch_pad_config(pad, 0);
  32. } else {
  33. int threshold = avg - 100;
  34. printf("Touch pad #%d average: %d, wakeup threshold set to %d.\n", pad, avg, threshold);
  35. touch_pad_config(pad, threshold);
  36. }
  37. }
  38. #endif
  39. void example_deep_sleep_register_touch_wakeup(void)
  40. {
  41. #if CONFIG_IDF_TARGET_ESP32
  42. // Initialize touch pad peripheral.
  43. // The default fsm mode is software trigger mode.
  44. ESP_ERROR_CHECK(touch_pad_init());
  45. // If use touch pad wake up, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
  46. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
  47. // Set reference voltage for charging/discharging
  48. // In this case, the high reference valtage will be 2.4V - 1V = 1.4V
  49. // The low reference voltage will be 0.5
  50. // The larger the range, the larger the pulse count value.
  51. touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
  52. //init RTC IO and mode for touch pad.
  53. touch_pad_config(TOUCH_PAD_NUM8, TOUCH_THRESH_NO_USE);
  54. touch_pad_config(TOUCH_PAD_NUM9, TOUCH_THRESH_NO_USE);
  55. calibrate_touch_pad(TOUCH_PAD_NUM8);
  56. calibrate_touch_pad(TOUCH_PAD_NUM9);
  57. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  58. /* Initialize touch pad peripheral. */
  59. touch_pad_init();
  60. /* Only support one touch channel in sleep mode. */
  61. touch_pad_config(TOUCH_PAD_NUM9);
  62. /* Denoise setting at TouchSensor 0. */
  63. touch_pad_denoise_t denoise = {
  64. /* The bits to be cancelled are determined according to the noise level. */
  65. .grade = TOUCH_PAD_DENOISE_BIT4,
  66. .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
  67. };
  68. touch_pad_denoise_set_config(&denoise);
  69. touch_pad_denoise_enable();
  70. printf("Denoise function init\n");
  71. /* Filter setting */
  72. touch_filter_config_t filter_info = {
  73. .mode = TOUCH_PAD_FILTER_IIR_16,
  74. .debounce_cnt = 1, // 1 time count.
  75. .noise_thr = 0, // 50%
  76. .jitter_step = 4, // use for jitter mode.
  77. .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
  78. };
  79. touch_pad_filter_set_config(&filter_info);
  80. touch_pad_filter_enable();
  81. printf("touch pad filter init %d\n", TOUCH_PAD_FILTER_IIR_8);
  82. /* Set sleep touch pad. */
  83. touch_pad_sleep_channel_enable(TOUCH_PAD_NUM9, true);
  84. touch_pad_sleep_channel_enable_proximity(TOUCH_PAD_NUM9, false);
  85. /* Reducing the operating frequency can effectively reduce power consumption. */
  86. touch_pad_sleep_channel_set_work_time(1000, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
  87. /* Enable touch sensor clock. Work mode is "timer trigger". */
  88. touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
  89. touch_pad_fsm_start();
  90. vTaskDelay(100 / portTICK_PERIOD_MS);
  91. /* set touchpad wakeup threshold */
  92. uint32_t touch_value, wake_threshold;
  93. touch_pad_sleep_channel_read_smooth(TOUCH_PAD_NUM9, &touch_value);
  94. wake_threshold = touch_value * 0.1; // wakeup when touch sensor crosses 10% of background level
  95. touch_pad_sleep_set_threshold(TOUCH_PAD_NUM9, wake_threshold);
  96. printf("Touch pad #%d average: %"PRIu32", wakeup threshold set to %"PRIu32"\n",
  97. TOUCH_PAD_NUM9, touch_value, (uint32_t)(touch_value * 0.1));
  98. #endif
  99. printf("Enabling touch pad wakeup\n");
  100. ESP_ERROR_CHECK(esp_sleep_enable_touchpad_wakeup());
  101. ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
  102. }
  103. #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP