Download presentation
Presentation is loading. Please wait.
1
Real-Time Concepts for Embedded Systems
Author: Qing Li with Caroline Yao ISBN: CMPBooks
2
Chapter 11 Timer and Timer Services
3
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
4
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
5
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
6
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
7
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
8
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
9
11.2 Real-Time Clocks and System Clocks
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
10
A Real-Time Clock
11
11.2 Real-Time Clocks and System Clocks
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
12
System Clock Initialization
13
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
14
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
15
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.
16
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.
17
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
18
Steps in Servicing the Timer Interrupt
19
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
20
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
21
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.
22
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
23
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
24
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
25
A model for Soft-Timer Handling Facility
26
Servicing the timer interrupt in the task context
27
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
28
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
29
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
30
Level 1 Delays- Timer Event Notification Delay
31
Level 2 Delays-Priority-Based, Task-Scheduling Delays
32
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
33
Fig. 11.8 Maintaining Soft Timers
34
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
35
Unsorted Soft Timers
36
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
37
Sorted Soft Timers
38
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
39
Timing Wheel
40
Timeout Event Handlers
41
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
42
Installing a Timeout Event
43
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
44
Timing Wheel Overflow Event Buffer
45
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
46
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
47
Unbounded Soft-Timer Handler Invocation
48
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
49
A Hierarchical Timing Wheel
50
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
51
Group 1 Operations sys_timer_enable sys_timer_disable
Enables the system timer chip interrupts sys_timer_disable Disables the system timer chip interrupts sys_timer_connect Installs the system timer ISR into the system exception vector table
52
Group 1 Operations (Cont.)
sys_timer_setrate Sets the system clock rate as the number of ticks per second the timer chip generates Internally, this operation reprograms the PIT to obtain the desired frequency sys_timer_getticks Returns the elapsed timer ticks since system power up
53
Group 2 Operations timer_create timer_delete timer_start timer_cancel
Creates a soft timer by allocating a soft-timer structure timer_delete Deletes a timer timer_start Starts a timer by installing a previously created soft timer into the timer-handling facility timer_cancel Cancels a timer by removing the currently running timer from the timer-handling facility
54
Group 3 Operations clock_get_time clock_set_time
Gets the current clock time from the system clock or the real-time clock clock_set_time Sets the system clock or real-time clock to a specified time
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.