]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/RTOS2/src/cmsis_os2_Message.txt
CMSIS-RTOS2: added documentation split for CM0-7 support only.
[cmsis] / CMSIS / DoxyGen / RTOS2 / src / cmsis_os2_Message.txt
1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 //  ==== Message Queue Management ====
3 /** 
4 @addtogroup CMSIS_RTOS_Message Message Queue
5 @ingroup CMSIS_RTOS
6 @brief Exchange messages between threads in a FIFO-like operation.
7 @details 
8 \b Message \b passing is another basic communication model between threads. In the message passing model, one thread sends
9 data explicitly, while another thread receives it. The operation is more like some kind of I/O rather than a direct access to
10 information to be shared. In CMSIS-RTOS, this mechanism is called s \b message \b queue. The data is passed from one thread
11 to another in a FIFO-like operation. Using message queue functions, you can control, send, receive, or wait for messages. The
12 data to be passed can be of integer or pointer type:
13
14 \image html "MessageQueue.png" "CMSIS-RTOS Message Queue"
15
16 Compared to a \ref CMSIS_RTOS_PoolMgmt, message queues are less efficient in general, but solve a broader range of problems.
17 Sometimes, threads do not have a common address space or the use of shared memory raises problems, such as mutual exclusion.
18
19 \note The functions \ref osMessageQueuePut, \ref osMessageQueueGet, \ref osMessageQueueGetCapacity,
20 \ref osMessageQueueGetMsgSize, \ref osMessageQueueGetCount, \ref osMessageQueueGetSpace can be called from
21 \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
22 \note Refer to \ref msgQueueConfig for RTX5 configuration options.
23 @{
24 */
25 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
26 /**
27 \def osMessageQueueId_t
28 \details
29 Returned by:
30 - \ref osMessageQueueNew
31 */
32
33 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
34 /** 
35 \struct osMessageQueueAttr_t
36 \details
37 Specifies the following attributes for the \ref osMessageQueueNew function.
38 */
39
40 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
41 /** 
42 \fn osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr)
43 \details
44 The function \ref osMessageQueueNew creates and initializes a  message queue object.
45 The function returns a message queue object identifier or \token{NULL} in case of an error. 
46
47 The function can be called after kernel initialization with \ref osKernelInitialize. It is possible to 
48 create message queue objects before the RTOS kernel is started with \ref osKernelStart.
49
50 The total amount of memory required for the message queue data is at least <code>msg_count * msg_size</code>.
51 The \em msg_size is rounded up to a double even number to ensure 32-bit alignment of the memory blocks.
52
53 The memory blocks allocated from the message queue have a fixed size defined with the parameter \c msg_size.
54
55 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
56
57 <b>Code Example</b>
58
59 Refer to \ref osMessageQueuePut
60 */
61
62 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
63 /**
64 \fn const char *osMessageQueueGetName (osMessageQueueId_t mq_id)
65 \details
66 The function \b osMessageQueueGetName returns the pointer to the name string of the message queue identified by parameter \a
67 mq_id or \token{NULL} in case of an error.
68
69 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
70 */
71
72 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
73 /** 
74 \fn osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
75 \details
76 The blocking function \b osMessageQueuePut puts the message pointed to by \a msg_ptr into the the message queue specified
77 by parameter \a mq_id. The parameter \a msg_prio is used to sort message according their priority (higher numbers indicate
78 a higher priority) on insertion.
79
80 The parameter \a timeout specifies how long the system waits to put the message into the queue. While the system waits, the
81 thread that is calling this function is put into the \ref ThreadStates "BLOCKED" state. The parameter \ref CMSIS_RTOS_TimeOutValue "timeout"
82 can have the following values:
83  - when \a timeout is \token{0}, the function returns instantly (i.e. try semantics).
84  - when \a timeout is set to \b osWaitForever the function will wait for an infinite time until the message is delivered (i.e. wait semantics).
85  - all other values specify a time in kernel ticks for a timeout (i.e. timed-wait semantics).
86
87 Possible \ref osStatus_t return values:
88  - \em osOK: the message has been put into the queue.
89  - \em osErrorTimeout: the message could not be put into the queue in the given time (wait-timed semantics).
90  - \em osErrorResource: not enough space in the queue (try semantics).
91  - \em osErrorParameter: the parameter \a mq_id is incorrect, non-zero timeout specified in an ISR.
92
93 \note May be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines" if the parameter \a timeout is set to
94 \token{0}.
95
96 <b>Code Example:</b>
97 \code
98 #include "cmsis_os2.h"                                             // CMSIS RTOS header file
99  
100 /*----------------------------------------------------------------------------
101  *      Message Queue creation & usage
102  *---------------------------------------------------------------------------*/
103   
104 void Thread_MsgQueue1 (void *argument);                            // thread function 1
105 void Thread_MsgQueue2 (void *argument);                            // thread function 2
106 osThreadId_t tid_Thread_MsgQueue1;                                 // thread id 1
107 osThreadId_t tid_Thread_MsgQueue2;                                 // thread id 2
108  
109 #define MSGQUEUE_OBJECTS      16                                   // number of Message Queue Objects
110  
111 typedef struct {                                                   // object data type
112   uint8_t Buf[32];
113   uint8_t Idx;
114 } MSGQUEUE_OBJ_t;
115  
116 osMessageQueueId_t mid_MsgQueue;                                   // message queue id
117  
118 int Init_MsgQueue (void) {
119   
120   mid_MsgQueue = osMessageQueueNew(MSGQUEUE_OBJECTS, sizeof(MSGQUEUE_OBJ_t), NULL);
121   if (!mid_MsgQueue) {
122     ; // Message Queue object not created, handle failure
123   }
124   
125   tid_Thread_MsgQueue1 = osThreadNew (Thread_MsgQueue1, NULL, NULL);
126   if (!tid_Thread_MsgQueue1) return(-1);
127   tid_Thread_MsgQueue2 = osThreadNew (Thread_MsgQueue2, NULL, NULL);
128   if (!tid_Thread_MsgQueue2) return(-1);
129   
130   return(0);
131 }
132  
133 void Thread_MsgQueue1 (void *argument) {
134   MSGQUEUE_OBJ_t msg;
135
136   while (1) {
137     ; // Insert thread code here...
138     msg.Buf[0] = 0x55;                                           // do some work...
139     msg.Idx    = 0;
140     osMessageQueuePut (mid_MsgQueue, &msg, 0, NULL);
141     osThreadYield ();                                              // suspend thread
142   }
143 }
144  
145 void Thread_MsgQueue2 (void *argument) {
146
147   MSGQUEUE_OBJ_t msg;
148   osStatus_t status;
149
150   while (1) {
151     ; // Insert thread code here...
152     status = osMessageQueueGet (mid_MsgQueue, &msg, NULL, NULL);  // wait for message
153     if (status == osOK) {
154         ; // process data
155     }
156   }
157 }
158 \endcode
159 */
160
161 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
162 /** 
163 \fn osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout)
164 \details
165 The function \b osMessageQueueGet retrieves a message from the message queue specified by the parameter \a mq_id and saves it
166 to the buffer pointed to by the parameter \a msg_ptr. The message priority is stored to parameter \a msg_prio if not token{NULL}.
167
168 The parameter \a timeout specifies how long the system waits to retrieve the message from the queue. While the system waits,
169 the thread that is calling this function is put into the \ref ThreadStates "BLOCKED" state. The parameter
170 \ref CMSIS_RTOS_TimeOutValue "timeout" can have the following values:
171  - when \a timeout is \token{0}, the function returns instantly (i.e. try semantics).
172  - when \a timeout is set to \b osWaitForever the function will wait for an infinite time until the message is retrieved (i.e. wait semantics).
173  - all other values specify a time in kernel ticks for a timeout (i.e. timed-wait semantics).
174
175 Possible \ref osStatus_t return values:
176  - \em osOK: the message has been retrieved from the queue.
177  - \em osErrorTimeout: the message could not be retrieved from the queue in the given time (timed-wait semantics).
178  - \em osErrorResource: nothing to get from the queue (try semantics).
179  - \em osErrorParameter: the parameter \a mq_id is incorrect, non-zero timeout specified in an ISR.
180
181 \note May be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines" if the parameter \a timeout is set to
182 \token{0}.
183
184 <b>Code Example</b>
185
186 Refer to \ref osMessageQueuePut
187 */
188
189 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
190 /** 
191 \fn uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id)
192 \details
193 The function \b osMessageQueueGetCapacity returns the maximum number of messages in the message queue object specified by
194 parameter \a mq_id or \token{0} in case of an error.
195
196 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
197 */
198
199 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
200 /** 
201 \fn uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id)
202 \details
203 The function \b osMessageQueueGetMsgSize returns the maximum message size in bytes for the message queue object specified by
204 parameter \a mq_id or \token{0} in case of an error.
205
206 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
207 */
208
209 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
210 /** 
211 \fn uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id)
212 \details
213 The function \b osMessageQueueGetCount returns the number of queued messages in the message queue object specified by
214 parameter \a mq_id or \token{0} in case of an error.
215
216 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
217 */
218
219 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
220 /** 
221 \fn uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id)
222 \details
223 The function \b osMessageQueueGetSpace returns the number available slots for messages in the message queue object specified
224 by parameter \a mq_id or \token{0} in case of an error.
225
226 \note This function may be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
227 */
228
229 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
230 /** 
231 \fn osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id)
232 \details
233 The function \b osMessageQueueReset resets the message queue specified by the parameter \a mq_id.
234
235 Possible \ref osStatus_t return values:
236  - \em osOK: the message queue has been rest.
237  - \em osErrorParameter: parameter \em mq_id is \token{NULL} or invalid.
238  - \em osErrorResource: the message queue specified by parameter \a mq_id is in an invalid message queue state.
239  - \em osErrorISR: \b osMessageQueueReset cannot be called from interrupt service routines.
240
241 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
242 */
243
244 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
245 /** 
246 \fn osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id)
247 \details
248 The function \b osMessageQueueDelete deletes a message queue object specified by parameter \a mq_id. It releases internal
249 memory obtained for message queue handling. After this call, the \a mq_id is no longer valid and cannot be used. The
250 message queue may be created again using the function \ref osMessageQueueNew.
251
252 Possible \ref osStatus_t return values:
253  - \em osOK: the message queue object has been deleted.
254  - \em osErrorParameter: parameter \em mq_id is \token{NULL} or invalid.
255  - \em osErrorResource: the message queue specified by parameter \a mq_id is in an invalid message queue state.
256  - \em osErrorISR: \b osMessageQueueDelete cannot be called from interrupt service routines.
257
258 \note This function \b cannot be called from \ref CMSIS_RTOS_ISR_Calls "Interrupt Service Routines".
259 */
260 /// @}
261
262 // these struct members must stay outside the group to avoid double entries in documentation
263 /**
264 \var osMessageQueueAttr_t::attr_bits
265 \details
266 Reserved for future use (must be set to '0' for future compatibility).
267
268 \var osMessageQueueAttr_t::cb_mem
269 \details
270 Pointer to a memory for the message queue control block object. Refer to \ref StaticObjectMemory for more information.
271
272 Default: \token{NULL} to use \ref CMSIS_RTOS_MemoryMgmt_Automatic for the message queue control block.
273
274 \var osMessageQueueAttr_t::cb_size
275 \details
276 The size (in bytes) of memory block passed with \ref cb_mem. For RTX, the minimum value is defined with \ref osRtxMessageQueueCbSize (higher values are permitted).
277
278 Default: \token{0} as the default is no memory provided with \ref cb_mem.
279
280 \var osMessageQueueAttr_t::name
281 \details
282 Pointer to a constant string with a human readable name (displayed during debugging) of the message queue object.
283
284 Default: \token{NULL} no name specified.
285
286 \var osMessageQueueAttr_t::mq_mem
287 \details
288 Pointer to a memory for the message queue data. Refer to \ref StaticObjectMemory for more information.
289
290 Default: \token{NULL} to use \ref CMSIS_RTOS_MemoryMgmt_Automatic for the memory pool data.
291
292 \var osMessageQueueAttr_t::mq_size
293 \details
294 The size (in bytes) of memory block passed with \ref mq_mem. The minimum memory block size is <code>msg_count * msg_size</code> (parameters of the \ref osMessageQueueNew function). The \em msg_size is rounded up to a double even number to ensure 32-bit alignment of the memory blocks.
295
296 Default: 0 as the default is no memory provided with \ref mq_mem.
297 */