Author Topic: STM32 Input Capture Mode Not Working  (Read 1917 times)

0 Members and 1 Guest are viewing this topic.

Offline EteslaTopic starter

  • Regular Contributor
  • *
  • Posts: 149
  • Country: us
STM32 Input Capture Mode Not Working
« on: July 29, 2022, 03:41:43 am »
Hi All. I'm just trying to get the most basic functionality of input capture mode to do anything. I can do so with the pin PB9, but I cannot seem to do so with pin PA7. I want to use PA7.

STM32F072 MCU

I'm trying to capture a rising edge and lock in a timers value when I do so.

I'm trying to use TIM17 for no specific reason.
I see that TIM17_CH1 is mapped as alternate function 5 on PA7. I want to use PA7. (It's also mapped to PB9. If I use PB9 it works great, but it does not seem to work with PA7???)

So far I have:
int main(void)
{
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // enable GPIO Port A clock
RCC->APB2ENR |= RCC_APB2ENR_TIM17EN; // enable the clock for TIM17
GPIOA->MODER |= GPIO_MODER_MODER7_1; // set PA7 into alternate function mode
GPIOA->AFR[0] |= 0x50000000; // select AF5 for PA7

// If I try to use PB9 instead of PA7 then it works fine. Why can't I use PA7 though???
//RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable GPIO Port B clock
//GPIOB->MODER |= GPIO_MODER_MODER9_1; // set PB9 into alternate function mode
//GPIOA->AFR[1] |= 0x20; // select AF2 for PB9

TIM17->CCMR1 |= TIM_CCMR1_CC1S_0; // input on TI1
TIM17->CCER |= TIM_CCER_CC1E; // enable the input
TIM17->CR1 |= TIM_CR1_CEN; // turn on the timer

while(1); // give PA7 a bunch of rising and falling edges while debugging to try to get CCR1 or CC1IF to do anything

}

I put rising and falling edges on PA7 expecting something to happen. While debugging I look for:
The CC1IF bit to flip to a 1 when I put a rising edge on PA7
The CCR1 register to change from the default value of 0.

Neither of these things ever happen. Those things do happen when I use PB9.

What am I doing wrong in the setup of this input capture with PA7? Is there an extra register somewhere dictating whether I can use PA7 VS PB9 as TIM17_CH1? Was there a typo in the documentation and PA7 is not actually associated with TIM17_CH1? What's going on here?
« Last Edit: July 29, 2022, 04:02:25 am by Etesla »
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: STM32 Input Capture Mode Not Working
« Reply #1 on: July 29, 2022, 05:36:04 am »
I did not check if it is also on the MCU you are using, but I know from the F103 and F303 that you have to enable the clock for the alternate function peripheral too.

Check the RCC enable registers to see where the bit for it sits.

Edit: I just downloaded the manual for your MCU and it does not use the mentioned above.

It does have this section, so the question is what values you set into the registers?

Quote
/* This sequence select AF2 for GPIOA4, 8 and 9. This can be easily adapted
with another port by changing all GPIOA references by another GPIO port,
and the alternate function number can be changed by replacing 0x04 or
0x02 for
each pin by the targeted alternate function in the 2 last code lines. */
/* (1) Enable the peripheral clock of GPIOA */
/* (2) Select alternate function mode on GPIOA pin 4, 8 and 9 */
/* (3) Select AF4 on PA4 in AFRL for TIM14_CH1 */
/* (4) Select AF2 on PA8 and PA9 in AFRH for TIM1_CH1 and TIM1_CH2 */
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; /* (1) */
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER4 | GPIO_MODER_MODER8
| GPIO_MODER_MODER9)) | GPIO_MODER_MODER4_1
| GPIO_MODER_MODER8_1 | GPIO_MODER_MODER9_1; /* (2) */
GPIOA->AFR[0] |= 0x04 << GPIO_AFRL_AFRL4_Pos; /* (3) */
GPIOA->AFR[1] |= (0x02 << GPIO_AFRL_AFRH8_Pos) | (0x02 <<
GPIO_AFRL_AFRH9_Pos); /* (4) */

