Skip to content

Commit b8f22bb

Browse files
author
Marcus Chang
committed
Fix us_ticker for NRF52 series
Changed comparison function when setting ticker timeout to fix tickers not set correctly.
1 parent d08c819 commit b8f22bb

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/us_ticker.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,31 +234,28 @@ void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
234234
// the interrupt generation.
235235
uint64_t current_time64 = common_rtc_64bit_us_get();
236236
// [add upper 32 bits from the current time to the timestamp value]
237-
uint64_t timestamp64 = us_timestamp +
238-
(current_time64 & ~(uint64_t)0xFFFFFFFF);
237+
uint64_t timestamp64 = (current_time64 & ~(uint64_t)0xFFFFFFFF) | us_timestamp;
239238
// [if the original timestamp value happens to be after the 32 bit counter
240239
// of microsends overflows, correct the upper 32 bits accordingly]
241-
if (us_timestamp < (uint32_t)(current_time64 & 0xFFFFFFFF)) {
240+
if (timestamp64 < current_time64) {
242241
timestamp64 += ((uint64_t)1 << 32);
243242
}
244243
// [microseconds -> ticks, always round the result up to avoid too early
245244
// interrupt generation]
246-
uint32_t compare_value =
247-
(uint32_t)CEIL_DIV((timestamp64) * RTC_INPUT_FREQ, 1000000);
248-
245+
uint32_t compare_value = RTC_WRAP((uint32_t)CEIL_DIV((timestamp64) * RTC_INPUT_FREQ, 1000000));
249246

250247
core_util_critical_section_enter();
251248
// The COMPARE event occurs when the value in compare register is N and
252249
// the counter value changes from N-1 to N. Therefore, the minimal safe
253250
// difference between the compare value to be set and the current counter
254251
// value is 2 ticks. This guarantees that the compare trigger is properly
255252
// setup before the compare condition occurs.
256-
uint32_t closest_safe_compare = common_rtc_32bit_ticks_get() + 2;
257-
if ((int)(compare_value - closest_safe_compare) <= 0) {
253+
uint32_t closest_safe_compare = RTC_WRAP(common_rtc_32bit_ticks_get() + 2);
254+
if (closest_safe_compare - compare_value < 2) {
258255
compare_value = closest_safe_compare;
259256
}
260257

261-
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, RTC_WRAP(compare_value));
258+
nrf_rtc_cc_set(COMMON_RTC_INSTANCE, cc_channel, compare_value);
262259
nrf_rtc_event_enable(COMMON_RTC_INSTANCE, int_mask);
263260

264261
core_util_critical_section_exit();

0 commit comments

Comments
 (0)