μC/OS-II---消息邮箱管理2(os_flag.c)

目录

    • 消息邮箱发送(扩展)
    • 从消息邮箱获取/无等待
    • 消息邮箱状态查询
    • 消息邮箱中断等待
    • 用邮箱作为二值信号量(无)
    • 用邮箱实现延时(无)

μC/OS-II---消息邮箱管理2(os_flag.c)_第1张图片

消息邮箱发送(扩展)

#if OS_MBOX_POST_OPT_EN > 0u
INT8U  OSMboxPostOpt (OS_EVENT  *pevent,
											void      *pmsg,
											INT8U      opt)
{
#if OS_CRITICAL_METHOD == 3u                          /* Allocate storage for CPU status register      */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pevent == (OS_EVENT *)0)                      /* Validate 'pevent'                             */
	{
		return (OS_ERR_PEVENT_NULL);
	}
	
	if (pmsg == (void *)0)                            /* Make sure we are not posting a NULL pointer   */
	{
		return (OS_ERR_POST_NULL_PTR);
	}
	
#endif
	
	if (pevent->OSEventType != OS_EVENT_TYPE_MBOX)    /* Validate event block type                     */
	{
		return (OS_ERR_EVENT_TYPE);
	}
	
	OS_ENTER_CRITICAL();
	
	if (pevent->OSEventGrp != 0u)                     /* See if any task pending on mailbox            */
	{
		if ((opt & OS_POST_OPT_BROADCAST) != 0x00u)   /* Do we need to post msg to ALL waiting tasks ? */
		{
			while (pevent->OSEventGrp != 0u)          /* Yes, Post to ALL tasks waiting on mailbox     */
			{
				(void)OS_EventTaskRdy (pevent, pmsg, OS_STAT_MBOX, OS_STAT_PEND_OK);
			}
		}
		
		else                                          /* No,  Post to HPT waiting on mbox              */
		{
			(void)OS_EventTaskRdy (pevent, pmsg, OS_STAT_MBOX, OS_STAT_PEND_OK);
		}
		
		OS_EXIT_CRITICAL();
		
		if ((opt & OS_POST_OPT_NO_SCHED) == 0u)       /* See if scheduler needs to be invoked          */
		{
			OS_Sched();                               /* Find HPT ready to run                         */
		}
		
		return (OS_ERR_NONE);
	}
	
	if (pevent->OSEventPtr != (void *)0)              /* Make sure mailbox doesn't already have a msg  */
	{
		OS_EXIT_CRITICAL();
		return (OS_ERR_MBOX_FULL);
	}
	
	pevent->OSEventPtr = pmsg;                        /* Place message in mailbox                      */
	OS_EXIT_CRITICAL();
	return (OS_ERR_NONE);
}
#endif

从消息邮箱获取/无等待

#if OS_MBOX_ACCEPT_EN > 0u
void  *OSMboxAccept (OS_EVENT *pevent)
{
	void      *pmsg;
#if OS_CRITICAL_METHOD == 3u                              /* Allocate storage for CPU status register  */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pevent == (OS_EVENT *)0)                          /* Validate 'pevent'                         */
	{
		return ((void *)0);
	}
	
#endif
	
	if (pevent->OSEventType != OS_EVENT_TYPE_MBOX)        /* Validate event block type                 */
	{
		return ((void *)0);
	}
	
	OS_ENTER_CRITICAL();
	pmsg               = pevent->OSEventPtr;
	pevent->OSEventPtr = (void *)0;                       /* Clear the mailbox                         */
	OS_EXIT_CRITICAL();
	return (pmsg);                                        /* Return the message received (or NULL)     */
}
#endif

消息邮箱状态查询

#if OS_MBOX_QUERY_EN > 0u
INT8U  OSMboxQuery (OS_EVENT      *pevent,
										OS_MBOX_DATA  *p_mbox_data)
{
	INT8U       i;
	OS_PRIO    *psrc;
	OS_PRIO    *pdest;
#if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */
	OS_CPU_SR   cpu_sr = 0u;
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pevent == (OS_EVENT *)0)                           /* Validate 'pevent'                        */
	{
		return (OS_ERR_PEVENT_NULL);
	}
	
	if (p_mbox_data == (OS_MBOX_DATA *)0)                  /* Validate 'p_mbox_data'                   */
	{
		return (OS_ERR_PDATA_NULL);
	}
	
#endif
	
	if (pevent->OSEventType != OS_EVENT_TYPE_MBOX)         /* Validate event block type                */
	{
		return (OS_ERR_EVENT_TYPE);
	}
	
	OS_ENTER_CRITICAL();
	p_mbox_data->OSEventGrp = pevent->OSEventGrp;          /* Copy message mailbox wait list           */
	psrc                    = &pevent->OSEventTbl[0];
	pdest                   = &p_mbox_data->OSEventTbl[0];
	
	for (i = 0u; i < OS_EVENT_TBL_SIZE; i++)
	{
		*pdest++ = *psrc++;
	}
	
	p_mbox_data->OSMsg = pevent->OSEventPtr;               /* Get message from mailbox                 */
	OS_EXIT_CRITICAL();
	return (OS_ERR_NONE);
}
#endif 

消息邮箱中断等待

#if OS_MBOX_PEND_ABORT_EN > 0u
INT8U  OSMboxPendAbort (OS_EVENT  *pevent,
												INT8U      opt,
												INT8U     *perr)
{
	INT8U      nbr_tasks;
#if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */
	OS_CPU_SR  cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return (0u);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pevent == (OS_EVENT *)0)                           /* Validate 'pevent'                        */
	{
		*perr = OS_ERR_PEVENT_NULL;
		return (0u);
	}
	
#endif
	
	if (pevent->OSEventType != OS_EVENT_TYPE_MBOX)         /* Validate event block type                */
	{
		*perr = OS_ERR_EVENT_TYPE;
		return (0u);
	}
	
	OS_ENTER_CRITICAL();
	
	if (pevent->OSEventGrp != 0u)                          /* See if any task waiting on mailbox?      */
	{
		nbr_tasks = 0u;
		
		switch (opt)
		{
			case OS_PEND_OPT_BROADCAST:                    /* Do we need to abort ALL waiting tasks?   */
				while (pevent->OSEventGrp != 0u)          /* Yes, ready ALL tasks waiting on mailbox  */
				{
					(void)OS_EventTaskRdy (pevent, (void *)0, OS_STAT_MBOX, OS_STAT_PEND_ABORT);
					nbr_tasks++;
				}
				
				break;
				
			case OS_PEND_OPT_NONE:
			default:                                       /* No,  ready HPT       waiting on mailbox  */
				(void)OS_EventTaskRdy (pevent, (void *)0, OS_STAT_MBOX, OS_STAT_PEND_ABORT);
				nbr_tasks++;
				break;
		}
		
		OS_EXIT_CRITICAL();
		OS_Sched();                                        /* Find HPT ready to run                    */
		*perr = OS_ERR_PEND_ABORT;
		return (nbr_tasks);
	}
	
	OS_EXIT_CRITICAL();
	*perr = OS_ERR_NONE;
	return (0u);                                           /* No tasks waiting on mailbox              */
}
#endif

用邮箱作为二值信号量(无)

用邮箱实现延时(无)

你可能感兴趣的:(μC/OS-II学习,c语言)