Edit2:

Code: [Select]
int main(void)
{
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // enable GPIO Port A clock
RCC->APB2ENR |= RCC_APB2ENR_TIM17EN; // enable the clock for TIM17
GPIOA->MODER |= GPIO_MODER_MODER7_1; // set PA7 into alternate function mode
GPIOA->AFR[0] |= 0x50000000; // select AF5 for PA7

// If I try to use PB9 instead of PA7 then it works fine. Why can't I use PA7 though???
//RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable GPIO Port B clock
//GPIOB->MODER |= GPIO_MODER_MODER9_1; // set PB9 into alternate function mode
//GPIOA->AFR[1] |= 0x20; // select AF2 for PB9

TIM17->CCMR1 |= TIM_CCMR1_CC1S_0; // input on TI1
TIM17->CCER |= TIM_CCER_CC1E; // enable the input
TIM17->CR1 |= TIM_CR1_CEN; // turn on the timer

while(1); // give PA7 a bunch of rising and falling edges while debugging to try to get CCR1 or CC1IF to do anything

}

You have commented it out and is for PB9, but you are addressing the wrong register for it "GPIOA->AFR[1]" so strange that that is working.
« Last Edit: July 29, 2022, 06:12:11 am by pcprogrammer »
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4228
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: STM32 Input Capture Mode Not Working
« Reply #2 on: July 29, 2022, 06:12:45 am »
Is it possible that the AFR and MODER registers bits aren't zero before your code runs? Your code ORs the existing values with new data but doesnt AND with a bit mask to clear any 1s.

PA7 has analogue capability, which PB9 doesn't. If it's set in analogue mode (MODER = 0b11) before this code snippet, it won't be changed back into AF mode and won't work as a digital input at all.

Offline wek

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: sk
Re: STM32 Input Capture Mode Not Working
« Reply #3 on: July 29, 2022, 09:40:10 am »
Read out and check/post content of TIM and relevant GPIO registers.

Check hardware connection by setting given pin as GPIO Out, toggling and observing.

JW

 

Offline EteslaTopic starter

  • Regular Contributor
  • *
  • Posts: 149
  • Country: us
Re: STM32 Input Capture Mode Not Working
« Reply #4 on: July 29, 2022, 01:28:31 pm »
Here's the registers if I try to use PA7:




I think AFRL from the datasheet is = AFR[0] right? And AFRH is AFR[1]? So I think my setup for PA7 is correct so far as I can tell?
 

Offline EteslaTopic starter

  • Regular Contributor
  • *
  • Posts: 149
  • Country: us
Re: STM32 Input Capture Mode Not Working
« Reply #5 on: July 29, 2022, 01:31:36 pm »
I did not check if it is also on the MCU you are using, but I know from the F103 and F303 that you have to enable the clock for the alternate function peripheral too.

Check the RCC enable registers to see where the bit for it sits.

Edit: I just downloaded the manual for your MCU and it does not use the mentioned above.

It does have this section, so the question is what values you set into the registers?



Edit2:


int main(void)
{
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // enable GPIO Port A clock
RCC->APB2ENR |= RCC_APB2ENR_TIM17EN; // enable the clock for TIM17
GPIOA->MODER |= GPIO_MODER_MODER7_1; // set PA7 into alternate function mode
GPIOA->AFR[0] |= 0x50000000; // select AF5 for PA7

// If I try to use PB9 instead of PA7 then it works fine. Why can't I use PA7 though???
//RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable GPIO Port B clock
//GPIOB->MODER |= GPIO_MODER_MODER9_1; // set PB9 into alternate function mode
//GPIOA->AFR[1] |= 0x20; // select AF2 for PB9

TIM17->CCMR1 |= TIM_CCMR1_CC1S_0; // input on TI1
TIM17->CCER |= TIM_CCER_CC1E; // enable the input
TIM17->CR1 |= TIM_CR1_CEN; // turn on the timer

while(1); // give PA7 a bunch of rising and falling edges while debugging to try to get CCR1 or CC1IF to do anything

}


