Tuesday, April 21, 2020

High Resolution Timers

Application programmers are familiar with functions that allow them to schedule tasks in a timely manner. But how many of them know what is hidden below that? What kind of interaction is taking place among the components starting at the application level, via the kernel - ending up with the Hardware components itself. This is what we are going to reveal here.


Hardware level:

The timer is a hardware device on your board. There are a few types of timers, which can be found on it. But there is one common thing about them - they all need a periodic pulse signal to operate. This periodic pulse signal is referred to as a clock.  In order to understand how the clock works and what it does, please read this post .
Once activated - the timer generates a sequence of periodic sawtooth waves.
It allows the software programmer to control the waveform and to read its current state.

The operation of the timer can be described by a rectangular coordinate system.

Similarities with the clock presentation

  • The horizontal axis represents the time and contains positive values only .
  • The values represented by the vertical axis vary within a limited range.
  • Both signals are periodic.

The difference from clock presentation

  • The vertical axis of the clock represents an analog value (Voltage), that can be measured with Lab equipment. 
  • The vertical axis of the timer represents a scalar value (Count), which can not be measured. This is a value that is read by the software programmer.
  • The clock signal oscillates periodically between low and high states.
  • The timer signal has the form of periodic Sawtooth wave.  


Figure 1 below provides a concurrent view of timer and clock operation.


Figure1: Graphic presentation of the timer

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.
Based on that, calculating SWTP is straight forward: SWTP=MCV*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 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