• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __CMSIS_OS_H__
16 #define __CMSIS_OS_H__
17 
18 #ifdef __cplusplus
19 extern "C"{
20 #endif
21 
22 ///< @b osCMSIS identifies the CMSIS-RTOS API version.
23 #if (CMSIS_OS_VER == 2)
24     #define osCMSIS 0x20001U    ///< API version (main[31:16].sub[15:0])
25     #include "cmsis_os2.h"
26 #else
27     #define osCMSIS 0x10001U    ///< API version (main[31:16].sub[15:0])
28     #include <stdint.h>
29     #include <stddef.h>
30 #endif
31 
32 ///< OS
33 /**@defgroup cmsis_os CMSIS OS Module.
34 * @{
35 * @ingroup cmsis
36 * @brief 标准CMSIS OS接口使用.
37 */
38 #if (osCMSIS < 0x20000U)
39 typedef enum {
40   osPriorityIdle          = -3,         ///< Priority: idle (lowest)
41   osPriorityLow           = -2,         ///< Priority: low
42   osPriorityBelowNormal   = -1,         ///< Priority: below normal
43   osPriorityNormal        =  0,         ///< Priority: normal (default)
44   osPriorityAboveNormal   = +1,         ///< Priority: above normal
45   osPriorityHigh          = +2,         ///< Priority: high
46   osPriorityRealtime      = +3,         ///< Priority: realtime (highest)
47   osPriorityError         = 0x84,       ///< System cannot determine priority or illegal priority.
48   osPriorityReserved      = 0x7FFFFFFF  ///< Prevents enum down-size compiler optimization.
49 } osPriority;
50 #else
51 typedef osPriority_t osPriority;
52 #endif
53 
54 ///< Status code values returned by CMSIS-RTOS functions.
55 #if (osCMSIS < 0x20000U)
56 typedef enum {
57   osOK                    =    0,       ///< Function completed; no error or event occurred.
58   osEventSignal           = 0x08,       ///< Function completed; signal event occurred.
59   osEventMessage          = 0x10,       ///< Function completed; message event occurred.
60   osEventMail             = 0x20,       ///< Function completed; mail event occurred.
61   osEventTimeout          = 0x40,       ///< Function completed; timeout occurred.
62   osErrorParameter        = 0x80,       ///< Parameter error: a mandatory parameter was missing or specified an incorrect object.
63   osErrorResource         = 0x81,       ///< Resource not available: a specified resource was not available.
64   osErrorTimeoutResource  = 0xC1,       ///< Resource not available within given time: a specified resource was not available within the timeout period.
65   osErrorISR              = 0x82,       ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
66   osErrorISRRecursive     = 0x83,       ///< Function called multiple times from ISR with same object.
67   osErrorPriority         = 0x84,       ///< System cannot determine priority or thread has illegal priority.
68   osErrorNoMemory         = 0x85,       ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
69   osErrorValue            = 0x86,       ///< Value of a parameter is out of range.
70   osErrorOS               = 0xFF,       ///< Unspecified RTOS error: run-time error but no other error message fits.
71   osStatusReserved        = 0x7FFFFFFF  ///< Prevents enum down-size compiler optimization.
72 } osStatus;
73 #else
74 typedef osStatus_t              osStatus;
75 #define osEventSignal           (0x08)
76 #define osEventMessage          (0x10)
77 #define osEventMail             (0x20)
78 #define osEventTimeout          (0x40)
79 #define osErrorOS               osError
80 #define osErrorTimeoutResource  osErrorTimeout
81 #define osErrorISRRecursive     (-126)
82 #define osErrorValue            (-127)
83 #define osErrorPriority         (-128)
84 #define os_status_reserved      osStatusReserved
85 #endif
86 
87 // The stack space occupied is mainly dependent on the underling C standard library
88 #if defined(__GNUC__) || defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__ICCARM__)
89 #    define WORDS_STACK_SIZE   512
90 #elif defined(TOOLCHAIN_ARM_MICRO)
91 #    define WORDS_STACK_SIZE   128
92 #endif
93 
94 #define DEFAULT_STACK_SIZE         (WORDS_STACK_SIZE*4)
95 
96 /// \note CAN BE CHANGED: \b osFeature_xxx identifies RTOS features.
97 #define osFeature_MainThread  0         ///< main thread      1=main can be thread, 0=not available
98 #define osFeature_Signals     16U       ///< maximum number of Signal Flags available per thread
99 #define osFeature_Semaphore   65535U    ///< maximum count for \ref osSemaphoreCreate function
100 #define osFeature_Wait        0         ///< osWait function: 1=available, 0=not available
101 #define osFeature_SysTick     1         ///< osKernelSysTick functions: 1=available, 0=not available
102 #define osFeature_Pool        1         ///< Memory Pools:    1=available, 0=not available
103 #define osFeature_MessageQ    1         ///< Message Queues:  1=available, 0=not available
104 #define osFeature_MailQ       1         ///< Mail Queues:     1=available, 0=not available
105 
106 #define osRtxSemaphoreTokenLimit 65535U ///< maximum number of tokens per semaphore
107 /**
108  * @brief Message ID identifies the message queue.
109  * @note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
110  */
111 typedef void *osMessageQId;
112 
113 /**
114  * @brief Mail ID identifies the mail queue.
115  * @note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
116  */
117 typedef void* osMailQId;
118 
119 ///< Event structure contains detailed information about an event.
120 typedef struct {
121   osStatus       status;     ///< status code: event or error information
122   union {
123     uint32_t     v;          ///< message as 32-bit value
124     void         *p;         ///< message or mail as void pointer
125     int32_t      signals;    ///< signal flags
126   } value;                   ///< event value
127   union {
128     osMailQId    mail_id;    ///< mail id obtained by \ref osMailCreate
129     osMessageQId message_id; ///< message id obtained by \ref osMessageCreate
130   } def;                     ///< event definition
131 } osEvent;
132 
133 /** @} cmsis_os */
134 
135 /**@defgroup cmsis_kernel CMSIS Kernel Module.
136 * @{
137 * @ingroup cmsis
138 * @brief 标准CMSIS Kernel 接口使用.
139 */
140 #if (osCMSIS < 0x20000U)
141 /**
142  * @brief Initialize the RTOS Kernel for creating objects.
143  * @return status code that indicates the execution status of the function.
144  */
145 osStatus osKernelInitialize (void);
146 
147 /**
148  * @brief Start the RTOS Kernel scheduler.
149  * @return status code that indicates the execution status of the function.
150  */
151 osStatus osKernelStart (void);
152 
153 /**
154  * @brief Check if the RTOS kernel is already started.
155  * @return 0 RTOS is not started, 1 RTOS is started.
156  */
157 int32_t osKernelRunning(void);
158 #endif
159 
160 #if (osCMSIS < 0x20000U)
161 
162 ///< The RTOS kernel system timer frequency in Hz.
163 ///< @note Reflects the system timer setting and is typically defined in a configuration file.
164 #define osKernelSysTickFrequency 100000000
165 
166 /**
167  * @brief Get the RTOS kernel system timer counter.
168  * @return RTOS kernel system timer as 32-bit value
169  */
170 uint32_t osKernelSysTick (void);
171 
172 #define osKernelSysTickFrq() osKernelSysTickFrequency
173 #else
174 //#define  osKernelSysTick osKernelGetSysTimerCount
175 //#define osKernelSysTickFrq() osKernelGetSysTimerFreq()
176 #define osKernelSysTick osKernelGetTickCount
177 #define osKernelSysTickFrq() osKernelGetTickFreq()
178 #endif
179 
180 /**
181  * @brief Convert a microseconds value to a RTOS kernel system timer value.
182  * @param         microsec     time value in microseconds.
183  * @return time value normalized to the \ref osKernelSysTickFrequency
184  */
185 #define osKernelSysTickMicroSec(microsec) (((uint64_t)microsec * osKernelSysTickFrq()) / 1000000)
186 /** @} cmsis_kernel */
187 
188 
189 /**@defgroup cmsis_thread CMSIS thread Module.
190 * @{
191 * @ingroup cmsis
192 * @brief 标准CMSIS thread接口使用.
193 */
194 typedef void (*os_pthread)(void const* args);
195 
196 #if (osCMSIS < 0x20000U)
197 typedef void*        osThreadId;
198 #else
199 typedef osThreadId_t osThreadId;
200 #endif
201 
202 #if (osCMSIS < 0x20000U)
203 typedef struct os_thread_def {
204   os_pthread     pthread;   ///< start address of thread function
205   osPriority     tpriority; ///< initial thread priority
206   uint32_t       instances; ///< maximum number of instances of that thread function
207   uint32_t       stacksize; ///< stack size requirements in bytes; 0 is default stack size
208 } osThreadDef_t;
209 #else
210 typedef struct os_thread_def {
211   os_pthread     pthread;   ///< start address of thread function
212   osThreadAttr_t attr;      ///< thread attributes
213 } osThreadDef_t;
214 #endif
215 
216 #if (osCMSIS < 0x20000U)
217 #define osThreadDef(name, priority, instances, stacksz) \
218     const osThreadDef_t os_thread_##name = {(name),(priority),(instances),(stacksz)}
219 #else
220 /* liteos does not support to use ext stack buf, only need to specify the stack size of thread */
221 #define osThreadDef(name,priority,instances,stacksz,task_name) \
222     const osThreadDef_t os_thread_##name = {(name),{task_name,osThreadDetached,NULL,0U,NULL,8*((stacksz+7)/8),(priority),0U,0U}}
223 /*  uint64_t os_thread_stack_##name [(8*((stacksz+7)/8))/sizeof(uint64_t)]; \
224     const osThreadDef_t os_thread_##name = {(name),{task_name,osThreadDetached,NULL,0U,os_thread_stack_##name,8*((stacksz+7)/8),(priority),0U,0U}}*/
225 #endif
226 
227 #define osThread(name) &os_thread_##name
228 
229 /**
230  * @brief Create a thread and add it to Active Threads and set it to state READY.
231  * @param[in]     thread_def    thread definition referenced with \ref osThread.
232  * @param[in]     argument      pointer that is passed to the thread function as start argument.
233  * @return thread ID for reference by other functions or NULL in case of error.
234  */
235 osThreadId osThreadCreate(const osThreadDef_t* thread_def, void* argument);
236 
237 #if (osCMSIS < 0x20000U)
238 /**
239  * @brief Return the thread ID of the current running thread.
240  * @return thread ID for reference by other functions or NULL in case of error.
241  */
242 osThreadId osThreadGetId(void);
243 
244 /**
245  * @brief Change priority of a thread.
246  * @param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
247  * @param[in]     priority      new priority value for the thread function.
248  * @return status code that indicates the execution status of the function.
249  */
250 osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority);
251 
252 /**
253  * @brief Get current priority of a thread.
254  * @param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
255  * @return current priority value of the specified thread.
256  */
257 osPriority osThreadGetPriority(osThreadId thread_id);
258 
259 /**
260  * @brief Pass control to next thread that is in state \b READY.
261  * @return status code that indicates the execution status of the function.
262  */
263 osStatus osThreadYield (void);
264 
265 /**
266  * @brief Terminate execution of a thread.
267  * @param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
268  * @return status code that indicates the execution status of the function.
269  */
270 osStatus osThreadTerminate(osThreadId thread_id);
271 #else
272 const char *osGetThreadName(void);
273 #endif
274 
275 int osGetThreadIntId(void);
276 void os_error_str(const char* fmt, ...);
277 void osThreadExit(void);
278 
279 /**
280  * @brief Wait for Timeout (Time Delay).
281  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue "time delay" value
282  * @return status code that indicates the execution status of the function.
283  */
284 #if (osCMSIS < 0x20000U)
285 osStatus osDelay (uint32_t millisec);
286 #endif
287 
288 /**
289  * @brief Wait for Signal, Message, Mail, or Timeout.
290  * @brief param[in] millisec          \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
291  * @return event that contains signal, message, or mail information or error code.
292  */
293 osEvent osWait (uint32_t millisec);
294 /** @} cmsis_thread */
295 
296 /**@defgroup cmsis_signal CMSIS Signal Module.
297 * @{
298 * @ingroup cmsis
299 * @brief 标准CMSIS Signal 接口使用.
300 */
301 /**
302  * @brief Set the specified Signal Flags of an active thread.
303  * @param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
304  * @param[in]     signals       specifies the signal flags of the thread that should be set.
305  * @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters.
306  */
307 int32_t osSignalSet (osThreadId thread_id, int32_t signals);
308 
309 /**
310  * @brief Clear the specified Signal Flags of an active thread.
311  * @param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
312  * @param[in]     signals       specifies the signal flags of the thread that shall be cleared.
313  * @return previous signal flags of the specified thread or 0x80000000 in case of incorrect parameters or call from ISR.
314  */
315 int32_t osSignalClear (osThreadId thread_id, int32_t signals);
316 
317 /**
318  * @brief Wait for one or more Signal Flags to become signaled for the current \b RUNNING thread.
319  * @param[in]     signals       wait until all specified signal flags set or 0 for any single signal flag.
320  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
321  * @return event flag information or error code.
322  */
323 osEvent osSignalWait (int32_t signals, uint32_t millisec);
324 /** @} cmsis_signal */
325 
326 /**@defgroup cmsis_mutex CMSIS Mutex Module.
327 * @{
328 * @ingroup cmsis
329 * @brief 标准CMSIS Mutex 接口使用.
330 */
331 /**
332  * @brief Mutex Definition structure contains setup information for a mutex.
333  * @note CAN BE CHANGED: \b os_mutex_def is implementation specific in every CMSIS-RTOS.
334  */
335 #if (osCMSIS < 0x20000U)
336 typedef struct os_mutex_def {
337   uint32_t dummy;   ///< dummy value
338 }osMutexDef_t;
339 #else
340 typedef osMutexAttr_t osMutexDef_t;
341 #endif
342 
343 #if (osCMSIS < 0x20000U)
344 typedef void*       osMutexId;
345 #else
346 typedef osMutexId_t osMutexId;
347 #endif
348 
349 /**
350  * @param         name          name of the mutex object.
351  * @note CAN BE CHANGED: The parameter to \b osMutexDef shall be consistent but the
352                          macro body is implementation specific in every CMSIS-RTOS.
353  */
354 #if defined (osObjectsExternal)  // object is external
355 #define osMutexDef(name) \
356 extern const osMutexDef_t os_mutex_def_##name
357 #else                   // define the object
358 #if (osCMSIS < 0x20000U)
359 #define osMutexDef(name) \
360 const osMutexDef_t os_mutex_##name = {0}
361 #else
362 #define osMutexDef(name) \
363 const osMutexDef_t os_mutex_##name = {NULL,osMutexRecursive|osMutexPrioInherit|osMutexRobust,NULL,0U}
364 #endif
365 #endif
366 
367 /************************add osMutexDefEx_t*****************************/
368 #if (osCMSIS < 0x20000U)
369 typedef struct os_mutex_def_ex  {
370   osMutexDef_t       os_mutex_def;
371 } osMutexDefEx_t;
372 
373 #define osMutexInit(pMutex) \
374 ((osMutexDefEx_t*)&pMutex)->os_mutex_def.mutex = 0
375 #else
376 typedef struct os_mutex_def_ex  {
377   osMutexDef_t       os_mutex_def;
378 } osMutexDefEx_t;
379 
380 #define osMutexInit(pMutex) \
381 ((osMutexDefEx_t*)&pMutex)->os_mutex_def.name = NULL; \
382 ((osMutexDefEx_t*)&pMutex)->os_mutex_def.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust; \
383 ((osMutexDefEx_t*)&pMutex)->os_mutex_def.cb_mem = NULL; \
384 ((osMutexDefEx_t*)&pMutex)->os_mutex_def.cb_size = 0U
385 #endif
386 /********************************add osMutexDefEx_t*********************/
387 
388 /**
389  * @brief Access a Mutex definition.
390  * @param         name          name of the mutex object.
391  * @note CAN BE CHANGED: The parameter to \b osMutex shall be consistent but the
392                          macro body is implementation specific in every CMSIS-RTOS.
393  */
394 #define osMutex(name) &os_mutex_##name
395 
396 /**
397  * @brief Create and Initialize a Mutex object.
398  * @param[in]     mutex_def     mutex definition referenced with \ref osMutex.
399  * @return mutex ID for reference by other functions or NULL in case of error.
400  */
401 osMutexId osMutexCreate(const osMutexDef_t* mutex_def);
402 
403 
404 #if (osCMSIS < 0x20000U)
405 /**
406  * @brief Wait until a Mutex becomes available.
407  * @param[in]     mutex_id      mutex ID obtained by \ref osMutexCreate.
408  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
409  * @return status code that indicates the execution status of the function.
410  */
411 osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec);
412 
413 /**
414  * @brief Release a Mutex that was obtained by \ref osMutexWait.
415  * @param[in]     mutex_id      mutex ID obtained by \ref osMutexCreate.
416  * @return status code that indicates the execution status of the function.
417  */
418 osStatus osMutexRelease (osMutexId mutex_id);
419 
420 /**
421  * @brief Delete a Mutex object.
422  * @param[in]     mutex_id      mutex ID obtained by \ref osMutexCreate.
423  * @return status code that indicates the execution status of the function.
424  */
425 osStatus osMutexDelete (osMutexId mutex_id);
426 #else
427 #define  osMutexWait osMutexAcquire
428 #endif
429 /** @} cmsis_mutex */
430 
431 
432 /**@defgroup cmsis_semaphore CMSIS Semaphore Module.
433 * @{
434 * @ingroup cmsis
435 * @brief 标准CMSIS Semaphore 接口使用.
436 */
437 /**
438  * @brief Semaphore ID identifies the semaphore.
439  * @note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
440  */
441 #if (osCMSIS < 0x20000U)
442 typedef void*           osSemaphoreId;
443 #else
444 typedef osSemaphoreId_t osSemaphoreId;
445 #endif
446 
447 /**
448  * @brief Semaphore Definition structure contains setup information for a semaphore.
449  * @note CAN BE CHANGED: \b os_semaphore_def is implementation specific in every CMSIS-RTOS.
450  */
451 #if (osCMSIS < 0x20000U)
452 typedef struct os_semaphore_def {
453   uint32_t                     dummy;   ///< dummy value
454 } osSemaphoreDef_t;
455 #else
456 typedef osSemaphoreAttr_t osSemaphoreDef_t;
457 #endif
458 
459 /**
460  * @brief Define a Semaphore object.
461  * @param         name          name of the semaphore object.
462  * @note CAN BE CHANGED: The parameter to \b osSemaphoreDef shall be consistent but the
463                          macro body is implementation specific in every CMSIS-RTOS.
464  */
465 #if (osCMSIS < 0x20000U)
466 #define osSemaphoreDef(name) const osSemaphoreDef_t os_semaphore_##name = {0}
467 #else
468 #define osSemaphoreDef(name) const osSemaphoreDef_t os_semaphore_##name = { NULL, 0U, NULL, 0U }
469 #endif
470 
471 /**
472  * @brief Access a Semaphore definition.
473  * @param         name          name of the semaphore object.
474  * @note CAN BE CHANGED: The parameter to \b osSemaphore shall be consistent but the
475                          macro body is implementation specific in every CMSIS-RTOS.
476  */
477 #define osSemaphore(name) &os_semaphore_##name
478 
479 /**
480  * @briedf Create and Initialize a Semaphore object.
481  * @param[in]     semaphore_def semaphore definition referenced with \ref osSemaphore.
482  * @param[in]     count         maximum and initial number of available tokens.
483  * @return semaphore ID for reference by other functions or NULL in case of error.
484  */
485 osSemaphoreId osSemaphoreCreate(const osSemaphoreDef_t *semaphore_def, int32_t count);
486 
487 /**
488  * @brief Wait until a Semaphore token becomes available.
489  * @param[in]     semaphore_id  semaphore object referenced with \ref osSemaphoreCreate.
490  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
491  * @return number of available tokens, or -1 in case of incorrect parameters.
492  */
493 int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec);
494 
495 #if (osCMSIS < 0x20000U)
496 /**
497  * @brief Release a Semaphore token.
498  * @param[in]     semaphore_id  semaphore object referenced with \ref osSemaphoreCreate.
499  * @return status code that indicates the execution status of the function.
500  */
501 osStatus osSemaphoreRelease (osSemaphoreId semaphore_id);
502 
503 /**
504  * @brief Delete a Semaphore object.
505  * @param[in]     semaphore_id  semaphore object referenced with \ref osSemaphoreCreate.
506  * @return status code that indicates the execution status of the function.
507  */
508 osStatus osSemaphoreDelete (osSemaphoreId semaphore_id);
509 #endif
510 /** @} cmsis_semaphore */
511 
512 /**@defgroup cmsis_timer CMSIS timer Module.
513 * @{
514 * @ingroup cmsis
515 * @brief 标准CMSIS timer 接口使用.
516 */
517 
518 typedef void (*os_ptimer)(void const* argument);
519 #if (osCMSIS < 0x20000U)
520 typedef void*       osTimerId;
521 #else
522 typedef osTimerId_t osTimerId;
523 #endif
524 
525 #if (osCMSIS < 0x20000U)
526 typedef enum {
527   osTimerOnce     = 0,          ///< One-shot timer.
528   osTimerPeriodic = 1           ///< Repeating timer.
529 } os_timer_type;
530 #else
531 typedef osTimerType_t os_timer_type;
532 #endif
533 
534 /**
535  * @brief Timer Definition structure contains timer parameters.
536  * @note CAN BE CHANGED: \b os_timer_def is implementation specific in every CMSIS-RTOS.
537  */
538 #if (osCMSIS < 0x20000U)
539 typedef struct os_timer_def {
540   os_ptimer     ptimer; ///< start address of a timer function
541 } osTimerDef_t;
542 #else
543 typedef struct os_timer_def {
544   os_ptimer     ptimer; ///< start address of a timer function
545   osTimerAttr_t attr;   ///< timer attributes
546 } osTimerDef_t;
547 #endif
548 
549 /**
550  * @brief Define a Timer object.
551  * @param         name          name of the timer object.
552  * @param         function      name of the timer call back function.
553  * @note CAN BE CHANGED: The parameter to \b osTimerDef shall be consistent but the
554                          macro body is implementation specific in every CMSIS-RTOS.
555  */
556 #if defined (osObjectsExternal)  // object is external
557 #define osTimerDef(name, function) \
558 extern const osTimerDef_t os_timer_def_##name
559 #else
560 #if (osCMSIS < 0x20000U)
561 #define osTimerDef(name, function) \
562 const osTimerDef_t os_timer_##name = { (function) }
563 #else
564 #define osTimerDef(name, function) \
565 const osTimerDef_t os_timer_##name = { (function), { NULL, 0U, NULL, 0U } }
566 #endif
567 #endif
568 
569 
570 /***********************add osTimerDefEx_t**********************/
571 #if (osCMSIS < 0x20000U)
572 typedef struct os_timer_def_ex  {
573   osTimerDef_t       os_timer_def;
574 } osTimerDefEx_t;
575 #define osTimerInit(pTimer,function)  \
576 ((osTimerDefEx_t*)&pTimer)->os_timer_def.ptimer = function
577 
578 #else
579 typedef struct os_timer_def_ex  {
580   osTimerDef_t       os_timer_def;
581 } osTimerDefEx_t;
582 
583 #define osTimerInit(pTimer,function)  \
584 ((osTimerDefEx_t*)&pTimer)->os_timer_def.ptimer = function; \
585 ((osTimerDefEx_t*)&pTimer)->os_timer_def.attr.name = NULL; \
586 ((osTimerDefEx_t*)&pTimer)->os_timer_def.attr.attr_bits = 0U; \
587 ((osTimerDefEx_t*)&pTimer)->os_timer_def.attr.cb_mem = NULL; \
588 ((osTimerDefEx_t*)&pTimer)->os_timer_def.attr.cb_size = 0U
589 #endif
590 /***********************add osTimerDefEx_t**********************/
591 
592 /**
593  * @brief Access a Timer definition.
594  * @param         name          name of the timer object.
595  * @note CAN BE CHANGED: The parameter to \b osTimer shall be consistent but the
596                          macro body is implementation specific in every CMSIS-RTOS.
597  */
598 #define osTimer(name) &os_timer_##name
599 
600 /**
601  * @brief Create and Initialize a timer.
602  * @param[in]     timer_def     timer object referenced with \ref osTimer.
603  * @param[in]     type          osTimerOnce for one-shot or osTimerPeriodic for periodic behavior.
604  * @param[in]     argument      argument to the timer call back function.
605  * @return timer ID for reference by other functions or NULL in case of error.
606  */
607 osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument);
608 
609 #if (osCMSIS < 0x20000U)
610 /**
611  * @brief Start or restart a timer.
612  * @param[in]     timer_id      timer ID obtained by \ref osTimerCreate.
613  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue "time delay" value of the timer.
614  * @return status code that indicates the execution status of the function.
615  */
616 osStatus osTimerStart (osTimerId timer_id, uint32_t millisec);
617 
618 /**
619  * @brief Stop a timer.
620  * @param[in]     timer_id      timer ID obtained by \ref osTimerCreate.
621  * @return status code that indicates the execution status of the function.
622  */
623 osStatus osTimerStop (osTimerId timer_id);
624 
625 /**
626  * @brief Delete a timer.
627  * @param[in]     timer_id      timer ID obtained by \ref osTimerCreate.
628  * @return status code that indicates the execution status of the function.
629  */
630 osStatus osTimerDelete (osTimerId timer_id);
631 #endif
632 /** @} cmsis_timer */
633 
634 ///< Message
635 /**@defgroup cmsis_message CMSIS message Module.
636 * @{
637 * @ingroup cmsis
638 * @brief 标准CMSIS message 接口使用.
639 */
640 
641 #if (osCMSIS < 0x20000U)
642 typedef struct os_messageQ_def {
643   uint32_t             queue_sz; ///< number of elements in the queue
644   void                 *pool;    ///< memory array for messages
645 } osMessageQDef_t;
646 #else
647 typedef struct os_messageQ_def {
648   uint32_t             queue_sz; ///< number of elements in the queue
649   osMessageQueueAttr_t attr;     ///< message queue attributes
650 } osMessageQDef_t;
651 #endif
652 
653 /**
654  * @brief Create a Message Queue Definition.
655  * @param         name          name of the queue.
656  * @param         queue_sz      maximum number of messages in the queue.
657  * @param         type          data type of a single message element (for debugger).
658  * @note CAN BE CHANGED: The parameter to \b osMessageQDef shall be consistent but the
659                          macro body is implementation specific in every CMSIS-RTOS.
660  */
661 #if (osCMSIS < 0x20000U)
662 #define osMessageQDef(name, queue_sz, type) const osMessageQDef_t os_msgQ_##name = { (queue_sz), NULL }
663 #else
664 #define osMessageQDef(name, queue_sz, type) const osMessageQDef_t os_msgQ_##name = { (queue_sz), { NULL, 0U, NULL, 0U, NULL, 0U } }
665 #endif
666 
667 /**
668  * @brief Access a Message Queue Definition.
669  * @param         name          name of the queue
670  * @note CAN BE CHANGED: The parameter to \b osMessageQ shall be consistent but the
671                          macro body is implementation specific in every CMSIS-RTOS.
672  */
673 #define osMessageQ(name) &os_msgQ_##name
674 
675 /**
676  * @brief Create and Initialize a Message Queue object.
677  * @param[in]     queue_def     message queue definition referenced with \ref osMessageQ.
678  * @param[in]     thread_id     thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
679  * @return message queue ID for reference by other functions or NULL in case of error.
680  */
681 osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id);
682 
683 /**
684  * @brief Put a Message to a Queue.
685  * @param[in]     queue_id      message queue ID obtained with \ref osMessageCreate.
686  * @param[in]     info          message information.
687  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
688  * @return status code that indicates the execution status of the function.
689  */
690 osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec);
691 
692 /**
693  * @brief Get a Message from a Queue or timeout if Queue is empty.
694  * @param[in]     queue_id      message queue ID obtained with \ref osMessageCreate.
695  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
696  * @return event information that includes status code.
697  */
698 osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec);
699 
700 uint32_t osMessageGetSpace (osMessageQId queue_id);
701 /** @} cmsis_message */
702 
703 /**@defgroup cmsis_mempool CMSIS memory pool Module.
704 * @{
705 * @ingroup cmsis
706 * @brief 标准CMSIS memory pool接口使用.
707 */
708 /**
709  * @brief Pool ID identifies the memory pool.
710  * @note CAN BE CHANGED: \b implementation specific in every CMSIS-RTOS.
711  */
712 typedef void* osPoolId;
713 
714 /**
715  * @brief Definition structure for memory block allocation.
716  * @note CAN BE CHANGED: \b os_pool_def is implementation specific in every CMSIS-RTOS.
717  */
718 #if (osCMSIS < 0x20000U)
719 typedef struct os_pool_def {
720   uint32_t            pool_sz; ///< number of items (elements) in the pool
721   uint32_t            item_sz; ///< size of an item
722   void                *pool;   ///< pointer to memory for pool
723 } osPoolDef_t;
724 #else
725 typedef struct os_pool_def {
726   uint32_t           pool_sz;  ///< number of items (elements) in the pool
727   uint32_t           item_sz;  ///< size of an item
728   osMemoryPoolAttr_t attr;     ///< memory pool attributes
729 } osPoolDef_t;
730 #endif
731 
732 /**
733  * @brief Define a Memory Pool.
734  * @param         name          name of the memory pool.
735  * @param         no            maximum number of blocks (objects) in the memory pool.
736  * @param         type          data type of a single block (object).
737  * @note CAN BE CHANGED: The parameter to \b osPoolDef shall be consistent but the
738                          macro body is implementation specific in every CMSIS-RTOS.
739  */
740 #if (osCMSIS < 0x20000U)
741 #define osPoolDef(name, no, type) const osPoolDef_t os_pool_##name = { (no), sizeof(type), NULL }
742 #else
743 #define osPoolDef(name, no, type) const osPoolDef_t os_pool_##name = { (no), sizeof(type), { NULL, 0U, NULL, 0U, NULL, 0U } }
744 #endif
745 
746 /**
747  * @brief Access a Memory Pool definition.
748  * @param         name          name of the memory pool
749  * @note CAN BE CHANGED: The parameter to \b osPool shall be consistent but the
750                          macro body is implementation specific in every CMSIS-RTOS.
751  */
752 #define osPool(name) &os_pool_##name
753 
754 /**
755  * @brief Create and Initialize a Memory Pool object.
756  * @param[in]     pool_def      memory pool definition referenced with \ref osPool.
757  * @return memory pool ID for reference by other functions or NULL in case of error.
758  */
759 osPoolId osPoolCreate (const osPoolDef_t *pool_def);
760 
761 /**
762  * @brief Allocate a memory block from a Memory Pool.
763  * @param[in]     pool_id       memory pool ID obtain referenced with \ref osPoolCreate.
764  * @return address of the allocated memory block or NULL in case of no memory available.
765  */
766 void *osPoolAlloc (osPoolId pool_id);
767 
768 /**
769  * @brief Allocate a memory block from a Memory Pool and set memory block to zero.
770  * @param[in]     pool_id       memory pool ID obtain referenced with \ref osPoolCreate.
771  * @return address of the allocated memory block or NULL in case of no memory available.
772  */
773 void *osPoolCAlloc (osPoolId pool_id);
774 
775 /**
776  * @brief Return an allocated memory block back to a Memory Pool.
777  * @param[in]     pool_id       memory pool ID obtain referenced with \ref osPoolCreate.
778  * @param[in]     block         address of the allocated memory block to be returned to the memory pool.
779  * @return status code that indicates the execution status of the function.
780  */
781 osStatus osPoolFree (osPoolId pool_id, void *block);
782 /** @} cmsis_mempool */
783 
784 
785 /**@defgroup cmsis_mailq CMSIS mail queue Module.
786 * @{
787 * @ingroup cmsis
788 * @brief 标准CMSIS mail queue接口使用.
789 */
790 
791 /**
792  * @brief Definition structure for mail queue.
793  * @note CAN BE CHANGED: \b os_mailQ_def is implementation specific in every CMSIS-RTOS.
794  */
795 #if (osCMSIS < 0x20000U)
796 typedef struct os_mailQ_def {
797   uint32_t               queue_sz; ///< number of elements in the queue
798   uint32_t               item_sz;  ///< size of an item
799   void                   *pool;    ///< memory array for mail
800 } osMailQDef_t;
801 #else
802 typedef struct os_mailQ_def {
803   uint32_t                queue_sz;    ///< number of elements in the queue
804   uint32_t                 item_sz;    ///< size of an item
805   void                       *pool;    ///< memory array for mail
806 } osMailQDef_t;
807 #endif
808 struct osMailQ {
809     uint32_t queue;
810     void *pool;
811 };
812 
813 
814 /**
815  * @brief Create a Mail Queue Definition.
816  * @param         name          name of the queue.
817  * @param         queue_sz      maximum number of mails in the queue.
818  * @param         type          data type of a single mail element.
819  * @note CAN BE CHANGED: The parameter to \b osMailQDef shall be consistent but the
820                          macro body is implementation specific in every CMSIS-RTOS.
821  */
822 #if (osCMSIS < 0x20000U)
823 #define osMailQDef(name, queue_sz, type) const osMailQDef_t os_mailQ_##name = { (queue_sz), sizeof(type), NULL }
824 #else
825 #define osMailQDef(name, queue_sz, type) \
826     struct osMailQ os_mailQ_p_##name = { 0, NULL }; \
827     const osMailQDef_t os_mailQ_##name = { (queue_sz), sizeof(type), &os_mailQ_p_##name }
828 #endif
829 
830 /**
831  * @brief Access a Mail Queue Definition.
832  * @param         name          name of the queue
833  * @note CAN BE CHANGED: The parameter to \b osMailQ shall be consistent but the
834                          macro body is implementation specific in every CMSIS-RTOS.
835  */
836 #define osMailQ(name) &os_mailQ_##name
837 
838 /**
839  * @brief Create and Initialize a Mail Queue object.
840  * @param[in]     queue_def     mail queue definition referenced with \ref osMailQ.
841  * @param[in]     thread_id     thread ID (obtained by \ref osThreadCreate or \ref osThreadGetId) or NULL.
842  * @return mail queue ID for reference by other functions or NULL in case of error.
843  */
844 osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id);
845 
846 /**
847  * @brief Allocate a memory block for mail from a mail memory pool.
848  * @param[in]     queue_id      mail queue ID obtained with \ref osMailCreate.
849  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
850  * @return pointer to memory block that can be filled with mail or NULL in case of error.
851  */
852 void *osMailAlloc (osMailQId queue_id, uint32_t millisec);
853 
854 /**
855  * @brief Allocate a memory block for mail from a mail memory pool and set memory block to zero.
856  * @param[in]     queue_id      mail queue ID obtained with \ref osMailCreate.
857  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out
858  * @return pointer to memory block that can be filled with mail or NULL in case of error.
859  */
860 void *osMailCAlloc (osMailQId queue_id, uint32_t millisec);
861 
862 /**
863  * @brief Put a Mail into a Queue.
864  * @param[in]     queue_id      mail queue ID obtained with \ref osMailCreate.
865  * @param[in]     mail          pointer to memory with mail to put into a queue.
866  * @return status code that indicates the execution status of the function.
867  */
868 osStatus osMailPut (osMailQId queue_id, const void *mail);
869 
870 /**
871  * @brief Get a Mail from a Queue or timeout if Queue is empty.
872  * @param[in]     queue_id      mail queue ID obtained with \ref osMailCreate.
873  * @param[in]     millisec      \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
874  * @return event information that includes status code.
875  */
876 osEvent osMailGet (osMailQId queue_id, uint32_t millisec);
877 
878 /**
879  * @brief Free a memory block by returning it to a mail memory pool.
880  * @param[in]     queue_id      mail queue ID obtained with \ref osMailCreate.
881  * @param[in]     mail          pointer to memory block that was obtained with \ref osMailGet.
882  * @return status code that indicates the execution status of the function.
883  */
884 osStatus osMailFree (osMailQId queue_id, void *mail);
885 /** @} cmsis_mailq */
886 
887 /// Get the mail number in the mailbox
888 /// \param[in]     queue_id      mail queue ID obtained with \ref osMailCreate.
889 /// \return  the number in the mailbox
890 uint32_t osMailGetCount (osMailQId queue_id);
891 
892 #ifdef __cplusplus
893 }
894 #endif
895 
896 #endif
897