你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

CMSIS-RTOS2 文档翻译 之 通用 RTOS 接口

[复制链接]
XinLiYF 发布时间:2018-4-21 15:48
本帖最后由 XinLiYF 于 2018-4-22 21:59 编辑

通用 RTOS 接口

CMSIS-RTOS2 是一个通用的 API ,不受底层 RTOS 内核的影响。应用程序员在用户代码中调用 CMSIS-RTOS2 API 函数以确保从一个 RTOS 到另一个 RTOS 的最大可移植性。使用 CMSIS-RTOS2 API 的中间件可以避免不必要的移植工作。


CMSIS-RTOS API 结构


典型的 CMSIS-RTOS2 API 实现与现有的实时内核接口。CMSIS-RTOS2 API 提供以下属性和功能:

  • 函数名称,标识符和参数是描述性的,易于理解的。这些功能强大而灵活,减少了暴露给用户的功能数量。
  • 线程管理允许您定义,创建和控制线程。
  • 中断服务程序(ISR)可以调用一些 CMSIS-RTOS 功能。 当 CMSIS-RTOS 函数不能从 ISR 上下文中调用时,它会拒绝调用并返回错误代码。
  • 三种不同的事件类型支持多个线程和/或 ISR 之间的通信:
    • 线程标志: 可用于向线程指示特定条件。
    • 事件标志: 可用于向线程或 ISR 指示事件。
    • 消息: 可以发送到线程或 ISR 。消息缓冲在队列中。
  • 互斥管理和信号量已合并。
  • CPU 时间可以安排以下功能:
    • 超时参数包含在许多 CMSIS-RTOS 功能中,以避免系统锁定。当指定超时时,系统会等待,直到资源可用或发生事件。在等待时,运行其他线程。
    • osDelay 和 osDelayUntil 函数将线程置于 WAITING 状态一段指定的时间。
    • osThreadYield 提供协作式线程切换并将执行传递给具有相同优先级的另一个线程。
  • 定时器管理功能用于触发功能的执行。

CMSIS-RTOS2 API 旨在通过 Cortex-M 存储器保护单元(MPU)选择性地整合多处理器系统和/或访问保护。

在一些 RTOS 实现中,线程可能在不同的处理器上执行,因此消息队列可能驻留在共享内存资源中。

CMSIS-RTOS2 API 鼓励软件行业发展现有的 RTOS 实施。实时操作系统的实现可以在针对 Cortex-M 处理器的不同方面进行不同的优化。可选功能可能是例如

  • 支持 Cortex-M 内存保护单元(MPU)。
  • 支持多处理器系统。
  • 支持 DMA 控制器。
  • 确定性上下文切换。
  • 循环上下文切换。
  • 避免死锁,例如优先倒置。
  • 通过使用 Armv7-M 指令 LDREX 和 STREX 来实现零中断延迟。

使用 CMSIS-RTOS2 实现

CMSIS-RTOS2 实现通常作为库提供。要将 RTOS 功能添加到现有的基于 CMSIS 的应用程序中,需要添加 RTOS 库(通常是一个或多个配置文件)。有一个新的头文件 cmsis_os2.h 可用。这是完全可移植应用程序所需的唯一头文件。在这种情况下,不能使用用户为控制块,对象数据和线程堆栈提供的内存。或者,您可以包含一个实现特定的头文件(例如 rtx_os.h),该文件还提供了用于资源分配的定义(例如控制块的大小,对象数据和线程堆栈所需的内存)。 这是可选的,意味着应用程序代码不是完全可移植的。


CMSIS-RTOS 文件结构



将文件添加到项目后,用户可以开始使用 CMSIS-RTOS 功能。下面提供了一个代码示例:

代码示例
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS 'main' function template
  3. *---------------------------------------------------------------------------*/

  4. #include "RTE_Components.h"
  5. #include  CMSIS_device_header
  6. #include "cmsis_os2.h"

  7. /*----------------------------------------------------------------------------
  8. * Application main thread
  9. *---------------------------------------------------------------------------*/
  10. void app_main (void *argument) {

  11.   // ...
  12.   for (;;) {}
  13. }

  14. int main (void) {

  15.   // System Initialization
  16.   SystemCoreClockUpdate();
  17. #ifdef RTE_Compiler_EventRecorder
  18.   // Initialize and start Event Recorder
  19.   EventRecorderInitialize(EventRecordError, 1U);
  20. #endif
  21.   // ...

  22.   osKernelInitialize();                 // Initialize CMSIS-RTOS
  23.   osThreadNew(app_main, NULL, NULL);    // Create application main thread
  24.   osKernelStart();                      // Start thread execution
  25.   for (;;) {}
  26. }
复制代码

cmsis_os2.h 头文件

文件 cmsis_os2.h 是一个标准头文件,与每个 CMSIS-RTOS2 兼容的实时操作系统(RTOS)相连接。每个实现都提供了相同的 cmsis_os2.h ,它定义了到 CMSIS-RTOS2 的接口。

通过使用 cmsis_os2.h 和动态对象分配,可以创建在不同的 CMSIS-RTOS2 实现上使用时不需要修改的源代码或库。

头文件 cmsis_os2.h
  1. /*
  2. * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * ----------------------------------------------------------------------
  19. *
  20. * $Date:        30. October 2017
  21. * $Revision:    V2.1.2
  22. *
  23. * Project:      CMSIS-RTOS2 API
  24. * Title:        cmsis_os2.h header file
  25. *
  26. * Version 2.1.2
  27. *    Additional functions allowed to be called from Interrupt Service Routines:
  28. *    - osKernelGetInfo, osKernelGetState
  29. * Version 2.1.1
  30. *    Additional functions allowed to be called from Interrupt Service Routines:
  31. *    - osKernelGetTickCount, osKernelGetTickFreq
  32. *    Changed Kernel Tick type to uint32_t:
  33. *    - updated: osKernelGetTickCount, osDelayUntil
  34. * Version 2.1.0
  35. *    Support for critical and uncritical sections (nesting safe):
  36. *    - updated: osKernelLock, osKernelUnlock
  37. *    - added: osKernelRestoreLock
  38. *    Updated Thread and Event Flags:
  39. *    - changed flags parameter and return type from int32_t to uint32_t
  40. * Version 2.0.0
  41. *    Initial Release
  42. *---------------------------------------------------------------------------*/

  43. #ifndef CMSIS_OS2_H_
  44. #define CMSIS_OS2_H_

  45. #ifndef __NO_RETURN
  46. #if   defined(__CC_ARM)
  47. #define __NO_RETURN __declspec(noreturn)
  48. #elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  49. #define __NO_RETURN __attribute__((__noreturn__))
  50. #elif defined(__GNUC__)
  51. #define __NO_RETURN __attribute__((__noreturn__))
  52. #elif defined(__ICCARM__)
  53. #define __NO_RETURN __noreturn
  54. #else
  55. #define __NO_RETURN
  56. #endif
  57. #endif

  58. #include <stdint.h>
  59. #include <stddef.h>

  60. #ifdef  __cplusplus
  61. extern "C"
  62. {
  63. #endif


  64. //  ==== Enumerations, structures, defines ====

  65. /// Version information.
  66. typedef struct {
  67.   uint32_t                       api;   ///< API version (major.minor.rev: mmnnnrrrr dec).
  68.   uint32_t                    kernel;   ///< Kernel version (major.minor.rev: mmnnnrrrr dec).
  69. } osVersion_t;

  70. /// Kernel state.
  71. typedef enum {
  72.   osKernelInactive        =  0,         ///< Inactive.
  73.   osKernelReady           =  1,         ///< Ready.
  74.   osKernelRunning         =  2,         ///< Running.
  75.   osKernelLocked          =  3,         ///< Locked.
  76.   osKernelSuspended       =  4,         ///< Suspended.
  77.   osKernelError           = -1,         ///< Error.
  78.   osKernelReserved        = 0x7FFFFFFFU ///< Prevents enum down-size compiler optimization.
  79. } osKernelState_t;

  80. /// Thread state.
  81. typedef enum {
  82.   osThreadInactive        =  0,         ///< Inactive.
  83.   osThreadReady           =  1,         ///< Ready.
  84.   osThreadRunning         =  2,         ///< Running.
  85.   osThreadBlocked         =  3,         ///< Blocked.
  86.   osThreadTerminated      =  4,         ///< Terminated.
  87.   osThreadError           = -1,         ///< Error.
  88.   osThreadReserved        = 0x7FFFFFFF  ///< Prevents enum down-size compiler optimization.
  89. } osThreadState_t;

  90. /// Priority values.
  91. typedef enum {
  92.   osPriorityNone          =  0,         ///< No priority (not initialized).
  93.   osPriorityIdle          =  1,         ///< Reserved for Idle thread.
  94.   osPriorityLow           =  8,         ///< Priority: low
  95.   osPriorityLow1          =  8+1,       ///< Priority: low + 1
  96.   osPriorityLow2          =  8+2,       ///< Priority: low + 2
  97.   osPriorityLow3          =  8+3,       ///< Priority: low + 3
  98.   osPriorityLow4          =  8+4,       ///< Priority: low + 4
  99.   osPriorityLow5          =  8+5,       ///< Priority: low + 5
  100.   osPriorityLow6          =  8+6,       ///< Priority: low + 6
  101.   osPriorityLow7          =  8+7,       ///< Priority: low + 7
  102.   osPriorityBelowNormal   = 16,         ///< Priority: below normal
  103.   osPriorityBelowNormal1  = 16+1,       ///< Priority: below normal + 1
  104.   osPriorityBelowNormal2  = 16+2,       ///< Priority: below normal + 2
  105.   osPriorityBelowNormal3  = 16+3,       ///< Priority: below normal + 3
  106.   osPriorityBelowNormal4  = 16+4,       ///< Priority: below normal + 4
  107.   osPriorityBelowNormal5  = 16+5,       ///< Priority: below normal + 5
  108.   osPriorityBelowNormal6  = 16+6,       ///< Priority: below normal + 6
  109.   osPriorityBelowNormal7  = 16+7,       ///< Priority: below normal + 7
  110.   osPriorityNormal        = 24,         ///< Priority: normal
  111.   osPriorityNormal1       = 24+1,       ///< Priority: normal + 1
  112.   osPriorityNormal2       = 24+2,       ///< Priority: normal + 2
  113.   osPriorityNormal3       = 24+3,       ///< Priority: normal + 3
  114.   osPriorityNormal4       = 24+4,       ///< Priority: normal + 4
  115.   osPriorityNormal5       = 24+5,       ///< Priority: normal + 5
  116.   osPriorityNormal6       = 24+6,       ///< Priority: normal + 6
  117.   osPriorityNormal7       = 24+7,       ///< Priority: normal + 7
  118.   osPriorityAboveNormal   = 32,         ///< Priority: above normal
  119.   osPriorityAboveNormal1  = 32+1,       ///< Priority: above normal + 1
  120.   osPriorityAboveNormal2  = 32+2,       ///< Priority: above normal + 2
  121.   osPriorityAboveNormal3  = 32+3,       ///< Priority: above normal + 3
  122.   osPriorityAboveNormal4  = 32+4,       ///< Priority: above normal + 4
  123.   osPriorityAboveNormal5  = 32+5,       ///< Priority: above normal + 5
  124.   osPriorityAboveNormal6  = 32+6,       ///< Priority: above normal + 6
  125.   osPriorityAboveNormal7  = 32+7,       ///< Priority: above normal + 7
  126.   osPriorityHigh          = 40,         ///< Priority: high
  127.   osPriorityHigh1         = 40+1,       ///< Priority: high + 1
  128.   osPriorityHigh2         = 40+2,       ///< Priority: high + 2
  129.   osPriorityHigh3         = 40+3,       ///< Priority: high + 3
  130.   osPriorityHigh4         = 40+4,       ///< Priority: high + 4
  131.   osPriorityHigh5         = 40+5,       ///< Priority: high + 5
  132.   osPriorityHigh6         = 40+6,       ///< Priority: high + 6
  133.   osPriorityHigh7         = 40+7,       ///< Priority: high + 7
  134.   osPriorityRealtime      = 48,         ///< Priority: realtime
  135.   osPriorityRealtime1     = 48+1,       ///< Priority: realtime + 1
  136.   osPriorityRealtime2     = 48+2,       ///< Priority: realtime + 2
  137.   osPriorityRealtime3     = 48+3,       ///< Priority: realtime + 3
  138.   osPriorityRealtime4     = 48+4,       ///< Priority: realtime + 4
  139.   osPriorityRealtime5     = 48+5,       ///< Priority: realtime + 5
  140.   osPriorityRealtime6     = 48+6,       ///< Priority: realtime + 6
  141.   osPriorityRealtime7     = 48+7,       ///< Priority: realtime + 7
  142.   osPriorityISR           = 56,         ///< Reserved for ISR deferred thread.
  143.   osPriorityError         = -1,         ///< System cannot determine priority or illegal priority.
  144.   osPriorityReserved      = 0x7FFFFFFF  ///< Prevents enum down-size compiler optimization.
  145. } osPriority_t;

  146. /// Entry point of a thread.
  147. typedef void (*osThreadFunc_t) (void *argument);

  148. /// Timer callback function.
  149. typedef void (*osTimerFunc_t) (void *argument);

  150. /// Timer type.
  151. typedef enum {
  152.   osTimerOnce               = 0,          ///< One-shot timer.
  153.   osTimerPeriodic           = 1           ///< Repeating timer.
  154. } osTimerType_t;

  155. // Timeout value.
  156. #define osWaitForever         0xFFFFFFFFU ///< Wait forever timeout value.

  157. // Flags options (\ref osThreadFlagsWait and \ref osEventFlagsWait).
  158. #define osFlagsWaitAny        0x00000000U ///< Wait for any flag (default).
  159. #define osFlagsWaitAll        0x00000001U ///< Wait for all flags.
  160. #define osFlagsNoClear        0x00000002U ///< Do not clear flags which have been specified to wait for.

  161. // Flags errors (returned by osThreadFlagsXxxx and osEventFlagsXxxx).
  162. #define osFlagsError          0x80000000U ///< Error indicator.
  163. #define osFlagsErrorUnknown   0xFFFFFFFFU ///< osError (-1).
  164. #define osFlagsErrorTimeout   0xFFFFFFFEU ///< osErrorTimeout (-2).
  165. #define osFlagsErrorResource  0xFFFFFFFDU ///< osErrorResource (-3).
  166. #define osFlagsErrorParameter 0xFFFFFFFCU ///< osErrorParameter (-4).
  167. #define osFlagsErrorISR       0xFFFFFFFAU ///< osErrorISR (-6).

  168. // Thread attributes (attr_bits in \ref osThreadAttr_t).
  169. #define osThreadDetached      0x00000000U ///< Thread created in detached mode (default)
  170. #define osThreadJoinable      0x00000001U ///< Thread created in joinable mode

  171. // Mutex attributes (attr_bits in \ref osMutexAttr_t).
  172. #define osMutexRecursive      0x00000001U ///< Recursive mutex.
  173. #define osMutexPrioInherit    0x00000002U ///< Priority inherit protocol.
  174. #define osMutexRobust         0x00000008U ///< Robust mutex.

  175. /// Status code values returned by CMSIS-RTOS functions.
  176. typedef enum {
  177.   osOK                      =  0,         ///< Operation completed successfully.
  178.   osError                   = -1,         ///< Unspecified RTOS error: run-time error but no other error message fits.
  179.   osErrorTimeout            = -2,         ///< Operation not completed within the timeout period.
  180.   osErrorResource           = -3,         ///< Resource not available.
  181.   osErrorParameter          = -4,         ///< Parameter error.
  182.   osErrorNoMemory           = -5,         ///< System is out of memory: it was impossible to allocate or reserve memory for the operation.
  183.   osErrorISR                = -6,         ///< Not allowed in ISR context: the function cannot be called from interrupt service routines.
  184.   osStatusReserved          = 0x7FFFFFFF  ///< Prevents enum down-size compiler optimization.
  185. } osStatus_t;


  186. /// \details Thread ID identifies the thread.
  187. typedef void *osThreadId_t;

  188. /// \details Timer ID identifies the timer.
  189. typedef void *osTimerId_t;

  190. /// \details Event Flags ID identifies the event flags.
  191. typedef void *osEventFlagsId_t;

  192. /// \details Mutex ID identifies the mutex.
  193. typedef void *osMutexId_t;

  194. /// \details Semaphore ID identifies the semaphore.
  195. typedef void *osSemaphoreId_t;

  196. /// \details Memory Pool ID identifies the memory pool.
  197. typedef void *osMemoryPoolId_t;

  198. /// \details Message Queue ID identifies the message queue.
  199. typedef void *osMessageQueueId_t;


  200. #ifndef TZ_MODULEID_T
  201. #define TZ_MODULEID_T
  202. /// \details Data type that identifies secure software modules called by a process.
  203. typedef uint32_t TZ_ModuleId_t;
  204. #endif


  205. /// Attributes structure for thread.
  206. typedef struct {
  207.   const char                   *name;   ///< name of the thread
  208.   uint32_t                 attr_bits;   ///< attribute bits
  209.   void                      *cb_mem;    ///< memory for control block
  210.   uint32_t                   cb_size;   ///< size of provided memory for control block
  211.   void                   *stack_mem;    ///< memory for stack
  212.   uint32_t                stack_size;   ///< size of stack
  213.   osPriority_t              priority;   ///< initial thread priority (default: osPriorityNormal)
  214.   TZ_ModuleId_t            tz_module;   ///< TrustZone module identifier
  215.   uint32_t                  reserved;   ///< reserved (must be 0)
  216. } osThreadAttr_t;

  217. /// Attributes structure for timer.
  218. typedef struct {
  219.   const char                   *name;   ///< name of the timer
  220.   uint32_t                 attr_bits;   ///< attribute bits
  221.   void                      *cb_mem;    ///< memory for control block
  222.   uint32_t                   cb_size;   ///< size of provided memory for control block
  223. } osTimerAttr_t;

  224. /// Attributes structure for event flags.
  225. typedef struct {
  226.   const char                   *name;   ///< name of the event flags
  227.   uint32_t                 attr_bits;   ///< attribute bits
  228.   void                      *cb_mem;    ///< memory for control block
  229.   uint32_t                   cb_size;   ///< size of provided memory for control block
  230. } osEventFlagsAttr_t;

  231. /// Attributes structure for mutex.
  232. typedef struct {
  233.   const char                   *name;   ///< name of the mutex
  234.   uint32_t                 attr_bits;   ///< attribute bits
  235.   void                      *cb_mem;    ///< memory for control block
  236.   uint32_t                   cb_size;   ///< size of provided memory for control block
  237. } osMutexAttr_t;

  238. /// Attributes structure for semaphore.
  239. typedef struct {
  240.   const char                   *name;   ///< name of the semaphore
  241.   uint32_t                 attr_bits;   ///< attribute bits
  242.   void                      *cb_mem;    ///< memory for control block
  243.   uint32_t                   cb_size;   ///< size of provided memory for control block
  244. } osSemaphoreAttr_t;

  245. /// Attributes structure for memory pool.
  246. typedef struct {
  247.   const char                   *name;   ///< name of the memory pool
  248.   uint32_t                 attr_bits;   ///< attribute bits
  249.   void                      *cb_mem;    ///< memory for control block
  250.   uint32_t                   cb_size;   ///< size of provided memory for control block
  251.   void                      *mp_mem;    ///< memory for data storage
  252.   uint32_t                   mp_size;   ///< size of provided memory for data storage
  253. } osMemoryPoolAttr_t;

  254. /// Attributes structure for message queue.
  255. typedef struct {
  256.   const char                   *name;   ///< name of the message queue
  257.   uint32_t                 attr_bits;   ///< attribute bits
  258.   void                      *cb_mem;    ///< memory for control block
  259.   uint32_t                   cb_size;   ///< size of provided memory for control block
  260.   void                      *mq_mem;    ///< memory for data storage
  261.   uint32_t                   mq_size;   ///< size of provided memory for data storage
  262. } osMessageQueueAttr_t;


  263. //  ==== Kernel Management Functions ====

  264. /// Initialize the RTOS Kernel.
  265. /// \return status code that indicates the execution status of the function.
  266. osStatus_t osKernelInitialize (void);

  267. ///  Get RTOS Kernel Information.
  268. /// \param[out]    version       pointer to buffer for retrieving version information.
  269. /// \param[out]    id_buf        pointer to buffer for retrieving kernel identification string.
  270. /// \param[in]     id_size       size of buffer for kernel identification string.
  271. /// \return status code that indicates the execution status of the function.
  272. osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size);

  273. /// Get the current RTOS Kernel state.
  274. /// \return current RTOS Kernel state.
  275. osKernelState_t osKernelGetState (void);

  276. /// Start the RTOS Kernel scheduler.
  277. /// \return status code that indicates the execution status of the function.
  278. osStatus_t osKernelStart (void);

  279. /// Lock the RTOS Kernel scheduler.
  280. /// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
  281. int32_t osKernelLock (void);

  282. /// Unlock the RTOS Kernel scheduler.
  283. /// \return previous lock state (1 - locked, 0 - not locked, error code if negative).
  284. int32_t osKernelUnlock (void);

  285. /// Restore the RTOS Kernel scheduler lock state.
  286. /// \param[in]     lock          lock state obtained by \ref osKernelLock or \ref osKernelUnlock.
  287. /// \return new lock state (1 - locked, 0 - not locked, error code if negative).
  288. int32_t osKernelRestoreLock (int32_t lock);

  289. /// Suspend the RTOS Kernel scheduler.
  290. /// \return time in ticks, for how long the system can sleep or power-down.
  291. uint32_t osKernelSuspend (void);

  292. /// Resume the RTOS Kernel scheduler.
  293. /// \param[in]     sleep_ticks   time in ticks for how long the system was in sleep or power-down mode.
  294. void osKernelResume (uint32_t sleep_ticks);

  295. /// Get the RTOS kernel tick count.
  296. /// \return RTOS kernel current tick count.
  297. uint32_t osKernelGetTickCount (void);

  298. /// Get the RTOS kernel tick frequency.
  299. /// \return frequency of the kernel tick in hertz, i.e. kernel ticks per second.
  300. uint32_t osKernelGetTickFreq (void);

  301. /// Get the RTOS kernel system timer count.
  302. /// \return RTOS kernel current system timer count as 32-bit value.
  303. uint32_t osKernelGetSysTimerCount (void);

  304. /// Get the RTOS kernel system timer frequency.
  305. /// \return frequency of the system timer in hertz, i.e. timer ticks per second.
  306. uint32_t osKernelGetSysTimerFreq (void);


  307. //  ==== Thread Management Functions ====

  308. /// Create a thread and add it to Active Threads.
  309. /// \param[in]     func          thread function.
  310. /// \param[in]     argument      pointer that is passed to the thread function as start argument.
  311. /// \param[in]     attr          thread attributes; NULL: default values.
  312. /// \return thread ID for reference by other functions or NULL in case of error.
  313. osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);

  314. /// Get name of a thread.
  315. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  316. /// \return name as NULL terminated string.
  317. const char *osThreadGetName (osThreadId_t thread_id);

  318. /// Return the thread ID of the current running thread.
  319. /// \return thread ID for reference by other functions or NULL in case of error.
  320. osThreadId_t osThreadGetId (void);

  321. /// Get current thread state of a thread.
  322. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  323. /// \return current thread state of the specified thread.
  324. osThreadState_t osThreadGetState (osThreadId_t thread_id);

  325. /// Get stack size of a thread.
  326. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  327. /// \return stack size in bytes.
  328. uint32_t osThreadGetStackSize (osThreadId_t thread_id);

  329. /// Get available stack space of a thread based on stack watermark recording during execution.
  330. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  331. /// \return remaining stack space in bytes.
  332. uint32_t osThreadGetStackSpace (osThreadId_t thread_id);

  333. /// Change priority of a thread.
  334. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  335. /// \param[in]     priority      new priority value for the thread function.
  336. /// \return status code that indicates the execution status of the function.
  337. osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority);

  338. /// Get current priority of a thread.
  339. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  340. /// \return current priority value of the specified thread.
  341. osPriority_t osThreadGetPriority (osThreadId_t thread_id);

  342. /// Pass control to next thread that is in state \b READY.
  343. /// \return status code that indicates the execution status of the function.
  344. osStatus_t osThreadYield (void);

  345. /// Suspend execution of a thread.
  346. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  347. /// \return status code that indicates the execution status of the function.
  348. osStatus_t osThreadSuspend (osThreadId_t thread_id);

  349. /// Resume execution of a thread.
  350. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  351. /// \return status code that indicates the execution status of the function.
  352. osStatus_t osThreadResume (osThreadId_t thread_id);

  353. /// Detach a thread (thread storage can be reclaimed when thread terminates).
  354. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  355. /// \return status code that indicates the execution status of the function.
  356. osStatus_t osThreadDetach (osThreadId_t thread_id);

  357. /// Wait for specified thread to terminate.
  358. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  359. /// \return status code that indicates the execution status of the function.
  360. osStatus_t osThreadJoin (osThreadId_t thread_id);

  361. /// Terminate execution of current running thread.
  362. __NO_RETURN void osThreadExit (void);

  363. /// Terminate execution of a thread.
  364. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  365. /// \return status code that indicates the execution status of the function.
  366. osStatus_t osThreadTerminate (osThreadId_t thread_id);

  367. /// Get number of active threads.
  368. /// \return number of active threads.
  369. uint32_t osThreadGetCount (void);

  370. /// Enumerate active threads.
  371. /// \param[out]    thread_array  pointer to array for retrieving thread IDs.
  372. /// \param[in]     array_items   maximum number of items in array for retrieving thread IDs.
  373. /// \return number of enumerated threads.
  374. uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items);


  375. //  ==== Thread Flags Functions ====

  376. /// Set the specified Thread Flags of a thread.
  377. /// \param[in]     thread_id     thread ID obtained by \ref osThreadNew or \ref osThreadGetId.
  378. /// \param[in]     flags         specifies the flags of the thread that shall be set.
  379. /// \return thread flags after setting or error code if highest bit set.
  380. uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags);

  381. /// Clear the specified Thread Flags of current running thread.
  382. /// \param[in]     flags         specifies the flags of the thread that shall be cleared.
  383. /// \return thread flags before clearing or error code if highest bit set.
  384. uint32_t osThreadFlagsClear (uint32_t flags);

  385. /// Get the current Thread Flags of current running thread.
  386. /// \return current thread flags.
  387. uint32_t osThreadFlagsGet (void);

  388. /// Wait for one or more Thread Flags of the current running thread to become signaled.
  389. /// \param[in]     flags         specifies the flags to wait for.
  390. /// \param[in]     options       specifies flags options (osFlagsXxxx).
  391. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  392. /// \return thread flags before clearing or error code if highest bit set.
  393. uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout);


  394. //  ==== Generic Wait Functions ====

  395. /// Wait for Timeout (Time Delay).
  396. /// \param[in]     ticks         \ref CMSIS_RTOS_TimeOutValue "time ticks" value
  397. /// \return status code that indicates the execution status of the function.
  398. osStatus_t osDelay (uint32_t ticks);

  399. /// Wait until specified time.
  400. /// \param[in]     ticks         absolute time in ticks
  401. /// \return status code that indicates the execution status of the function.
  402. osStatus_t osDelayUntil (uint32_t ticks);


  403. //  ==== Timer Management Functions ====

  404. /// Create and Initialize a timer.
  405. /// \param[in]     func          function pointer to callback function.
  406. /// \param[in]     type          \ref osTimerOnce for one-shot or \ref osTimerPeriodic for periodic behavior.
  407. /// \param[in]     argument      argument to the timer callback function.
  408. /// \param[in]     attr          timer attributes; NULL: default values.
  409. /// \return timer ID for reference by other functions or NULL in case of error.
  410. osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr);

  411. /// Get name of a timer.
  412. /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
  413. /// \return name as NULL terminated string.
  414. const char *osTimerGetName (osTimerId_t timer_id);

  415. /// Start or restart a timer.
  416. /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
  417. /// \param[in]     ticks         \ref CMSIS_RTOS_TimeOutValue "time ticks" value of the timer.
  418. /// \return status code that indicates the execution status of the function.
  419. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks);

  420. /// Stop a timer.
  421. /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
  422. /// \return status code that indicates the execution status of the function.
  423. osStatus_t osTimerStop (osTimerId_t timer_id);

  424. /// Check if a timer is running.
  425. /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
  426. /// \return 0 not running, 1 running.
  427. uint32_t osTimerIsRunning (osTimerId_t timer_id);

  428. /// Delete a timer.
  429. /// \param[in]     timer_id      timer ID obtained by \ref osTimerNew.
  430. /// \return status code that indicates the execution status of the function.
  431. osStatus_t osTimerDelete (osTimerId_t timer_id);


  432. //  ==== Event Flags Management Functions ====

  433. /// Create and Initialize an Event Flags object.
  434. /// \param[in]     attr          event flags attributes; NULL: default values.
  435. /// \return event flags ID for reference by other functions or NULL in case of error.
  436. osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr);

  437. /// Get name of an Event Flags object.
  438. /// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew.
  439. /// \return name as NULL terminated string.
  440. const char *osEventFlagsGetName (osEventFlagsId_t ef_id);

  441. /// Set the specified Event Flags.
  442. /// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew.
  443. /// \param[in]     flags         specifies the flags that shall be set.
  444. /// \return event flags after setting or error code if highest bit set.
  445. uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags);

  446. /// Clear the specified Event Flags.
  447. /// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew.
  448. /// \param[in]     flags         specifies the flags that shall be cleared.
  449. /// \return event flags before clearing or error code if highest bit set.
  450. uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags);

  451. /// Get the current Event Flags.
  452. /// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew.
  453. /// \return current event flags.
  454. uint32_t osEventFlagsGet (osEventFlagsId_t ef_id);

  455. /// Wait for one or more Event Flags to become signaled.
  456. /// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew.
  457. /// \param[in]     flags         specifies the flags to wait for.
  458. /// \param[in]     options       specifies flags options (osFlagsXxxx).
  459. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  460. /// \return event flags before clearing or error code if highest bit set.
  461. uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);

  462. /// Delete an Event Flags object.
  463. /// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew.
  464. /// \return status code that indicates the execution status of the function.
  465. osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id);


  466. //  ==== Mutex Management Functions ====

  467. /// Create and Initialize a Mutex object.
  468. /// \param[in]     attr          mutex attributes; NULL: default values.
  469. /// \return mutex ID for reference by other functions or NULL in case of error.
  470. osMutexId_t osMutexNew (const osMutexAttr_t *attr);

  471. /// Get name of a Mutex object.
  472. /// \param[in]     mutex_id      mutex ID obtained by \ref osMutexNew.
  473. /// \return name as NULL terminated string.
  474. const char *osMutexGetName (osMutexId_t mutex_id);

  475. /// Acquire a Mutex or timeout if it is locked.
  476. /// \param[in]     mutex_id      mutex ID obtained by \ref osMutexNew.
  477. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  478. /// \return status code that indicates the execution status of the function.
  479. osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout);

  480. /// Release a Mutex that was acquired by \ref osMutexAcquire.
  481. /// \param[in]     mutex_id      mutex ID obtained by \ref osMutexNew.
  482. /// \return status code that indicates the execution status of the function.
  483. osStatus_t osMutexRelease (osMutexId_t mutex_id);

  484. /// Get Thread which owns a Mutex object.
  485. /// \param[in]     mutex_id      mutex ID obtained by \ref osMutexNew.
  486. /// \return thread ID of owner thread or NULL when mutex was not acquired.
  487. osThreadId_t osMutexGetOwner (osMutexId_t mutex_id);

  488. /// Delete a Mutex object.
  489. /// \param[in]     mutex_id      mutex ID obtained by \ref osMutexNew.
  490. /// \return status code that indicates the execution status of the function.
  491. osStatus_t osMutexDelete (osMutexId_t mutex_id);


  492. //  ==== Semaphore Management Functions ====

  493. /// Create and Initialize a Semaphore object.
  494. /// \param[in]     max_count     maximum number of available tokens.
  495. /// \param[in]     initial_count initial number of available tokens.
  496. /// \param[in]     attr          semaphore attributes; NULL: default values.
  497. /// \return semaphore ID for reference by other functions or NULL in case of error.
  498. osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr);

  499. /// Get name of a Semaphore object.
  500. /// \param[in]     semaphore_id  semaphore ID obtained by \ref osSemaphoreNew.
  501. /// \return name as NULL terminated string.
  502. const char *osSemaphoreGetName (osSemaphoreId_t semaphore_id);

  503. /// Acquire a Semaphore token or timeout if no tokens are available.
  504. /// \param[in]     semaphore_id  semaphore ID obtained by \ref osSemaphoreNew.
  505. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  506. /// \return status code that indicates the execution status of the function.
  507. osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout);

  508. /// Release a Semaphore token up to the initial maximum count.
  509. /// \param[in]     semaphore_id  semaphore ID obtained by \ref osSemaphoreNew.
  510. /// \return status code that indicates the execution status of the function.
  511. osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id);

  512. /// Get current Semaphore token count.
  513. /// \param[in]     semaphore_id  semaphore ID obtained by \ref osSemaphoreNew.
  514. /// \return number of tokens available.
  515. uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id);

  516. /// Delete a Semaphore object.
  517. /// \param[in]     semaphore_id  semaphore ID obtained by \ref osSemaphoreNew.
  518. /// \return status code that indicates the execution status of the function.
  519. osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id);


  520. //  ==== Memory Pool Management Functions ====

  521. /// Create and Initialize a Memory Pool object.
  522. /// \param[in]     block_count   maximum number of memory blocks in memory pool.
  523. /// \param[in]     block_size    memory block size in bytes.
  524. /// \param[in]     attr          memory pool attributes; NULL: default values.
  525. /// \return memory pool ID for reference by other functions or NULL in case of error.
  526. osMemoryPoolId_t osMemoryPoolNew (uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr);

  527. /// Get name of a Memory Pool object.
  528. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  529. /// \return name as NULL terminated string.
  530. const char *osMemoryPoolGetName (osMemoryPoolId_t mp_id);

  531. /// Allocate a memory block from a Memory Pool.
  532. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  533. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  534. /// \return address of the allocated memory block or NULL in case of no memory is available.
  535. void *osMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout);

  536. /// Return an allocated memory block back to a Memory Pool.
  537. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  538. /// \param[in]     block         address of the allocated memory block to be returned to the memory pool.
  539. /// \return status code that indicates the execution status of the function.
  540. osStatus_t osMemoryPoolFree (osMemoryPoolId_t mp_id, void *block);

  541. /// Get maximum number of memory blocks in a Memory Pool.
  542. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  543. /// \return maximum number of memory blocks.
  544. uint32_t osMemoryPoolGetCapacity (osMemoryPoolId_t mp_id);

  545. /// Get memory block size in a Memory Pool.
  546. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  547. /// \return memory block size in bytes.
  548. uint32_t osMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id);

  549. /// Get number of memory blocks used in a Memory Pool.
  550. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  551. /// \return number of memory blocks used.
  552. uint32_t osMemoryPoolGetCount (osMemoryPoolId_t mp_id);

  553. /// Get number of memory blocks available in a Memory Pool.
  554. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  555. /// \return number of memory blocks available.
  556. uint32_t osMemoryPoolGetSpace (osMemoryPoolId_t mp_id);

  557. /// Delete a Memory Pool object.
  558. /// \param[in]     mp_id         memory pool ID obtained by \ref osMemoryPoolNew.
  559. /// \return status code that indicates the execution status of the function.
  560. osStatus_t osMemoryPoolDelete (osMemoryPoolId_t mp_id);


  561. //  ==== Message Queue Management Functions ====

  562. /// Create and Initialize a Message Queue object.
  563. /// \param[in]     msg_count     maximum number of messages in queue.
  564. /// \param[in]     msg_size      maximum message size in bytes.
  565. /// \param[in]     attr          message queue attributes; NULL: default values.
  566. /// \return message queue ID for reference by other functions or NULL in case of error.
  567. osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);

  568. /// Get name of a Message Queue object.
  569. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  570. /// \return name as NULL terminated string.
  571. const char *osMessageQueueGetName (osMessageQueueId_t mq_id);

  572. /// Put a Message into a Queue or timeout if Queue is full.
  573. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  574. /// \param[in]     msg_ptr       pointer to buffer with message to put into a queue.
  575. /// \param[in]     msg_prio      message priority.
  576. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  577. /// \return status code that indicates the execution status of the function.
  578. osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout);

  579. /// Get a Message from a Queue or timeout if Queue is empty.
  580. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  581. /// \param[out]    msg_ptr       pointer to buffer for message to get from a queue.
  582. /// \param[out]    msg_prio      pointer to buffer for message priority or NULL.
  583. /// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out.
  584. /// \return status code that indicates the execution status of the function.
  585. osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout);

  586. /// Get maximum number of messages in a Message Queue.
  587. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  588. /// \return maximum number of messages.
  589. uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id);

  590. /// Get maximum message size in a Memory Pool.
  591. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  592. /// \return maximum message size in bytes.
  593. uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id);

  594. /// Get number of queued messages in a Message Queue.
  595. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  596. /// \return number of queued messages.
  597. uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id);

  598. /// Get number of available slots for messages in a Message Queue.
  599. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  600. /// \return number of available slots for messages.
  601. uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id);

  602. /// Reset a Message Queue to initial empty state.
  603. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  604. /// \return status code that indicates the execution status of the function.
  605. osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id);

  606. /// Delete a Message Queue object.
  607. /// \param[in]     mq_id         message queue ID obtained by \ref osMessageQueueNew.
  608. /// \return status code that indicates the execution status of the function.
  609. osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id);


  610. #ifdef  __cplusplus
  611. }
  612. #endif

  613. #endif  // CMSIS_OS2_H_
