deep_sleep_example_main.c 11 KB

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