STM32 Timers

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
I'm just reading about timers and they seem more sophisticated than I imagined.

One thing I'm not clear is whether we can assign a timer directly to a GPIO in some way other than through software.

I have a rudimentary app for the STM32F407G-DISC1 board that uses TIM2 in interrupt mode, in the handler I toggle pin PD15 and I can see on the scope a (slightly unstable) square wave - as I expect.

I've got that toggling at around 24.6 KHz (using some trial and error values in the prescaler and period settings).

Is there a way though to tell the timer to simply "drive" a GPIO directly without the need to have a handler and interrupts etc.

I'd imagine that the frequency of such a signal could then be higher and its stability greater.

Thanks
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
Doesn't the processor chip's data sheet tell you how to do this?
The reference manual is 1,750 pages long and presupposes some reasonable familiarity with the device, if it was that straightforward I'd not have asked the question here!
 

OBW0549

Joined Mar 2, 2015
3,566
The reference manual is 1,750 pages long and presupposes some reasonable familiarity with the device, if it was that straightforward I'd not have asked the question here!
I doubt you'd need to read all 1749 pages of the manual; the information you're seeking is most likely in the section on timers (Section 17). Section 17.3.9, beginning on page 540, describes the Output Compare function of the timers; Output Compare (which is available on many other processors such as the PICs) allows timers to directly control output pins in various ways (toggle, single-shot, PWM, etc.) which I take it is what you want.

BTW, I came up with the foregoing with about 10 minutes of work, including locating the manual, downloading it, scanning through the TOC, and quick-reading the Timer subsection on OC.

Also, regarding familiarity, I wouldn't know an STM32F407 if one came up behind me and bit me on the ass.

It ain't that difficult to master this stuff.
 

MrChips

Joined Oct 2, 2009
30,706
In a nutshell, yes it can be done.
Can it be done without software? No. You need to at least initialize the appropriate peripheral module before it will run.
And as others have said, you do need to read the manual. And you don't need to read all 1749 pages of the User Manual, just the chapters that apply. Which Chapters? Well you need to glean over the information supplied by ST. I cannot think of another way.

If you need help getting started I am here to lend a hand. But you do have to apply some effort of your own. I learned all that I know mostly on my own.
 

Analog Ground

Joined Apr 24, 2019
460
Some timers have direct connections to pins. The most common one is the "Input Capture/Output Compare/PWM" timers. If all you want to do is produce a repetitive, output waveform, look into the "PWM" module. They go by different names but the keyword is PWM (Pulse Width Modulation). The reason they connect to the outside world is to control motors and such. Check it out. Maybe you could tell us exactly what you are trying to do with the signal?
 

MrChips

Joined Oct 2, 2009
30,706
Yes, you can connect GPIO pins to a timer clock input and timer outputs to suit the application.
Furthermore, you can make the timer trigger events such as read an ADC, transfer data to memory, output data to DAC, etc. entirely with no software required in the process.

I have an app that generates VGA graphics entirely from hardware with no software intervention.
You still need software to configure the hardware.
 

Analog Ground

Joined Apr 24, 2019
460
I wish people would stop calling them "GPIO Pins". In a modern microcontroller with reconfigurable I/O, I/O pins can connect to multiple, internal modules or "peripherals" and GPIO is just one type. Others modules could be a timer like a PWM peripheral, an ADC, whatever. Thanks for letting me vent.
 

MrChips

Joined Oct 2, 2009
30,706
I wish people would stop calling them "GPIO Pins". In a modern microcontroller with reconfigurable I/O, I/O pins can connect to multiple, internal modules or "peripherals" and GPIO is just one type. Others modules could be a timer like a PWM peripheral, an ADC, whatever. Thanks for letting me vent.
Sorry. I got the term primarily from working with ST Microelectronics STM32 MCUs.
This is the term they use predominantly in all their documentation and library calls. It has stuck.

ST AN4899
 

Analog Ground

Joined Apr 24, 2019
460
Sorry. I got the term primarily from working with ST Microelectronics STM32 MCUs.
This is the term they use predominantly in all their documentation and library calls. It has stuck.

ST AN4899
You are absolutely correct. STM is redefining the meaning of GPIO from "parallel, digital I/O" to mean a "pin which connects to lots of different stuff". NXP sticks to the old view. Maybe I need to modernize! Thanx!
 

mckenney

