void xPortSysTickHandler( void ) { nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_TICK); #if configUSE_TICKLESS_IDLE == 1 nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0); #endif uint32_t isrstate = portSET_INTERRUPT_MASK_FROM_ISR(); do{ #if configUSE_TICKLESS_IDLE == 1 TickType_t diff; TickType_t actualTicks = xTaskGetTickCountFromISR(); TickType_t hwTicks = nrf_rtc_counter_get(portNRF_RTC_REG); diff = (hwTicks - actualTicks) & portNRF_RTC_MAXTICKS; if(diff <= 0) { break; } if(diff > 2) // Correct internat ticks { vTaskStepTick(diff - 1); } #endif /* Increment the RTOS tick. */ if( xTaskIncrementTick() != pdFALSE ) { /* A context switch is required. Context switching is performed in the PendSV interrupt. Pend the PendSV interrupt. */ SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; __SEV(); } }while(0); portCLEAR_INTERRUPT_MASK_FROM_ISR( isrstate ); }
void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) { TickType_t wakeupTime; /* Make sure the SysTick reload value does not overflow the counter. */ if( xExpectedIdleTime > portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP ) { xExpectedIdleTime = portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP; } /* Block the scheduler now */ portDISABLE_INTERRUPTS(); /* Stop tick events */ nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK); /* Configure CTC interrupt */ wakeupTime = nrf_rtc_counter_get(portNRF_RTC_REG) + xExpectedIdleTime; wakeupTime &= portNRF_RTC_MAXTICKS; nrf_rtc_cc_set(portNRF_RTC_REG, 0, wakeupTime); nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0); nrf_rtc_int_enable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK); if( eTaskConfirmSleepModeStatus() == eAbortSleep ) { portENABLE_INTERRUPTS(); } else { TickType_t xModifiableIdleTime = xExpectedIdleTime; configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); if( xModifiableIdleTime > 0 ) { __DSB(); #ifdef SOFTDEVICE_PRESENT /* With SD there is no problem with possibility of interrupt lost. * every interrupt is counted and the counter is processed inside * sd_app_evt_wait function. */ portENABLE_INTERRUPTS(); sd_app_evt_wait(); #else /* No SD - we would just block interrupts globally. * BASEPRI cannot be used for that because it would prevent WFE from wake up. */ __disable_irq(); portENABLE_INTERRUPTS(); do{ __WFE(); } while(0 == (NVIC->ISPR[0] | NVIC->ISPR[1])); __enable_irq(); #endif } configPOST_SLEEP_PROCESSING( xExpectedIdleTime ); portENABLE_INTERRUPTS(); } // We can do operations below safely, because when we are inside vPortSuppressTicksAndSleep // scheduler is already suspended. nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK); nrf_rtc_int_enable (portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK); }
uint32_t common_rtc_32bit_ticks_get(void) { uint32_t ticks = nrf_rtc_counter_get(COMMON_RTC_INSTANCE); // The counter used for time measurements is less than 32 bit wide, // so its value is complemented with the number of registered overflows // of the counter. ticks += (m_common_rtc_overflows << RTC_COUNTER_BITS); return ticks; }
nrfx_err_t nrfx_rtc_cc_set(nrfx_rtc_t const * const p_instance, uint32_t channel, uint32_t val, bool enable_irq) { NRFX_ASSERT(m_cb[p_instance->instance_id].state != NRFX_DRV_STATE_UNINITIALIZED); NRFX_ASSERT(channel<p_instance->cc_channel_count); nrfx_err_t err_code; uint32_t int_mask = RTC_CHANNEL_INT_MASK(channel); nrf_rtc_event_t event = RTC_CHANNEL_EVENT_ADDR(channel); nrf_rtc_event_disable(p_instance->p_reg, int_mask); nrf_rtc_int_disable(p_instance->p_reg, int_mask); val = RTC_WRAP(val); if (m_cb[p_instance->instance_id].reliable) { nrf_rtc_cc_set(p_instance->p_reg,channel,val); uint32_t cnt = nrf_rtc_counter_get(p_instance->p_reg); int32_t diff = cnt - val; if (cnt < val) { diff += RTC_COUNTER_COUNTER_Msk; } if (diff < m_cb[p_instance->instance_id].tick_latency) { err_code = NRFX_ERROR_TIMEOUT; NRFX_LOG_WARNING("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code)); return err_code; } } else { nrf_rtc_cc_set(p_instance->p_reg,channel,val); } if (enable_irq) { nrf_rtc_event_clear(p_instance->p_reg,event); nrf_rtc_int_enable(p_instance->p_reg, int_mask); } nrf_rtc_event_enable(p_instance->p_reg,int_mask); NRFX_LOG_INFO("RTC id: %d, channel enabled: %lu, compare value: %lu.", p_instance->instance_id, channel, val); err_code = NRFX_SUCCESS; NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code)); return err_code; }
inline uint64_t nrf5AlarmGetCurrentTime() { uint32_t rtcValue1; uint32_t rtcValue2; uint32_t offset; rtcValue1 = nrf_rtc_counter_get(RTC_INSTANCE); __DMB(); offset = sTimeOffset; __DMB(); rtcValue2 = nrf_rtc_counter_get(RTC_INSTANCE); if ((rtcValue2 < rtcValue1) || (rtcValue1 == 0)) { // Overflow detected. Additional condition (rtcValue1 == 0) covers situation when overflow occurred in // interrupt state, before this function was entered. But in general, this function shall not be called // from interrupt other than alarm interrupt. // Wait at least 20 cycles, to ensure that if interrupt is going to be called, it will be called now. for (uint32_t i = 0; i < 4; i++) { __NOP(); __NOP(); __NOP(); } // If the event flag is still on, it means that the interrupt was not called, as we are in interrupt state. if (nrf_rtc_event_pending(RTC_INSTANCE, NRF_RTC_EVENT_OVERFLOW)) { HandleOverflow(); } offset = sTimeOffset; } return US_PER_MS * (uint64_t)offset + TicksToTime(rtcValue2); }
void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) { TickType_t wakeupTime; /* Make sure the SysTick reload value does not overflow the counter. */ if( xExpectedIdleTime > portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP ) { xExpectedIdleTime = portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP; } /* Block the scheduler now */ portDISABLE_INTERRUPTS(); /* Stop tick events */ nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK); /* Configure CTC interrupt */ wakeupTime = nrf_rtc_counter_get(portNRF_RTC_REG) + xExpectedIdleTime; wakeupTime &= portNRF_RTC_MAXTICKS; nrf_rtc_cc_set(portNRF_RTC_REG, 0, wakeupTime); nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0); nrf_rtc_int_enable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK); if( eTaskConfirmSleepModeStatus() == eAbortSleep ) { portENABLE_INTERRUPTS(); } else { TickType_t xModifiableIdleTime = xExpectedIdleTime; configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); if( xModifiableIdleTime > 0 ) { __DSB(); do{ __WFE(); } while(0 == (NVIC->ISPR[0])); } configPOST_SLEEP_PROCESSING( xExpectedIdleTime ); portENABLE_INTERRUPTS(); } // We can do operations below safely, because when we are inside vPortSuppressTicksAndSleep // scheduler is already suspended. nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK); nrf_rtc_int_enable (portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK); }
ret_code_t nrf_drv_rtc_cc_set(nrf_drv_rtc_t const * const p_instance, uint32_t channel, uint32_t val, bool enable_irq) { ASSERT(m_cb[p_instance->instance_id].state != NRF_DRV_STATE_UNINITIALIZED); ASSERT(channel<p_instance->cc_channel_count); uint32_t int_mask = RTC_CHANNEL_INT_MASK(channel); nrf_rtc_event_t event = RTC_CHANNEL_EVENT_ADDR(channel); nrf_rtc_event_disable(p_instance->p_reg, int_mask); nrf_rtc_int_disable(p_instance->p_reg, int_mask); val = RTC_WRAP(val); if (m_cb[p_instance->instance_id].reliable) { nrf_rtc_cc_set(p_instance->p_reg,channel,val); uint32_t cnt = nrf_rtc_counter_get(p_instance->p_reg); int32_t diff = cnt - val; if (cnt < val) { diff += RTC_COUNTER_COUNTER_Msk; } if (diff < m_cb[p_instance->instance_id].tick_latency) { return NRF_ERROR_TIMEOUT; } } else { nrf_rtc_cc_set(p_instance->p_reg,channel,val); } if (enable_irq) { nrf_rtc_event_clear(p_instance->p_reg,event); nrf_rtc_int_enable(p_instance->p_reg, int_mask); } nrf_rtc_event_enable(p_instance->p_reg,int_mask); return NRF_SUCCESS; }
void xPortSysTickHandler( void ) { nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_TICK); #if configUSE_TICKLESS_IDLE == 1 nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0); #endif /* The SysTick runs at the lowest interrupt priority, so when this interrupt executes all interrupts must be unmasked. There is therefore no need to save and then restore the interrupt mask value as its value is already known. */ ( void ) portSET_INTERRUPT_MASK_FROM_ISR(); do{ #if configUSE_TICKLESS_IDLE == 1 TickType_t diff; TickType_t actualTicks = xTaskGetTickCountFromISR(); TickType_t hwTicks = nrf_rtc_counter_get(portNRF_RTC_REG); diff = (hwTicks - actualTicks) & portNRF_RTC_MAXTICKS; if(diff <= 0) { break; } if(diff > 2) // Correct internat ticks { vTaskStepTick(diff - 1); } #endif /* Increment the RTOS tick. */ if( xTaskIncrementTick() != pdFALSE ) { /* A context switch is required. Context switching is performed in the PendSV interrupt. Pend the PendSV interrupt. */ SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; __SEV(); } }while(0); portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 ); }
static inline u32_t counter(void) { return nrf_rtc_counter_get(RTC); }
void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) { /* * Implementation note: * * To help debugging the option configUSE_TICKLESS_IDLE_SIMPLE_DEBUG was presented. * This option would make sure that even if program execution was stopped inside * this function no more than expected number of ticks would be skipped. * * Normally RTC works all the time even if firmware execution was stopped * and that may lead to skipping too much of ticks. */ TickType_t enterTime; /* Make sure the SysTick reload value does not overflow the counter. */ if ( xExpectedIdleTime > portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP ) { xExpectedIdleTime = portNRF_RTC_MAXTICKS - configEXPECTED_IDLE_TIME_BEFORE_SLEEP; } /* Block the scheduler now */ portDISABLE_INTERRUPTS(); /* Configure CTC interrupt */ enterTime = nrf_rtc_counter_get(portNRF_RTC_REG); if ( eTaskConfirmSleepModeStatus() == eAbortSleep ) { portENABLE_INTERRUPTS(); } else { TickType_t xModifiableIdleTime; TickType_t wakeupTime = (enterTime + xExpectedIdleTime) & portNRF_RTC_MAXTICKS; /* Stop tick events */ nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK); /* Configure CTC interrupt */ nrf_rtc_cc_set(portNRF_RTC_REG, 0, wakeupTime); nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_COMPARE_0); nrf_rtc_int_enable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK); __DSB(); /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can * set its parameter to 0 to indicate that its implementation contains * its own wait for interrupt or wait for event instruction, and so wfi * should not be executed again. However, the original expected idle * time variable must remain unmodified, so a copy is taken. */ xModifiableIdleTime = xExpectedIdleTime; configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); if ( xModifiableIdleTime > 0 ) { #ifdef SOFTDEVICE_PRESENT sd_app_evt_wait(); #else do{ __WFE(); } while (0 == (NVIC->ISPR[0])); #endif } configPOST_SLEEP_PROCESSING( xExpectedIdleTime ); nrf_rtc_int_disable(portNRF_RTC_REG, NRF_RTC_INT_COMPARE0_MASK); portENABLE_INTERRUPTS(); /* Correct the system ticks */ portENTER_CRITICAL(); { TickType_t diff; TickType_t hwTicks = nrf_rtc_counter_get(portNRF_RTC_REG); nrf_rtc_event_clear(portNRF_RTC_REG, NRF_RTC_EVENT_TICK); nrf_rtc_int_enable (portNRF_RTC_REG, NRF_RTC_INT_TICK_MASK); if(enterTime > hwTicks) { hwTicks += portNRF_RTC_MAXTICKS + 1U; } diff = (hwTicks - enterTime); if((configUSE_TICKLESS_IDLE_SIMPLE_DEBUG) && (diff > xExpectedIdleTime)) { diff = xExpectedIdleTime; } if (diff > 0) { vTaskStepTick(diff); } } portEXIT_CRITICAL(); } }