Introduction to STM32 Timers
Keywords: Embedded systems, ARM, STM32F4, Timers, Counter
In the world of embedded systems the most important parameter is time. It’s the time that qualifies a systems from simple blinky led to complex inter-systems communication. Its time which defines how fast/slow a job can be done on target processor. Obviously what’s the purpose of time if you can’t count it? This where timers comes into action. Timers provide the facility to measure time relatively from one reference point to another. This relative time measure enables us to generate an event or a specific time interval (like signal) or conversely measure an event time. This makes timers one of the crucial part of Embedded Systems.
The basic idea behind timers is that its logic is provided with some clock and upon each clock it increments/ decrements a counter register value. At the end the counted number of cycles is multiplied with input clock Time period to get the total time elapsed.
Total Time: Counter Value * 1/Frequency

A term that usually confuses newbies is the difference between a Timer and a Counter. As a matter of fact there is no such thing as Timers…!!! We only have counters in embedded systems. The source of confusion is that both these terms are NOT so often used INTERFACHANGABLY. The basic functions of both is to count time based events. A minor difference that is usually considered to differentiate timers and counters is frequency source. While in case of timers the input events (clock pulses) have same frequency i.e. clock to timers is fixed; and when the timer is feed from some external events which may or may not of fixed time intervals, the timer is known be as Counter (Counting events). But the basic idea behind both of them is same – They just count events. That’s it.
STM32 Timers:
Based on features/functionalities STM32 Microcontrollers Timers are categorized in 3 main types.
- Basic Timers
- General Purpose Timers
- Advanced Timers
Almost all STM32Fxxx devices come with General purpose timers and Advanced timers with optional availability of Basic Timers which is covered by ARM Systick timer itself.
Basic Timers:
As the name suggests, its a timer with basic functionality of counting and works independently of any I/O interaction. Its just connected to some on-chip frequency source and simply counts time. This type of timer can’t capture external events or produce output signals like PWM etc.
General-Purpose Timers:
This type of timers are available on almost every STM32Fxxx device. These timers are like the Basic timers with one exception that they can also perform I/O operation/interaction like generating PWM on a pin or capturing an event from external source for comparison purpose.
Advanced timers:
These type of timers inherent all the features of General-Purpose timers with add-ons of few advance features that generate SPECIAL signals for some time critical devices like Power Electronics, Motor Control etc.
There is another special purpose timer usually available on embedded SOCs known as watchdog timer. This is a special purpose timer which prevents processor from getting stuck at a single point in software for more than a specified allowed time. The watchdog timer is kicked (reset) in various points in software flow. If software gets stuck in some infinite loop and watchdog timer gets fired (counted to specified value), the watchdog will reset the processor similar to Power ON Reset.

Example: The STM32F405xx and STM32F407xx devices include two advanced-control timers, eight general-purpose timers, two basic timers and two watchdog timers.
The following table summarize their features.

STM32 Timer Internals:
Let’s discuss briefly what’s going on under the surface. In this section we will try to dig down a little more into working anatomy of STM32 Timers. I will try to keep things as simple as possible, for more in-depth information, refer to respective SoC datasheet.
Shadow and Preload Register:
In STM32 Timers, certain registers have their duplicate/alias registers. The actual registers are called Preload registers and the duplicate/alias ones are called shadow registers. The shadow registers are not accessible/visible to software while the actual registers can be programmed by software. The point of duplication is to copy and lock the value from actual register to shadow register and then comparison is made with the shadow registers to prevent undesirable results. In other words the Preload registers are used by Software and Shadow registers are used by Hardware.
Following are the registers in STM32 microcontrollers that have their duplicate registers.
- Pre-scaler Register (TIMx_PSC)
- Auto-reload Register (TIMx_ARR)
- Repetition Counter Register (TIMx_RCR)
STM32 Timer Events:
Update Event (UEV): This event causes the shadow registers values to be updated from preload registers (ARR, PSC, CCRx) for possible new values. This event is fired whenever the Counter register overflows/underflows or when the UG bit is set manually by software in TIMx_EGR register.
STM32 Timers implementations consists of the following main blocks.
- The clock-subsystem
- Time-base Unit (The Timer Kernel)
- Input Stage and Signal Conditioning
- Output Stage
1. Time-Base Unit (The Timer Kernel):
This is the hearth of STM32 Timers. As a matter of fact this is the actual Timer. It is the base logic where all the counting occurs. The Time-Base Unit basically consists of the following main registers.
- Auto-Reload Register: This contains the actual value with which the Timer counted value is compared. There are actually two auto-reload registers. The one that is accessible to software and the one that is shadow of this register. The shadow register is not accessible to software and the value from actual auto-reload register is shifted to shadow register for comparison.
- Prescaler Register: This register is used to cut-down/divide the input clock frequency to timer by a constant factor. For example if 100Mhz is the input clock to Timers logic before prescaler and the prescaler register is loaded with 1 (actual divider: prescaler register value + 1) then the frequency that will appear at timer input will be 50Mhz.
- Counter Register: This is the actual register that contains the value counted by the timer at any given time.
- Repetition Register: This register is used to configure STM32 timers not to generate interrupt/event every time the counter register counts to auto reload shadow-register. The event is generated only when a number (REP register value) of count cycles are completed.
- Capture/compare Registers: As the name suggests these register captures input from input channels for comparison and/or produce output after comparison on output channels.

