Testing [Andrew L] mbos RTOS for mbed Simply by copying code for main.cpp from mbos.h-comments

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

mbos Class Reference

mbos Class Reference

A pre-emptive, mutlitasking real-time operating system for mbed applications. More...

#include <mbos.h>

Public Member Functions

 mbos (uint ntasks, uint ntimers=0, uint nresources=0)
 Create an mbos object.
void Start (uint idlestacksize=32)
 Start mbos.
void CreateTask (uint taskid, uint priority, uint stacksz, void(*fun)(void))
 Create an mbos task.
uint GetTask (void)
 Get the ID of the current task.
void SetPriority (uint priority)
 Set the priority of the current task.
uint GetPriority (void)
 Get the priority of the current task.
void WaitEvent (uint event)
 Wait for an event or events.
void SetEvent (uint event, uint task)
 Post an event or events to a task.
uint GetEvent (void)
 Returns the event flag(s) which last caused the task to unblock.
void CreateTimer (uint timerid, uint taskid, uint event)
 Create a mbos timer.
void SetTimer (uint timerid, uint time, uint reload=0)
 Starts an mbos timer.
void RedirectTimer (uint timerid, uint taskid, uint event)
 Redirects an mbos timer.
void ClearTimer (uint timerid)
 Stops and clears an mbos timer.
void CreateResource (uint resourceid, uint priority)
 Creates an mbos resource.
uint LockResource (uint resourceid)
 Locks an mbos resource and temporarily allocates the resource's priority to the calling task.
uint TestResource (uint resourceid)
 Tests whether a resource is locked or free, without changing its state.
uint FreeResource (uint resource)
 Frees a resource Frees an mbos resource and restores the calling task's original priority.

Detailed Description

A pre-emptive, mutlitasking real-time operating system for mbed applications.

Allows the user to write their application in the form of independant tasks. The operating system manages a completely separate processor context for each task (processor registers and stack) and swaps between the tasks according to a simple scheduling algorithm.

Tasks are allocated a priority, and the scheduler ensures that the task that with the highest priority that is not waiting for an event, has control of the processor. If more than one task shares the highest priority, they will be run sequentially in round-robin fashion.

Each task may wait for one or more of up to 32 different events, each represented by one bit in the event flags word. Events may be posted by another task, by a mbos timer, or in response to some asynchronous event such as an peripheral interrupt.

mbos implements the SysTick timer to generate a System Tick every millisecond. Scheduling occurs once every Tick, or whenever a task blocks or an event is posted. The user may create one-shot or continuous timers to post events to tasks.

mbos implements a simple ceiling protocol resource locking mechanism To manage access to system resources. Tasks may take exclusive ownership of resources for a period.

A typical simple example with two tasks, and one timer, might look like this:

 // mbos Blinky demonstration.
 // Task 1 toggles LED1 every second, under control of a timer. It then posts an event to
 // task 2 which flashed LED2 briefly.
 #include "mbed.h"                    
 #include "mbos.h"

 #define TASK1_ID                1       // Id for task 1 (idle task is 0)
 #define TASK1_PRIO              50      // priority for task 1
 #define TASK1_STACK_SZ          32      // stack size for task 1 in words 
 #define TASK2_ID                2       // Id for task 2 
 #define TASK2_PRIO              60      // priority for task 2
 #define TASK2_STACK_SZ          32      // stack size for task 2 in words 
 #define TIMER0_ID               0       // Id for timer 0
 #define TIMER0_PERIOD           1000    // Time period in milliseconds
 #define TIMER0_EVENT            1       // Event flag (1 << 0)
 #define T1_TO_T2_EVENT          2       // Event flag (1 << 1)

 void task1(void);                       // task function prototypes
 void task2(void);

 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 mbos os(2, 1);                          // Instantiate mbos with 2 tasks & 1 timer    

 int main(void)
 {
     // Configure tasks and timers
     os.CreateTask(TASK1_ID, TASK1_PRIO, TASK1_STACK_SZ, task1);
     os.CreateTask(TASK2_ID, TASK2_PRIO, TASK2_STACK_SZ, task2);
     os.CreateTimer(TIMER0_ID, TIMER0_EVENT, TASK1_ID);
     // Start mbos
     os.Start();
     // never  return!
 }

 void task1(void)
 {
     os.SetTimer(TIMER0_ID, TIMER0_PERIOD, TIMER0_PERIOD);
     while(1){
         os.WaitEvent(TIMER0_EVENT);
         led1 = !led1;
         os.SetEvent(T1_TO_T2_EVENT, TASK2_ID);
     }
 }

 void task2(void)
 {
     while(1){
         os.WaitEvent(T1_TO_T2_EVENT);
         led2 = 1;
         wait_ms(100);
         led2 = 0;
     }
 }

Definition at line 103 of file mbos.h.


Constructor & Destructor Documentation

mbos ( uint  ntasks,
uint  ntimers = 0,
uint  nresources = 0 
)

Create an mbos object.

Instantiate mbos and define the number of tasks, timers and resources.

Parameters:
ntasksThe number of user tasks (1 .. 99).
ntimersOptional number of timers (0 .. 100).
nresourcesOptional number of resources (0 .. 100).

Definition at line 73 of file mbos.cpp.


