@@ -234,31 +234,28 @@ void common_rtc_set_interrupt(uint32_t us_timestamp, uint32_t cc_channel,
234
234
// the interrupt generation.
235
235
uint64_t current_time64 = common_rtc_64bit_us_get ();
236
236
// [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 ;
239
238
// [if the original timestamp value happens to be after the 32 bit counter
240
239
// of microsends overflows, correct the upper 32 bits accordingly]
241
- if (us_timestamp < ( uint32_t )( current_time64 & 0xFFFFFFFF ) ) {
240
+ if (timestamp64 < current_time64 ) {
242
241
timestamp64 += ((uint64_t )1 << 32 );
243
242
}
244
243
// [microseconds -> ticks, always round the result up to avoid too early
245
244
// 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 ));
249
246
250
247
core_util_critical_section_enter ();
251
248
// The COMPARE event occurs when the value in compare register is N and
252
249
// the counter value changes from N-1 to N. Therefore, the minimal safe
253
250
// difference between the compare value to be set and the current counter
254
251
// value is 2 ticks. This guarantees that the compare trigger is properly
255
252
// 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 ) {
258
255
compare_value = closest_safe_compare ;
259
256
}
260
257
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 );
262
259
nrf_rtc_event_enable (COMMON_RTC_INSTANCE , int_mask );
263
260
264
261
core_util_critical_section_exit ();
0 commit comments