mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
simple test for uart wakeup
This commit is contained in:
parent
176f44c15f
commit
ea837e3a5e
@ -19,77 +19,98 @@
|
||||
#include "driver/uart.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "soc/uart_struct.h"
|
||||
|
||||
/* Most development boards have "boot" button attached to GPIO0.
|
||||
* You can also change this to another pin.
|
||||
*/
|
||||
#if CONFIG_IDF_TARGET_ESP32C3
|
||||
#define BUTTON_GPIO_NUM_DEFAULT 9
|
||||
#else
|
||||
#define BUTTON_GPIO_NUM_DEFAULT 0
|
||||
#endif
|
||||
#define TAG "UART"
|
||||
#define TEST_UART_NUM 1
|
||||
#define TEST_BUF_SIZE 1024
|
||||
|
||||
/* "Boot" button is active low */
|
||||
#define BUTTON_WAKEUP_LEVEL_DEFAULT 0
|
||||
static QueueHandle_t uart0_queue;
|
||||
|
||||
void light_sleep_wakeup_config(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(gpio_sleep_set_direction(6, GPIO_MODE_INPUT));
|
||||
ESP_ERROR_CHECK(gpio_sleep_set_pull_mode(6, GPIO_PULLUP_ONLY));
|
||||
|
||||
if (uart_set_wakeup_threshold(TEST_UART_NUM, 3) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "set uart1 wakeup threshold failed");
|
||||
}
|
||||
if (esp_sleep_enable_uart_wakeup(TEST_UART_NUM) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "set uart1 wakeup failed");
|
||||
}
|
||||
ESP_LOGI(TAG, "set_light_sleep_wakeup ok");
|
||||
}
|
||||
|
||||
static void uart_wakeup_task(void *arg)
|
||||
{
|
||||
uart_event_t event;
|
||||
esp_light_sleep_start();
|
||||
for(;;) {
|
||||
//Waiting for UART event.
|
||||
if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
|
||||
ESP_LOGI(TAG, "uart[%d] event:", TEST_UART_NUM);
|
||||
switch(event.type) {
|
||||
case UART_DATA:
|
||||
ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
|
||||
//uart_read_bytes(TEST_UART_NUM, dtmp, event.size, portMAX_DELAY);
|
||||
//ESP_LOGI(TAG, "[DATA EVT]:");
|
||||
//uart_write_bytes(TEST_UART_NUM, (const char*) dtmp, event.size);
|
||||
break;
|
||||
//Event of HW FIFO overflow detected
|
||||
case UART_FIFO_OVF:
|
||||
ESP_LOGI(TAG, "hw fifo overflow");
|
||||
// If fifo overflow happened, you should consider adding flow control for your application.
|
||||
// The ISR has already reset the rx FIFO,
|
||||
// As an example, we directly flush the rx buffer here in order to read more data.
|
||||
uart_flush_input(TEST_UART_NUM);
|
||||
xQueueReset(uart0_queue);
|
||||
break;
|
||||
//Event of UART ring buffer full
|
||||
case UART_BUFFER_FULL:
|
||||
ESP_LOGI(TAG, "ring buffer full");
|
||||
// If buffer full happened, you should consider encreasing your buffer size
|
||||
// As an example, we directly flush the rx buffer here in order to read more data.
|
||||
uart_flush_input(TEST_UART_NUM);
|
||||
xQueueReset(uart0_queue);
|
||||
break;
|
||||
//Event of UART RX break detected
|
||||
case UART_BREAK:
|
||||
ESP_LOGI(TAG, "uart rx break");
|
||||
break;
|
||||
//Event of UART parity check error
|
||||
case UART_PARITY_ERR:
|
||||
ESP_LOGI(TAG, "uart parity error");
|
||||
break;
|
||||
//Event of UART frame error
|
||||
case UART_FRAME_ERR:
|
||||
ESP_LOGI(TAG, "uart frame error");
|
||||
break;
|
||||
case UART_WAKEUP:
|
||||
ESP_LOGI(TAG, "uart uart wakeup");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGI(TAG, "uart event type: %d", event.type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
/* Configure the button GPIO as input, enable wakeup */
|
||||
const int button_gpio_num = BUTTON_GPIO_NUM_DEFAULT;
|
||||
const int wakeup_level = BUTTON_WAKEUP_LEVEL_DEFAULT;
|
||||
gpio_config_t config = {
|
||||
.pin_bit_mask = BIT64(button_gpio_num),
|
||||
.mode = GPIO_MODE_INPUT
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = 115200,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_XTAL,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&config));
|
||||
gpio_wakeup_enable(button_gpio_num,
|
||||
wakeup_level == 0 ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
|
||||
|
||||
while (true) {
|
||||
/* Wake up in 2 seconds, or when button is pressed */
|
||||
esp_sleep_enable_timer_wakeup(2000000);
|
||||
esp_sleep_enable_gpio_wakeup();
|
||||
|
||||
/* Wait until GPIO goes high */
|
||||
if (gpio_get_level(button_gpio_num) == wakeup_level) {
|
||||
printf("Waiting for GPIO%d to go high...\n", button_gpio_num);
|
||||
do {
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
} while (gpio_get_level(button_gpio_num) == wakeup_level);
|
||||
}
|
||||
|
||||
printf("Entering light sleep\n");
|
||||
/* To make sure the complete line is printed before entering sleep mode,
|
||||
* need to wait until UART TX FIFO is empty:
|
||||
*/
|
||||
uart_wait_tx_idle_polling(CONFIG_ESP_CONSOLE_UART_NUM);
|
||||
|
||||
/* Get timestamp before entering sleep */
|
||||
int64_t t_before_us = esp_timer_get_time();
|
||||
|
||||
/* Enter sleep mode */
|
||||
esp_light_sleep_start();
|
||||
/* Execution continues here after wakeup */
|
||||
|
||||
/* Get timestamp after waking up from sleep */
|
||||
int64_t t_after_us = esp_timer_get_time();
|
||||
|
||||
/* Determine wake up reason */
|
||||
const char* wakeup_reason;
|
||||
switch (esp_sleep_get_wakeup_cause()) {
|
||||
case ESP_SLEEP_WAKEUP_TIMER:
|
||||
wakeup_reason = "timer";
|
||||
break;
|
||||
case ESP_SLEEP_WAKEUP_GPIO:
|
||||
wakeup_reason = "pin";
|
||||
break;
|
||||
default:
|
||||
wakeup_reason = "other";
|
||||
break;
|
||||
}
|
||||
|
||||
printf("Returned from light sleep, reason: %s, t=%lld ms, slept for %lld ms\n",
|
||||
wakeup_reason, t_after_us / 1000, (t_after_us - t_before_us) / 1000);
|
||||
}
|
||||
|
||||
//Install UART driver, and get the queue.
|
||||
uart_driver_install(TEST_UART_NUM, TEST_BUF_SIZE * 2, TEST_BUF_SIZE * 2, 20, &uart0_queue, 0);
|
||||
uart_param_config(TEST_UART_NUM, &uart_config);
|
||||
uart_set_pin(TEST_UART_NUM, 7, 6, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
||||
light_sleep_wakeup_config();
|
||||
xTaskCreate(uart_wakeup_task, "uart_wakeup_task", 2048, NULL, 12, NULL);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user