Renesas Electronics America Inc.© 2012 Renesas Electronics America Inc.. © 2012 Renesas Electronics America Inc.. All rights reserved.21 Kernel Basics Application is divided into tasks
Trang 1Renesas Electronics America Inc.
© 2012 Renesas Electronics America Inc All rights reserved.
Getting Started with Micriμm’s μC/OS-III Kernel
Trang 2Renesas Technology & Solution Portfolio
Trang 3© 2012 Renesas Electronics America Inc All rights reserved.
Trang 4Introduction
Trang 5© 2012 Renesas Electronics America Inc All rights reserved.
5
Class Objectives
Understand what services a real-time kernel provides
Learn how to make use of kernel services
Learn how kernels are implemented
Gain experience with an actual kernel
5
Trang 6 Based on µC/OS-III
Real-time kernel from Micriµm
Concepts underlying the labs are not µC/OS-III-specific
Step-by-step instructions are provided for each lab
Trang 7© 2012 Renesas Electronics America Inc All rights reserved.
Micriµm’s Modules (Hardware-Specific Code)
Hardware
Trang 8µC/LIB µC/CPU
Trang 9© 2012 Renesas Electronics America Inc All rights reserved.
9
Directory Structure
9 Workspace files
Trang 11© 2012 Renesas Electronics America Inc All rights reserved.
11
Lab 1
11
Trang 12Lab 1 Summary
The kernel is built alongside application code
A kernel-based application looks much like any other C
program
Application code interacts with the kernel through API
functions
Trang 13© 2012 Renesas Electronics America Inc All rights reserved.
13
Foreground/Background
Systems
13
Trang 15© 2012 Renesas Electronics America Inc All rights reserved.
15
Foreground/Background Benefits
No upfront cost
Minimal training required
Developers don’t need to learn a kernel’s API
No need to set aside memory resources to accommodate a kernel
There is a small amount of overhead associated with a kernel
15
Trang 16Foreground/Background Drawbacks
Difficult to ensure that each operation will meet its deadline
All code in the background essentially has the same importance,
Potential to delay entire application
Trang 17© 2012 Renesas Electronics America Inc All rights reserved.
Trang 18Foreground/Background Drawbacks
(Cont.)
Problems with multiple developers
Developers’ efforts must be closely coordinated
Difficult expansion, even with one developer
Changes to one portion of the application may negatively impact the remainder of the code
Trang 19© 2012 Renesas Electronics America Inc All rights reserved.
19
Kernel-Based Applications
19
Trang 21© 2012 Renesas Electronics America Inc All rights reserved.
21
Kernel Basics
Application is divided into tasks
Kernel shares CPU amongst tasks
Developer may assign importance, or priority, to each task
21
Trang 23© 2012 Renesas Electronics America Inc All rights reserved.
23
Initiating Multitasking
23
Trang 24Initializing and Starting the Kernel
Application code must initialize the kernel
µC/OS-III is typically initialized in main()
Initialization accomplished through kernel API functions
Trang 25© 2012 Renesas Electronics America Inc All rights reserved.
25
OSInit()
Must be invoked before any kernel services are used
Initializes data structures
Creates internal tasks
Number of tasks depends on configuration
25
Trang 26µC/OS-III Internal Tasks
ISR Handler Task
Facilitates deferred interrupt scheme
Timer Task
Manages software timers
Trang 27© 2012 Renesas Electronics America Inc All rights reserved.
27
Creating a Task
void OSTaskCreate (OS_TCB *p_tcb,
CPU_CHAR *p_name, OS_TASK_PTR p_task, void *p_arg, OS_PRIO prio, CPU_STK *p_stk_base, CPU_STK *p_stk_limit, OS_STK_SIZE stk_size,
OS_MSG_QTY q_size, OS_TICK time_quanta, void *p_ext,
OS_OPT opt, OS_ERR *p_err);
27
The task itself
The task’s
the task’s stack
Trang 28A Task Control Block (TCB)
Contains information on the task’s status
23 - 51 fields
StkPtr ExtPtr StkLimitPtr NextPtr PrevPtr
Trang 29© 2012 Renesas Electronics America Inc All rights reserved.
29
Stacks
Each task has a stack
Context is stored on stacks
Stack growth conventions vary
across platforms
29
PSW (0x00010000)
PC (p_task) R15 (0x15151515) R14 (0x14141414) R13 (0x13131313) R12 (0x12121212) R11 (0x11111111) R10 (0x10101010) R9 (0x09090909) R8 (0x08080808) R7 (0x07070707) R6 (0x06060606) R5 (0x05050505) R4 (0x04040404) R3 (0x03030303) R2 (0x02020202) R1 (p_arg) p_stk
Higher memory addresses
Lower memory addresses
Trang 30 Runs highest priority task
Initializes CPU registers
Should be the last function called from main()
Trang 31© 2012 Renesas Electronics America Inc All rights reserved.
31
Lab 2
31
Trang 32Lab 2 Summary
Application code creates tasks by calling kernel API functions
Each task has its own stack
A priority must be assigned to each task
Trang 33© 2012 Renesas Electronics America Inc All rights reserved.
33
Scheduling and Context
Switches
33
Trang 34Two Types of Multitasking
Scheduling differs from kernel to kernel
There are two common approaches to scheduling multiple tasks
Cooperative scheduling
Preemptive scheduling
Trang 35© 2012 Renesas Electronics America Inc All rights reserved.
35
Cooperative Scheduling
35
Task B Task A ISR
Time
Interrupt signals the availability of Task A’s data
Task A cannot run until Task B
completes
Trang 36The high-priority task is scheduled
by the kernel
Trang 37© 2012 Renesas Electronics America Inc All rights reserved.
37
Round-Robin Scheduling
37
Task C Task B Task A
Time
Time Quantum
Trang 38Scheduling in µC/OS-III
µC/OS-III is preemptive
Finds highest-priority, ready task
Scheduling can be optimized
Assembly language, tables
Round-robin scheduling is performed only when enabled
Trang 39© 2012 Renesas Electronics America Inc All rights reserved.
PSW PC
PSW
R15 R14 R13 R12 R11 R10 R9 R8 R7 R6 R5 R4 R3 R2 R1
Save stack pointer
Update kernel variables
Load new stack pointer
Pop registers from stack
OSTCBCurPtr->StkPtr OSTCBCurPtr OSPrioCur
Trang 40 In a preemptive kernel, interrupt handlers are capable of
triggering context switches
Restore CPU registers;
Return from interrupt;
void AppISR (void) {
Trang 41© 2012 Renesas Electronics America Inc All rights reserved.
41
The Tick Interrupt
Most kernels keep track of time via a periodic interrupt,
often called a “tick”
A kernel can use its tick interrupt to implement a number of useful features:
Time delays
Software timers
Timeouts for blocking API functions
41
Trang 42Time Delays
void OSTimeDly (OS_TICK dly,
OS_OPT opt, OS_ERR *p_err);
void OSTimeDlyHMSM (CPU_INT16U hours,
CPU_INT16U minutes, CPU_INT16U seconds, CPU_INT32U milli, OS_OPT opt, OS_ERR *p_err);
Trang 43© 2012 Renesas Electronics America Inc All rights reserved.
43
Lab 3
43
Trang 44Lab 3 Summary
Most µC/OS-III ISRs are written at least partially in
assembly language
ISRs must perform a few kernel-specific operations
An interrupt can be set up with a fairly small amount of code
Trang 45© 2012 Renesas Electronics America Inc All rights reserved.
45
Additional Kernel
Services
45
Trang 46Beyond Task Management
A kernel does more than just switch between tasks
Synchronization
Inter-task communication
Resource protection
Trang 47© 2012 Renesas Electronics America Inc All rights reserved.
47
Synchronization
Can be thought of as signaling
Tasks can be signaled by ISRs or other tasks
While one tasks waits for a signal, the kernel runs other
tasks
47
Trang 48 A means of synchronization
Based on a counter
Counter value indicates whether or not an event has occurred
Two basic operations
Pend: wait for event
Post: signal occurrence of event
Trang 49© 2012 Renesas Electronics America Inc All rights reserved.
49
Semaphore API
void OSSemCreate (OS_SEM *p_sem,
CPU_CHAR *p_name, OS_SEM_CTR cnt, OS_ERR *p_err);
OS_SEM_CTR OSSemPend (OS_SEM *p_sem,
OS_TICK timeout, OS_OPT opt,
CPU_TS *p_ts, OS_ERR *p_err);
OS_SEM_CTR OSSemPost (OS_SEM *p_sem,
OS_OPT opt, OS_ERR *p_err);
49
Trang 50Calculate statistics;
OSSemPost((OS_SEM *)&AppSemDisp,
(OS_OPT )OS_OPT_POST_1, (OS_ERR *)&err);
Delay for 5 ms;
} }
Trang 51© 2012 Renesas Electronics America Inc All rights reserved.
OS_SEM
Type NamePtr PendList Ctr
TS
Trang 52Event Flags
Another means of synchronization
Each event represented by a bit
Pend and post operations
Pend for multiple events
Trang 53© 2012 Renesas Electronics America Inc All rights reserved.
53
Event Flag API
void OSFlagCreate (OS_FLAG_GRP *p_grp,
CPU_CHAR *p_name, OS_FLAGS flags, OS_ERR *p_err);
OS_FLAGS OSFlagPend (OS_FLAG_GRP *p_grp,
OS_FLAGS flags, OS_TICK timeout, OS_OPT opt,
CPU_TS *p_ts, OS_ERR *p_err);
OS_FLAGS OSFlagPost (OS_FLAG_GRP *p_grp,
OS_FLAGS flags, OS_OPT opt, OS_ERR *p_err);
53
Trang 54Shared Resources
Peripheral devices, buffer pools, or simple global variables
Accessed by more than one task or by at least one task and one ISR
Can cause race conditions
Trang 55© 2012 Renesas Electronics America Inc All rights reserved.
55
Shared Resource Example
void AppTaskUART (void *p_arg)
void AppTaskFS (void *p_arg) {
Perform initializations; while (1) {
Trang 56Protecting Shared Resources
Disabling and enabling interrupts
Locking and unlocking the scheduler
Trang 57© 2012 Renesas Electronics America Inc All rights reserved.
57
Mutexes
Implemented much like semaphores
Manipulated through pend and post functions
Offer protection against priority inversion
57
Trang 58Mutex API
void OSMutexCreate (OS_MUTEX *p_mutex,
CPU_CHAR *p_name, OS_ERR *p_err);
void OSMutexPend (OS_MUTEX *p_mutex,
OS_TICK timeout, OS_OPT opt,
CPU_TS *p_ts, OS_ERR *p_err);
void OSMutexPost (OS_MUTEX *p_mutex,
OS_OPT opt,
Trang 59© 2012 Renesas Electronics America Inc All rights reserved.
(OS_ERR *)&err);
Write pressure to SD card;
OSMutexPost((OS_MUTEX *)&AppMutexSD,
(OS_OPT )OS_OPT_POST_NONE, (OS_ERR *)&err);
(OS_ERR *)&err);
Write errors to SD card;
OSMutexPost((OS_MUTEX *)&AppMutexSD,
(OS_OPT )OS_OPT_POST_NONE, (OS_ERR *)&err);
}
}
Trang 60Inter-Task Communication
Sending and receiving messages
Tasks can send or receive
ISRs can send
Messages stored in a queue managed by the kernel
While one task waits for a message, other tasks run
Trang 61© 2012 Renesas Electronics America Inc All rights reserved.
61
Message Queue API
void OSQCreate (OS_Q *p_q,
CPU_CHAR *p_name, OS_MSG_QTY max_qty, OS_ERR *p_err);
void *OSQPend (OS_Q *p_q,
OS_TICK timeout, OS_OPT opt, OS_MSG_SIZE *p_msg_size, CPU_TS *p_ts,
OS_ERR *p_err);
void OSQPost (OS_Q *p_q,
void *p_void, OS_MSG_SIZE msg_size, OS_OPT opt,
OS_ERR *p_err);
61
Trang 62Message Queue Example
ADC Task ISR
Read new value;
Clear ADC interrupt;
OSQPost((OS_Q *)&AppQADC,
(void *)adc_val, (OS_MSG_SIZE)msg_size, (OS_OPT )OS_OPT_POST_FIFO,
Trang 63© 2012 Renesas Electronics America Inc All rights reserved.
63
Task Message Queue
Message queue included in TCB
Less overhead than standard message queue
Can be used whenever only one task will be receiving
messages
63
Trang 64Additional Services
Multi-pend
Pend on multiple queues and semaphores
Dynamic memory allocation
Timers
One-shot and periodic software timers with callbacks
Trang 65© 2012 Renesas Electronics America Inc All rights reserved.
65
Lab 4
65
Trang 66Lab 4 Summary
In general, it is desirable to keep interrupt handlers as short
as possible
Using semaphores, interrupt handlers can signal tasks
The two primary semaphore operations are pend and post
Trang 67© 2012 Renesas Electronics America Inc All rights reserved.
67
Conclusion
67
Trang 68 Today we discussed…
The differences between foreground/background systems and kernel-based applications
How a kernel is initialized
The contents of a task
How a kernel schedules tasks
Trang 69© 2012 Renesas Electronics America Inc All rights reserved.
69
Summary (Cont.)
Today we discussed…
What happens during a context switch
The structure of ISRs in kernel-based applications
Synchronization, mutual exclusion, and inter-task communication services
69
Trang 70Questions?
Trang 71Renesas Electronics America Inc.
© 2012 Renesas Electronics America Inc All rights reserved.