⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os_mbox.c

📁 avr ucos 代码 测试环境:source insight WINAVR 4个进程
💻 C
📖 第 1 页 / 共 3 页
字号:
        *err = OS_ERR_EVENT_TYPE;
        return (pevent);//如果事件不是邮箱模式
    }
#endif
    OS_ENTER_CRITICAL();
    if (pevent->OSEventGrp != 0x00) {                      /* See if any tasks waiting on mailbox      */
		//看邮箱中有没有任务在等待
        tasks_waiting = TRUE;                              /* Yes                                      */
    } else {
        tasks_waiting = FALSE;                             /* No                                       */
    }
    switch (opt) {
        case OS_DEL_NO_PEND:                               /* Delete mailbox only if no task waiting   */
			//没有任务等待的情况下才删除邮箱
             if (tasks_waiting == FALSE) {//如果没有任务等待
                 pevent->OSEventType = OS_EVENT_TYPE_UNUSED;//将事件标志为没有用的
                 pevent->OSEventPtr  = OSEventFreeList;    /* Return Event Control Block to free list  */
				 //将ECB返回给空闲列表
                 OSEventFreeList     = pevent;             /* Get next free event control block        */
				 //它指向新的空闲列表
                 OS_EXIT_CRITICAL();
                 *err = OS_NO_ERR;//成功删除
                 return ((OS_EVENT *)0);                   /* Mailbox has been deleted                 */
             } else {
                 OS_EXIT_CRITICAL();
                 *err = OS_ERR_TASK_WAITING;//如果有任务在等待
                 return (pevent);
             }

        case OS_DEL_ALWAYS:                                /* Always delete the mailbox                */
             while (pevent->OSEventGrp != 0x00) {          /* Ready ALL tasks waiting for mailbox      */
                 OS_EventTaskRdy(pevent, (void *)0, OS_STAT_MBOX);
             }//所以等待该信号的任务都进入就绪态,每个任务都以为自己得到了
             //NULL指针,每个任务必须检查返回的指针,看是不是非NULL,另外,
             //当每个任务都进入就绪态,中断是关的,也延长了中断响应时间。
             pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
             pevent->OSEventPtr  = OSEventFreeList;        /* Return Event Control Block to free list  */
             OSEventFreeList     = pevent;                 /* Get next free event control block        */
			 //同上面一样,将ECB返回给空闲列表,并指向新的表头
             OS_EXIT_CRITICAL();
             if (tasks_waiting == TRUE) {                  /* Reschedule only if task(s) were waiting  */
			 	//只有有任务等待的时候才需要任务调度
                 OS_Sched();                               /* Find highest priority task ready to run  */
             }
             *err = OS_NO_ERR;
             return ((OS_EVENT *)0);                       /* Mailbox has been deleted                 */

        default:
             OS_EXIT_CRITICAL();
             *err = OS_ERR_INVALID_OPT;//如果有其它不合理的选项。
             return (pevent);
    }
}
#endif

/*$PAGE*/
/*
*********************************************************************************************************
*                                      PEND ON MAILBOX FOR A MESSAGE
*
* Description: This function waits for a message to be sent to a mailbox
*
* Arguments  : pevent        is a pointer to the event control block associated with the desired mailbox
*
*              timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will
*                            wait for a message to arrive at the mailbox up to the amount of time
*                            specified by this argument.  If you specify 0, however, your task will wait
*                            forever at the specified mailbox or, until a message arrives.
*
*              err           is a pointer to where an error message will be deposited.  Possible error
*                            messages are:
*
*                            OS_NO_ERR           The call was successful and your task received a
*                                                message.
*                            OS_TIMEOUT          A message was not received within the specified timeout
*                            OS_ERR_EVENT_TYPE   Invalid event type
*                            OS_ERR_PEND_ISR     If you called this function from an ISR and the result
*                                                would lead to a suspension.
*                            OS_ERR_PEVENT_NULL  If 'pevent' is a NULL pointer
*
* Returns    : != (void *)0  is a pointer to the message received
*              == (void *)0  if no message was received or,
*                            if 'pevent' is a NULL pointer or,
*                            if you didn't pass the proper pointer to the event control block.
                                                 等待邮箱中的消息
描述:等待将要送到邮箱中的消息
参数:pevent:指向目标邮箱ECB的指针
                 timeout:超时阶段的选项,如果非零,事件将在邮箱中等待消息的到来
                 直到这个值的时间到;如果是零,消息将永远等待,直到消息到来。
                 err:指向将要传递的错误消息指针。可能如下:
                            OS_NO_ERR          操作成功,任务得到消息
*                            OS_TIMEOUT          规定时间内没有收到消息
*                            OS_ERR_EVENT_TYPE  非法事件类型
*                            OS_ERR_PEND_ISR     如果在中断中调用,结果将中止
*                            OS_ERR_PEVENT_NULL  如果pevent 是空指针
*********************************************************************************************************
*/