You have commented it out and is for PB9, but you are addressing the wrong register for it "GPIOA->AFR[1]" so strange that that is working.


That's a typo. In real life when I tested it and it worked it was GPIOB->AFR[1]. I added the comment retroactively.
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3710
  • Country: nl
Re: STM32 Input Capture Mode Not Working
« Reply #6 on: July 29, 2022, 02:11:12 pm »
The values you show in, I guess, a debug session seem to be correct, but the manual also states this:

Quote
To use an I/O in a given configuration, the user has to proceed as follows:
• Debug function: after each device reset these pins are assigned as alternate function pins immediately usable by the debugger host
• GPIO: configure the desired I/O as output, input or analog in the GPIOx_MODER register.
• Peripheral alternate function:
– Connect the I/O to the desired AFx in one of the GPIOx_AFRL or GPIOx_AFRH register.
Select the type, pull-up/pull-down and output speed via the GPIOx_OTYPER, GPIOx_PUPDR and GPIOx_OSPEEDER registers, respectively.
– Configure the desired I/O as an alternate function in the GPIOx_MODER register.

Quote
8.3.11 Alternate function configuration
When the I/O port is programmed as alternate function:
• The output buffer can be configured in open-drain or push-pull mode
• The output buffer is driven by the signals coming from the peripheral (transmitter enable and data)
• The Schmitt trigger input is activated
• The weak pull-up and pull-down resistors are activated or not depending on the value in the GPIOx_PUPDR register
• The data present on the I/O pin are sampled into the input data register every AHB clock cycle
• A read access to the input data register gets the I/O state

So it looks like that even when the alternate function is selected other settings on the IO pin can influence things.

Edit: You can check with your debugger if the state of PA7 changes when you make the pin high or low. It should reflect in GPIOA->IDR.
« Last Edit: July 29, 2022, 02:21:04 pm by pcprogrammer »
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: sk
Re: STM32 Input Capture Mode Not Working
« Reply #7 on: July 29, 2022, 03:32:54 pm »
Registers look good. Now second part of my post above [EDIT] or what's in edit of pcprogrammer's post above [/EDIT]

JW
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: STM32 Input Capture Mode Not Working
« Reply #8 on: July 29, 2022, 05:41:35 pm »
Maybe you misidentified your pins.

This is for a g031 nucleo board, not much different, and works for either pin-
Code: [Select]
#include "MyStm32.hpp"

int main(){

    RCC->APBENR2 |= 1<<18; //TIM17 clock enable
    TIM17->CCMR1 = 1;//TI1, (PA7/AF5 or PB9/AF2)
    TIM17->CCER = 1; //capt enable
    TIM17->CR1 = 1; //TIM17 enable

    GpioPin( PINS::PA7 ).altFunc( AF5 ); // normally a pull wanted so no floating pin-> .pull( PULLDOWN )
    //GpioPin( PINS::PB9 ).altFunc( AF2 ); //but floating in this case means we can just touch the pin to get transitions

    while(1){
        uart << "TIM17 CCR1: " << TIM17->CCR1 << endl;
        //shows capture values ok
    }

}
The only thing happening in the pin init is rcc enable for port, and setting the afr register then changing mode to alternate.

edit- Also, if both pins are set to alternate function both become an input to the timer capture.
« Last Edit: July 29, 2022, 09:06:46 pm by cv007 »
 

Offline EteslaTopic starter

  • Regular Contributor
  • *
  • Posts: 149
  • Country: us
Re: STM32 Input Capture Mode Not Working
« Reply #9 on: July 29, 2022, 10:55:31 pm »
It works now. Turns out the dev board I was using had an open solder bridge between the MCU pin and the header pin. I thought I had beeped it out between the header pin and MCU before, but I must have had the pin tied to ground or Vcc or something while beeping it out causing me to mistakenly think they were connected. Thanks for the help.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf