Timers: Software Timers

Using one-shot timers is a very effective approach to timing events. However, we only have two hardware timers to work with. If each of them are configured as one-shots, then we can only time two separate events simultaneously. What if we had three or more events that we wanted to time? We would need another hardware timer configured as a one-shot, but it would be rather inefficient and difficult to try and implement another hardware timer into the SoC. We will need a different approach instead.


Suppose you had a stopwatch that has been running for a while. When you first look at it, the elapsed time is 1 minute and 42 seconds. Some time passes, and then you check again. Now the stopwatch shows 1 minutes and 57 seconds. How much time has passed? If we do the math, we can see that 15 seconds had elapsed since the first time we checked the stopwatch. This is a simple calculation of T2 - T1 (the later time minus the earlier time). The minutes are the same, so we can just compare the seconds. 57 - 42 = 15.


What if T2 was 2 minutes and 9 seconds instead? Doing T2 - T1 may not be as straightforward here. If we subtracted the seconds, we would end up with a negative number. Instead, let’s think about how we can break this time interval into two parts. We started at 1 minute, 42 seconds (1:42) and ended at 2 minutes, 9 seconds (2:09). Since the calculation when the minutes were identical was so convenient, why don’t we express this time interval as the time it takes to get from the start to the two-minute mark, then from that two-minute mark to the end? That is, we can say that the time between 1:42 and 2:09 is the same as the time between 1:42 and 1:59 plus the time between 2:00 and 2:09 (plus 1). Then the time elapsed is 17 seconds and 9 seconds, respectively. Add 1 to account for the time between 1:59 and 2:00, and we get 17 + 1 + 9 = 27 seconds.


We can extend the previous situation to a 3 minutes, 44 seconds end time example, too. This time, the difference in minutes is greater than two. Since this is the case, we know at least one minute has elapsed from 2:00 to 3:00. From the previous example, we know that the time from 1:42 to 1:59 is 17 seconds, and the time from 1:59 to 2:00 is 1 second. Finally, the time from 3:00 to 3:44 is 44 seconds. Thus, the total elapsed time is 17 + 1 + 60 + 44 = 122 seconds, or 2 minutes, 2 seconds.


Since the values on the stopwatch are continuously increasing, we can keep track of how much time has passed as long as we keep account of the minutes and seconds at the two points in time. Similarly, we can apply this to the periodic timer. In this mode, the timer is periodically going from its load value to zero. If we keep track of the counter value at both points in time and how many times the counter has been reloaded, we can figure out how much time has passed. Thus, we will need to keep track of the number of rollovers that have occurred since the start of the periodic timer. Rollover is when the counter is reloaded from zero to the load value. This is similar to when the seconds counter resets to 0 when a minute has passed, and in fact, we can draw an analogy between the stopwatch and the periodic timer: the seconds value is the counter value, and the minutes value is the number of rollovers.