Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University...
-
Upload
letitia-allison -
Category
Documents
-
view
218 -
download
1
Transcript of Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University...
![Page 1: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/1.jpg)
Real-time OS
Hao-yuan Chin
Feb. 18, 2002
![Page 2: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/2.jpg)
2
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Outline
• Introduction to RTOS • Introduction to μC/OS-II
![Page 3: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/3.jpg)
3
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Real Time OS
Real-time OS (RTOS) is an intermediate layer between hardware devices and software programming
“Real-time” means keeping deadlines, not speed
Advantages of RTOS in SoC design
• Shorter development time
• Less porting efforts
• Better reusability
Disadvantages
• More system resources needed
• Future development confined to the chosen RTOS
![Page 4: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/4.jpg)
4
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Soft and Hard Real Time
Soft real-time
• Tasks are performed by the system as fast as possible, but tasks don’t have to finish by specific times
• Priority scheduling
• Multimedia streaming
Hard real-time
• Tasks have to be performed correctly and on time
• Deadline scheduling
• Aircraft controller, Nuclear reactor controller
![Page 5: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/5.jpg)
5
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Outline
• Introduction to RTOS • Introduction to μC/OS-II
![Page 6: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/6.jpg)
6
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
μC/OS-II
Written by Jean J. Labrosse in ANSI C
A portable, ROMable, scalable, preemptive, real-time, multitasking kernel
Used in hundreds of products since its introduction in 1992
Certified by the FAA for use in commercial aircraft
Available in ARM Firmware Suite (AFS)
Over 90 ports for free download
http://www.ucos-ii.com
![Page 7: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/7.jpg)
7
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
μC/OS-II Features
Portable 8-bit ~ 64 bit
ROMable small memory footprint
Scalable select features at compile time
Multitasking preemptive scheduling, up to 64 tasks
![Page 8: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/8.jpg)
8
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
C/OS-II vs. HAL
uHAL is shipped in ARM Firmware Suite
uHAL is a basic library that enables simple application to run on a variety of ARM-based development systems
uC/OS-II use uHAL to access ARM-based hardware
uC/OS-II & User application AFS Utilities
C, C++ libraries
uHAL routines
Development board
AFS supportroutines
![Page 9: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/9.jpg)
9
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Kernel
Basic jobs of uC/OS-II kernel
• Task management
• Interrupt handling
• Inter-process communication
![Page 10: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/10.jpg)
10
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Kernel API
Service routines provided by uC/OS-II Kernel
• Task management APIs
• Time management APIs
• Memory management APIs
• Inter-process communication APIs
• Synchronization APIs
To make a System Call means to call Kernel API
![Page 11: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/11.jpg)
11
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task
Task is an instance of program
Task thinks that it has the CPU all to itself
Task is assigned a unique priority
Task has its own set of stack
Task has its own set of CPU registers (backup in its stack)
Task is the basic unit for scheduling
Task status are stored in Task Control Block (TCB)
![Page 12: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/12.jpg)
12
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task Structure
Task structure:
An infinite loop
An self-delete function
void ExampleTask(void *pdata)
{
for(;;) {
/* User Code */
/* System Call */
/* User Code */
}
}
Task with infinite loop structure
void ExampleTask(void *pdata)
{
/* User Code */
OSTaskDel(PRIO_SELF);
}
Task that delete itself
![Page 13: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/13.jpg)
13
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task States
WaitingWaiting
DormantDormantReadyReady RunningRunning ISRISR
Task Create
Task Delete
Highest Priority Task
Task is Preempted
Task Pending EventsTask Gets Event
Task Delete
Interrupt
Int. Exit
Task Delete
![Page 14: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/14.jpg)
14
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task Priority
Unique priority (also used as task identifiers)
64 priorities max (8 reserved)
Always run the highest priority task that is READY
Allow dynamically change priority
![Page 15: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/15.jpg)
15
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task Control Block
uC/OS-II use TCB to keep record of each task
States
Stack Pointer
Priority
Misc …
Link Pointer
![Page 16: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/16.jpg)
16
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Exchanging CPU Control
void ExampleTask(void *pdata)
{
for(;;) {
/* User Code */
/*System Call */
/* User Code */
}
}
OSMboxPend();
OSQPend();
OSSemPend();
OSTaskSuspend();
OSTimeDly();
OSTimeDlyHMSM();
More…
uC/OS-II Kernel API
Control returns from task to OS when Kernel API is called
![Page 17: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/17.jpg)
17
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Exchanging CPU Control
A B B C A
Interrupt Handler
OS
Task
Time
Scheduling
System Call
Interrupt
Interrupt Exit
Only one of OS, Task, Interrupt Handler gets CPU control at a time
![Page 18: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/18.jpg)
18
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task Scheduling
Time
ISR
Low-priority Task
High-priority Task
ISR makes thehigh-priority task ready
low-priority taskRelinquishes the CPU
Non-preemptive
![Page 19: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/19.jpg)
19
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Task Scheduling
Preemptive
uC/OS-II adopts preemptive scheduling
Time
ISR
Low-priority Task
High-priority Task
ISR makes thehigh-priority task ready
high-priority taskRelinquishes the CPU
![Page 20: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/20.jpg)
20
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Context Switching
Context SwitchingThe process of swap in/out the context of tasks when scheduler choose a new task to run.
ContextUsually means values in registers used by the task.
Switching Steps1. Save registers of current task to stack(curr)
2. Load new task’s SP into CPU
3. Restore registers of new task from stack(new)
4. Resume execution of new task
![Page 21: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/21.jpg)
21
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Context Switching
Time
ISR
Low-priority Task
High-priority Task
ISR makes thehigh-priority task ready
ContextSwitching
ContextSwitching
high-priority taskRelinquishes the CPU
![Page 22: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/22.jpg)
22
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Critical Region
Code need to be treated indivisibly
Code have to be executed without interrupt
creating task…etc
Non-reentrant code
accessing shared variable…etc
![Page 23: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/23.jpg)
23
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Critical Region
Protecting critical region
• OS_ENTER_CRITICAL( )
temporarily disable interrupt
• OS_EXIT_CRITICAL( )
re-enable interrupt
![Page 24: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/24.jpg)
24
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Events
A way for Synchronization and Communication
• Pend (Wait for an event)
• Post (Signal an event)
Events in uC/OS-II
• Semaphore
• Counting Semaphore
• Message Mailbox
• Message Queues
![Page 25: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/25.jpg)
25
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Semaphore
Semaphore serves as a key to the resource
A flag represent the status of the resource
Prevent re-entering Critical Region
Can extent to counting Semaphore
Task 1Task 1
Task 2Task 2
RS-232RS-232
Send data via RS232
Send data via RS232
Semaphore
Request/Release
Request/Release
![Page 26: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/26.jpg)
26
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Semaphore
Using Semaphore in uC/OS-II
OSSemCreate()
OSSemPend()
OSSemPost()
OSSemQuery()
OSSemAccept()
![Page 27: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/27.jpg)
27
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Message Mailbox
Used for Inter-Process Communication (IPC)
A pointer in MailBox points to the transmitted message
TaskTask
ISRISR
TaskTask
“message”
Mail BoxPost
Post
Pend
![Page 28: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/28.jpg)
28
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Message Queues
Array of MailBox
FIFO & FILO configuration
TaskTask
ISRISR
TaskTask
“message”
Mail QueuesPost
Post
Pend
![Page 29: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/29.jpg)
29
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
MailBox & Queues
Using MailBox in uC/OS-II
OSMboxCreate()
OSMboxPend()
OSMboxPost()
OSMboxQuery()
OSMboxAccept()
Using Queue in uC/OS-II
OSQCreate()
OSQPend()
OSQPost()
OSQPostFront()
OSQQuery()
OSQAccept()
OSQFlush()
![Page 30: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/30.jpg)
30
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Memory Management
Semi-dynamic memory allocation Allocate statically and dispatch dynamically Explore memory requirements at design time
Task 1Task 1 Task 2Task 2 Task 3Task 3
allocate statically
partition into memory blocks
dispatch dynamically
OS Initializing OS running
![Page 31: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/31.jpg)
31
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Memory Management
Using Memory in uC/OS-II
OSMemCreate()
OSMemGet()
OSSemPut()
OSSemQuery()
![Page 32: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/32.jpg)
32
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Interrupt
Interrupt ControllerA device that accepts up to 22 interrupt sources from other pheripherals and signals ARM processor
Interrupt HandlerA routine executed whenever an interrupt occurs. It determines the interrupt source and calls corresponding ISR. Usually provided by OS.
Interrupt Service Routine (ISR)Service routines specific to each interrupt source. This is usually provided by hardware manufacturer.
![Page 33: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/33.jpg)
33
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Interrupt
Peripheral sends interrupt request to interrupt controller
Interrupt controller sends masked request to ARM
ARM executes interrupt handler to determine int. source
Peripheral BPeripheral B
InterruptController
InterruptController
Peripheral APeripheral A
ARMProcessor
ARMProcessor
bus
![Page 34: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/34.jpg)
34
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Interrupt
Default interrupt handler: uHALr_TrapIRQ()
1. Save all registers in APCS-compliant manner
2. Call StartIRQ(), if defined
3. Determine interrupt source, call the corresponding interrupt service routine (ISR)
4. Call FinishIRQ(), if defined
5. Return from interrupt
![Page 35: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/35.jpg)
35
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Interrupt
When an interrupt occurs, no further interrupt accepted
To achieve real-time, the period of interrupt disabled should be as short as possible
Do only necessary jobs in ISR, leave other jobs in a deferred Task
![Page 36: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/36.jpg)
36
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Starting C/OS-II
Initialize hardware & uC/OS-IIARMTargetInit(), OSInit()
Initialize hardware & uC/OS-IIARMTargetInit(), OSInit()
Allocate resourcesOSMemCreate(), OSMboxCreate(), …etc
Allocate resourcesOSMemCreate(), OSMboxCreate(), …etc
Create at least one taskOSTaskCreate()
Create at least one taskOSTaskCreate()
Start SchedulerOSStart()
Start SchedulerOSStart()
![Page 37: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/37.jpg)
37
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Porting Application
Necessary coding changes
variables
• use local variables for preemption
• use semaphore to protect global variables (resources)
data transfer
• arguments => mailbox/queue
memory allocation
• malloc() => OSMemCreate() OSMemGet()
![Page 38: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/38.jpg)
38
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Porting Application
assign task priorities
unique priority level in uC/OS-II
• only 56 levels available
• priority can be change dynamically
call OSTimeDly() in infinite loop task
• ensure lower priority task get a chance to run
MUST: if lower priority task is pending data from higher priority task
![Page 39: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/39.jpg)
39
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Priority Inversion
Task 1 (H)
Task 2 (M)
Task 3 (L)
Task 3 gets semaphore (2)
Task 1 preempts Task 3 (3)
Task 1 fails to get semaphore (5)
Task 2 preempts task 3 (7)
Task 3 resumes (9)
Task 3 releases the semaphore (11)
(1)
(4)
(6)
(8)
(12)
(10)
Priority Inversion
running
waiting
![Page 40: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/40.jpg)
40
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
What is a Driver• A named entity that support basic I/O of a device
• A handler that manages interrupt from a device
• A description to a device, registered and managed by OS
• An abstraction layer for user application to access device easily
![Page 41: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/41.jpg)
41
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
Original configuration of a System –
No additional Hardware
ApplicationApplication
OSOS
HALHAL
Development BoardDevelopment Board
![Page 42: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/42.jpg)
42
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
New configuration of a System – additional Hardware added
We must add driver into system to provide easy access of new hardware
ApplicationApplication
OSOS
HALHAL
Development BoardDevelopment BoardNew HardwareNew Hardware
DriverDriver
![Page 43: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/43.jpg)
43
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
High-level API• Interactive with OS• Interface to Application
Low-level Command• Direct access to hardware (usually written in assembly)
Data• Private data to driver
ApplicationApplication
OSOS
HALHAL
Development BoardDevelopment BoardNew HardwareNew Hardware
API
CommandData
![Page 44: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/44.jpg)
44
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
Command Layer
• A set of functions/macros to manipulate hardware
• Interrupt handler
• Usually written in Assembly to get optimized speed
• Keep essential commands only
Common essential commands
• Read, Write, Init, Enable, Disable
![Page 45: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/45.jpg)
45
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
API layer
• Interactive with OS to maintain resources
• Interactive with OS to acknowledge interrupt and task scheduling
• Provides unique interface to application to achieve device abstraction
• Encapsulate driver internal data
• Combine commands to provide various functionality
Data
• Driver Status
• Data Buffer
![Page 46: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/46.jpg)
46
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
Why divide driver into two parts• Easy to replace underlying hardware
• Unique interface to applications
• Easy to adopt pre-written assembly code
![Page 47: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/47.jpg)
47
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
API Example
Available commands:
• Read_Byte
• Write_Byte
• Init_Device
• Enable_Device
• Disable_Device
Set_serial_baudrate(int baud)
{
OS_Request_Device();
Disable_Device();
Write_Byte(CONF_BASE, baud);
Enable_Device();
OS_Release_Device();
}
![Page 48: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/48.jpg)
48
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver
Send_serial(char data[], int len)
{
OS_Request_Device();
for(I=0;I<len;I++)
Write_Byte(IO_BASE, data[I]);
OS_Release_Device();
}
Available commands:
• Read_Byte
• Write_Byte
• Init_Device
• Enable_Device
• Disable_Device
API Example
![Page 49: Real-time OS Hao-yuan Chin Feb. 18, 2002. Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.](https://reader030.fdocuments.in/reader030/viewer/2022032612/56649f055503460f94c198d1/html5/thumbnails/49.jpg)
49
Institu
te of E
lectron
ics, Natio
nal C
hia
o T
ung
Un
iversity
Real-tim
e OS
Driver Design Flow
startstart
Decide I/O interface(I/O address, I/O method)
Decide I/O interface(I/O address, I/O method)
Write Assembly codeto control hardware
Write Assembly codeto control hardware
Write API base on previouslyDeveloped commands
Write API base on previouslyDeveloped commands
Write interrupt handlerWrite interrupt handler
Pack assembly codeinto commands
Pack assembly codeinto commands
endend
Test assembly codeTest assembly code
Test DriverTest Driver