0% found this document useful (0 votes)
142 views

Real Time Operating Systems - MicroC - OS-II

This document provides information about real-time operating systems and MicroC/OS-II (μC/OS-II or MUCOS). It discusses the basic functions of RTOS including scheduling, interrupts, tasks, timers, memory allocation, and inter-process communication mechanisms. It also summarizes the key characteristics, applications, and features of μC/OS-II such as multitasking, portability, scalability, and support for preemptive scheduling and different processor architectures. The document outlines the basic system-level and task-level functions in μC/OS-II like OSInit(), OSStart(), and OSTickInit().
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Real Time Operating Systems - MicroC - OS-II

This document provides information about real-time operating systems and MicroC/OS-II (μC/OS-II or MUCOS). It discusses the basic functions of RTOS including scheduling, interrupts, tasks, timers, memory allocation, and inter-process communication mechanisms. It also summarizes the key characteristics, applications, and features of μC/OS-II such as multitasking, portability, scalability, and support for preemptive scheduling and different processor architectures. The document outlines the basic system-level and task-level functions in μC/OS-II like OSInit(), OSStart(), and OSTickInit().
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 84

DEPARTMENT OF MECHATRONICS

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

● Design of complex real time multitasking embedded


system requires an RTOS and real time clock based
hardware.
● System consist of timers, scheduler, device driver,
device manager and additional functions
● Ex:TCP/IP or USB port, networking functions and
error and exception handling functions.
Real time operating systems: MicroC / OS-II
Basic functions in the RTOS
● Basic kernel function ,scheduling and interrupt service
mechanisms
● Support to number of processor architectures and
multiprocessor environment and reusable module for
different functions
● Preemitive scheduling with or without option of scheduling
in equal time slices.
Basic functions in the RTOS
● Allocation of priority to tasks and interrupt service threads.
● Features of priority inheritance or priority inheritance plus
option of priority ceiling
● Predictable scheduling of tasks and interrupts,predictable
interrupt dispatch latencies
● use of limited number of tasks such that latencies are
small
Basic function in the RTOS
● Function for task scheduling, IPC(signals, semaphores,
queues mailboxes,pipes,sockets),event handling
● Support to clock, timer function,memory
allocation,deallocation,shared memory systems.
● RTOS scalability and support to small memory footprint of
RTOS
● support to execute -in-place codes,which means application
codes can directly run from flash or ROM
Basic function in the RTOS
● Support to user task option to run on kernel mode
● support to fixed memory blocks and dynamic allocation of
blocks by user tasks.
● Support to device imaging tools and device drivers
● Support to file systems,flash systems,TCP/IP suite of
protocol, network and bus protocol
● Support to IDE
● portable software on
silicon(pSOS)
● Quantum software
systems(QNX)
● Embedded configurable
OS(eCOS)
● Windows Embedded
compact(Win CE)
RTOS deploy the following approaches for system
development
1.Host and target based development approach
2.self host based development approach
Host and Target development approach:
Real time or non real time application is the host target approach
●Host machine eg: PC use general purpose OS(Windows or UNIX)
●Target connect by network through USB or TCP/IP during
development phase.
●codes developed, RTOS function connect a target.
●codes download into the target, Target finally disconnects the
host eg VxWorks, PSoS
Self-Host based Development approach
● Same system with full RTOS is used for development on
which the application will be running
● Application codes are ready
● Required RTOS functions and applications codes are
downloaded into ROM of the target board
Eg: MUCOS
1. In house developed RTOSs

●small level application


●codes written for specific needs and specific application
or product
●codes are customised in-house for the design needs
2.Broad based commercial RTOSs

Help in building the product fast


-Readily available broad based commercial RTOS
packages offers following advantages
●Provides thoroughly tested and debugged RTOS
function
● provides several development tools-source code
engineering,testing ,simulation and debugging tools
2.Broad based commercial RTOSs

● support many processor architectures,


Eg:ARM,x86,MIPS
● Support GUI
Support many devices,graphics,network-connectivity
protocols and file systems
Saves large amount of development
time ,maintenance costs
3.General purposes OS with additional RTOS functions

● Embedded Linux or Windows XP is general purpose


OS.
● Not componentized.
● Footprint (the code that goes as ROM image) is not
reducible.
● The tasks are not assignable priorities.
3.General purposes OS with additional RTOS functions

● However, they offer powerful GUIs, rich multimedia


