2022-01-02 03:14:17 -05:00
|
|
|
/*
|
2023-01-31 01:36:21 -05:00
|
|
|
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "esp_err.h"
|
2022-07-13 01:17:32 -04:00
|
|
|
#include "driver/gptimer_types.h"
|
|
|
|
#include "driver/gptimer_etm.h"
|
2022-01-02 03:14:17 -05:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief General Purpose Timer configuration
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
gptimer_clock_source_t clk_src; /*!< GPTimer clock source */
|
|
|
|
gptimer_count_direction_t direction; /*!< Count direction */
|
|
|
|
uint32_t resolution_hz; /*!< Counter resolution (working frequency) in Hz,
|
|
|
|
hence, the step size of each count tick equals to (1 / resolution_hz) seconds */
|
2023-08-01 23:26:47 -04:00
|
|
|
int intr_priority; /*!< GPTimer interrupt priority,
|
|
|
|
if set to 0, the driver will try to allocate an interrupt with a relative low priority (1,2,3) */
|
2022-01-02 03:14:17 -05:00
|
|
|
struct {
|
|
|
|
uint32_t intr_shared: 1; /*!< Set true, the timer interrupt number can be shared with other peripherals */
|
2024-05-22 22:42:30 -04:00
|
|
|
uint32_t backup_before_sleep: 1; /*!< If set, the driver will backup/restore the GPTimer registers before/after entering/exist sleep mode.
|
|
|
|
By this approach, the system can power off GPTimer's power domain.
|
|
|
|
This can save power, but at the expense of more RAM being consumed */
|
2022-05-11 03:52:09 -04:00
|
|
|
} flags; /*!< GPTimer config flags*/
|
2022-01-02 03:14:17 -05:00
|
|
|
} gptimer_config_t;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a new General Purpose Timer, and return the handle
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note The newly created timer is put in the "init" state.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
|
|
|
* @param[in] config GPTimer configuration
|
|
|
|
* @param[out] ret_timer Returned timer handle
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Create GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Create GPTimer failed because of invalid argument
|
|
|
|
* - ESP_ERR_NO_MEM: Create GPTimer failed because out of memory
|
|
|
|
* - ESP_ERR_NOT_FOUND: Create GPTimer failed because all hardware timers are used up and no more free one
|
|
|
|
* - ESP_FAIL: Create GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_new_timer(const gptimer_config_t *config, gptimer_handle_t *ret_timer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delete the GPTimer handle
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note A timer must be in the "init" state before it can be deleted.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @return
|
|
|
|
* - ESP_OK: Delete GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Delete GPTimer failed because of invalid argument
|
2022-04-23 06:59:38 -04:00
|
|
|
* - ESP_ERR_INVALID_STATE: Delete GPTimer failed because the timer is not in init state
|
2022-01-02 03:14:17 -05:00
|
|
|
* - ESP_FAIL: Delete GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_del_timer(gptimer_handle_t timer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set GPTimer raw count value
|
|
|
|
*
|
|
|
|
* @note When updating the raw count of an active timer, the timer will immediately start counting from the new value.
|
|
|
|
* @note This function is allowed to run within ISR context
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
|
|
|
|
* makes it possible to execute even when the Flash Cache is disabled.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @param[in] value Count value to be set
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Set GPTimer raw count value successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Set GPTimer raw count value failed because of invalid argument
|
|
|
|
* - ESP_FAIL: Set GPTimer raw count value failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_set_raw_count(gptimer_handle_t timer, uint64_t value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get GPTimer raw count value
|
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @note This function will trigger a software capture event and then return the captured count value.
|
2022-12-14 05:40:13 -05:00
|
|
|
* @note With the raw count value and the resolution returned from `gptimer_get_resolution`, you can convert the count value into seconds.
|
2022-01-02 03:14:17 -05:00
|
|
|
* @note This function is allowed to run within ISR context
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
|
|
|
|
* makes it possible to execute even when the Flash Cache is disabled.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @param[out] value Returned GPTimer count value
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Get GPTimer raw count value successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Get GPTimer raw count value failed because of invalid argument
|
|
|
|
* - ESP_FAIL: Get GPTimer raw count value failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_get_raw_count(gptimer_handle_t timer, uint64_t *value);
|
|
|
|
|
2022-12-14 05:40:13 -05:00
|
|
|
/**
|
|
|
|
* @brief Return the real resolution of the timer
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note usually the timer resolution is same as what you configured in the `gptimer_config_t::resolution_hz`,
|
|
|
|
* but some unstable clock source (e.g. RC_FAST) will do a calibration, the real resolution can be different from the configured one.
|
2022-12-14 05:40:13 -05:00
|
|
|
*
|
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
|
|
|
* @param[out] out_resolution Returned timer resolution, in Hz
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Get GPTimer resolution successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Get GPTimer resolution failed because of invalid argument
|
|
|
|
* - ESP_FAIL: Get GPTimer resolution failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_get_resolution(gptimer_handle_t timer, uint32_t *out_resolution);
|
|
|
|
|
2022-07-13 01:17:32 -04:00
|
|
|
/**
|
|
|
|
* @brief Get GPTimer captured count value
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note The capture action can be issued either by ETM event or by software (see also `gptimer_get_raw_count`).
|
2022-07-13 01:17:32 -04:00
|
|
|
* @note This function is allowed to run within ISR context
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
|
|
|
|
* makes it possible to execute even when the Flash Cache is disabled.
|
2022-07-13 01:17:32 -04:00
|
|
|
*
|
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
|
|
|
* @param[out] value Returned captured count value
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Get GPTimer captured count value successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Get GPTimer captured count value failed because of invalid argument
|
|
|
|
* - ESP_FAIL: Get GPTimer captured count value failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_get_captured_count(gptimer_handle_t timer, uint64_t *value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Group of supported GPTimer callbacks
|
|
|
|
* @note The callbacks are all running under ISR environment
|
|
|
|
* @note When CONFIG_GPTIMER_ISR_IRAM_SAFE is enabled, the callback itself and functions called by it should be placed in IRAM.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
gptimer_alarm_cb_t on_alarm; /*!< Timer alarm callback */
|
|
|
|
} gptimer_event_callbacks_t;
|
|
|
|
|
2022-01-02 03:14:17 -05:00
|
|
|
/**
|
|
|
|
* @brief Set callbacks for GPTimer
|
|
|
|
*
|
2022-04-23 06:59:38 -04:00
|
|
|
* @note User registered callbacks are expected to be runnable within ISR context
|
2022-08-08 03:47:30 -04:00
|
|
|
* @note The first call to this function needs to be before the call to `gptimer_enable`
|
|
|
|
* @note User can deregister a previously registered callback by calling this function and setting the callback member in the `cbs` structure to NULL.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @param[in] cbs Group of callback functions
|
|
|
|
* @param[in] user_data User data, which will be passed to callback functions directly
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Set event callbacks successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Set event callbacks failed because of invalid argument
|
2022-04-23 06:59:38 -04:00
|
|
|
* - ESP_ERR_INVALID_STATE: Set event callbacks failed because the timer is not in init state
|
2022-01-02 03:14:17 -05:00
|
|
|
* - ESP_FAIL: Set event callbacks failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_register_event_callbacks(gptimer_handle_t timer, const gptimer_event_callbacks_t *cbs, void *user_data);
|
|
|
|
|
2022-07-13 01:17:32 -04:00
|
|
|
/**
|
|
|
|
* @brief General Purpose Timer alarm configuration
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
uint64_t alarm_count; /*!< Alarm target count value */
|
|
|
|
uint64_t reload_count; /*!< Alarm reload count value, effect only when `auto_reload_on_alarm` is set to true */
|
|
|
|
struct {
|
|
|
|
uint32_t auto_reload_on_alarm: 1; /*!< Reload the count value by hardware, immediately at the alarm event */
|
|
|
|
} flags; /*!< Alarm config flags*/
|
|
|
|
} gptimer_alarm_config_t;
|
|
|
|
|
2022-01-02 03:14:17 -05:00
|
|
|
/**
|
|
|
|
* @brief Set alarm event actions for GPTimer.
|
|
|
|
*
|
2024-03-26 04:13:17 -04:00
|
|
|
* @note This function is allowed to run within ISR context, so you can update new alarm action immediately in any ISR callback.
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
|
|
|
|
* makes it possible to execute even when the Flash Cache is disabled.
|
2024-03-26 04:13:17 -04:00
|
|
|
* In this case, please also ensure the `gptimer_alarm_config_t` instance is placed in the static data section
|
|
|
|
* instead of in the read-only data section. e.g.: `static gptimer_alarm_config_t alarm_config = { ... };`
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @param[in] config Alarm configuration, especially, set config to NULL means disabling the alarm function
|
|
|
|
* @return
|
|
|
|
* - ESP_OK: Set alarm action for GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Set alarm action for GPTimer failed because of invalid argument
|
|
|
|
* - ESP_FAIL: Set alarm action for GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_set_alarm_action(gptimer_handle_t timer, const gptimer_alarm_config_t *config);
|
|
|
|
|
|
|
|
/**
|
2022-04-23 06:59:38 -04:00
|
|
|
* @brief Enable GPTimer
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note This function will transit the timer state from "init" to "enable".
|
2022-07-13 01:17:32 -04:00
|
|
|
* @note This function will enable the interrupt service, if it's lazy installed in `gptimer_register_event_callbacks`.
|
2022-04-23 06:59:38 -04:00
|
|
|
* @note This function will acquire a PM lock, if a specific source clock (e.g. APB) is selected in the `gptimer_config_t`, while `CONFIG_PM_ENABLE` is enabled.
|
2022-07-13 01:17:32 -04:00
|
|
|
* @note Enable a timer doesn't mean to start it. See also `gptimer_start` for how to make the timer start counting.
|
2022-04-23 06:59:38 -04:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-04-23 06:59:38 -04:00
|
|
|
* @return
|
|
|
|
* - ESP_OK: Enable GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Enable GPTimer failed because of invalid argument
|
|
|
|
* - ESP_ERR_INVALID_STATE: Enable GPTimer failed because the timer is already enabled
|
|
|
|
* - ESP_FAIL: Enable GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_enable(gptimer_handle_t timer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disable GPTimer
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note This function will transit the timer state from "enable" to "init".
|
|
|
|
* @note This function will disable the interrupt service if it's installed.
|
|
|
|
* @note This function will release the PM lock if it's acquired in the `gptimer_enable`.
|
2022-07-13 01:17:32 -04:00
|
|
|
* @note Disable a timer doesn't mean to stop it. See also `gptimer_stop` for how to make the timer stop counting.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-04-23 06:59:38 -04:00
|
|
|
* @return
|
|
|
|
* - ESP_OK: Disable GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Disable GPTimer failed because of invalid argument
|
|
|
|
* - ESP_ERR_INVALID_STATE: Disable GPTimer failed because the timer is not enabled yet
|
|
|
|
* - ESP_FAIL: Disable GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_disable(gptimer_handle_t timer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Start GPTimer (internal counter starts counting)
|
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note This function will transit the timer state from "enable" to "run".
|
2022-01-02 03:14:17 -05:00
|
|
|
* @note This function is allowed to run within ISR context
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
|
|
|
|
* makes it possible to execute even when the Flash Cache is disabled.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @return
|
|
|
|
* - ESP_OK: Start GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Start GPTimer failed because of invalid argument
|
2023-03-06 00:32:35 -05:00
|
|
|
* - ESP_ERR_INVALID_STATE: Start GPTimer failed because the timer is not enabled or is already in running
|
2022-01-02 03:14:17 -05:00
|
|
|
* - ESP_FAIL: Start GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_start(gptimer_handle_t timer);
|
|
|
|
|
|
|
|
/**
|
2022-04-23 06:59:38 -04:00
|
|
|
* @brief Stop GPTimer (internal counter stops counting)
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note This function will transit the timer state from "run" to "enable".
|
2022-01-02 03:14:17 -05:00
|
|
|
* @note This function is allowed to run within ISR context
|
2023-03-06 00:32:35 -05:00
|
|
|
* @note If `CONFIG_GPTIMER_CTRL_FUNC_IN_IRAM` is enabled, this function will be placed in the IRAM by linker,
|
|
|
|
* makes it possible to execute even when the Flash Cache is disabled.
|
2022-01-02 03:14:17 -05:00
|
|
|
*
|
2022-07-13 01:17:32 -04:00
|
|
|
* @param[in] timer Timer handle created by `gptimer_new_timer`
|
2022-01-02 03:14:17 -05:00
|
|
|
* @return
|
|
|
|
* - ESP_OK: Stop GPTimer successfully
|
|
|
|
* - ESP_ERR_INVALID_ARG: Stop GPTimer failed because of invalid argument
|
2023-03-06 00:32:35 -05:00
|
|
|
* - ESP_ERR_INVALID_STATE: Stop GPTimer failed because the timer is not in running.
|
2022-01-02 03:14:17 -05:00
|
|
|
* - ESP_FAIL: Stop GPTimer failed because of other error
|
|
|
|
*/
|
|
|
|
esp_err_t gptimer_stop(gptimer_handle_t timer);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|