void  *OSMboxPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
{
#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
    OS_CPU_SR  cpu_sr;
#endif    
    void      *msg;


    if (OSIntNesting > 0) {                           /* See if called from ISR ...                    */
        *err = OS_ERR_PEND_ISR;                       /* ... can't PEND from an ISR                    */
        return ((void *)0);
    }//中断服务子程序不能等待。
#if OS_ARG_CHK_EN > 0
    if (pevent == (OS_EVENT *)0) {                    /* Validate 'pevent'                             */
        *err = OS_ERR_PEVENT_NULL;
        return ((void *)0);//目标邮箱ECB指针为零,不合理
    }
    if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) {  /* Validate event block type                     */
		//事件类型不是邮箱类型
        *err = OS_ERR_EVENT_TYPE;
        return ((void *)0);
    }
#endif
    OS_ENTER_CRITICAL();
    msg = pevent->OSEventPtr;
    if (msg != (void *)0) {                           /* See if there is already a message */
//如果邮箱中有消息,则从中取出消息,返回给调用函数,并将NULL存入邮箱,
//同时返回无错代码给它的调用函数,这个返回结果表示,已由另一个任务或者中断
//服务子程序将信息发送到邮箱中。如果为空的话,调用此函数的任务将进入睡眠状态
//等待另一个任务(可中断服务程序)通过邮箱发送消息,OSMboxPend()允许定义一个
//最长等待时间作为它的参数,防止任务无期限等待
        pevent->OSEventPtr = (void *)0;               /* Clear the mailbox                             */
        OS_EXIT_CRITICAL();
        *err = OS_NO_ERR;
        return (msg);                                 /* Return the message received (or NULL)         */
		//返回得到的消息
    }
    OSTCBCur->OSTCBStat |= OS_STAT_MBOX;              /* Message not available, task will pend         */
	//消息无效,挂起任务,进入睡眠,等待其它邮箱消息唤醒
    OSTCBCur->OSTCBDly   = timeout;                   /* Load timeout in TCB                           */
	//等待时间也放入任务控制块中,该址在OSTimeTick()中被逐次减一
    OS_EventTaskWait(pevent);                         /* Suspend task until event or timeout occurs    */
	//真正将任务进入睡眠,
    OS_EXIT_CRITICAL();
    OS_Sched();                                       /* Find next highest priority task ready to run  */
	//任务调度,下一个优先级的任务运行。
    OS_ENTER_CRITICAL();
    msg = OSTCBCur->OSTCBMsg;//取出消息
    if (msg != (void *)0) {                           /* See if we were given the message              */
        OSTCBCur->OSTCBMsg      = (void *)0;          /* Yes, clear message received                   */
        OSTCBCur->OSTCBStat     = OS_STAT_RDY;
        OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;      /* No longer waiting for event                   */
        OS_EXIT_CRITICAL();
		//是否真正接收到消息,如果是,进入就绪态,不再等待事件,
        *err                    = OS_NO_ERR;
        return (msg);                                 /* Return the message received                   */
		//将收到的消息返回
    }
    OS_EventTO(pevent);                               /* Timed out, Make task ready                    */
	//如果超时,让任务就绪
    OS_EXIT_CRITICAL();
    *err = OS_TIMEOUT;                                /* Indicate that a timeout occured               */
    return ((void *)0);                               /* Return a NULL message                         */
	//返回空指针
}
/*$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.
*
* 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
                                                       向邮箱发送一则消息
描述:向邮箱发送一则消息
参数:pevent:指向目标邮箱的ECB的指针
                 msg:将要发送的消息指针,不能发送空指针
 返回:OS_NO_ERR:消息发送成功
                OS_MBOX_FULL :如果邮箱中已经有消息了,一次只能发送一条消息,
                在你允许发送另一条前,消息必须用掉
                OS_ERR_EVENT_TYPE:如果你要发送到的不是邮箱
                 OS_ERR_PEVENT_NULL:如果目标ECB是空指针
                 OS_ERR_POST_NULL_PTR:如果你想发空指针
*********************************************************************************************************
*/

#if OS_MBOX_POST_EN > 0
INT8U  OSMboxPost (OS_EVENT *pevent, void *msg)
{
#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);
    }//合理的目标ECB
    if (msg == (void *)0) {                           /* Make sure we are not posting a NULL pointer   */
        return (OS_ERR_POST_NULL_PTR);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -