Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2012 / uCOS / uCOSII_ebook.pdf
Скачиваний:
70
Добавлен:
10.02.2015
Размер:
1.08 Mб
Скачать

OSSemAccept()

INT16U OSSemAccept(OS_EVENT *pevent);

File

Called from

Code enabled by

OS_SEM.C

Task or ISR

OS_SEM_EN

OSSemAccept() allows you to check to see if a resource is available or an event occurred. Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the resource is not available. You would use OSSemAccept() from an ISR to obtain the semaphore.

Arguments

pevent is a pointer to the semaphore that guards the resource. This pointer is returned to your application when the semaphore is created (see OSSemCreate()).

Returned Value

When the semaphore value is greater than 0 when OSSemAccept() is called then, the semaphore value is decremented and the value of the semaphore before the decrement is returned to your application. If, however, the semaphore value is 0 then, the resource is not available and 0 is returned to your application.

Notes/Warnings

Semaphores must be created before they are used.

Example

OS_EVENT *DispSem;

void Task (void *pdata)

{

INT16U value;

pdata = pdata; for (;;) {

value = OSSemAccept(DispSem); /* Check resource availability */

if (value > 0) {

 

.

/* Resource available, process */

.

 

}

 

.

 

.

 

}

 

}

OSSemCreate()

OS_EVENT *OSSemCreate(WORD value);

File

Called from

Code enabled by

OS_SEM.C

Task or startup code

OS_SEM_EN

OSSemCreate() is used to create and initialize a semaphore. A semaphore is usedto:

1)Allow a task to synchronize with either an ISR or a task

2)Gain exclusive access to a resource

3)Signal the occurrence of an event

Arguments

value is the initial value of the semaphore. The initial value of the semaphore is allowed to be between 0 and 65535.

Returned Value

A pointer to the event control block allocated to the semaphore. If no event control block is available, OSSemCreate() will return a NULL pointer.

Notes/Warnings

Semaphores must be created before they are used.

Example

OS_EVENT *DispSem;

 

 

void main(void)

 

 

{

 

 

.

 

 

.

 

 

OSInit();

/* Initialize µC/OS-II

*/

.

 

 

.

 

 

DispSem = OSSemCreate(1);

/* Create Display Semaphore

*/

.

 

 

.

 

 

OSStart();

/* Start Multitasking

*/

}

 

 

OSSemPend()

void OSSemPend(OS_EVENT *pevent, INT16U timeout, INT8U *err);

File

Called from

Code enabled by

OS_SEM.C

Task only

OS_SEM_EN

OSSemPend()is used when a task desires to get exclusive access to a resource, synchronize its activities with an ISR, a task or until an event occurs. If a task callsOSSemPend() and the value of the semaphore is greater than 0, then OSSemPend() will decrement the semaphore and return to its caller. However, if the value of the semaphore is equal to zero, OSSemPend() places the calling task in the waiting list for the semaphore. The task will thus wait until a task or an ISR signals the semaphore or, the specified timeout expires. If the semaphore is signaled before the timeout expires,

µC/OS-II will resume the highest priority task that is waiting for the semaphore. A pended task that has been suspended with OSTaskSuspend()can obtain the semaphore. The task will, however, remain suspended until the task is

resumed by calling OSTaskResume().

Arguments

pevent is a pointer to the semaphore. This pointer is returned to your application when the semaphore is created (see

OSSemCreate()).

timeout is used to allow 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 desires to wait forever for the message. The maximum timeout is 65535 clock ticks. The timeout value is not synchronized with the clock tick. The timeout count starts being decremented on the next clock tick which could potentially occur immediately.

err is a pointer to a variable which will be used to hold an error code. OSSemPend() sets *err to either:

1)OS_NO_ERR, the semaphore is available

2)OS_TIMEOUT, the semaphore was not signaled within the specified timeout

3)OS_ERR_PEND_ISR, you called this function from an ISR and µC/OS-II would have to suspend the ISR. In general, you should not call OSMboxPend(). µC/OS-II checks for this situation in case you do anyway.

Returned Value

NONE

Notes/Warnings

Semaphores must be created before they are used.

Example

OS_EVENT *DispSem;

void DispTask(void *pdata)

{

INT8U err;

pdata = pdata; for (;;) {

.

.

OSSemPend(DispSem, 0, &err);

.

/*

The only way this task continues is if … */

.

/*

… the semaphore is signaled!

*/

}

}

OSSemPost()

INT8U OSSemPost(OS_EVENT *pevent);

File

Called from

Code enabled by

OS_SEM .C

Task or ISR

OS_SEM_EN

A semaphore is signaled by calling OSSemPost(). If the semaphore value is greater than or equal to zero, the semaphore is incremented andOSSemPost() returns to its caller. If tasks are waiting for the semaphore to be signaled then, OSSemPost() removes the highest priority task pending (waiting) for the semaphore from the waiting list and makes this task ready to run. The scheduler is then called to determine if the awakened task is now the highest priority task ready to run.

Arguments

pevent is a pointer to the semaphore. This pointer is returned to your application when the semaphore is created (see

OSSemCreate()).

Returned Value

OSSemPost() returns one of these two error codes

1)OS_NO_ERR, if the semaphore was successfully signaled

2)OS_SEM_OVF, if the semaphore count overflowed

Notes/Warnings

Semaphores must be created before they are used.

Example

OS_EVENT *DispSem;

void TaskX(void *pdata)

{

INT8U err;

pdata = pdata; for (;;) {

.

.

err = OSSemPost(DispSem);

Соседние файлы в папке uCOS