1 /* ULP riscv 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 "esp_sleep.h"
12 #include "soc/rtc_cntl_reg.h"
13 #include "soc/sens_reg.h"
14 #include "soc/rtc_periph.h"
15 #include "hal/rtc_io_ll.h"
16 #include "driver/gpio.h"
17 #include "driver/rtc_io.h"
18 #include "esp32s2/ulp.h"
19 #include "esp32s2/ulp_riscv.h"
20 #include "ulp_main.h"
21 #include "freertos/FreeRTOS.h"
22 #include "freertos/task.h"
23
24 /* We alternate between start conversion and read result every other ULP wakeup,
25 Conversion time is 750 ms for 12 bit resolution
26 */
27 #define WAKEUP_PERIOD_US (750000)
28
29 extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
30 extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
31
32 static void init_ulp_program(void);
33
app_main(void)34 void app_main(void)
35 {
36 esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
37 /* not a wakeup from ULP, load the firmware */
38 if (cause != ESP_SLEEP_WAKEUP_ULP) {
39 printf("Not a ULP-RISC-V wakeup (cause = %d), initializing it! \n", cause);
40 init_ulp_program();
41 }
42
43 /* ULP Risc-V read and detected a temperature above the limit */
44 if (cause == ESP_SLEEP_WAKEUP_ULP) {
45 printf("ULP-RISC-V woke up the main CPU, temperature is above set limit! \n");
46 printf("ULP-RISC-V read temperature is %f\n", ulp_temp_reg_val / 16.0);
47 }
48 /* Go back to sleep, only the ULP Risc-V will run */
49 printf("Entering in deep sleep\n\n");
50
51 /* Small delay to ensure the messages are printed */
52 vTaskDelay(100);
53
54 ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup());
55
56 esp_deep_sleep_start();
57 }
58
init_ulp_program(void)59 static void init_ulp_program(void)
60 {
61 esp_err_t err = ulp_riscv_load_binary(ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start));
62 ESP_ERROR_CHECK(err);
63
64 /* The first argument is the period index, which is not used by the ULP-RISC-V timer
65 * The second argument is the period in microseconds, which gives a wakeup time period of: 750ms
66 */
67 ulp_set_wakeup_period(0, WAKEUP_PERIOD_US);
68
69 /* Start the program */
70 err = ulp_riscv_run();
71 ESP_ERROR_CHECK(err);
72 }
73