2. The clock-subsystem:
The clock subsystem also known as clock and trigger controller is main clock providing unit that multiplex multiple selectable clock sources on single Timer input line. Following are the main clock sources that feed clock and trigger controller.
- Internal Clock: This is the clock source that is feed from RCC STM32 Unit. This clock source comes from main system clock of STM32 MCU Figure-4(1).
- ITRx: These are input lines that can be connected to the output of other on-chip timers in master/slave architecture. In this configuration, one timer, called Master, provides clock and other control signals while the receiving timer, called Slave, actually perform the counting operation Figure-4(2).
- TIMx_ETR: This line connects timers with external PIN to count external events like counting external pulses Figure-4(3).
- TIxFPx: These lines also connect timer to external clock source like TIMx_ETR with the only exception that the input clock from the external pins are first passed through some intermediate logic (discussed later) for edge detection/digital filtration to remove spurious events Figure-4(4).

3. Input Stage and Signal Conditioning:
The input stage and signal conditioning provide the facility intermediate process like digital filtering etc. before the signal is feed to timer.

4. Output Compare:
The output stage is used to generate some timing signals like PWM etc. for off-chip peripherals.

The whole STM32 timers units are interconnected as follow:

References:
[1] – Reference manual-RM0090
Outstanding story there. What occurred after? Good luck!
0mniartist asmr
Do you mind if I quote a few of your posts as long as I provide credit and sources back
to your website? My blog is in the exact same area of interest as yours and my visitors would genuinely benefit from
a lot of the information you provide here. Please let me know if this okay with you.
Regards! asmr 0mniartist
This is my first time pay a visit at here and i am in fact impressed to read all at single
place. 0mniartist asmr
Thank you for any other great post. The place else could anyone get that kind of information in such an ideal method of writing?
I have a presentation subsequent week, and I’m at the look for such info.
asmr 0mniartist
I visited several sites but the audio feature for audio songs present at this web site is really superb.
0mniartist asmr
Hey There. I found your blog using msn. This is an extremely well written article.
I’ll make sure to bookmark it and return to read more of your useful info.
Thanks for the post. I will definitely return.
Very energetic article, I liked that bit. Will
there be a part 2?
I’m not certain the place you are getting your information, but great topic.
I needs to spend a while finding out much more or figuring out more.
Thank you for excellent information I was searching for this
information for my mission.
Hello, after reading this amazing post i am as well glad to share my know-how here with
mates.
Hi everyone, it’s my first pay a quick visit at this web page,
and post is genuinely fruitful designed for
me, keep up posting these articles or reviews.
Hello just wanted to give you a quick heads up and let you know a few of the pictures aren’t
loading properly. I’m not sure why but I think its a
linking issue. I’ve tried it in two different internet browsers and both show the same results.
Excellent blog! Do you have any tips and hints for aspiring writers?
I’m hoping to start my own blog soon but I’m a little lost on everything.
Would you advise starting with a free platform
like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed ..
Any ideas? Many thanks!
Woah! I’m really loving the template/theme of this
blog. It’s simple, yet effective. A lot of
times it’s tough to get that “perfect balance” between usability and
visual appearance. I must say you’ve done a great job with
this. Also, the blog loads extremely fast for me on Chrome.
Outstanding Blog!
It’s not my first time to visit this web page, i am visiting this web site dailly
and take good facts from here every day.
scoliosis
Great information. Lucky me I discovered your site by chance (stumbleupon).
I have saved as a favorite for later! scoliosis
scoliosis
Greetings from Carolina! I’m bored to death at work so I decided to
browse your site on my iphone during lunch break.
I love the info you present here and can’t wait
to take a look when I get home. I’m shocked at how fast your blog loaded on my mobile ..
I’m not even using WIFI, just 3G .. Anyways, excellent site!
scoliosis
scoliosis
I am sure this piece of writing has touched all the internet users, its really really good post on building up
new web site. scoliosis
dating sites
Informative article, just what I was looking for. dating sites
https://785days.tumblr.com/
dating sites
I take pleasure in, lead to I found exactly what I was taking a look for.
You have ended my 4 day lengthy hunt! God Bless you man. Have a great day.
Bye free dating sites
Thank you, I have recently been searching for info approximately this subject for a
while and yours is the greatest I have came upon till now.
However, what in regards to the conclusion? Are you certain concerning the
source?
Thanks for every other informative blog. Where else could I am getting that type of information written in such an ideal manner?
I have a mission that I’m just now running on, and I’ve been on the glance out for such info.
Have you ever thought about publishing an ebook or guest authoring on other blogs?
I have a blog based upon on the same topics you discuss and would
really like to have you share some stories/information. I know my subscribers would appreciate your
work. If you are even remotely interested, feel free to send
me an e mail.
Hi, I do think this is a great website. I stumbledupon it
😉 I’m going to revisit once again since I book-marked it.
Money and freedom is the greatest way to change, may you be rich and continue to
guide others.
Thank you for another excellent article. The place else may just anyone get that kind of information in such an ideal method of writing?
I have a presentation subsequent week, and I am at the look for such info.
I am no longer certain the place you’re getting your information, however great topic.
I needs to spend some time studying more or understanding more.
Thanks for great information I was on the lookout for this information for my mission.
Interesting blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would really make
my blog shine. Please let me know where you got your design. Kudos
Thanks on your marvelous posting! I actually enjoyed reading it, you could
be a great author.I will always bookmark your blog and may come back
in the future. I want to encourage you to definitely continue your great writing,
have a nice evening!
Good day! I just want to give you a big thumbs up for
the great information you have right here on this post.
I’ll be coming back to your website for more soon.
Appreciate this post. Will try it out.
Wow, awesome blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your website is great, let
alone the content!
Its not my first time to visit this web page, i am
browsing this site dailly and get nice data from here every day.
I’m really loving the theme/design of your blog. Do you
ever run into any browser compatibility issues?
A few of my blog visitors have complained about
my site not working correctly in Explorer but looks
great in Safari. Do you have any recommendations to help fix this issue?
Pretty! This was an incredibly wonderful article. Many thanks for supplying this information.
There is certainly a great deal to learn about this topic.
I like all of the points you made.
It’s an awesome paragraph for all the web
people; they will get benefit from it I am sure.
I visited multiple sites however the audio quality for audio songs present at this web site is actually superb.
Nice blog right here! Additionally your website lots up very fast!
What host are you the usage of? Can I am getting your affiliate link
in your host? I wish my website loaded up as
quickly as yours lol
Hello, all the time i used to check webpage posts here in the early hours in the dawn, as i like to learn more and more.
That is really attention-grabbing, You’re an excessively skilled blogger.
I have joined your rss feed and sit up for searching for more of your magnificent post.
Also, I’ve shared your site in my social networks
Greetings! I know this is kind of off topic but I was wondering which blog platform are you using
for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking
at options for another platform. I would be fantastic if you could point me in the direction of a good platform.
Very soon this web site will be famous among all blogging and site-building
viewers, due to it’s fastidious posts
Truly when someone doesn’t understand then its up to other users that they will help,
so here it happens.
Howdy! I could have sworn I’ve been to this blog before but after checking
through some of the post I realized it’s new to me.
Nonetheless, I’m definitely delighted I found it and I’ll be book-marking and checking back
frequently!
My partner and I absolutely love your blog and find the majority of your post’s to be what
precisely I’m looking for. Would you offer guest writers to write content for yourself?
I wouldn’t mind producing a post or elaborating on a lot
of the subjects you write related to here. Again, awesome web site!
Pretty! This was an extremely wonderful post. Thanks for providing this information.
It’s the best time to make some plans for the longer term and it’s time to be happy.
I have learn this submit and if I could I wish to suggest you some fascinating
things or tips. Maybe you could write next articles referring to this article.
I desire to learn more issues about it!
Because the admin of this web page is working, no uncertainty very soon it will be well-known, due
to its feature contents.
Oh my goodness! Incredible article dude! Many thanks, However I am having troubles with your RSS.
I don’t understand why I can’t join it. Is there anyone else getting
identical RSS problems? Anybody who knows the answer
will you kindly respond? Thanks!!
Hi there, I log on to your blogs like every week.
Your humoristic style is awesome, keep doing what you’re doing!
Its like you read my mind! You appear to know a lot
about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but
other than that, this is great blog. An excellent
read. I will definitely be back.
Pretty part of content. I just stumbled upon your weblog and in accession capital to say that I get in fact enjoyed account your weblog posts.
Anyway I will be subscribing on your feeds and even I
success you access persistently fast. asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
If some one wishes to be updated with most up-to-date technologies after that
he must be go to see this site and be up to date
all the time. quest bars http://tinyurl.com/49u8p8w7 quest bars
Hello to all, how is all, I think every one is getting more from this site, and your views are good in favor
of new people. scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
This is a topic which is close to my heart… Best wishes!
Where are your contact details though? cheap flights http://1704milesapart.tumblr.com/ cheap flights
Hello excellent blog! Does running a blog such as this require a lot of work?
I have virtually no expertise in coding but I was
hoping to start my own blog soon. Anyways, if you have any recommendations or techniques for new blog owners please share.
I understand this is off topic but I just wanted to ask.
Appreciate it! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
Thanks in support of sharing such a fastidious thinking,
piece of writing is nice, thats why i have read it completely quest bars https://www.iherb.com/search?kw=quest%20bars quest bars
I’m really enjoying the design and layout of your site.
It’s a very easy on the eyes which makes it
much more pleasant for me to come here and visit more often. Did you hire out
a designer to create your theme? Outstanding work! ps4 games https://j.mp/3z5HwTp ps4 games
Howdy just wanted to give you a brief heads up and let you know a few of the images aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same outcome.
this content
argument essay outline personal essay
I love reading through a post that can make people think.
Also, many thanks for allowing me to comment!
https://parttimejobshiredin30minutes.wildapricot.org/ part time jobs hired in 30 minutes
It’s a shame you don’t have a donate button! I’d without a
doubt donate to this excellent blog! I suppose
for now i’ll settle for bookmarking and adding your RSS feed
to my Google account. I look forward to fresh updates and will talk about this blog
with my Facebook group. Talk soon!
Thanks for every other informative web site. The place else could I am getting
that kind of info written in such an ideal means?
I’ve a undertaking that I’m simply now working on, and I’ve been at the
look out for such information.
Woah! I’m really digging the template/theme of this
site. It’s simple, yet effective. A lot of times it’s hard to get that “perfect balance” between user friendliness and visual appearance.
I must say you have done a very good job with this. Also, the
blog loads very quick for me on Firefox. Excellent Blog!
Hello! Ofc good article!
Wow, great blog post.Really thank you! Really Cool.
I value the blog. Fantastic.
I just could not go away your web site prior to suggesting that I really loved
the standard information an individual supply in your guests?
Is gonna be again continuously in order to investigate cross-check new posts
Hello! This post could not be written any better! Reading this post reminds me of my old room mate!
He always kept talking about this. I will forward this article to him.
Pretty sure he will have a good read. Thanks for sharing!
You are so awesome! I do not believe I have read through anything like
that before. So great to discover someone with a
few unique thoughts on this subject. Really.. thank you for starting this up.
This website is something that’s needed on the web,
someone with a bit of originality!
With havin so much content do you ever run into any
problems of plagorism or copyright violation? My site has a
lot of exclusive content I’ve either written myself or outsourced
but it appears a lot of it is popping it up all over the web without
my authorization. Do you know any techniques to help stop content from being stolen? I’d definitely appreciate it.
Thanks for a marvelous posting! I definitely enjoyed reading it, you can be a great author.
I will be sure to bookmark your blog and may come back in the future.
I want to encourage that you continue your great posts, have a nice holiday
weekend!