/***************************************************************************//** * @brief * RxTimer, Timer0 IRQHandler. ******************************************************************************/ void TIMER0_IRQHandler(void) { uint32_t irqFlags; irqFlags = TIMER_IntGet(HIJACK_RX_TIMER); TIMER_IntClear(HIJACK_RX_TIMER, irqFlags); if (TIMER_IF_CC1 & irqFlags) { cur_stamp = TIMER_CaptureGet(HIJACK_RX_TIMER, 1); TIMER_CounterSet(HIJACK_RX_TIMER, 0); edge_occur = true; /* Check what transition it was. */ if (GPIO_PinInGet(HIJACK_RX_GPIO_PORT, HIJACK_RX_GPIO_PIN)) { cur_edge = rising; BSP_LedSet( 0 ); } else { cur_edge = falling; BSP_LedClear( 0 ); } } }
/***************************************************************************//** * @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); }
/***************************************************************************//** * @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(); }
void us_ticker_init(void) { if (us_ticker_inited) { /* calling init again should cancel current interrupt */ us_ticker_disable_interrupt(); return; } us_ticker_inited = true; /* Enable clock for TIMERs */ CMU_ClockEnable(US_TICKER_TIMER_CLOCK, true); if (REFERENCE_FREQUENCY > 24000000) { US_TICKER_TIMER->CTRL = (US_TICKER_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (4 << _TIMER_CTRL_PRESC_SHIFT); } else { US_TICKER_TIMER->CTRL = (US_TICKER_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (3 << _TIMER_CTRL_PRESC_SHIFT); } /* Clear TIMER counter value */ TIMER_CounterSet(US_TICKER_TIMER, 0); /* Start TIMER */ TIMER_Enable(US_TICKER_TIMER, true); /* Select Compare Channel parameters */ TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT; timerCCInit.mode = timerCCModeCompare; /* Configure Compare Channel 0 */ TIMER_InitCC(US_TICKER_TIMER, 0, &timerCCInit); /* Enable interrupt vector in NVIC */ TIMER_IntClear(US_TICKER_TIMER, TIMER_IEN_CC0); NVIC_SetVector(US_TICKER_TIMER_IRQ, (uint32_t) us_ticker_irq_handler); NVIC_EnableIRQ(US_TICKER_TIMER_IRQ); }
void timer_reset(tim_t dev) { TIMER_CounterSet(timer_config[dev].timer.dev, 0); }
int main(void) { uint16_t vector_start = 0; uint16_t vector_end = 0; /* Initialize chip */ CHIP_Init(); // start HFXO CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO); /* Start LFXO, and use LFXO for low-energy modules */ CMU_ClockSelectSet(cmuClock_LFA, cmuSelect_LFXO); CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO); CMU_ClockEnable(cmuClock_GPIO, true); delay_init(); initLeuart(); initSpi(); #if 0 printf("cmuClock_HF: %" PRIu32 "\r\n", CMU_ClockFreqGet(cmuClock_HF)); printf("cmuClock_CORE: %" PRIu32 "\r\n", CMU_ClockFreqGet(cmuClock_CORE)); printf("cmuClock_HFPER: %" PRIu32 "\r\n", CMU_ClockFreqGet(cmuClock_HFPER)); printf("cmuClock_TIMER1: %" PRIu32 "\r\n", CMU_ClockFreqGet(cmuClock_TIMER1)); #endif init_analog_switches(); printf("\r\n* vector test *\r\n"); while (1) { char c; c = leuart_blocking_read_char(); switch(c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': vector_start = vector_end; vector_end = 4095 * (c - '0') / 9; break; case 'v': printf("vector from %d to %d\r\n", vector_start, vector_end); setDAC(0, true, vector_start); set_analog_switch(T1, false); setDAC(0, true, vector_end); TIMER_CounterSet(T2_TIMER, 0); TIMER_Enable(T2_TIMER, true); while(T2_TIMER->STATUS & TIMER_STATUS_RUNNING) ; set_analog_switch(T1, true); break; default: break; } } }
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; }