复制代码

归档链接:
CMSIS-RTOS2 文档翻译 之 简介
CMSIS-RTOS2 文档翻译 之 修订记录
CMSIS-RTOS2 文档翻译 之 功能概述
CMSIS-RTOS2 文档翻译 之 RTOS 验证
收藏 评论4 发布时间:2018-4-21 15:48

举报

4个回答
黑皮男 回答时间:2018-4-21 16:36:17
支持,我现在用的是cmsis_os接口
XinLiYF 回答时间:2018-4-22 09:14:50
黑皮男 发表于 2018-4-21 16:36
支持,我现在用的是cmsis_os接口

就是相关的教程和文档比较少,用的人比较少,你很有前瞻性啊。
黑皮男 回答时间:2018-4-22 09:42:30
XinLiYF 发表于 2018-4-22 09:14
就是相关的教程和文档比较少,用的人比较少,你很有前瞻性啊。

之前都是自己封装RTOS的接口,发现cmsis_os后,既然都有统一的接口,干脆就拿来用了,比自己封装的要好很多
XinLiYF 回答时间:2018-4-22 10:26:00
黑皮男 发表于 2018-4-22 09:42
之前都是自己封装RTOS的接口,发现cmsis_os后,既然都有统一的接口,干脆就拿来用了,比自己封装的要好很 ...

刚去你 GitHub 逛啦一圈 ,你肯定是一个外表粗犷内心细腻的人 ,GitHub 上关注你了

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版