目录
事件控制块的清空
将事件控制块中的所有任务从它的等待队列中移除,再将这些任务插入就绪队列。
事件控制块的状态查询
仅需知道事件状态块中有多少个任务需要等待。
tEvent.c
#include "tinyOS.h"
/* 事件控制块初始化函数 */
void tEventInit(tEvent *event, tEventType type)
{
event->type = tEventTypeUnknow;
tListInit(&event->waitList);
}
/* 事件控制块等待函数 */
//参数:事件控制块,任务,消息(传入消息来源,在事件发生以后存放具体的消息),等待的状态,超时时间
void tEventWait(tEvent *event, tTask *task, void *msg, uint32_t state, uint32_t timeout)
{
uint32_t status = tTaskEnterCritical();
task->state |= state;
task->waitEvent = event;
task->eventMsg = msg;
task->waitEventResult = tErrorNoError;
tTaskSchedUnRdy(task);//移出就绪队列
tListAddLast(&event->waitList, &task->linkNode);//插入事件控制块等待队列的尾部
if(timeout)
{
tTimeTaskWait(task, timeout);//设置了超时事件,插入延时队列
}
tTaskExitCritical(status);
}
/* 事件控制块通知函数(将任务从事件控制块中唤醒) */
tTask *tEventWakeUp(tEvent *event, void *msg, uint32_t result)
{
tNode *node;
tTask *task = (tTask *)0;
uint32_t status = tTaskEnterCritical();
if((node = tListRemoveFirst(&event->waitList)) != (tNode *)0)
{
task = (tTask *)tNodeParent(node, tTask, linkNode);//插入到事件控制块是用linknode
task->waitEvent = (tEvent *)0;//不等待任何事件
task->eventMsg = msg;
task->waitEventResult = result;
task->state &= ~TINYOS_TASK_WAIT_MASK;
if(task->delayTicks != 0)//有延时
{
tTimeTaskWakeUp(task);//强制将任务从延时队列中移除
}
tTaskSchedRdy(task);//插入就绪队列
}
tTaskExitCritical(status);
return task;
}
/* 事件控制块移除函数 */
void tEventRemoveTask(tTask *task, void *msg, uint32_t result)
{
uint32_t status = tTaskEnterCritical();
tListRemove(&task->waitEvent->waitList, &task->linkNode);
task->waitEvent = (tEvent *)0;
task->eventMsg = msg;
task->waitEventResult = result;
task->state &= ~TINYOS_TASK_WAIT_MASK;
tTaskExitCritical(status);
}
/* 事件控制块清空函数 */
//返回值:事件任务块被清空时,它的等待队列中有多少任务
uint32_t tEventRemoveAll(tEvent *event, void *msg, uint32_t result)
{
tNode *node;
uint32_t count = 0;
uint32_t status = tTaskEnterCritical();
count = tListCount(&event->waitList);//等待队列中有多少任务
while((node = tListRemoveFirst(&event->waitList)) != (tNode *)0)//移除等待队列头部任务
{
tTask *task = (tTask *)tNodeParent(node, tTask, linkNode);//获取task结构
task->waitEvent = (tEvent *)0;//不再等待事件
task->eventMsg = msg;
task->waitEventResult = result;
task->state &= ~TINYOS_TASK_WAIT_MASK;
if(task->delayTicks != 0)//任务有延时
{
tTimeTaskWakeUp(task);//移出延时队列
}
tTaskSchedRdy(task);
}
tTaskExitCritical(status);
return count;
}
/* 获取事件控制块中等待任务函数 */
uint32_t tEventWaitCount(tEvent *event)
{
uint32_t count = 0;
uint32_t status = tTaskEnterCritical();
count = tListCount(&event->waitList);
tTaskExitCritical(status);
return count;
}
tEvent.h
#ifndef __TEVENT_H
#define __TEVENT_H
#include "tTask.h"
/* 事件控制块类型 */
typedef enum _tEventType{
tEventTypeUnknow,
}tEventType;
/* 事件控制块 */
typedef struct _tEvent{
tEventType type; //事件控制块类型
tList waitList; //等待列表
}tEvent;
void tEventInit(tEvent *event, tEventType type);
void tEventWait(tEvent *event, tTask *task, void *msg, uint32_t state, uint32_t timeout);
tTask *tEventWakeUp(tEvent *event, void *msg, uint32_t result);
void tEventRemoveTask(tTask *task, void *msg, uint32_t result);
uint32_t tEventRemoveAll(tEvent *event, void *msg, uint32_t result);
uint32_t tEventWaitCount(tEvent *event);
#endif
app.c
#include "tinyOS.h"
//定义任务,分别为它们配备独立的堆栈空间
tTask tTask1;
tTask tTask2;
tTask tTask3;
tTask tTask4;
tTaskStack task1Env[1024];
tTaskStack task2Env[1024];
tTaskStack task3Env[1024];
tTaskStack task4Env[1024];
//定义事件控制块
tEvent eventWaitTimeout;
tEvent eventWaitNormal;
//定义任务要执行的功能
int task1Flag;
void task1Entry(void *param)
{
tSetSysTickPeriod(10);//初始化
tEventInit(&eventWaitNormal, tEventTypeUnknow);
for(;;)//任务里是for的死循环
{
uint32_t count = tEventWaitCount(&eventWaitNormal);
uint32_t waskUpCount = tEventRemoveAll(&eventWaitNormal, (void *)0, 0);
if(waskUpCount > 0)
{
tTaskSched();
count = tEventWaitCount(&eventWaitNormal);
}
task1Flag = 0;
tTaskDelay(1);
task1Flag = 1;
tTaskDelay(1);
}
}
int task2Flag;
void task2Entry(void *param)
{
for(;;)
{
tEventWait(&eventWaitNormal, currentTask, (void *)0, 0, 0);
tTaskSched();
task2Flag = 0;
tTaskDelay(1);
task2Flag = 1;
tTaskDelay(1);
}
}
int task3Flag;
void task3Entry(void *param)
{
for(;;)
{
tEventWait(&eventWaitNormal, currentTask, (void *)0, 0, 0);
tTaskSched();
task3Flag = 0;
tTaskDelay(1);
task3Flag = 1;
tTaskDelay(1);
}
}
int task4Flag;
void task4Entry(void *param)
{
for(;;)
{
tEventWait(&eventWaitNormal, currentTask, (void *)0, 0, 0);
tTaskSched();
task4Flag = 0;
tTaskDelay(1);
task4Flag = 1;
tTaskDelay(1);
}
}
/* 应用任务初始化函数 */
void tInitApp(void)
{
//最后一个参数:传堆栈末端地址,因为堆栈是向下生长的,初始堆栈地址是堆栈空间最后一个单元地址的末端
tTaskInit(&tTask1, task1Entry, (void *)0x11111111, 0, &task1Env[1024]);
tTaskInit(&tTask2, task2Entry, (void *)0x22222222, 1, &task2Env[1024]);
tTaskInit(&tTask3, task3Entry, (void *)0x22222222, 1, &task3Env[1024]);
tTaskInit(&tTask4, task4Entry, (void *)0x22222222, 1, &task4Env[1024]);
}
标签:event,task,tTask,void,查询,事件,清空,tEvent,uint32
From: https://blog.csdn.net/daybydayby/article/details/144436125