Joined Nov 10, 2018
125
STM is redefining the meaning of GPIO from "parallel, digital I/O" to mean a "pin which connects to lots of different stuff". NXP sticks to the old view.
I sympathize, but there is a need for a term which distinguishes them from power, JTAG, VREF, and other kinds of pins which the software can't control at all. I think we used to refer to "port pins", though that kind of has the same ring to it.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
I doubt you'd need to read all 1749 pages of the manual; the information you're seeking is most likely in the section on timers (Section 17). Section 17.3.9, beginning on page 540, describes the Output Compare function of the timers; Output Compare (which is available on many other processors such as the PICs) allows timers to directly control output pins in various ways (toggle, single-shot, PWM, etc.) which I take it is what you want.

BTW, I came up with the foregoing with about 10 minutes of work, including locating the manual, downloading it, scanning through the TOC, and quick-reading the Timer subsection on OC.

Also, regarding familiarity, I wouldn't know an STM32F407 if one came up behind me and bit me on the ass.

It ain't that difficult to master this stuff.
Well thank you for taking the time to dig that out.

I know that it isn't necessary to read the entire document, and I know that the details are likely in that document but that's a reference manual not an instruction book.

All I was seeking was conformation that yes, we can 100% do this and perhaps some sample code for this, do not think it was laziness for me not to rely purely on the reference manual.

When I'm learning anything I consult various sources and that includes people who may have some knowledge, a forum like this seems a good place to ask such questions.

I actually did spend several hours researching this but got bogged down in many technical details, nothing I read seemed to confirm 100% that we can use a timer to directly drive an ouput pin.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
In a nutshell, yes it can be done.
Can it be done without software? No. You need to at least initialize the appropriate peripheral module before it will run.
And as others have said, you do need to read the manual. And you don't need to read all 1749 pages of the User Manual, just the chapters that apply. Which Chapters? Well you need to glean over the information supplied by ST. I cannot think of another way.

If you need help getting started I am here to lend a hand. But you do have to apply some effort of your own. I learned all that I know mostly on my own.
Thanks for this information, if you - or anyone - know of any articles that explain this with examples that'll be appreciated, I did search but my familiarity with these devices is still pretty feeble!
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
Just FYI my motive for even asking this is to answer a simple question that came up as I was exploring the STM32F407G-DISC1 board.

Namely - what's the highest frequency square was I can generate from the board - this is not exciting I know but these kinds of challenges are something I do a lot when exploring and learning new technologies.

Crude experiments (informal stuff, just fiddling really) have gotten me generating a 24 KHz square wave from GPIO PD15 on that board.

This uses a timer setup with a simple interrupt handler that just toggles the output pin.

I was therefore curious about how a professional design might generate higher frequencies, especially as the CPU clock is running at many MHz.

This is where my question came from.

The source code incidentally can be seen here.
 

MrChips

Joined Oct 2, 2009
30,706
You do not need interrupts to toggle a GPIO pin.
Without actually looking it up I will guess that the timer can toggle at rates in excess of 50MHz, taken into account that the timer itself is being clocked at 168MHz.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
You do not need interrupts to toggle a GPIO pin.
Without actually looking it up I will guess that the timer can toggle at rates in excess of 50MHz, taken into account that the timer itself is being clocked at 168MHz.
OK this is all I needed, now I can dig down and get at the details!

Thanks
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
A big part of the challenge here is the fact that we have HAL routines (which many of the VisualGDB examples use) and the lower level direct register manipulation. These HAL routines conveniently wrap these hardware specific register details yet also mask what's going on.

Calling the HAL toggle in a crude loop yields 169 KHz, and cloning the toggle code into the loop (eliminating the function call overhead and assert checking) yields: 346 KHz.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,533
Reducing my crude loop to this yields almost 1 MHz (of course I'm not using a timer at this stage, just playing around).

snip1.gif.jpg

Optimizing the code sees this ramp to around 3.18 MHz, close to what this article sees.

(I'm absolutely loving this ability to snapshot a trace, never had this before):
 

MrChips

Joined Oct 2, 2009
30,706
You don't use SW loops for timing functions. You let the timer do it for you.
As I said, I use timers to generate VGA VSYNC, HSYNC, and VIDEO straight from hardware without SW intervention.
I can digitize 50MHz ADC continuously straight into memory without SW and interrupts.
You can't do this on Arduino.
 
Top