1 /* ULP riscv DS18B20 1wire temperature sensor 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 "driver/gpio.h"
16 #include "driver/rtc_io.h"
17 #include "esp32s2/ulp.h"
18 #include "esp32s2/ulp_riscv.h"
19 #include "ulp_main.h"
20 #include "freertos/FreeRTOS.h"
21 #include "freertos/task.h"
22
23 extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
24 extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
25
26 static void init_ulp_program(void);
27
app_main(void)28 void app_main(void)
29 {
30 /* Initialize selected GPIO as RTC IO, enable input, disable pullup and pulldown */
31 rtc_gpio_init(GPIO_NUM_0);
32 rtc_gpio_set_direction(GPIO_NUM_0, RTC_GPIO_MODE_INPUT_ONLY);
33 rtc_gpio_pulldown_dis(GPIO_NUM_0);
34 rtc_gpio_pullup_dis(GPIO_NUM_0);
35 rtc_gpio_hold_en(GPIO_NUM_0);
36
37 esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
38 /* not a wakeup from ULP, load the firmware */
39 if (cause != ESP_SLEEP_WAKEUP_ULP) {
40 printf("Not a ULP-RISC-V wakeup, initializing it! \n");
41 init_ulp_program();
42 }
43
44 /* ULP Risc-V read and detected a change in GPIO_0, prints */
45 if (cause == ESP_SLEEP_WAKEUP_ULP) {
46 printf("ULP-RISC-V woke up the main CPU! \n");
47 printf("ULP-RISC-V read changes in GPIO_0 current is: %s \n",
48 (bool)(ulp_gpio_level_previous == 0) ? "Low" : "High" );
49
50 }
51
52 /* Go back to sleep, only the ULP Risc-V will run */
53 printf("Entering in deep sleep\n\n");
54
55 /* Small delay to ensure the messages are printed */
56 vTaskDelay(100);
57
58 ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup());
59 esp_deep_sleep_start();
60 }
61
init_ulp_program(void)62 static void init_ulp_program(void)
63 {
64 esp_err_t err = ulp_riscv_load_binary(ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start));
65 ESP_ERROR_CHECK(err);
66
67 /* The first argument is the period index, which is not used by the ULP-RISC-V timer
68 * The second argument is the period in microseconds, which gives a wakeup time period of: 20ms
69 */
70 ulp_set_wakeup_period(0, 20000);
71
72 /* Start the program */
73 err = ulp_riscv_run();
74 ESP_ERROR_CHECK(err);
75 }
76