Skip to main content
Michael98006
Associate III
July 6, 2019
Solved

Starting a timer on a GPIO input rising edge

  • July 6, 2019
  • 4 replies
  • 4090 views

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!

This topic has been closed for replies.
Best answer by Piranha

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.

4 replies

Piranha
PiranhaBest answer
Principal III
July 6, 2019

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.
Michael98006
Associate III
July 8, 2019

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!

waclawek.jan
Super User
July 6, 2019

> 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