Transcript ppt

RTX - RTOS
MS_uC / fue1 / V01
5- 1
Programming Microcontroller
RTX - Real Time Operating System - RTOS
Autumn term 2012
RTOS - RTX
MS_uC / fue1 / V01
5- 2
Tasks of the Operating System (1/3)
 Key tasks of the OS (Kernel)
 Additional general tasks (OS)
 External general tasks (Utilities)
 Image processing
Utilities
OS
Kernel
2
RTOS - RTX
MS_uC / fue1 / V01
5- 3
Tasks of the OS (2/3)
 Kernel
Resource Management
Attribution of CPU time
Task-, Process- or (Thread-) management
Memory Management
Peripheral Devices
Time of the Day and Date
3
RTOS - RTX
MS_uC / fue1 / V01
5- 4
Tasks of the OS (3/3)
 Additional general tasks (OS)
I/O management (Driver)
File management
Interrupt treatments
Processing "Power-up", "Power-down", "Shut-up", "Shut-down"…
Troubleshooting
 External general tasks (Utilities)
 Image processing
RTOS - RTX
MS_uC / fue1 / V01




5- 5
RTOS: Basic themes
Real time
Multitasking res. multiprogramming
Scheduling
Tasks, Threads and Processes
5
RTOS - RTX
MS_uC / fue1 / V01
5- 6
Real Time (RT) computer
 RT computer works synchronously to the rhythm of the technical
process
It responds on time to its needs
It takes measures on times
Real time processor
Process image
Control algorithm
Output
data
Input
data
Control
Process
variables characteristics
Process
parameter
Metrics
Events
Discrete
events
Technical
process
Process
input
Process
output
Interference
Retroactivity
RTOS - RTX
MS_uC / fue1 / V01
5- 7
Multitasking res. multiprogramming
 Multitasking OS allows the programming with temporally
independent parallel units
Simulation of mutually independent, quasi-parallel operating
processors
 Example
Priority
I/O
Job A
Job B
t
t1
t2
RTOS - RTX
MS_uC / fue1 / V01
5- 8
Multitasking res. multiprogramming
 Multiple tasks can run simultaneously on one computer
A task can be started before the other tasks have been completed
 Example: Windows7
RTOS - RTX
MS_uC / fue1 / V01
5- 9
Tasks, threads and processes
 A task is a ideal program unit with a time response
The code for a specific task is defined once
Many task can be generated and started from this definition
 Tasks can run concurrently
Task can be started while another is still working
 Depending on the OS, Task are always known and existing
They can be started or terminated
They can communicate with each other
 Quasi-parallelism is realized by switching from one to another task
RTOS - RTX
MS_uC / fue1 / V01
5- 10
State of a task
 The task states are managed with FIFO
 Each task state contains its own FIFO
Blocked
Not Existing
ReadyTo-Run
Running
RTOS - RTX
MS_uC / fue1 / V01
5- 11
Description of the task states
 Not-Existing
Does not exist in OS, which contain only active tasks
 Running
Task, which is currently executed by the CPU
 Ready-To-Run
All the conditions, which are needed to run the task are, are realized
CPU is currently occupied with the execution of another task
 Blocked
Task is waiting because of synchronization tools
Semaphore, Event-flags, Suspend/Resume etc.
RTOS - RTX
MS_uC / fue1 / V01
5- 12
Scheduling
 Scheduling is the process, which attributes CPU time to the
threads, processes or task
 Non preemptive Scheduling
Task exchanges are only realized at given points
Task must systematically give back CPU time
 Preemptive scheduling
Preemptive means interruption
Task can loose CPU time at any point during its execution
Can be realized only with “Interrupt-Response-Program”
Clock tics
RTOS - RTX
MS_uC / fue1 / V01
5- 13
Round Robin Scheduling
 Each task becomes a certain number of CPU time quanta
Task will be stopped, if it is not finished after this amount of time
If the task is finished before, it will be interrupted immediately
 The active task are managed with a list
A
G
B
C
E
 When the task A is interrupted, it will be put at the end of the list
G
B
C
E
A
RTOS - RTX
MS_uC / fue1 / V01
5- 14
Priority Scheduling
 All the task have the same priority
 All the task have predefined priorities
