Skip to content

Commit 8f89a22

Browse files
committed
change(example): add support for hp uart wakeup mode 1 2 3
1 parent bcd138f commit 8f89a22

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
menu "Example Configuration"
2+
3+
choice EXAMPLE_UART_WAKEUP_MODE
4+
prompt "uart wakeup mode"
5+
default UART_WK_MODE_ACTIVE_THRESH
6+
help
7+
Uart wakeup MODE_ACTIVE_THRESH | MODE_FIFO_THRESH | MODE_START_BIT | MODE_CHAR_SEQ to be selected
8+
for uart wakeup during light sleep. Specifically, wakeup MODE_ACTIVE_THRESH doesn't require a clock.
9+
In contrast, the other three wakeup modes need XTAL (not RC FAST for instability) ungated during light
10+
sleep, where the chips used should support sleep clock icg control. However, they consume more power
11+
as XTAL must be powered on.
12+
13+
config UART_WK_MODE_ACTIVE_THRESH
14+
bool "MODE_ACTIVE_THRESH"
15+
config UART_WK_MODE_FIFO_THRESH
16+
bool "MODE_FIFO_THRESH"
17+
depends on SOC_UART_WAKEUP_SUPPORT_FIFO_THRESH_MODE && SOC_PM_SUPPORT_PMU_CLK_ICG && SOC_PMU_SUPPORTED
18+
config UART_WK_MODE_START_BIT
19+
bool "MODE_START_BIT"
20+
depends on SOC_UART_WAKEUP_SUPPORT_START_BIT_MODE && SOC_PM_SUPPORT_PMU_CLK_ICG && SOC_PMU_SUPPORTED
21+
config UART_WK_MODE_CHAR_SEQ
22+
bool "MODE_CHAR_SEQ"
23+
depends on SOC_UART_WAKEUP_SUPPORT_CHAR_SEQ_MODE && SOC_PM_SUPPORT_PMU_CLK_ICG && SOC_PMU_SUPPORTED
24+
endchoice
25+
26+
config EXAMPLE_UART_WAKEUP_MODE_SELCTED
27+
int
28+
default 0 if UART_WK_MODE_ACTIVE_THRESH
29+
default 1 if UART_WK_MODE_FIFO_THRESH
30+
default 2 if UART_WK_MODE_START_BIT
31+
default 3 if UART_WK_MODE_CHAR_SEQ
32+
33+
endmenu

examples/system/light_sleep/main/uart_wakeup.c

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
6+
#include <string.h>
67
#include "freertos/FreeRTOS.h"
78
#include "freertos/task.h"
89
#include "esp_check.h"
10+
#include "esp_err.h"
911
#include "esp_sleep.h"
1012
#include "soc/uart_pins.h"
1113
#include "driver/uart.h"
14+
#include "driver/uart_wakeup.h"
1215
#include "driver/gpio.h"
1316
#include "sdkconfig.h"
1417

@@ -18,7 +21,10 @@
1821
#define EXAMPLE_UART_TX_IO_NUM U0TXD_GPIO_NUM
1922
#define EXAMPLE_UART_RX_IO_NUM U0RXD_GPIO_NUM
2023

21-
#define EXAMPLE_UART_WAKEUP_THRESHOLD 3
24+
#define EXAMPLE_UART_WAKEUP_EDGE_THRESHOLD 3
25+
#define EXAMPLE_UART_WAKEUP_FIFO_THRESHOLD 8
26+
#define EXAMPLE_UART_WAKEUP_CHARS_SEQ "ok"
27+
#define EXAMPLE_UART_WAKEUP_CHARS_SEQ_LEN SOC_UART_WAKEUP_CHARS_SEQ_MAX_LEN
2228

2329
#define EXAMPLE_READ_BUF_SIZE 1024
2430
#define EXAMPLE_UART_BUF_SIZE (EXAMPLE_READ_BUF_SIZE * 2)
@@ -123,12 +129,45 @@ static esp_err_t uart_initialization(void)
123129

124130
static esp_err_t uart_wakeup_config(void)
125131
{
126-
/* UART will wakeup the chip up from light sleep if the edges that RX pin received has reached the threshold */
127-
ESP_RETURN_ON_ERROR(uart_set_wakeup_threshold(EXAMPLE_UART_NUM, EXAMPLE_UART_WAKEUP_THRESHOLD),
128-
TAG, "Set uart wakeup threshold failed");
129-
/* Only uart0 and uart1 (if has) support to be configured as wakeup source */
130-
ESP_RETURN_ON_ERROR(esp_sleep_enable_uart_wakeup(EXAMPLE_UART_NUM),
131-
TAG, "Configure uart as wakeup source failed");
132+
uart_wakeup_cfg_t uart_wakeup_cfg = {};
133+
uint8_t wakeup_mode = CONFIG_EXAMPLE_UART_WAKEUP_MODE_SELCTED;
134+
switch (wakeup_mode) {
135+
/* UART will wakeup the chip up from light sleep if the edges that RX pin received reaches the threshold */
136+
#if SOC_UART_WAKEUP_SUPPORT_ACTIVE_THRESH_MODE
137+
case UART_WK_MODE_ACTIVE_THRESH:
138+
uart_wakeup_cfg.wakeup_mode = UART_WK_MODE_ACTIVE_THRESH;
139+
uart_wakeup_cfg.rx_edge_threshold = EXAMPLE_UART_WAKEUP_EDGE_THRESHOLD;
140+
break;
141+
#endif
142+
/* UART will wakeup the chip up from light sleep if the number of chars that RX FIFO received reaches the threshold */
143+
#if SOC_UART_WAKEUP_SUPPORT_FIFO_THRESH_MODE
144+
case UART_WK_MODE_FIFO_THRESH:
145+
uart_wakeup_cfg.wakeup_mode = UART_WK_MODE_FIFO_THRESH;
146+
uart_wakeup_cfg.rx_fifo_threshold = EXAMPLE_UART_WAKEUP_FIFO_THRESHOLD;
147+
break;
148+
#endif
149+
/* UART will wakeup the chip up from light sleep if RX FIFO receives a start bit */
150+
#if SOC_UART_WAKEUP_SUPPORT_START_BIT_MODE
151+
case UART_WK_MODE_START_BIT:
152+
uart_wakeup_cfg.wakeup_mode = UART_WK_MODE_START_BIT;
153+
break;
154+
#endif
155+
/* UART will wakeup the chip up from light sleep if the chars sequence that RX FIFO received matches the predefined value */
156+
#if SOC_UART_WAKEUP_SUPPORT_CHAR_SEQ_MODE
157+
case UART_WK_MODE_CHAR_SEQ:
158+
uart_wakeup_cfg.wakeup_mode = UART_WK_MODE_CHAR_SEQ;
159+
// uart wakeup chars len need less than SOC_UART_WAKEUP_CHARS_SEQ_MAX_LEN
160+
strncpy(uart_wakeup_cfg.wake_chars_seq, EXAMPLE_UART_WAKEUP_CHARS_SEQ, EXAMPLE_UART_WAKEUP_CHARS_SEQ_LEN);
161+
break;
162+
#endif
163+
default:
164+
ESP_LOGE(TAG, "Unknown UART wakeup mode");
165+
return ESP_FAIL;
166+
break;
167+
}
168+
169+
ESP_ERROR_CHECK(uart_wakeup_setup(EXAMPLE_UART_NUM, &uart_wakeup_cfg));
170+
ESP_ERROR_CHECK(esp_sleep_enable_uart_wakeup(EXAMPLE_UART_NUM));
132171
return ESP_OK;
133172
}
134173

0 commit comments

Comments
 (0)