Trouble Waking from Ligh Sleep using ULP
Posted: Fri Jan 10, 2020 8:25 am
I'm trying to figure out how to wake the ESP32 form a light sleep using the ULP. To this end I've put together some simple code that counts up in 2s. This works as expected for deep sleep (uncomment line 57, and comment line 56 of the c code), but not for light sleep. In light sleep, the program just hangs. Does anyone have any idea on how I could get this to work?
- /* idf.py menuconfig weas used to adjust
- * Component config -> ESP32 specific -> select 'Enable Ultra Low Power Coprocessor'
- * to 4096 top get this to work
- */
- /* ULP assembly files are passed through C preprocessor first, so include directives
- and C macros may be used in these files
- */
- #include "soc/rtc_cntl_reg.h"
- #include "soc/rtc_io_reg.h"
- #include "soc/soc_ulp.h"
- /* Define variables, which go into .bss section (zero-initialized data) */
- .bss
- .global testval
- testval: .long 0
- /* Code goes into .text section */
- .text
- .global entry
- entry:
- move r0, testval
- ld r1, r0, 0
- add r1, r1, 2
- st r1, r0, 0
- STAGE_RST
- wait100ms:
- wait 8000
- STAGE_INC 1
- jumps wake_up, 100, GE
- jump wait100ms
- // wake up at the end
- wake_up:
- // Check if the system can be woken up
- READ_RTC_FIELD(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP)
- and r0, r0, 1
- jump wake_up, eq
- // Wake up the SoC, end program
- wake
- REG_WR 0x006, 24, 24, 0 // Stop ULP timer (clear RTC_CNTL_ULP_CP_SLP_TIMER_EN)
- halt
- #include <stdio.h>
- #include "esp_sleep.h"
- #include "soc/rtc_cntl_reg.h"
- #include "soc/rtc_io_reg.h"
- #include "soc/sens_reg.h"
- #include "soc/rtc_periph.h"
- #include "soc/soc.h"
- #include "driver/gpio.h"
- #include "driver/rtc_io.h"
- //#include "sdkconfig.h"
- #include "esp32/ulp.h"
- #include "ulp_main.h"
- extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start");
- extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end");
- static void init_ulp_program();
- static void init_ulp_program()
- {
- esp_err_t err = ulp_load_binary(0, ulp_main_bin_start,
- (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
- ESP_ERROR_CHECK(err);
- // Init Values
- ulp_testval = 0;
- //esp_deep_sleep_disable_rom_logging(); // suppress boot messages
- /* Set ULP wake up period to 20ms */
- ulp_set_wakeup_period(0, 20000);
- ESP_ERROR_CHECK( ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t)));
- }
- void app_main()
- {
- //testNoSleep();
- printf("start\n");
- esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
- if (cause != ESP_SLEEP_WAKEUP_ULP)
- {
- printf("Not ULP wakeup, initializing ULP\n");
- init_ulp_program();
- }
- else
- {
- printf("ULP Wakeup\n");
- ESP_ERROR_CHECK( ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t)));
- printf("%u\n", ulp_testval& 0x0000FFFF);
- printf("\n");
- }
- ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
- esp_light_sleep_start();
- //esp_deep_sleep_start();
- }