deep_sleep_example_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Deep sleep wake up 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 <string.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "esp_sleep.h"
  15. #include "esp_log.h"
  16. #include "esp32/ulp.h"
  17. #include "driver/touch_pad.h"
  18. #include "driver/adc.h"
  19. #include "driver/rtc_io.h"
  20. #include "soc/rtc_cntl_reg.h"
  21. #include "soc/sens_reg.h"
  22. #include "soc/rtc.h"
  23. static RTC_DATA_ATTR struct timeval sleep_enter_time;
  24. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  25. /*
  26. * Offset (in 32-bit words) in RTC Slow memory where the data is placed
  27. * by the ULP coprocessor. It can be chosen to be any value greater or equal
  28. * to ULP program size, and less than the CONFIG_ULP_COPROC_RESERVE_MEM/4 - 6,
  29. * where 6 is the number of words used by the ULP coprocessor.
  30. */
  31. #define ULP_DATA_OFFSET 36
  32. _Static_assert(ULP_DATA_OFFSET < CONFIG_ULP_COPROC_RESERVE_MEM/4 - 6,
  33. "ULP_DATA_OFFSET is set too high, or CONFIG_ULP_COPROC_RESERVE_MEM is not sufficient");
  34. /**
  35. * @brief Start ULP temperature monitoring program
  36. *
  37. * This function loads a program into the RTC Slow memory and starts up the ULP.
  38. * The program monitors on-chip temperature sensor and wakes up the SoC when
  39. * the temperature goes lower or higher than certain thresholds.
  40. */
  41. static void start_ulp_temperature_monitoring();
  42. /**
  43. * @brief Utility function which reads data written by ULP program
  44. *
  45. * @param offset offset from ULP_DATA_OFFSET in RTC Slow memory, in words
  46. * @return lower 16-bit part of the word writable by the ULP
  47. */
  48. static inline uint16_t ulp_data_read(size_t offset)
  49. {
  50. return RTC_SLOW_MEM[ULP_DATA_OFFSET + offset] & 0xffff;
  51. }
  52. /**
  53. * @brief Utility function which writes data to be ready by ULP program
  54. *
  55. * @param offset offset from ULP_DATA_OFFSET in RTC Slow memory, in words
  56. * @param value lower 16-bit part of the word to be stored
  57. */
  58. static inline void ulp_data_write(size_t offset, uint16_t value)
  59. {
  60. RTC_SLOW_MEM[ULP_DATA_OFFSET + offset] = value;
  61. }
  62. #endif // CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  63. #ifdef CONFIG_ENABLE_TOUCH_WAKEUP
  64. static void calibrate_touch_pad(touch_pad_t pad);
  65. #endif
  66. void app_main()
  67. {
  68. struct timeval now;
  69. gettimeofday(&now, NULL);
  70. int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
  71. switch (esp_sleep_get_wakeup_cause()) {
  72. case ESP_SLEEP_WAKEUP_EXT1: {
  73. uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
  74. if (wakeup_pin_mask != 0) {
  75. int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
  76. printf("Wake up from GPIO %d\n", pin);
  77. } else {
  78. printf("Wake up from GPIO\n");
  79. }
  80. break;
  81. }
  82. case ESP_SLEEP_WAKEUP_TIMER: {
  83. printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
  84. break;
  85. }
  86. #ifdef CONFIG_ENABLE_TOUCH_WAKEUP
  87. case ESP_SLEEP_WAKEUP_TOUCHPAD: {
  88. printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
  89. break;
  90. }
  91. #endif // CONFIG_ENABLE_TOUCH_WAKEUP
  92. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  93. case ESP_SLEEP_WAKEUP_ULP: {
  94. printf("Wake up from ULP\n");
  95. int16_t diff_high = (int16_t) ulp_data_read(3);
  96. int16_t diff_low = (int16_t) ulp_data_read(4);
  97. if (diff_high < 0) {
  98. printf("High temperature alarm was triggered\n");
  99. } else if (diff_low < 0) {
  100. printf("Low temperature alarm was triggered\n");
  101. } else {
  102. assert(false && "temperature has stayed within limits, but got ULP wakeup\n");
  103. }
  104. break;
  105. }
  106. #endif // CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  107. case ESP_SLEEP_WAKEUP_UNDEFINED:
  108. default:
  109. printf("Not a deep sleep reset\n");
  110. }
  111. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  112. if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED) {
  113. printf("ULP did %d temperature measurements in %d ms\n", ulp_data_read(1), sleep_time_ms);
  114. printf("Initial T=%d, latest T=%d\n", ulp_data_read(0), ulp_data_read(2));
  115. }
  116. #endif // CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  117. vTaskDelay(1000 / portTICK_PERIOD_MS);
  118. const int wakeup_time_sec = 20;
  119. printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
  120. esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
  121. const int ext_wakeup_pin_1 = 25;
  122. const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
  123. const int ext_wakeup_pin_2 = 26;
  124. const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2;
  125. printf("Enabling EXT1 wakeup on pins GPIO%d, GPIO%d\n", ext_wakeup_pin_1, ext_wakeup_pin_2);
  126. esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
  127. #ifdef CONFIG_ENABLE_TOUCH_WAKEUP
  128. touch_pad_init();
  129. calibrate_touch_pad(TOUCH_PAD_NUM8);
  130. calibrate_touch_pad(TOUCH_PAD_NUM9);
  131. printf("Enabling touch pad wakeup\n");
  132. esp_sleep_enable_touchpad_wakeup();
  133. #endif // CONFIG_ENABLE_TOUCH_WAKEUP
  134. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  135. printf("Enabling ULP wakeup\n");
  136. esp_sleep_enable_ulp_wakeup();
  137. #endif
  138. // Isolate GPIO12 pin from external circuits. This is needed for modules
  139. // which have an external pull-up resistor on GPIO12 (such as ESP32-WROVER)
  140. // to minimize current consumption.
  141. rtc_gpio_isolate(GPIO_NUM_12);
  142. printf("Entering deep sleep\n");
  143. gettimeofday(&sleep_enter_time, NULL);
  144. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  145. start_ulp_temperature_monitoring();
  146. #endif
  147. esp_deep_sleep_start();
  148. }
  149. #ifdef CONFIG_ENABLE_TOUCH_WAKEUP
  150. static void calibrate_touch_pad(touch_pad_t pad)
  151. {
  152. touch_pad_config(pad, 1000);
  153. int avg = 0;
  154. const size_t calibration_count = 128;
  155. for (int i = 0; i < calibration_count; ++i) {
  156. uint16_t val;
  157. touch_pad_read(pad, &val);
  158. avg += val;
  159. }
  160. avg /= calibration_count;
  161. const int min_reading = 300;
  162. if (avg < min_reading) {
  163. printf("Touch pad #%d average reading is too low: %d (expecting at least %d). "
  164. "Not using for deep sleep wakeup.\n", pad, avg, min_reading);
  165. touch_pad_config(pad, 0);
  166. } else {
  167. int threshold = avg - 100;
  168. printf("Touch pad #%d average: %d, wakeup threshold set to %d.\n", pad, avg, threshold);
  169. touch_pad_config(pad, threshold);
  170. }
  171. }
  172. #endif // CONFIG_ENABLE_TOUCH_WAKEUP
  173. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  174. static void start_ulp_temperature_monitoring()
  175. {
  176. /*
  177. * This ULP program monitors the on-chip temperature sensor and wakes the chip up when
  178. * the temperature goes outside of certain window.
  179. * When the program runs for the first time, it saves the temperature measurement,
  180. * it is considered initial temperature (T0).
  181. *
  182. * On each subsequent run, temperature measured and compared to T0.
  183. * If the measured value is higher than T0 + max_temp_diff or lower than T0 - max_temp_diff,
  184. * the chip is woken up from deep sleep.
  185. */
  186. /* Temperature difference threshold which causes wakeup
  187. * With settings here (TSENS_CLK_DIV=2, 8000 cycles),
  188. * TSENS measurement is done in units of 0.73 degrees Celsius.
  189. * Therefore, max_temp_diff below is equivalent to ~2.2 degrees Celsius.
  190. */
  191. const int16_t max_temp_diff = 3;
  192. // Number of measurements ULP should do per second
  193. const uint32_t measurements_per_sec = 5;
  194. // Allow TSENS to be controlled by the ULP
  195. SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 2, SENS_TSENS_CLK_DIV_S);
  196. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
  197. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  198. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  199. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
  200. // Clear the part of RTC_SLOW_MEM reserved for the ULP. Makes debugging easier.
  201. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  202. // The first word of memory (at data offset) is used to store the initial temperature (T0)
  203. // Zero it out here, then ULP will update it on the first run.
  204. ulp_data_write(0, 0);
  205. // The second word is used to store measurement count, zero it out as well.
  206. ulp_data_write(1, 0);
  207. const ulp_insn_t program[] = {
  208. // load data offset into R2
  209. I_MOVI(R2, ULP_DATA_OFFSET),
  210. // load/increment/store measurement counter using R1
  211. I_LD(R1, R2, 1),
  212. I_ADDI(R1, R1, 1),
  213. I_ST(R1, R2, 1),
  214. // enable temperature sensor
  215. I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 3),
  216. // do temperature measurement and store result in R3
  217. I_TSENS(R3, 8000),
  218. // disable temperature sensor
  219. I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 0),
  220. // Save current measurement at offset+2
  221. I_ST(R3, R2, 2),
  222. // load initial value into R0
  223. I_LD(R0, R2, 0),
  224. // if threshold value >=1 (i.e. initialized), goto 1
  225. M_BGE(1, 1),
  226. // otherwise, save the current value as initial (T0)
  227. I_MOVR(R0, R3),
  228. I_ST(R0, R2, 0),
  229. M_LABEL(1),
  230. // check if the temperature is greater or equal (T0 + max_temp_diff)
  231. // uses R1 as scratch register, difference is saved at offset + 3
  232. I_ADDI(R1, R0, max_temp_diff - 1),
  233. I_SUBR(R1, R1, R3),
  234. I_ST(R1, R2, 3),
  235. M_BXF(2),
  236. // check if the temperature is less or equal (T0 - max_temp_diff)
  237. // uses R1 as scratch register, difference is saved at offset + 4
  238. I_SUBI(R1, R0, max_temp_diff - 1),
  239. I_SUBR(R1, R3, R1),
  240. I_ST(R1, R2, 4),
  241. M_BXF(2),
  242. // temperature is within (T0 - max_temp_diff; T0 + max_temp_diff)
  243. // stop ULP until the program timer starts it again
  244. I_HALT(),
  245. M_LABEL(2),
  246. // temperature is out of bounds
  247. // disable ULP program timer
  248. I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0),
  249. // initiate wakeup of the SoC
  250. I_WAKE(),
  251. // stop the ULP program
  252. I_HALT()
  253. };
  254. // Load ULP program into RTC_SLOW_MEM, at offset 0
  255. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  256. ESP_ERROR_CHECK( ulp_process_macros_and_load(0, program, &size) );
  257. assert(size < ULP_DATA_OFFSET && "ULP_DATA_OFFSET needs to be greater or equal to the program size");
  258. // Set ULP wakeup period
  259. const uint32_t sleep_cycles = rtc_clk_slow_freq_get_hz() / measurements_per_sec;
  260. REG_WRITE(SENS_ULP_CP_SLEEP_CYC0_REG, sleep_cycles);
  261. // Start ULP
  262. ESP_ERROR_CHECK( ulp_run(0) );
  263. }
  264. #endif // CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP