void timer_setup(void) { /* Enable timer clock. */ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); /* Reset timer. */ timer_reset(TIM1); /* Configure prescaler. */ timer_set_prescaler(TIM1, 160); /* Configure PE11 (AF1: TIM1_CH2) (SYNC_IN). */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPEEN); gpio_mode_setup(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11); gpio_set_af(GPIOE, GPIO_AF1, GPIO11); /* Configure input capture. */ timer_ic_disable(TIM1, TIM_IC2); timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI2); timer_ic_set_polarity(TIM1, TIM_IC2, TIM_IC_RISING); timer_ic_set_prescaler(TIM1, TIM_IC2, TIM_IC_PSC_OFF); timer_ic_set_filter(TIM1, TIM_IC2, TIM_IC_OFF); timer_ic_enable(TIM1, TIM_IC2); /* Enable counter. */ timer_enable_counter(TIM1); /* Enable IRQs */ nvic_enable_irq(NVIC_TIM1_UP_TIM10_IRQ); timer_enable_irq(TIM1, TIM_DIER_UIE); nvic_enable_irq(NVIC_TIM1_CC_IRQ); timer_enable_irq(TIM1, TIM_DIER_CC2IE); }
void ppm_arch_init ( void ) { /* timer clock enable */ rcc_peripheral_enable_clock(PPM_RCC, PPM_PERIPHERAL); /* GPIOA clock enable */ rcc_peripheral_enable_clock(&RCC_APB2ENR, PPM_GPIO_PERIPHERAL); /* timer gpio configuration */ gpio_set_mode(PPM_GPIO_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, PPM_GPIO_PIN); /* Time Base configuration */ timer_reset(PPM_TIMER); timer_set_mode(PPM_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(PPM_TIMER, 0xFFFF); /* run ppm timer at cpu freq / 9 = 8MHz */ timer_set_prescaler(PPM_TIMER, 8); /* TIM configuration: Input Capture mode --------------------- The Rising edge is used as active edge, Intput pin is either PA1 or PA10 ------------------------------------------------------------ */ #if defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_POSITIVE timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_RISING); #elif defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_NEGATIVE timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_FALLING); #else #error "Unknown PM_PULSE_TYPE" #endif timer_ic_set_input(PPM_TIMER, PPM_CHANNEL, PPM_TIMER_INPUT); timer_ic_set_prescaler(PPM_TIMER, PPM_CHANNEL, TIM_IC_PSC_OFF); timer_ic_set_filter(PPM_TIMER, PPM_CHANNEL, TIM_IC_OFF); /* Enable timer Interrupt(s). */ nvic_set_priority(PPM_IRQ, 2); nvic_enable_irq(PPM_IRQ); #ifdef PPM_IRQ2 nvic_set_priority(PPM_IRQ2, 2); nvic_enable_irq(PPM_IRQ2); #endif /* Enable the CC2 and Update interrupt requests. */ timer_enable_irq(PPM_TIMER, PPM_IRQ_FLAGS); /* Enable capture channel. */ timer_ic_enable(PPM_TIMER, PPM_CHANNEL); /* TIM enable counter */ timer_enable_counter(PPM_TIMER); ppm_last_pulse_time = 0; ppm_cur_pulse = RADIO_CONTROL_NB_CHANNEL; timer_rollover_cnt = 0; }
void ppm_arch_init ( void ) { /* timer clock enable */ rcc_peripheral_enable_clock(PPM_RCC, PPM_PERIPHERAL); /* GPIO clock enable */ gpio_enable_clock(PPM_GPIO_PORT); /* timer gpio configuration */ gpio_setup_pin_af(PPM_GPIO_PORT, PPM_GPIO_PIN, PPM_GPIO_AF, FALSE); /* Time Base configuration */ timer_reset(PPM_TIMER); timer_set_mode(PPM_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(PPM_TIMER, 0xFFFF); timer_set_prescaler(PPM_TIMER, (PPM_TIMER_CLK / (RC_PPM_TICKS_PER_USEC*ONE_MHZ_CLK)) - 1); /* TIM configuration: Input Capture mode --------------------- The Rising edge is used as active edge ------------------------------------------------------------ */ #if defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_POSITIVE timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_RISING); #elif defined PPM_PULSE_TYPE && PPM_PULSE_TYPE == PPM_PULSE_TYPE_NEGATIVE timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_FALLING); #else #error "Unknown PPM_PULSE_TYPE" #endif timer_ic_set_input(PPM_TIMER, PPM_CHANNEL, PPM_TIMER_INPUT); timer_ic_set_prescaler(PPM_TIMER, PPM_CHANNEL, TIM_IC_PSC_OFF); timer_ic_set_filter(PPM_TIMER, PPM_CHANNEL, TIM_IC_OFF); /* Enable timer Interrupt(s). */ nvic_set_priority(PPM_IRQ, 2); nvic_enable_irq(PPM_IRQ); #ifdef PPM_IRQ2 nvic_set_priority(PPM_IRQ2, 2); nvic_enable_irq(PPM_IRQ2); #endif /* Enable the Capture/Compare and Update interrupt requests. */ timer_enable_irq(PPM_TIMER, (PPM_CC_IE | TIM_DIER_UIE)); /* Enable capture channel. */ timer_ic_enable(PPM_TIMER, PPM_CHANNEL); /* TIM enable counter */ timer_enable_counter(PPM_TIMER); ppm_last_pulse_time = 0; ppm_cur_pulse = RADIO_CONTROL_NB_CHANNEL; timer_rollover_cnt = 0; }
void ppm_arch_init ( void ) { /* timer clock enable */ rcc_peripheral_enable_clock(PPM_RCC, PPM_PERIPHERAL); /* GPIOA clock enable */ rcc_peripheral_enable_clock(&RCC_APB2ENR, PPM_GPIO_PERIPHERAL); /* timer gpio configuration */ gpio_set_mode(PPM_GPIO_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, PPM_GPIO_PIN); /* Time Base configuration */ timer_reset(PPM_TIMER); timer_set_mode(PPM_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(PPM_TIMER, 0xFFFF); timer_set_prescaler(PPM_TIMER, 0x8); /* TIM2 configuration: Input Capture mode --------------------- The external signal is connected to TIM2 CH2 pin (PA.01) The Rising edge is used as active edge, ------------------------------------------------------------ */ timer_ic_set_polarity(PPM_TIMER, PPM_CHANNEL, TIM_IC_RISING); timer_ic_set_input(PPM_TIMER, PPM_CHANNEL, PPM_TIMER_INPUT); timer_ic_set_prescaler(PPM_TIMER, PPM_CHANNEL, TIM_IC_PSC_OFF); timer_ic_set_filter(PPM_TIMER, PPM_CHANNEL, TIM_IC_OFF); /* Enable timer Interrupt(s). */ nvic_set_priority(PPM_IRQ, 2); nvic_enable_irq(PPM_IRQ); #ifdef PPM_IRQ2 nvic_set_priority(PPM_IRQ2, 2); nvic_enable_irq(PPM_IRQ2); #endif /* Enable the CC2 and Update interrupt requests. */ timer_enable_irq(PPM_TIMER, PPM_IRQ_FLAGS); /* Enable capture channel. */ timer_ic_enable(PPM_TIMER, PPM_CHANNEL); /* TIM2 enable counter */ timer_enable_counter(PPM_TIMER); ppm_last_pulse_time = 0; ppm_cur_pulse = RADIO_CONTROL_NB_CHANNEL; timer_rollover_cnt = 0; }
void freq_capture_setup(void) { /* Configure PE11 (AF1: TIM1_CH2) (SYNC_IN). */ rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPEEN); gpio_mode_setup(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11); gpio_set_af(GPIOE, GPIO_AF1, GPIO11); /* Timer1: Input compare */ /* Enable timer clock. */ rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_TIM1EN); /* Reset timer. */ timer_reset(TIM1); /* Configure timer1. */ timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, // Internal clock TIM_CR1_CMS_EDGE, // Edge synchronization TIM_CR1_DIR_UP); // Count upward timer_set_prescaler(TIM1, TIMER1_PRESCALER); timer_set_period(TIM1, TIMER1_PERIOD); //Sets TIM1_ARR timer_continuous_mode(TIM1); /* Configure PE13: Toggle pin on falling edge via interrupt */ //rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPEEN); //gpio_mode_setup(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLDOWN, GPIO13); /* Configure input capture. */ timer_ic_disable(TIM1, TIM_IC2); timer_ic_set_polarity(TIM1, TIM_IC2, TIM_IC_RISING); timer_ic_set_prescaler(TIM1, TIM_IC2, TIM_IC_PSC_OFF); timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI2); // See RM, p. 561: digital filter //timer_ic_set_filter(TIM1, TIM_IC2, TIM_IC_DTF_DIV_32_N_8); timer_ic_set_filter(TIM1, TIM_IC2, TIM_IC_OFF); timer_ic_enable(TIM1, TIM_IC2); /* Enable counter. */ timer_enable_counter(TIM1); timer_clear_flag (TIM1, TIM_SR_CC2IF); /* Enable IRQs */ nvic_enable_irq(NVIC_TIM1_UP_TIM10_IRQ); timer_enable_irq(TIM1, TIM_DIER_UIE); nvic_enable_irq(NVIC_TIM1_CC_IRQ); timer_enable_irq(TIM1, TIM_DIER_CC2IE); }
static void tim_setup(void) { /* Enable TIM1 clock. */ rcc_periph_clock_enable(RCC_TIM1); /* Configure TIM1_CH1 and TIM1_CH2 as inputs */ gpio_set_mode(GPIO_BANK_TIM1_CH1, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_TIM1_CH1); gpio_set_mode(GPIO_BANK_TIM1_CH2, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO_TIM1_CH2); /* Enable TIM1 interrupt. */ nvic_enable_irq(NVIC_TIM1_CC_IRQ); /* Reset TIM1 peripheral. */ /* timer_reset(TIM1); */ timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); /* set prescaler value -> 1us */ timer_set_prescaler(TIM1, 72 - 1); timer_set_period(TIM1, 0xFFFF); timer_set_repetition_counter(TIM1, 0); /* Enable preload */ /* timer_disable_preload(TIM1); */ /* Continous mode */ timer_continuous_mode(TIM1); /* configure Channel 1 */ timer_ic_set_input(TIM1, TIM_IC1, TIM_IC_IN_TI1); timer_ic_set_filter(TIM1, TIM_IC1, TIM_IC_OFF); timer_ic_set_polarity(TIM1, TIM_IC1, TIM_IC_RISING); timer_ic_set_prescaler(TIM1, TIM_IC1, TIM_IC_PSC_OFF); timer_ic_enable(TIM1, TIM_IC1); timer_clear_flag(TIM1, TIM_SR_CC1IF); timer_enable_irq(TIM1, TIM_DIER_CC1IE); /* configure Channel 2 */ timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI2); timer_ic_set_filter(TIM1, TIM_IC2, TIM_IC_OFF); timer_ic_set_polarity(TIM1, TIM_IC2, TIM_IC_RISING); timer_ic_set_prescaler(TIM1, TIM_IC2, TIM_IC_PSC_OFF); timer_ic_enable(TIM1, TIM_IC2); timer_clear_flag(TIM1, TIM_SR_CC2IF); timer_enable_irq(TIM1, TIM_DIER_CC2IE); timer_enable_counter(TIM1); #if 0 /* Disable outputs. */ timer_disable_oc_output(TIM1, TIM_OC1); timer_disable_oc_output(TIM1, TIM_OC2); timer_disable_oc_output(TIM1, TIM_OC3); timer_disable_oc_output(TIM1, TIM_OC4); /* -- OC1 configuration -- */ /* Configure global mode of line 1. */ timer_disable_oc_clear(TIM1, TIM_OC1); timer_disable_oc_preload(TIM1, TIM_OC1); timer_set_oc_slow_mode(TIM1, TIM_OC1); timer_set_oc_mode(TIM1, TIM_OC1, TIM_OCM_FROZEN); /* Set the capture compare value for OC1. */ timer_set_oc_value(TIM1, TIM_OC1, 1000); /* ---- */ /* ARR reload enable. */ timer_disable_preload(TIM1); /* Counter enable. */ timer_enable_counter(TIM1); /* Enable commutation interrupt. */ timer_enable_irq(TIM1, TIM_DIER_CC1IE); #endif }