Real Time Operating Systems - MicroC - OS-II
Real Time Operating Systems - MicroC - OS-II
ENGINEERING
18MT502-EMBEDDED SYSTEMS FOR MECHATRONICS
MODULE -3
Topic: Real time operating systems: MicroC / OS-II
Module 3
BSP - Board
Support package
Real time operating systems: MicroC / OS-II
● Use of µc/OS – II
● It is used for non commercial use, it is a freeware.
● Name is derived from Microcontroller Operating System
● Also known as MUCOS, UCOS, ..
Platforms:ARM Cortex-M3, Cortex-M4F, ARM7TDMI; Atmel
AVR
Characteristics
• Multitasking
• Deterministic
• Portable as ROM image
• Scalable
• Preemptive RTOS
• Different Platforms support
Applications
● Automotive
Source code has been certified
● Avionics
by department of Defence,USA
● Consumer electronics
for use in Avionics and Medical
● Medical devices
application
● Military
● Aerospace
● Networking
● Systems-on-a-chip.
Features of mucos
1. OS or OS_ when used as a prefix denotes that the function or variable
in mucos operating system
• EG: OSTaskCreate() creates a task, OS_NO_ERR,OS_MAX_TASKS
2. It is scalable OS.OS function that are necessary become part of
application is used
The functions needed for task servicing, inter process communications etc
must be predefined in a configuration file.file is included withuser codes
Features of mucos
8. It has IPC
9. Semaphore functions which are usable like the event flag,
resource acquiring keys or counting semaphores
10. Has mailbox functions, queue functions etc
Features of mucos
MUCOS real time kernel additional support for the following:
µC/BuildingBlocks [an embedded system building blocks
(software components) for hardware peripherals, for example
clock ( µC/Clk) and LCD ( µC/LCD)]
µC/FL (an embedded flash memory loader)
µC/FS (an embedded memory file system)
µC/GUI (an embedded GUI platform),
µC/Probe (a real time monitoring tool)
Features of mucos
OS_MAX_TASKS
─ user definable constant for specifying maximum number of
tasks in user application
MUCOS Basic Functions
• System Level
– OS initiate, start, system timer set, ISR enter and exit
• Task Service Functions
– create, run, suspend, resume
• Task delay
• Memory allocation
MUCOS Basic Functions
IPCs – Semaphore, Queue and Mailbox
• Same Semaphore function usable as event flag, resource
key and counting semaphore
• Mailbox one message pointer per mailbox
OSStart ( )
Function void OSStart (void)
●to start the initiated operating system and created tasks
●Its use is compulsory for the multitasking OS kernel
operations
µC/OS -II-System level and task Ievel Functions
OSIntEnter ( )
●Function void OSIntEnter (void)
● used at the start of ISR
●For sending a message to RTOS kernel for taking control
● compulsory to let OS kernel control the nesting of the
ISRs in case of occurrences of multiple interrupts of varying
priorities
µC/OS -II-System level and task Ievel Functions
OSIntExit ( )
●Function void OSIntExit (void)
●used just before the return from the running ISR
● For sending a message to RTOS kernel for quitting
control of presently running ISR
Critical Section Start and End
OS_ENTER_CRITICAL
─ Macro to disable interrupts before a critical section
OS_EXIT_CRITICAL
─ Macro to enable interrupts. [ENTER and EXIT functions
form a pair in the critical section]
Critical Section Start and End
OS_ENTER_CRITICAL
─ used at the start of a ISR or task
- for sending a message to RTOS kernel and disabling the
interrupts
─ use compulsory when the OS kernel is to take note of and
disable the interrupts of the system
Critical Section Start and End
• OS_EXIT_CRITICAL
used at the end of critical section
for sending a message to RTOS kernel and enabling the
interrupts
Use is compulsory to OS kernel for taking note of and enable
the disabled interrupts.
Critical Section Start and End
OSSchedLock()
to lock scheduling of another task at the beginning of critical
section
OSSchedUnlock()
Unlock scheduling of task.
Lock and unlock function form a pair in critical section.
System level function
#define task2Priority 10
#define task2StackSize 100
/* Define other task-priorities & stacksizes*/
static void task2(*taskPointer);
static OS_STK task2 [task2StackSize]
Programming Method and Example
Step C: Main function
void main (void) {
OSInit ();
/* Create First task */
OSTaskCreate (FirstTask, void (*) 0, (void
*)&FirstTaskStack[ FirstTaskStackSize],
FirstTaskPriority);
OSStart ( );
}
Programming Method and Example
Step D: First task for starting system clock and creating
application tasks
static void FirstTask (void *taskPointer) {
/*System clock time set */
OSTaskCreate (task1, void (*) 0,(void *)&task1Stack
[task1StackSize], task1Priority);
OSTaskCreate (task2, void (*) 0,(void*)&task2Stack
[task2StackSize], task2Priority);
/* Create All application related remaining tasks*/
Programming Method and Example
Step D: First task for setting and starting system clock and
creating application Tasks
OSTimeSet (presetTime);
OSTickInit (); /* Initiate system timer ticking*/
/* Create application related highest priority
tasks */
...; ...; ..;
while (1) {...;
System Time and Time Delay Functions
1. Delaying by defining a time delay by number of clock ticks
2. Resuming a delayed task by OSTimeDly.
3. Delaying by defining a time delay in units of hours,
minutes, seconds and milliseconds.
System Time and Time Delay Functions
OSTimeSet (counts)
void OSTimeSet (unsigned int counts)
Used when system time is to be set by counts
OSTimeGet( ) and OSTimeDly(delayCount)
unsigned int OSTimeGet (void)
To find present counts when system time is read.
void OSTimeDly (unsigned short delayCount)
To delay a task by period of count-inputs equal to delayCount -
1
OSTimeDlyHMSM(hr, mn, sec, ms)
void OSTimeDlyHMSM (unsigned byte hr, unsigned byte mn,
unsigned byte sec,unsigned short ms)
When need is to delay and block a task for hr hours, mn
minutes, sec seconds and ms milliseconds
•OSMem
*OSMemCreate (void *memAddr, MEMTYPE numBlocks,
MEMTYPE blockSize, unsigned byte *memErr) to create and
initialise a memory partition
OSMemPut (*memCBPointer, *memErr )
unsigned byte OSMemPut (OS_MEM * memCBPointer, void
*memBlock) To return a pointer of memory block in memory
partitions from the memory control-block pointer
Memory Allocation Related Functions
The message pointers post into a queue by the tasks either at the back
as in a queue or at the front as in a stack.
• A task can thus insert a given message for deleting either in the first
in first out (FIFO) mode or in priority mode for priority message.
Assume pointer, **Qtop, to a queue of pointers for the messages and
Assume two pointers, *QfrontPointer and *QbackPointer to insert
(post) and delete (retrieve), respectively, the pointer of the message.
Queue Functions
1.Create a queue for an IPC
2.Emptying the queue and Eliminating all the message
pointers
3.waiting and reading a message at a queue
4.senting(inserting) a message pointer to the queue
5.Sending a message pointer and inserting it at the queue front
6.Queuing to Find the message and error information for the
queue ECB
OSQCreate (**QTop, qSize )
OS_Event OSQCreate (void **QTop, unsigned byte qSize)
/*OS creates a Queue ECB. This creates and initialize an array of
pointers for the queue at QTop*/
Queue can be of maximum size = qSize.
• QTop points to top (0th element of an array).
ECB points at the QMsgPointer.
OSQPost (*QMsgPointer, *QMsg)
unsigned byte OSQPost (OS_EVENT*QMsgPointer, void *QMsg)
─Sends a pointer of the message QMsg to the QMsgPointer at the queue
back. The message inserts at the queue back pointer in the ECB
OSQPostFront (*QMsgPointer, *QMsg)
• unsigned byte OSQPostFront (OS_EVENT *QMsgPointer, void * QMsg)
─Sends QMsg to the QMsgPointer at the queue.
It points to the queue front pointer in the ECB where pointer for QMsg now
stores pushing other message-pointers back.
OSQPend (*QMsgPointer , timeOut, *Qerr )
void *OSQPend (OS_Event *QMsgPointer, unsigned short timeOut,
unsigned byte *Qerr)
─ The message pointer points to the queue front (head) at the ECB for the
queue defined by QMsgPointer.
It suspends the task *QMsgPointer is Null (until either message inserts or
timeout ticks of RTOS timer).
OSQFlush(*QMsgPointer )
unsigned byte *OSQFlush (OS_EVENT *QMsgPointer)
─ To eliminate all the messages in the queue that have been
sent. This function checks if a queue has a message pending
at QMsgPointer.