540 likes | 724 Views
Real-Time Concepts for Embedded Systems. Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMP Books. Chapter 11 Timer and Timer Services. Outline. 11.1 Introduction 11.2 Real-Time Clocks and System Clocks 11.3 Programmable Interval Timers 11.4 Timer Interrupt Service Routines
E N D
Real-Time Concepts for Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks
Outline • 11.1 Introduction • 11.2 Real-Time Clocks and System Clocks • 11.3 Programmable Interval Timers • 11.4 Timer Interrupt Service Routines • 11.5 A Model for Implementing the Soft-Timer Handling Facility • 11.6 Timing Wheels • 11.7 Soft Timers and Timer Related Operations
11.1 Introduction • System tasks and user tasks often schedule and perform activities after some time has elapsed. For example: • Scheduler • Software-based memory refresh mechanism • Communication protocols schedule activities for data retransmission and protocol recovery • Scheduling future activities is accomplished through timers using timer services
Introduction (Cont.) • Timer • Scheduling of an event according to a predefined time value in the future, similar to setting an alarm clock • Most embedded systems use two different forms of timers to drive time-sensitive activities: • Hard timers and soft timers
Two Different Forms of Timers • Hard timers • Derived from physical timer chips that directly interrupt the processor when they expire • Operations with demanding requirements for precision or latency • Soft timers • Software events that are scheduled through a software facility • Allows for efficiently scheduling of non-high-precision software events
Soft Timers • 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 • Efficient timer removal, i.e., stopping a timer • Reasons for using soft timers • Applications requiring timeouts with course granularity • TCP module, RTP (Real-Time Transport) Protocol module, ARP module • To reduce system-interrupt overhead
Clocks Used in an Embedded System • Real-time clock (RTC) • Track time, date, month, and year • System clock • Track either real-time or elapsed time following system power up • Programmable interval timer(PIT) • Drives the system clock, i.e. the system clock increments in value per timer interrupt
11.2 Real-Time Clocks and System Clocks • Real-Time Clock • Track time, date, month, and year • Integrated with battery-powered DRAM • As shown in the next slide • Thus, RTC is independent of the CPU and the programmable interval timer • Make the maintenance of real time between system power cycles possible
11.2 Real-Time Clocks and System Clocks • System Clock • A software clock • Track either real-time or elapsed time following system power up • The initial value of the system clock is retrieved from the real-time clock at power up • The programmable interval timer then drives the system clock • The system clock increments in value per timer interrupt
11.3 Programmable Interval Timers • Programmable interval timer(PIT), also known as the timer chip • The functionality of the PIT is commonly incorporated into the embedded processor • Thus, often called an On-chip timer • However, dedicated stand-alone timer chips may also available • To reduce processor overhead
Programmable Interval Timers (Cont.) • Timer chips • Feature an input clock source with a fixed frequency, as well as a set of programmable timer control registers • Timer interrupt rate • Number of timer interrupts generated per second • Timer countdown value • Determines when the next timer interrupt occurs • Loaded in timer control registers and decremented by one every input clock cycle
Timer-Chip Initialization • Resetting the timer chip into a known hardware state. • Programming timer interrupt frequency into the appropriate timer control register. • Programming other timer control registers with correct values. • Dependent on the timer chip • Programming the timer chip with the proper mode of operation. • e.g., periodic timer interrupt is used or one-shot timer • Installing the timer interrupt service routine. • Enabling the timer interrupt.
Programmable Interval Timers (Cont.) • Timer interrupt rate • Number of timer interrupt occurrences per second • Set by the timer interrupt-rate register (TINTR) • Each interrupt is called a tick, which represents a unit of time. • e.g., if the timer rate is 100 ticks, each tick represents an elapsed time of 10 milliseconds.
11.4 Timer Interrupt Service Routines • Updating the system clock: both the absolute time and elapsed time are updated • Absolute time: time kept in date, hours, minutes, and seconds • Elapsed time: usually kept in ticks and indicates how long the system has been running since power up • Calling a registered kernel function to notify the passage of a preprogrammed period • Acknowledging the interrupt, reinitializing the necessary timer control register(s), and returning from interrupt
11.5 A Model for Implementing the Soft-Timer Handling Facility • Functions performed by the soft-timer facility, called the timer facility, include: • Allowing applications to start a timer • Allowing applications to stop or cancel a previously installed timer • Internally maintaining the application timers
11.5 A Model for Implementing the Soft-Timer Handling Facility (Cont.) • The soft-timer facility is comprised of two components • One lives within the timer tick ISR • The other lives in the context of a task • Why? • If all of the soft-timer processing is done with the ISR • The timer tick event might be lost since the execution of ISR takes too much time
Soft-Timer Handling Facility • Thus, the timer tick handler must be short and must be conducting the least amount of work possible. • Processing of expired soft timer is delayed into a dedicated processing task • Because applications using soft timers can tolerate a bounded timer inaccuracy.
Soft-Timer Handling Facility • A workable model for implementing a soft-timer handling facility: • Processing of expired soft timers is delayed into a dedicated processing task (called work task in the text book) • In conjunction with the system timer ISR
Example • An application requires three soft timers • Timeout values: 200 ms, 300 ms, 500ms • The least common denominator is 100 ms • Hardware timer tick: 10ms • 100ms: countdown value of 10 • Thus, the ISR decrements the countdown value by one during each invocation • If reach to zero, the ISR wake up the worker task • Reinitialize the countdown value back to 10
Example (Cont.) • Worker task must maintain an application-level, timer-countdown table based on 100ms granularity • Three countdown values: 2, 3, and 5 • An application-installed, timer-expiration function is associated with each other
A model for Soft-Timer Handling Facility (Cont.) • A single ISR-level timer drives three application timers at the task-level • Decrease in the number of ISR timers installed • Improves overall system performance • Application-installed timers are called soft timers
11.5.1 Possible Processing Delays • An ISR must perform the smallest amount of work possible. • Typical implementations perform real work • Either inside a worker task that is a dedicated daemon task • Or within the application that originally installed the timer
Possible Processing Delays (Cont.) • First level of delay • The event-driven, task-scheduling delay • Second level of delay • The priority-based, task-scheduling delay • Third level of delay • Introduced when an application installs many soft timers • Introduced later
11.5.2 Implementation Considerations • A soft-timer facility should be efficient in • Timer insertion, timer deletion and cancellation, and timer update • The timer list may be implemented as a double- linked list • Fig. 11.8
11.5.2 Implementation Considerations (Cont.) • If the timer list is not sorted • Maintaining timer ticks can prove costly • Timer installation can be performed in constant time • Timer cancellation and timer update require O(N) in the worst case
11.5.2 Implementation Considerations (Cont.) • Sorting expiration times in ascending order results in efficient timer bookkeeping • Timer installation requires O(log(N)) • Timer cancellation is also O(log(N)) • Timer update require constant time • Only the first entry update is necessary
11.6 Timing Wheels • Timing wheel • A construct with a fixed-size array • Each slot represents a unit of time with respect to the precision of the soft-timer facility • Within each slot, a doubly linked list of timeout event handlers is stored and invoked on timer expiration • Advantage: • Has the advantage of the sorted timer list for updating the timers efficiently • Provides efficient operations for timer installation and cancellation
Installing a Timeout Event • When installing a new timer event • The current location of the clock dial is used as the reference point to determine the time slot in which the new event handler will be stored • Example • When the developer want to schedule a 200 ms timeout in the feature • The time slot marked +200 is the time slot in which to store an event handler
11.6.1 Issues • First issue: the number of slots in the timing wheel has a limit • Approaches to deal with timing wheel overflow: • Deny installation of timers outside the fixed range • Use event overflow buffer
Issues Associated with the Timing Wheel Approach (Cont.) • Second issue: the precision of the installed timeouts • For example, a 150 ms timer event is being scheduled while the clock is ticking but before the tick announcement reaches the timing while • Should the timer event be added to the +150ms slot or placed in the +200ms slot? • On average, the error is approximately half the size of the tick
Issues Associated with the Timing Wheel Approach (Cont.) • Third issue: relates to the invocation time of the callbacks installed at each time slot • Many handler may be lined in the same time slot • The length of execution of each handler is unknown • No guarantee or predictable measures exist concerning when a callback in a later position of the list can be called • Introduces non-determinism into the system and is undesirable
11.6.2 Hierarchical Timing Wheels • Using the hierarchical timing wheel approach can solve the timer overflow problem • Multiple timing wheels are organized in a hierarchical order. • Each timing wheel in the hierarchy set has a different granularity
11.7 Soft Timers and Timer Related Operations • Can be cataloged into three groups: • Group 1-provides low-level hardware related operations • Developed and provided by the BSP developers • Group 2-provides soft-timer-related services • Used by both the system modules and applications • Group 3-provides access either to the storage of the real-time clock or to the system clock • Used by user-level applications