I am working with the nRF52840 DK and was looking for a way to set a compare register to trigger at 300 ticks, and another to trigger at 40000 ticks. The register that triggers at 300 ticks should trigger an interrupt where I want to grab the current value of the 40000 tick register. In my implementation, I am triggering the interrupt with CC[0] and was hoping to capture the ticks of CC[1], however I believe I am doing this incorrectly as the timer seems to reset when I reach the interrupt and capture CC[1] so I get the same value every time. Is the timer resetting its count every time it reaches the interrupt, and if so would I need to implement a separate timer to achieve my goal?
#define FREE_TIMER NRF_TIMER4 #define FREE_TIMER_IRQn TIMER4_IRQn #define FREE_TIMER_IRQHandler TIMER4_IRQHandler static void freeTimerInit(void) { FREE_TIMER->TASKS_STOP = 1; // Stop the timer FREE_TIMER->TASKS_CLEAR = 1; // Clear the timer FREE_TIMER->BITMODE = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos; // Ensure the timer uses 32-bit bitmode or higher FREE_TIMER->PRESCALER = 0; // Set the prescaler to 0, for a timer interval of 0.06 us (16M / 2^0) FREE_TIMER->CC[0] = 300; // Set the CC[0] register to hit every 18 us (to check sample skip status) FREE_TIMER->CC[1] = 40000; FREE_TIMER->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk; // Make sure the timer clears after reaching CC[0] FREE_TIMER->INTENSET = TIMER_INTENSET_COMPARE0_Msk; // Trigger the interrupt when reaching CC[0] NVIC_SetPriority(FREE_TIMER_IRQn, 7); // Set a low IRQ priority and enable interrupts for the timer module NVIC_EnableIRQ(FREE_TIMER_IRQn); FREE_TIMER->TASKS_START = 1; // Start the timer } uint32_t freeGetTicks(uint8_t chn) { FREE_TIMER->TASKS_CAPTURE[chn] = 1; return FREE_TIMER->CC[chn]; } void FREE_TIMER_IRQHandler(void) { sync_ticks = ts_timestamp_get_ticks_u32(7); free_ticks = freeGetTicks(1); if (FREE_TIMER->EVENTS_COMPARE[0]) { FREE_TIMER->EVENTS_COMPARE[0] = 0; } }