The priority for each task must be unique
Several tasks can have the same priority
Os Algorithm manage their precedence (Round Robin)
The priority is set explicitly or implicitly at task start and will be
changed explicitly later
The priority remains the same during all the existence of the task
(static)
Priority are changed dynamically by the scheduler
RTOS - RTX
MS_uC / fue1 / V01
5- 15
Definition and starting of a task
within Keil
 Definition of the task code
 __task void task_name (void) {
/* Initialization of the task resources */
…
while(1) {
/* Execution of the task algorithms */
…
}
}
 Create and start the task
 int main (void) {
…
/* 2 variants to create and start the task */
os_tsk_create (task_name, prio);
os_tsk_create_user (task_name, prio, \
task_stack, sizeof(task_stack));
…
}
RTOS - RTX
MS_uC / fue1 / V01
5- 16
Classical problems of the parallel
data processing
 Introduction example : Digital Voltmeter (DVM)
A/D conversion time: 75 ms
LCD must be “refreshed” after 50 ms
 The multitasking enables to enhance the design and the program
speed
Solution for the screen flickering
Acquisition
Task
Display
Task
Data store
Voltage
A/D
Microprocessor
Display (LCD)
16
MS_uC / fue1 / V01
13.04.
2015
Code example
Acquisition Task
while (1) {
Wait until A/D ready
Read A/D
Process measurements
for (all digits) {
Store digit
} // end for
} // end while
Display Task
while (1) {
Wait until LCD needs refresh
for (all digits) {
Fetch digit from store
Display digit on LCD
} // end for
} // end while
 Problem of this solution
“Acquisition” and “Display” tasks can access to the data simultaneously
Sometimes wrong values will be displayed
Program must guaranty the mutual exclusion
17
RTOS - RTX
MS_uC / fue1 / V01
5- 18
Mutual Exclusion (MUTEX)
 2 train must bypass trough a critical section (Tunnel)
Only one train is authorized to go through the tunnel at the given
moment
Both locomotive drivers are blind and deaf
The can only deposit or retire a stone in a bowl
18
RTOS - RTX
MS_uC / fue1 / V01
5- 19
First solution
 Procedure for the locomotive conductors
#define EMPTY 0
#define OCCUPIED 1
int bowl; // semaphore
while (bowl == OCCUPIED) {
/*make Siesta*/
}
//search a stone
bowl = OCCUPIED ;
// drive the train through the critical tunnel
bowl = EMPTY ;
MS_uC / fue1 / V01
13.04.
2015
Frage zur ersten Lösung
 Is this procedure really sure?
No, because the exclusion is not enough sure (danger of collision)
Train A
Finds the bowl empty
Search a stone
>>--------->>
Put the stone into the bowl
Pass through the critical part
Train B
Finds the bowl empty
Search a stone
Put the stone into the bowl
Pass through the critical part
20
MS_uC / fue1 / V01

13.04.
2015
Solution with a privileged section
// Cover the bowl, wait if already covered
*1)
while (bowl == NOT_EMPTY) {};
// Search a stone
bowl = NOT EMPTY ;
// Uncover the bowl
// Drive the train through the critical tunnel
bowl = EMPTY ;
// *2)
// continue the non critical part of the journey
 Comments
*1) Wait if the cover is already on the bowl
*2) Access possible, even with covered bowl
 Is this procedure really sure?
Yes, because it fulfills the mutual exclusion
MS_uC / fue1 / V01
13.04.
2015
SEMAPHORE
 Flag, which can be accessed only within privileged section
Dijkstra 1968
 Coordination using privileged actions
flag = 1;
// *1)
while (flag == 0) { /* nothing */ }
flag = flag - 1 ;
// go through the critical region
flag = flag + 1 ;
// continue the non critical part of the job
 *1) init-value defines the number of trains allowed in the critical part
Value = 1 → binary semaphore
Value > 1 → general semaphore
22
MS_uC / fue1 / V01
13.04.
2015
Privileged function
P(int * s) & V(int * s)
 Definition of the privileged functions
