systimer: add helper functions to convert between tick and us

This commit is contained in:
morris
2022-07-25 11:23:39 +08:00
parent 783e1781bd
commit 5e50ec1d66
8 changed files with 164 additions and 0 deletions

View File

@@ -40,6 +40,10 @@ if(NOT BOOTLOADER_BUILD)
list(APPEND srcs "esp_async_memcpy.c") list(APPEND srcs "esp_async_memcpy.c")
endif() endif()
if(CONFIG_SOC_SYSTIMER_SUPPORTED)
list(APPEND srcs "port/${target}/systimer.c")
endif()
else() else()
# Requires "_esp_error_check_failed()" function # Requires "_esp_error_check_failed()" function
list(APPEND priv_requires "esp_system") list(APPEND priv_requires "esp_system")

View File

@@ -0,0 +1,33 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#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

View File

@@ -28,3 +28,5 @@ entries:
gdma: gdma_stop (noflash) gdma: gdma_stop (noflash)
gdma: gdma_append (noflash) gdma: gdma_append (noflash)
gdma: gdma_reset (noflash) gdma: gdma_reset (noflash)
if SOC_SYSTIMER_SUPPORTED = y:
systimer (noflash)

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}