Question
STM32L151: Timer Capture Input (TIMx_CHy vs TIMx_ICy)
Posted on June 23, 2014 at 17:02
Hi,
I'm using a STM32L151 and would like to connect the pin PA2 to one of the timer capture inputs. According to the reference manual and the example InputCaptureRouting in the STM32L1xx_StdPeriph_Lib_V1.1.1, this code should do the trick:RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOAEN, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_COMP | RCC_APB1Periph_PWR, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = 0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); &sharpif 1// Route PA2 to TIM2_IC3
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_RI); SYSCFG_RITIMSelect(TIM_Select_TIM2); SYSCFG_RITIMInputCaptureConfig(RI_InputCapture_IC3, RI_InputCaptureRouting_0/*0: PA2*/); &sharpelse// Use PA2 as TIM2_CH3
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_TIM2); &sharpendif TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock/*Hz*/ / 100000/*10us/s*/) - 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Use TIM2_IC3 as Input Capture TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 3; // Filter: 8 clocks TIM_ICInit(TIM2, &TIM_ICInitStructure); TIM_Cmd(TIM2, ENABLE); This should connect the pin PA2 to the Timer Capture Input TIM2_IC3. I would expect that pin PA2 is in a high impedance state in this case, but on a scope it is easy to see that it is driven low by the STM32. If I change the&sharpif 1
in the snippet above to&sharpif 0
, everything works as expected: PA2 is in high impedance state and a rising edge captures TIM2_CNT in register TIM2_CCR3. Actually, I need to use PA10 as a Timer Capture Input which is only possible if it's routed via the RI module. I assumed that (if correctly configured) theTIMx_ICy
signals are equivalent to the accordingTIMx_CHy
signals. Is this correct (the documentation is not 100% clear in this point)? #worst-forum-software-ever #stm32-timer-capture-input