proberen 
verhogen 
try
increment
void proberen (int *s) {
while (*s == 0) {}
*s = *s - 1;
} // end proberen
// ev. <= 0
void verhogen (int *s) {
*s = *s + 1;
} // end verhogen
23
MS_uC / fue1 / V01
13.04.
2015
Using of the privileged functions
within tasks
 #define n 1
#define n > 1
int s = n;
// for a binary semaphore
// for a general semaphore
probeeren (&s);
// go though critical region
verhogen (&s);
// go though rest of job
24
MS_uC / fue1 / V01
13.04.
2015
Problem of the waiting loop of
probeeren
 The waiting loop of probeeren occupies the CPU almost by 100%
while (s == 0) { /* nothing */ }
 Solution
Task which calls the waiting process must be put in a waiting list
MS_uC / fue1 / V01
13.04.
2015
Procedure wait(int* s)
void wait (int *sema) {
// wait if resource occupied
if (*sema == 0) {
// enter this task into the sema-waiting-queue for this
// semaphore Q(s) and leave the "ready state"
// (goes sleeping in a waiting state)
} else {
*sema = *sema - 1;
} // end if … else
} // end wait
26
MS_uC / fue1 / V01
13.04.
2015
Procedure signal (int *s)
void signal (int *sema) {
// free the resource
// read the waiting queue Q(s).
if /* this queue was EMPTY */
*sema = *sema + 1 ;
} else {
// change this sleeping task to the ready-to run state *
// and call a wait(&sema);
} // end if … else
} // end signal
27
MS_uC / fue1 / V01
13.04.
2015
Definition of the semaphore variable
selon Dijkstra
 Definition of the semaphore variable
int s >= 0
 Rules for the semaphore variable
Binary semaphore:
General semaphore:
s = {0, 1}
0 <= s <= n
 Each semaphore variable contains its own waiting list
Writing into and reading from is realized in privileged mode
 wait(int *s) & signal(int *s) are privileged functions
28
signal and wait functions of Keil
RTOS - RTX
MS_uC / fue1 / V01
5- 29
 Semaphore functions of Keil
void os_sem_init (OS_ID semaphore, U16 token_count);
OS_RESULT os_sem_send (OS_ID semaphore);
OS_RESULT os_sem_wait (OS_ID semaphore, U16 timeout);
 Telegram functions of Keil
 void os_mbx_init (OS_ID mailbox, U16 mbx_size);
 OS_RESULT os_mbx_send (OS_ID mailbox, void *message_ptr,
U16 timeout);
 OS_RESULT os_mbx_wait (OS_ID mailbox, void **message,
U16 timeout);
RTOS - RTX
MS_uC / fue1 / V01
5- 30
Keil code for the locomotiv conductors
OS_SEM sema;
// definition of the semaphore as global variable
…
os_sem_init(&sema,1);
// Initialization of the semaphore in main
Locomotive driver: West
Locomotive driver: East
while (1) {
res = os_sem_wait(&sema, 1000);
if (res != OS_R_TMO) {
/* Go through the critical
section */
os_sem_send(&sema);
} // end if
} // end while
while (1) {
res = os_sem_wait(&sema, 1000);
if (res != OS_R_TMO) {
/* Go through the critical
section */
os_sem_send(&sema);
} // end if
} // end while
Keil code for parallel data processing
RTOS - RTX
MS_uC / fue1 / V01
5- 31
Acquisition
Task
Display
Task
Data store
Voltage
A/D
Microprocessor
Display (LCD)
/* Declaration of a mailbox for 20 messages */
os_mbx_declare (mbox, 20);
Acquisition Task
Display Task
int *p_msg;
while (1) {
/* Wait until A/D ready and
store its conversion result
into new_AD_value */
p_msg = malloc(sizeof(int));
*p_msg = new_AD_value;
if (os_mbx_check(&mbox) > 0) {
rt_mbx_send(&mbox, p_msg,10);
} // end if
os_dly_wait (75);
} // end while
int *p_msg;
while (1) {
if (os_mbx_check(&mbox) < 20) {
rt_mbx_wait(&mbox,&p_msg,10);
/* Display od the LCD
the measurements
addressed by p_msg */
free(p_msg);
} // end if
os_dly_wait (50);
} // end while