cancel
Showing results for 
Search instead for 
Did you mean: 

Starting a timer on a GPIO input rising edge

Michael98006
Associate III

I'm trying to set up TIM2 timer on my NUCLEO-H743ZI2 board (STM32H743ZI MCU) to start TIM2 timer on a rising edge of a GPIO input pin. I'm using STM32CubeIDE with STM32CubeMx plugin. Is it possible to set up the hardware to start the timer automatically without calling HAL API to start the timer from the GPIO EXTI interrupt callback function? The timer seems to have lots of trigger inputs for this, but I'm not sure how to route GPIO inputs to timers. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Piranha
Chief II

Most likely You need TIMx_SMCR field SMS set to "Combined reset + trigger mode" (note that bit SMS[3] is actually separate bit 16 in that register). And for input trigger You have two options:

  1. Use TIMx_ETR pin and set TIMx_SMCR field TS to ETRF.
  2. Use TIMx_CH1/2 and set TIMx_SMCR field TS to TI1FP1/TI2FP2 respectively.

View solution in original post

4 REPLIES 4
Piranha
Chief II

Most likely You need TIMx_SMCR field SMS set to "Combined reset + trigger mode" (note that bit SMS[3] is actually separate bit 16 in that register). And for input trigger You have two options:

  1. Use TIMx_ETR pin and set TIMx_SMCR field TS to ETRF.
  2. Use TIMx_CH1/2 and set TIMx_SMCR field TS to TI1FP1/TI2FP2 respectively.

> how to route GPIO inputs to timers

You can't route an arbitrary pin to a timer. Look at the pin assignment table in the datasheet, there are pins labeled TIMx_CHy, you can use only them for TIMx.

You set them in the respective GPIOx_MODER as AF, and then chose the proper AF number in GPIOx_AFR.

Then you set up the timer as Piranha said above.

There's probably a way how to do this by some wild clicking in CubeMX; I don't know, I don't use CubeMX.

JW

I didn't know how TIMx_ETR pins worked, but now I understand what they're for. Thank you so much! This solved my problem.

By the way, this turns out to be easy to set up via CubeMx, except it gets confusing when you have to choose between trigger sources "ETR1" and "ETR1 through remap". I chose "ETR1" as my trigger source, and it worked. Also, "Combined reset + trigger mode" didn't work for me since I was using a button to trigger the timer, and CubeMx didn't have a drop-down to select triggering on a rising edge only. So I changed the slave mode to "Trigger Mode", and it worked like a charm.

Thanks again!

Hi @Piranha​ ,@Community member​ 

My requirement is to start timer from a gpio pin (TIMx_ETR) and when gpio pin set i want to toggle the led pin using timer callback function.

I have followed the instructions as you mentioned and added implementation of toggle pin in HAL_TIM_TriggerCallback() but iam unable to toggle the pin , can you please explain the steps after configuring using Cube MX.

what do we need to add in the main.c to work properly after configuring using CubeMX.0693W00000DqLjmQAF.png