From bcfe684951213a86b5b311a70023c1422b92f063 Mon Sep 17 00:00:00 2001 From: chenjianqiang Date: Mon, 25 Nov 2019 20:10:27 +0800 Subject: [PATCH] bugfix(timer): add a macro to control making the XTAL related functions --- components/driver/timer.c | 9 +++------ .../soc/esp32/include/soc/timer_group_caps.h | 15 +++++++++++++++ .../esp32s2beta/include/soc/timer_group_caps.h | 17 +++++++++++++++++ components/soc/include/hal/timer_hal.h | 3 ++- components/soc/include/hal/timer_types.h | 5 +++-- .../timer_group/main/timer_group_example_main.c | 2 +- 6 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 components/soc/esp32/include/soc/timer_group_caps.h create mode 100644 components/soc/esp32s2beta/include/soc/timer_group_caps.h diff --git a/components/driver/timer.c b/components/driver/timer.c index e1303b6b5..567aea446 100644 --- a/components/driver/timer.c +++ b/components/driver/timer.c @@ -82,13 +82,10 @@ esp_err_t timer_get_counter_time_sec(timer_group_t group_num, timer_idx_t timer_ if (err == ESP_OK) { uint16_t div; timer_hal_get_divider(&(p_timer_obj[group_num][timer_num]->hal), &div); -#ifdef CONFIG_IDF_TARGET_ESP32 - *time = (double)timer_val * div / TIMER_BASE_CLK; -#elif defined CONFIG_IDF_TARGET_ESP32S2BETA + *time = (double)timer_val * div / rtc_clk_apb_freq_get(); +#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK if (timer_hal_get_use_xtal(&(p_timer_obj[group_num][timer_num]->hal))) { *time = (double)timer_val * div / ((int)rtc_clk_xtal_freq_get() * 1000000); - } else { - *time = (double)timer_val * div / rtc_clk_apb_freq_get(); } #endif } @@ -327,7 +324,7 @@ esp_err_t timer_init(timer_group_t group_num, timer_idx_t timer_num, const timer // timer_hal_set_edge_int_enable(&(p_timer_obj[group_num][timer_num]->hal), true); // } timer_hal_set_counter_enable(&(p_timer_obj[group_num][timer_num]->hal), config->counter_en); -#ifdef CONFIG_IDF_TARGET_ESP32S2BETA +#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK timer_hal_set_use_xtal(&(p_timer_obj[group_num][timer_num]->hal), config->clk_src); #endif TIMER_EXIT_CRITICAL(&timer_spinlock[group_num]); diff --git a/components/soc/esp32/include/soc/timer_group_caps.h b/components/soc/esp32/include/soc/timer_group_caps.h new file mode 100644 index 000000000..e15269aae --- /dev/null +++ b/components/soc/esp32/include/soc/timer_group_caps.h @@ -0,0 +1,15 @@ +// Copyright 2015-2019 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. + +#pragma once diff --git a/components/soc/esp32s2beta/include/soc/timer_group_caps.h b/components/soc/esp32s2beta/include/soc/timer_group_caps.h new file mode 100644 index 000000000..8e4451349 --- /dev/null +++ b/components/soc/esp32s2beta/include/soc/timer_group_caps.h @@ -0,0 +1,17 @@ +// Copyright 2015-2019 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. + +#pragma once + +#define TIMER_GROUP_SUPPORTS_XTAL_CLOCK diff --git a/components/soc/include/hal/timer_hal.h b/components/soc/include/hal/timer_hal.h index 2ea8eee17..bc4dcc251 100644 --- a/components/soc/include/hal/timer_hal.h +++ b/components/soc/include/hal/timer_hal.h @@ -29,6 +29,7 @@ extern "C" { #include "hal/timer_ll.h" #include "hal/timer_types.h" +#include "soc/timer_group_caps.h" /** * Context that should be maintained by both the driver and the HAL @@ -293,7 +294,7 @@ void timer_hal_init(timer_hal_context_t *hal, timer_group_t group_num, timer_idx */ #define timer_hal_get_intr_status_reg(hal, intr_status_reg) timer_ll_get_intr_status_reg((hal)->dev, intr_status_reg) -#ifdef CONFIG_IDF_TARGET_ESP32S2BETA +#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK /** * @brief Set clock source. * diff --git a/components/soc/include/hal/timer_types.h b/components/soc/include/hal/timer_types.h index 123875565..09e0fcc24 100644 --- a/components/soc/include/hal/timer_types.h +++ b/components/soc/include/hal/timer_types.h @@ -21,6 +21,7 @@ extern "C" { #include #include #include +#include "soc/timer_group_caps.h" /** * @brief Selects a Timer-Group out of 2 available groups @@ -107,7 +108,7 @@ typedef enum { TIMER_AUTORELOAD_MAX, } timer_autoreload_t; -#ifdef CONFIG_IDF_TARGET_ESP32S2BETA +#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK /** * @brief Select timer source clock. */ @@ -127,7 +128,7 @@ typedef struct { timer_count_dir_t counter_dir; /*!< Counter direction */ timer_autoreload_t auto_reload; /*!< Timer auto-reload */ uint32_t divider; /*!< Counter clock divider. The divider's range is from from 2 to 65536. */ -#ifdef CONFIG_IDF_TARGET_ESP32S2BETA +#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK timer_src_clk_t clk_src; /*!< Use XTAL as source clock. */ #endif } timer_config_t; diff --git a/examples/peripherals/timer_group/main/timer_group_example_main.c b/examples/peripherals/timer_group/main/timer_group_example_main.c index 30b3a9c00..736514df0 100644 --- a/examples/peripherals/timer_group/main/timer_group_example_main.c +++ b/examples/peripherals/timer_group/main/timer_group_example_main.c @@ -111,7 +111,7 @@ static void example_tg0_timer_init(int timer_idx, config.alarm_en = TIMER_ALARM_EN; config.intr_type = TIMER_INTR_LEVEL; config.auto_reload = auto_reload; -#ifdef CONFIG_IDF_TARGET_ESP32S2BETA +#ifdef TIMER_GROUP_SUPPORTS_XTAL_CLOCK config.clk_src = TIMER_SRC_CLK_APB; #endif timer_init(TIMER_GROUP_0, timer_idx, &config);