📄 os_mbox.lst
字号:
ANSI-C/cC++ Compiler for HC08 V-5.0.12 ICG, Oct 6 2000
1: /*
2: *********************************************************************************************************
3: * uC/OS-II
4: * The Real-Time Kernel
5: * MESSAGE MAILBOX MANAGEMENT
6: *
7: * (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
8: * All Rights Reserved
9: *
10: * V2.00
11: *
12: * File : OS_MBOX.C
13: * By : Jean J. Labrosse
14: *********************************************************************************************************
15: */
16:
17: #include "includes.h"
18:
19: #if OS_MBOX_EN
20: /*
21: *********************************************************************************************************
22: * ACCEPT MESSAGE FROM MAILBOX
23: *
24: * Description: This function checks the mailbox to see if a message is available. Unlike OSMboxPend(),
25: * OSMboxAccept() does not suspend the calling task if a message is not available.
26: *
27: * Arguments : pevent is a pointer to the event control block
28: *
29: * Returns : != (void *)0 is the message in the mailbox if one is available. The mailbox is cleared
30: * so the next time OSMboxAccept() is called, the mailbox will be empty.
31: * == (void *)0 if the mailbox is empty or if you didn't pass the proper event pointer.
32: *********************************************************************************************************
33: */
34:
35: void *OSMboxAccept (OS_EVENT *pevent)
36: {
37: void *msg;
38:
39:
40: OS_ENTER_CRITICAL();
41: if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
42: OS_EXIT_CRITICAL();
43: return ((void *)0);
44: }
45: msg = pevent->OSEventPtr;
46: if (msg != (void *)0) { /* See if there is already a message */
47: pevent->OSEventPtr = (void *)0; /* Clear the mailbox */
48: }
49: OS_EXIT_CRITICAL();
50: return (msg); /* Return the message received (or NULL) */
51: }
52: /*$PAGE*/
53: /*
54: *********************************************************************************************************
55: * CREATE A MESSAGE MAILBOX
56: *
57: * Description: This function creates a message mailbox if free event control blocks are available.
58: *
59: * Arguments : msg is a pointer to a message that you wish to deposit in the mailbox. If
60: * you set this value to the NULL pointer (i.e. (void *)0) then the mailbox
61: * will be considered empty.
62: *
63: * Returns : != (void *)0 is a pointer to the event control clock (OS_EVENT) associated with the
64: * created mailbox
65: * == (void *)0 if no event control blocks were available
66: *********************************************************************************************************
67: */
68:
69: OS_EVENT *OSMboxCreate (void *msg)
70: {
71: OS_EVENT *pevent;
72:
73:
74: OS_ENTER_CRITICAL();
75: pevent = OSEventFreeList; /* Get next free event control block */
76: if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
77: OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
78: }
79: OS_EXIT_CRITICAL();
80: if (pevent != (OS_EVENT *)0) {
81: pevent->OSEventType = OS_EVENT_TYPE_MBOX;
82: pevent->OSEventPtr = msg; /* Deposit message in event control block */
83: OSEventWaitListInit(pevent);
84: }
85: return (pevent); /* Return pointer to event control block */
86: }
87: /*$PAGE*/
88: /*
89: *********************************************************************************************************
90: * PEND ON MAILBOX FOR A MESSAGE
91: *
92: * Description: This function waits for a message to be sent to a mailbox
93: *
94: * Arguments : pevent is a pointer to the event control block associated with the desired mailbox
95: *
96: * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
97: * wait for a message to arrive at the mailbox up to the amount of time
98: * specified by this argument. If you specify 0, however, your task will wait
99: * forever at the specified mailbox or, until a message arrives.
100: *
101: * err is a pointer to where an error message will be deposited. Possible error
102: * messages are:
103: *
104: * OS_NO_ERR The call was successful and your task received a message.
105: * OS_TIMEOUT A message was not received within the specified timeout
106: * OS_ERR_EVENT_TYPE Invalid event type
107: * OS_ERR_PEND_ISR If you called this function from an ISR and the result
108: * would lead to a suspension.
109: *
110: * Returns : != (void *)0 is a pointer to the message received
111: * == (void *)0 if no message was received or you didn't pass the proper pointer to the
112: * event control block.
113: *********************************************************************************************************
114: */
115:
116: void *OSMboxPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
117: {
118: void *msg;
119:
120:
121: OS_ENTER_CRITICAL();
122: if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
123: OS_EXIT_CRITICAL();
124: *err = OS_ERR_EVENT_TYPE;
125: return ((void *)0);
126: }
127: msg = pevent->OSEventPtr;
128: if (msg != (void *)0) { /* See if there is already a message */
129: pevent->OSEventPtr = (void *)0; /* Clear the mailbox */
130: OS_EXIT_CRITICAL();
131: *err = OS_NO_ERR;
132: } else if (OSIntNesting > 0) { /* See if called from ISR ... */
133: OS_EXIT_CRITICAL(); /* ... can't PEND from an ISR */
134: *err = OS_ERR_PEND_ISR;
135: } else {
136: OSTCBCur->OSTCBStat |= OS_STAT_MBOX; /* Message not available, task will pend */
137: OSTCBCur->OSTCBDly = timeout; /* Load timeout in TCB */
138: OSEventTaskWait(pevent); /* Suspend task until event or timeout occurs */
139: OS_EXIT_CRITICAL();
140: OSSched(); /* Find next highest priority task ready to run */
141: OS_ENTER_CRITICAL();
142: if ((msg = OSTCBCur->OSTCBMsg) != (void *)0) { /* See if we were given the message */
143: OSTCBCur->OSTCBMsg = (void *)0; /* Yes, clear message received */
144: OSTCBCur->OSTCBStat = OS_STAT_RDY;
145: OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* No longer waiting for event */
146: OS_EXIT_CRITICAL();
147: *err = OS_NO_ERR;
148: } else if (OSTCBCur->OSTCBStat & OS_STAT_MBOX) { /* If status is not OS_STAT_RDY, timed out */
149: OSEventTO(pevent); /* Make task ready */
150: OS_EXIT_CRITICAL();
151: msg = (void *)0; /* Set message contents to NULL */
152: *err = OS_TIMEOUT; /* Indicate that a timeout occured */
153: } else {
154: msg = pevent->OSEventPtr; /* Message received */
155: pevent->OSEventPtr = (void *)0; /* Clear the mailbox */
156: OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;
157: OS_EXIT_CRITICAL();
158: *err = OS_NO_ERR;
159: }
160: }
161: return (msg); /* Return the message received (or NULL) */
162: }
163: /*$PAGE*/
164: /*
165: *********************************************************************************************************
166: * POST MESSAGE TO A MAILBOX
167: *
168: * Description: This function sends a message to a mailbox
169: *
170: * Arguments : pevent is a pointer to the event control block associated with the desired mailbox
171: *
172: * msg is a pointer to the message to send. You MUST NOT send a NULL pointer.
173: *
174: * Returns : OS_NO_ERR The call was successful and the message was sent
175: * OS_MBOX_FULL If the mailbox already contains a message. You can can only send one
176: * message at a time and thus, the message MUST be consumed before you are
177: * allowed to send another one.
178: * OS_ERR_EVENT_TYPE If you are attempting to post to a non mailbox.
179: *********************************************************************************************************
180: */
181:
182: INT8U OSMboxPost (OS_EVENT *pevent, void *msg)
183: {
184: OS_ENTER_CRITICAL();
185: if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
186: OS_EXIT_CRITICAL();
187: return (OS_ERR_EVENT_TYPE);
188: }
189: if (pevent->OSEventGrp) { /* See if any task pending on mailbox */
190: OSEventTaskRdy(pevent, msg, OS_STAT_MBOX); /* Ready highest priority task waiting on event */
191: OS_EXIT_CRITICAL();
192: OSSched(); /* Find highest priority task ready to run */
193: return (OS_NO_ERR);
194: } else {
195: if (pevent->OSEventPtr != (void *)0) { /* Make sure mailbox doesn't already have a msg */
196: OS_EXIT_CRITICAL();
197: return (OS_MBOX_FULL);
198: } else {
199: pevent->OSEventPtr = msg; /* Place message in mailbox */
200: OS_EXIT_CRITICAL();
201: return (OS_NO_ERR);
202: }
203: }
204: }
205: /*$PAGE*/
206: /*
207: *********************************************************************************************************
208: * QUERY A MESSAGE MAILBOX
209: *
210: * Description: This function obtains information about a message mailbox.
211: *
212: * Arguments : pevent is a pointer to the event control block associated with the desired mailbox
213: *
214: * pdata is a pointer to a structure that will contain information about the message
215: * mailbox.
216: *
217: * Returns : OS_NO_ERR The call was successful and the message was sent
218: * OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non mailbox.
219: *********************************************************************************************************
220: */
221:
222: INT8U OSMboxQuery (OS_EVENT *pevent, OS_MBOX_DATA *pdata)
223: {
224: INT8U i;
225: INT8U *psrc;
226: INT8U *pdest;
227:
228:
229: OS_ENTER_CRITICAL();
230: if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
231: OS_EXIT_CRITICAL();
232: return (OS_ERR_EVENT_TYPE);
233: }
234: pdata->OSEventGrp = pevent->OSEventGrp; /* Copy message mailbox wait list */
235: psrc = &pevent->OSEventTbl[0];
236: pdest = &pdata->OSEventTbl[0];
237: for (i = 0; i < OS_EVENT_TBL_SIZE; i++) {
238: *pdest++ = *psrc++;
239: }
240: pdata->OSMsg = pevent->OSEventPtr; /* Get message from mailbox */
241: OS_EXIT_CRITICAL();
242: return (OS_NO_ERR);
243: }
244: #endif
245:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -