In any case, embedded applications need to schedule future events. Scheduling future activities is accomplished through timers using timer services.
Timers are an integral part of many real-time embedded systems. A
A complex embedded system is comprised of many different software modules and components, each requiring timers of varying timeout values. Most embedded systems use two different forms of timers to drive time-sensitive activities: hard timers and soft timers. Hard timers are derived from physical timer chips that directly interrupt the processor when they expire. Operations with demanding requirements for precision or latency need the predictable performance of a hard timer. Soft timers are software events that are scheduled through a software facility.
A soft-timer facility allows for efficiently scheduling of non-high-precision software events. A practical design for the soft-timer handling facility should have the following properties:
· efficient timer maintenance, i.e., counting down a timer,
· efficient timer installation, i.e., starting a timer, and
· efficient timer removal, i.e., stopping a timer.
While an application might require several high-precision timers with resolutions on the order of microseconds or even nanoseconds, not all of the time requirements have to be high precision. Even demanding applications also have some timing functions for which resolutions on the order of milliseconds, or even of hundreds of milliseconds, are sufficient. Aspects of applications requiring timeouts with course granularity (for example, with tolerance for bounded inaccuracy) should use soft timers. Examples include the Transmission Control Protocol module, the Real-time Transport Protocol module, and the Address Resolution Protocol module.
Another reason for using soft timers is to reduce system-interrupt overhead. The physical timer chip rate is usually set so that the interval between consecutive timer interrupts is within tens of milliseconds or even within tens of microseconds. The interrupt latency and overhead can be substantial and can grow with the increasing number of outstanding timers. This issue particularly occurs when each timer is implemented by being directly interfaced with the physical timer hardware.
This chapter focuses on:
· real-time clocks versus system clocks,
· programmable interval timers,
· timer interrupt service routines,
· timer-related operations,
· soft timers, and
· implementing soft-timer handling facilities.
11.2 Real-Time Clocks and System Clocks
In some references, the term
Real-time clocks exist in many embedded systems and track time, date, month, and year. Commonly, they are integrated with battery-powered DRAM as shown in Figure 11.1. This integrated real-time clock becomes independent of the CPU and the programmable interval timer, making the maintenance of real time between system power cycles possible.
Figure 11.1: A real-time clock.
The job of the system clock is identical to that of the real-time clock: to track either real-time or elapsed time following system power up (depending on implementation). The initial value of the system clock is typically retrieved from the real-time clock at power up or is set by the user. The programmable interval timer drives the system clock, i.e. the system clock increments in value per timer interrupt. Therefore, an important function performed at the timer interrupt is maintaining the system clock, as shown in Figure 11.2.
Figure 11.2: System clock initialization.
11.3 Programmable Interval Timers
The
The functionality of the PIT is commonly incorporated into the embedded processor, where it is called an
A related value is the
Customized embedded systems come with schematics detailing the interconnection of the system components. From these schematics, a developer can determine which external components are dependent on the timer chip as the input clock source. For example, if a timer chip output pin interconnects with the control input pin of the DMA chip, the timer chip controls the DRAM refresh rate.
Timer-chip initialization is performed as part of the system startup. Generally, initialization of the timer chip involves the following steps:
· Resetting and bringing the timer chip into a known hardware state.
· Calculating the proper value to obtain the desired timer interrupt frequency and programming this value into the appropriate timer control register.
· Programming other timer control registers that are related to the earlier interrupt frequency with correct values. This step is dependent on the timer chip and is specified in detail by the timer chip hardware reference manual.
· Programming the timer chip with the proper mode of operation.
· Installing the timer interrupt service routine into the system.
· Enabling the timer interrupt.
The behavior of the timer chip output is programmable through the control registers, the most important of which is the
where