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

📄 os_mutex.s43

📁 IAR project for MSP430 and uC/OS. All configured to start filling with tasks.
💻 S43
📖 第 1 页 / 共 3 页
字号:
	NAME	os_mutex(16)
	RSEG	CODE(1)
	EXTERN	OSEventFreeList
	EXTERN	OSIntNesting
	EXTERN	OSMapTbl
	PUBLIC	OSMutexAccept
	PUBLIC	OSMutexCreate
	PUBLIC	OSMutexDel
	PUBLIC	OSMutexPend
	PUBLIC	OSMutexPost
	PUBLIC	OSMutexQuery
	EXTERN	OSRdyGrp
	EXTERN	OSRdyTbl
	EXTERN	OSTCBCur
	EXTERN	OSTCBPrioTbl
	EXTERN	OS_EventTO
	EXTERN	OS_EventTaskRdy
	EXTERN	OS_EventTaskWait
	EXTERN	OS_EventWaitListInit
	EXTERN	OS_Sched
	EXTERN	?CL430_1_26_L08
	RSEG	CODE
OSMutexAccept:
; 1.	/*
; 2.	*********************************************************************************************************
; 3.	*                                                uC/OS-II
; 4.	*                                          The Real-Time Kernel
; 5.	*                                  MUTUAL EXCLUSION SEMAPHORE MANAGEMENT
; 6.	*
; 7.	*                          (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
; 8.	*                                           All Rights Reserved
; 9.	*
; 10.	* File : OS_MUTEX.C
; 11.	* By   : Jean J. Labrosse
; 12.	*********************************************************************************************************
; 13.	*/
; 14.	
; 15.	#ifndef  OS_MASTER_FILE
; 16.	#include "includes.h"
; 17.	#endif
; 18.	
; 19.	/*
; 20.	*********************************************************************************************************
; 21.	*                                            LOCAL CONSTANTS
; 22.	*********************************************************************************************************
; 23.	*/
; 24.	
; 25.	#define  OS_MUTEX_KEEP_LOWER_8   0x00FF
; 26.	#define  OS_MUTEX_KEEP_UPPER_8   0xFF00
; 27.	
; 28.	#define  OS_MUTEX_AVAILABLE      0x00FF
; 29.	
; 30.	
; 31.	#if OS_MUTEX_EN > 0
; 32.	/*
; 33.	*********************************************************************************************************
; 34.	*                                   ACCEPT MUTUAL EXCLUSION SEMAPHORE
; 35.	*
; 36.	* Description: This  function checks the mutual exclusion semaphore to see if a resource is available.
; 37.	*              Unlike OSMutexPend(), OSMutexAccept() does not suspend the calling task if the resource is
; 38.	*              not available or the event did not occur.
; 39.	*
; 40.	* Arguments  : pevent     is a pointer to the event control block
; 41.	*
; 42.	*              err        is a pointer to an error code which will be returned to your application:
; 43.	*                            OS_NO_ERR          if the call was successful.
; 44.	*                            OS_ERR_EVENT_TYPE  if 'pevent' is not a pointer to a mutex
; 45.	*                            OS_ERR_PEVENT_NULL 'pevent' is a NULL pointer
; 46.	*                            OS_ERR_PEND_ISR     if you called this function from an ISR
; 47.	*
; 48.	* Returns    : == 1       if the resource is available, the mutual exclusion semaphore is acquired
; 49.	*              == 0       a) if the resource is not available
; 50.	*                         b) you didn't pass a pointer to a mutual exclusion semaphore
; 51.	*                         c) you called this function from an ISR
; 52.	*
; 53.	* Warning(s) : This function CANNOT be called from an ISR because mutual exclusion semaphores are
; 54.	*              intended to be used by tasks only.
; 55.	*********************************************************************************************************
; 56.	*/
; 57.	
; 58.	#if OS_MUTEX_ACCEPT_EN > 0
; 59.	INT8U  OSMutexAccept (OS_EVENT *pevent, INT8U *err)
; 60.	{
; 61.	#if OS_CRITICAL_METHOD == 3                            /* Allocate storage for CPU status register     */
; 62.	    OS_CPU_SR  cpu_sr;
; 63.	#endif    
; 64.	    
; 65.	    
; 66.	    if (OSIntNesting > 0) {                            /* Make sure it's not called from an ISR        */
	CMP.B	#0,&OSIntNesting	
	JEQ	(?0057)	
; 67.	        *err = OS_ERR_PEND_ISR;
	MOV.B	#2,0(R14)	
; 68.	        return (0);
	MOV.B	#0,R12	
; 69.	    }
	RET		
?0057:
; 70.	#if OS_ARG_CHK_EN > 0
; 71.	    if (pevent == (OS_EVENT *)0) {                     /* Validate 'pevent'                            */
	CMP	#0,R12	
	JNE	(?0059)	
; 72.	        *err = OS_ERR_PEVENT_NULL;
	MOV.B	#4,0(R14)	
; 73.	        return (0);
; 74.	    }
	RET		
?0059:
; 75.	    if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {  /* Validate event block type                    */
	MOV.B	#4,R13	
	CMP.B	@R12,R13	
	JEQ	(?0061)	
; 76.	        *err = OS_ERR_EVENT_TYPE;
	MOV.B	#1,0(R14)	
; 77.	        return (0);
	MOV.B	#0,R12	
; 78.	    }
	RET		
?0061:
; 79.	#endif                                                     
; 80.	    OS_ENTER_CRITICAL();                                                           /* Get value (0 or 1) of Mutex                  */
	DINT		
; 81.	    if ((pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {     
	MOV	2(R12),R13	
	AND	#255,R13	
	CMP	#255,R13	
	JNE	(?0063)	
; 82.	        pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;   /*      Mask off LSByte (Acquire Mutex)         */
	AND	#65280,2(R12)	
; 83.	        pevent->OSEventCnt |= OSTCBCur->OSTCBPrio;     /*      Save current task priority in LSByte    */
	MOV	&OSTCBCur,R13	
	MOV.B	29(R13),R13	
	BIS	R13,2(R12)	
; 84.	        pevent->OSEventPtr  = (void *)OSTCBCur;        /*      Link TCB of task owning Mutex           */
	MOV	&OSTCBCur,4(R12)	
; 85.	        OS_EXIT_CRITICAL();
	EINT		
; 86.	        *err = OS_NO_ERR;
	MOV.B	#0,0(R14)	
; 87.	        return (1);
	MOV.B	#1,R12	
; 88.	    }
	RET		
?0063:
; 89.	    OS_EXIT_CRITICAL();
	EINT		
; 90.	    *err = OS_NO_ERR;
	MOV.B	#0,0(R14)	
; 91.	    return (0);
	MOV.B	#0,R12	
; 92.	}
	RET		
OSMutexCreate:
; 93.	#endif                                                     
; 94.	
; 95.	/*$PAGE*/
; 96.	/*
; 97.	*********************************************************************************************************
; 98.	*                                  CREATE A MUTUAL EXCLUSION SEMAPHORE
; 99.	*
; 100.	* Description: This function creates a mutual exclusion semaphore.
; 101.	*
; 102.	* Arguments  : prio          is the priority to use when accessing the mutual exclusion semaphore.  In
; 103.	*                            other words, when the semaphore is acquired and a higher priority task
; 104.	*                            attempts to obtain the semaphore then the priority of the task owning the
; 105.	*                            semaphore is raised to this priority.  It is assumed that you will specify
; 106.	*                            a priority that is LOWER in value than ANY of the tasks competing for the
; 107.	*                            mutex.
; 108.	*
; 109.	*              err           is a pointer to an error code which will be returned to your application:
; 110.	*                               OS_NO_ERR           if the call was successful.
; 111.	*                               OS_ERR_CREATE_ISR   if you attempted to create a MUTEX from an ISR
; 112.	*                               OS_PRIO_EXIST       if a task at the priority inheritance priority
; 113.	*                                                   already exist.
; 114.	*                               OS_ERR_PEVENT_NULL  No more event control blocks available.
; 115.	*                               OS_PRIO_INVALID     if the priority you specify is higher that the 
; 116.	*                                                   maximum allowed (i.e. > OS_LOWEST_PRIO)
; 117.	*
; 118.	* Returns    : != (void *)0  is a pointer to the event control clock (OS_EVENT) associated with the
; 119.	*                            created mutex.
; 120.	*              == (void *)0  if an error is detected.
; 121.	*
; 122.	* Note(s)    : 1) The LEAST significant 8 bits of '.OSEventCnt' are used to hold the priority number
; 123.	*                 of the task owning the mutex or 0xFF if no task owns the mutex.
; 124.	*              2) The MOST  significant 8 bits of '.OSEventCnt' are used to hold the priority number
; 125.	*                 to use to reduce priority inversion.
; 126.	*********************************************************************************************************
; 127.	*/
; 128.	
; 129.	OS_EVENT  *OSMutexCreate (INT8U prio, INT8U *err)
; 130.	{
	PUSH	R10	
	PUSH	R11	
	MOV	R14,R10	
; 131.	#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */
; 132.	    OS_CPU_SR  cpu_sr;
; 133.	#endif    
; 134.	    OS_EVENT  *pevent;
; 135.	
; 136.	
; 137.	    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
	CMP.B	#0,&OSIntNesting	
	JEQ	(?0066)	
; 138.	        *err = OS_ERR_CREATE_ISR;                          /* ... can't CREATE mutex from an ISR       */
	MOV.B	#141,0(R10)	
; 139.	        return ((OS_EVENT *)0);
	MOV	#0,R12	
; 140.	    }
	JMP	(?0073)	
?0066:
; 141.	#if OS_ARG_CHK_EN > 0
; 142.	    if (prio >= OS_LOWEST_PRIO) {                          /* Validate PIP                             */
	CMP.B	#12,R12	
	JNC	(?0068)	
; 143.	        *err = OS_PRIO_INVALID;
	MOV.B	#42,0(R10)	
; 144.	        return ((OS_EVENT *)0);
	MOV	#0,R12	
; 145.	    }
	JMP	(?0073)	
?0068:
; 146.	#endif
; 147.	    OS_ENTER_CRITICAL();
	DINT		
; 148.	    if (OSTCBPrioTbl[prio] != (OS_TCB *)0) {               /* Mutex priority must not already exist    */
	MOV.B	R12,R13	
	ADD	R13,R13	
	CMP	#0,OSTCBPrioTbl(R13)	
	JEQ	(?0070)	
; 149.	        OS_EXIT_CRITICAL();                                /* Task already exist at priority ...       */
	EINT		
; 150.	        *err = OS_PRIO_EXIST;                              /* ... inheritance priority                 */
	MOV.B	#40,0(R10)	
; 151.	        return ((OS_EVENT *)0);                            
	MOV	#0,R12	
; 152.	    }
	JMP	(?0073)	
?0070:
; 153.	    OSTCBPrioTbl[prio] = (OS_TCB *)1;                      /* Reserve the table entry                  */
	MOV.B	R12,R13	
	ADD	R13,R13	
	MOV	#1,OSTCBPrioTbl(R13)	
; 154.	    pevent             = OSEventFreeList;                  /* Get next free event control block        */
	MOV	&OSEventFreeList,R11	
; 155.	    if (pevent == (OS_EVENT *)0) {                         /* See if an ECB was available              */
	CMP	#0,R11	
	JNE	(?0072)	
; 156.	        OSTCBPrioTbl[prio] = (OS_TCB *)0;                  /* No, Release the table entry              */
	AND.B	#-1,R12	
	ADD	R12,R12	
	MOV	#0,OSTCBPrioTbl(R12)	
; 157.	        OS_EXIT_CRITICAL();
	EINT		
; 158.	        *err               = OS_ERR_PEVENT_NULL;           /* No more event control blocks             */
	MOV.B	#4,0(R10)	
; 159.	        return (pevent);
	MOV	#0,R12	
; 160.	    }
	JMP	(?0073)	
?0072:
; 161.	    OSEventFreeList     = (OS_EVENT *)OSEventFreeList->OSEventPtr;   /* Adjust the free list           */
	MOV	&OSEventFreeList,R13	
	MOV	4(R13),&OSEventFreeList	
; 162.	    OS_EXIT_CRITICAL();
	EINT		
; 163.	    pevent->OSEventType = OS_EVENT_TYPE_MUTEX;
	MOV.B	#4,0(R11)	
; 164.	    pevent->OSEventCnt  = (prio << 8) | OS_MUTEX_AVAILABLE;/* Resource is available                    */
	AND.B	#-1,R12	
	SWPB	R12	
	BIS	#255,R12	
	MOV	R12,2(R11)	
; 165.	    pevent->OSEventPtr  = (void *)0;                       /* No task owning the mutex                 */
	MOV	#0,4(R11)	
; 166.	    OS_EventWaitListInit(pevent);
	MOV	R11,R12	
	CALL	#OS_EventWaitListInit	
; 167.	    *err                = OS_NO_ERR;
	MOV.B	#0,0(R10)	
; 168.	    return (pevent);
	MOV	R11,R12	
; 169.	}
?0073:
	POP	R11	
	POP	R10	
	RET		
OSMutexDel:
; 170.	
; 171.	/*$PAGE*/
; 172.	/*
; 173.	*********************************************************************************************************
; 174.	*                                          DELETE A MUTEX
; 175.	*
; 176.	* Description: This function deletes a mutual exclusion semaphore and readies all tasks pending on the it.
; 177.	*
; 178.	* Arguments  : pevent        is a pointer to the event control block associated with the desired mutex.
; 179.	*
; 180.	*              opt           determines delete options as follows:
; 181.	*                            opt == OS_DEL_NO_PEND   Delete mutex ONLY if no task pending
; 182.	*                            opt == OS_DEL_ALWAYS    Deletes the mutex even if tasks are waiting.
; 183.	*                                                    In this case, all the tasks pending will be readied.
; 184.	*
; 185.	*              err           is a pointer to an error code that can contain one of the following values:
; 186.	*                            OS_NO_ERR               The call was successful and the mutex was deleted
; 187.	*                            OS_ERR_DEL_ISR          If you attempted to delete the MUTEX from an ISR
; 188.	*                            OS_ERR_INVALID_OPT      An invalid option was specified
; 189.	*                            OS_ERR_TASK_WAITING     One or more tasks were waiting on the mutex
; 190.	*                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a mutex
; 191.	*                            OS_ERR_PEVENT_NULL      If 'pevent' is a NULL pointer.
; 192.	*
; 193.	* Returns    : pevent        upon error
; 194.	*              (OS_EVENT *)0 if the mutex was successfully deleted.
; 195.	*
; 196.	* Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of
; 197.	*                 the mutex MUST check the return code of OSMutexPend().
; 198.	*              2) This call can potentially disable interrupts for a long time.  The interrupt disable
; 199.	*                 time is directly proportional to the number of tasks waiting on the mutex.
; 200.	*              3) Because ALL tasks pending on the mutex will be readied, you MUST be careful because the
; 201.	*                 resource(s) will no longer be guarded by the mutex.
; 202.	*********************************************************************************************************
; 203.	*/
; 204.	
; 205.	#if OS_MUTEX_DEL_EN
; 206.	OS_EVENT  *OSMutexDel (OS_EVENT *pevent, INT8U opt, INT8U *err)
; 207.	{
	PUSH	R10	
	PUSH	R11	
	PUSH	R8	
	MOV	R12,R11	
	MOV	8(SP),R8	
; 208.	#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */
; 209.	    OS_CPU_SR  cpu_sr;
; 210.	#endif    
; 211.	    BOOLEAN    tasks_waiting;
; 212.	    INT8U      pip;
; 213.	
; 214.	
; 215.	    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
	CMP.B	#0,&OSIntNesting	

⌨️ 快捷键说明

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