static void platform_init_freqsensor(unsigned char pin) { uint32_t tim; switch(pin) { case 1: /* TIM1 CH1 */ tim = TIM1; gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8); gpio_set_af(GPIOA, GPIO_AF1, GPIO8); break; }; timer_reset(tim); timer_set_mode(tim, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_set_period(tim, 0xFFFFFFFF); timer_disable_preload(tim); timer_continuous_mode(tim); /* Setup output compare registers */ timer_disable_oc_output(tim, TIM_OC1); timer_disable_oc_output(tim, TIM_OC2); timer_disable_oc_output(tim, TIM_OC3); timer_disable_oc_output(tim, TIM_OC4); /* Set up compare */ timer_ic_set_input(tim, TIM_IC1, TIM_IC_IN_TI1); timer_ic_set_filter(tim, TIM_IC1, TIM_IC_CK_INT_N_8); timer_ic_set_polarity(tim, TIM_IC1, TIM_IC_RISING); timer_set_prescaler(tim, 2*SENSOR_FREQ_DIVIDER); /* Prescale set to map up to 20kHz */ timer_slave_set_mode(tim, TIM_SMCR_SMS_RM); timer_slave_set_trigger(tim, TIM_SMCR_TS_IT1FP1); timer_ic_enable(tim, TIM_IC1); timer_enable_counter(tim); timer_enable_irq(tim, TIM_DIER_CC1IE); switch(pin) { case 1: nvic_enable_irq(NVIC_TIM1_CC_IRQ); nvic_set_priority(NVIC_TIM1_CC_IRQ, 64); break; } }
void pwm_input_init(void) { int i; // initialize the arrays to 0 for (i = 0; i < PWM_INPUT_NB; i++) { pwm_input_duty_tics[i] = 0; pwm_input_duty_valid[i] = 0; pwm_input_period_tics[i] = 0; pwm_input_period_valid[i] = 0; } /** Configure timers * - timer clock enable * - base configuration * - enable counter */ #if USE_PWM_INPUT_TIM1 rcc_periph_clock_enable(RCC_TIM1); pwm_input_set_timer(TIM1, PWM_INPUT_TIM1_TICKS_PER_USEC); #endif #if USE_PWM_INPUT_TIM2 rcc_periph_clock_enable(RCC_TIM2); pwm_input_set_timer(TIM2, PWM_INPUT_TIM2_TICKS_PER_USEC); #endif #if USE_PWM_INPUT_TIM3 rcc_periph_clock_enable(RCC_TIM3); pwm_input_set_timer(TIM3, PWM_INPUT_TIM3_TICKS_PER_USEC); #endif #if USE_PWM_INPUT_TIM5 rcc_periph_clock_enable(RCC_TIM5); pwm_input_set_timer(TIM5, PWM_INPUT_TIM5_TICKS_PER_USEC); #endif #if USE_PWM_INPUT_TIM8 rcc_periph_clock_enable(RCC_TIM8); pwm_input_set_timer(TIM8, PWM_INPUT_TIM8_TICKS_PER_USEC); #endif #if USE_PWM_INPUT_TIM9 rcc_periph_clock_enable(RCC_TIM9); pwm_input_set_timer(TIM9, PWM_INPUT_TIM9_TICKS_PER_USEC); #endif #ifdef USE_PWM_INPUT1 /* GPIO configuration as input capture for timer */ gpio_setup_pin_af(PWM_INPUT1_GPIO_PORT, PWM_INPUT1_GPIO_PIN, PWM_INPUT1_GPIO_AF, FALSE); /** TIM configuration: Input Capture mode * Two IC signals are mapped to the same TI input */ timer_ic_set_input(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_PERIOD, PWM_INPUT1_TIMER_INPUT); timer_ic_set_input(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_DUTY, PWM_INPUT1_TIMER_INPUT); #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW timer_ic_set_polarity(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_PERIOD, TIM_IC_RISING); timer_ic_set_polarity(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_DUTY, TIM_IC_FALLING); #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH timer_ic_set_polarity(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_PERIOD, TIM_IC_FALLING); timer_ic_set_polarity(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_DUTY, TIM_IC_RISING); #endif /* Select the valid trigger input */ timer_slave_set_trigger(PWM_INPUT1_TIMER, PWM_INPUT1_SLAVE_TRIG); /* Configure the slave mode controller in reset mode */ timer_slave_set_mode(PWM_INPUT1_TIMER, TIM_SMCR_SMS_RM); /* Enable timer Interrupt(s). */ nvic_set_priority(PWM_INPUT1_IRQ, PWM_INPUT_IRQ_PRIO); nvic_enable_irq(PWM_INPUT1_IRQ); #ifdef PWM_INPUT1_IRQ2 nvic_set_priority(PWM_INPUT1_IRQ2, PWM_INPUT_IRQ_PRIO); nvic_enable_irq(PWM_INPUT1_IRQ2); #endif /* Enable the Capture/Compare and Update interrupt requests. */ timer_enable_irq(PWM_INPUT1_TIMER, (PWM_INPUT1_CC_IE | TIM_DIER_UIE)); /* Enable capture channel. */ timer_ic_enable(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_PERIOD); timer_ic_enable(PWM_INPUT1_TIMER, PWM_INPUT1_CHANNEL_DUTY); #endif #ifdef USE_PWM_INPUT2 /* GPIO configuration as input capture for timer */ gpio_setup_pin_af(PWM_INPUT2_GPIO_PORT, PWM_INPUT2_GPIO_PIN, PWM_INPUT2_GPIO_AF, FALSE); /** TIM configuration: Input Capture mode * Two IC signals are mapped to the same TI input */ timer_ic_set_input(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_PERIOD, PWM_INPUT2_TIMER_INPUT); timer_ic_set_input(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_DUTY, PWM_INPUT2_TIMER_INPUT); #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW timer_ic_set_polarity(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_PERIOD, TIM_IC_RISING); timer_ic_set_polarity(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_DUTY, TIM_IC_FALLING); #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH timer_ic_set_polarity(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_PERIOD, TIM_IC_FALLING); timer_ic_set_polarity(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_DUTY, TIM_IC_RISING); #endif /* Select the valid trigger input */ timer_slave_set_trigger(PWM_INPUT2_TIMER, PWM_INPUT2_SLAVE_TRIG); /* Configure the slave mode controller in reset mode */ timer_slave_set_mode(PWM_INPUT2_TIMER, TIM_SMCR_SMS_RM); /* Enable timer Interrupt(s). */ nvic_set_priority(PWM_INPUT2_IRQ, PWM_INPUT_IRQ_PRIO); nvic_enable_irq(PWM_INPUT2_IRQ); #ifdef PWM_INPUT2_IRQ2 nvic_set_priority(PWM_INPUT2_IRQ2, PWM_INPUT_IRQ_PRIO); nvic_enable_irq(PWM_INPUT2_IRQ2); #endif /* Enable the Capture/Compare and Update interrupt requests. */ timer_enable_irq(PWM_INPUT2_TIMER, (PWM_INPUT2_CC_IE | TIM_DIER_UIE)); /* Enable capture channel. */ timer_ic_enable(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_PERIOD); timer_ic_enable(PWM_INPUT2_TIMER, PWM_INPUT2_CHANNEL_DUTY); #endif }