/** * \brief Initializes SysTick. */ static void _hal_tcInit(void) { uint32_t l_ticks; TIMER_TypeDef* ps_timer = TIMER1; TIMER_Init_TypeDef s_timerInit = TIMER_INIT_DEFAULT; s_timerInit.enable = true; s_timerInit.prescale = timerPrescale2; s_timerInit.riseAction = timerInputActionReloadStart; /* caluclate ticks */ l_ticks = SystemHFClockGet() / 2 / 1000; /* configure timer for 1ms */ TIMER_TopSet( ps_timer, l_ticks ); /* enable timer interrupts */ NVIC_DisableIRQ( TIMER1_IRQn ); NVIC_ClearPendingIRQ( TIMER1_IRQn ); NVIC_EnableIRQ( TIMER1_IRQn ); TIMER_IntEnable( ps_timer, TIMER_IEN_OF ); /* initialize and start timer */ TIMER_Init( ps_timer, &s_timerInit ); } /* _hal_tcInit() */
/***************************************************************************//** * @brief * Activate the hardware timer used to pace the 1 millisecond timer system. * * @details * Call this function whenever the HFPERCLK frequency is changed. * This function is initially called by HOST and DEVICE stack xxxx_Init() * functions. ******************************************************************************/ void USBTIMER_Init( void ) { uint32_t freq; TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT; TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT; freq = CMU_ClockFreqGet( cmuClock_HFPER ); ticksPrMs = ( freq + 500 ) / 1000; ticksPr1us = ( freq + 500000 ) / 1000000; ticksPr10us = ( freq + 50000 ) / 100000; ticksPr100us = ( freq + 5000 ) / 10000; timerCCInit.mode = timerCCModeCompare; CMU_ClockEnable( TIMER_CLK, true ); TIMER_TopSet( TIMER, 0xFFFF ); TIMER_InitCC( TIMER, 0, &timerCCInit ); TIMER_Init( TIMER, &timerInit ); #if ( NUM_QTIMERS > 0 ) TIMER_IntClear( TIMER, 0xFFFFFFFF ); TIMER_IntEnable( TIMER, TIMER_IEN_CC0 ); TIMER_CompareSet( TIMER, 0, TIMER_CounterGet( TIMER ) + ticksPrMs ); NVIC_ClearPendingIRQ( TIMER_IRQ ); NVIC_EnableIRQ( TIMER_IRQ ); #endif /* ( NUM_QTIMERS > 0 ) */ }
void initTimer() { TIMER_Init_TypeDef initValues = TIMER_INIT_DEFAULT; /* Enable clock for TIMER0 */ CMU_ClockEnable(cmuClock_TIMER0, true); CMU_ClockEnable(cmuClock_I2C0, true); /* Enable underflow and overflow interrupt for TIMER0*/ TIMER_IntEnable(TIMER0, TIMER_IF_OF); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER0_IRQn); /* Set TIMER0 Top value */ //TIMER_TopSet(TIMER0, 27342); TIMER_TopSet(TIMER0, 7000); /* Initialize TIMER0 in Up mode with 1024x prescaling */ initValues.prescale = timerPrescale1024; initValues.mode = timerModeUp; TIMER_Init(TIMER0, &initValues); /* Start TIMER0 */ TIMER0->CMD = TIMER_CMD_START; }
int timer_init(tim_t dev, unsigned long freq, timer_cb_t callback, void *arg) { TIMER_TypeDef *pre, *tim; /* test if given timer device is valid */ if (dev >= TIMER_NUMOF) { return -1; } /* save callback */ isr_ctx[dev].cb = callback; /* get timers */ pre = timer_config[dev].prescaler.dev; tim = timer_config[dev].timer.dev; /* enable clocks */ CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(timer_config[dev].prescaler.cmu, true); CMU_ClockEnable(timer_config[dev].timer.cmu, true); /* reset and initialize peripherals */ EFM32_CREATE_INIT(init_pre, TIMER_Init_TypeDef, TIMER_INIT_DEFAULT, .conf.enable = false, .conf.prescale = timerPrescale1 ); EFM32_CREATE_INIT(init_tim, TIMER_Init_TypeDef, TIMER_INIT_DEFAULT, .conf.enable = false, .conf.clkSel = timerClkSelCascade ); TIMER_Reset(tim); TIMER_Reset(pre); TIMER_Init(tim, &init_tim.conf); TIMER_Init(pre, &init_pre.conf); /* configure the prescaler top value */ uint32_t freq_timer = CMU_ClockFreqGet(timer_config[dev].prescaler.cmu); uint32_t top = ( freq_timer / TIMER_Prescaler2Div(init_pre.conf.prescale) / freq) - 1; TIMER_TopSet(pre, top); TIMER_TopSet(tim, 0xffff); /* enable interrupts for the channels */ TIMER_IntClear(tim, TIMER_IFC_CC0 | TIMER_IFC_CC1 | TIMER_IFC_CC2); TIMER_IntEnable(tim, TIMER_IEN_CC0 | TIMER_IEN_CC1 | TIMER_IEN_CC2); NVIC_ClearPendingIRQ(timer_config[dev].irq); NVIC_EnableIRQ(timer_config[dev].irq); /* start the timers */ TIMER_Enable(tim, true); TIMER_Enable(pre, true); return 0; }
void bsp_tick_timer_start(void) { // Enable the interrupt and start the timer TIMER_IntEnable(TIMER0, TIMER_IF_OF); NVIC_SetPriority(TIMER0_IRQn, 1); NVIC_EnableIRQ(TIMER0_IRQn); TIMER_Enable(TIMER0, true); }
void us_ticker_set_interrupt(timestamp_t timestamp) { TIMER_IntDisable(US_TICKER_TIMER, TIMER_IEN_CC0); TIMER_IntClear(US_TICKER_TIMER, TIMER_IEN_CC0); TIMER_CompareSet(US_TICKER_TIMER, 0, timestamp); TIMER_IntEnable(US_TICKER_TIMER, TIMER_IEN_CC0); }
//================================================================================ // TIMER1_enter_DefaultMode_from_RESET //================================================================================ extern void TIMER1_enter_DefaultMode_from_RESET(void) { // $[TIMER1 initialization] TIMER_Init_TypeDef init = TIMER_INIT_DEFAULT; init.enable = 0;//初始化完成后不使能 init.debugRun = 0; init.dmaClrAct = 0; init.sync = 0; init.clkSel = timerClkSelHFPerClk;//HFPERCLK时钟 init.prescale = timerPrescale2; //2分频 init.fallAction = timerInputActionNone; init.riseAction = timerInputActionNone; init.mode = timerModeUp; init.quadModeX4 = 0; init.oneShot = 0; // [TIMER1 initialization]$ // $[TIMER1 CC0 init] TIMER_InitCC_TypeDef initCC0 = TIMER_INITCC_DEFAULT; initCC0.prsInput = false; initCC0.edge = timerEdgeBoth; initCC0.mode = timerCCModePWM; initCC0.eventCtrl = timerEventEveryEdge; initCC0.filter = 0; initCC0.cofoa = timerOutputActionNone; initCC0.cufoa = timerOutputActionNone; initCC0.cmoa = timerOutputActionToggle; initCC0.coist = 0; initCC0.outInvert = 0; TIMER_InitCC(TIMER1, 0, &initCC0); // [TIMER1 CC0 init]$ /* Route CC0 to location 0 (PC11) and enable pin */ TIMER1->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_LOCATION_LOC0); /* Set Top Value */ TIMER_TopSet(TIMER0, 8000000/PWM_FREQ); //PWM频率设定,此处8M是HFPERCLK频率,待定 /* Set compare value starting at 0 - it will be incremented in the interrupt handler */ TIMER_CompareBufSet(TIMER0, 0, 0); /* Enable overflow interrupt */ TIMER_IntEnable(TIMER0, TIMER_IF_OF); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER0_IRQn); TIMER_Init(TIMER1, &init); }
void enc_init(void) { static const TIMER_Init_TypeDef txTimerInit = { false, /* Don't enable timer when init complete. */ false, /* Stop counter during debug halt. */ HIJACK_TIMER_RESOLUTION,/* ... */ timerClkSelHFPerClk, /* Select HFPER clock. */ false, /* Not 2x count mode. */ false, /* No ATI. */ timerInputActionNone, /* No action on falling input edge. */ timerInputActionNone, /* No action on rising input edge. */ timerModeUp, /* Up-counting. */ false, /* Do not clear DMA requests when DMA channel is active. */ false, /* Select X2 quadrature decode mode (if used). */ false, /* Disable one shot. */ false /* Not started/stopped/reloaded by other timers. */ }; /* Ensure core frequency has been updated */ SystemCoreClockUpdate(); /* Enable peripheral clocks. */ CMU_ClockEnable(cmuClock_HFPER, true); CMU_ClockEnable(HIJACK_TX_TIMERCLK, true); /* Configure Rx timer. */ TIMER_Init(HIJACK_TX_TIMER, &txTimerInit); /* Configure Tx timer output compare channel 0. */ HIJACK_CompareConfig(hijackOutputModeToggle); TIMER_CompareSet(HIJACK_TX_TIMER, 0, HIJACK_NUM_TICKS_PER_HALF_CYCLE); /* Route the capture channels to the correct pins, enable CC feature. */ HIJACK_TX_TIMER->ROUTE = HIJACK_TX_LOCATION | TIMER_ROUTE_CC0PEN; /* Tx: Configure the corresponding GPIO pin as an input. */ GPIO_PinModeSet(HIJACK_TX_GPIO_PORT, HIJACK_TX_GPIO_PIN, gpioModePushPull, 0); /* Enable Tx timer CC0 interrupt. */ NVIC_EnableIRQ(TIMER1_IRQn); TIMER_IntEnable(HIJACK_TX_TIMER, TIMER_IF_CC0); /* Enable the timer. */ TIMER_Enable(HIJACK_TX_TIMER, true); }
/** * \brief decode initial. */ void dec_init(void) { static const TIMER_Init_TypeDef rxTimerInit = { false, /* Don't enable timer when init complete. */ false, /* Stop counter during debug halt. */ HIJACK_TIMER_RESOLUTION,/* ... */ timerClkSelHFPerClk, /* Select HFPER clock. */ false, /* Not 2x count mode. */ false, /* No ATI. */ timerInputActionNone, /* No action on falling input edge. */ timerInputActionNone, /* No action on rising input edge. */ timerModeUp, /* Up-counting. */ false, /* Do not clear DMA requests when DMA channel is active. */ false, /* Select X2 quadrature decode mode (if used). */ false, /* Disable one shot. */ false /* Not started/stopped/reloaded by other timers. */ }; /* Ensure core frequency has been updated */ SystemCoreClockUpdate(); /* Enable RX_TIMER clock . */ CMU_ClockEnable(HIJACK_RX_TIMERCLK, true); /* Configure Rx timer. */ TIMER_Init(HIJACK_RX_TIMER, &rxTimerInit); /* Configure Rx timer input capture channel 0. */ HIJACK_CaptureConfig(hijackEdgeModeBoth); /* Route the capture channels to the correct pins, enable CC1. */ HIJACK_RX_TIMER->ROUTE = TIMER_ROUTE_LOCATION_LOC3 | TIMER_ROUTE_CC1PEN; /* Rx: Configure the corresponding GPIO pin (PortD, Ch2) as an input. */ GPIO_PinModeSet(HIJACK_RX_GPIO_PORT, HIJACK_RX_GPIO_PIN, gpioModeInput, 0); /* Enable Rx timer CC1 interrupt. */ NVIC_EnableIRQ(TIMER0_IRQn); TIMER_IntEnable(HIJACK_RX_TIMER, TIMER_IF_CC1); /* Enable the timer. */ TIMER_Enable(HIJACK_RX_TIMER, true); }
/***************************************************************************//** * @brief * Start the TIMER1 to generate a 50% duty cycle output. * * @param[in] frequency * The output frequency in Hz. ******************************************************************************/ void TD_TIMER_Start(uint32_t frequency) { uint32_t top; top = CMU_ClockFreqGet(cmuClock_TIMER1); top = top / frequency; // Enable clock for TIMER1 module CMU_ClockEnable(cmuClock_TIMER1, true); // Configure CC channel 0 TIMER_InitCC(TIMER1, 0, &timerCCInit); // Route CC0 to location 0 (PC13) and enable pin //TIMER1->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_LOCATION_LOC0); // Set Top Value TIMER_TopSet(TIMER1, top); // Set compare value starting at 0 - it will be incremented in the interrupt handler TIMER_CompareBufSet(TIMER1, 0, top >> 1); // Configure timer TIMER_Init(TIMER1, &timerInit); // Enable overflow interrupt TIMER_IntEnable(TIMER1, TIMER_IF_OF); // Disable interrupts //TIMER_IntDisable(TIMER1, TIMER_IF_OF); // Enable TIMER1 interrupt vector in NVIC NVIC_EnableIRQ(TIMER1_IRQn); // Enable timer TIMER_Enable(TIMER1, true); TD_TIMER_Enabled = true; }
void fd_led_wake(void) { fd_lp55231_power_on(); fd_lp55231_wake(); CMU_ClockEnable(cmuClock_TIMER0, true); TIMER_InitCC_TypeDef timer_initcc = TIMER_INITCC_DEFAULT; timer_initcc.cmoa = timerOutputActionToggle; timer_initcc.mode = timerCCModePWM; TIMER_InitCC(TIMER0, /* channel */ 1, &timer_initcc); TIMER_InitCC(TIMER0, /* channel */ 2, &timer_initcc); TIMER0->ROUTE = TIMER_ROUTE_CC1PEN | TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC4; TIMER_TopSet(TIMER0, TOP); TIMER_CompareSet(TIMER0, /* channel */ 1, TOP); TIMER_CompareSet(TIMER0, /* channel */ 2, TOP); TIMER_Init_TypeDef timer_init = TIMER_INIT_DEFAULT; TIMER_Init(TIMER0, &timer_init); CMU_ClockEnable(cmuClock_TIMER3, true); TIMER_InitCC(TIMER3, /* channel */ 1, &timer_initcc); TIMER_InitCC(TIMER3, /* channel */ 2, &timer_initcc); TIMER3->ROUTE = TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC0; TIMER_TopSet(TIMER3, TOP); TIMER_CompareSet(TIMER3, /* channel */ 1, TOP); TIMER_CompareSet(TIMER3, /* channel */ 2, TOP); TIMER_IntEnable(TIMER3, TIMER_IF_CC1 | TIMER_IF_OF); NVIC_EnableIRQ(TIMER3_IRQn); TIMER_Init(TIMER3, &timer_init); }
int main(void) { CHIP_Init(); CMU_ClockEnable(cmuClock_GPIO, true); CMU_ClockEnable(cmuClock_TIMER1, true); CMU_ClockEnable(cmuClock_TIMER3, true); // Set up TIMER1 for timekeeping TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT; timerInit.prescale = timerPrescale1024; TIMER_IntEnable(TIMER1, TIMER_IF_OF); // Enable TIMER1 interrupt vector in NVIC NVIC_EnableIRQ(TIMER1_IRQn); // Set TIMER Top value TIMER_TopSet(TIMER1, ONE_SECOND_TIMER_COUNT); TIMER_Init(TIMER1, &timerInit); // Wait for the timer to get going while (TIMER1->CNT == 0) ; // Enable LED output GPIO_PinModeSet(LED_PORT, LED_PIN, gpioModePushPull, 0); // Create the object initializer for LED PWM TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT; timerCCInit.mode = timerCCModePWM; timerCCInit.cmoa = timerOutputActionToggle; // Configure TIMER3 CC channel 2 TIMER_InitCC(TIMER3, TIMER_CHANNEL, &timerCCInit); // Route CC2 to location 1 (PE3) and enable pin for cc2 TIMER3->ROUTE |= (TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC1); // Set Top Value TIMER_TopSet(TIMER3, TIMER_TOP); // Set the PWM duty cycle here! TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, 0); // Create a timerInit object, based on the API default TIMER_Init_TypeDef timerInit2 = TIMER_INIT_DEFAULT; timerInit2.prescale = timerPrescale256; TIMER_Init(TIMER3, &timerInit2); enum mode_values { RAMPING_UP, HIGH, RAMPING_DOWN, LOW}; // Check for properly sized constants uint16_t delta = MAX_BRIGHTNESS - MIN_BRIGHTNESS; if ( delta == 0 || RAMP_UP_TIME_MS % delta || RAMP_DOWN_TIME_MS % delta) { DEBUG_BREAK } // Set the initial condition uint16_t mode = RAMPING_UP; uint32_t time_step = RAMP_UP_TIME_MS / delta; uint16_t brightness = MIN_BRIGHTNESS; TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, brightness); uint64_t mode_timeout = set_ms_timeout(RAMP_UP_TIME_MS); while (1) { switch (mode) { case RAMPING_UP: delay_ms(time_step); brightness++; TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, brightness); if (expired_ms(mode_timeout)) { mode = HIGH; mode_timeout = set_ms_timeout(HIGH_DURATION_MS); } break; case HIGH: if (expired_ms(mode_timeout)) { mode = RAMPING_DOWN; time_step = RAMP_DOWN_TIME_MS / delta; mode_timeout = set_ms_timeout(RAMP_DOWN_TIME_MS); } break; case RAMPING_DOWN: delay_ms(time_step); brightness--; TIMER_CompareBufSet(TIMER3, TIMER_CHANNEL, brightness); if (expired_ms(mode_timeout)) { mode = LOW; mode_timeout = set_ms_timeout(LOW_DURATION_MS); } break; case LOW: if (expired_ms(mode_timeout)) { mode = RAMPING_UP; time_step = RAMP_UP_TIME_MS / delta; mode_timeout = set_ms_timeout(RAMP_UP_TIME_MS); } break; } } }
/**************************************************************************//** * @brief TIMER0_setup * Configures the TIMER *****************************************************************************/ void TIMER_setup(void) { /* Enable necessary clocks */ CMU_ClockEnable(cmuClock_TIMER0, true); CMU_ClockEnable(cmuClock_PRS, true); /* Select CC channel parameters */ TIMER_InitCC_TypeDef timerCCInit = { .eventCtrl = timerEventEveryEdge, /* Input capture event control */ .edge = timerEdgeBoth, /* Input capture on falling edge */ .prsSel = timerPRSSELCh5, /* Prs channel select channel 5*/ .cufoa = timerOutputActionNone, /* No action on counter underflow */ .cofoa = timerOutputActionNone, /* No action on counter overflow */ .cmoa = timerOutputActionNone, /* No action on counter match */ .mode = timerCCModeCapture, /* CC channel mode capture */ .filter = false, /* No filter */ .prsInput = true, /* CC channel PRS input */ .coist = false, /* Comparator output initial state */ .outInvert = false, /* No output invert */ }; /* Initialize TIMER0 CC0 channel */ TIMER_InitCC(HIJACK_RX_TIMER, 0, &timerCCInit); /* Select timer parameters */ const TIMER_Init_TypeDef timerInit = { .enable = false, /* Do not start counting when init complete */ .debugRun = false, /* Counter not running on debug halt */ .prescale = HIJACK_TIMER_RESOLUTION, /* Prescaler of 1 */ .clkSel = timerClkSelHFPerClk, /* TIMER0 clocked by the HFPERCLK */ .fallAction = timerInputActionReloadStart, /* Stop counter on falling edge */ .riseAction = timerInputActionReloadStart, /* Reload and start on rising edge */ .mode = timerModeUp, /* Counting up */ .dmaClrAct = false, /* No DMA */ .quadModeX4 = false, /* No quad decoding */ .oneShot = false, /* Counting up constinuously */ .sync = false, /* No start/stop/reload by other timers */ }; /* Initialize TIMER0 */ TIMER_Init(HIJACK_RX_TIMER, &timerInit); /* PRS setup */ /* Select ACMP as source and ACMP0OUT (ACMP0 OUTPUT) as signal */ PRS_SourceSignalSet(5, PRS_CH_CTRL_SOURCESEL_ACMP0, PRS_CH_CTRL_SIGSEL_ACMP0OUT, prsEdgeOff); /* Enable CC0 interrupt */ TIMER_IntEnable(HIJACK_RX_TIMER, TIMER_IF_CC0); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER0_IRQn); } /**************************************************************************//** * @brief ACMP_setup * Configures and starts the ACMP *****************************************************************************/ static void ACMP_setup(void) { /* Enable necessary clocks */ CMU_ClockEnable(HIJACK_RX_ACMPCLK, true); CMU_ClockEnable(cmuClock_GPIO, true); /* Configure ACMP input pin. */ GPIO_PinModeSet(HIJACK_RX_GPIO_PORT, HIJACK_RX_GPIO_PIN, gpioModeInput, 0); /* Analog comparator parameters */ const ACMP_Init_TypeDef acmpInit = { .fullBias = false, /* No full bias current*/ .halfBias = true, /* No half bias current */ .biasProg = 2, /* Biasprog current 1.4 uA */ .interruptOnFallingEdge = false, /* Disable interrupt for falling edge */ .interruptOnRisingEdge = false, /* Disable interrupt for rising edge */ .warmTime = acmpWarmTime256, /* Warm-up time in clock cycles, should be >140 cycles for >10us warm-up @ 14MHz */ .hysteresisLevel = acmpHysteresisLevel7, /* Hysteresis level 0 - no hysteresis */ .inactiveValue = 1, /* Inactive comparator output value */ .lowPowerReferenceEnabled = false, /* Low power reference mode disabled */ .vddLevel = HIJACK_RX_ACMP_LEVEL, /* Vdd reference scaling of 32 */ }; /* Use ACMP0 output, PD6 . */ //GPIO_PinModeSet(gpioPortD, 6, gpioModePushPull, 0); //ACMP_GPIOSetup(ACMP0, 2, true, false); /* Init ACMP and set ACMP channel 4 as positive input and scaled Vdd as negative input */ ACMP_Init(HIJACK_RX_ACMP, &acmpInit); ACMP_ChannelSet(HIJACK_RX_ACMP, HIJACK_RX_ACMP_NEG, HIJACK_RX_ACMP_CH); ACMP_Enable(HIJACK_RX_ACMP); } /** * @brief calculate whether cnt is in 500us region * ticker = 64Mhz/128 = 2us * 475us < cnt < 510us * */ static chk_result_t IsTime2Detect(uint32_t inv) { chk_result_t ret; if( inv < HIJACK_DEC_NUM_TICKS_MIN){ offset = inv; ret = pass; } else if ( ( inv <= HIJACK_DEC_NUM_TICKS_MAX ) && ( inv >= HIJACK_DEC_NUM_TICKS_MIN ) ) { offset = 0; inv = 0; ret = suit; } else{ offset = 0; inv = 0; ret = error; } return ret; } /* * Find phase remain or phase reversal. */ static void dec_parser(uint8_t bit_msk, state_t state) { if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( falling == cur_edge ){ dec.data &= ~(1 << bit_msk); #if DEC_DEBUG == 1 uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif//DEC_DEBUG == 1 } else{ dec.data |= (1 << bit_msk); #if DEC_DEBUG == 1 uartPutChar( '_' ) ; uartPutChar( '+' ) ; #endif//DEC_DEBUG == 1 dec.odd++; } dec.state = state; //state switch } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; //state switch } } /**************************************************************************//** * @brief decode state machine * Invoke in TIMER_ISR for decoding. *****************************************************************************/ void decode_machine(void) { inv = offset + cur_stamp; //update offset #if 0 if( dec.state > Waiting ){ USART_printHexBy16u(inv); if(cur_edge == rising){ uartPutChar( '\\' ) ; } else{ uartPutChar( '/' ) ; } } #endif switch (dec.state){ case Waiting: /* go to start bit if rising edge exist. */ if (rising == cur_edge) { dec.state = Sta0; offset = 0; inv = 0; } break; // case Sta0: if( ( suit == IsTime2Detect(inv) ) && ( falling == cur_edge ) ){ dec.data = 0; //clear data field for store new potential data dec.odd = 0; //clear odd field parity counter dec.state = Bit0; #if DEC_DEBUG == 1 uartPutChar( 'S' ) ; uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif } else{ dec.state = Waiting; } break; // case Bit0: #if DEC_DEBUG == 1 uartPutChar( '0' ) ; #endif dec_parser(BIT0, Bit1); break; // case Bit1: #if DEC_DEBUG == 1 uartPutChar( '1' ) ; #endif dec_parser(BIT1, Bit2); break; // case Bit2: #if DEC_DEBUG == 1 uartPutChar( '2' ) ; #endif dec_parser(BIT2, Bit3); break; // case Bit3: #if DEC_DEBUG == 1 uartPutChar( '3' ) ; #endif dec_parser(BIT3, Bit4); break; // case Bit4: #if DEC_DEBUG == 1 uartPutChar( '4' ) ; #endif dec_parser(BIT4, Bit5); break; // case Bit5: #if DEC_DEBUG == 1 uartPutChar( '5' ) ; #endif dec_parser(BIT5, Bit6); break; // case Bit6: #if DEC_DEBUG == 1 uartPutChar( '6' ) ; #endif dec_parser(BIT6, Bit7); break; // case Bit7: #if DEC_DEBUG == 1 uartPutChar( '7' ) ; #endif dec_parser(BIT7, Parity); break; // case Parity: if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( rising == cur_edge ){ dec.odd++; #if DEC_DEBUG == 1 uartPutChar( '_' ) ; uartPutChar( '+' ) ; #endif } else{ #if DEC_DEBUG == 1 uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif } #if DEC_DEBUG == 1 uartPutChar( dec.odd + 0x30) ; #endif if( 1 == (dec.odd%2)){ //parity pass dec.state = Sto0; } else{ //parity failed dec.state = Waiting; } } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; } break; // case Sto0: if ( ( suit == IsTime2Detect(inv) ) ){ //it's time to determine if( rising == cur_edge ){ //stop bit is rising edge USART_txByte(dec.data); #if DEC_DEBUG == 1 uartPutChar( '_' ) ; uartPutChar( '+' ) ; #endif HIJACKPutData(&dec.data, &decBuf, sizeof(uint8_t)); } else{ #if DEC_DEBUG == 1 uartPutChar( '+' ) ; uartPutChar( '_' ) ; #endif } dec.state = Waiting; #if DEC_DEBUG == 1 uartPutChar( '\r' ) ; uartPutChar( '\n' ) ; #endif } else if ( error == IsTime2Detect(inv) ){ //wait for edge detection time dec.state = Waiting; } break; // default: break; // } }
void initPWM() // Brightness should be less than TIMER_TOP (1024) { /* Enable clocks */ CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO; CMU_ClockEnable(cmuClock_TIMER1, true); /* Set pins */ GPIO_PinModeSet(gpioPortE,10,gpioModePushPull,1); GPIO_PinModeSet(gpioPortE,11,gpioModePushPull,1); /* Select CC channel parameters */ TIMER_InitCC_TypeDef timerCCInit = { .eventCtrl = timerEventEveryEdge, .edge = timerEdgeBoth, .prsSel = timerPRSSELCh0, .cufoa = timerOutputActionNone, .cofoa = timerOutputActionNone, .cmoa = timerOutputActionToggle, .mode = timerCCModePWM, .filter = false, .prsInput = false, .coist = false, .outInvert = false, }; /* Configure CC channels */ TIMER_InitCC(TIMER1, 0, &timerCCInit); TIMER_InitCC(TIMER1, 1, &timerCCInit); /* Set which pins will be set by the timer */ TIMER1->ROUTE = TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC1; /* Set Top Value */ TIMER_TopSet(TIMER1, TIMER_TOP); /* Set compare value starting at top - it will be incremented in the interrupt handler */ TIMER_CompareBufSet(TIMER1, 0, TIMER_TOP + 1); TIMER_CompareBufSet(TIMER1, 1, TIMER_TOP + 1); /* Select timer parameters */ TIMER_Init_TypeDef timerInit = { .enable = true, .debugRun = false, .prescale = timerPrescale16, .clkSel = timerClkSelHFPerClk, .fallAction = timerInputActionNone, .riseAction = timerInputActionNone, .mode = timerModeUp, .dmaClrAct = false, .quadModeX4 = false, .oneShot = false, .sync = false, }; /* Enable overflow interrupt */ TIMER_IntEnable(TIMER1, TIMER_IF_OF); TIMER_IntClear(TIMER1, TIMER_IF_OF); /* Enable TIMER1 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER1_IRQn); /* Configure timer */ TIMER_Init(TIMER1, &timerInit); }
void backlight_init() { /*init and setup PA9(Backlight) and PA8(Motor) to GPIO output and PWM*/ /* Enable clock for TIMER0 module */ CMU_ClockEnable(cmuClock_TIMER2, true); /* Set location 4 pin (PD7) as output */ GPIO_PinModeSet(gpioPortA, 8, gpioModePushPull, 0); GPIO_PinModeSet(gpioPortA, 9, gpioModePushPull, 0); /*PA8 PA9 at TIMER2 Location0 CC0(PA8) and CC1(PA9)*/ /* Select CC channel parameters */ TIMER_InitCC_TypeDef timerCCInit = { .eventCtrl = timerEventEveryEdge, .edge = timerEdgeBoth, .prsSel = timerPRSSELCh0, .cufoa = timerOutputActionNone, .cofoa = timerOutputActionNone, .cmoa = timerOutputActionToggle, .mode = timerCCModePWM, .filter = false, .prsInput = false, .coist = false, .outInvert = false, }; /* Configure CC channel 0 CC0 for PA8 */ TIMER_InitCC(TIMER2, 0, &timerCCInit); /* Configure CC channel 1 CC0 for PA9 */ timerCCInit.prsSel = timerPRSSELCh1; TIMER_InitCC(TIMER2, 1, &timerCCInit); /* Route CC0/CC1 to location 0 (PA8/PA9) and enable pin */ TIMER2->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC0); /* Set Top Value */ TIMER_TopSet(TIMER2, TOP); /*Set CCVB = 0 to TIMER2_CC0 and TIMER2_CC1 */ TIMER_CompareBufSet(TIMER2, 0, 0); TIMER_CompareBufSet(TIMER2, 1, 0); /* Select timer parameters */ TIMER_Init_TypeDef timerInit = { .enable = true, .debugRun = true, .prescale = timerPrescale64, .clkSel = timerClkSelHFPerClk, .fallAction = timerInputActionNone, .riseAction = timerInputActionNone, .mode = timerModeUp, .dmaClrAct = false, .quadModeX4 = false, .oneShot = false, .sync = false, }; /* Enable overflow interrupt */ TIMER_IntEnable(TIMER2, TIMER_IF_OF); /* Enable TIMER2 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER2_IRQn); /* Configure timer */ TIMER_Init(TIMER2, &timerInit); light_state = MOTOR_ON | LIGHT_ON; LIGHTLEVEL = 0; } void backlight_shutdown() { TIMER_CompareBufSet(TIMER2, 0, 0); TIMER_CompareBufSet(TIMER2, 1, 0); GPIO_PinOutClear(gpioPortA, 8); GPIO_PinOutClear(gpioPortA, 9); light_state = 0; TIMER_Enable(TIMER2,false); CMU_ClockEnable(cmuClock_TIMER2, false); } void light_stop(void *ptr) { if (LIGHTLEVEL > 2) { LIGHTLEVEL--; clock_time_t length = (clock_time_t)ptr; ctimer_set(&light_timer, length, light_stop, (void*)length); } else { TIMER_CompareBufSet(TIMER2, 1, 0); GPIO_PinOutClear(gpioPortA, 9); light_state &= ~LIGHT_ON; if(light_state == 0) { TIMER_Enable(TIMER2,false); CMU_ClockEnable(cmuClock_TIMER2, false); } } } void backlight_on(uint8_t level, clock_time_t length) { CMU_ClockEnable(cmuClock_TIMER2, true); TIMER_Enable(TIMER2,true); if (level > 8) level = 8; if (level == 0) { TIMER_CompareBufSet(TIMER2, 1, 0); } else { LIGHTLEVEL = level * 2; TIMER_CompareBufSet(TIMER2, 1, ( LIGHTLEVEL * 100)); if (length > 0) ctimer_set(&light_timer, length, light_stop, (void*)(CLOCK_SECOND/8)); } }
void InitAudioPWM(void) { CMU_ClockEnable(cmuClock_TIMER1, true); /* Select CC channel parameters */ TIMER_InitCC_TypeDef timerCCInit = { .eventCtrl = timerEventEveryEdge, .edge = timerEdgeBoth, .prsSel = timerPRSSELCh0, .cufoa = timerOutputActionNone, .cofoa = timerOutputActionNone, .cmoa = timerOutputActionToggle, .mode = timerCCModePWM, .filter = false, .prsInput = false, .coist = false, .outInvert = false, }; /* Configure CC channel 0 */ TIMER_InitCC(TIMER1, 0, &timerCCInit); TIMER_InitCC(TIMER1, 1, &timerCCInit); //TIMER_InitCC(TIMER0, 2, &timerCCInit); // TIMER3->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_CC2PEN | TIMER_ROUTE_LOCATION_LOC0); TIMER1->ROUTE = TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC1; /* Set Top Value */ TIMER_TopSet(TIMER1, 255);//384 /* Set compare value starting at top - it will be incremented in the interrupt handler */ TIMER_CompareBufSet(TIMER1, 0, 256);//385 TIMER_CompareBufSet(TIMER1, 1, 256);//385 //TIMER_CompareBufSet(TIMER3, 2, RGB_PWM_TIMER_TOP + 1); /* Select timer parameters */ TIMER_Init_TypeDef timerInit = { .enable = true, .debugRun = false, .prescale = timerPrescale8, .clkSel = timerClkSelHFPerClk, .fallAction = timerInputActionNone, .riseAction = timerInputActionNone, .mode = timerModeUp, .dmaClrAct = false, .quadModeX4 = false, .oneShot = false, .sync = false, }; ///* Enable overflow interrupt */ TIMER_IntEnable(TIMER1, TIMER_IF_OF); TIMER_IntClear(TIMER1, TIMER_IF_OF); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER1_IRQn); /* Configure timer */ TIMER_Init(TIMER1, &timerInit); } void TIMER1_IRQHandler(void) { int audio_Sample = 0; if(toPlay > 0) { audio_Sample = buff[(iterator = next(iterator))]; toPlay--; } else { audio_Sample = 0; } TIMER_CompareBufSet(TIMER1, 0, (audio_Sample)); TIMER_IntClear(TIMER1, TIMER_IF_OF); }
/******************************************************************************* * @brief Initialize TIMER1 in Up/Down Count mode with interrupts on overflow * ******************************************************************************/ void InitTimer1(void) { /* Enable clock for GPIO module */ CMU_ClockEnable( cmuClock_GPIO, true ); /* Enable clock for TIMER1 */ CMU_ClockEnable( cmuClock_TIMER1, true ); /* Set CC0 location 3 pin (PD1) as output */ GPIO_PinModeSet( DL_PORT, DL_PIN, gpioModePushPull, DL_OFF_LVL ); /* Set CC1 location 3 pin (PD2) as output */ GPIO_PinModeSet( DH_PORT, DH_PIN, gpioModePushPull, DH_OFF_LVL); #define TOP (F_HFXO/F_TX)/2 // timer 0 top value use F_HFXO, F_TX /* Configuring the Capture-Compare-Channels */ /* Configure CC channel 0 */ #define CC_CH_0 0 TIMER1->CC[CC_CH_0].CTRL = DL_CC_STOP; #define CC_VAL_CH0 TOP/4 TIMER_CompareSet( TIMER1, CC_CH_0, CC_VAL_CH0 ); /* Configure CC channel 1 */ #define CC_CH_1 1 TIMER1->CC[CC_CH_1].CTRL = DH_CC_STOP; #define CC_VAL_CH1 TOP/4*3 TIMER_CompareSet( TIMER1, CC_CH_1, CC_VAL_CH1 ); /* Configure CC channel 2 */ #define CC_CH_2 2 TIMER1->CC[CC_CH_2].CTRL = CC2_STOP; #define CC_VAL_CH2 TOP/2 TIMER_CompareSet( TIMER1, CC_CH_2, CC_VAL_CH2 ); /* Enable overflow interrupt for TIMER1*/ TIMER_IntEnable( TIMER1, TIMER_IEN_CC2 ); /* Clear pending TIMER1 interrupts */ NVIC_ClearPendingIRQ( TIMER1_IRQn ); /* Enable TIMER1 interrupt vector in NVIC */ NVIC_EnableIRQ( TIMER1_IRQn ); /* Route CC0 and CC1 to location 4 (PD6 PD7) and enable pins */ TIMER1->ROUTE |= (TIMER_ROUTE_CC0PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC4); /* Set Top Value */ TIMER_TopSet( TIMER1, TOP ); /* Select timer parameters */ TIMER_Init_TypeDef timerInit = TIMER_INIT_DEFAULT; timerInit.enable = false; // if timer is active after init // timerInit.enable = true; // if timer is active after init timerInit.debugRun = true; // if timer runs when CPU in debugmode timerInit.prescale = timerPrescale1; // prescaler of Timer timerInit.clkSel = timerClkSelHFPerClk; // CLK source select timerInit.fallAction = timerInputActionNone; // counter action if falling edge on input timerInit.riseAction = timerInputActionNone; // counter action if rising edge on input timerInit.mode = timerModeUpDown; // Mode of timer timerInit.dmaClrAct = false; // DMA mode clear or active timerInit.quadModeX4 = false; // Quadrature decode mode timerInit.oneShot = false; // determine if only one count cycle timerInit.sync = false; // Start/stop/reload by other timers /* Initialize and Configure timer */ TIMER_Init( TIMER1, &timerInit ); /* Start timer */ } // END: InitTimer
/***************************************************************************//** * @brief * Initialize the specified TIMER unit * * @details * * @note * * @param[in] device * Pointer to device descriptor * * @param[in] unitNumber * Unit number * * @return * Pointer to TIMER device ******************************************************************************/ static struct efm32_timer_device_t *rt_hw_timer_unit_init( rt_device_t device, rt_uint8_t unitNumber, rt_uint8_t mode) { struct efm32_timer_device_t *timer; TIMER_Init_TypeDef init; efm32_irq_hook_init_t hook; CMU_Clock_TypeDef timerClock; IRQn_Type timerIrq; do { /* Allocate device */ timer = rt_malloc(sizeof(struct efm32_timer_device_t)); if (timer == RT_NULL) { timer_debug("no memory for TIMER%d driver\n", unitNumber); break; } /* Initialization */ if (unitNumber >= TIMER_COUNT) { break; } switch (unitNumber) { case 0: timer->timer_device = TIMER0; timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER0; timerIrq = TIMER0_IRQn; break; case 1: timer->timer_device = TIMER1; timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER1; timerIrq = TIMER1_IRQn; break; case 2: timer->timer_device = TIMER2; timerClock = (CMU_Clock_TypeDef)cmuClock_TIMER2; timerIrq = TIMER2_IRQn; break; default: break; } /* Enable TIMER clock */ CMU_ClockEnable(timerClock, true); /* Reset */ TIMER_Reset(timer->timer_device); /* Init specified TIMER unit */ init.enable = false; init.debugRun = true; init.prescale = TMR_CFG_PRESCALER; init.clkSel = timerClkSelHFPerClk; init.fallAction = timerInputActionNone; init.riseAction = timerInputActionNone; init.mode = timerModeUp; init.dmaClrAct = false; init.quadModeX4 = false; init.oneShot = (mode > 0) ? true : false; init.sync = false; TIMER_Init(timer->timer_device, &init); /* Config interrupt and NVIC */ hook.type = efm32_irq_type_timer; hook.unit = unitNumber; hook.cbFunc = rt_hw_timer_isr; hook.userPtr = device; efm32_irq_hook_register(&hook); /* Enable overflow interrupt */ TIMER_IntEnable(timer->timer_device, TIMER_IF_OF); TIMER_IntClear(timer->timer_device, TIMER_IF_OF); /* Enable TIMERn interrupt vector in NVIC */ NVIC_ClearPendingIRQ(timerIrq); NVIC_SetPriority(timerIrq, EFM32_IRQ_PRI_DEFAULT); NVIC_EnableIRQ(timerIrq); return timer; } while(0); if (timer) { rt_free(timer); } rt_kprintf("TIMER: Init failed!\n"); return RT_NULL; }
/**************************************************************************//** * @brief Main function * Main is called from __iar_program_start, see assembly startup file *****************************************************************************/ int motor_init(void) { uint32_t top_value = 0; /* Enable clock for GPIO module */ CMU_ClockEnable(cmuClock_GPIO, true); /* Enable clock for TIMER0 module */ CMU_ClockEnable(cmuClock_TIMER0, true); /* Set CC1 location 3 pin (PD2) as output */ //Left wheel GPIO_PinModeSet(gpioPortD, 2, gpioModePushPull, 0); /* Set CC2 location 3 pin (PD3) as output */ //Right wheel GPIO_PinModeSet(gpioPortD, 3, gpioModePushPull, 0); /* Select CC channel parameters */ TIMER_InitCC_TypeDef timerCCInit = { .eventCtrl = timerEventEveryEdge, .edge = timerEdgeBoth, .prsSel = timerPRSSELCh0, .cufoa = timerOutputActionNone, .cofoa = timerOutputActionNone, .cmoa = timerOutputActionToggle, .mode = timerCCModePWM, .filter = false, .prsInput = false, .coist = false, .outInvert = false, }; /* Configure CC channel 0 */ TIMER_InitCC(TIMER0, 1, &timerCCInit); TIMER_InitCC(TIMER0, 2, &timerCCInit); /* Route CC0 to location 3 (PD1) and enable pin */ TIMER0->ROUTE |= (TIMER_ROUTE_CC2PEN | TIMER_ROUTE_CC1PEN | TIMER_ROUTE_LOCATION_LOC3); /* Set Top Value */ TIMER_TopSet(TIMER0, CMU_ClockFreqGet(cmuClock_HFPER)/PWM_FREQ); top_value = CMU_ClockFreqGet(cmuClock_HFPER)/PWM_FREQ; forward_value = ((18*top_value)/200); backward_value = ((12*top_value)/200); steady_value = ((15*top_value)/200); /* Set compare value starting at 0 - it will be incremented in the interrupt handler */ TIMER_CompareBufSet(TIMER0, 1, 0); TIMER_CompareBufSet(TIMER0, 2, 0); /* Select timer parameters */ TIMER_Init_TypeDef timerInit = { .enable = true, .debugRun = true, .prescale = timerPrescale64, .clkSel = timerClkSelHFPerClk, .fallAction = timerInputActionNone, .riseAction = timerInputActionNone, .mode = timerModeUp, .dmaClrAct = false, .quadModeX4 = false, .oneShot = false, .sync = false, }; /* Enable overflow interrupt */ TIMER_IntEnable(TIMER0, TIMER_IF_OF); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER0_IRQn); /* Configure timer */ TIMER_Init(TIMER0, &timerInit); return 0; }
void timerTurnOn(void) { /* Enable overflow interrupt */ TIMER_IntEnable(TIMER0, TIMER_IF_OF); }
/**************************************************************************//** * @brief Main function *****************************************************************************/ int main(void) { /* Initialize chip */ CHIP_Init(); /* Initialize the LCD */ SegmentLCD_Init(true); /* Enable the DMA and TIMER0 clocks */ CMU_ClockEnable(cmuClock_DMA, true); CMU_ClockEnable(cmuClock_TIMER0, true); /* Enable overflow interrupt */ TIMER_IntEnable(TIMER0, TIMER_IF_OF); /* Enable TIMER0 interrupt vector in NVIC */ NVIC_EnableIRQ(TIMER0_IRQn); /* Initialize TIMER0 */ TIMER_Init_TypeDef timerInit = { .enable = true, .debugRun = true, .prescale = timerPrescale64, .clkSel = timerClkSelHFPerClk, .fallAction = timerInputActionNone, .riseAction = timerInputActionNone, .mode = timerModeUp, .dmaClrAct = false, .quadModeX4 = false, .oneShot = false, .sync = false, }; TIMER_Init(TIMER0, &timerInit); /* Initialize DMA */ DMA_Init_TypeDef dmaInit; dmaInit.hprot = 0; dmaInit.controlBlock = dmaControlBlock; DMA_Init(&dmaInit); /* Configure the DMA and perform the transfer */ performFlashTransfer(); while (1) { /* The transfer has finished. We wish to display the result on the LCD * but use as little power as possible; go to EM2 (the LCD requires EM2). */ EMU_EnterEM2(true); } }