Skip to content

Commit a98316b

Browse files
ananglcarlescufi
authored andcommitted
drivers: nrf_rtc_timer: Fix checking of maximum timeout value
Align the condition checked in compare_set_no_locks() with what set_absolute_alarm() actually provides (and slightly correct the latter function so that it provides what it is supposed to). Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent e5aa0ab commit a98316b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

drivers/timer/nrf_rtc_timer.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ uint64_t z_nrf_rtc_timer_get_ticks(k_timeout_t t)
219219

220220
/** @brief Function safely sets absolute alarm.
221221
*
222-
* It assumes that provided value is less than COUNTER_HALF_SPAN from now.
222+
* It assumes that provided value is at most COUNTER_HALF_SPAN cycles from now.
223223
* It detects late setting and also handle +1 cycle case.
224224
*
225225
* @param[in] chan A channel for which a new CC value is to be set.
@@ -228,6 +228,11 @@ uint64_t z_nrf_rtc_timer_get_ticks(k_timeout_t t)
228228
*/
229229
static void set_absolute_alarm(int32_t chan, uint32_t abs_val)
230230
{
231+
/* Ensure that the value exposed in this driver API is consistent with
232+
* assumptions of this function.
233+
*/
234+
BUILD_ASSERT(NRF_RTC_TIMER_MAX_SCHEDULE_SPAN <= COUNTER_HALF_SPAN);
235+
231236
uint32_t cc_val = abs_val & COUNTER_MAX;
232237
uint32_t cc_inc = 2;
233238

@@ -267,7 +272,7 @@ static void set_absolute_alarm(int32_t chan, uint32_t abs_val)
267272
* But if the COMPARE event turns out to be already generated,
268273
* there is obviously no need to continue the loop.
269274
*/
270-
if ((counter_sub(cc_val, now + 2) > COUNTER_HALF_SPAN) &&
275+
if ((counter_sub(cc_val, now + 2) > (COUNTER_HALF_SPAN - 2)) &&
271276
!event_check(chan)) {
272277
cc_val = now + cc_inc;
273278
cc_inc++;
@@ -286,7 +291,7 @@ static int compare_set_nolocks(int32_t chan, uint64_t target_time,
286291
uint64_t curr_time = z_nrf_rtc_timer_read();
287292

288293
if (curr_time < target_time) {
289-
if (target_time - curr_time > COUNTER_SPAN) {
294+
if (target_time - curr_time > COUNTER_HALF_SPAN) {
290295
/* Target time is too distant. */
291296
return -EINVAL;
292297
}

0 commit comments

Comments
 (0)