1 /* Deep sleep wake up example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <sys/time.h>
15 #include "sdkconfig.h"
16 #include "soc/soc_caps.h"
17 #include "freertos/FreeRTOS.h"
18 #include "freertos/task.h"
19 #include "esp_sleep.h"
20 #include "esp_log.h"
21 #include "driver/adc.h"
22 #include "driver/rtc_io.h"
23 #include "soc/rtc.h"
24 
25 #if CONFIG_IDF_TARGET_ESP32
26 #include "esp32/ulp.h"
27 #endif
28 
29 #if SOC_TOUCH_SENSOR_NUM > 0
30 #include "soc/sens_periph.h"
31 #include "driver/touch_pad.h"
32 #endif
33 
34 #ifdef CONFIG_IDF_TARGET_ESP32C3
35 #define DEFAULT_WAKEUP_PIN      CONFIG_EXAMPLE_GPIO_WAKEUP_PIN
36 #ifdef CONFIG_EXAMPLE_GPIO_WAKEUP_HIGH_LEVEL
37 #define DEFAULT_WAKEUP_LEVEL    ESP_GPIO_WAKEUP_GPIO_HIGH
38 #else
39 #define DEFAULT_WAKEUP_LEVEL    ESP_GPIO_WAKEUP_GPIO_LOW
40 #endif
41 #endif
42 
43 static RTC_DATA_ATTR struct timeval sleep_enter_time;
44 
45 #ifdef CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
46 #if CONFIG_IDF_TARGET_ESP32
47 
48 /*
49  * Offset (in 32-bit words) in RTC Slow memory where the data is placed
50  * by the ULP coprocessor. It can be chosen to be any value greater or equal
51  * to ULP program size, and less than the CONFIG_ESP32_ULP_COPROC_RESERVE_MEM/4 - 6,
52  * where 6 is the number of words used by the ULP coprocessor.
53  */
54 #define ULP_DATA_OFFSET     36
55 
56 _Static_assert(ULP_DATA_OFFSET < CONFIG_ESP32_ULP_COPROC_RESERVE_MEM/4 - 6,
57         "ULP_DATA_OFFSET is set too high, or CONFIG_ESP32_ULP_COPROC_RESERVE_MEM is not sufficient");
58 
59 /**
60  * @brief Start ULP temperature monitoring program
61  *
62  * This function loads a program into the RTC Slow memory and starts up the ULP.
63  * The program monitors on-chip temperature sensor and wakes up the SoC when
64  * the temperature goes lower or higher than certain thresholds.
65  */
66 static void start_ulp_temperature_monitoring(void);
67 
68 /**
69  * @brief Utility function which reads data written by ULP program
70  *
71  * @param offset offset from ULP_DATA_OFFSET in RTC Slow memory, in words
72  * @return lower 16-bit part of the word writable by the ULP
73  */
ulp_data_read(size_t offset)74 static inline uint16_t ulp_data_read(size_t offset)
75 {
76     return RTC_SLOW_MEM[ULP_DATA_OFFSET + offset] & 0xffff;
77 }
78 
79 /**
80  * @brief Utility function which writes data to be ready by ULP program
81  *
82  * @param offset offset from ULP_DATA_OFFSET in RTC Slow memory, in words
83  * @param value lower 16-bit part of the word to be stored
84  */
ulp_data_write(size_t offset,uint16_t value)85 static inline void ulp_data_write(size_t offset, uint16_t value)
86 {
87     RTC_SLOW_MEM[ULP_DATA_OFFSET + offset] = value;
88 }
89 #endif // CONFIG_IDF_TARGET_ESP32
90 #endif // CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
91 
92 #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
93 #if CONFIG_IDF_TARGET_ESP32
94 #define TOUCH_THRESH_NO_USE 0
95 static void calibrate_touch_pad(touch_pad_t pad);
96 #endif
97 #endif
98 
app_main(void)99 void app_main(void)
100 {
101     struct timeval now;
102     gettimeofday(&now, NULL);
103     int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
104 
105     switch (esp_sleep_get_wakeup_cause()) {
106 #ifdef CONFIG_EXAMPLE_EXT1_WAKEUP
107         case ESP_SLEEP_WAKEUP_EXT1: {
108             uint64_t wakeup_pin_mask = esp_sleep_get_ext1_wakeup_status();
109             if (wakeup_pin_mask != 0) {
110                 int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
111                 printf("Wake up from GPIO %d\n", pin);
112             } else {
113                 printf("Wake up from GPIO\n");
114             }
115             break;
116         }
117 #endif // CONFIG_EXAMPLE_EXT1_WAKEUP
118 #if SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
119         case ESP_SLEEP_WAKEUP_GPIO: {
120             uint64_t wakeup_pin_mask = esp_sleep_get_gpio_wakeup_status();
121             if (wakeup_pin_mask != 0) {
122                 int pin = __builtin_ffsll(wakeup_pin_mask) - 1;
123                 printf("Wake up from GPIO %d\n", pin);
124             } else {
125                 printf("Wake up from GPIO\n");
126             }
127             break;
128         }
129 #endif //SOC_GPIO_SUPPORT_DEEPSLEEP_WAKEUP
130         case ESP_SLEEP_WAKEUP_TIMER: {
131             printf("Wake up from timer. Time spent in deep sleep: %dms\n", sleep_time_ms);
132             break;
133         }
134 #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
135         case ESP_SLEEP_WAKEUP_TOUCHPAD: {
136             printf("Wake up from touch on pad %d\n", esp_sleep_get_touchpad_wakeup_status());
137             break;
138         }
139 #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
140 #ifdef CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
141 #if CONFIG_IDF_TARGET_ESP32
142         case ESP_SLEEP_WAKEUP_ULP: {
143             printf("Wake up from ULP\n");
144             int16_t diff_high = (int16_t) ulp_data_read(3);
145             int16_t diff_low = (int16_t) ulp_data_read(4);
146             if (diff_high < 0) {
147                 printf("High temperature alarm was triggered\n");
148             } else if (diff_low < 0) {
149                 printf("Low temperature alarm was triggered\n");
150             } else {
151                 assert(false && "temperature has stayed within limits, but got ULP wakeup\n");
152             }
153             break;
154         }
155 #endif // CONFIG_IDF_TARGET_ESP32
156 #endif // CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
157         case ESP_SLEEP_WAKEUP_UNDEFINED:
158         default:
159             printf("Not a deep sleep reset\n");
160     }
161 
162 #ifdef CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
163 #if CONFIG_IDF_TARGET_ESP32
164     if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED) {
165         printf("ULP did %d temperature measurements in %d ms\n", ulp_data_read(1), sleep_time_ms);
166         printf("Initial T=%d, latest T=%d\n", ulp_data_read(0), ulp_data_read(2));
167     }
168 #endif // CONFIG_IDF_TARGET_ESP32
169 #endif // CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
170 
171     vTaskDelay(1000 / portTICK_PERIOD_MS);
172 
173     const int wakeup_time_sec = 20;
174     printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
175     esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);
176 
177 #ifdef CONFIG_EXAMPLE_EXT1_WAKEUP
178     const int ext_wakeup_pin_1 = 2;
179     const uint64_t ext_wakeup_pin_1_mask = 1ULL << ext_wakeup_pin_1;
180     const int ext_wakeup_pin_2 = 4;
181     const uint64_t ext_wakeup_pin_2_mask = 1ULL << ext_wakeup_pin_2;
182 
183     printf("Enabling EXT1 wakeup on pins GPIO%d, GPIO%d\n", ext_wakeup_pin_1, ext_wakeup_pin_2);
184     esp_sleep_enable_ext1_wakeup(ext_wakeup_pin_1_mask | ext_wakeup_pin_2_mask, ESP_EXT1_WAKEUP_ANY_HIGH);
185 #endif // CONFIG_EXAMPLE_EXT1_WAKEUP
186 
187 #ifdef CONFIG_EXAMPLE_GPIO_WAKEUP
188     const gpio_config_t config = {
189         .pin_bit_mask = BIT(DEFAULT_WAKEUP_PIN),
190         .mode = GPIO_MODE_INPUT,
191     };
192     ESP_ERROR_CHECK(gpio_config(&config));
193     ESP_ERROR_CHECK(esp_deep_sleep_enable_gpio_wakeup(BIT(DEFAULT_WAKEUP_PIN), DEFAULT_WAKEUP_LEVEL));
194     printf("Enabling GPIO wakeup on pins GPIO%d\n", DEFAULT_WAKEUP_PIN);
195 #endif
196 
197 #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
198 #if CONFIG_IDF_TARGET_ESP32
199     // Initialize touch pad peripheral.
200     // The default fsm mode is software trigger mode.
201     ESP_ERROR_CHECK(touch_pad_init());
202     // If use touch pad wake up, should set touch sensor FSM mode at 'TOUCH_FSM_MODE_TIMER'.
203     touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
204     // Set reference voltage for charging/discharging
205     // In this case, the high reference valtage will be 2.4V - 1V = 1.4V
206     // The low reference voltage will be 0.5
207     // The larger the range, the larger the pulse count value.
208     touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
209     //init RTC IO and mode for touch pad.
210     touch_pad_config(TOUCH_PAD_NUM8, TOUCH_THRESH_NO_USE);
211     touch_pad_config(TOUCH_PAD_NUM9, TOUCH_THRESH_NO_USE);
212     calibrate_touch_pad(TOUCH_PAD_NUM8);
213     calibrate_touch_pad(TOUCH_PAD_NUM9);
214 #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
215     /* Initialize touch pad peripheral. */
216     touch_pad_init();
217     /* Only support one touch channel in sleep mode. */
218     touch_pad_config(TOUCH_PAD_NUM9);
219     /* Denoise setting at TouchSensor 0. */
220     touch_pad_denoise_t denoise = {
221         /* The bits to be cancelled are determined according to the noise level. */
222         .grade = TOUCH_PAD_DENOISE_BIT4,
223         .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
224     };
225     touch_pad_denoise_set_config(&denoise);
226     touch_pad_denoise_enable();
227     printf("Denoise function init\n");
228     /* Filter setting */
229     touch_filter_config_t filter_info = {
230         .mode = TOUCH_PAD_FILTER_IIR_16,
231         .debounce_cnt = 1,      // 1 time count.
232         .noise_thr = 0,         // 50%
233         .jitter_step = 4,       // use for jitter mode.
234         .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,
235     };
236     touch_pad_filter_set_config(&filter_info);
237     touch_pad_filter_enable();
238     printf("touch pad filter init %d\n", TOUCH_PAD_FILTER_IIR_8);
239     /* Set sleep touch pad. */
240     touch_pad_sleep_channel_enable(TOUCH_PAD_NUM9, true);
241     touch_pad_sleep_channel_enable_proximity(TOUCH_PAD_NUM9, false);
242     /* Reducing the operating frequency can effectively reduce power consumption. */
243     touch_pad_sleep_channel_set_work_time(1000, TOUCH_PAD_MEASURE_CYCLE_DEFAULT);
244     /* Enable touch sensor clock. Work mode is "timer trigger". */
245     touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
246     touch_pad_fsm_start();
247     vTaskDelay(100 / portTICK_RATE_MS);
248 
249     /* set touchpad wakeup threshold */
250     uint32_t touch_value, wake_threshold;
251     touch_pad_sleep_channel_read_smooth(TOUCH_PAD_NUM9, &touch_value);
252     wake_threshold = touch_value * 0.1; // wakeup when touch sensor crosses 10% of background level
253     touch_pad_sleep_set_threshold(TOUCH_PAD_NUM9, wake_threshold);
254     printf("Touch pad #%d average: %d, wakeup threshold set to %d\n",
255         TOUCH_PAD_NUM9, touch_value, (uint32_t)(touch_value * 0.1));
256 #endif
257     printf("Enabling touch pad wakeup\n");
258     esp_sleep_enable_touchpad_wakeup();
259     esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);
260 #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
261 
262 #ifdef CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
263 #if CONFIG_IDF_TARGET_ESP32
264     printf("Enabling ULP wakeup\n");
265     esp_sleep_enable_ulp_wakeup();
266 #endif
267 #endif
268 
269 #if CONFIG_IDF_TARGET_ESP32
270     // Isolate GPIO12 pin from external circuits. This is needed for modules
271     // which have an external pull-up resistor on GPIO12 (such as ESP32-WROVER)
272     // to minimize current consumption.
273     rtc_gpio_isolate(GPIO_NUM_12);
274 #endif
275 
276     printf("Entering deep sleep\n");
277     gettimeofday(&sleep_enter_time, NULL);
278 
279 #ifdef CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
280 #if CONFIG_IDF_TARGET_ESP32
281     start_ulp_temperature_monitoring();
282 #endif
283 #endif
284 
285     esp_deep_sleep_start();
286 }
287 
288 #ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
289 #if CONFIG_IDF_TARGET_ESP32
calibrate_touch_pad(touch_pad_t pad)290 static void calibrate_touch_pad(touch_pad_t pad)
291 {
292     int avg = 0;
293     const size_t calibration_count = 128;
294     for (int i = 0; i < calibration_count; ++i) {
295         uint16_t val;
296         touch_pad_read(pad, &val);
297         avg += val;
298     }
299     avg /= calibration_count;
300     const int min_reading = 300;
301     if (avg < min_reading) {
302         printf("Touch pad #%d average reading is too low: %d (expecting at least %d). "
303                "Not using for deep sleep wakeup.\n", pad, avg, min_reading);
304         touch_pad_config(pad, 0);
305     } else {
306         int threshold = avg - 100;
307         printf("Touch pad #%d average: %d, wakeup threshold set to %d.\n", pad, avg, threshold);
308         touch_pad_config(pad, threshold);
309     }
310 }
311 #endif
312 #endif // CONFIG_EXAMPLE_TOUCH_WAKEUP
313 
314 #ifdef CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
315 #if CONFIG_IDF_TARGET_ESP32
start_ulp_temperature_monitoring(void)316 static void start_ulp_temperature_monitoring(void)
317 {
318     /*
319      * This ULP program monitors the on-chip temperature sensor and wakes the chip up when
320      * the temperature goes outside of certain window.
321      * When the program runs for the first time, it saves the temperature measurement,
322      * it is considered initial temperature (T0).
323      *
324      * On each subsequent run, temperature measured and compared to T0.
325      * If the measured value is higher than T0 + max_temp_diff or lower than T0 - max_temp_diff,
326      * the chip is woken up from deep sleep.
327      */
328 
329     /* Temperature difference threshold which causes wakeup
330      * With settings here (TSENS_CLK_DIV=2, 8000 cycles),
331      * TSENS measurement is done in units of 0.73 degrees Celsius.
332      * Therefore, max_temp_diff below is equivalent to ~2.2 degrees Celsius.
333      */
334     const int16_t max_temp_diff = 3;
335 
336     // Number of measurements ULP should do per second
337     const uint32_t measurements_per_sec = 5;
338 
339     // Allow TSENS to be controlled by the ULP
340     SET_PERI_REG_BITS(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_CLK_DIV, 2, SENS_TSENS_CLK_DIV_S);
341     SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
342     CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP);
343     CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_DUMP_OUT);
344     CLEAR_PERI_REG_MASK(SENS_SAR_TSENS_CTRL_REG, SENS_TSENS_POWER_UP_FORCE);
345 
346     // Clear the part of RTC_SLOW_MEM reserved for the ULP. Makes debugging easier.
347     memset(RTC_SLOW_MEM, 0, CONFIG_ESP32_ULP_COPROC_RESERVE_MEM);
348 
349     // The first word of memory (at data offset) is used to store the initial temperature (T0)
350     // Zero it out here, then ULP will update it on the first run.
351     ulp_data_write(0, 0);
352     // The second word is used to store measurement count, zero it out as well.
353     ulp_data_write(1, 0);
354 
355     const ulp_insn_t program[] = {
356         // load data offset into R2
357         I_MOVI(R2, ULP_DATA_OFFSET),
358         // load/increment/store measurement counter using R1
359         I_LD(R1, R2, 1),
360         I_ADDI(R1, R1, 1),
361         I_ST(R1, R2, 1),
362         // enable temperature sensor
363         I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 3),
364         // do temperature measurement and store result in R3
365         I_TSENS(R3, 8000),
366         // disable temperature sensor
367         I_WR_REG(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR_S, SENS_FORCE_XPD_SAR_S + 1, 0),
368         // Save current measurement at offset+2
369         I_ST(R3, R2, 2),
370         // load initial value into R0
371         I_LD(R0, R2, 0),
372         // if threshold value >=1 (i.e. initialized), goto 1
373         M_BGE(1, 1),
374             // otherwise, save the current value as initial (T0)
375             I_MOVR(R0, R3),
376             I_ST(R0, R2, 0),
377         M_LABEL(1),
378         // check if the temperature is greater or equal (T0 + max_temp_diff)
379         // uses R1 as scratch register, difference is saved at offset + 3
380         I_ADDI(R1, R0, max_temp_diff - 1),
381         I_SUBR(R1, R1, R3),
382         I_ST(R1, R2, 3),
383         M_BXF(2),
384         // check if the temperature is less or equal (T0 - max_temp_diff)
385         // uses R1 as scratch register, difference is saved at offset + 4
386         I_SUBI(R1, R0, max_temp_diff - 1),
387         I_SUBR(R1, R3, R1),
388         I_ST(R1, R2, 4),
389         M_BXF(2),
390             // temperature is within (T0 - max_temp_diff; T0 + max_temp_diff)
391             // stop ULP until the program timer starts it again
392             I_HALT(),
393         M_LABEL(2),
394             // temperature is out of bounds
395             // disable ULP program timer
396             I_WR_REG_BIT(RTC_CNTL_STATE0_REG, RTC_CNTL_ULP_CP_SLP_TIMER_EN_S, 0),
397             // initiate wakeup of the SoC
398             I_WAKE(),
399             // stop the ULP program
400             I_HALT()
401     };
402 
403     // Load ULP program into RTC_SLOW_MEM, at offset 0
404     size_t size = sizeof(program)/sizeof(ulp_insn_t);
405     ESP_ERROR_CHECK( ulp_process_macros_and_load(0, program, &size) );
406     assert(size < ULP_DATA_OFFSET && "ULP_DATA_OFFSET needs to be greater or equal to the program size");
407 
408     // Set ULP wakeup period
409     const uint32_t sleep_cycles = rtc_clk_slow_freq_get_hz() / measurements_per_sec;
410     REG_WRITE(SENS_ULP_CP_SLEEP_CYC0_REG, sleep_cycles);
411 
412     // Start ULP
413     ESP_ERROR_CHECK( ulp_run(0) );
414 }
415 #endif // CONFIG_IDF_TARGET_ESP32
416 #endif // CONFIG_EXAMPLE_ULP_TEMPERATURE_WAKEUP
417