void timerDelayMs(uint32_t timems) { /* In an RTOS, the thread would sleep allowing other threads to run. For standalone operation, we just spin on RI timer */ int32_t curr = (int32_t) Chip_RIT_GetCounter(LPC_RITIMER); int32_t final = curr + ((SystemCoreClock / 1000) * timems); if (final == curr) return; if ((final < 0) && (curr > 0)) { while (Chip_RIT_GetCounter(LPC_RITIMER) < (uint32_t) final) { } } else { while ((int32_t) Chip_RIT_GetCounter(LPC_RITIMER) < final) {
/** * @brief RITimer IRQ handler and timebase management * @return Nothing * @note This function keeps a timebase for LWIP that can be * used for other functions. */ void OSEK_ISR_RIT_IRQHandler(void) { /* Clear RITimer Interrupt, Reload counter value */ Chip_RIT_ClearInt(LPC_RITIMER); Chip_RIT_SetCOMPVAL(LPC_RITIMER, Chip_RIT_GetCounter(LPC_RITIMER) + reload_val);/* Reload value */ /* Increment tick count */ systick_timems += saved_period; }
/** * @brief Initialize tick interrupts * @return Nothing * @note This function initializes the tick interrupts, and must * be called before starting the freeRTOS scheduler. */ void prvSetupTimerInterrupt(void) { /* Clear any pending interrupt */ Chip_RIT_ClearInt(LPC_RITIMER); /* Calculate reload value */ reload_val = (configCPU_CLOCK_HZ / configTICK_RATE_HZ); Chip_RIT_SetCOMPVAL(LPC_RITIMER, Chip_RIT_GetCounter(LPC_RITIMER) + reload_val);/* Start tick */ /* Set the priority and enable the interrupt */ NVIC_SetPriority((IRQn_Type) RITIMER_IRQn, portNVIC_SYSTICK_PRI); NVIC_EnableIRQ((IRQn_Type) RITIMER_IRQn); }
/* Enable LWIP tick and interrupt */ void lwipSysTick_Enable(uint32_t period) { saved_period = period; /* Clear any pending interrupt */ Chip_RIT_ClearInt(LPC_RITIMER); /* Calculate reload value */ reload_val = ( SystemCoreClock / ( 1000 / period ) ); Chip_RIT_SetCOMPVAL(LPC_RITIMER, Chip_RIT_GetCounter(LPC_RITIMER) + reload_val);/* Let it tick */ /* Set the priority and enable the interrupt */ NVIC_SetPriority((IRQn_Type) RITIMER_IRQn, RITIMER_IRQn_PRI); NVIC_EnableIRQ((IRQn_Type) RITIMER_IRQn); }
/** * @brief RITimer IRQ handler and timebase management * @return Nothing * @note This function keeps a timebase for LWIP that can be * used for other functions. */ void RIT_IRQHandler(void) { #define CONTEXT_ISR2 3 typedef uint8_t ContextType; extern ContextType ActualContext; ContextType ctx = ActualContext; ActualContext = CONTEXT_ISR2; /* Clear RITimer Interrupt, Reload counter value */ Chip_RIT_ClearInt(LPC_RITIMER); Chip_RIT_SetCOMPVAL(LPC_RITIMER, Chip_RIT_GetCounter(LPC_RITIMER) + reload_val);/* Reload value */ /* Increment tick count */ systick_timems += saved_period; ActualContext = ctx; }
/** * @brief Tick interrupt handler routine * @return Nothing * @note This function handles the tick interrupts that are generated by RITIMER. */ void xPortSysTickHandler(void) { unsigned long ulDummy; /* TODO: check if WWDT interrupt and redirect */ Chip_RIT_ClearInt(LPC_RITIMER); Chip_RIT_SetCOMPVAL(LPC_RITIMER, Chip_RIT_GetCounter(LPC_RITIMER) + reload_val);/* Reload value */ #if configUSE_PREEMPTION == 1 /* If using preemption, also force a context switch. */ *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET; #endif ulDummy = portSET_INTERRUPT_MASK_FROM_ISR(); { vTaskIncrementTick(); } portCLEAR_INTERRUPT_MASK_FROM_ISR(ulDummy); }
/** * @brief Tick interrupt handler routine * @return Nothing * @note This function handles the tick interrupts that are generated by RITIMER. */ void RIT_IRQHandler(void) { unsigned long ulPreviousMask; /* TODO: check if WWDT interrupt and redirect */ Chip_RIT_ClearInt(LPC_RITIMER); Chip_RIT_SetCOMPVAL(LPC_RITIMER, Chip_RIT_GetCounter(LPC_RITIMER) + reload_val);/* Reload value */ #if configUSE_PREEMPTION == 1 /* If using preemption, also force a context switch. */ *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET; #endif ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR(); { /* Increment the RTOS tick. */ if( xTaskIncrementTick() != pdFALSE ) { /* Pend a context switch. */ *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET; } } portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask ); }