int main() { // Configure IO pins with default mode and selector TIMER_ISR::Init(); TIMER_TO_PROC1::Init(); PROC1::Init(); PROC2::Init(); PROC3::Init(); TIMER_HOOK::Init(); IDLE_HOOK::Init(); // Setup timer for TimerEvent generation // Timer 1, 25 MHz timer clock. // Interrupt rate slightly higher than 200 Hz for better scope view LPC_SC->PCONP |= CLKPWR_PCONP_PCTIM1; LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & ~(0x03 << CLKPWR_PCLKSEL_TIMER1)) | (CLKPWR_PCLKSEL_CCLK_DIV_4 << CLKPWR_PCLKSEL_TIMER1); LPC_TIM1->CTCR = 0; LPC_TIM1->PR = 0; LPC_TIM1->PC = 0; LPC_TIM1->TC = 0; LPC_TIM1->MR0 = SYSTICKFREQ / 4 / 200.4 - 1; LPC_TIM1->MCR = TIM_RESET_ON_MATCH(0) | TIM_INT_ON_MATCH(0); LPC_TIM1->EMR = TIM_EM_SET(0, TIM_EM_TOGGLE); LPC_TIM1->TCR = (1 << 0); NVIC->IP[ TIMER1_IRQn ] = 0x80; // Middle priority NVIC->ISER[0] = (1 << TIMER1_IRQn); // run OS::run(); }
/*********************************************************************//** * @brief Configuration for Match register * @param[in] TIMx Pointer to timer device * @param[in] TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type * - MatchChannel : choose channel 0 or 1 * - IntOnMatch : if SET, interrupt will be generated when MRxx match * the value in TC * - StopOnMatch : if SET, TC and PC will be stopped whenM Rxx match * the value in TC * - ResetOnMatch : if SET, Reset on MR0 when MRxx match * the value in TC * -ExtMatchOutputType: Select output for external match * + 0: Do nothing for external output pin if match * + 1: Force external output pin to low if match * + 2: Force external output pin to high if match * + 3: Toggle external output pin if match * MatchValue: Set the value to be compared with TC value * @return None **********************************************************************/ void TIM_ConfigMatch(TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType)); uint32_t timer = TIM_ConverPtrToTimeNum(TIMx) ; // TIMx->MR[TIM_MatchConfigStruct->MatchChannel] = TIM_MatchConfigStruct->MatchValue; switch(TIM_MatchConfigStruct->MatchChannel) { case 0: TIMx->MR0 = TIM_MatchConfigStruct->MatchValue; break; case 1: TIMx->MR1 = TIM_MatchConfigStruct->MatchValue; break; } //interrupt on MRn TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel); if (TIM_MatchConfigStruct->IntOnMatch) TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //reset on MRn if (TIM_MatchConfigStruct->ResetOnMatch) TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //stop on MRn if (TIM_MatchConfigStruct->StopOnMatch) TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); // match output type TIMx->EMR &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel); TIMx->EMR = TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType); //pin output configuration if (TIM_MatchConfigStruct->ExtMatchOutputType >0) { if ((timer <2)&& (TIM_MatchConfigStruct->MatchChannel < 2)) { PINSEL_ConfigPin((PINSEL_CFG_Type *)&timer_match_pin[2*timer +TIM_MatchConfigStruct->MatchChannel]); } if ((timer ==2)) { PINSEL_ConfigPin( (PINSEL_CFG_Type *)&timer_match_pin[2*timer + TIM_MatchConfigStruct->MatchChannel]); } if ((timer ==3)&&(TIM_MatchConfigStruct->MatchChannel < 2)) { PINSEL_ConfigPin( (PINSEL_CFG_Type *)&timer_match_pin[4*timer + TIM_MatchConfigStruct->MatchChannel]); } } }
/*********************************************************************//** * @brief Configuration for Match register * @param[in] TIMx Pointer to timer device, should be: * - LPC_TIM0: TIMER0 peripheral * - LPC_TIM1: TIMER1 peripheral * - LPC_TIM2: TIMER2 peripheral * - LPC_TIM3: TIMER3 peripheral * @param[in] TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type * - MatchChannel : choose channel 0 or 1 * - IntOnMatch : if SET, interrupt will be generated when MRxx match * the value in TC * - StopOnMatch : if SET, TC and PC will be stopped whenM Rxx match * the value in TC * - ResetOnMatch : if SET, Reset on MR0 when MRxx match * the value in TC * -ExtMatchOutputType: Select output for external match * + 0: Do nothing for external output pin if match * + 1: Force external output pin to low if match * + 2: Force external output pin to high if match * + 3: Toggle external output pin if match * MatchValue: Set the value to be compared with TC value * @return None **********************************************************************/ void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct) { CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType)); switch(TIM_MatchConfigStruct->MatchChannel) { case 0: TIMx->MR0 = TIM_MatchConfigStruct->MatchValue; break; case 1: TIMx->MR1 = TIM_MatchConfigStruct->MatchValue; break; case 2: TIMx->MR2 = TIM_MatchConfigStruct->MatchValue; break; case 3: TIMx->MR3 = TIM_MatchConfigStruct->MatchValue; break; default: //Error match value //Error loop while(1); break; } //interrupt on MRn TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel); if (TIM_MatchConfigStruct->IntOnMatch) TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //reset on MRn if (TIM_MatchConfigStruct->ResetOnMatch) TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //stop on MRn if (TIM_MatchConfigStruct->StopOnMatch) TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); // match output type TIMx->EMR &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel); TIMx->EMR |= TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType); }
/*********************************************************************//** * @brief Configuration for Match register * @param[in] TIMx Pointer to timer device * @param[in] TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type * - MatchChannel : choose channel 0 or 1 * - IntOnMatch : if SET, interrupt will be generated when MRxx match * the value in TC * - StopOnMatch : if SET, TC and PC will be stopped whenM Rxx match * the value in TC * - ResetOnMatch : if SET, Reset on MR0 when MRxx match * the value in TC * -ExtMatchOutputType: Select output for external match * + 0: Do nothing for external output pin if match * + 1: Force external output pin to low if match * + 2: Force external output pin to high if match * + 3: Toggle external output pin if match * MatchValue: Set the value to be compared with TC value * @return None **********************************************************************/ void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct) { uint32_t timer; CHECK_PARAM(PARAM_TIMx(TIMx)); CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType)); timer = TIM_ConverPtrToTimeNum(TIMx) ; switch(TIM_MatchConfigStruct->MatchChannel) { case 0: TIMx->MR0 = TIM_MatchConfigStruct->MatchValue; break; case 1: TIMx->MR1 = TIM_MatchConfigStruct->MatchValue; break; } //interrupt on MRn TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel); if (TIM_MatchConfigStruct->IntOnMatch) TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //reset on MRn if (TIM_MatchConfigStruct->ResetOnMatch) TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); //stop on MRn if (TIM_MatchConfigStruct->StopOnMatch) TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel); // TIMx->MCR = 0x02; // match output type TIMx->EMR &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel); TIMx->EMR = TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType); }