Timers: Architecture 

Hardware timers are peripherals on the chip that can be used to measure time. The MSP432 uses a 32-bit timer, aptly named Timer32. There are two of these hardware timers on each MSP432 chip. Each Timer32 consists of two core components: a counter and a clock. The counter’s primary purpose is to store a load value or set-count that determines how much time the timer counts before it expires. Subsequently, the clock signal drives this counter, decreasing its value after every clock cycle. In other words, when one period of the clock signal passes, the timer’s counter is decremented by one. This clock signal is typically fed by the system clock, which operates in units of Hertz. Because of this architecture, the hardware timers can operate independently of the CPU. So even if the CPU is busy doing other tasks, the timer keeps time properly as long as its clock is ticking.

When the timer’s counter reaches zero, we say it has expired. Upon expiration, one of two things can happen, depending on the Timer32 mode: one-shot mode or periodic mode. In periodic mode, the timer reloads its counter with the load value/set-count, thus restarting the count-down process. Keep in mind that the counter cannot be both zero and the load value at the same time; thus, once the counter hits zero, it takes the counter one more clock cycle to be reloaded with the set-count value. In one-shot mode, the timer will remain zero until it is manually reloaded with a new value. Both of these modes are very useful, depending on the application. 

The speed of the clock that ticks the counter decides the granularity we can measure time using the timer and how fast the timer expires. If the clock is fast, it means the clock period or the length of each cycle is short. Therefore, the time steps or the time increments are short, which eventually means we have finer granularity for measuring time. However, if we keep the load value constant, a faster clock results in a timer that expires quicker and therefore, can measure short time intervals. Due to this tradeoff, there are times that we wish the clock to the counter to be fast, and there are times we want it to be slow. For measuring short time intervals with high accuracy, we prefer a fast clock, and for measuring long time intervals with lower precision, we prefer a slow clock. A module called prescaler or clock divider helps reduce the speed of fast clocks in a timer so that the designers can choose a clock speed of their choice for their timers. 

The below figure shows the architecture of a generic simple hardware timer. The clock the feeds the timer is called the input clock and its frequency and clock period are f_in and T_in respectively. The perscaler divides the f_in by P and creates the counter clock, whose frequency and clock period are denoted by f_counter and T_counter. Based on these symbols, we can write the following formulas:

f_counter = f_in / P

T_counter = T_in * P

If we call the load value or set-count N and the time interval the timer measures between loading and expiration, t_expired, we have:

t_expired = N * T_counter