From 5806e577733444c8008b8a86b6be95b6df1c1107 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Mon, 8 Nov 2021 14:53:52 +0530 Subject: [PATCH] esp_timer: Added esp_timer_get_period/expiry_time APIs Added the following new APIs to the esp_timer module: - esp_timer_get_period(): Returns the period of a timer in microseconds. - esp_timer_get_expiry_time(): Returns the timeout value of a one-shot timer in microseconds. Signed-off-by: Sudeep Mohanty --- components/esp_timer/include/esp_timer.h | 33 ++++++++++++++++++++++ components/esp_timer/src/esp_timer.c | 35 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/components/esp_timer/include/esp_timer.h b/components/esp_timer/include/esp_timer.h index eb4c6d707a..e7e5ad9bb8 100644 --- a/components/esp_timer/include/esp_timer.h +++ b/components/esp_timer/include/esp_timer.h @@ -211,6 +211,39 @@ int64_t esp_timer_get_next_alarm(void); */ int64_t esp_timer_get_next_alarm_for_wake_up(void); +/** + * @brief Get the period of a timer + * + * This function fetches the timeout period of a timer. + * + * @note The timeout period is the time interval with which a timer restarts after expiry. For one-shot timers, the + * period is 0 as there is no periodicity associated with such timers. + * + * @param timer timer handle allocated using esp_timer_create + * @param period memory to store the timer period value in microseconds + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG if the arguments are invalid + */ +esp_err_t esp_timer_get_period(esp_timer_handle_t timer, uint64_t *period); + +/** + * @brief Get the expiry time of a one-shot timer + * + * This function fetches the expiry time of a one-shot timer. + * + * @note This API returns a valid expiry time only for a one-shot timer. It returns an error if the timer handle passed + * to the function is for a periodic timer. + * + * @param timer timer handle allocated using esp_timer_create + * @param expiry memory to store the timeout value in microseconds + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG if the arguments are invalid + * - ESP_ERR_NOT_SUPPORTED if the timer type is periodic + */ +esp_err_t esp_timer_get_expiry_time(esp_timer_handle_t timer, uint64_t *expiry); + /** * @brief Dump the list of timers to a stream * diff --git a/components/esp_timer/src/esp_timer.c b/components/esp_timer/src/esp_timer.c index 0ad6bb76f9..86b279de76 100644 --- a/components/esp_timer/src/esp_timer.c +++ b/components/esp_timer/src/esp_timer.c @@ -623,6 +623,41 @@ int64_t IRAM_ATTR esp_timer_get_next_alarm_for_wake_up(void) return next_alarm; } +esp_err_t IRAM_ATTR esp_timer_get_period(esp_timer_handle_t timer, uint64_t *period) +{ + if (timer == NULL || period == NULL) { + return ESP_ERR_INVALID_ARG; + } + + esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; + + timer_list_lock(dispatch_method); + *period = timer->period; + timer_list_unlock(dispatch_method); + + return ESP_OK; +} + +esp_err_t IRAM_ATTR esp_timer_get_expiry_time(esp_timer_handle_t timer, uint64_t *expiry) +{ + if (timer == NULL || expiry == NULL) { + return ESP_ERR_INVALID_ARG; + } + + if (timer->period > 0) { + /* Return error for periodic timers */ + return ESP_ERR_NOT_SUPPORTED; + } + + esp_timer_dispatch_t dispatch_method = timer->flags & FL_ISR_DISPATCH_METHOD; + + timer_list_lock(dispatch_method); + *expiry = timer->alarm; + timer_list_unlock(dispatch_method); + + return ESP_OK; +} + bool esp_timer_is_active(esp_timer_handle_t timer) { return timer_armed(timer);