From 1d31a493e4000ae748db3ef7a4caf70b9dd3e6e8 Mon Sep 17 00:00:00 2001 From: Chen Wu Date: Tue, 27 Jul 2021 15:46:23 +0800 Subject: [PATCH] debug: esp32c3 uart1 wakeup --- components/hal/esp32c3/include/hal/uart_ll.h | 18 ++---- examples/system/light_sleep/README.md | 2 + .../main/light_sleep_example_main.c | 57 ++++++++++++++----- .../system/light_sleep/sdkconfig.defaults | 3 +- 4 files changed, 53 insertions(+), 27 deletions(-) diff --git a/components/hal/esp32c3/include/hal/uart_ll.h b/components/hal/esp32c3/include/hal/uart_ll.h index a494e7b259..f4a21e90e8 100644 --- a/components/hal/esp32c3/include/hal/uart_ll.h +++ b/components/hal/esp32c3/include/hal/uart_ll.h @@ -1,16 +1,8 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ // The LL layer for UART register operations. // Note that most of the register operations in this layer are non-atomic operations. diff --git a/examples/system/light_sleep/README.md b/examples/system/light_sleep/README.md index c9212067c6..4ae642df2e 100644 --- a/examples/system/light_sleep/README.md +++ b/examples/system/light_sleep/README.md @@ -1,3 +1,5 @@ +| Supported Targets | ESP32-C3 | +| ----------------- | -------- | # Light Sleep Example (See the README.md file in the upper level 'examples' directory for more information about examples.) diff --git a/examples/system/light_sleep/main/light_sleep_example_main.c b/examples/system/light_sleep/main/light_sleep_example_main.c index 4696b4b83f..7643f4a88e 100644 --- a/examples/system/light_sleep/main/light_sleep_example_main.c +++ b/examples/system/light_sleep/main/light_sleep_example_main.c @@ -1,11 +1,10 @@ -/* Light sleep example +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ +/* Light sleep example */ #include #include @@ -20,6 +19,9 @@ #include "driver/gpio.h" #include "esp_timer.h" #include "soc/uart_struct.h" +#include "soc/rtc.h" +#include "esp_pm.h" +#include "esp32c3/pm.h" #define TAG "UART" #define TEST_UART_NUM 1 @@ -41,20 +43,43 @@ void light_sleep_wakeup_config(void) ESP_LOGI(TAG, "set_light_sleep_wakeup ok"); } +void light_sleep_setup(void) +{ + light_sleep_wakeup_config(); + + esp_pm_config_esp32c3_t pm_config = { + .max_freq_mhz = CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ, + .min_freq_mhz = (int) rtc_clk_xtal_freq_get(), + .light_sleep_enable = true + }; + ESP_ERROR_CHECK(esp_pm_configure(&pm_config)); +} + +#define RD_BUF_SIZE 1024 + static void uart_wakeup_task(void *arg) { uart_event_t event; - esp_light_sleep_start(); + // esp_light_sleep_start(); + + esp_pm_lock_handle_t lock = ((struct { esp_pm_lock_handle_t lock; } *)arg)->lock; + light_sleep_setup(); + + uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE); + for(;;) { //Waiting for UART event. if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) { - ESP_LOGI(TAG, "uart[%d] event:", TEST_UART_NUM); + + esp_pm_lock_acquire(lock); + + ESP_LOGI(TAG, "uar%d recved event:%d (wk:%d)", TEST_UART_NUM, event.type, UART_WAKEUP); 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); + 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: @@ -92,6 +117,8 @@ static void uart_wakeup_task(void *arg) ESP_LOGI(TAG, "uart event type: %d", event.type); break; } + ESP_LOGI(TAG, "uart[%d] esp_pm_lock_release()", TEST_UART_NUM); + esp_pm_lock_release(lock); } } vTaskDelete(NULL); @@ -112,5 +139,9 @@ void app_main(void) 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); + + static esp_pm_lock_handle_t uart_event_lock; + ESP_ERROR_CHECK(esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "uart_evt", &uart_event_lock)); + struct { esp_pm_lock_handle_t lock; } args = { .lock = uart_event_lock }; + xTaskCreate(uart_wakeup_task, "uart_wakeup_task", 2048, &args, 12, NULL); } diff --git a/examples/system/light_sleep/sdkconfig.defaults b/examples/system/light_sleep/sdkconfig.defaults index 8e2af8cd2c..e8945648db 100644 --- a/examples/system/light_sleep/sdkconfig.defaults +++ b/examples/system/light_sleep/sdkconfig.defaults @@ -1 +1,2 @@ -CONFIG_ESP32_DEFAULT_CPU_FREQ_80=y +CONFIG_PM_ENABLE=y +CONFIG_FREERTOS_USE_TICKLESS_IDLE=y