2020
2121EventGroupHandle_t user_event_group ;
2222
23- #if defined(CONFIG_ENABLE_SLP_KEY ) || defined(CONFIG_ENABLE_BLE_IF )
23+ #if defined(CONFIG_ENABLE_SLEEP_KEY ) || defined(CONFIG_ENABLE_BLE_CONTROL_IF )
2424static EventBits_t sleep_wait_bits = 0 ;
2525static EventBits_t reset_wait_bits = 0 ;
2626
@@ -37,43 +37,52 @@ static void os_pwr_task_handle(void *pvParameters)
3737 portMAX_DELAY
3838 );
3939
40- ESP_LOGW (OS_PWR_TAG , "waiting for unfinished jobs...." );
41- vTaskDelay (500 / portTICK_RATE_MS );
42-
4340 EventBits_t uxBits = xEventGroupGetBits (user_event_group );
4441 if (uxBits & OS_PWR_SLEEP_BIT ) {
45- xEventGroupWaitBits (
46- user_event_group ,
47- sleep_wait_bits ,
48- pdFALSE ,
49- pdTRUE ,
50- portMAX_DELAY
51- );
52-
53- #ifdef CONFIG_ENABLE_SLP_KEY
42+ if (sleep_wait_bits ) {
43+ ESP_LOGW (OS_PWR_TAG , "waiting for unfinished jobs...." );
44+
45+ vTaskDelay (500 / portTICK_RATE_MS );
46+
47+ xEventGroupWaitBits (
48+ user_event_group ,
49+ sleep_wait_bits ,
50+ pdFALSE ,
51+ pdTRUE ,
52+ portMAX_DELAY
53+ );
54+ }
55+
56+ #ifdef CONFIG_ENABLE_SLEEP_KEY
5457 esp_sleep_pd_config (ESP_PD_DOMAIN_RTC_PERIPH , ESP_PD_OPTION_ON );
55- rtc_gpio_set_direction (CONFIG_SLP_KEY_PIN , RTC_GPIO_MODE_INPUT_ONLY );
56- #ifdef CONFIG_SLP_KEY_ACTIVE_LOW
57- rtc_gpio_pulldown_dis (CONFIG_SLP_KEY_PIN );
58- rtc_gpio_pullup_en (CONFIG_SLP_KEY_PIN );
59- esp_sleep_enable_ext1_wakeup (1ULL << CONFIG_SLP_KEY_PIN , ESP_EXT1_WAKEUP_ALL_LOW );
58+ rtc_gpio_set_direction (CONFIG_SLEEP_KEY_PIN , RTC_GPIO_MODE_INPUT_ONLY );
59+ #ifdef CONFIG_SLEEP_KEY_ACTIVE_LOW
60+ rtc_gpio_pulldown_dis (CONFIG_SLEEP_KEY_PIN );
61+ rtc_gpio_pullup_en (CONFIG_SLEEP_KEY_PIN );
62+ esp_sleep_enable_ext1_wakeup (1ULL << CONFIG_SLEEP_KEY_PIN , ESP_EXT1_WAKEUP_ALL_LOW );
6063 #else
61- rtc_gpio_pullup_dis (CONFIG_SLP_KEY_PIN );
62- rtc_gpio_pulldown_en (CONFIG_SLP_KEY_PIN );
63- esp_sleep_enable_ext1_wakeup (1ULL << CONFIG_SLP_KEY_PIN , ESP_EXT1_WAKEUP_ANY_HIGH );
64+ rtc_gpio_pullup_dis (CONFIG_SLEEP_KEY_PIN );
65+ rtc_gpio_pulldown_en (CONFIG_SLEEP_KEY_PIN );
66+ esp_sleep_enable_ext1_wakeup (1ULL << CONFIG_SLEEP_KEY_PIN , ESP_EXT1_WAKEUP_ANY_HIGH );
6467 #endif
6568#endif
6669
6770 ESP_LOGW (OS_PWR_TAG , "sleep now" );
6871 esp_deep_sleep_start ();
6972 } else if (uxBits & OS_PWR_RESET_BIT ) {
70- xEventGroupWaitBits (
71- user_event_group ,
72- reset_wait_bits ,
73- pdFALSE ,
74- pdTRUE ,
75- portMAX_DELAY
76- );
73+ if (reset_wait_bits ) {
74+ ESP_LOGW (OS_PWR_TAG , "waiting for unfinished jobs...." );
75+
76+ vTaskDelay (500 / portTICK_RATE_MS );
77+
78+ xEventGroupWaitBits (
79+ user_event_group ,
80+ reset_wait_bits ,
81+ pdFALSE ,
82+ pdTRUE ,
83+ portMAX_DELAY
84+ );
85+ }
7786
7887 ESP_LOGW (OS_PWR_TAG , "reset now" );
7988 esp_restart ();
@@ -83,53 +92,46 @@ static void os_pwr_task_handle(void *pvParameters)
8392
8493void os_pwr_sleep_wait (EventBits_t bits )
8594{
86- if (bits ) {
87- sleep_wait_bits = bits ;
88- xEventGroupSetBits (user_event_group , OS_PWR_SLEEP_BIT );
89- } else {
90- #ifdef CONFIG_ENABLE_SLP_KEY
91- esp_sleep_pd_config (ESP_PD_DOMAIN_RTC_PERIPH , ESP_PD_OPTION_ON );
92- rtc_gpio_set_direction (CONFIG_SLP_KEY_PIN , RTC_GPIO_MODE_INPUT_ONLY );
93- #ifdef CONFIG_SLP_KEY_ACTIVE_LOW
94- rtc_gpio_pulldown_dis (CONFIG_SLP_KEY_PIN );
95- rtc_gpio_pullup_en (CONFIG_SLP_KEY_PIN );
96- esp_sleep_enable_ext1_wakeup (1ULL << CONFIG_SLP_KEY_PIN , ESP_EXT1_WAKEUP_ALL_LOW );
97- #else
98- rtc_gpio_pullup_dis (CONFIG_SLP_KEY_PIN );
99- rtc_gpio_pulldown_en (CONFIG_SLP_KEY_PIN );
100- esp_sleep_enable_ext1_wakeup (1ULL << CONFIG_SLP_KEY_PIN , ESP_EXT1_WAKEUP_ANY_HIGH );
101- #endif
102- #endif
103- ESP_LOGW (OS_PWR_TAG , "sleep now" );
104- esp_deep_sleep_start ();
105- }
95+ sleep_wait_bits = bits ;
96+ xEventGroupSetBits (user_event_group , OS_PWR_SLEEP_BIT );
10697}
10798
10899void os_pwr_reset_wait (EventBits_t bits )
109100{
110- if (bits ) {
111- reset_wait_bits = bits ;
112- xEventGroupSetBits (user_event_group , OS_PWR_RESET_BIT );
113- } else {
114- ESP_LOGW (OS_PWR_TAG , "reset now" );
115- esp_restart ();
116- }
101+ reset_wait_bits = bits ;
102+ xEventGroupSetBits (user_event_group , OS_PWR_RESET_BIT );
117103}
118104#endif
119105
120106void os_init (void )
121107{
122108 user_event_group = xEventGroupCreate ();
123109
124- #ifdef CONFIG_ENABLE_SLP_KEY
110+ #if defined(CONFIG_ENABLE_SLEEP_KEY ) || defined(CONFIG_ENABLE_BLE_CONTROL_IF )
111+ xTaskCreatePinnedToCore (os_pwr_task_handle , "osPwrT" , 2048 , NULL , 5 , NULL , 0 );
112+ #endif
113+
114+ #ifdef CONFIG_ENABLE_SLEEP_KEY
125115 if (esp_sleep_get_wakeup_cause () != ESP_SLEEP_WAKEUP_UNDEFINED ) {
126- ESP_LOGW (OS_PWR_TAG , "resuming from sleep mode" );
116+ #if CONFIG_SLEEP_KEY_HOLD_TIME > 500
117+ vTaskDelay ((CONFIG_SLEEP_KEY_HOLD_TIME - 500 ) / portTICK_RATE_MS );
118+
119+ #ifdef CONFIG_SLEEP_KEY_ACTIVE_LOW
120+ if (rtc_gpio_get_level (CONFIG_SLEEP_KEY_PIN ) == 0 ) {
121+ #else
122+ if (rtc_gpio_get_level (CONFIG_SLEEP_KEY_PIN ) == 1 ) {
123+ #endif
124+ ESP_LOGW (OS_PWR_TAG , "resuming from sleep mode...." );
125+ } else {
126+ ESP_LOGW (OS_PWR_TAG , "resuming aborted." );
127+
128+ os_pwr_sleep_wait (0 );
129+ }
130+ #else
131+ ESP_LOGW (OS_PWR_TAG , "resuming from sleep mode...." );
132+ #endif
127133 } else {
128134 os_pwr_sleep_wait (0 );
129135 }
130136#endif
131-
132- #if defined(CONFIG_ENABLE_SLP_KEY ) || defined(CONFIG_ENABLE_BLE_IF )
133- xTaskCreatePinnedToCore (os_pwr_task_handle , "osPwrT" , 2048 , NULL , 5 , NULL , 0 );
134- #endif
135137}
0 commit comments