📄 os_sem.s
字号:
.module os_sem.c
.area text(rom, con, rel)
.dbfile E:\ypt050912\XIAZAI\xinzai\UCOSAVR128\Source\os_sem.c
.dbfunc e OSSemAccept _OSSemAccept fi
.dbstruct 0 8 os_event
.dbfield 0 OSEventType c
.dbfield 1 OSEventPtr pV
.dbfield 3 OSEventCnt i
.dbfield 5 OSEventGrp c
.dbfield 6 OSEventTbl A[2:2]c
.dbend
; cpu_sr -> R22
; cnt -> R20,R21
; pevent -> R10,R11
.even
_OSSemAccept::
xcall push_gset3
movw R10,R16
.dbline -1
.dbline 41
; /*
; *********************************************************************************************************
; * uC/OS-II
; * The Real-Time Kernel
; * SEMAPHORE MANAGEMENT
; *
; * (c) Copyright 1992-2005, Jean J. Labrosse, Weston, FL
; * All Rights Reserved
; *
; * File : OS_SEM.C
; * By : Jean J. Labrosse
; * Version : V2.80
; *********************************************************************************************************
; */
;
; #ifndef OS_MASTER_FILE
; #include <ucos_ii.h>
; #endif
;
; #if OS_SEM_EN > 0
; /*
; *********************************************************************************************************
; * ACCEPT SEMAPHORE
; *
; * Description: This function checks the semaphore to see if a resource is available or, if an event
; * occurred. Unlike OSSemPend(), OSSemAccept() does not suspend the calling task if the
; * resource is not available or the event did not occur.
; *
; * Arguments : pevent is a pointer to the event control block
; *
; * Returns : > 0 if the resource is available or the event did not occur the semaphore is
; * decremented to obtain the resource.
; * == 0 if the resource is not available or the event did not occur or,
; * if 'pevent' is a NULL pointer or,
; * if you didn't pass a pointer to a semaphore
; *********************************************************************************************************
; */
;
; #if OS_SEM_ACCEPT_EN > 0
; INT16U OSSemAccept (OS_EVENT *pevent)
; {
.dbline 44
; INT16U cnt;
; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
; OS_CPU_SR cpu_sr = 0;
clr R22
.dbline 54
; #endif
;
;
;
; #if OS_ARG_CHK_EN > 0
; if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
; return (0);
; }
; #endif
; if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
movw R30,R10
ldd R24,z+0
cpi R24,3
breq L2
.dbline 54
.dbline 55
; return (0);
clr R16
clr R17
xjmp L1
L2:
.dbline 57
; }
; OS_ENTER_CRITICAL();
xcall _OS_CPU_SR_Save
mov R22,R16
.dbline 58
; cnt = pevent->OSEventCnt;
movw R30,R10
ldd R20,z+3
ldd R21,z+4
.dbline 59
; if (cnt > 0) { /* See if resource is available */
cpi R20,0
cpc R20,R21
breq L4
X0:
.dbline 59
.dbline 60
; pevent->OSEventCnt--; /* Yes, decrement semaphore and notify caller */
movw R24,R10
adiw R24,3
movw R30,R24
ldd R24,z+0
ldd R25,z+1
sbiw R24,1
std z+1,R25
std z+0,R24
.dbline 61
; }
L4:
.dbline 62
; OS_EXIT_CRITICAL();
mov R16,R22
xcall _OS_CPU_SR_Restore
.dbline 63
; return (cnt); /* Return semaphore count */
movw R16,R20
.dbline -2
L1:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r cpu_sr 22 c
.dbsym r cnt 20 i
.dbsym r pevent 10 pS[os_event]
.dbend
.dbfunc e OSSemCreate _OSSemCreate fpS[os_event]
; cpu_sr -> R10
; pevent -> R20,R21
; cnt -> R22,R23
.even
_OSSemCreate::
xcall push_gset3
movw R22,R16
.dbline -1
.dbline 86
; }
; #endif
;
; /*$PAGE*/
; /*
; *********************************************************************************************************
; * CREATE A SEMAPHORE
; *
; * Description: This function creates a semaphore.
; *
; * Arguments : cnt is the initial value for the semaphore. If the value is 0, no resource is
; * available (or no event has occurred). You initialize the semaphore to a
; * non-zero value to specify how many resources are available (e.g. if you have
; * 10 resources, you would initialize the semaphore to 10).
; *
; * Returns : != (void *)0 is a pointer to the event control clock (OS_EVENT) associated with the
; * created semaphore
; * == (void *)0 if no event control blocks were available
; *********************************************************************************************************
; */
;
; OS_EVENT *OSSemCreate (INT16U cnt)
; {
.dbline 89
; OS_EVENT *pevent;
; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
; OS_CPU_SR cpu_sr = 0;
clr R10
.dbline 94
; #endif
;
;
;
; if (OSIntNesting > 0) { /* See if called from ISR ... */
clr R2
lds R3,_OSIntNesting
cp R2,R3
brsh L7
.dbline 94
.dbline 95
; return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
clr R16
clr R17
xjmp L6
L7:
.dbline 97
; }
; OS_ENTER_CRITICAL();
xcall _OS_CPU_SR_Save
mov R10,R16
.dbline 98
; pevent = OSEventFreeList; /* Get next free event control block */
lds R20,_OSEventFreeList
lds R21,_OSEventFreeList+1
.dbline 99
; if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
cpi R20,0
cpc R20,R21
breq L9
X1:
.dbline 99
.dbline 100
; OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
lds R30,_OSEventFreeList
lds R31,_OSEventFreeList+1
ldd R2,z+1
ldd R3,z+2
sts _OSEventFreeList+1,R3
sts _OSEventFreeList,R2
.dbline 101
; }
L9:
.dbline 102
; OS_EXIT_CRITICAL();
mov R16,R10
xcall _OS_CPU_SR_Restore
.dbline 103
; if (pevent != (OS_EVENT *)0) { /* Get an event control block */
cpi R20,0
cpc R20,R21
breq L11
X2:
.dbline 103
.dbline 104
; pevent->OSEventType = OS_EVENT_TYPE_SEM;
ldi R24,3
movw R30,R20
std z+0,R24
.dbline 105
; pevent->OSEventCnt = cnt; /* Set semaphore value */
std z+4,R23
std z+3,R22
.dbline 106
; pevent->OSEventPtr = (void *)0; /* Unlink from ECB free list */
clr R2
clr R3
movw R30,R20
std z+2,R3
std z+1,R2
.dbline 111
; #if OS_EVENT_NAME_SIZE > 1
; pevent->OSEventName[0] = '?'; /* Unknown name */
; pevent->OSEventName[1] = OS_ASCII_NUL;
; #endif
; OS_EventWaitListInit(pevent); /* Initialize to 'nobody waiting' on sem. */
movw R16,R20
xcall _OS_EventWaitListInit
.dbline 112
; }
L11:
.dbline 113
; return (pevent);
movw R16,R20
.dbline -2
L6:
xcall pop_gset3
.dbline 0 ; func end
ret
.dbsym r cpu_sr 10 c
.dbsym r pevent 20 pS[os_event]
.dbsym r cnt 22 i
.dbend
.dbfunc e OSSemDel _OSSemDel fpS[os_event]
; tasks_waiting -> R14
; pevent_return -> R20,R21
; cpu_sr -> R12
; err -> R10,R11
; opt -> R20
; pevent -> R22,R23
.even
_OSSemDel::
xcall push_gset5
mov R20,R18
movw R22,R16
sbiw R28,1
ldd R10,y+11
ldd R11,y+12
.dbline -1
.dbline 156
; }
;
; /*$PAGE*/
; /*
; *********************************************************************************************************
; * DELETE A SEMAPHORE
; *
; * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
; *
; * Arguments : pevent is a pointer to the event control block associated with the desired
; * semaphore.
; *
; * opt determines delete options as follows:
; * opt == OS_DEL_NO_PEND Delete semaphore ONLY if no task pending
; * opt == OS_DEL_ALWAYS Deletes the semaphore even if tasks are waiting.
; * In this case, all the tasks pending will be readied.
; *
; * err is a pointer to an error code that can contain one of the following values:
; * OS_NO_ERR The call was successful and the semaphore was deleted
; * OS_ERR_DEL_ISR If you attempted to delete the semaphore from an ISR
; * OS_ERR_INVALID_OPT An invalid option was specified
; * OS_ERR_TASK_WAITING One or more tasks were waiting on the semaphore
; * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a semaphore
; * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
; *
; * Returns : pevent upon error
; * (OS_EVENT *)0 if the semaphore was successfully deleted.
; *
; * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
; * the semaphore MUST check the return code of OSSemPend().
; * 2) OSSemAccept() callers will not know that the intended semaphore has been deleted unless
; * they check 'pevent' to see that it's a NULL pointer.
; * 3) This call can potentially disable interrupts for a long time. The interrupt disable
; * time is directly proportional to the number of tasks waiting on the semaphore.
; * 4) Because ALL tasks pending on the semaphore will be readied, you MUST be careful in
; * applications where the semaphore is used for mutual exclusion because the resource(s)
; * will no longer be guarded by the semaphore.
; *********************************************************************************************************
; */
;
; #if OS_SEM_DEL_EN > 0
; OS_EVENT *OSSemDel (OS_EVENT *pevent, INT8U opt, INT8U *err)
; {
.dbline 160
; BOOLEAN tasks_waiting;
; OS_EVENT *pevent_return;
; #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
; OS_CPU_SR cpu_sr = 0;
clr R12
.dbline 174
; #endif
;
;
;
; #if OS_ARG_CHK_EN > 0
; if (err == (INT8U *)0) { /* Validate 'err' */
; return (pevent);
; }
; if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
; *err = OS_ERR_PEVENT_NULL;
; return (pevent);
; }
; #endif
; if (pevent->OSEventType != OS_EVENT_TYPE_SEM) { /* Validate event block type */
movw R30,R22
ldd R24,z+0
cpi R24,3
breq L14
.dbline 174
.dbline 175
; *err = OS_ERR_EVENT_TYPE;
ldi R24,1
movw R30,R10
std z+0,R24
.dbline 176
; return (pevent);
movw R16,R22
xjmp L13
L14:
.dbline 178
; }
; if (OSIntNesting > 0) { /* See if called from ISR ... */
clr R2
lds R3,_OSIntNesting
cp R2,R3
brsh L16
.dbline 178
.dbline 179
; *err = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
ldi R24,140
movw R30,R10
std z+0,R24
.dbline 180
; return (pevent);
movw R16,R22
xjmp L13
L16:
.dbline 182
; }
; OS_ENTER_CRITICAL();
xcall _OS_CPU_SR_Save
mov R12,R16
.dbline 183
; if (pevent->OSEventGrp != 0) { /* See if any tasks waiting on semaphore */
movw R30,R22
ldd R2,z+5
tst R2
breq L18
.dbline 183
.dbline 184
; tasks_waiting = TRUE; /* Yes */
clr R14
inc R14
.dbline 185
xjmp L19
L18:
.dbline 185
; } else {
.dbline 186
; tasks_waiting = FALSE; /* No */
clr R14
.dbline 187
; }
L19:
.dbline 188
clr R21
cpi R20,0
cpc R20,R21
breq L23
X3:
cpi R20,1
ldi R30,0
cpc R21,R30
brne X5
xjmp L28
X5:
xjmp L20
X4:
.dbline 188
; switch (opt) {
L23:
.dbline 190
; case OS_DEL_NO_PEND: /* Delete semaphore only if no task waiting */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -