Simple 32F417 TIM1 usage to measure TIM1_CH1 pulse duration
I have PE9 as an input, and using AF1, PE9 should be connecting to TIM1_CH1 input.
Input is 400Hz square wave, 0 to +3.3V.
This is the code
// Set up TIM1 for ~400Hz waveform period measurement, via PE9
static void KDE_config_TIM1_period_measure(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__GPIOE_CLK_ENABLE(); // already enabled in switch init but never mind
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; // PE9 = TIM1_CH1
GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // PE9 = TIM1_CH1 input, not the output!
GPIO_InitStruct.Pull = GPIO_PULLUP; // PE9 = pullup because comparator driving it is open drain o/p
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
// Set up TIM1 to count up and on PE9 +ve edge transfer the count to CCR and reload with 0
__HAL_RCC_TIM1_CLK_ENABLE(); // TIM1EN=1 in RCC2ENR and does a short delay
TIM1->CR1 = 0; // TIM1 upcounter, counter disabled for now
TIM1->ARR = 0xffff; // auto reload value
TIM1->PSC = 3; // prescaler=4, for 10.5MHz counter speed
TIM1->CCMR1 = (1 << 4) // IC1F: input filter = 2
|(0 << 2) // IC1PSC: input prescaler off
|(1 << 0); // CC1S: 01 = channel is input, from TI1
TIM1->CCER = (0 << 3) // CC1NP: CC1 rising edge capture
|(0 << 2) // CC1NE: oc1n not active
|(0 << 1) // CC1P: rising edge capture
|(1 << 0); // CC1E: enable CC1 capture
TIM1->EGR = 0; // CC1G=0
TIM1->CNT = 0;
TIM1->CR2 = 0; // TI1S=0 - TIM1_CH1 pin is connected to TI1 input
TIM1->RCR = 0;
TIM1->SMCR = 0; // slave mode disabled, internal clock source
TIM1->DIER = 0; // interrupts disabled
TIM1->CR1 = (1 << 0); // CEN=1 - enable TIM1
}
and this is the RTOS tack which is reading CCR1
while (1)
{
uint16_t cnt = TIM1->CNT;
// TIM1->EGR = (1<<1); // force capture CNT to CCR1
uint16_t ccr1 = TIM1->CCR1;
uint16_t sr = TIM1->SR;
debug_thread_printf("===== TIM1 = %5u %5u %05x", cnt, ccr1, sr);
osDelay(1000);
}The output I am getting is
==== TIM1 = 63286 0 0001d
with various values for the CNT, so the counter is running ok. If I uncomment the above force capture line, I get the software-triggered capture taking place (3rd value not shown here)
===== TIM1 = 16491 16493
===== TIM1 = 58404 58406
===== TIM1 = 34750 34753
===== TIM1 = 11067 11069
===== TIM1 = 52958 52961
===== TIM1 = 29275 29277
so it just looks like the PE9 signal is not getting through to IC1PS. I have checked it with a scope on the pin. I al also suspecting that my PE9 config is not right; I cannot find any reference to actual init code for PE9 for the TIM1_CH1 input; all examples I found show PE9 to be the TIM1_CH1 pwm output. Is it correct that selecting AF1 and selecting PE9 as an input is sufficient to select TIM1_CH1 input?
Many thanks for any tips. I've spent 2 days on this so far, and reading the RM, appnots, etc. Page 662/1751 of the RM tells you how to do exactly this, so something must be missing.
