AnsweredAssumed Answered

TIM3 CH2 external counter configuration

Question asked by Gergo Santha on Apr 10, 2018
Latest reply on Apr 11, 2018 by Gergo Santha

Hi!

 

I want to use an external clock signal for timing. This clock signal is ~130 kHz. I want to count N pulses with a timer and I want an IRQ after N pulses. For this purpose I'd like to use TIM3 in external counter mode 1. The input pin is PC7 which is TIM3_CH2 in alternate function 2. I'm using the LL driver. So far I came up with this but it's not working no matter what value I write in to the ARR register. This code is based on several examples in CUBE_FW_L4 and other web sources. I know that there are lots of code out there but it seems that people are using different drivers like standard, HAL etc. I call timing_init_timing() function to configure the pins, timer etc. The function timing_set_ARR is used from a command terminal and I get zero calling timing_get_counter() function and the timcnt readout is always 1.

 

I already had my laps on reference manual but I really not get the concept as the driver is using confusing names which I couldn't found in the RM of the MCU. I'm missing some points probably so any help would be great.

 

void timing_configure_input_pin(void) {
  // Clock
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOC);
 
  // TIM3 configuration on GPIO PC7, alternate function 2
  LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_7, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetPinPull(GPIOC, LL_GPIO_PIN_7, LL_GPIO_PULL_DOWN);
  LL_GPIO_SetPinSpeed(GPIOC, LL_GPIO_PIN_7, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetAFPin_0_7(GPIOC, LL_GPIO_PIN_7, LL_GPIO_AF_2);
 
}
 
void timing_configure_nvic(void) {
  NVIC_SetPriority(TIM3_IRQn, 0);
  NVIC_EnableIRQ(TIM3_IRQn);
}
 
void timing_configure_timer(void) {
  // Clock
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
 
  LL_TIM_IC_SetFilter(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
 
  LL_TIM_IC_SetPrescaler(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV8);
 
  LL_TIM_IC_SetPolarity(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
 
  LL_TIM_IC_SetActiveInput(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
 
  LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_EXT_MODE1);
 
  LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH2);
 
  LL_TIM_EnableIT_UPDATE(TIM3);
 
  LL_TIM_GenerateEvent_UPDATE(TIM3);
}
 
void timing_set_ARR(uint16_t val) {
  LL_TIM_SetAutoReload(TIM3, val);  
}
 
uint32_t timing_get_counter(void) {
  return LL_TIM_GetCounter(TIM3);
}
 
void timing_init_timing(void) {
  timing_configure_input_pin();
  timing_configure_nvic();
  timing_configure_timer();
}
 
void TIM3_IRQHandler(void) {
  if(LL_TIM_IsActiveFlag_UPDATE(TIM3) == 1) {
    // Clear the update interrupt flag
    LL_TIM_ClearFlag_UPDATE(TIM3);
 
    board_toggle_NUCLEO_LED();
    timcnt++;
  }
}

Outcomes