drivers: timer: extend nrf_rtc_timer

This commit introduces the following changes:

* nrf_rtc_timer is extended with a capability to handle RTC overflow,
allowing it to operate on absolute RTC ticks, rather than relative
ticks.
* overflow handling is ZLI-proof and relies on the sys clock
handler being executed twice every RTC counter's overflow.
* callbacks are given an absolute RTC tick value as a parameter instead
of CC register's value. The absolute RTC tick value is the RTC counter
value set during CC channel configuration extended to 64 bits.
* in case the timer's target time is in the past or is the current tick,
the timer fires as soon as possible, however still from the RTC's ISR
context.
* in case an active timer is set again with the same target time, it is
not scheduled again - only its event data is updated. Otherwise, the
timer is scheduled as usual.
* a scheduled timer can be aborted.
* system clock functions are now using 64 bit values internally.

Signed-off-by: Andrzej Kuroś <andrzej.kuros@nordicsemi.no>
Signed-off-by: Jedrzej Ciupis <jedrzej.ciupis@nordicsemi.no>
Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
Signed-off-by: Paweł Kwiek <pawel.kwiek@nordicsemi.no>
This commit is contained in:
Jedrzej Ciupis 2021-09-30 15:07:40 +02:00 committed by Carles Cufí
commit fcda8699cb
4 changed files with 474 additions and 136 deletions

View file

@ -11,8 +11,25 @@
extern "C" {
#endif
/** @brief Maximum allowed time span that is considered to be in the future.
*/
#define NRF_RTC_TIMER_MAX_SCHEDULE_SPAN BIT(23)
/** @brief RTC timer compare event handler.
*
* Called from RTC ISR context when processing a compare event.
*
* @param id Compare channel ID.
*
* @param expire_time An actual absolute expiration time set for a compare
* channel. It can differ from the requested target time
* and the difference can be used to determine whether the
* time set was delayed.
*
* @param user_data Pointer to a user context data.
*/
typedef void (*z_nrf_rtc_timer_compare_handler_t)(int32_t id,
uint32_t cc_value,
uint64_t expire_time,
void *user_data);
/** @brief Allocate RTC compare channel.
@ -30,11 +47,11 @@ int32_t z_nrf_rtc_timer_chan_alloc(void);
*/
void z_nrf_rtc_timer_chan_free(int32_t chan);
/** @brief Read current RTC counter value.
/** @brief Read current absolute time.
*
* @return Current RTC counter value.
* @return Current absolute time.
*/
uint32_t z_nrf_rtc_timer_read(void);
uint64_t z_nrf_rtc_timer_read(void);
/** @brief Get COMPARE event register address.
*
@ -76,38 +93,52 @@ uint32_t z_nrf_rtc_timer_compare_read(int32_t chan);
/** @brief Try to set compare channel to given value.
*
* Provided value is absolute and cannot be further in future than half span of
* the RTC counter. Function continouosly retries to set compare register until
* value that is written is far enough in the future and will generate an event.
* Because of that, compare register value may be different than the one
* requested. During this operation interrupt from that compare channel is
* disabled. Other interrupts are not locked during this operation.
*
* There is no option to abort the request once it is set. However, it can be
* overwritten.
* Provided value is absolute and cannot be further in the future than
* @c NRF_RTC_TIMER_MAX_SCHEDULE_SPAN. If given value is in the past then an RTC
* interrupt is triggered immediately. Otherwise function continuously retries
* to set compare register until value that is written is far enough in the
* future and will generate an event. Because of that, compare register value
* may be different than the one requested. During this operation interrupt
* from that compare channel is disabled. Other interrupts are not locked during
* this operation.
*
* @param chan Channel ID between 1 and CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT.
*
* @param cc_value Absolute value. Values which are further distanced from
* current counter value than half RTC span are considered in the past.
* @param target_time Absolute target time in ticks.
*
* @param handler User function called in the context of the RTC interrupt.
*
* @param user_data Data passed to the handler.
*
* @retval 0 if the compare channel was set successfully.
* @retval -EINVAL if provided target time was further than
* @c NRF_RTC_TIMER_MAX_SCHEDULE_SPAN ticks in the future.
*/
void z_nrf_rtc_timer_compare_set(int32_t chan, uint32_t cc_value,
z_nrf_rtc_timer_compare_handler_t handler,
void *user_data);
int z_nrf_rtc_timer_set(int32_t chan, uint64_t target_time,
z_nrf_rtc_timer_compare_handler_t handler,
void *user_data);
/** @brief Abort a timer requested with @ref z_nrf_rtc_timer_set.
*
* If an abort operation is performed too late it is still possible for an event
* to fire. The user can detect a spurious event by comparing absolute time
* provided in callback and a result of @ref z_nrf_rtc_timer_read. During this
* operation interrupt from that compare channel is disabled. Other interrupts
* are not locked during this operation.
*
* @param chan Channel ID between 1 and CONFIG_NRF_RTC_TIMER_USER_CHAN_COUNT.
*/
void z_nrf_rtc_timer_abort(int32_t chan);
/** @brief Convert system clock time to RTC ticks.
*
* @p t can be absolute or relative. @p t cannot be further from now than half
* of the RTC range (e.g. 256 seconds if RTC is running at 32768 Hz).
* @p t can be absolute or relative. @p t cannot be further into the future
* from now than the RTC range (e.g. 512 seconds if RTC is running at 32768 Hz).
*
* @retval Positive value represents @p t in RTC tick value.
* @retval -EINVAL if @p t is out of range.
*/
int z_nrf_rtc_timer_get_ticks(k_timeout_t t);
uint64_t z_nrf_rtc_timer_get_ticks(k_timeout_t t);
#ifdef __cplusplus
}