Member Function Documentation

void ClearTimer ( uint  timerid )

Stops and clears an mbos timer.

Parameters:
timeridThe ID of the timer to clear.

Definition at line 275 of file mbos.cpp.

void CreateResource ( uint  resourceid,
uint  priority 
)

Creates an mbos resource.

Allocates and initialises the data structures for the Resource.

Parameters:
resourceidUnique ID for the resource (0 .. nresource).
priorityPriority of the resource (normally > than that of any task using the resource).

Definition at line 286 of file mbos.cpp.

void CreateTask ( uint  taskid,
uint  priority,
uint  stacksz,
void(*)(void)  fun 
)

Create an mbos task.

Allocates and initialises data structures for the task.

Parameters:
taskidUnique ID for the task. (1 .. ntasks).
priorityPriority (0 .. 99) of the task. Tasks may share the same priority.
stacksizeSize in words (>= 32) of the task's stack.
funPointer to the task function. Function must be of type static void, and must return nothing.

Definition at line 106 of file mbos.cpp.

void CreateTimer ( uint  timerid,
uint  taskid,
uint  event 
)

Create a mbos timer.

Allocates and initialises the data structures for the timer.

Parameters:
timeridUnique ID for the timer (0 .. ntimers - 1).
taskidThe ID of the task to which the timer will post events. May not be the idle task.
eventThe event flag(s) that the timer should post on timeout. May not be NULL.

Definition at line 235 of file mbos.cpp.

uint FreeResource ( uint  resource )

Frees a resource Frees an mbos resource and restores the calling task's original priority.

Does nothing if the resource is already free, if called from the idle task, or the resource was locked by a different task.

Parameters:
resourceidThe ID of the resource t free.
Returns:
Zero if the resource was freed successfully, or the ID of the task that is locking the resource, if not.

Definition at line 318 of file mbos.cpp.

uint GetEvent ( void   )

Returns the event flag(s) which last caused the task to unblock.

Returns:
The event flags.

Definition at line 230 of file mbos.cpp.

uint GetPriority ( void   )

Get the priority of the current task.

Returns:
The priority (0 .. 99) of the calling task.

Definition at line 195 of file mbos.cpp.

uint GetTask ( void   )

Get the ID of the current task.

Returns:
The ID (0 .. 99) of the calling task.

Definition at line 183 of file mbos.cpp.

uint LockResource ( uint  resourceid )

Locks an mbos resource and temporarily allocates the resource's priority to the calling task.

Does nothing if the resource is already locked, or if called from the idle task.

Parameters:
resourceidThe ID of the resource to lock.
Returns:
Zero if the resource was locked successfully, or the ID of the task that is currently locking the resource, if not.

Definition at line 297 of file mbos.cpp.

void RedirectTimer ( uint  timerid,
uint  taskid,
uint  event 
)

Redirects an mbos timer.

Changes the task and event associated with a timer. If the timer is running, the fun ction has no effect.

Parameters:
timeridThe ID of the timer.
taskidThe ID of the task to which the timer will post events. May not be the idle task.
eventThe event flag(s) that the timer should post on timeout. May not be NULL.

Definition at line 260 of file mbos.cpp.

void SetEvent ( uint  event,
uint  task 
)

Post an event or events to a task.

Posts the nominated event(s) to the specified task. Forces a context switch if the events are posted successfully. Does nothing if the task is not waiting, or is not waiting for the posted event.

Parameters:
eventEvent flag(s) to post.
taskThe ID of the task to which to post the event(s). May not be idle task.

Definition at line 212 of file mbos.cpp.

void SetPriority ( uint  priority )

Set the priority of the current task.

Does nothing if called from the idle task.

Parameters:
priorityPriority (0 .. 99) to apply to the calling task.

Definition at line 188 of file mbos.cpp.

void SetTimer ( uint  timerid,
uint  time,
uint  reload = 0 
)

Starts an mbos timer.

If the reload time is zero or omitted, the timer will be reset once it has posted a single event, after time milliseconds. If the reload time is non-zero, this value will be loaded into the timer on time-out, and the timer will continue indefinitely. In this case the interval to the first event will be time, and the interval between subsequent events will be reloadtime.

Parameters:
timeridThe ID of the timer.
timeThe period in milliseconds before the timer times out.
reloadThe optional time to reload into the timer when it times out.

Definition at line 249 of file mbos.cpp.

void Start ( uint  idlestacksize = 32 )

Start mbos.

Optionally specify the size of the stack for a user-written idle task. All tasks, timers and resources must be created before calling Start.

Parameters:
idlestacksizeSize in words (>= 32) of the user-written idle task if present.
Returns:
Never returns

Definition at line 127 of file mbos.cpp.

uint TestResource ( uint  resourceid )

Tests whether a resource is locked or free, without changing its state.

Parameters:
resourceidThe ID of the resouce to test.
Returns:
Zero if the resource is free, or the ID of the task that is currently locking the resouce , if not.

Definition at line 311 of file mbos.cpp.

void WaitEvent ( uint  event )

Wait for an event or events.

Causes the current task to block, waiting for the specified event. Does nothing if called from the idle task, or if the event is NULL.

Parameters:
eventEvent flag(s) to wait for.

Definition at line 200 of file mbos.cpp.