54
Real-Time Concepts for Real-Time Concepts for Embedded Systems Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks

Real-Time Concepts for Embedded Systems

  • Upload
    ormand

  • View
    60

  • Download
    8

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Real-Time Concepts for Embedded Systems

Real-Time Concepts for Real-Time Concepts for Embedded SystemsEmbedded Systems

Author: Qing Li with Caroline Yao

ISBN: 1-57820-124-1CMPBooks

Page 2: Real-Time Concepts for Embedded Systems

Chapter 11Chapter 11 Timer and Timer Services Timer and Timer Services

Page 3: Real-Time Concepts for Embedded Systems

OutlineOutline 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

Page 4: Real-Time Concepts for Embedded Systems

11.1 Introduction11.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

Page 5: Real-Time Concepts for Embedded Systems

Introduction (Cont.)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

Page 6: Real-Time Concepts for Embedded Systems

Two Different Forms of TimersTwo 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

Page 7: Real-Time Concepts for Embedded Systems

Soft TimersSoft 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

Page 8: Real-Time Concepts for Embedded Systems

Clocks Used in an Embedded System 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

Page 9: Real-Time Concepts for Embedded Systems

11.2 11.2 Real-Time Clocks and System Real-Time Clocks and System ClocksClocks 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

Page 10: Real-Time Concepts for Embedded Systems

A Real-Time Clock A Real-Time Clock

Page 11: Real-Time Concepts for Embedded Systems

11.2 11.2 Real-Time Clocks and System Real-Time Clocks and System ClocksClocks 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

Page 12: Real-Time Concepts for Embedded Systems

System Clock Initialization System Clock Initialization

Page 13: Real-Time Concepts for Embedded Systems

11.3 11.3 Programmable Interval Timers 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

Page 14: Real-Time Concepts for Embedded Systems

Programmable Interval Timers Programmable Interval Timers (Cont.)(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

Page 15: Real-Time Concepts for Embedded Systems

Timer-Chip Initialization 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.

Page 16: Real-Time Concepts for Embedded Systems

Programmable Interval Timers Programmable Interval Timers (Cont.)(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.

Page 17: Real-Time Concepts for Embedded Systems

11.4 11.4 Timer Interrupt Service Timer Interrupt Service Routines 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

Page 18: Real-Time Concepts for Embedded Systems

Steps in Servicing the Timer Steps in Servicing the Timer Interrupt Interrupt

Page 19: Real-Time Concepts for Embedded Systems

11.5 11.5 A Model for Implementing the A Model for Implementing the Soft-Timer Handling Facility 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

Page 20: Real-Time Concepts for Embedded Systems

11.5 11.5 A Model for Implementing the A Model for Implementing the Soft-Timer Handling Facility (Cont.)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

Page 21: Real-Time Concepts for Embedded Systems

Soft-Timer Handling FacilitySoft-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.

Page 22: Real-Time Concepts for Embedded Systems

Soft-Timer Handling FacilitySoft-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

Page 23: Real-Time Concepts for Embedded Systems

ExampleExample 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

Page 24: Real-Time Concepts for Embedded Systems

Example (Cont.)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

Page 25: Real-Time Concepts for Embedded Systems

A model for Soft-Timer Handling A model for Soft-Timer Handling Facility Facility

Page 26: Real-Time Concepts for Embedded Systems

Servicing the timer interrupt in the Servicing the timer interrupt in the task contexttask context

Page 27: Real-Time Concepts for Embedded Systems

A model for Soft-Timer Handling A model for Soft-Timer Handling Facility (Cont.)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

Page 28: Real-Time Concepts for Embedded Systems

11.5.1 11.5.1 Possible Processing Delays 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

Page 29: Real-Time Concepts for Embedded Systems

Possible Processing Delays Possible Processing Delays (Cont.)(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

Page 30: Real-Time Concepts for Embedded Systems

Level 1 Delays- Timer Event Level 1 Delays- Timer Event Notification Delay Notification Delay

Page 31: Real-Time Concepts for Embedded Systems

Level 2 Delays-Priority-Based, Task-Level 2 Delays-Priority-Based, Task-Scheduling Delays Scheduling Delays

Page 32: Real-Time Concepts for Embedded Systems

11.5.2 11.5.2 Implementation Implementation ConsiderationsConsiderations 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

Page 33: Real-Time Concepts for Embedded Systems

Fig. 11.8 Maintaining Soft TimersFig. 11.8 Maintaining Soft Timers

Page 34: Real-Time Concepts for Embedded Systems

11.5.2 11.5.2 Implementation Implementation Considerations (Cont.)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

Page 35: Real-Time Concepts for Embedded Systems

Unsorted Soft Timers Unsorted Soft Timers

Page 36: Real-Time Concepts for Embedded Systems

11.5.2 11.5.2 Implementation Implementation Considerations (Cont.)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

Page 37: Real-Time Concepts for Embedded Systems

Sorted Soft Timers Sorted Soft Timers

Page 38: Real-Time Concepts for Embedded Systems

11.6 11.6 Timing WheelsTiming 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

Page 39: Real-Time Concepts for Embedded Systems

Timing WheelTiming Wheel

Page 40: Real-Time Concepts for Embedded Systems

Timeout Event Handlers Timeout Event Handlers

Page 41: Real-Time Concepts for Embedded Systems

Installing a Timeout EventInstalling 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

Page 42: Real-Time Concepts for Embedded Systems

Installing a Timeout Event Installing a Timeout Event

Page 43: Real-Time Concepts for Embedded Systems

11.6.1 11.6.1 Issues 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

Page 44: Real-Time Concepts for Embedded Systems

Timing Wheel Overflow Event Timing Wheel Overflow Event BufferBuffer

Page 45: Real-Time Concepts for Embedded Systems

Issues Associated with the Timing Issues Associated with the Timing Wheel Approach (Cont.)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

Page 46: Real-Time Concepts for Embedded Systems

Issues Associated with the Timing Issues Associated with the Timing Wheel Approach (Cont.)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

Page 47: Real-Time Concepts for Embedded Systems

Unbounded Soft-Timer Handler Unbounded Soft-Timer Handler InvocationInvocation

Page 48: Real-Time Concepts for Embedded Systems

11.6.2 11.6.2 Hierarchical Timing WheelsHierarchical 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

Page 49: Real-Time Concepts for Embedded Systems

A Hierarchical Timing Wheel A Hierarchical Timing Wheel

Page 50: Real-Time Concepts for Embedded Systems

11.7 11.7 Soft Timers and Timer Related Soft Timers and Timer Related Operations 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

Page 51: Real-Time Concepts for Embedded Systems

Group 1 Operations Group 1 Operations sys_timer_enable

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

Page 52: Real-Time Concepts for Embedded Systems

Group 1 Operations (Cont.)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

Page 53: Real-Time Concepts for Embedded Systems

Group 2 Operations Group 2 Operations timer_create

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

Page 54: Real-Time Concepts for Embedded Systems

Group 3 Operations Group 3 Operations clock_get_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