interfaces and have low cost.
● The general purpose OS can be used in combination
with the RTOS functions.
● eg: RTLinux is a real time kernels over the Linux
kernel.
● Windows XP Embedded for 80x86 architecture
4.Special focus RTOSs

● Used with specific processors like ARM or 8051 or


DSP,
● OSEK for automotives or
● Symbian OS for Mobile phones
µc/OS – II(MUCOS)

● 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

3. For multitasking, it employs a preemptive scheduler


4. MUCOS has system level functions-for initialization and start of
RTOS
Critical section:MUCOS has interrupts disabling and enabling
functions that execute at entering and exiting the section
Features of mucos

5. It has task service functions


Task creating, running, suspending and resuming
6. MUCOS has task delay functions
7. MUCOS has memory allocation functions for creating and
partitioning into blocks, getting a block putting into the block and
querying during debugging at a block
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

µC/TCP-IP (an embedded TCP/IP stack),


µC/CAN (an embedded Controller Area Network bus)
µC/USB device and µC/USB host (an embedded USB
devices framework).
Source File:

● 10000 + lines of codes


● It has two types of source files
● – Processor dependent source file
● – Processor independent source file
Master header file includes
# includes Preprocessor commands for both types
"include.h" ---->file referred
Processor dependent source file

• Two header files at the master are the


following
– Os_cpu.h is the processor definitions header file
– Os_cfg.h is the kernel building configuration file
2 C files are for ISRs and RTOS timers.
os_tick.c
timer related codes file
os_cpu_c. c
processor C Codes
os_cpu_a.s12
-Assembly code for task switching function
An example of assembly codes file for 68HC12 MC
os_cpu_a.s51--->8051
Processor independent source file

two files, MUCOS header and C files are ucos.ii.c, ucos.ii.h


●Files for core, timer and task files are
●os_core.c, os_time.c,,os_task.c
●os_mem.c --->for memory functions
●os_sem.c,os_mbox.c,os_q.c
●for semaphores, mailbox and queues
Feature of MUCOS

MUCOS Naming Basics


●OS or OS_ prefix denotes that the function or variable is a
MUCOS operating system function or variable
●For examples, OSTaskCreate ( ) ─ a MUCOS function that
creates a task,
●Macros:OS_NO_ERR
MUCOS macro that returns true in case no error is reported
from a OS function
Feature 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

• Queue permit array of message-pointers


µC/OS -II-System level and task Ievel Functions

System level Functions:


OSInit ( )- void OSInit (void) At the beginning prior to the
OSStart ( )
●Function void OSInit (void) to initiate the operating system
●Use is compulsory before calling any OS kernel functions
●Use start function for starting the MUCOS multitasking
function.
µC/OS -II-System level and task Ievel Functions

● Start function is used after the creation of at least one task


● Start_Task or First _task
● MUCOS RTOS has system function that should be
executed when entering and exiting the ISR.
µC/OS -II-System level and task Ievel Functions

● OSStart ( ) and OSTickInit


● void OSStart (void)
After OSInit ( ) and task-creating function(s)
● void OSTickInit (void)
In first task function that executes once.
● Initializes the system timer ticks (RTC interrupts)
µC/OS -II-System level and task Ievel Functions

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

System Clock Tick Initiate


Function void OSTickInit (void)
─ used to initiate the system clock ticks and interrupts at
regular intervals as per OS_TICKS_PER_SEC predefined
when defining configuration of MUCOS
µC/OS -II-System level and task Ievel Functions

Interrupt Service Task (ISR) Start and End


OSIntEnter ( ) and OSIntExit ( )
void OSIntEnter (void)
Just after start of the ISR codes OSIntExit must call just before
the return from ISR
• void OSIntExit (void)
After the OSIntEnter ( ) is called just after the start of the ISR
codes and OSIntExit is called just before the return from ISR.
µ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

1.Initiating the OS before starting the use of the RTOS


