• μC/OS-II---消息邮箱管理1(os_mbox.c)


    在这里插入图片描述

    消息邮箱创建

    OS_EVENT  *OSMboxCreate (void *pmsg)
    {
    	OS_EVENT  *pevent;
    #if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
    	OS_CPU_SR  cpu_sr = 0u;
    #endif
    #ifdef OS_SAFETY_CRITICAL_IEC61508
    	
    	if (OSSafetyCriticalStartFlag == OS_TRUE)
    	{
    		OS_SAFETY_CRITICAL_EXCEPTION();
    		return ((OS_EVENT *)0);
    	}
    	
    #endif
    	
    	if (OSIntNesting > 0u)                       /* See if called from ISR ...                         */
    	{
    		return ((OS_EVENT *)0);                  /* ... can't CREATE from an ISR                       */
    	}
    	
    	OS_ENTER_CRITICAL();
    	pevent = OSEventFreeList;                    /* Get next free event control block                  */
    	
    	if (OSEventFreeList != (OS_EVENT *)0)        /* See if pool of free ECB pool was empty             */
    	{
    		OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
    	}
    	
    	OS_EXIT_CRITICAL();
    	
    	if (pevent != (OS_EVENT *)0)
    	{
    		pevent->OSEventType    = OS_EVENT_TYPE_MBOX;
    		pevent->OSEventCnt     = 0u;
    		pevent->OSEventPtr     = pmsg;           /* Deposit message in event control block             */
    #if OS_EVENT_NAME_EN > 0u
    		pevent->OSEventName    = (INT8U *) (void *)"?";
    #endif
    		OS_EventWaitListInit (pevent);
    	}
    	
    	return (pevent);                             /* Return pointer to event control block              */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    消息邮箱删除

    #if OS_MBOX_DEL_EN > 0u
    OS_EVENT  *OSMboxDel (OS_EVENT  *pevent,
    											INT8U      opt,
    											INT8U     *perr)
    {
    	BOOLEAN    tasks_waiting;
    	OS_EVENT  *pevent_return;
    #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 ((OS_EVENT *)0);
    	}
    	
    #endif
    #if OS_ARG_CHK_EN > 0u
    	
    	if (pevent == (OS_EVENT *)0)                           /* Validate 'pevent'                        */
    	{
    		*perr = OS_ERR_PEVENT_NULL;
    		return (pevent);
    	}
    	
    #endif
    	
    	if (pevent->OSEventType != OS_EVENT_TYPE_MBOX)         /* Validate event block type                */
    	{
    		*perr = OS_ERR_EVENT_TYPE;
    		return (pevent);
    	}
    	
    	if (OSIntNesting > 0u)                                 /* See if called from ISR ...               */
    	{
    		*perr = OS_ERR_DEL_ISR;                            /* ... can't DELETE from an ISR             */
    		return (pevent);
    	}
    	
    	OS_ENTER_CRITICAL();
    	
    	if (pevent->OSEventGrp != 0u)                          /* See if any tasks waiting on mailbox      */
    	{
    		tasks_waiting = OS_TRUE;                           /* Yes                                      */
    	}
    	
    	else
    	{
    		tasks_waiting = OS_FALSE;                          /* No                                       */
    	}
    	
    	switch (opt)
    	{
    		case OS_DEL_NO_PEND:                               /* Delete mailbox only if no task waiting   */
    			if (tasks_waiting == OS_FALSE)
    			{
    #if OS_EVENT_NAME_EN > 0u
    				pevent->OSEventName = (INT8U *) (void *)"?";
    #endif
    				pevent->OSEventType = OS_EVENT_TYPE_UNUSED;
    				pevent->OSEventPtr  = OSEventFreeList;    /* Return Event Control Block to free list  */
    				pevent->OSEventCnt  = 0u;
    				OSEventFreeList     = pevent;             /* Get next free event control block        */
    				OS_EXIT_CRITICAL();
    				*perr               = OS_ERR_NONE;
    				pevent_return       = (OS_EVENT *)0;      /* Mailbox has been deleted                 */
    			}
    			
    			else
    			{
    				OS_EXIT_CRITICAL();
    				*perr               = OS_ERR_TASK_WAITING;
    				pevent_return       = pevent;
    			}
    			
    			break;
    			
    		case OS_DEL_ALWAYS:                                /* Always delete the mailbox                */
    			while (pevent->OSEventGrp != 0u)              /* Ready ALL tasks waiting for mailbox      */
    			{
    				(void)OS_EventTaskRdy (pevent, (void *)0, OS_STAT_MBOX, OS_STAT_PEND_ABORT);
    			}
    			
    #if OS_EVENT_NAME_EN > 0u
    			pevent->OSEventName    = (INT8U *) (void *)"?";
    #endif
    			pevent->OSEventType    = OS_EVENT_TYPE_UNUSED;
    			pevent->OSEventPtr     = OSEventFreeList;     /* Return Event Control Block to free list  */
    			pevent->OSEventCnt     = 0u;
    			OSEventFreeList        = pevent;              /* Get next free event control block        */
    			OS_EXIT_CRITICAL();
    			
    			if (tasks_waiting == OS_TRUE)                 /* Reschedule only if task(s) were waiting  */
    			{
    				OS_Sched();                               /* Find highest priority task ready to run  */
    			}
    			
    			*perr         = OS_ERR_NONE;
    			pevent_return = (OS_EVENT *)0;                /* Mailbox has been deleted                 */
    			break;
    			
    		default:
    			OS_EXIT_CRITICAL();
    			*perr         = OS_ERR_INVALID_OPT;
    			pevent_return = pevent;
    			break;
    	}
    	
    	return (pevent_return);
    }
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113

    等待邮箱中的消息

    void  *OSMboxPend (OS_EVENT  *pevent,
    									 INT32U     timeout,
    									 INT8U     *perr)
    {
    	void      *pmsg;
    #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 ((void *)0);
    	}
    	
    #endif
    #if OS_ARG_CHK_EN > 0u
    	
    	if (pevent == (OS_EVENT *)0)                      /* Validate 'pevent'                             */
    	{
    		*perr = OS_ERR_PEVENT_NULL;
    		return ((void *)0);
    	}
    	
    #endif
    	
    	if (pevent->OSEventType != OS_EVENT_TYPE_MBOX)    /* Validate event block type                     */
    	{
    		*perr = OS_ERR_EVENT_TYPE;
    		return ((void *)0);
    	}
    	
    	if (OSIntNesting > 0u)                            /* See if called from ISR ...                    */
    	{
    		*perr = OS_ERR_PEND_ISR;                      /* ... can't PEND from an ISR                    */
    		return ((void *)0);
    	}
    	
    	if (OSLockNesting > 0u)                           /* See if called with scheduler locked ...       */
    	{
    		*perr = OS_ERR_PEND_LOCKED;                   /* ... can't PEND when locked                    */
    		return ((void *)0);
    	}
    	
    	OS_ENTER_CRITICAL();
    	pmsg = pevent->OSEventPtr;
    	
    	if (pmsg != (void *)0)                            /* See if there is already a message             */
    	{
    		pevent->OSEventPtr = (void *)0;               /* Clear the mailbox                             */
    		OS_EXIT_CRITICAL();
    		*perr = OS_ERR_NONE;
    		return (pmsg);                                /* Return the message received (or NULL)         */
    	}
    	
    	OSTCBCur->OSTCBStat     |= OS_STAT_MBOX;          /* Message not available, task will pend         */
    	OSTCBCur->OSTCBStatPend  = OS_STAT_PEND_OK;
    	OSTCBCur->OSTCBDly       = timeout;               /* Load timeout in TCB                           */
    	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();
    	
    	switch (OSTCBCur->OSTCBStatPend)                  /* See if we timed-out or aborted                */
    	{
    		case OS_STAT_PEND_OK:
    			pmsg =  OSTCBCur->OSTCBMsg;
    			*perr =  OS_ERR_NONE;
    			break;
    			
    		case OS_STAT_PEND_ABORT:
    			pmsg = (void *)0;
    			*perr =  OS_ERR_PEND_ABORT;               /* Indicate that we aborted                      */
    			break;
    			
    		case OS_STAT_PEND_TO:
    		default:
    			OS_EventTaskRemove (OSTCBCur, pevent);
    			pmsg = (void *)0;
    			*perr =  OS_ERR_TIMEOUT;                  /* Indicate that we didn't get event within TO   */
    			break;
    	}
    	
    	OSTCBCur->OSTCBStat          =  OS_STAT_RDY;      /* Set   task  status to ready                   */
    	OSTCBCur->OSTCBStatPend      =  OS_STAT_PEND_OK;  /* Clear pend  status                            */
    	OSTCBCur->OSTCBEventPtr      = (OS_EVENT  *)0;    /* Clear event pointers                          */
    #if (OS_EVENT_MULTI_EN > 0u)
    	OSTCBCur->OSTCBEventMultiPtr = (OS_EVENT **)0;
    #endif
    	OSTCBCur->OSTCBMsg           = (void      *)0;    /* Clear  received message                       */
    	OS_EXIT_CRITICAL();
    	return (pmsg);                                    /* Return received message                       */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    向邮箱发送一则消息

    #if OS_MBOX_POST_EN > 0u
    INT8U  OSMboxPost (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 (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            */
    	{
    		/* Ready HPT waiting on event                    */
    		(void)OS_EventTaskRdy (pevent, pmsg, OS_STAT_MBOX, OS_STAT_PEND_OK);
    		OS_EXIT_CRITICAL();
    		OS_Sched();                                   /* Find highest priority task 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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    MR混合现实模拟消防安全演练场景实训
    3. Java 数据类型
    Linux C语言开发-D10控制语句if
    PE文件-C++-MFC-IDA-逆向分析-x32dbg
    【Linux系统KVM虚拟机实战】LVM逻辑卷之磁盘扩容
    【OpenCV 例程200篇】229. 特征描述之 LBP 算子比较(skimage)
    LeetCode刷题(python版)——Topic60排列序列
    用“价值”的视角来看安全:《构建新型网络形态下的网络空间安全体系》
    学成在线第一天-课程内容管理服务搭建以及查询课程接口设计
    高等教育学:学生与教师
  • 原文地址:https://blog.csdn.net/weixin_45880844/article/details/134406060