📄 os_mbox.c
字号:
}//发送的不是空指针
if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
return (OS_ERR_EVENT_TYPE);
}
#endif
OS_ENTER_CRITICAL();
if (pevent->OSEventGrp != 0x00) { /* See if any task pending on mailbox */
//是不是有任务在等待该邮箱中的消息,如果OSEventGrp非零,则表明有任务
OS_EventTaskRdy(pevent, msg, OS_STAT_MBOX); /* Ready highest priority task waiting on event */
//将最高优先级的任务从等待列表中删除
OS_EXIT_CRITICAL();
OS_Sched(); /* Find highest priority task ready to run */
//任务调度,检查该任务是否系统中就绪任务优先级最高,如果是,任务切换,
//该任务得以执行,如果不是,则OS_Sched()返回,OSMboxPost的调用函数继续。
return (OS_NO_ERR);
}
if (pevent->OSEventPtr != (void *)0) { /* Make sure mailbox doesn't already have a msg */
OS_EXIT_CRITICAL();
return (OS_MBOX_FULL);//如果邮箱中有消息,表明邮箱满了
}
pevent->OSEventPtr = msg; /* Place message in mailbox */
//将消息放到邮箱中
OS_EXIT_CRITICAL();
return (OS_NO_ERR);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* POST MESSAGE TO A MAILBOX
*
* Description: This function sends a message to a mailbox
*
* Arguments : pevent is a pointer to the event control block associated with the desired mailbox
*
* msg is a pointer to the message to send. You MUST NOT send a NULL pointer.
*
* opt determines the type of POST performed:
* OS_POST_OPT_NONE POST to a single waiting task
* (Identical to OSMboxPost())
* OS_POST_OPT_BROADCAST POST to ALL tasks that are waiting on the mailbox
*
* Returns : OS_NO_ERR The call was successful and the message was sent
* OS_MBOX_FULL If the mailbox already contains a message. You can can only send one
* message at a time and thus, the message MUST be consumed before you
* are allowed to send another one.
* OS_ERR_EVENT_TYPE If you are attempting to post to a non 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
*
* Warning : Interrupts can be disabled for a long time if you do a 'broadcast'. In fact, the
* interrupt disable time is proportional to the number of tasks waiting on the mailbox.
功能更强的向邮箱中发送一则消息
描述:发送一个消息到邮箱
参数:pevent:指向目标邮箱的ECB的指针
msg:将要发送的消息指针,不能发送空指针
opt:决定如下发送模式:
OS_POST_OPT_NONE :发给单片等待任务,这个和OSMboxPost()等同。
OS_POST_OPT_BROADCAST:向在邮箱中等待的所有任务发送。
返回: OS_NO_ERR:消息发送成功
OS_MBOX_FULL :如果邮箱中已经有消息了,一次只能发送一条消息,
在你允许发送另一条前,消息必须用掉
OS_ERR_EVENT_TYPE:如果你要发送到的不是邮箱
OS_ERR_PEVENT_NULL:如果目标ECB是空指针
OS_ERR_POST_NULL_PTR:如果你想发空指针
*********************************************************************************************************
*/
#if OS_MBOX_POST_OPT_EN > 0
INT8U OSMboxPostOpt (OS_EVENT *pevent, void *msg, INT8U opt)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
return (OS_ERR_PEVENT_NULL);
}
if (msg == (void *)0) { /* Make sure we are not posting a NULL pointer */
return (OS_ERR_POST_NULL_PTR);
}//保证传递的不是空指针
if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
return (OS_ERR_EVENT_TYPE);
}//保证是邮箱类型
#endif
OS_ENTER_CRITICAL();
if (pevent->OSEventGrp != 0x00) { /* See if any task pending on mailbox */
//是否有任务在邮箱中挂起
if ((opt & OS_POST_OPT_BROADCAST) != 0x00) { /* Do we need to post msg to ALL waiting tasks ? */
//是不是向所有任务发消息?
while (pevent->OSEventGrp != 0x00) { /* Yes, Post to ALL tasks waiting on mailbox */
//如果是,向所有等待任务发,将所有任务从等待列表中删除
OS_EventTaskRdy(pevent, msg, OS_STAT_MBOX);
}
} else {
OS_EventTaskRdy(pevent, msg, OS_STAT_MBOX); /* No, Post to HPT waiting on mbox */
//如果没有广播,则只有最高优先级进入就绪态,准备运行,
//OS_EventTaskRdy函数只将最高优先级任务从等待列表中删除
}
OS_EXIT_CRITICAL();
OS_Sched(); /* Find highest priority task ready to run */
//任务调度
return (OS_NO_ERR);
}
if (pevent->OSEventPtr != (void *)0) { /* Make sure mailbox doesn't already have a msg */
OS_EXIT_CRITICAL();
return (OS_MBOX_FULL);//如果邮箱中有邮件,返回已经满了。
}
pevent->OSEventPtr = msg; /* Place message in mailbox */
//保存邮箱中的消息
OS_EXIT_CRITICAL();
return (OS_NO_ERR);
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* QUERY A MESSAGE MAILBOX
*
* Description: This function obtains information about a message mailbox.
*
* Arguments : pevent is a pointer to the event control block associated with the desired mailbox
*
* pdata is a pointer to a structure that will contain information about the message
* mailbox.
*
* Returns : OS_NO_ERR The call was successful and the message was sent
* OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non mailbox.
* OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
查询一个邮箱的状态
描述:包含消息邮箱的信息
参数:pevent:指向目标邮箱ECB的指针
pdata:包含消息邮箱信息的结构指针
返回:OS_NO_ERR 调用成功,消息发送成功
* OS_ERR_EVENT_TYPE 你想从非邮箱中得到数据
* OS_ERR_PEVENT_NULL 如果pevent是NULL
*********************************************************************************************************
*/
#if OS_MBOX_QUERY_EN > 0
INT8U OSMboxQuery (OS_EVENT *pevent, OS_MBOX_DATA *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT8U *psrc;
INT8U *pdest;
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
return (OS_ERR_PEVENT_NULL);
}//不合理的pevent
if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
return (OS_ERR_EVENT_TYPE);
}//如果不是邮箱类型
#endif
OS_ENTER_CRITICAL();
pdata->OSEventGrp = pevent->OSEventGrp; /* Copy message mailbox wait list */
//拷贝消息邮箱等待列表
psrc = &pevent->OSEventTbl[0];
pdest = &pdata->OSEventTbl[0];//邮箱中等待事件发生链表
//复制等待任务列表,,之所以使用条件编译产生的内嵌代码,不用循环语句,
//是因为这样代码运行速度更快
#if OS_EVENT_TBL_SIZE > 0
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 1
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 2
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 3
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 4
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 5
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 6
*pdest++ = *psrc++;
#endif
#if OS_EVENT_TBL_SIZE > 7
*pdest = *psrc;
#endif
pdata->OSMsg = pevent->OSEventPtr; /* Get message from mailbox */
//从邮箱中获得消息
OS_EXIT_CRITICAL();
return (OS_NO_ERR);
}
#endif /* OS_MBOX_QUERY_EN */
#endif /* OS_MBOX_EN */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -