µC/OS-II API Reference
This section provides a user’s guide to µC/OS-II services. Each of the user-accessible kernel services is presented in alphabetical order and the following information is provided for each of the services.
- A brief description
- The function prototype
- The filename of the source code
- The
#define
constant needed to enable the code for the service - A description of the arguments passed to the function
- A description of the return value(s)
- Specific notes and warnings on using the service
- One or two examples of how to use the function
OS_ENTER_CRITICAL()
File | Called From | Code Enabled By |
---|---|---|
OS_CPU.H | Task or ISR | N/A |
OS_ENTER_CRITICAL()
and OS_EXIT_CRITICAL()
are macros used to disable and enable, respectively, the processor’s interrupts.
Arguments
None
Returned Value
None
Notes/Warnings
These macros must be used in pairs.
If OS_CRITICAL_METHOD
is set to 3, your code is assumed to have allocated local storage for a variable of type OS_CPU_SR
called cpu_sr
as follows:
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr; #endif
Example
void TaskX(void *pdata) { #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif for (;;) { . . OS_ENTER_CRITICAL(); /* Disable interrupts */ . . /* Access critical code */ . OS_EXIT_CRITICAL(); /* Enable interrupts */ . . } }
OS_EXIT_CRITICAL()
File | Called From | Code Enabled By |
---|---|---|
OS_CPU.H | Task or ISR | N/A |
OS_ENTER_CRITICAL()
and OS_EXIT_CRITICAL()
are macros used to disable and enable, respectively, the processor’s interrupts.
Arguments
None
Returned Value
None
Notes/Warnings
These macros must be used in pairs.
If OS_CRITICAL_METHOD
is set to 3, your code is assumed to have allocated local storage for a variable of type OS_CPU_SR
called cpu_sr
as follows:
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr; #endif
Example
void TaskX(void *pdata) { #if OS_CRITICAL_METHOD == 3 OS_CPU_SR cpu_sr; #endif for (;;) { . . OS_ENTER_CRITICAL(); /* Disable interrupts */ . . /* Access critical code */ . OS_EXIT_CRITICAL(); /* Enable interrupts */ . . } }
OSFlagAccept()
OS_FLAGS OSFlagAccept(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_FLAG.C | Task | OS_FLAG_EN and OS_FLAG_ACCEPT_EN |
OSFlagAccept()
allows you to check the status of a combination of bits to be either set or cleared in an event flag group. Your application can check for ANY bit to be set/cleared or ALL bits to be set/cleared. This function behaves exactly as OSFlagPend()
except that the caller will NOT block if the desired event flags are not present.
Arguments
pgrp
is a pointer to the event flag group. This pointer is returned to your application when the event flag group is created (see OSFlagCreate()
).
flags
is a bit pattern indicating which bit(s) (i.e., flags
) you wish to check. The bits you want are specified by setting the corresponding bits in flags
. word
wait_type
specifies whether you want ALL bits to be set/cleared or ANY of the bits to be set/cleared. You can specify the following argument:
OS_FLAG_WAIT_CLR_ALL
You will check ALL bits in 'flags
' to be clear (0)
OS_FLAG_WAIT_CLR_ANY
You will check ANY bit in 'flags
' to be clear (0)
OS_FLAG_WAIT_SET_ALL
You will check ALL bits in 'flags
' to be set (1)
OS_FLAG_WAIT_SET_ANY
You will check ANY bit in 'flags
' to be set (1)
You can add OS_FLAG_CONSUME
if you want the event flag(s) to be ‘consumed’ by the call. For example, to wait for ANY flag in a group and then clear the flags
that are present, set wait_type
to:
OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
err
is a pointer to an error code and can be:
OS_NO_ERR
No error
OS_ERR_EVENT_TYPE
You are not pointing to an event flag group
OS_FLAG_ERR_WAIT_TYPE
You didn't specify a proper 'wait_type
' argument.
OS_FLAG_INVALID_pgrp
You passed a NULL pointer instead of the event flag handle.
OS_FLAG_ERR_NOT_RDY
The desired flags
you are waiting for are not available.
Returned Value
The state of the flags
in the event flag group.
Notes/Warnings
- The event flag group must be created before it is used.
- This function does NOT block if the desired
flags
are not present.
Example
#define ENGINE_OIL_PRES_OK 0x01 #define ENGINE_OIL_TEMP_OK 0x02 #define ENGINE_START 0x04 OS_FLAG_GRP *EngineStatus; void Task (void *pdata) { INT8U err; OS_FLAGS value; pdata = pdata; for (;;) { value = OSFlagAccept(EngineStatus, ENGINE_OIL_PRES_OK + ENGINE_OIL_TEMP_OK, OS_FLAG_WAIT_SET_ALL, &err); switch (err) { case OS_NO_ERR: /* Desired flags are available */ break; case OS_FLAG_ERR_NOT_RDY: /* The desired flags are NOT available */ break; } . . } }
OSFlagCreate()
OS_FLAG_GRP *OSFlagCreate (OS_FLAGS flags, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_FLAG.C | Task or startup code | OS_FLAG_EN |
OSFlagCreate()
is used to create and initialize an event flag group.
Arguments
flags
contains the initial value to store in the event flag group.
err
is a pointer to a variable which will be used to hold an error code. The error code can be one of the following:
OS_NO_ERR
if the call was successful and the event flag group was created.
OS_ERR_CREATE_ISR
if you attempted to create an event flag group from an ISR.
OS_FLAG_GRP_DEPLETED
if there are no more event flag groups available. You will need to increase the value of OS_MAX_FLAGS
in OS_CFG.H
.
Returned Value
A pointer to the event flag group if a free one is available. If no event flag group is available, OSFlagCreate()
will return a NULL pointer.
Notes/Warnings
Event flag groups must be created by this function before they can be used by the other services.
Example
OS_FLAG_GRP *EngineStatus; void main (void) { INT8U err; . . OSInit(); /* Initialize µC/OS-II */ . . /* Create a flag group containing the engine's status */ EngineStatus = OSFlagCreate(0x00, &err); . . OSStart(); /* Start Multitasking */ }
OSFlagDel()
OS_FLAG_GRP *OSFlagDel(OS_FLAG_GRP *pgrp, INT8U opt, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_FLAG.C | Task | OS_FLAG_EN and OS_FLAG_DEL_EN |
OSFlagDel()
is used to delete an event flag group. This is a dangerous function to use because multiple tasks could be relying on the presence of the event flag group. You should always use this function with great care. Generally speaking, before you would delete an event flag group, you would first delete all the tasks that access the event flag group.
Arguments
pgrp
is a pointer to the event flag group. This pointer is returned to your application when the event flag group is created (see OSFlagCreate()
).
opt
specifies whether you want to delete the event flag group only if there are no pending tasks (OS_DEL_NO_PEND
) or whether you always want to delete the event flag group regardless of whether tasks are pending or not (OS_DEL_ALWAYS
). In this case, all pending task will be readied.
err
is a pointer to a variable which will be used to hold an error code. The error code can be one of the following:
OS_NO_ERR
if the call was successful and the event flag group was deleted.
OS_ERR_DEL_ISR
if you attempted to delete an event flag group from an ISR.
OS_FLAG_INVALID_pgrp
if you passed a NULL pointer in pgrp
.
OS_ERR_EVENT_TYPE
if pgrp
is not pointing to an event flag group.
OS_ERR_INVALID_OPT
if you didn’t specify one of the two options mentioned above.
OS_ERR_TASK_WAITING
if one or more task were waiting on the event flag group and you specified OS_DEL_NO_PEND
.
Returned Value
A NULL pointer if the event flag group is deleted, or pgrp
if the event flag group was not deleted. In the latter case, you would need to examine the error code to determine the reason.
Notes/Warnings
- You should use this call with care because other tasks may expect the presence of the event flag group.
- This call can potentially disable interrupts for a long time. The interrupt disable time is directly proportional to the number of tasks waiting on the event flag group.
Example
OS_FLAG_GRP *EngineStatusFlags; void Task (void *pdata) { INT8U err; OS_FLAG_GRP *pgrp ; pdata = pdata; while (1) { . . pgrp = OSFlagDel(EngineStatusFlags, OS_DEL_ALWAYS, &err); if (pgrp == (OS_FLAG_GRP *)0) { /* The event flag group was deleted */ } . . } }
OSFlagPend()
OS_FLAGS OSFlagPend(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U wait_type, INT16U timeout, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_FLAG.C | Task only | OS_FLAG_EN |
OSFlagPend()
is used to have a task wait for a combination of conditions (i.e., events or bits) to be set (or cleared) in an event flag group. You application can wait for ANY condition to be set (or cleared) or, ALL conditions to be either set or cleared. If the events that the calling task desires are not available then, the calling task will be blocked until the desired conditions are satisfied or, the specified timeout expires.
Arguments
pgrp
is a pointer to the event flag group. This pointer is returned to your application when the event flag group is created (see OSFlagCreate()
).
flags
is a bit pattern indicating which bit(s) (i.e., flags
) you wish to check. The bits you want are specified by setting the corresponding bits in flags
.
wait_type
specifies whether you want ALL bits to be set/cleared or ANY of the bits to be set/cleared. You can specify the following argument:
OS_FLAG_WAIT_CLR_ALL
You will check ALL bits in 'flags
' to be clear (0)
OS_FLAG_WAIT_CLR_ANY
You will check ANY bit in 'flags
' to be clear (0)
OS_FLAG_WAIT_SET_ALL
You will check ALL bits in 'flags
' to be set (1)
OS_FLAG_WAIT_SET_ANY
You will check ANY bit in 'flags
' to be set (1)
You can also specify whether the flags
will be ‘consumed’ by adding OS_FLAG_CONSUME
to the wait_type
. For example, to wait for ANY flag in a group and then CLEAR the flags
that satisfy the condition, set wait_type
to:
OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME
err
is a pointer to an error code and can be:
OS_NO_ERR
No error
OS_ERR_PEND_ISR
You tried to call OSFlagPend from an ISR which is not allowed.
OS_FLAG_INVALID_pgrp
You passed a NULL pointer instead of the event flag handle.
OS_ERR_EVENT_TYPE
You are not pointing to an event flag group
OS_TIMEOUT
The flags
were not available within the specified amount of time.
OS_FLAG_ERR_WAIT_TYPE
You didn't specify a proper 'wait_type
' argument.
Returned Value
The value of the flags
in the event flag group after they are consumed (if OS_FLAG_CONSUME
is specified) or, the state of the flags
just before OSFlagPend()
returns. OSFlagPend()
returns 0 if a timeout occurs.
Notes/Warnings
The event flag group must be created before it’s used.
Example
#define ENGINE_OIL_PRES_OK 0x01 #define ENGINE_OIL_TEMP_OK 0x02 #define ENGINE_START 0x04 OS_FLAG_GRP *EngineStatus; void Task (void *pdata) { INT8U err; OS_FLAGS value; pdata = pdata; for (;;) { value = OSFlagPend(EngineStatus, ENGINE_OIL_PRES_OK + ENGINE_OIL_TEMP_OK, OS_FLAG_WAIT_SET_ALL + OS_FLAG_CONSUME, 10, &err); switch (err) { case OS_NO_ERR: /* Desired flags are available */ break; case OS_TIMEOUT: /* The desired flags were NOT available before 10 ticks occurred */ break; } . . } }
OSFlagPost()
OS_FLAGS OSFlagPost(OS_FLAG_GRP *pgrp, OS_FLAGS flags, INT8U opt, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_FLAG.C | Task or ISR | OS_FLAG_EN |
You set or clear event flag bits by calling OSFlagPost()
. The bits set or cleared are specified in a ‘bit mask’. OSFlagPost()
will ready each task that has it’s desired bits satisfied by this call. You can set or clear bits that are already set or cleared.
Arguments
pgrp
is a pointer to the event flag group. This pointer is returned to your application when the event flag group is created (see OSFlagCreate()
).
flags
specifies which bits you want set or cleared. If opt (see below) is OS_FLAG_SET
, each bit that is set in 'flags
' will set the corresponding bit in the event flag group. For example, to set bits 0, 4 and 5 you would set flags
to 0x31 (note, bit 0 is least significant bit). If opt (see below) is OS_FLAG_CLR
, each bit that is set in flags
will CLEAR the corresponding bit in the event flag group. For example, to clear bits 0, 4 and 5 you would specify 'flags
' as 0x31 (note, bit 0 is least significant bit).
opt
indicates whether the flags
will be set (OS_FLAG_SET
) or cleared (OS_FLAG_CLR
).
err
is a pointer to an error code and can be:
OS_NO_ERR
The call was successful
OS_FLAG_INVALID_pgrp
You passed a NULL pointer
OS_ERR_EVENT_TYPE
You are not pointing to an event flag group
OS_FLAG_INVALID_OPT
You specified an invalid option
Returned Value
The new value of the event flags.
Notes/Warnings
- Event flag groups must be created before they are used.
- The execution time of this function depends on the number of tasks waiting on the event flag group. However, the execution time is deterministic.
- The amount of time interrupts are DISABLED also depends on the number of tasks waiting on the event flag group.
Example
#define ENGINE_OIL_PRES_OK 0x01 #define ENGINE_OIL_TEMP_OK 0x02 #define ENGINE_START 0x04 OS_FLAG_GRP *EngineStatusFlags; void TaskX (void *pdata) { INT8U err; pdata = pdata; for (;;) { . . err = OSFlagPost(EngineStatusFlags, ENGINE_START, OS_FLAG_SET, &err); . . } }
OSFlagQuery()
OS_FLAGS OSFlagQuery (OS_FLAG_GRP *pgrp, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_FLAG.C | Task or ISR | OS_FLAG_EN and OS_FLAG_QUERY_EN |
OSFlagQuery()
is used to obtain the current value of the event flags in a group. At this time, this function does NOT return the list of tasks waiting for the event flag group.
Arguments
pgrp
is a pointer to the event flag group. This pointer is returned to your application when the event flag group is created (see OSFlagCreate()
).
err
is a pointer to an error code and can be:
OS_NO_ERR
The call was successful
OS_FLAG_INVALID_pgrp
You passed a NULL pointer
OS_ERR_EVENT_TYPE
You are not pointing to an event flag group
Returned Value
The state of the flags
in the event flag
group.
Notes/Warnings
- The event flag group to query must be created.
- You can call this function from an ISR.
Example
OS_FLAG_GRP *EngineStatusFlags; void Task (void *pdata) { OS_FLAGS flags; INT8U err; pdata = pdata; for (;;) { . . flags = OSFlagQuery(EngineStatusFlags, &err); . . } }
OSInit()
void OSInit (void);
File | Called From | Code Enabled By |
---|---|---|
OS_CORE.C | Startup code only | N/A |
OSInit()
initializes µC/OS-II and must be called prior to calling OSStart()
, which actually starts multitasking.
Arguments
None
Returned Value
None
Notes/Warnings
OSInit()
must be called before OSStart()
.
Example
void main (void) { . . OSInit(); /* Initialize uC/OS-II */ . . OSStart(); /* Start Multitasking */ }
OSIntEnter()
void OSIntEnter (void);
File | Called From | Code Enabled By |
---|---|---|
OS_CORE.C | ISR only | N/A |
OSIntEnter()
notifies µC/OS-II that an ISR is being processed. This allows µC/OS-II to keep track of interrupt nesting. OSIntEnter()
is used in conjunction with OSIntExit()
.
Arguments
None
Returned Value
None
Notes/Warnings
This function must not be called by task-level code.
You can increment the interrupt nesting counter (OSIntNesting
) directly in your ISR to avoid the overhead of the function call/return. It’s save to increment OSIntNesting
in your ISR because interrupts are assumed to be disabled when OSIntNesting
needs to be incremented.
You are allowed to nest interrupts up to 255 levels deep.
Example 1
(Intel 80x86, real mode, large model)
Use OSIntEnter()
for backward compatibility with µC/OS.
ISRx PROC FAR PUSHA ; Save interrupted task's context PUSH ES PUSH DS ; MOV AX, SEG(_OSIntNesting) ; Reload DS MOV DS, AX ; CALL FAR PTR _OSIntEnter ; Notify µC/OS-II of start of ISR . . POP DS ; Restore processor registers POP ES POPA IRET ; Return from interrupt ISRx ENDP
Example 2
(Intel 80x86, real mode, large model)
ISRx PROC FAR PUSHA ; Save interrupted task's context PUSH ES PUSH DS ; MOV AX, SEG(_OSIntNesting) ; Reload DS MOV DS, AX ; INC BYTE PTR _OSIntNesting ; Notify ?C/OS-II of start of ISR . . . POP DS ; Restore processor registers POP ES POPA IRET ; Return from interrupt ISRx ENDP
OSIntExit()
void OSIntExit (void);
File | Called From | Code Enabled By |
---|---|---|
OS_CORE.C | ISR only | N/A |
OSIntExit()
notifies µC/OS-II that an ISR has completed. This allows µC/OS-II to keep track of interrupt nesting. OSIntExit()
is used in conjunction with OSIntEnter()
. When the last nested interrupt completes, OSIntExit()
determines if a higher priority task has been made ready to run, in which case, the interrupt returns to the higher priority task instead of the interrupted task.
Arguments
None
Returned Value
None
Notes/Warnings
This function must not be called by task-level code. Also, if you decided to increment OSIntNesting, you still need to call OSIntExit()
.
Example
(Intel 80x86, real mode, large model) ISRx PROC FAR PUSHA ; Save processor registers PUSH ES PUSH DS . . CALL FAR PTR _OSIntExit ; Notify µC/OS-II of end of ISR POP DS ; Restore processor registers POP ES POPA IRET ; Return to interrupted task ISRx ENDP
OSMboxAccept()
void *OSMboxAccept (OS_EVENT *pevent);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task or ISR | OS_MBOX_EN and OS_MBOX_ACCEPT_EN |
OSMboxAccept()
allows you to see if a message is available from the desired mailbox. Unlike OSMboxPend()
, OSMboxAccept()
does not suspend the calling task if a message is not available. In other words, OSMboxAccept()
is non-blocking. If a message is available, the message is returned to your application and the content of the mailbox is cleared. This call is typically used by ISRs because an ISR is not allowed to wait for a message at a mailbox.
Arguments
pevent
is a pointer to the mailbox from which the message is received. This pointer is returned to your application when the mailbox is created [see OSMboxCreate()
].
Returned Value
A pointer to the message if one is available; NULL if the mailbox does not contain a message.
Notes/Warnings
Mailboxes must be created before they are used.
Example
OS_EVENT *CommMbox; void Task (void *pdata) { void *msg; pdata = pdata; for (;;) { msg = OSMboxAccept(CommMbox); /* Check mailbox for a message */ if (msg != (void *)0) { . /* Message received, process */ . } else { . /* Message not received, do .. */ . /* .. something else */ } . . } }
OSMboxCreate()
OS_EVENT *OSMboxCreate (void *msg);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task or startup code | OS_MBOX_EN |
OSMboxCreate()
creates and initializes a mailbox. A mailbox allows tasks or ISRs to send a pointer-sized variable (message) to one or more tasks.
Arguments
msg
is used to initialize the contents of the mailbox. The mailbox is empty when msg is a NULL pointer. The mailbox initially contains a message when msg is non-NULL.
Returned Value
A pointer to the event control block allocated to the mailbox. If no event control block is available, OSMboxCreate()
returns a NULL pointer.
Notes/Warnings
Mailboxes must be created before they are used.
Example
OS_EVENT *CommMbox; void main(void) { . . OSInit(); /* Initialize ?C/OS-II */ . . CommMbox = OSMboxCreate((void *)0); /* Create COMM mailbox */ OSStart(); /* Start Multitasking */ }
OSMboxDel()
OS_EVENT *OSMboxDel (OS_EVENT *pevent, INT8U opt, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task | OS_MBOX_EN and OS_MBOX_DEL_EN |
OSMboxDel()
is used to delete a message mailbox. This is a dangerous function to use because multiple tasks could attempt to access a deleted mailbox. You should always use this function with great care. Generally speaking, before you would delete a mailbox, you would first delete all the tasks that can access the mailbox.
Arguments
pevent
is a pointer to the mailbox. This pointer is returned to your application when the mailbox is created (see OSMboxCreate()
).
opt
specifies whether you want to delete the mailbox only if there are no pending tasks (OS_DEL_NO_PEND
) or whether you always want to delete the mailbox regardless of whether tasks are pending or not (OS_DEL_ALWAYS
). In this case, all pending task will be readied.
err
is a pointer to a variable which will be used to hold an error code. The error code can be one of the following:
OS_NO_ERR
if the call was successful and the mailbox was deleted.
OS_ERR_DEL_ISR
if you attempted to delete the mailbox from an ISR
OS_ERR_INVALID_OPT
if you didn’t specify one of the two options mentioned above.
OS_ERR_TASK_WAITING
One or more tasks were waiting on the mailbox
OS_ERR_EVENT_TYPE
if pevent is not pointing to a mailbox.
OS_ERR_PEVENT_NULL
if there are no more OS_EVENT
structures available.
Returned Value
A NULL pointer if the mailbox is deleted or, pevent if the mailbox was not deleted. In the latter case, you would need to examine the error code to determine the reason.
Notes/Warnings
You should use this call with care because other tasks may expect the presence of the mailbox.
Interrupts are disabled when pended tasks are readied. This means that interrupt latency depends on the number of tasks that were waiting on the mailbox.
OSMboxAccept()
callers will not know that the mailbox has been deleted.
Example
OS_EVENT *DispMbox; void Task (void *pdata) { INT8U err; pdata = pdata; while (1) { . . DispMbox = OSMboxDel(DispMbox, OS_DEL_ALWAYS, &err); if (DispMbox == (OS_EVENT *)0) { /* Mailbox has been deleted */ } . . } }
OSMboxPend()
void *OSMboxPend (OS_EVENT *pevent, INT16U timeout, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task only | OS_MBOX_EN |
OSMboxPend()
is used when a task expects to receive a message. The message is sent to the task either by an ISR or by another task. The message received is a pointer-sized variable and its use is application specific. If a message is present in the mailbox when OSMboxPend()
is called, the message is retrieved, the mailbox is emptied, and the retrieved message is returned to the caller. If no message is present in the mailbox, OSMboxPend()
suspends the current task until either a message is received or a user-specified timeout expires. If a message is sent to the mailbox and multiple tasks are waiting for the message, µC/OS-II resumes the highest priority task waiting to run. A pended task that has been suspended with OSTaskSuspend()
can receive a message. However, the task remains suspended until it is resumed by calling OSTaskResume()
.
Arguments
pevent
is a pointer to the mailbox from which the message is received. This pointer is returned to your application when the mailbox is created [see OSMboxCreate()
].
timeout
allows the task to resume execution if a message is not received from the mailbox within the specified number of clock ticks. A timeout value of 0 indicates that the task wants to wait forever for the message. The maximum timeout is 65,535 clock ticks. The timeout value is not synchronized with the clock tick. The timeout count begins decrementing on the next clock tick, which could potentially occur immediately.
err
is a pointer to a variable that holds an error code. OSMboxPend()
sets *err to one of the following:
OS_NO_ERR
if a message was received.
OS_TIMEOUT
if a message was not received within the specified timeout period.
OS_ERR_EVENT_TYPE
pevent is not pointing to a mailbox.
OS_ERR_PEND_ISR
if you called this function from an ISR and µC/OS-II has to suspend it. In general, you should not call OSMboxPend()
from an ISR, but µC/OS-II checks for this situation anyway.
OS_ERR_PEVENT_NULL
if pevent is a NULL pointer.
Returned Value
OSMboxPend()
returns the message sent by either a task or an ISR and *err is set to OS_NO_ERR
. If a message is not received within the specified timeout period, the returned message is a NULL pointer and *err is set to OS_TIMEOUT
.
Notes/Warnings
- Mailboxes must be created before they are used.
- You should not call
OSMboxPend()
from an ISR.
Example
OS_EVENT *CommMbox; void CommTask(void *pdata) { INT8U err; void *msg; pdata = pdata; for (;;) { . . msg = OSMboxPend(CommMbox, 10, &err); if (err == OS_NO_ERR) { . . /* Code for received message */ . } else { . . /* Code for message not received within timeout */ . } . . } }
OSMboxPost()
INT8U OSMboxPost (OS_EVENT *pevent, void *msg);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task or ISR | OS_MBOX_EN and OS_MBOX_POST_EN |
OSMboxPost()
sends a message to a task through a mailbox. A message is a pointer-sized variable and its use is application specific. If a message is already in the mailbox, an error code is returned indicating that the mailbox is full. OSMboxPost()
then immediately returns to its caller and the message is not placed in the mailbox. If any task is waiting for a message at the mailbox, the highest priority task waiting receives the message. If the task waiting for the message has a higher priority than the task sending the message, the higher priority task is resumed and the task sending the message is suspended. In other words, a context switch occurs.
Arguments
pevent
is a pointer to the mailbox into which the message is deposited. This pointer is returned to your application when the mailbox is created [see OSMboxCreate()
].
msg
is the actual message sent to the task. msg is a pointer-sized variable and is application specific. You must never post a NULL pointer because this indicates that the mailbox is empty.
Returned Value
OSMboxPost()
returns one of two error codes:
OS_NO_ERR
if the message was deposited in the mailbox.
OS_MBOX_FULL
if the mailbox already contained a message.
OS_ERR_EVENT_TYPE
if pevent is not pointing to a mailbox.
OS_ERR_PEVENT_NULL
if pevent is a pointer to NULL.
OS_ERR_POST_NULL_PTR
if you are attempting to post a NULL pointer. By convention a NULL pointer is not supposed to point to anything.
Notes/Warnings
Mailboxes must be created before they are used.
You must never post a NULL pointer because this indicates that the mailbox is empty.
Example
OS_EVENT *CommMbox; INT8U CommRxBuf[100]; void CommTaskRx(void *pdata) { INT8U err; pdata = pdata; for (;;) { . . err = OSMboxPost(CommMbox, (void *)&CommRxBuf[0]); . . } }
OSMboxPostOpt()
INT8U OSMboxPostOpt (OS_EVENT *pevent, void *msg, INT8U opt);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task or ISR | OS_MBOX_EN and OS_MBOX_POST_OPT_EN |
OSMboxPostOpt()
works just like OSMboxPost()
except that it allows you to post a message to multiple tasks. In other words, OSMboxPostOpt()
allows the message posted to be broadcast to ALL tasks waiting on the mailbox. OSMboxPostOpt()
can actually replace OSMboxPost()
because it can emulate OSMboxPost()
.
OSMboxPostOpt()
is used to send a message to a task through a mailbox. A message is a pointer-sized variable and its use is application specific. If a message is already in the mailbox, an error code is returned indicating that the mailbox is full. OSMboxPostOpt()
then immediately returns to its caller and the message is not placed in the mailbox. If any task is waiting for a message at the mailbox, OSMboxPostOpt()
allows you to either post the message to the highest priority task waiting at the mailbox (opt set to OS_POST_OPT_NONE
) or, to all tasks waiting at the mailbox (opt is set to OS_POST_OPT_BROADCAST
). In either case, scheduling will occur and if any of the task that receives the message has a higher priority than the task that is posting the message then, the higher priority task will be resumed and the sending task will be suspended. In other words, a context switch will occur.
Arguments
pevent
is a pointer to the mailbox. This pointer is returned to your application when the mailbox is created (see OSMboxCreate()
).
msg
is the actual message sent to the task(s) msg is a pointer-sized variable and what msg points to is application specific. You must never post a NULL pointer because this indicates that the mailbox is empty.
opt
specifies whether you want to send the message to the highest priority task waiting at the mailbox (when opt is set to OS_POST_OPT_NONE
) or, to ALL tasks waiting at the mailbox (when opt is set to OS_POST_OPT_BROADCAST
).
Returned Value
err
is a pointer to a variable which will be used to hold an error code. The error code can be one of the following:
OS_NO_ERR
if the call was successful and the message was sent.
OS_MBOX_FULL
if the mailbox already contains a message. You can only send ONE message at a time to a mailbox and thus, the message MUST be consumed before you are allowed to send another one.
OS_ERR_EVENT_TYPE
if pevent is not pointing to a mailbox.
OS_ERR_PEVENT_NULL
if pevent is a NULL pointer.
OS_ERR_POST_NULL_PTR
if you are attempting to post a NULL pointer. By convention, a NULL pointer is not supposed to point to anything.
Notes/Warnings
Mailboxes must be created before they are used.
You must NEVER post a NULL pointer to a mailbox because this indicates that the mailbox is empty.
If you need to use this function and want to reduce code space, you may disable code generation of OSMboxPost()
since OSMboxPostOpt()
can emulate OSMboxPost()
.
The execution time of OSMboxPostOpt()
depends on the number of tasks waiting on the mailbox if you set opt to OS_POST_OPT_BROADCAST
.
Example
OS_EVENT *CommMbox; INT8U CommRxBuf[100]; void CommRxTask (void *pdata) { INT8U err; pdata = pdata; for (;;) { . . err = OSMboxPostOpt(CommMbox, (void *)&CommRxBuf[0], OS_POST_OPT_BROADCAST); . . } }
OSMboxQuery()
INT8U OSMboxQuery (OS_EVENT *pevent, OS_MBOX_DATA *pdata);
File | Called From | Code Enabled By |
---|---|---|
OS_MBOX.C | Task or ISR | OS_MBOX_EN and OS_MBOX_QUERY_EN |
OSMboxQuery()
obtains information about a message mailbox. Your application must allocate an OS_MBOX_DATA
data structure, which is used to receive data from the event control block of the message mailbox. OSMboxQuery()
allows you to determine whether any tasks are waiting for a message at the mailbox and how many tasks are waiting (by counting the number of 1s in the .OSEventTbl[]
field). You can also examine the content of the mailbox. Note that the size of .OSEventTbl[]
is established by the #define
constant OS_EVENT_TBL_SIZE
(see uCOS_II.H
).
Arguments
pevent
is a pointer to the mailbox. This pointer is returned to your application when the mailbox is created [see OSMboxCreate()
].
pdata
is a pointer to a data structure of type OS_MBOX_DATA
, which contains the following fields:
void *OSMsg; /* Copy of the message stored in the mailbox */ INT8U OSEventTbl[OS_EVENT_TBL_SIZE]; /* Copy of the mailbox wait list */ INT8U OSEventGrp;
Returned Value
OSMboxQuery()
returns one of two error codes:
OS_NO_ERR
if the call was successful.
OS_ERR_PEVENT_NULL
if pevent is a NULL pointer.
OS_ERR_EVENT_TYPE
if you didn’t pass a pointer to a message mailbox.
Notes/Warnings
Message mailboxes must be created before they are used.
Example
OS_EVENT *CommMbox; void Task (void *pdata) { OS_MBOXDATA mbox_data; INT8U err; pdata = pdata; for (;;) { . . err = OSMboxQuery(CommMbox, &mbox_data); if (err == OS_NO_ERR) { . /* Mailbox contains a message if mbox_data.OSMsg is not NULL */ } . . } }
OSMemCreate()
OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_MEM.C | Task or startup code | OS_MEM_EN |
OSMemCreate()
creates and initializes a memory partition. A memory partition contains a user-specified number of fixed-size memory blocks. Your application can obtain one of these memory blocks and, when done, release the block back to the partition.
Arguments
addr
is the address of the start of a memory area that is used to create fixed-size memory blocks. Memory partitions can be created either using static arrays or malloc()
during startup.
nblks
contains the number of memory blocks available from the specified partition. You must specify at least two memory blocks per partition.
blksize
specifies the size (in bytes) of each memory block within a partition. A memory block must be large enough to hold at least a pointer.
err
is a pointer to a variable that holds an error code. OSMemCreate()
sets *err
to
OS_NO_ERR
if the memory partition was created successfully,
OS_MEM_INVALID_ADDR
if your are specifying an invalid address (i.e., addr is a NULL pointer),
OS_MEM_INVALID_PART
if a free memory partition was not available,
OS_MEM_INVALID_BLKS
if you didn’t specify at least two memory blocks per partition, or
OS_MEM_INVALID_SIZE
if you didn’t specify a block size that can contain at least a pointer variable.
Returned Value
OSMemCreate()
returns a pointer to the created memory partition control block if one is available. If no memory partition control block is available, OSMemCreate()
returns a NULL pointer.
Notes/Warnings
Memory partitions must be created before they are used.
Example
OS_MEM *CommMem; INT8U CommBuf[16][128]; void main(void) { INT8U err; OSInit(); /* Initialize µC/OS-II */ . . CommMem = OSMemCreate(&CommBuf[0][0], 16, 128, &err); . . OSStart(); /* Start Multitasking */ }
OSMemGet()
void *OSMemGet (OS_MEM *pmem, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_MEM.C | Task or ISR | OS_MEM_EN |
OSMemGet obtains a memory block from a memory partition. It is assumed that your application knows the size of each memory block obtained. Also, your application must return the memory block [using OSMemPut()
] when it no longer needs it. You can call OSMemGet()
more than once until all memory blocks are allocated.
Arguments
pmem
is a pointer to the memory partition control block that is returned to your application from the OSMemCreate()
call.
err
is a pointer to a variable that holds an error code. OSMemGet()
sets *err
to one of the following:
OS_NO_ERR
if a memory block was available and returned to your application.
OS_MEM_NO_FREE_BLKS
if the memory partition didn’t contain any more memory blocks to allocate.
OS_MEM_INVALID_PMEM
if pmem is a NULL pointer.
Returned Value
OSMemGet()
returns a pointer to the allocated memory block if one is available. If no memory block is available from the memory partition, OSMemGet()
returns a NULL pointer.
Notes/Warnings
Memory partitions must be created before they are used.
Example
OS_MEM *CommMem; void Task (void *pdata) { INT8U *msg; pdata = pdata; for (;;) { msg = OSMemGet(CommMem, &err); if (msg != (INT8U *)0) { . /* Memory block allocated, use it. */ . } . . } }
OSMemPut()
INT8U OSMemPut (OS_MEM *pmem, void *pblk);
File | Called From | Code Enabled By |
---|---|---|
OS_MEM.C | Task or ISR | OS_MEM_EN |
OSMemPut()
returns a memory block to a memory partition. It is assumed that you will return the memory block to the appropriate memory partition.
Arguments
pmem
is a pointer to the memory partition control block that is returned to your application from the OSMemCreate()
call.
pblk
is a pointer to the memory block to be returned to the memory partition.
Returned Value
OSMemPut()
returns one of the following error codes:
OS_NO_ERR
if a memory block was available and returned to your application.
OS_MEM_FULL
if the memory partition could not accept more memory blocks. This is surely an indication that something is wrong because you are returning more memory blocks than you obtained using OSMemGet()
.
OS_MEM_INVALID_PMEM
if pmem is a NULL pointer.
OS_MEM_INVALID_PBLK
if pblk is a NULL pointer.
Notes/Warnings
Memory partitions must be created before they are used.
You must return a memory block to the proper memory partition.
Example
OS_MEM *CommMem; INT8U *CommMsg; void Task (void *pdata) { INT8U err; pdata = pdata; for (;;) { err = OSMemPut(CommMem, (void *)CommMsg); if (err == OS_NO_ERR) { . /* Memory block released */ . } . . } }
OSMemQuery()
INT8U OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *pdata);
File | Called From | Code Enabled By |
---|---|---|
OS_MEM.C | Task or ISR | OS_MEM_EN and OS_MEM_QUERY_EN |
OSMemQuery()
obtains information about a memory partition. Basically, this function returns the same information found in the OS_MEM
data structure, but in a new data structure called OS_MEM_DATA
. OS_MEM_DATA
also contains an additional field that indicates the number of memory blocks in use.
Arguments
pmem
is a pointer to the memory partition control block that is returned to your application from the OSMemCreate()
call.
pdata
is a pointer to a data structure of type OS_MEM_DATA
, which contains the following fields:
void *OSAddr; /* Points to beginning address of the memory partition */ void *OSFreeList; /* Points to beginning of the free list of memory blocks */ INT32U OSBlkSize; /* Size (in bytes) of each memory block */ INT32U OSNBlks; /* Total number of blocks in the partition */ INT32U OSNFree; /* Number of memory blocks free */ INT32U OSNUsed; /* Number of memory blocks used */
Returned Value
OSMemQuery()
returns one of the following error codes:
OS_NO_ERR
if a memory block was available and returned to your application.
OS_MEM_INVALID_PMEM
if pmem is a NULL pointer.
OS_MEM_INVALID_PDATA
if pdata is a NULL pointer.
Notes/Warnings
Memory partitions must be created before they are used.
Example
OS_MEM *CommMem; void Task (void *pdata) { INT8U err; OS_MEM_DATA mem_data; pdata = pdata; for (;;) { . . err = OSMemQuery(CommMem, &mem_data); . . } }
OSMutexAccept()
INT8U OSMutexAccept (OS_EVENT *pevent, INT8U *err);
File | Called From | Code Enabled By |
---|---|---|
OS_MUTEX.C | Task | OS_MUTEX_EN |
OSMutexAccept()
allows you to check to see if a resource is available. Unlike OSMutexPend()
, OSMutexAccept()
does not suspend the calling task if the resource is not available. In other words, OSMutexAccept()
is non-blocking.
Arguments
pevent
is a pointer to the mutex that guards the resource. This pointer is returned to your application when the mutex is created (see OSMutexCreate()
).
err
is a pointer to a variable used to hold an error code. OSMutexAccept()
sets *err to one of the following:
OS_NO_ERR
if the call was successful.
OS_ERR_EVENT_TYPE
if pevent is not pointing to a mutex.
OS_ERR_PEVENT_NULL
if pevent is a NULL pointer.
OS_ERR_PEND_ISR
if you called OSMutexAccept()
from an ISR.
Returned Value
If the mutex was available, OSMutexAccept()
returns 1. If the mutex was owned by another task, OSMutexAccept()
returns 0.
Notes/Warnings
- Mutexes must be created before they are used.
- This function MUST NOT be called by an ISR.
- If you acquire the mutex through
OSMutexAccept()
, you MUST callOSMutexPost()
to release the mutex when you are done with the resource.