ms_uc / fue1 / v01 5- 1 rtx - rtos programming microcontroller rtx - real time operating system -...
TRANSCRIPT
MS_uC / fue1 / V01 5- 1
RTX - RTOS
Programming MicrocontrollerRTX - Real Time Operating System - RTOS
Autumn term 2012
MS_uC / fue1 / V01
Tasks of the Operating System (1/3)
Key tasks of the OS (Kernel) Additional general tasks (OS) External general tasks (Utilities)
Image processing
2
Utilities
OS
Kernel
RTOS - RTX
5- 2
MS_uC / fue1 / V01
3
Kernel Resource Management
Attribution of CPU timeTask-, Process- or (Thread-) management Memory ManagementPeripheral Devices
Time of the Day and Date
Tasks of the OS (2/3)RTOS - RTX
5- 3
MS_uC / fue1 / V01Tasks 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
5- 4
RTOS - RTX
MS_uC / fue1 / V01RTOS: Basic themes
Real time Multitasking res. multiprogramming Scheduling Tasks, Threads and Processes
5
RTOS - RTX
5- 5
MS_uC / fue1 / V01Real 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
5- 6
RTOS - RTX
Discrete events
Interference
Process output
Process input
Process parameter
Process characteristics
Input data
Metrics Control variables
Output data
Real time processor
Technical process
Events
Retroactivity
Process image
Control algorithm
MS_uC / fue1 / V01 Multitasking res. multiprogramming
Multitasking OS allows the programming with temporally independent parallel units Simulation of mutually independent, quasi-parallel operating
processors
Example
5- 7
RTOS - RTX
Job B
Job A
I/O
Priority
t t1 t2
MS_uC / fue1 / V01 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
5- 8
RTOS - RTX
MS_uC / fue1 / V01Tasks, 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
5- 9
RTOS - RTX
MS_uC / fue1 / V01
The task states are managed with FIFO Each task state contains its own FIFO
State of a task
Not Existing
Ready- To-Run
Blocked
Running
RTOS - RTX
5- 10
MS_uC / fue1 / V01Description 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.
5- 11
RTOS - RTX
MS_uC / fue1 / V01Scheduling
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
5- 12
RTOS - RTX
MS_uC / fue1 / V01Round 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
When the task A is interrupted, it will be put at the end of the list
5- 13
RTOS - RTX
A G B C E
G B C E A
MS_uC / fue1 / V01Priority 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
5- 14
RTOS - RTX
MS_uC / fue1 / V01
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));
…
}
5- 15
RTOS - RTX
MS_uC / fue1 / V01
Classical problems of the parallel data processing
16
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
Voltage A/D Microprocessor Display (LCD)
Data store
Acquisition Task
Display Task
RTOS - RTX
5- 16
MS_uC / fue1 / V01 20.04.23
17
Code example
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
Acquisition Task Display Task
while (1) {
Wait until A/D ready
Read A/D
Process measurements
for (all digits) {
Store digit
} // end for
} // end while
while (1) {
Wait until LCD needs refresh
for (all digits) {
Fetch digit from store
Display digit on LCD
} // end for
} // end while
MS_uC / fue1 / V01 Mutual Exclusion (MUTEX)
18
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
RTOS - RTX
5- 18
MS_uC / fue1 / V01First 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 ;
5- 19
RTOS - RTX
MS_uC / fue1 / V01 20.04.23
20
Frage zur ersten Lösung
Is this procedure really sure? No, because the exclusion is not enough sure (danger of
collision)
Train A Train B
Finds the bowl empty
Search a stone >>--------->>
Put the stone into the bowl
Pass through the critical part
Finds the bowl empty
Search a stone
Put the stone into the bowl
Pass through the critical part
MS_uC / fue1 / V01 20.04.23
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 20.04.23
22
SEMAPHORE
Flag, which can be accessed only within privileged section Dijkstra 1968
Coordination using privileged actions
*1) init-value defines the number of trains allowed in the critical part Value = 1 → binary semaphore Value > 1 → general semaphore
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
MS_uC / fue1 / V01 20.04.23
23
Definition of the privileged functions proberen try verhogen increment
void proberen (int *s) {
while (*s == 0) {} // ev. <= 0
*s = *s - 1;
} // end proberen
void verhogen (int *s) {
*s = *s + 1;
} // end verhogen
Privileged function P(int * s) & V(int * s)
MS_uC / fue1 / V01 20.04.23
24
Using of the privileged functions within tasks
#define n 1 // for a binary semaphore
#define n > 1 // for a general semaphore
int s = n;
probeeren (&s);
// go though critical region
verhogen (&s);
// go though rest of job
MS_uC / fue1 / V01 20.04.23
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 20.04.23
26
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
MS_uC / fue1 / V01 20.04.23
27
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
MS_uC / fue1 / V01
Definition of the semaphore variable selon Dijkstra
Definition of the semaphore variable int s >= 0
Rules for the semaphore variable Binary semaphore: s = {0, 1} General semaphore: 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
20.04.23
28
MS_uC / fue1 / V01signal and wait functions of Keil
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);
5- 29
RTOS - RTX
MS_uC / fue1 / V01Keil code for the locomotiv conductors
5- 30
RTOS - RTX
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
MS_uC / fue1 / V01Keil code for parallel data processing
5- 31
RTOS - RTX
/* 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
Voltage A/D Microprocessor Display (LCD)
Data store
Acquisition Task
Display Task