From 5e50ec1d668987209accd91a8584e77f6dc294f9 Mon Sep 17 00:00:00 2001 From: morris Date: Mon, 25 Jul 2022 11:23:39 +0800 Subject: [PATCH] systimer: add helper functions to convert between tick and us --- components/esp_hw_support/CMakeLists.txt | 4 ++ .../include/esp_private/systimer.h | 33 +++++++++++++++++ components/esp_hw_support/linker.lf | 2 + .../esp_hw_support/port/esp32c2/systimer.c | 37 +++++++++++++++++++ .../esp_hw_support/port/esp32c3/systimer.c | 22 +++++++++++ .../esp_hw_support/port/esp32h2/systimer.c | 22 +++++++++++ .../esp_hw_support/port/esp32s2/systimer.c | 22 +++++++++++ .../esp_hw_support/port/esp32s3/systimer.c | 22 +++++++++++ 8 files changed, 164 insertions(+) create mode 100644 components/esp_hw_support/include/esp_private/systimer.h create mode 100644 components/esp_hw_support/port/esp32c2/systimer.c create mode 100644 components/esp_hw_support/port/esp32c3/systimer.c create mode 100644 components/esp_hw_support/port/esp32h2/systimer.c create mode 100644 components/esp_hw_support/port/esp32s2/systimer.c create mode 100644 components/esp_hw_support/port/esp32s3/systimer.c diff --git a/components/esp_hw_support/CMakeLists.txt b/components/esp_hw_support/CMakeLists.txt index 6598f3f181..788cdfa686 100644 --- a/components/esp_hw_support/CMakeLists.txt +++ b/components/esp_hw_support/CMakeLists.txt @@ -40,6 +40,10 @@ if(NOT BOOTLOADER_BUILD) list(APPEND srcs "esp_async_memcpy.c") endif() + if(CONFIG_SOC_SYSTIMER_SUPPORTED) + list(APPEND srcs "port/${target}/systimer.c") + endif() + else() # Requires "_esp_error_check_failed()" function list(APPEND priv_requires "esp_system") diff --git a/components/esp_hw_support/include/esp_private/systimer.h b/components/esp_hw_support/include/esp_private/systimer.h new file mode 100644 index 0000000000..da31530af2 --- /dev/null +++ b/components/esp_hw_support/include/esp_private/systimer.h @@ -0,0 +1,33 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Convert ticks to microseconds + * + * @param ticks ticks to convert + * @return microseconds + */ +uint64_t systimer_ticks_to_us(uint64_t ticks) __attribute__((const)); + +/** + * @brief Convert microseconds to ticks + * + * @param us microseconds to convert + * @return ticks + */ +uint64_t systimer_us_to_ticks(uint64_t us) __attribute__((const)); + +#ifdef __cplusplus +} +#endif diff --git a/components/esp_hw_support/linker.lf b/components/esp_hw_support/linker.lf index 689c677236..c63173bf35 100644 --- a/components/esp_hw_support/linker.lf +++ b/components/esp_hw_support/linker.lf @@ -28,3 +28,5 @@ entries: gdma: gdma_stop (noflash) gdma: gdma_append (noflash) gdma: gdma_reset (noflash) + if SOC_SYSTIMER_SUPPORTED = y: + systimer (noflash) diff --git a/components/esp_hw_support/port/esp32c2/systimer.c b/components/esp_hw_support/port/esp32c2/systimer.c new file mode 100644 index 0000000000..293a73fa0e --- /dev/null +++ b/components/esp_hw_support/port/esp32c2/systimer.c @@ -0,0 +1,37 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "sdkconfig.h" +#include "esp_private/systimer.h" + +/** + * @brief systimer's clock source is fixed to XTAL (40MHz/26MHz), and has a fixed fractional divider (2.5). + * So the resolution of the systimer is 40MHz/2.5 = 16MHz, or 26MHz/2.5 = 10.4MHz. + */ + +#if CONFIG_ESP32C2_XTAL_FREQ_40 +uint64_t systimer_ticks_to_us(uint64_t ticks) +{ + return ticks / 16; +} + +uint64_t systimer_us_to_ticks(uint64_t us) +{ + return us * 16; +} +#elif CONFIG_ESP32C2_XTAL_FREQ_26 +uint64_t systimer_ticks_to_us(uint64_t ticks) +{ + return ticks * 5 / 52; +} + +uint64_t systimer_us_to_ticks(uint64_t us) +{ + return us * 52 / 5; +} +#else +#error "Unsupported XTAL frequency by systimer" +#endif // CONFIG_ESP32C2_XTAL_FREQ_xx diff --git a/components/esp_hw_support/port/esp32c3/systimer.c b/components/esp_hw_support/port/esp32c3/systimer.c new file mode 100644 index 0000000000..d5ea58b1aa --- /dev/null +++ b/components/esp_hw_support/port/esp32c3/systimer.c @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_private/systimer.h" + +/** + * @brief systimer's clock source is fixed to XTAL (40MHz), and has a fixed fractional divider (2.5). + * So the resolution of the systimer is 40MHz/2.5 = 16MHz. + */ + +uint64_t systimer_ticks_to_us(uint64_t ticks) +{ + return ticks / 16; +} + +uint64_t systimer_us_to_ticks(uint64_t us) +{ + return us * 16; +} diff --git a/components/esp_hw_support/port/esp32h2/systimer.c b/components/esp_hw_support/port/esp32h2/systimer.c new file mode 100644 index 0000000000..d5ea58b1aa --- /dev/null +++ b/components/esp_hw_support/port/esp32h2/systimer.c @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_private/systimer.h" + +/** + * @brief systimer's clock source is fixed to XTAL (40MHz), and has a fixed fractional divider (2.5). + * So the resolution of the systimer is 40MHz/2.5 = 16MHz. + */ + +uint64_t systimer_ticks_to_us(uint64_t ticks) +{ + return ticks / 16; +} + +uint64_t systimer_us_to_ticks(uint64_t us) +{ + return us * 16; +} diff --git a/components/esp_hw_support/port/esp32s2/systimer.c b/components/esp_hw_support/port/esp32s2/systimer.c new file mode 100644 index 0000000000..723d06bc61 --- /dev/null +++ b/components/esp_hw_support/port/esp32s2/systimer.c @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_private/systimer.h" + +/** + * @brief systimer's clock source can be switch between XTAL (40MHz) and APB (80MHz). + * But the resolution of the systimer is configured to fix to 80MHz. + */ + +uint64_t systimer_ticks_to_us(uint64_t ticks) +{ + return ticks / 80; +} + +uint64_t systimer_us_to_ticks(uint64_t us) +{ + return us * 80; +} diff --git a/components/esp_hw_support/port/esp32s3/systimer.c b/components/esp_hw_support/port/esp32s3/systimer.c new file mode 100644 index 0000000000..d5ea58b1aa --- /dev/null +++ b/components/esp_hw_support/port/esp32s3/systimer.c @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "esp_private/systimer.h" + +/** + * @brief systimer's clock source is fixed to XTAL (40MHz), and has a fixed fractional divider (2.5). + * So the resolution of the systimer is 40MHz/2.5 = 16MHz. + */ + +uint64_t systimer_ticks_to_us(uint64_t ticks) +{ + return ticks / 16; +} + +uint64_t systimer_us_to_ticks(uint64_t us) +{ + return us * 16; +}