Most application programmers are familiar with APIs that schedule work to occur after a specified delay or at a specified time. But how many of them know what lies beneath these APIs? Although these APIs appear simple, they rely on several layers of software and hardware that work together. This is what we are going to explore.
Hardware level:
A hardware timer is a peripheral implemented in the processor, SoC, or supporting chipset. It provides precise time measurement and event generation capabilities. Different types of hardware timers exist, but they all share one fundamental characteristic: they require a clock signal to operate. A clock signal is a periodic electrical signal that determines when the timer advances from one count value to the next. In order to understand how the clock works and what it does, please read this post .
Once activated, the timer repeatedly increments or decrements its counter. If the counter value is plotted as a function of time, the resulting graph has the shape of a periodic sawtooth waveform.
The software configures the timer's operating parameters and can read the current counter value at any time.
In order to understand Figure 1, a few abbreviations and terms will be defined:
More to come...
Once activated, the timer repeatedly increments or decrements its counter. If the counter value is plotted as a function of time, the resulting graph has the shape of a periodic sawtooth waveform.
The software configures the timer's operating parameters and can read the current counter value at any time.
Similarities to the Clock Graph
- The horizontal axis represents time and contains only positive values.
- The values represented by the vertical axis vary within a limited range.
- Both the clock signal and the timer counter value repeat periodically.
Differences from the Clock Graph
- The vertical axis of the clock graph represents the clock voltage, which can be measured using laboratory equipment such as an oscilloscope.
- The vertical axis of the timer graph represents the counter value. Unlike the clock voltage, this value is read by software from the timer registers rather than measured electrically.
- The clock signal oscillates periodically between low and high states.
- The graph of the timer counter value has the shape of a periodic sawtooth waveform.
Figure 1 below provides a concurrent view of timer and clock operation.
![]() |
| Figure1: Relationship between the clock signal and the timer counter |
In order to understand Figure 1, a few abbreviations and terms will be defined:
- CTP-Clock time period.
- SWTP-Sawtooth wave time period.
- Count-A scalar value that upon each new clock period, either decrements or returns back to its maximum value if the previous one was zero.
- MCV-Maximum value of Count(see definition of previous term).
- Step-An interval between two consecutive Count values.
- The number of steps in one SWTP is equal to the maximum value of Count.
- The time duration of one step is equal to CTP.
Using the timer
In order to make use of it, the software programmer should do the following:- Initialize it.
- Set MCV
- Set CTP, based on the clock of the bus on which the timer device resides.
- Configure it to produce periodic signal
- Start it.
- Whenever needed, read the current Count and use it for time calculations..
So, why is it a high resolution timer?
Since CTP is set by the software programmer during the initialization stage, the step width is determined.
In the previous example, the step width is 0.000001 second (1 micro second).
It means that we can measure long time ranges in units of 1 microsecond.
A few points should be considered, though:
A few points should be considered, though:
- A redundancy error of one step unit may exist(1 microsecond).
- Since the approach we describe here does not deal with timer interrupts, the program should repeatedly read the timer Counter without any interference. This approach is better known as "polling"-meaning that the CPU is 100% busy.
- The CPU is faster than the timer. So, two consecutive reads may return the same Count value.
Once we took everything into account, we can start implementing a Delay function that makes an extensive use of the CPU during the delay interval.
More to come...


No comments:
Post a Comment