|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
5 | 5 | */ |
| 6 | +#include <string.h> |
6 | 7 | #include "freertos/FreeRTOS.h" |
7 | 8 | #include "freertos/task.h" |
8 | 9 | #include "esp_check.h" |
| 10 | +#include "esp_err.h" |
9 | 11 | #include "esp_sleep.h" |
10 | 12 | #include "soc/uart_pins.h" |
11 | 13 | #include "driver/uart.h" |
| 14 | +#include "driver/uart_wakeup.h" |
12 | 15 | #include "driver/gpio.h" |
13 | 16 | #include "sdkconfig.h" |
14 | 17 |
|
|
18 | 21 | #define EXAMPLE_UART_TX_IO_NUM U0TXD_GPIO_NUM |
19 | 22 | #define EXAMPLE_UART_RX_IO_NUM U0RXD_GPIO_NUM |
20 | 23 |
|
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 |
22 | 28 |
|
23 | 29 | #define EXAMPLE_READ_BUF_SIZE 1024 |
24 | 30 | #define EXAMPLE_UART_BUF_SIZE (EXAMPLE_READ_BUF_SIZE * 2) |
@@ -123,12 +129,45 @@ static esp_err_t uart_initialization(void) |
123 | 129 |
|
124 | 130 | static esp_err_t uart_wakeup_config(void) |
125 | 131 | { |
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)); |
132 | 171 | return ESP_OK; |
133 | 172 | } |
134 | 173 |
|
|
0 commit comments