Development Guidelines

Available APIs

Category

API

Description

Checking events

LOS_EventPoll

Checks whether the expected event occurs based on eventID, eventMask, and mode.

NOTICE:

If mode contains LOS_WAITMODE_CLR and the expected event occurs, the event that meets the requirements in eventID will be cleared. In this case, eventID is an input parameter and an output parameter. In other cases, eventID is used only as an input parameter.

Initializing events

LOS_EventInit

Initializes an event control block.

Reading events

LOS_EventRead

Reads an event (wait event). The task is blocked to wait based on the timeout period (in ticks).

If no event is read, 0 is returned.

If an event is successfully read, a positive value (event set) is returned.

In other cases, a specific error code is returned.

Writing events

LOS_EventWrite

Writes a specific event to the event control block.

Clearing events

LOS_EventClear

Clears an event in the event control block based on the event mask.

Destroying events

LOS_EventDestroy

Destroys an event control block.

How to Develop

The typical event development process is as follows:

  1. Initialize an event control block.
  2. Block a read event control block.
  3. Write related events.
  4. Wake up a blocked task, read the event, and check whether the event meets conditions.
  5. Handle the event control block.
  6. Destroy an event control block.

NOTE:

  • When an event is read or written, the 25th bit of the event is reserved and cannot be set.
  • Repeated writes of the same event are treated as one write.

Development Example

Example Description

In this example, run the Example_TaskEntry task to create the Example_Event task. Run the Example_Event task to read an event to trigger task switching. Run the Example_TaskEntry task to write an event. You can understand the task switching during event operations based on the sequence in which logs are recorded.

  1. Create the Example_Event task in the Example_TaskEntry task with a higher priority than the Example_TaskEntry task.
  2. Run the Example_Event task to read event 0x00000001. Task switching occurs to execute the Example_TaskEntry task.
  3. Run the Example_TaskEntry task to write event 0x00000001. Task switching occurs to execute the Example_Event task.
  4. The Example_Event task is executed.
  5. The Example_TaskEntry task is executed.

Sample Code

The sample code is as follows:

#include "los_event.h"
#include "los_task.h"
#include "securec.h"

/* Task ID*/
UINT32 g_testTaskId;

/* Event control structure*/ 
EVENT_CB_S g_exampleEvent;

/* Type of the wait event*/
#define EVENT_WAIT 0x00000001

/* Example task entry function*/
VOID Example_Event(VOID)
{
    UINT32 ret;
    UINT32 event;

    /* Set a timeout period for event reading to 100 ticks. If the specified event is not read within 100 ticks, the read operation times out and the task is woken up.*/
    printf("Example_Event wait event 0x%x \n", EVENT_WAIT);

    event = LOS_EventRead(&g_exampleEvent, EVENT_WAIT, LOS_WAITMODE_AND, 100);
    if (event == EVENT_WAIT) {
        printf("Example_Event,read event :0x%x\n", event);
    } else {
        printf("Example_Event,read event timeout\n");
    }
}

UINT32 Example_TaskEntry(VOID)
{
    UINT32 ret;
    TSK_INIT_PARAM_S task1;

    /* Initialize the event.*/ 
    ret = LOS_EventInit(&g_exampleEvent);
    if (ret != LOS_OK) {
        printf("init event failed .\n");
        return -1;
    }

    /* Create a task.*/ 
    (VOID)memset_s(&task1, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
    task1.pfnTaskEntry = (TSK_ENTRY_FUNC)Example_Event;
    task1.pcName       = "EventTsk1";
    task1.uwStackSize  = OS_TSK_DEFAULT_STACK_SIZE;
    task1.usTaskPrio   = 5;
    ret = LOS_TaskCreate(&g_testTaskId, &task1);
    if (ret != LOS_OK) {
        printf("task create failed.\n");
        return LOS_NOK;
    }

    /* Write the task wait event (g_testTaskId). */
    printf("Example_TaskEntry write event.\n");

    ret = LOS_EventWrite(&g_exampleEvent, EVENT_WAIT);
    if (ret != LOS_OK) {
        printf("event write failed.\n");
        return LOS_NOK;
    }

    /* Clear the flag.*/ 
    printf("EventMask:%d\n", g_exampleEvent.uwEventID);
    LOS_EventClear(&g_exampleEvent, ~g_exampleEvent.uwEventID);
    printf("EventMask:%d\n", g_exampleEvent.uwEventID);

    /* Delete the task.*/ 
    ret = LOS_TaskDelete(g_testTaskId);
    if (ret != LOS_OK) {
        printf("task delete failed.\n");
        return LOS_NOK;
    }

    return LOS_OK;
}

Verification

The development is successful if the return result is as follows:

Example_Event wait event 0x1 
Example_TaskEntry write event.
Example_Event,read event :0x1
EventMask:1
EventMask:0