void fd_led_initialize(void) { fd_timer_add(&override_timer, override_callback); TIMER_CompareSet(TIMER0, /* channel */ 1, TOP); TIMER_CompareSet(TIMER0, /* channel */ 2, TOP); TIMER_CompareSet(TIMER3, /* channel */ 1, TOP); TIMER_CompareSet(TIMER3, /* channel */ 2, TOP); fd_led_sleep(); fd_event_add_em2_check(fd_led_em2_check); }
/***************************************************************************//** * @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 fd_led_set_usb(uint8_t orange, uint8_t green) { led_state.usb.o = orange; led_state.usb.g = green; if (override_timer.active) { return; } fd_led_change_before(5, (orange << 8) | green); // to distinguish overflow and compare interrupts unambiguously -denis if (orange == 0xff) { orange = 0xfe; } TIMER_CompareSet(TIMER3, /* channel */ 1, (~orange) << 8); TIMER_CompareSet(TIMER3, /* channel */ 2, (~green) << 8); fd_led_change_after(5, (orange << 8) | green); }
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); }
void fd_led_set_d4(uint8_t value) { led_state.d4.r = value; if (override_timer.active) { return; } fd_led_change_before(4, value); TIMER_CompareSet(TIMER0, /* channel */ 1, (~value) << 8); fd_led_change_after(4, value); }
void TIMER_IRQHandler( void ) { uint32_t flags; flags = TIMER_IntGet( TIMER ); if ( flags & TIMER_IF_CC0 ) { TIMER_IntClear( TIMER, TIMER_IFC_CC0 ); TIMER_CompareSet( TIMER, 0, TIMER_CaptureGet( TIMER, 0 ) + ticksPrMs ); TimerTick(); } }
/***************************************************************************//** * @brief * Timer1 IRQHandler. ******************************************************************************/ void TIMER1_IRQHandler(void) { uint32_t irqFlags; /* Clear all pending IRQ flags. */ irqFlags = TIMER_IntGet(HIJACK_TX_TIMER); TIMER_IntClear(HIJACK_TX_TIMER, irqFlags); /* Reset the counter and the compare value. */ TIMER_CounterSet(HIJACK_TX_TIMER, 0); TIMER_CompareSet(HIJACK_TX_TIMER, 0, HIJACK_NUM_TICKS_PER_HALF_CYCLE); }
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); }
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 * Timer1 IRQHandler. ******************************************************************************/ void TIMER1_IRQHandler(void) { //static volatile uint8_t currentPin = 1; //static volatile uint8_t currentSym = 1; static volatile uint8_t txParity = 0; uint32_t irqFlags; /* Clear all pending IRQ flags. */ irqFlags = TIMER_IntGet(HIJACK_TX_TIMER); TIMER_IntClear(HIJACK_TX_TIMER, irqFlags); ticker++; /* Reset the counter and the compare value. */ TIMER_CounterSet(HIJACK_TX_TIMER, 0); TIMER_CompareSet(HIJACK_TX_TIMER, 0, 500ul*HIJACK_NUM_TICKS_PER_1US*HIJACK_ENC_CARRIER_FREQ_1KHZ/HIJACK_ENC_CARRIER_FREQ_CONF); //BSP_LedToggle(0); encode_machine(); }
static void init_analog_switches(void) { CMU_ClockEnable(cmuClock_GPIO, true); CMU_ClockEnable(cmuClock_TIMER1, true); port_init(analog_switches, sizeof(analog_switches)/sizeof(port_init_t)); TIMER_Init(T2_TIMER, & t2_timer_init); TIMER_InitCC(T2_TIMER, T2_TIMER_CC, & t2_timer_cc_init); T2_TIMER->ROUTE = TIMER_ROUTE_CC1PEN | (T2_TIMER_LOC << _TIMER_ROUTE_LOCATION_SHIFT); T2_TIMER->CTRL |= TIMER_CTRL_RSSCOIST; uint16_t t1_to_t2_delay = ROUND_F_TO_I(T1_TO_T2_DELAY * CMU_ClockFreqGet(cmuClock_TIMER1)); uint16_t timer_max_count = ROUND_F_TO_I((T1_TO_T2_DELAY + T2_PULSE_TIME) * CMU_ClockFreqGet(cmuClock_TIMER1)); #if 0 printf("t1_to_t2_delay: %" PRIu16 "\r\n", t1_to_t2_delay); printf("timer_max_count: %" PRIu16 "\r\n", timer_max_count); #endif TIMER_CompareSet(T2_TIMER, T2_TIMER_CC, t1_to_t2_delay); TIMER_TopSet(T2_TIMER, timer_max_count); }
/******************************************************************************* * @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
void PWM_Init(PWM_Settings_t PWM_Settings) { /* enable TIMER0 peripheral clock */ CMU_ClockEnable(cmuClock_TIMER0, true); /* Setup Timer Channel Configuration for PWM */ TIMER_InitCC_TypeDef TimerCCInit = { .eventCtrl = timerEventEveryEdge, /* this value will be ignored since we aren't using input capture */ .edge = timerEdgeNone, /* this value will be ignored since we aren't using input capture */ .prsSel = timerPRSSELCh0, /* this value will be ignored since we aren't using PRS */ .cufoa = timerOutputActionNone, /* no action on underflow (up-count mode) */ .cofoa = timerOutputActionSet, /* on overflow, we want the output to go high, but in PWM mode this should happen automatically */ .cmoa = timerOutputActionClear, /* on compare match, we want output to clear, but in PWM mode this should happen automatically */ .mode = timerCCModePWM, /* set timer channel to run in PWM mode */ .filter = false, /* not using input, so don't need a filter */ .prsInput = false, /* not using PRS */ .coist = false, /* initial state for PWM is high when timer is enabled */ .outInvert = false, /* non-inverted output */ }; /* Setup Timer Configuration for PWM */ TIMER_Init_TypeDef TimerPWMSetup = { .enable = true, /* start timer upon configuration */ .debugRun = true, /* run timer in debug mode */ .prescale = timerPrescale1, /* set prescaler to 1 */ .clkSel = timerClkSelHFPerClk, /* set clock source as HFPERCLK */ .fallAction = timerInputActionNone, /* no action from inputs */ .riseAction = timerInputActionNone, /* no action from inputs */ .mode = timerModeUp, /* use up-count mode */ .dmaClrAct = false, /* not using DMA */ .quadModeX4 = false, /* not using Quad Dec. mode */ .oneShot = false, /* not using one shot mode */ .sync = false, /* not synchronizing timer3 with other timers */ }; /* by default */ PWM_SetPeriod(1000ULL, 10000000ULL); TIMER_CounterSet(TIMER0, 0); /* start counter at 0 (up-count mode) */ for (uint8_t CCx = 0; CCx < PWM_NUMBER_OF_CC_CHANNELS; CCx++) { PWM_SetDuty(CCx, 0); TIMER_InitCC(TIMER0, CCx, &TimerCCInit); /* apply channel configuration to Timer0 channel 0 */ } TIMER0->ROUTE = (PWM_Settings.location << _TIMER_ROUTE_LOCATION_SHIFT); if (PWM_Settings.CC0_enable == true) { TIMER0->ROUTE |= TIMER_ROUTE_CC0PEN; } if (PWM_Settings.CC1_enable == true) { TIMER0->ROUTE |= TIMER_ROUTE_CC1PEN; } if (PWM_Settings.CC2_enable == true) { TIMER0->ROUTE |= TIMER_ROUTE_CC2PEN; } TIMER_Init(TIMER0, &TimerPWMSetup); /* apply PWM configuration to timer1 */ } void PWM_TurnOff(PWM_CC_Channel CCx) { TIMER_CompareSet(TIMER0, CCx, 0); TIMER_CompareBufSet(TIMER0, CCx, 0); g_PWM_dutyCycle[CCx] = 0; } void PWM_SetDuty(PWM_CC_Channel CCx, uint32_t duty_miliPercents) { uint32_t duty = (uint32_t)(((uint64_t)g_PWM_Period * (uint64_t)duty_miliPercents) / 10000ULL); TIMER_CompareSet(TIMER0, CCx, 0); TIMER_CompareBufSet(TIMER0, CCx, duty); g_PWM_dutyCycle[CCx] = duty; }