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

📄 os_mutex.ls1

📁 UCOS在51上的移植,绝对经典!可以较少开发人员的工作量
💻 LS1
📖 第 1 页 / 共 5 页
字号:
                     173     ; #if OS_MUTEX_ACCEPT_EN > 0
                     174     ; INT8U  OSMutexAccept (OS_EVENT *pevent, INT8U *err)LG_REENTRANT
                     175     ; {
                     176     ; #if OS_CRITICAL_METHOD == 3                            /* Allocate storage for CPU status
                              register     */
A51 MACRO ASSEMBLER  OS_MUTEX                                                             05/17/2005 11:19:51 PAGE     4

                     177     ;     OS_CPU_SR  cpu_sr;
                     178     ; #endif    
                     179     ;     
                     180     ;     
                     181     ;     if (OSIntNesting > 0) {                            /* Make sure it's not called from 
                             an ISR        */
                     182     ;         *err = OS_ERR_PEND_ISR;
                     183     ;         return (0);
                     184     ;     }
                     185     ; #if OS_ARG_CHK_EN > 0
                     186     ;     if (pevent == (OS_EVENT *)0) {                     /* Validate 'pevent'              
                                           */
                     187     ;         *err = OS_ERR_PEVENT_NULL;
                     188     ;         return (0);
                     189     ;     }
                     190     ;     if (pevent->OSEventType != OS_EVENT_TYPE_MUTEX) {  /* Validate event block type      
                                           */
                     191     ;         *err = OS_ERR_EVENT_TYPE;
                     192     ;         return (0);
                     193     ;     }
                     194     ; #endif                                                     
                     195     ;     OS_ENTER_CRITICAL();                                                         /* Get v
                             alue (0 or 1) of Mutex                  */
                     196     ;     if ((pevent->OSEventCnt & OS_MUTEX_KEEP_LOWER_8) == OS_MUTEX_AVAILABLE) {     
                     197     ;         pevent->OSEventCnt &= OS_MUTEX_KEEP_UPPER_8;   /*      Mask off LSByte (Acquire M
                             utex)         */
                     198     ;         pevent->OSEventCnt |= OSTCBCur->OSTCBPrio;     /*      Save current task priority
                              in LSByte    */
                     199     ;         pevent->OSEventPtr  = (void *)OSTCBCur;        /*      Link TCB of task owning Mu
                             tex           */
                     200     ;         OS_EXIT_CRITICAL();
                     201     ;         *err = OS_NO_ERR;
                     202     ;         return (1);
                     203     ;     }
                     204     ;     OS_EXIT_CRITICAL();
                     205     ;     *err = OS_NO_ERR;
                     206     ;     return (0);
                     207     ; }
                     208     ; #endif                                                     
                     209     ; 
                     210     ; /*$PAGE*/
                     211     ; /*
                     212     ; *****************************************************************************************
                             ****************
                     213     ; *                                  CREATE A MUTUAL EXCLUSION SEMAPHORE
                     214     ; *
                     215     ; * Description: This function creates a mutual exclusion semaphore.
                     216     ; *
                     217     ; * Arguments  : prio          is the priority to use when accessing the mutual exclusion s
                             emaphore.  In
                     218     ; *                            other words, when the semaphore is acquired and a higher pri
                             ority task
                     219     ; *                            attempts to obtain the semaphore then the priority of the ta
                             sk owning the
                     220     ; *                            semaphore is raised to this priority.  It is assumed that yo
                             u will specify
                     221     ; *                            a priority that is LOWER in value than ANY of the tasks comp
                             eting for the
                     222     ; *                            mutex.
                     223     ; *
                     224     ; *              err           is a pointer to an error code which will be returned to your
                              application:
                     225     ; *                               OS_NO_ERR           if the call was successful.
                     226     ; *                               OS_ERR_CREATE_ISR   if you attempted to create a MUTEX fr
                             om an ISR
                     227     ; *                               OS_PRIO_EXIST       if a task at the priority inheritance
A51 MACRO ASSEMBLER  OS_MUTEX                                                             05/17/2005 11:19:51 PAGE     5

                              priority
                     228     ; *                                                   already exist.
                     229     ; *                               OS_ERR_PEVENT_NULL  No more event control blocks availabl
                             e.
                     230     ; *                               OS_PRIO_INVALID     if the priority you specify is higher
                              that the 
                     231     ; *                                                   maximum allowed (i.e. > OS_LOWEST_PRI
                             O)
                     232     ; *
                     233     ; * Returns    : != (void *)0  is a pointer to the event control clock (OS_EVENT) associate
                             d with the
                     234     ; *                            created mutex.
                     235     ; *              == (void *)0  if an error is detected.
                     236     ; *
                     237     ; * Note(s)    : 1) The LEAST significant 8 bits of '.OSEventCnt' are used to hold the prio
                             rity number
                     238     ; *                 of the task owning the mutex or 0xFF if no task owns the mutex.
                     239     ; *              2) The MOST  significant 8 bits of '.OSEventCnt' are used to hold the prio
                             rity number
                     240     ; *                 to use to reduce priority inversion.
                     241     ; *****************************************************************************************
                             ****************
                     242     ; */
                     243     ; 
                     244     ; OS_EVENT  *OSMutexCreate (INT8U prio, INT8U *err)LG_REENTRANT
                     245     ; {
                     246     ; #if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU st
                             atus register */
                     247     ;     OS_CPU_SR  cpu_sr;
                     248     ; #endif    
                     249     ;     OS_EVENT  *pevent;
                     250     ; 
                     251     ; 
                     252     ;     if (OSIntNesting > 0) {                                /* See if called from ISR ... 
                                           */
                     253     ;         *err = OS_ERR_CREATE_ISR;                          /* ... can't CREATE mutex from
                              an ISR       */
                     254     ;         return ((OS_EVENT *)0);
                     255     ;     }
                     256     ; #if OS_ARG_CHK_EN > 0
                     257     ;     if (prio >= OS_LOWEST_PRIO) {                          /* Validate PIP               
                                           */
                     258     ;         *err = OS_PRIO_INVALID;
                     259     ;         return ((OS_EVENT *)0);
                     260     ;     }
                     261     ; #endif
                     262     ;     OS_ENTER_CRITICAL();
                     263     ;     if (OSTCBPrioTbl[prio] != (OS_TCB *)0) {               /* Mutex priority must not alr
                             eady exist    */
                     264     ;         OS_EXIT_CRITICAL();                                /* Task already exist at prior
                             ity ...       */
                     265     ;         *err = OS_PRIO_EXIST;                              /* ... inheritance priority   
                                           */
                     266     ;         return ((OS_EVENT *)0);                            
                     267     ;     }
                     268     ;     OSTCBPrioTbl[prio] = (OS_TCB *)1;                      /* Reserve the table entry    
                                           */
                     269     ;     pevent             = OSEventFreeList;                  /* Get next free event control
                              block        */
                     270     ;     if (pevent == (OS_EVENT *)0) {                         /* See if an ECB was available
                                           */
                     271     ;         OSTCBPrioTbl[prio] = (OS_TCB *)0;                  /* No, Release the table entry
                                           */
                     272     ;         OS_EXIT_CRITICAL();
                     273     ;         *err               = OS_ERR_PEVENT_NULL;           /* No more event control block
                             s             */
A51 MACRO ASSEMBLER  OS_MUTEX                                                             05/17/2005 11:19:51 PAGE     6

                     274     ;         return (pevent);
                     275     ;     }
                     276     ;     OSEventFreeList     = (OS_EVENT *)OSEventFreeList->OSEventPtr;   /* Adjust the free l
                             ist           */
                     277     ;     OS_EXIT_CRITICAL();
                     278     ;     pevent->OSEventType = OS_EVENT_TYPE_MUTEX;
                     279     ;     pevent->OSEventCnt  = (prio << 8) | OS_MUTEX_AVAILABLE;/* Resource is available      
                                           */
                     280     ;     pevent->OSEventPtr  = (void *)0;                       /* No task owning the mutex   
                                           */
                     281     ;     OS_EventWaitListInit(pevent);
                     282     ;     *err                = OS_NO_ERR;
                     283     ;     return (pevent);
                     284     ; }
                     285     ; 
                     286     ; /*$PAGE*/
                     287     ; /*
                     288     ; *****************************************************************************************
                             ****************
                     289     ; *                                          DELETE A MUTEX
                     290     ; *
                     291     ; * Description: This function deletes a mutual exclusion semaphore and readies all tasks p
                             ending on the it.
                     292     ; *
                     293     ; * Arguments  : pevent        is a pointer to the event control block associated with the 
                             desired mutex.
                     294     ; *
                     295     ; *              opt           determines delete options as follows:
                     296     ; *                            opt == OS_DEL_NO_PEND   Delete mutex ONLY if no task pending
                     297     ; *                            opt == OS_DEL_ALWAYS    Deletes the mutex even if tasks are 
                             waiting.
                     298     ; *                                                    In this case, all the tasks pending 
                             will be readied.
                     299     ; *
                     300     ; *              err           is a pointer to an error code that can contain one of the fo
                             llowing values:
                     301     ; *                            OS_NO_ERR               The call was successful and the mute
                             x was deleted
                     302     ; *                            OS_ERR_DEL_ISR          If you attempted to delete the MUTEX
                              from an ISR
                     303     ; *                            OS_ERR_INVALID_OPT      An invalid option was specified
                     304     ; *                            OS_ERR_TASK_WAITING     One or more tasks were waiting on th
                             e mutex
                     305     ; *                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a mu
                             tex
                     306     ; *                            OS_ERR_PEVENT_NULL      If 'pevent' is a NULL pointer.
                     307     ; *
                     308     ; * Returns    : pevent        upon error
                     309     ; *              (OS_EVENT *)0 if the mutex was successfully deleted.
                     310     ; *
                     311     ; * Note(s)    : 1) This function must be used with care.  Tasks that would normally expect
                              the presence of
                     312     ; *                 the mutex MUST check the return code of OSMutexPend().
                     313     ; *              2) This call can potentially disable interrupts for a long time.  The inte
                             rrupt disable

⌨️ 快捷键说明

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