DE5 Thursday, September 23, 2010 Real-time Embedded System Programming 1 Real Real-Time Time Embedded System Programming Embedded System Programming Embedded System Programming Embedded System Programming Course Teacher: D. M. Akbar Hussain Course Teacher: D. M. Akbar Hussain Dr. D. M. Akbar Hussain Department of Electronic Systems 1 DE5 DE5-2010 2010 – Lessen # 3 Lessen # 3 1 1 Real Real Time System Design Time System Design 1. 1. Real Real- -Time System Design Time System Design 2. 2. Real Time Operating System Real Time Operating System 3. 3. Scheduling Scheduling 4. 4. Projects: Projects: Dr. D. M. Akbar Hussain Department of Electronic Systems 2 4. 4. Projects: Projects: • Hardware Hardware • Software Software
36
Embed
Course Teacher: D. M. Akbar Hussain - Aalborg Universitethomes.et.aau.dk/akh/2011/real-time-ep-2011_files/lect2-1-RTES-Clas… · DE5 Thursday, September 23, 2010 Real-time Embedded
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
DE5 Thursday, September 23, 2010
Real-time Embedded System Programming 1
RealReal--Time Time Embedded System ProgrammingEmbedded System ProgrammingEmbedded System ProgrammingEmbedded System Programming
Course Teacher: D. M. Akbar HussainCourse Teacher: D. M. Akbar Hussain
Dr. D. M. Akbar Hussain
Department of Electronic Systems 1
DE5DE5--2010 2010 –– Lessen # 3Lessen # 3
11 RealReal Time System DesignTime System Design1.1. RealReal--Time System DesignTime System Design
2.2. Real Time Operating SystemReal Time Operating System
3.3. SchedulingScheduling
4.4. Projects:Projects:
Dr. D. M. Akbar Hussain
Department of Electronic Systems 2
4.4. Projects:Projects:
•• HardwareHardware•• SoftwareSoftware
DE5 Thursday, September 23, 2010
Real-time Embedded System Programming 2
RealReal--Time System DesignTime System Design
Dr. D. M. Akbar Hussain
Department of Electronic Systems 3
ObjectivesObjectives
•• To explain the concept of aTo explain the concept of a realreal--time systemtime system and why these systems areand why these systems areTo explain the concept of a To explain the concept of a realreal time system time system and why these systems are and why these systems are usually implemented as concurrent usually implemented as concurrent processes.processes.
•• To describe a To describe a design process design process for realfor real--time time systems.systems.
•• To explain the To explain the role of a realrole of a real--time operating time operating systemsystem..
•• To introduce generic process architectures To introduce generic process architectures for:for:
Dr. D. M. Akbar Hussain
Department of Electronic Systems 4
•• MonitoringMonitoring•• Control Control •• Data Data AAcquisition Systems.cquisition Systems.
•• System System DesignDesign•• RealReal--time time Operating SystemsOperating Systems•• Monitoring and Monitoring and Control Control SSystemsystems•• Data Data Acquisition Acquisition SSystemsystems
Stimulus/Response SystemsStimulus/Response Systems
•• Given a stimulus, the system must produce a Given a stimulus, the system must produce a response response within a within a specified timespecified time..
•• Periodic stimuli. Periodic stimuli. Stimuli which occur at Stimuli which occur at predictable predictable time time intervalsintervals–– For example, a temperature sensor may be polled 10 times per secondFor example, a temperature sensor may be polled 10 times per second..
•• AperiodicAperiodic stimuli. stimuli. Stimuli which occur at Stimuli which occur at unpredictable times unpredictable times intervalsintervals–– For example, a system power failure may trigger an For example, a system power failure may trigger an interrupt interrupt which must be processed by which must be processed by
•• Sensor control processesSensor control processes–– Collect information from sensors. May buffer information collected in response Collect information from sensors. May buffer information collected in response
to a sensor stimulusto a sensor stimulus..
•• Data processorData processor–– Carries out processing of collected information and computes the system Carries out processing of collected information and computes the system
responseresponse..
Dr. D. M. Akbar Hussain
Department of Electronic Systems 13
•• Actuator control processesActuator control processes–– Generates control signals for the actuators.Generates control signals for the actuators.
Java as a Java as a RealReal--time time LLanguageanguage
•• Java supports lightweight concurrency (threads and synchronized methods) andJava supports lightweight concurrency (threads and synchronized methods) and•• Java supports lightweight concurrency (threads and synchronized methods) and Java supports lightweight concurrency (threads and synchronized methods) and can be used for some can be used for some soft realsoft real--time systemstime systems..
•• Java 2.0 is not suitable for Java 2.0 is not suitable for Hard Hard RT programming but realRT programming but real--time versions of Java are time versions of Java are now available that address problems such now available that address problems such as:as:
–– Not possible to specify thread execution Not possible to specify thread execution time.time.–– Different timing in different virtual Different timing in different virtual machines.machines.–– Uncontrollable garbage Uncontrollable garbage collection.collection.–– Not possible to discover queue sizes for shared Not possible to discover queue sizes for shared resources.resources.
Dr. D. M. Akbar Hussain
Department of Electronic Systems 15
p qp q–– Not possible to access system Not possible to access system hardware.hardware.–– Not possible to do space or timing analysis.Not possible to do space or timing analysis.
•• AggregateAggregate thethe stimulusstimulus andand responseresponse processingprocessing intointo concurrentconcurrentAA bb i t di t d ithith hh ll ff ti lti l dd
S tS t d llid lli h lh l thth l tl t tt d t dd t d thth f ti litf ti lit ff thth•• SystemSystem modellingmodelling helpshelps thethe analystanalyst toto understandunderstand thethe functionalityfunctionality ofof thethesystemsystem andand modelsmodels areare usedused toto communicatecommunicate withwith customerscustomers..
•• Behavioural models are used to describe the overall behaviour of a system.Behavioural models are used to describe the overall behaviour of a system.
•• Two types of behavioural model are:Two types of behavioural model are:
–– Data processing models Data processing models that show how data is processed as it moves through the system;that show how data is processed as it moves through the system;–– State machine models State machine models that show the systems response to events.that show the systems response to events.
•• These modelsThese models show different perspectivesshow different perspectives so both of them are required to describe theso both of them are required to describe the
•• These models These models show different perspectives show different perspectives so both of them are required to describe the so both of them are required to describe the system’s behaviour.system’s behaviour.
DE5 Thursday, September 23, 2010
Real-time Embedded System Programming 12
Behavioural Model ExampleBehavioural Model Example
•• Structured methods incorporate system modelling as an inherent part of the method.Structured methods incorporate system modelling as an inherent part of the method.
•• Methods define a set of models, a process for deriving these models and rules and Methods define a set of models, a process for deriving these models and rules and guidelines that should apply to the models.guidelines that should apply to the models.
•• CASE tools support system modelling as part of a structured method.CASE tools support system modelling as part of a structured method.
•• Data processing model Data processing model showing how the data is processed at different stages.showing how the data is processed at different stages.•• Composition model Composition model showing how entities are composed of other entities.showing how entities are composed of other entities.•• Architectural model Architectural model showing principal subshowing principal sub--systems.systems.•• Classification model Classification model showing how entities have common characteristics.showing how entities have common characteristics.•• Stimulus/ResponseStimulus/Response model showing the system’s reaction to events.model showing the system’s reaction to events.
Dr. D. M. Akbar Hussain
Department of Electronic Systems 26
DE5 Thursday, September 23, 2010
Real-time Embedded System Programming 14
RealReal--Time System ModellingTime System Modelling
ThTh ff tff t ff ti lti l ii ll titi tt t it i t itit iti ff•• TheThe effecteffect ofof aa stimulusstimulus inin aa realreal--timetime systemsystem maymay triggertrigger aa transitiontransition fromfromoneone statestate toto anotheranother..
RealReal--Time Operating Time Operating SSystemsystems
R lR l titi titi tt i li di li d titi tt hi hhi h•• RealReal--timetime operatingoperating systemssystems areare specialisedspecialised operatingoperating systemssystems whichwhichmanagemanage thethe processesprocesses inin thethe RTSRTS..
NonNon--Stop System Stop System CComponentsomponents
•• Configuration Configuration ManagerManager–– Responsible for the dynamic reconfiguration of the Responsible for the dynamic reconfiguration of the system software system software and hardware. and hardware.
Hardware modules may be replaced and software upgraded without stopping the Hardware modules may be replaced and software upgraded without stopping the systemssystems..
•• Fault Fault ManagerManager–– Responsible for detecting software and hardware faults and Responsible for detecting software and hardware faults and taking taking appropriate actions appropriate actions
(e.g. switching to backup disks) to ensure that the system continues in operation.(e.g. switching to backup disks) to ensure that the system continues in operation.
Kernel Space Kernel Space vsvs User Space User Space vsvs Real Time Real Time SpaceSpace
Most modern processors allow programs to run in two different hardware protectionMost modern processors allow programs to run in two different hardware protectionlevels.
Linux calls these two levels kernel space and user space.
The latter have more protection against erroneous accesses to physical memory of I/Odevices.
The real-time Linux variants add a third layer, the real-time space.
Dr. D. M. Akbar Hussain
Department of Electronic Systems 34
y , p
This is in fact nothing else but a part of kernel space used, but used in a particular way.
1. A monolithic kernel has all OS services (including device drivers, network stacks, file systems,etc.) running within the privileged mode of the processor.
2. A micro-kernel, on the other hand, uses the privileged mode only for really core services (taskmanagement and scheduling, inter-process communication, interrupt handling, and memorymanagement), and has most of the device drivers and OS services running as “normal” tasks.
3. The trade-off between both is as follows:
1. Monolithic kernel is easier to make more efficient (because OS services can runcompletely without switches from privileged to non-privileged mode).
2 Micro kernel is more difficult to crash (an error in a device driver that doesn’t run in
Dr. D. M. Akbar Hussain
Department of Electronic Systems 35
2. Micro-kernel is more difficult to crash (an error in a device driver that doesn t run inprivileged mode is less likely to cause a system halt than an error occurring in privilegedmode).
Scalability is much lessless ofof anan issueissue inin realreal--timetime applications,applications, because the goals are so different:
The desire behind scalable systems is to divide a large work load transparentlyover a number of available CPUs.
The desiredesire behindbehind realreal--timetime systems is that everything is controlled in a strictlydeterministic way.
Dr. D. M. Akbar Hussain
Department of Electronic Systems 38
DE5 Thursday, September 23, 2010
Real-time Embedded System Programming 20
Memory ManagementMemory Management
Virtual memory and dynamic allocation and de-allocation of memory pages are amongst the mostcommonly used memory management services of a general purpose operating system.
However, this memory management induces overhead, and some simpler processors have no supportfor this memory management. On these processors (which power an enormous number of embeddedsystems!), all tasks share the same memory space, such that developers must take care of the properuse of that memory.
Also some real-time kernels (such as RTLinux) have all their tasks share the same address space
Dr. D. M. Akbar Hussain
Department of Electronic Systems 39
( ) p(even if the processor supports memory management), because this allows more efficient code.
Dedicated Dedicated VsVs GeneralGeneral
For many applications, it is worthwhile not to use a commercially or freely availableoperating system but write one that is optimised for the task at handoperating system, but write one that is optimised for the task at hand.
Examples are the operating systems for mobile phones, or Personal Digital Assistants.
Standard operating systems are too big, and they don’t have the specific signal processingsupport (speech and handwriting recognition) that is typical for these applications.
Some applications even don’t need an operating system at all. (For example, a simplevending machine).
Dr. D. M. Akbar Hussain
Department of Electronic Systems 40
The trade-offs here are: cost of development and decreased portability, against cost ofsmaller and cheaper embedded systems.
DE5 Thursday, September 23, 2010
Real-time Embedded System Programming 21
Operating System Operating System VSVS Language RuntimeLanguage Runtime
Application programs make use of “lower-level” primitives to build their functionality ThisApplication programs make use of lower level primitives to build their functionality. Thisfunctionality can be offered by the operating system (via system calls), or by a programminglanguage (via language primitives and libraries).
Languages such as C++, Ada and Java offer lots of functionality this way: memory management,threading, task synchronization, exception handling, etc. This functionality is collected in a so-calledruntime.
The advantages of using a runtime are: its interface is portable over different operating systems, andit offers ready-to-use and/or safe solutions to common problems.
Dr. D. M. Akbar Hussain
Department of Electronic Systems 41
The disadvantages are that a runtime is in general “heavy”, not deterministic in execution time, andnot very configurable. These disadvantages are important in real-time and embedded contexts.
Process Process PriorityPriority
•• The processing of some types of stimuli must The processing of some types of stimuli must sometimes sometimes take prioritytake priority..
•• Interrupt level priority. Interrupt level priority. Highest priority which is Highest priority which is allocated allocated to processes requiring a to processes requiring a very fast very fast response.response.
•• Clock level priority.Clock level priority. Allocated to periodic Allocated to periodic processes.processes.
•• Within these, further levels of priority may beWithin these, further levels of priority may be assignedassigned..
Dr. D. M. Akbar Hussain
Department of Electronic Systems 42
Within these, further levels of priority may be Within these, further levels of priority may be assignedassigned..
•• Control is transferred Control is transferred automaticallyautomatically to a to a prepre--determined determined memory locationmemory location..
•• This location contains an instruction This location contains an instruction to jump to to jump to an an interrupt service interrupt service routine (ISR).routine (ISR).
•• Further interrupts are disabled, Further interrupts are disabled, the interrupt the interrupt serviced serviced and control returned to the and control returned to the interrupted interrupted process.process.
Dr. D. M. Akbar Hussain
Department of Electronic Systems 43
•• Interrupt service routines Interrupt service routines MUST be shortMUST be short, , simple simple and fast.and fast.
•• Concerned with managing the set of concurrent processesConcerned with managing the set of concurrent processes..g g pg g p
•• Periodic processes are executed at prePeriodic processes are executed at pre--specified time intervalsspecified time intervals..
•• The RTOS uses the realThe RTOS uses the real--time clock to determine when to execute time clock to determine when to execute a process a process taking into accounttaking into account::
–– Process period Process period -- Time Time between executions.between executions.–– Process deadline Process deadline -- The The time by which processing must be complete.time by which processing must be complete.
•• The scheduler chooses the The scheduler chooses the next process next process to be executed by the processor. to be executed by the processor.
•• This This depends on a scheduling strategy which may take the depends on a scheduling strategy which may take the process priority process priority into accountinto account..
•• The resource manager The resource manager allocates memory and a processor allocates memory and a processor for the process for the process to be executedto be executed..
Dr. D. M. Akbar Hussain
Department of Electronic Systems 47
•• The dispatcher takes the process The dispatcher takes the process from ready list, loads it from ready list, loads it onto a processor onto a processor and starts execution.and starts execution.
•• Non preNon pre--emptive schedulingemptive scheduling–– Once a process has been scheduled for execution, it runs to completion or Once a process has been scheduled for execution, it runs to completion or
until it is blocked for some reason (e.g. waiting for I/Ountil it is blocked for some reason (e.g. waiting for I/O).).
•• PrePre--emptive schedulingemptive scheduling–– The execution of an executing processes may be stopped if a higher priority The execution of an executing processes may be stopped if a higher priority
process requires serviceprocess requires service..
•• A system is required to monitor sensors on doors and windows to A system is required to monitor sensors on doors and windows to detect the presence of intruders in a buildingdetect the presence of intruders in a building..
•• When a sensor indicates a breakWhen a sensor indicates a break--in, the system switches on lights in, the system switches on lights around the area and calls police automaticallyaround the area and calls police automatically..
•• The system should include provision for operation without a mains The system should include provision for operation without a mains
•• SensorsSensors–– Movement detectors, window sensors, door sensors;Movement detectors, window sensors, door sensors;–– 50 window sensors, 30 door sensors and 200 movement 50 window sensors, 30 door sensors and 200 movement detectorsdetectors;;–– Voltage drop sensorVoltage drop sensor..
•• ActionsActions–– When an intruder is detected, police are called When an intruder is detected, police are called automaticallyautomatically;;–– Lights are switched on in rooms with active sensors;Lights are switched on in rooms with active sensors;–– An audible alarm is switched on;An audible alarm is switched on;
Th t it h t ti ll t b k h lt d iTh t it h t ti ll t b k h lt d i
Dr. D. M. Akbar Hussain
Department of Electronic Systems 52
–– The system switches automatically to backup power when a voltage drop is The system switches automatically to backup power when a voltage drop is detected.detected.
•• Power Power FailureFailure–– Generated Generated aa--periodically periodically by a circuit monitor. When received, the system by a circuit monitor. When received, the system
must switch to backup power within 50 msmust switch to backup power within 50 ms..
•• Intruder Intruder AlarmAlarm–– Stimulus generated by system sensors. Response is to call the police, switch Stimulus generated by system sensors. Response is to call the police, switch
on building lights and the audible alarm.on building lights and the audible alarm.
Stimulus/Response Timing requirementsPower fail interrupt The switch to backup power must be completedPower fail interrupt The switch to backup power must be completed
within a deadline of 50 ms. Door alarm Each door alarm should be polled twice per
second.Window alarm Each window alarm should be polled twice per
second.Movement detector Each movement detector should be polled twice
per second.Audible alarm The audible alarm should be switched on within
1/2 second of an alarm being raised by a sensor. Li ht it h Th li ht h ld b it h d ithi 1/2
Dr. D. M. Akbar Hussain
Department of Electronic Systems 55
Lights switch The lights should be switched on within 1/2 second of an alarm being raised by a sensor.
Communications The call to the police should be started within 2 seconds of an alarm being raised by a sensor.
Voice synthesiser A synthesised message should be available within 4 seconds of an alarm being raised by a sensor.
Building monitor process 2Building monitor process 2public void run ()
{int room = 0 ;while (true)while (true){
// poll the movement sensors at least twice per second (400 Hz)move = movements.getVal () ;// poll the window sensors at least twice/second (100 Hz)win = windows.getVal () ;// poll the door sensors at least twice per second (60 Hz)door = doors.getVal () ;if (move.sensorVal == 1 | door.sensorVal == 1 | win.sensorVal == 1)