deep_sleep_example_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. printf("Entering deep sleep\n");
  139. gettimeofday(&sleep_enter_time, NULL);
  140. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  141. start_ulp_temperature_monitoring();
  142. #endif
  143. esp_deep_sleep_start();
  144. }
  145. #ifdef CONFIG_ENABLE_TOUCH_WAKEUP
  146. static void calibrate_touch_pad(touch_pad_t pad)
  147. {
  148. touch_pad_config(pad, 1000);
  149. int avg = 0;
  150. const size_t calibration_count = 128;
  151. for (int i = 0; i < calibration_count; ++i) {
  152. uint16_t val;
  153. touch_pad_read(pad, &val);
  154. avg += val;
  155. }
  156. avg /= calibration_count;
  157. const int min_reading = 300;
  158. if (avg < min_reading) {
  159. printf("Touch pad #%d average reading is too low: %d (expecting at least %d). "
  160. "Not using for deep sleep wakeup.\n", pad, avg, min_reading);
  161. touch_pad_config(pad, 0);
  162. } else {
  163. int threshold = avg - 100;
  164. printf("Touch pad #%d average: %d, wakeup threshold set to %d.\n", pad, avg, threshold);
  165. touch_pad_config(pad, threshold);
  166. }
  167. }
  168. #endif // CONFIG_ENABLE_TOUCH_WAKEUP
  169. #ifdef CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP
  170. static void start_ulp_temperature_monitoring()
  171. {
  172. /*
  173. * This ULP program monitors the on-chip temperature sensor and wakes the chip up when
  174. * the temperature goes outside of certain window.
  175. * When the program runs for the first time, it saves the temperature measurement,
  176. * it is considered initial temperature (T0).
  177. *
  178. * On each subsequent run, temperature measured and compared to T0.
  179. * If the measured value is higher than T0 + max_temp_diff or lower than T0 - max_temp_diff,
  180. * the chip is woken up from deep sleep.
  181. */
  182. /* Temperature difference threshold which causes wakeup
  183. * With settings here (TSENS_CLK_DIV=2, 8000 cycles),
  184. * TSENS measurement is done in units of 0.73 degrees Celsius.
  185. * Therefore, max_temp_diff below is equivalent to ~2.2 degrees Celsius.
  186. */
  187. const int16_t max_temp_diff = 3;
  188. // Number of measurements ULP should do per second
  189. const uint32_t measurements_per_sec = 5;
  190. // Allow TSENS to be controlled by the ULP
  191. SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 2, SENS_TSENS_CLK_DIV_S);
  192. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
  193. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
  194. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
  195. CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
  196. // Clear the part of RTC_SLOW_MEM reserved for the ULP. Makes debugging easier.
  197. memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM);
  198. // The first word of memory (at data offset) is used to store the initial temperature (T0)
  199. // Zero it out here, then ULP will update it on the first run.
  200. ulp_data_write(0, 0);
  201. // The second word is used to store measurement count, zero it out as well.
  202. ulp_data_write(1, 0);
  203. const ulp_insn_t program[] = {
  204. // load data offset into R2
  205. I_MOVI(R2, ULP_DATA_OFFSET),
  206. // load/increment/store measurement counter using R1
  207. I_LD(R1, R2, 1),
  208. I_ADDI(R1, R1, 1),
  209. I_ST(R1, R2, 1),
  210. // enable temperature sensor
  211. I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 3),
  212. // do temperature measurement and store result in R3
  213. I_TSENS(R3, 8000),
  214. // disable temperature sensor
  215. I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 0),
  216. // Save current measurement at offset+2
  217. I_ST(R3, R2, 2),
  218. // load initial value into R0
  219. I_LD(R0, R2, 0),
  220. // if threshold value >=1 (i.e. initialized), goto 1
  221. M_BGE(1, 1),
  222. // otherwise, save the current value as initial (T0)
  223. I_MOVR(R0, R3),
  224. I_ST(R0, R2, 0),
  225. M_LABEL(1),
  226. // check if the temperature is greater or equal (T0 + max_temp_diff)
  227. // uses R1 as scratch register, difference is saved at offset + 3
  228. I_ADDI(R1, R0, max_temp_diff - 1),
  229. I_SUBR(R1, R1, R3),
  230. I_ST(R1, R2, 3),
  231. M_BXF(2),
  232. // check if the temperature is less or equal (T0 - max_temp_diff)
  233. // uses R1 as scratch register, difference is saved at offset + 4
  234. I_SUBI(R1, R0, max_temp_diff - 1),
  235. I_SUBR(R1, R3, R1),
  236. I_ST(R1, R2, 4),
  237. M_BXF(2),
  238. // temperature is within (T0 - max_temp_diff; T0 + max_temp_diff)
  239. // stop ULP until the program timer starts it again
  240. I_HALT(),
  241. M_LABEL(2),
  242. // temperature is out of bounds
  243. // disable ULP program timer
  244. I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0),
  245. // initiate wakeup of the SoC
  246. I_WAKE(),
  247. // stop the ULP program
  248. I_HALT()
  249. };
  250. // Load ULP program into RTC_SLOW_MEM, at offset 0
  251. size_t size = sizeof(program)/sizeof(ulp_insn_t);
  252. ESP_ERROR_CHECK( ulp_process_macros_and_load(0, program, &size) );
  253. assert(size < ULP_DATA_OFFSET && "ULP_DATA_OFFSET needs to be greater or equal to the program size");
  254. // Set ULP wakeup period
  255. const uint32_t sleep_cycles = rtc_clk_slow_freq_get_hz() / measurements_per_sec;
  256. REG_WRITE(SENS_ULP_CP_SLEEP_CYC0_REG, sleep_cycles);
  257. // Start ULP
  258. ESP_ERROR_CHECK( ulp_run(0) );
  259. }
  260. #endif // CONFIG_ENABLE_ULP_TEMPERATURE_WAKEUP