|
61 | 61 | #define SLAVE_UART_RX_IO_NUM DEFAULT_UART1_TX_IO_NUM |
62 | 62 | #define UART_BAUD_RATE (115200) |
63 | 63 | #define BUF_SIZE (1024) |
64 | | -#define TIMER_WAKEUP_TIME_US (5 * 100 * 1000) |
| 64 | +#define TIMER_WAKEUP_TIME_US (1 * 100 * 1000) |
65 | 65 |
|
66 | 66 | static void force_stdout(void) |
67 | 67 | { |
@@ -139,14 +139,16 @@ void send_and_verify_recived_data(const char* message, uint8_t length, bool shou |
139 | 139 |
|
140 | 140 | uart_flush_input(MASTER_UART_NUM); |
141 | 141 | uart_write_bytes(MASTER_UART_NUM, message, length); |
142 | | - |
143 | | - char *data = (char *) malloc(BUF_SIZE); |
144 | | - int len = uart_read_bytes(MASTER_UART_NUM, data, (BUF_SIZE - 1), 1000 / portTICK_PERIOD_MS); |
| 142 | + /* Wait for uart write finish */ |
| 143 | + uart_wait_tx_idle_polling(MASTER_UART_NUM); |
145 | 144 |
|
146 | 145 | bool wake_up_detected = false; |
147 | 146 | const char *target = "Wakeup OK!"; |
148 | 147 | int target_len = 11; |
149 | 148 | bool match = true; |
| 149 | + char *data = (char *) malloc(BUF_SIZE); |
| 150 | + int len = uart_read_bytes(MASTER_UART_NUM, data, target_len, 5000 / portTICK_PERIOD_MS); |
| 151 | + |
150 | 152 | if (len > 0) { |
151 | 153 | if (len != target_len) { |
152 | 154 | match = false; |
@@ -207,8 +209,11 @@ static void enter_sleep_and_send_respond(void) |
207 | 209 | /* Get timestamp after waking up from sleep */ |
208 | 210 | int64_t t_after_us = esp_timer_get_time(); |
209 | 211 |
|
| 212 | + /* Clear uart input buffer */ |
210 | 213 | uart_flush_input(SLAVE_UART_NUM); |
211 | 214 | printf("sleep duration: %lld\n", t_after_us - t_before_us); |
| 215 | + |
| 216 | + /* Determine the reason for uart wakeup */ |
212 | 217 | switch (esp_sleep_get_wakeup_cause()) { |
213 | 218 | case ESP_SLEEP_WAKEUP_UART: |
214 | 219 | /* Hang-up for a while to switch and execute the uart task |
|
0 commit comments