function.
2.Starting the use of RTOS multitasking function and running
the tasks.
3.Starting the use of RTOS system clock.
4.Sending message to RTOS kernel for taking control at the
start of an ISR.
Programming Examples─ OS Init and OS Start
Step i: Initiating the RTOS
void main (void) {
OSInit ();
/* Create a task */
.
Step j: Starting the RTOS
void main (void) {
OSInit ();
/* Create a task */
.
/*Start the RTOS */
OSStart (
Task Service Functions
Service functions mean the functions of multitasking service
(task create, suspend or resume), time setting and time
retrieving (getting) functions.
OSTaskCreate
unsigned byte OSTaskCreate (void (*task) (void *taskPointer),
void *pmdata,
OS_STK taskStackPointer, unsigned byte taskPriority) -----
>Called for creating a task*taskPointer
─ a pointer to the codes of the task being created
Task Service Functions
• *pmdata─ pointer
for an optional message data reference passed to
the task. If none, assign as NULL

OSTaskSuspend and OSTaskResume
unsigned byte OSTaskSuspend
(unsigned byte taskPriority)
Called for blocking a task
• unsigned byte OSTaskResume
(unsigned byte task Priority)
Called for resuming a blocked task
Programming Method and Example

Preprocessor commands, main and


Task Creation
Method: RTOS after start first runs FirstTask, and then
FirstTask creates all the application tasks and initiates system
clock ticks
• later suspend itself in infinite while loop
Programming Method and Example
Step A: Program preprocessor commands
#define OS_MAX_TASKS 8
#define OS_LOWESTPRIO 23
#define OS_TASK_CREATE_EN 1
#define OS_TASK_DEL_EN 1
#define OS_TASK_SUSPEND_EN 1
#define OS_TASK_RESUME_EN 1
#define OS_TICS_PER_SEC 100(Let no. of ticks be 100 per sec. system
clock will interrupt and thus tick every 10ms to update the set counts and
to transfer control to MUCOS)
Programming Method and Example
Step B: Global functions and their parameters declarations
#define FirstTaskPriority 8
#define FirstTaskStackSize 100
/* Define other task-priorities & stacksizes*/
static void FirstTask(*taskPointer);
static OS_STK FirstTask [FirstTask_StackSize]
#define task1Priority 9
#define task1StackSize 100
/* Define other task-priorities & stacksizes*/
static void task1(*taskPointer);
static OS_STK task1 [task1StackSize]
Programming Method and Example

#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

• OSTimeDlyHMSM (0, 0, 0, 1000)


Global Variables
─ # define volatile boolean IntrEnable
• Constants ─ # define false 0
• Strings ─ # define welcomemsg
"Welcome To ABC Telecom"
Preprocessor Macros

Macro - A named collection of codes that is defined in a


program as preprocessor directive.

• Differs from a function in the sense that once a macro is


defined by a name, the compiler puts the corresponding
codes at the macro at every place where that macro-name
appears
Memory Allocation Related Functions

•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

void *OSMemGet (OS_MEM *memCBPointer, unsigned byte


*memErr) ─ To find pointer of the memory control block
allocated to the memory-blocks
OSMemQuery (* memCBPointer, *memData)
unsigned byte OSMemQuery (OS_MEM * memCBPointer,
OS_MEM_DATA *memData) To find pointer of memory
control block and OS_MemData data-structure
2. Macros for Memory Functions

Macros to find status after execution of OS Memory


Functions

• OS_NO_ERR returns true when creation succeeds.

OS_MEM_INVALID_BLKS returns true , when at least two


blocks are not passed as arguments.
• OS_MEM_INVALID_PART returns true, when memory for
partition is not available.

• OS_MEM_INVALID_SIZE returns true, when block size is


smaller than a pointer variable.
Semaphore Functions

Provides for using same semaphore functions as an event


signaling flag or mutex or counting semaphore
Semaphore for event flag functions
When a semaphore created is used as a an event signaling flag
or as counting semaphore,
semaphore value at start = 0,
which means event yet to occur and 1 will mean event occurred.
1.Create a semaphore for an IPC
2.check for availability of an IPC after a semaphore
release
3.waiting for an IPC for an semaphore release
4.senting an IPC after a semephore release
5.Retrieve the error information for a semephore
Mailbox Functions

● Used to communicate a pointer for information.


● µC/OS-II permits one message-pointer per mailbox.
● At the pointer, there can be a string or data structure of no
size limit.
● Assume an event pointer to the mailbox = *mboxMsg,
Mailbox Functions

● Pointer to the message, *MsgPointer (for retrieving the


message itself).
OSMboxCreate (*mboxMsg )
OS_Event *OSMboxCreate (void *mboxMsg) ─To create a
mailbox message pointer ECB of a mailbox message.
Mailbox Functions

1.Create a mailbox for an IPC


2.check for availability of an IPC after a message at a mailbox
3.waiting for availability of an IPC for an msg mailbox
4.sent a message for an IPC through mailbox
5.Finding mailbox data and retriving the error information for
a mailbox
Queue 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.

You might also like