Procházet zdrojové kódy

RTX5: improved MISRA compliance (various items)

Robert Rostohar před 8 roky
rodič
revize
de65d1dafa

+ 1 - 0
CMSIS/RTOS2/RTX/Config/RTX_Config.c

@@ -56,6 +56,7 @@ __WEAK uint32_t osRtxErrorNotify (uint32_t code, void *object_id) {
       // Standard C/C++ library mutex initialization failed
       break;
     default:
+      // Reserved
       break;
   }
   for (;;) {}

+ 23 - 23
CMSIS/RTOS2/RTX/Include/rtx_os.h

@@ -85,15 +85,15 @@ extern "C"
 #define osRtxThreadBlocked              ((uint8_t)osThreadBlocked)
 #define osRtxThreadTerminated           ((uint8_t)osThreadTerminated)
  
-#define osRtxThreadWaitingDelay         (osRtxThreadBlocked | 0x10U)
-#define osRtxThreadWaitingJoin          (osRtxThreadBlocked | 0x20U)
-#define osRtxThreadWaitingThreadFlags   (osRtxThreadBlocked | 0x30U) 
-#define osRtxThreadWaitingEventFlags    (osRtxThreadBlocked | 0x40U) 
-#define osRtxThreadWaitingMutex         (osRtxThreadBlocked | 0x50U)
-#define osRtxThreadWaitingSemaphore     (osRtxThreadBlocked | 0x60U)
-#define osRtxThreadWaitingMemoryPool    (osRtxThreadBlocked | 0x70U)
-#define osRtxThreadWaitingMessageGet    (osRtxThreadBlocked | 0x80U)
-#define osRtxThreadWaitingMessagePut    (osRtxThreadBlocked | 0x90U)
+#define osRtxThreadWaitingDelay         ((uint8_t)(osRtxThreadBlocked | 0x10U))
+#define osRtxThreadWaitingJoin          ((uint8_t)(osRtxThreadBlocked | 0x20U))
+#define osRtxThreadWaitingThreadFlags   ((uint8_t)(osRtxThreadBlocked | 0x30U))
+#define osRtxThreadWaitingEventFlags    ((uint8_t)(osRtxThreadBlocked | 0x40U))
+#define osRtxThreadWaitingMutex         ((uint8_t)(osRtxThreadBlocked | 0x50U))
+#define osRtxThreadWaitingSemaphore     ((uint8_t)(osRtxThreadBlocked | 0x60U))
+#define osRtxThreadWaitingMemoryPool    ((uint8_t)(osRtxThreadBlocked | 0x70U))
+#define osRtxThreadWaitingMessageGet    ((uint8_t)(osRtxThreadBlocked | 0x80U))
+#define osRtxThreadWaitingMessagePut    ((uint8_t)(osRtxThreadBlocked | 0x90U))
  
 /// Thread Flags definitions
 #define osRtxThreadFlagDefStack 0x10U   ///< Default Stack flag
@@ -142,7 +142,7 @@ typedef struct osRtxThread_s {
  
 /// Timer Function Information
 typedef struct {
-  void                            *fp;  ///< Function Pointer
+  osTimerFunc_t                  func;  ///< Function Pointer
   void                           *arg;  ///< Function Argument
 } osRtxTimerFinfo_t;
  
@@ -164,7 +164,7 @@ typedef struct osRtxTimer_s {
 //  ==== Event Flags definitions ====
  
 /// Event Flags Control Block
-typedef struct osRtxEventFlags_s {
+typedef struct {
   uint8_t                          id;  ///< Object Identifier
   uint8_t                       state;  ///< Object State
   uint8_t                       flags;  ///< Object Flags
@@ -196,7 +196,7 @@ typedef struct osRtxMutex_s {
 //  ==== Semaphore definitions ====
  
 /// Semaphore Control Block
-typedef struct osRtxSemaphore_s {
+typedef struct {
   uint8_t                          id;  ///< Object Identifier
   uint8_t                       state;  ///< Object State
   uint8_t                       flags;  ///< Object Flags
@@ -211,7 +211,7 @@ typedef struct osRtxSemaphore_s {
 //  ==== Memory Pool definitions ====
  
 /// Memory Pool Information
-typedef struct osRtxMpInfo_s {
+typedef struct {
   uint32_t                 max_blocks;  ///< Maximum number of Blocks
   uint32_t                used_blocks;  ///< Number of used Blocks
   uint32_t                 block_size;  ///< Block Size
@@ -221,7 +221,7 @@ typedef struct osRtxMpInfo_s {
 } osRtxMpInfo_t;
  
 /// Memory Pool Control Block
-typedef struct osRtxMemoryPool_s {
+typedef struct {
   uint8_t                          id;  ///< Object Identifier
   uint8_t                       state;  ///< Object State
   uint8_t                       flags;  ///< Object Flags
@@ -245,7 +245,7 @@ typedef struct osRtxMessage_s {
 } osRtxMessage_t;
  
 /// Message Queue Control Block
-typedef struct osRtxMessageQueue_s {
+typedef struct {
   uint8_t                          id;  ///< Object Identifier
   uint8_t                       state;  ///< Object State
   uint8_t                       flags;  ///< Object Flags
@@ -263,7 +263,7 @@ typedef struct osRtxMessageQueue_s {
 //  ==== Generic Object definitions ====
  
 /// Generic Object Control Block
-typedef struct osRtxObject_s {
+typedef struct {
   uint8_t                          id;  ///< Object Identifier
   uint8_t                       state;  ///< Object State
   uint8_t                       flags;  ///< Object Flags
@@ -292,7 +292,7 @@ typedef struct {
       osRtxThread_t             *curr;  ///< Current running Thread
       osRtxThread_t             *next;  ///< Next Thread to Run
     } run;
-    volatile osRtxObject_t      ready;  ///< Ready List Object
+    osRtxObject_t               ready;  ///< Ready List Object
     osRtxThread_t               *idle;  ///< Idle Thread
     osRtxThread_t         *delay_list;  ///< Delay List
     osRtxThread_t          *wait_list;  ///< Wait List (no Timeout)
@@ -321,7 +321,7 @@ typedef struct {
     void (*event_flags)(osRtxEventFlags_t*);    ///< Event Flags Post Processing function
     void    (*semaphore)(osRtxSemaphore_t*);    ///< Semaphore Post Processing function
     void (*memory_pool)(osRtxMemoryPool_t*);    ///< Memory Pool Post Processing function
-    void  (*message_queue)(osRtxMessage_t*);    ///< Message Queue Post Processing function
+    void        (*message)(osRtxMessage_t*);    ///< Message Post Processing function
   } post_process;
   struct {                              ///< Memory Pools (Variable Block Size)
     void                       *stack;  ///< Stack Memory
@@ -377,11 +377,11 @@ extern osRtxInfo_t osRtxInfo;           ///< OS Runtime Information
 //  ==== OS External Functions ====
  
 /// OS Error Codes
-#define osRtxErrorStackUnderflow        1U ///< Stack overflow, i.e. stack pointer below its lower memory limit for descending stacks.
-#define osRtxErrorISRQueueOverflow      2U ///< ISR Queue overflow detected when inserting object.
-#define osRtxErrorTimerQueueOverflow    3U ///< User Timer Callback Queue overflow detected for timer.
-#define osRtxErrorClibSpace             4U ///< Standard C/C++ library libspace not available: increase \c OS_THREAD_LIBSPACE_NUM.
-#define osRtxErrorClibMutex             5U ///< Standard C/C++ library mutex initialization failed.
+#define osRtxErrorStackUnderflow        1U  ///< Stack overflow, i.e. stack pointer below its lower memory limit for descending stacks.
+#define osRtxErrorISRQueueOverflow      2U  ///< ISR Queue overflow detected when inserting object.
+#define osRtxErrorTimerQueueOverflow    3U  ///< User Timer Callback Queue overflow detected for timer.
+#define osRtxErrorClibSpace             4U  ///< Standard C/C++ library libspace not available: increase \c OS_THREAD_LIBSPACE_NUM.
+#define osRtxErrorClibMutex             5U  ///< Standard C/C++ library mutex initialization failed.
  
 /// OS Error Callback function
 extern uint32_t osRtxErrorNotify (uint32_t code, void *object_id);

+ 11 - 11
CMSIS/RTOS2/RTX/Source/rtx_evflags.c

@@ -218,7 +218,7 @@ static osEventFlagsId_t svcRtxEventFlagsNew (const osEventFlagsAttr_t *attr) {
 /// Get name of an Event Flags object.
 /// \note API identical to osEventFlagsGetName
 static const char *svcRtxEventFlagsGetName (osEventFlagsId_t ef_id) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
 
   // Check parameters
   if ((ef == NULL) || (ef->id != osRtxIdEventFlags)) {
@@ -240,7 +240,7 @@ static const char *svcRtxEventFlagsGetName (osEventFlagsId_t ef_id) {
 /// Set the specified Event Flags.
 /// \note API identical to osEventFlagsSet
 static uint32_t svcRtxEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
   os_thread_t      *thread;
   os_thread_t      *thread_next;
   uint32_t          event_flags;
@@ -289,7 +289,7 @@ static uint32_t svcRtxEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
 /// Clear the specified Event Flags.
 /// \note API identical to osEventFlagsClear
 static uint32_t svcRtxEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
   uint32_t          event_flags;
 
   // Check parameters
@@ -316,7 +316,7 @@ static uint32_t svcRtxEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags) {
 /// Get the current Event Flags.
 /// \note API identical to osEventFlagsGet
 static uint32_t svcRtxEventFlagsGet (osEventFlagsId_t ef_id) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
 
   // Check parameters
   if ((ef == NULL) || (ef->id != osRtxIdEventFlags)) {
@@ -338,7 +338,7 @@ static uint32_t svcRtxEventFlagsGet (osEventFlagsId_t ef_id) {
 /// Wait for one or more Event Flags to become signaled.
 /// \note API identical to osEventFlagsWait
 static uint32_t svcRtxEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
   os_thread_t      *running_thread;
   uint32_t          event_flags;
 
@@ -375,7 +375,7 @@ static uint32_t svcRtxEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, ui
       running_thread->flags_options = (uint8_t)options;
       // Suspend current Thread
       if (osRtxThreadWaitEnter(osRtxThreadWaitingEventFlags, timeout)) {
-        osRtxThreadListPut((os_object_t*)ef, running_thread, running_thread);
+        osRtxThreadListPut(osRtxObject(ef), running_thread);
       } else {
         EvrRtxEventFlagsWaitTimeout(ef);
       }
@@ -392,7 +392,7 @@ static uint32_t svcRtxEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, ui
 /// Delete an Event Flags object.
 /// \note API identical to osEventFlagsDelete
 static osStatus_t svcRtxEventFlagsDelete (osEventFlagsId_t ef_id) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
   os_thread_t      *thread;
 
   // Check parameters
@@ -413,7 +413,7 @@ static osStatus_t svcRtxEventFlagsDelete (osEventFlagsId_t ef_id) {
   // Unblock waiting threads
   if (ef->thread_list != NULL) {
     do {
-      thread = osRtxThreadListGet((os_object_t*)ef);
+      thread = osRtxThreadListGet(osRtxObject(ef));
       osRtxThreadWaitExit(thread, (uint32_t)osErrorResource, FALSE);
     } while (ef->thread_list != NULL);
     osRtxThreadDispatch(NULL);
@@ -449,7 +449,7 @@ SVC0_1(EventFlagsDelete,  osStatus_t,       osEventFlagsId_t)
 /// \note API identical to osEventFlagsSet
 __STATIC_INLINE
 uint32_t isrRtxEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
   uint32_t          event_flags;
 
   // Check parameters
@@ -469,7 +469,7 @@ uint32_t isrRtxEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
   event_flags = EventFlagsSet(ef, flags);
 
   // Register post ISR processing
-  osRtxPostProcess((os_object_t *)ef);
+  osRtxPostProcess(osRtxObject(ef));
 
   EvrRtxEventFlagsSetDone(ef, event_flags);
 
@@ -480,7 +480,7 @@ uint32_t isrRtxEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
 /// \note API identical to osEventFlagsWait
 __STATIC_INLINE
 uint32_t isrRtxEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout) {
-  os_event_flags_t *ef = (os_event_flags_t *)ef_id;
+  os_event_flags_t *ef = osRtxEventFlagsId(ef_id);
   uint32_t          event_flags;
 
   // Check parameters

+ 11 - 7
CMSIS/RTOS2/RTX/Source/rtx_kernel.c

@@ -199,6 +199,7 @@ static osStatus_t svcRtxKernelInitialize (void) {
 ///  Get RTOS Kernel Information.
 /// \note API identical to osKernelGetInfo
 static osStatus_t svcRtxKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
+  uint32_t size;
 
   if (version != NULL) {
     version->api    = osRtxVersionAPI;
@@ -207,9 +208,11 @@ static osStatus_t svcRtxKernelGetInfo (osVersion_t *version, char *id_buf, uint3
 
   if ((id_buf != NULL) && (id_size != 0U)) {
     if (id_size > sizeof(osRtxKernelId)) {
-      id_size = sizeof(osRtxKernelId);
+      size = sizeof(osRtxKernelId);
+    } else {
+      size = id_size;
     }
-    memcpy(id_buf, osRtxKernelId, id_size);
+    memcpy(id_buf, osRtxKernelId, size);
   }
 
   EvrRtxKernelInfoRetrieved(version, id_buf);
@@ -220,8 +223,9 @@ static osStatus_t svcRtxKernelGetInfo (osVersion_t *version, char *id_buf, uint3
 /// Get the current RTOS Kernel state.
 /// \note API identical to osKernelGetState
 static osKernelState_t svcRtxKernelGetState (void) {
-  EvrRtxKernelGetState((osKernelState_t)(osRtxInfo.kernel.state));
-  return ((osKernelState_t)(osRtxInfo.kernel.state));
+  osKernelState_t state = osRtxKernelState();
+  EvrRtxKernelGetState(state);
+  return state;
 }
 
 /// Start the RTOS Kernel scheduler.
@@ -358,9 +362,9 @@ static int32_t svcRtxKernelRestoreLock (int32_t lock) {
 /// Suspend the RTOS Kernel scheduler.
 /// \note API identical to osKernelSuspend
 static uint32_t svcRtxKernelSuspend (void) {
-  os_thread_t *thread;
-  os_timer_t  *timer;
-  uint32_t     delay;
+  const os_thread_t *thread;
+  const os_timer_t  *timer;
+  uint32_t           delay;
 
   if (osRtxInfo.kernel.state != osRtxKernelRunning) {
     EvrRtxKernelError(osRtxErrorKernelNotRunning);

+ 25 - 22
CMSIS/RTOS2/RTX/Source/rtx_lib.c

@@ -85,13 +85,13 @@ __attribute__((section(".bss.os.thread.stack")));
 // Memory Pool for Thread Control Blocks
 static osRtxMpInfo_t os_mpi_thread \
 __attribute__((section(".data.os.thread.mpi"))) =
-{ (uint32_t)OS_THREAD_NUM, 0U, (uint32_t)osRtxThreadCbSize, &os_thread_cb, NULL, NULL };
+{ (uint32_t)OS_THREAD_NUM, 0U, (uint32_t)osRtxThreadCbSize, &os_thread_cb[0], NULL, NULL };
 
 // Memory Pool for Thread Default Stack
 #if (OS_THREAD_DEF_STACK_NUM != 0)
 static osRtxMpInfo_t os_mpi_def_stack \
 __attribute__((section(".data.os.thread.mpi"))) =
-{ (uint32_t)OS_THREAD_DEF_STACK_NUM, 0U, (uint32_t)OS_STACK_SIZE, &os_thread_def_stack, NULL, NULL };
+{ (uint32_t)OS_THREAD_DEF_STACK_NUM, 0U, (uint32_t)OS_STACK_SIZE, &os_thread_def_stack[0], NULL, NULL };
 #endif
 
 // Memory Pool for Thread Stack
@@ -125,7 +125,7 @@ static const osThreadAttr_t os_idle_thread_attr = {
   osThreadDetached,
   &os_idle_thread_cb,
   (uint32_t)sizeof(os_idle_thread_cb),
-  &os_idle_thread_stack,
+  &os_idle_thread_stack[0],
   (uint32_t)sizeof(os_idle_thread_stack),
   osPriorityIdle,
 #if defined(OS_IDLE_THREAD_TZ_MOD_ID)
@@ -153,7 +153,7 @@ __attribute__((section(".bss.os.timer.cb")));
 // Memory Pool for Timer Control Blocks
 static osRtxMpInfo_t os_mpi_timer \
 __attribute__((section(".data.os.timer.mpi"))) =
-{ (uint32_t)OS_TIMER_NUM, 0U, (uint32_t)osRtxTimerCbSize, &os_timer_cb, NULL, NULL };
+{ (uint32_t)OS_TIMER_NUM, 0U, (uint32_t)osRtxTimerCbSize, &os_timer_cb[0], NULL, NULL };
 
 #endif  // (OS_TIMER_OBJ_MEM != 0)
 
@@ -178,7 +178,7 @@ static const osThreadAttr_t os_timer_thread_attr = {
   osThreadDetached,
   &os_timer_thread_cb,
   (uint32_t)sizeof(os_timer_thread_cb),
-  &os_timer_thread_stack,
+  &os_timer_thread_stack[0],
   (uint32_t)sizeof(os_timer_thread_stack),
   (osPriority_t)OS_TIMER_THREAD_PRIO,
 #if defined(OS_TIMER_THREAD_TZ_MOD_ID)
@@ -203,7 +203,7 @@ static const osMessageQueueAttr_t os_timer_mq_attr = {
   0U,
   &os_timer_mq_cb,
   (uint32_t)sizeof(os_timer_mq_cb),
-  &os_timer_mq_data,
+  &os_timer_mq_data[0],
   (uint32_t)sizeof(os_timer_mq_data)
 };
 
@@ -231,7 +231,7 @@ __attribute__((section(".bss.os.evflags.cb")));
 // Memory Pool for Event Flags Control Blocks
 static osRtxMpInfo_t os_mpi_ef \
 __attribute__((section(".data.os.evflags.mpi"))) =
-{ (uint32_t)OS_EVFLAGS_NUM, 0U, (uint32_t)osRtxEventFlagsCbSize, &os_ef_cb, NULL, NULL };
+{ (uint32_t)OS_EVFLAGS_NUM, 0U, (uint32_t)osRtxEventFlagsCbSize, &os_ef_cb[0], NULL, NULL };
 
 #endif  // (OS_EVFLAGS_OBJ_MEM != 0)
 
@@ -252,7 +252,7 @@ __attribute__((section(".bss.os.mutex.cb")));
 // Memory Pool for Mutex Control Blocks
 static osRtxMpInfo_t os_mpi_mutex \
 __attribute__((section(".data.os.mutex.mpi"))) =
-{ (uint32_t)OS_MUTEX_NUM, 0U, (uint32_t)osRtxMutexCbSize, &os_mutex_cb, NULL, NULL };
+{ (uint32_t)OS_MUTEX_NUM, 0U, (uint32_t)osRtxMutexCbSize, &os_mutex_cb[0], NULL, NULL };
 
 #endif  // (OS_MUTEX_OBJ_MEM != 0)
 
@@ -273,7 +273,7 @@ __attribute__((section(".bss.os.semaphore.cb")));
 // Memory Pool for Semaphore Control Blocks
 static osRtxMpInfo_t os_mpi_semaphore \
 __attribute__((section(".data.os.semaphore.mpi"))) =
-{ (uint32_t)OS_SEMAPHORE_NUM, 0U, (uint32_t)osRtxSemaphoreCbSize, &os_semaphore_cb, NULL, NULL };
+{ (uint32_t)OS_SEMAPHORE_NUM, 0U, (uint32_t)osRtxSemaphoreCbSize, &os_semaphore_cb[0], NULL, NULL };
 
 #endif  // (OS_SEMAPHORE_OBJ_MEM != 0)
 
@@ -294,7 +294,7 @@ __attribute__((section(".bss.os.mempool.cb")));
 // Memory Pool for Memory Pool Control Blocks
 static osRtxMpInfo_t os_mpi_mp \
 __attribute__((section(".data.os.mempool.mpi"))) =
-{ (uint32_t)OS_MEMPOOL_NUM, 0U, (uint32_t)osRtxMemoryPoolCbSize, &os_mp_cb, NULL, NULL };
+{ (uint32_t)OS_MEMPOOL_NUM, 0U, (uint32_t)osRtxMemoryPoolCbSize, &os_mp_cb[0], NULL, NULL };
 
 // Memory Pool for Memory Pool Data Storage
 #if (OS_MEMPOOL_DATA_SIZE != 0)
@@ -324,7 +324,7 @@ __attribute__((section(".bss.os.msgqueue.cb")));
 // Memory Pool for Message Queue Control Blocks
 static osRtxMpInfo_t os_mpi_mq \
 __attribute__((section(".data.os.msgqueue.mpi"))) =
-{ (uint32_t)OS_MSGQUEUE_NUM, 0U, (uint32_t)osRtxMessageQueueCbSize, &os_mq_cb, NULL, NULL };
+{ (uint32_t)OS_MSGQUEUE_NUM, 0U, (uint32_t)osRtxMessageQueueCbSize, &os_mq_cb[0], NULL, NULL };
 
 // Memory Pool for Message Queue Data Storage
 #if (OS_MSGQUEUE_DATA_SIZE != 0)
@@ -341,9 +341,11 @@ __attribute__((section(".bss.os.msgqueue.mem")));
 // OS Configuration
 // ================
 
-__USED
-__attribute__((section(".rodata")))
-const osRtxConfig_t osRtxConfig = {
+
+const osRtxConfig_t osRtxConfig \
+__USED \
+__attribute__((section(".rodata"))) =
+{
   0U   // Flags
 #if (OS_PRIVILEGE_MODE != 0)
   | osRtxConfigPrivilegedMode
@@ -361,26 +363,26 @@ const osRtxConfig_t osRtxConfig = {
 #else
   0U,
 #endif
-  { &os_isr_queue[0], sizeof(os_isr_queue)/sizeof(void *), 0U },
+  { &os_isr_queue[0], (uint16_t)(sizeof(os_isr_queue)/sizeof(void *)), 0U },
   { 
     // Memory Pools (Variable Block Size)
 #if ((OS_THREAD_OBJ_MEM != 0) && (OS_THREAD_USER_STACK_SIZE != 0))
-    &os_thread_stack, sizeof(os_thread_stack),
+    &os_thread_stack[0], sizeof(os_thread_stack),
 #else
     NULL, 0U,
 #endif
 #if ((OS_MEMPOOL_OBJ_MEM != 0) && (OS_MEMPOOL_DATA_SIZE != 0))
-    &os_mp_data, sizeof(os_mp_data),
+    &os_mp_data[0], sizeof(os_mp_data),
 #else
     NULL, 0U,
 #endif
 #if ((OS_MSGQUEUE_OBJ_MEM != 0) && (OS_MSGQUEUE_DATA_SIZE != 0))
-    &os_mq_data, sizeof(os_mq_data),
+    &os_mq_data[0], sizeof(os_mq_data),
 #else
     NULL, 0U,
 #endif
 #if (OS_DYNAMIC_MEM_SIZE != 0)
-    &os_mem, (uint32_t)OS_DYNAMIC_MEM_SIZE,
+    &os_mem[0], (uint32_t)OS_DYNAMIC_MEM_SIZE,
 #else
     NULL, 0U
 #endif
@@ -497,8 +499,9 @@ __asm (".global os_cb_sections");
 
 extern const uint32_t os_cb_sections[];
 
-__attribute__((section(".rodata")))
-const uint32_t os_cb_sections[] = {
+const uint32_t os_cb_sections[] \
+__attribute__((section(".rodata"))) =
+{
   (uint32_t)&__os_thread_cb_start__,
   (uint32_t)&__os_thread_cb_end__,
   (uint32_t)&__os_timer_cb_start__,
@@ -552,7 +555,7 @@ __WEAK void software_init_hook (void) {
 #define LIBSPACE_SIZE 96
 
 // Memory for libspace
-static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/sizeof(uint32_t)] \
+static uint32_t os_libspace[OS_THREAD_LIBSPACE_NUM+1][LIBSPACE_SIZE/4] \
 __attribute__((section(".bss.os")));
 
 // Thread IDs for libspace

+ 99 - 10
CMSIS/RTOS2/RTX/Source/rtx_lib.h

@@ -54,24 +54,113 @@
 
 //  ==== Inline functions ====
 
-// Kernel Inline functions
-__STATIC_INLINE uint8_t      osRtxKernelGetState   (void) { return osRtxInfo.kernel.state; }
-
-// Thread Inline functions
-__STATIC_INLINE os_thread_t *osRtxThreadGetRunning (void) { return osRtxInfo.thread.run.curr; }
-__STATIC_INLINE void         osRtxThreadSetRunning (os_thread_t *thread) { osRtxInfo.thread.run.curr = thread; }
+// Thread ID
+__STATIC_INLINE os_thread_t *osRtxThreadId (osThreadId_t thread_id) {
+  return ((os_thread_t *)thread_id);
+}
+// Timer ID
+__STATIC_INLINE os_timer_t *osRtxTimerId (osTimerId_t timer_id) {
+  return ((os_timer_t *)timer_id);
+}
+// Event Flags ID
+__STATIC_INLINE os_event_flags_t *osRtxEventFlagsId (osEventFlagsId_t ef_id) {
+  return ((os_event_flags_t *)ef_id);
+}
+// Mutex ID
+__STATIC_INLINE os_mutex_t *osRtxMutexId (osMutexId_t mutex_id) {
+  return ((os_mutex_t *)mutex_id);
+}
+// Semaphore ID
+__STATIC_INLINE os_semaphore_t *osRtxSemaphoreId (osSemaphoreId_t semaphore_id) {
+  return ((os_semaphore_t *)semaphore_id);
+}
+// Memory Pool ID
+__STATIC_INLINE os_memory_pool_t *osRtxMemoryPoolId (osMemoryPoolId_t mp_id) {
+  return ((os_memory_pool_t *)mp_id);
+}
+// Message Queue ID
+__STATIC_INLINE os_message_queue_t *osRtxMessageQueueId (osMessageQueueId_t mq_id) {
+  return ((os_message_queue_t *)mq_id);
+}
+
+// Generic Object
+__STATIC_INLINE os_object_t *osRtxObject (void *object) {
+  return ((os_object_t *)object);
+}
+
+// Thread Object
+__STATIC_INLINE os_thread_t *osRtxThreadObject (os_object_t *object) {
+  return ((os_thread_t *)object);
+}
+// Timer Object
+__STATIC_INLINE os_timer_t *osRtxTimerObject (os_object_t *object) {
+  return ((os_timer_t *)object);
+}
+// Event Flags Object
+__STATIC_INLINE os_event_flags_t *osRtxEventFlagsObject (os_object_t *object) {
+  return ((os_event_flags_t *)object);
+}
+// Mutex Object
+__STATIC_INLINE os_mutex_t *osRtxMutexObject (os_object_t *object) {
+  return ((os_mutex_t *)object);
+}
+// Semaphore Object
+__STATIC_INLINE os_semaphore_t *osRtxSemaphoreObject (os_object_t *object) {
+  return ((os_semaphore_t *)object);
+}
+// Memory Pool Object
+__STATIC_INLINE os_memory_pool_t *osRtxMemoryPoolObject (os_object_t *object) {
+  return ((os_memory_pool_t *)object);
+}
+// Message Queue Object
+__STATIC_INLINE os_message_queue_t *osRtxMessageQueueObject (os_object_t *object) {
+  return ((os_message_queue_t *)object);
+}
+// Message Object
+__STATIC_INLINE os_message_t *osRtxMessageObject (os_object_t *object) {
+  return ((os_message_t *)object);
+}
+
+// Kernel State
+__STATIC_INLINE osKernelState_t osRtxKernelState (void) {
+  return ((osKernelState_t)(osRtxInfo.kernel.state));
+}
+
+// Thread State
+__STATIC_INLINE osThreadState_t osRtxThreadState (const os_thread_t *thread) {
+  uint8_t state = thread->state & osRtxThreadStateMask;
+  return ((osThreadState_t)state);
+}
+
+// Thread Priority
+__STATIC_INLINE osPriority_t osRtxThreadPriority (const os_thread_t *thread) {
+  return ((osPriority_t)thread->priority);
+}
+
+// Kernel Get State
+__STATIC_INLINE uint8_t osRtxKernelGetState (void) {
+  return osRtxInfo.kernel.state;
+}
+
+// Thread Get/Set Running
+__STATIC_INLINE os_thread_t *osRtxThreadGetRunning (void) {
+  return osRtxInfo.thread.run.curr;
+}
+__STATIC_INLINE void osRtxThreadSetRunning (os_thread_t *thread) {
+  osRtxInfo.thread.run.curr = thread;
+}
 
 
 //  ==== Library functions ====
 
 // Thread Library functions
-extern void         osRtxThreadListPut    (volatile os_object_t *object, os_thread_t *thread);
-extern os_thread_t *osRtxThreadListGet    (volatile os_object_t *object);
+extern void         osRtxThreadListPut    (os_object_t *object, os_thread_t *thread);
+extern os_thread_t *osRtxThreadListGet    (os_object_t *object);
 extern void         osRtxThreadListSort   (os_thread_t *thread);
 extern void         osRtxThreadListRemove (os_thread_t *thread);
 extern void         osRtxThreadReadyPut   (os_thread_t *thread);
 extern void         osRtxThreadDelayTick  (void);
-extern uint32_t    *osRtxThreadRegPtr     (os_thread_t *thread);
+extern uint32_t    *osRtxThreadRegPtr     (const os_thread_t *thread);
 extern void         osRtxThreadSwitch     (os_thread_t *thread);
 extern void         osRtxThreadDispatch   (os_thread_t *thread);
 extern void         osRtxThreadWaitExit   (os_thread_t *thread, uint32_t ret_val, bool_t dispatch);
@@ -91,7 +180,7 @@ extern void    *osRtxMemoryAlloc(void *mem, uint32_t size, uint32_t type);
 extern uint32_t osRtxMemoryFree (void *mem, void *block);
 
 // Memory Pool Library functions
-extern uint32_t   osRtxMemoryPoolInit  (os_mp_info_t *mp_info, uint32_t blocks, uint32_t block_size, void *block_mem);
+extern uint32_t   osRtxMemoryPoolInit  (os_mp_info_t *mp_info, uint32_t block_count, uint32_t block_size, void *block_mem);
 extern void      *osRtxMemoryPoolAlloc (os_mp_info_t *mp_info);
 extern osStatus_t osRtxMemoryPoolFree  (os_mp_info_t *mp_info, void *block);
 

+ 39 - 19
CMSIS/RTOS2/RTX/Source/rtx_memory.c

@@ -27,7 +27,7 @@
 
 
 //  Memory Pool Header structure
-typedef struct mem_head_s {
+typedef struct {
   uint32_t size;                // Memory Pool size
   uint32_t used;                // Used Memory
 } mem_head_t;
@@ -41,6 +41,22 @@ typedef struct mem_block_s {
 #define MB_INFO_LEN_MASK        0xFFFFFFFCU
 #define MB_INFO_TYPE_MASK       0x00000003U
 
+//  Memory Head Pointer
+__STATIC_INLINE mem_head_t *MemHeadPtr (void *mem) {
+  return ((mem_head_t *)mem);
+}
+
+//  Memory Block Pointer
+__STATIC_INLINE mem_block_t *MemBlockPtr (void *mem, uint32_t offset) {
+  uint32_t     addr;
+  mem_block_t *ptr;
+
+  addr = (uint32_t)mem + offset;
+  ptr  = (mem_block_t *)addr;
+
+  return ptr;
+}
+
 
 //  ==== Library functions ====
 
@@ -58,12 +74,12 @@ __WEAK uint32_t osRtxMemoryInit (void *mem, uint32_t size) {
     return 0U;
   }
 
-  head = (mem_head_t *)mem;
+  head = MemHeadPtr(mem);
   head->size = size;
   head->used = sizeof(mem_head_t) + sizeof(mem_block_t);
 
-  ptr = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
-  ptr->next = (mem_block_t *)((uint32_t)mem + size - sizeof(mem_block_t));
+  ptr = MemBlockPtr(mem, sizeof(mem_head_t));
+  ptr->next = MemBlockPtr(mem, size - sizeof(mem_block_t));
   ptr->next->next = NULL;
   ptr->info = 0U;
 
@@ -78,7 +94,9 @@ __WEAK uint32_t osRtxMemoryInit (void *mem, uint32_t size) {
 /// \param[in]  type            memory block type: 0 - generic, 1 - control block
 /// \return allocated memory block or NULL in case of no memory is available.
 __WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
-  mem_block_t *p, *p_new, *ptr;
+  mem_block_t *ptr;
+  mem_block_t *p, *p_new;
+  uint32_t     block_size;
   uint32_t     hole_size;
 
   if ((mem == NULL) || (size == 0U) || ((type & ~MB_INFO_TYPE_MASK) != 0U)) {
@@ -87,16 +105,16 @@ __WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
   }
 
   // Add header to size
-  size += sizeof(mem_block_t);
+  block_size = size + sizeof(mem_block_t);
   // Make sure that block is 8-byte aligned
-  size = (size + 7U) & ~((uint32_t)7U);
+  block_size = (block_size + 7U) & ~((uint32_t)7U);
 
   // Search for hole big enough
-  p = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
+  p = MemBlockPtr(mem, sizeof(mem_head_t));
   for (;;) {
     hole_size  = (uint32_t)p->next - (uint32_t)p;
     hole_size -= p->info & MB_INFO_LEN_MASK;
-    if (hole_size >= size) {
+    if (hole_size >= block_size) {
       // Hole found
       break;
     }
@@ -108,19 +126,19 @@ __WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
     }
   }
 
-  ((mem_head_t *)mem)->used += size;
+  (MemHeadPtr(mem))->used += block_size;
 
   if (p->info == 0U) {
     // No block allocated, set info of first element
-    p->info = size | type;
-    ptr = (mem_block_t *)((uint32_t)p + sizeof(mem_block_t));
+    p->info = block_size | type;
+    ptr = MemBlockPtr(p, sizeof(mem_block_t));
   } else {
     // Insert new element into the list
-    p_new = (mem_block_t *)((uint32_t)p + (p->info & MB_INFO_LEN_MASK));
+    p_new = MemBlockPtr(p, p->info & MB_INFO_LEN_MASK);
     p_new->next = p->next;
-    p_new->info = size | type;
+    p_new->info = block_size | type;
     p->next = p_new;
-    ptr = (mem_block_t *)((uint32_t)p_new + sizeof(mem_block_t));
+    ptr = MemBlockPtr(p_new, sizeof(mem_block_t));
   }
 
   EvrRtxMemoryAlloc(mem, size, type, ptr);
@@ -133,18 +151,20 @@ __WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
 /// \param[in]  block           memory block to be returned to the memory pool.
 /// \return 1 - success, 0 - failure.
 __WEAK uint32_t osRtxMemoryFree (void *mem, void *block) {
-  mem_block_t *p, *p_prev, *ptr;
+  const mem_block_t *ptr;
+        mem_block_t *p, *p_prev;
 
   if ((mem == NULL) || (block == NULL)) {
     EvrRtxMemoryFree(mem, block, 0U);
     return 0U;
   }
 
-  ptr = (mem_block_t *)((uint32_t)block - sizeof(mem_block_t));
+  ptr = MemBlockPtr(block, 0U);
+  ptr--;
 
   // Search for header
   p_prev = NULL;
-  p = (mem_block_t *)((uint32_t)mem + sizeof(mem_head_t));
+  p = MemBlockPtr(mem, sizeof(mem_head_t));
   while (p != ptr) {
     p_prev = p;
     p = p->next;
@@ -155,7 +175,7 @@ __WEAK uint32_t osRtxMemoryFree (void *mem, void *block) {
     }
   }
 
-  ((mem_head_t *)mem)->used -= p->info & MB_INFO_LEN_MASK;
+  (MemHeadPtr(mem))->used -= p->info & MB_INFO_LEN_MASK;
 
   if (p_prev == NULL) {
     // Release first block, only set info to 0

+ 37 - 28
CMSIS/RTOS2/RTX/Source/rtx_mempool.c

@@ -35,6 +35,7 @@
 /// \param[in]  block_mem       pointer to memory for block storage.
 /// \return 1 - success, 0 - failure.
 uint32_t osRtxMemoryPoolInit (os_mp_info_t *mp_info, uint32_t block_count, uint32_t block_size, void *block_mem) {
+  void *mem;
   void *block;
 
   // Check parameters
@@ -48,17 +49,18 @@ uint32_t osRtxMemoryPoolInit (os_mp_info_t *mp_info, uint32_t block_count, uint3
   mp_info->block_size  = block_size;
   mp_info->block_base  = block_mem;
   mp_info->block_free  = block_mem;
-  mp_info->block_lim   = (uint8_t *)block_mem + (block_count * block_size);
+  mp_info->block_lim   = &(((uint8_t *)block_mem)[block_count * block_size]);
 
   EvrRtxMemoryBlockInit(mp_info, block_count, block_size, block_mem);
 
   // Link all free blocks
+  mem = block_mem;
   while (--block_count != 0U) {
-    block = (uint8_t *)block_mem + block_size;
-    *((void **)block_mem) = block;
-    block_mem = block;
+    block = &((uint8_t *)mem)[block_size];
+    *((void **)mem) = block;
+    mem = block;
   }
-  *((void **)block_mem) = NULL;
+  *((void **)mem) = NULL;
 
   return 1U;
 }
@@ -150,6 +152,9 @@ osStatus_t osRtxMemoryPoolFree (os_mp_info_t *mp_info, void *block) {
   return status;
 }
 
+
+//  ==== Post ISR processing ====
+
 /// Memory Pool post ISR processing.
 /// \param[in]  mp              memory pool object.
 static void osRtxMemoryPoolPostProcess (os_memory_pool_t *mp) {
@@ -166,7 +171,7 @@ static void osRtxMemoryPoolPostProcess (os_memory_pool_t *mp) {
     block = osRtxMemoryPoolAlloc(&mp->mp_info);
     if (block != NULL) {
       // Wakeup waiting Thread with highest Priority
-      thread = osRtxThreadListGet((os_object_t*)mp);
+      thread = osRtxThreadListGet(osRtxObject(mp));
       osRtxThreadWaitExit(thread, (uint32_t)block, FALSE);
       EvrRtxMemoryPoolAllocated(mp, block);
     }
@@ -182,6 +187,8 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew (uint32_t block_count, uint32_t bloc
   os_memory_pool_t *mp;
   void             *mp_mem;
   uint32_t          mp_size;
+  uint32_t          b_count;
+  uint32_t          b_size;
   uint32_t          size;
   uint8_t           flags;
   const char       *name;
@@ -191,13 +198,14 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew (uint32_t block_count, uint32_t bloc
     EvrRtxMemoryPoolError(NULL, (int32_t)osErrorParameter);
     return NULL;
   }
-  block_size = (block_size + 3U) & ~3UL;
-  if ((__CLZ(block_count) + __CLZ(block_size)) < 32U) {
+  b_count =  block_count;
+  b_size  = (block_size + 3U) & ~3UL;
+  if ((__CLZ(b_count) + __CLZ(b_size)) < 32U) {
     EvrRtxMemoryPoolError(NULL, (int32_t)osErrorParameter);
     return NULL;
   }
 
-  size = block_count * block_size;
+  size = b_count * b_size;
 
   // Process attributes
   if (attr != NULL) {
@@ -270,7 +278,7 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew (uint32_t block_count, uint32_t bloc
     mp->flags       = flags;
     mp->name        = name;
     mp->thread_list = NULL;
-    (void)osRtxMemoryPoolInit(&mp->mp_info, block_count, block_size, mp_mem);
+    (void)osRtxMemoryPoolInit(&mp->mp_info, b_count, b_size, mp_mem);
 
     // Register post ISR processing function
     osRtxInfo.post_process.memory_pool = osRtxMemoryPoolPostProcess;
@@ -286,7 +294,7 @@ static osMemoryPoolId_t svcRtxMemoryPoolNew (uint32_t block_count, uint32_t bloc
 /// Get name of a Memory Pool object.
 /// \note API identical to osMemoryPoolGetName
 static const char *svcRtxMemoryPoolGetName (osMemoryPoolId_t mp_id) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
 
   // Check parameters
   if ((mp == NULL) || (mp->id != osRtxIdMemoryPool)) {
@@ -308,7 +316,7 @@ static const char *svcRtxMemoryPoolGetName (osMemoryPoolId_t mp_id) {
 /// Allocate a memory block from a Memory Pool.
 /// \note API identical to osMemoryPoolAlloc
 static void *svcRtxMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
   void             *block;
 
   // Check parameters
@@ -333,7 +341,7 @@ static void *svcRtxMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout) {
       EvrRtxMemoryPoolAllocPending(mp, timeout);
       // Suspend current Thread
       if (osRtxThreadWaitEnter(osRtxThreadWaitingMemoryPool, timeout)) {
-        osRtxThreadListPut((os_object_t*)mp, osRtxThreadGetRunning());
+        osRtxThreadListPut(osRtxObject(mp), osRtxThreadGetRunning());
       } else {
         EvrRtxMemoryPoolAllocTimeout(mp);
       }
@@ -348,7 +356,8 @@ static void *svcRtxMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout) {
 /// Return an allocated memory block back to a Memory Pool.
 /// \note API identical to osMemoryPoolFree
 static osStatus_t svcRtxMemoryPoolFree (osMemoryPoolId_t mp_id, void *block) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
+  void             *block0;
   os_thread_t      *thread;
   osStatus_t        status;
 
@@ -371,12 +380,12 @@ static osStatus_t svcRtxMemoryPoolFree (osMemoryPoolId_t mp_id, void *block) {
     // Check if Thread is waiting to allocate memory
     if (mp->thread_list != NULL) {
       // Allocate memory
-      block = osRtxMemoryPoolAlloc(&mp->mp_info);
-      if (block != NULL) {
+      block0 = osRtxMemoryPoolAlloc(&mp->mp_info);
+      if (block0 != NULL) {
         // Wakeup waiting Thread with highest Priority
-        thread = osRtxThreadListGet((os_object_t*)mp);
-        osRtxThreadWaitExit(thread, (uint32_t)block, TRUE);
-        EvrRtxMemoryPoolAllocated(mp, block);
+        thread = osRtxThreadListGet(osRtxObject(mp));
+        osRtxThreadWaitExit(thread, (uint32_t)block0, TRUE);
+        EvrRtxMemoryPoolAllocated(mp, block0);
       }
     }
   } else {
@@ -389,7 +398,7 @@ static osStatus_t svcRtxMemoryPoolFree (osMemoryPoolId_t mp_id, void *block) {
 /// Get maximum number of memory blocks in a Memory Pool.
 /// \note API identical to osMemoryPoolGetCapacity
 static uint32_t svcRtxMemoryPoolGetCapacity (osMemoryPoolId_t mp_id) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
 
   // Check parameters
   if ((mp == NULL) || (mp->id != osRtxIdMemoryPool)) {
@@ -411,7 +420,7 @@ static uint32_t svcRtxMemoryPoolGetCapacity (osMemoryPoolId_t mp_id) {
 /// Get memory block size in a Memory Pool.
 /// \note API identical to osMemoryPoolGetBlockSize
 static uint32_t svcRtxMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
 
   // Check parameters
   if ((mp == NULL) || (mp->id != osRtxIdMemoryPool)) {
@@ -433,7 +442,7 @@ static uint32_t svcRtxMemoryPoolGetBlockSize (osMemoryPoolId_t mp_id) {
 /// Get number of memory blocks used in a Memory Pool.
 /// \note API identical to osMemoryPoolGetCount
 static uint32_t svcRtxMemoryPoolGetCount (osMemoryPoolId_t mp_id) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
 
   // Check parameters
   if ((mp == NULL) || (mp->id != osRtxIdMemoryPool)) {
@@ -455,7 +464,7 @@ static uint32_t svcRtxMemoryPoolGetCount (osMemoryPoolId_t mp_id) {
 /// Get number of memory blocks available in a Memory Pool.
 /// \note API identical to osMemoryPoolGetSpace
 static uint32_t svcRtxMemoryPoolGetSpace (osMemoryPoolId_t mp_id) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
 
   // Check parameters
   if ((mp == NULL) || (mp->id != osRtxIdMemoryPool)) {
@@ -477,7 +486,7 @@ static uint32_t svcRtxMemoryPoolGetSpace (osMemoryPoolId_t mp_id) {
 /// Delete a Memory Pool object.
 /// \note API identical to osMemoryPoolDelete
 static osStatus_t svcRtxMemoryPoolDelete (osMemoryPoolId_t mp_id) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
   os_thread_t      *thread;
 
   // Check parameters
@@ -498,7 +507,7 @@ static osStatus_t svcRtxMemoryPoolDelete (osMemoryPoolId_t mp_id) {
   // Unblock waiting threads
   if (mp->thread_list != NULL) {
     do {
-      thread = osRtxThreadListGet((os_object_t*)mp);
+      thread = osRtxThreadListGet(osRtxObject(mp));
       osRtxThreadWaitExit(thread, 0U, FALSE);
     } while (mp->thread_list != NULL);
     osRtxThreadDispatch(NULL);
@@ -541,7 +550,7 @@ SVC0_1(MemoryPoolDelete,       osStatus_t,       osMemoryPoolId_t)
 /// \note API identical to osMemoryPoolAlloc
 __STATIC_INLINE
 void *isrRtxMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
   void             *block;
 
   // Check parameters
@@ -571,7 +580,7 @@ void *isrRtxMemoryPoolAlloc (osMemoryPoolId_t mp_id, uint32_t timeout) {
 /// \note API identical to osMemoryPoolFree
 __STATIC_INLINE
 osStatus_t isrRtxMemoryPoolFree (osMemoryPoolId_t mp_id, void *block) {
-  os_memory_pool_t *mp = (os_memory_pool_t *)mp_id;
+  os_memory_pool_t *mp = osRtxMemoryPoolId(mp_id);
   osStatus_t        status;
 
   // Check parameters
@@ -590,7 +599,7 @@ osStatus_t isrRtxMemoryPoolFree (osMemoryPoolId_t mp_id, void *block) {
   status = osRtxMemoryPoolFree(&mp->mp_info, block);
   if (status == osOK) {
     // Register post ISR processing
-    osRtxPostProcess((os_object_t *)mp);
+    osRtxPostProcess(osRtxObject(mp));
     EvrRtxMemoryPoolDeallocated(mp, block);
   } else {
     EvrRtxMemoryPoolFreeFailed(mp, block);

+ 64 - 59
CMSIS/RTOS2/RTX/Source/rtx_msgqueue.c

@@ -154,9 +154,11 @@ static void MessageQueueRemove (os_message_queue_t *mq, const os_message_t *msg)
 /// \param[in]  msg             message object.
 static void osRtxMessageQueuePostProcess (os_message_t *msg) {
   os_message_queue_t *mq;
+  os_message_t       *msg0;
   os_thread_t        *thread;
-  uint32_t           *reg;
-  void              **ptr;
+  const uint32_t     *reg;
+  const void         *ptr_src;
+        void         *ptr_dst;
 
   if (msg->state == osRtxObjectInactive) {
     return;
@@ -164,8 +166,7 @@ static void osRtxMessageQueuePostProcess (os_message_t *msg) {
 
   if (msg->flags != 0U) {
     // Remove Message
-    ptr = (void *)((uint8_t *)msg + sizeof(os_message_t));
-    mq = *ptr;
+    mq = *((os_message_queue_t **)(void *)&msg[1]);
     if (mq->state == osRtxObjectInactive) {
       return;
     }
@@ -176,21 +177,22 @@ static void osRtxMessageQueuePostProcess (os_message_t *msg) {
     // Check if Thread is waiting to send a Message
     if ((mq->thread_list != NULL) && (mq->thread_list->state == osRtxThreadWaitingMessagePut)) {
       // Try to allocate memory
-      msg = osRtxMemoryPoolAlloc(&mq->mp_info);
-      if (msg != NULL) {
+      msg0 = osRtxMemoryPoolAlloc(&mq->mp_info);
+      if (msg0 != NULL) {
         // Wakeup waiting Thread with highest Priority
-        thread = osRtxThreadListGet((os_object_t*)mq);
+        thread = osRtxThreadListGet(osRtxObject(mq));
         osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
         // Copy Message (R2: const void *msg_ptr, R3: uint8_t msg_prio)
         reg = osRtxThreadRegPtr(thread);
-        memcpy((uint8_t *)msg + sizeof(os_message_t), (void *)reg[2], mq->msg_size);
+        ptr_src = (const void *)reg[2];
+        memcpy(&msg0[1], ptr_src, mq->msg_size);
         // Store Message into Queue
-        msg->id       = osRtxIdMessage;
-        msg->state    = osRtxObjectActive;
-        msg->flags    = 0U;
-        msg->priority = (uint8_t)reg[3];
-        MessageQueuePut(mq, msg);
-        EvrRtxMessageQueueInserted(mq, (void *)reg[2]);
+        msg0->id       = osRtxIdMessage;
+        msg0->state    = osRtxObjectActive;
+        msg0->flags    = 0U;
+        msg0->priority = (uint8_t)reg[3];
+        MessageQueuePut(mq, msg0);
+        EvrRtxMessageQueueInserted(mq, ptr_src);
       }
     }
   } else {
@@ -199,24 +201,26 @@ static void osRtxMessageQueuePostProcess (os_message_t *msg) {
     if (mq->state == osRtxObjectInactive) {
       return;
     }
+    ptr_src = (const void *)msg->prev;
     // Check if Thread is waiting to receive a Message
     if ((mq->thread_list != NULL) && (mq->thread_list->state == osRtxThreadWaitingMessageGet)) {
-      EvrRtxMessageQueueInserted(mq, (void *)msg->prev);
+      EvrRtxMessageQueueInserted(mq, ptr_src);
       // Wakeup waiting Thread with highest Priority
-      thread = osRtxThreadListGet((os_object_t*)mq);
+      thread = osRtxThreadListGet(osRtxObject(mq));
       osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
       // Copy Message (R2: void *msg_ptr, R3: uint8_t *msg_prio)
       reg = osRtxThreadRegPtr(thread);
-      memcpy((void *)reg[2], (uint8_t *)msg + sizeof(os_message_t), mq->msg_size);
+      ptr_dst = (void *)reg[2];
+      memcpy(ptr_dst, &msg[1], mq->msg_size);
       if (reg[3] != 0U) {
         *((uint8_t *)reg[3]) = msg->priority;
       }
-      EvrRtxMessageQueueRetrieved(mq, (void *)reg[2]);
+      EvrRtxMessageQueueRetrieved(mq, ptr_dst);
       // Free memory
       msg->state = osRtxObjectInactive;
       (void)osRtxMemoryPoolFree(&mq->mp_info, msg);
     } else {
-      EvrRtxMessageQueueInserted(mq, (void *)msg->prev);
+      EvrRtxMessageQueueInserted(mq, ptr_src);
       MessageQueuePut(mq, msg);
     }
   }
@@ -327,7 +331,7 @@ static osMessageQueueId_t svcRtxMessageQueueNew (uint32_t msg_count, uint32_t ms
     (void)osRtxMemoryPoolInit(&mq->mp_info, msg_count, block_size, mq_mem);
 
     // Register post ISR processing function
-    osRtxInfo.post_process.message_queue = osRtxMessageQueuePostProcess;
+    osRtxInfo.post_process.message = osRtxMessageQueuePostProcess;
 
     EvrRtxMessageQueueCreated(mq, mq->name);
   } else {
@@ -340,7 +344,7 @@ static osMessageQueueId_t svcRtxMessageQueueNew (uint32_t msg_count, uint32_t ms
 /// Get name of a Message Queue object.
 /// \note API identical to osMessageQueueGetName
 static const char *svcRtxMessageQueueGetName (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
 
   // Check parameters
   if ((mq == NULL) || (mq->id != osRtxIdMessageQueue)) {
@@ -362,10 +366,11 @@ static const char *svcRtxMessageQueueGetName (osMessageQueueId_t mq_id) {
 /// Put a Message into a Queue or timeout if Queue is full.
 /// \note API identical to osMessageQueuePut
 static osStatus_t svcRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
   os_message_t       *msg;
   os_thread_t        *thread;
   uint32_t           *reg;
+  void               *ptr;
   osStatus_t          status;
 
   // Check parameters
@@ -384,22 +389,23 @@ static osStatus_t svcRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *m
   if ((mq->thread_list != NULL) && (mq->thread_list->state == osRtxThreadWaitingMessageGet)) {
     EvrRtxMessageQueueInserted(mq, msg_ptr);
     // Wakeup waiting Thread with highest Priority
-    thread = osRtxThreadListGet((os_object_t*)mq);
+    thread = osRtxThreadListGet(osRtxObject(mq));
     osRtxThreadWaitExit(thread, (uint32_t)osOK, TRUE);
     // Copy Message (R2: void *msg_ptr, R3: uint8_t *msg_prio)
     reg = osRtxThreadRegPtr(thread);
-    memcpy((void *)reg[2], msg_ptr, mq->msg_size);
+    ptr = (void *)reg[2];
+    memcpy(ptr, msg_ptr, mq->msg_size);
     if (reg[3] != 0U) {
       *((uint8_t *)reg[3]) = msg_prio;
     }
-    EvrRtxMessageQueueRetrieved(mq, (void *)reg[2]);
+    EvrRtxMessageQueueRetrieved(mq, ptr);
     status = osOK;
   } else {
     // Try to allocate memory
     msg = osRtxMemoryPoolAlloc(&mq->mp_info);
     if (msg != NULL) {
       // Copy Message
-      memcpy((uint8_t *)msg + sizeof(os_message_t), msg_ptr, mq->msg_size);
+      memcpy(&msg[1], msg_ptr, mq->msg_size);
       // Put Message into Queue
       msg->id       = osRtxIdMessage;
       msg->state    = osRtxObjectActive;
@@ -414,7 +420,7 @@ static osStatus_t svcRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *m
         EvrRtxMessageQueuePutPending(mq, msg_ptr, timeout);
         // Suspend current Thread
         if (osRtxThreadWaitEnter(osRtxThreadWaitingMessagePut, timeout)) {
-          osRtxThreadListPut((os_object_t*)mq, osRtxThreadGetRunning());
+          osRtxThreadListPut(osRtxObject(mq), osRtxThreadGetRunning());
           // Save arguments (R2: const void *msg_ptr, R3: uint8_t msg_prio)
           reg = (uint32_t *)(__get_PSP());
           reg[2] = (uint32_t)msg_ptr;
@@ -436,10 +442,11 @@ static osStatus_t svcRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *m
 /// Get a Message from a Queue or timeout if Queue is empty.
 /// \note API identical to osMessageQueueGet
 static osStatus_t svcRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
   os_message_t       *msg;
   os_thread_t        *thread;
   uint32_t           *reg;
+  const void         *ptr;
   osStatus_t          status;
 
   // Check parameters
@@ -459,7 +466,7 @@ static osStatus_t svcRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr
   if (msg != NULL) {
     MessageQueueRemove(mq, msg);
     // Copy Message
-    memcpy(msg_ptr, (uint8_t *)msg + sizeof(os_message_t), mq->msg_size);
+    memcpy(msg_ptr, &msg[1], mq->msg_size);
     if (msg_prio != NULL) {
       *msg_prio = msg->priority;
     }
@@ -473,18 +480,19 @@ static osStatus_t svcRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr
       msg = osRtxMemoryPoolAlloc(&mq->mp_info);
       if (msg != NULL) {
         // Wakeup waiting Thread with highest Priority
-        thread = osRtxThreadListGet((os_object_t*)mq);
+        thread = osRtxThreadListGet(osRtxObject(mq));
         osRtxThreadWaitExit(thread, (uint32_t)osOK, TRUE);
         // Copy Message (R2: const void *msg_ptr, R3: uint8_t msg_prio)
         reg = osRtxThreadRegPtr(thread);
-        memcpy((uint8_t *)msg + sizeof(os_message_t), (void *)reg[2], mq->msg_size);
+        ptr = (const void *)reg[2];
+        memcpy(&msg[1], ptr, mq->msg_size);
         // Store Message into Queue
         msg->id       = osRtxIdMessage;
         msg->state    = osRtxObjectActive;
         msg->flags    = 0U;
         msg->priority = (uint8_t)reg[3];
         MessageQueuePut(mq, msg);
-        EvrRtxMessageQueueInserted(mq, (void *)reg[2]);
+        EvrRtxMessageQueueInserted(mq, ptr);
       }
     }
     status = osOK;
@@ -494,7 +502,7 @@ static osStatus_t svcRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr
       EvrRtxMessageQueueGetPending(mq, msg_ptr, timeout);
       // Suspend current Thread
       if (osRtxThreadWaitEnter(osRtxThreadWaitingMessageGet, timeout)) {
-        osRtxThreadListPut((os_object_t*)mq, osRtxThreadGetRunning());
+        osRtxThreadListPut(osRtxObject(mq), osRtxThreadGetRunning());
         // Save arguments (R2: void *msg_ptr, R3: uint8_t *msg_prio)
         reg = (uint32_t *)(__get_PSP());
         reg[2] = (uint32_t)msg_ptr;
@@ -515,7 +523,7 @@ static osStatus_t svcRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr
 /// Get maximum number of messages in a Message Queue.
 /// \note API identical to osMessageGetCapacity
 static uint32_t svcRtxMessageQueueGetCapacity (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
 
   // Check parameters
   if ((mq == NULL) || (mq->id != osRtxIdMessageQueue)) {
@@ -537,7 +545,7 @@ static uint32_t svcRtxMessageQueueGetCapacity (osMessageQueueId_t mq_id) {
 /// Get maximum message size in a Memory Pool.
 /// \note API identical to osMessageGetMsgSize
 static uint32_t svcRtxMessageQueueGetMsgSize (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
 
   // Check parameters
   if ((mq == NULL) || (mq->id != osRtxIdMessageQueue)) {
@@ -559,7 +567,7 @@ static uint32_t svcRtxMessageQueueGetMsgSize (osMessageQueueId_t mq_id) {
 /// Get number of queued messages in a Message Queue.
 /// \note API identical to osMessageGetCount
 static uint32_t svcRtxMessageQueueGetCount (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
 
   // Check parameters
   if ((mq == NULL) || (mq->id != osRtxIdMessageQueue)) {
@@ -581,7 +589,7 @@ static uint32_t svcRtxMessageQueueGetCount (osMessageQueueId_t mq_id) {
 /// Get number of available slots for messages in a Message Queue.
 /// \note API identical to osMessageGetSpace
 static uint32_t svcRtxMessageQueueGetSpace (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
 
   // Check parameters
   if ((mq == NULL) || (mq->id != osRtxIdMessageQueue)) {
@@ -603,10 +611,11 @@ static uint32_t svcRtxMessageQueueGetSpace (osMessageQueueId_t mq_id) {
 /// Reset a Message Queue to initial empty state.
 /// \note API identical to osMessageQueueReset
 static osStatus_t svcRtxMessageQueueReset (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
   os_message_t       *msg;
   os_thread_t        *thread;
-  uint32_t           *reg;
+  const uint32_t     *reg;
+  const void         *ptr;
 
   // Check parameters
   if ((mq == NULL) || (mq->id != osRtxIdMessageQueue)) {
@@ -641,18 +650,19 @@ static osStatus_t svcRtxMessageQueueReset (osMessageQueueId_t mq_id) {
       msg = osRtxMemoryPoolAlloc(&mq->mp_info);
       if (msg != NULL) {
         // Wakeup waiting Thread with highest Priority
-        thread = osRtxThreadListGet((os_object_t*)mq);
+        thread = osRtxThreadListGet(osRtxObject(mq));
         osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
         // Copy Message (R2: const void *msg_ptr, R3: uint8_t msg_prio)
         reg = osRtxThreadRegPtr(thread);
-        memcpy((uint8_t *)msg + sizeof(os_message_t), (void *)reg[2], mq->msg_size);
+        ptr = (const void *)reg[2];
+        memcpy(&msg[1], ptr, mq->msg_size);
         // Store Message into Queue
         msg->id       = osRtxIdMessage;
         msg->state    = osRtxObjectActive;
         msg->flags    = 0U;
         msg->priority = (uint8_t)reg[3];
         MessageQueuePut(mq, msg);
-        EvrRtxMessageQueueInserted(mq, (void *)reg[2]);
+        EvrRtxMessageQueueInserted(mq, ptr);
       }
     } while ((msg != NULL) && (mq->thread_list != NULL));
     osRtxThreadDispatch(NULL);
@@ -666,7 +676,7 @@ static osStatus_t svcRtxMessageQueueReset (osMessageQueueId_t mq_id) {
 /// Delete a Message Queue object.
 /// \note API identical to osMessageQueueDelete
 static osStatus_t svcRtxMessageQueueDelete (osMessageQueueId_t mq_id) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
   os_thread_t        *thread;
 
   // Check parameters
@@ -687,8 +697,8 @@ static osStatus_t svcRtxMessageQueueDelete (osMessageQueueId_t mq_id) {
   // Unblock waiting threads
   if (mq->thread_list != NULL) {
     do {
-      thread = osRtxThreadListGet((os_object_t*)mq);
-      osRtxThreadWaitExit(thread, (uint32_t)osErrorResource, false);
+      thread = osRtxThreadListGet(osRtxObject(mq));
+      osRtxThreadWaitExit(thread, (uint32_t)osErrorResource, FALSE);
     } while (mq->thread_list != NULL);
     osRtxThreadDispatch(NULL);
   }
@@ -731,9 +741,8 @@ SVC0_1(MessageQueueDelete,      osStatus_t,         osMessageQueueId_t)
 /// \note API identical to osMessageQueuePut
 __STATIC_INLINE
 osStatus_t isrRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
   os_message_t       *msg;
-  const void        **ptr;
   osStatus_t          status;
 
   // Check parameters
@@ -752,17 +761,15 @@ osStatus_t isrRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr,
   msg = osRtxMemoryPoolAlloc(&mq->mp_info);
   if (msg != NULL) {
     // Copy Message
-    memcpy((uint8_t *)msg + sizeof(os_message_t), msg_ptr, mq->msg_size);
+    memcpy(&msg[1], msg_ptr, mq->msg_size);
     msg->id       = osRtxIdMessage;
     msg->state    = osRtxObjectActive;
     msg->flags    = 0U;
     msg->priority = msg_prio;
     // Register post ISR processing
-     ptr = (void *)&msg->prev;
-    *ptr = msg_ptr;
-     ptr = (void *)&msg->next;
-    *ptr = mq;
-    osRtxPostProcess((os_object_t *)msg);
+    *((const void **)(void *)&msg->prev) = msg_ptr;
+    *(      (void **)        &msg->next) = mq;
+    osRtxPostProcess(osRtxObject(msg));
     EvrRtxMessageQueueInsertPending(mq, msg_ptr);
     status = osOK;
   } else {
@@ -778,9 +785,8 @@ osStatus_t isrRtxMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr,
 /// \note API identical to osMessageQueueGet
 __STATIC_INLINE
 osStatus_t isrRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout) {
-  os_message_queue_t *mq = (os_message_queue_t *)mq_id;
+  os_message_queue_t *mq = osRtxMessageQueueId(mq_id);
   os_message_t       *msg;
-  void              **ptr;
   osStatus_t          status;
 
   // Check parameters
@@ -799,14 +805,13 @@ osStatus_t isrRtxMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8
   msg = MessageQueueGet(mq);
   if (msg != NULL) {
     // Copy Message
-    memcpy(msg_ptr, (uint8_t *)msg + sizeof(os_message_t), mq->msg_size);
+    memcpy(msg_ptr, &msg[1], mq->msg_size);
     if (msg_prio != NULL) {
       *msg_prio = msg->priority;
     }
     // Register post ISR processing
-     ptr = (void *)((uint8_t *)msg + sizeof(os_message_t));
-    *ptr = mq;
-    osRtxPostProcess((os_object_t *)msg);
+    *((os_message_queue_t **)(void *)&msg[1]) = mq;
+    osRtxPostProcess(osRtxObject(msg));
     EvrRtxMessageQueueRetrieved(mq, msg_ptr);
     status = osOK;
   } else {

+ 20 - 19
CMSIS/RTOS2/RTX/Source/rtx_mutex.c

@@ -33,11 +33,12 @@
 /// \return 1 - success, 0 - failure.
 void osRtxMutexOwnerRelease (os_mutex_t *mutex_list) {
   os_mutex_t  *mutex;
+  os_mutex_t  *mutex_next;
   os_thread_t *thread;
 
   mutex = mutex_list;
-  while (mutex) {
-    mutex_list = mutex->owner_next;
+  while (mutex != NULL) {
+    mutex_next = mutex->owner_next;
     // Check if Mutex is Robust
     if ((mutex->attr & osMutexRobust) != 0U) {
       // Clear Lock counter
@@ -46,7 +47,7 @@ void osRtxMutexOwnerRelease (os_mutex_t *mutex_list) {
       // Check if Thread is waiting for a Mutex
       if (mutex->thread_list != NULL) {
         // Wakeup waiting Thread with highest Priority
-        thread = osRtxThreadListGet((os_object_t*)mutex);
+        thread = osRtxThreadListGet(osRtxObject(mutex));
         osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
         // Thread is the new Mutex owner
         mutex->owner_thread = thread;
@@ -57,7 +58,7 @@ void osRtxMutexOwnerRelease (os_mutex_t *mutex_list) {
         EvrRtxMutexAcquired(mutex, 1U);
       }
     }
-    mutex = mutex_list;
+    mutex = mutex_next;
   }
 }
 
@@ -130,7 +131,7 @@ static osMutexId_t svcRtxMutexNew (const osMutexAttr_t *attr) {
 /// Get name of a Mutex object.
 /// \note API identical to osMutexGetName
 static const char *svcRtxMutexGetName (osMutexId_t mutex_id) {
-  os_mutex_t *mutex = (os_mutex_t *)mutex_id;
+  os_mutex_t *mutex = osRtxMutexId(mutex_id);
 
   // Check parameters
   if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
@@ -152,7 +153,7 @@ static const char *svcRtxMutexGetName (osMutexId_t mutex_id) {
 /// Acquire a Mutex or timeout if it is locked.
 /// \note API identical to osMutexAcquire
 static osStatus_t svcRtxMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
-  os_mutex_t  *mutex = (os_mutex_t *)mutex_id;
+  os_mutex_t  *mutex = osRtxMutexId(mutex_id);
   os_thread_t *runnig_thread;
   osStatus_t   status;
 
@@ -214,7 +215,7 @@ static osStatus_t svcRtxMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
         EvrRtxMutexAcquirePending(mutex, timeout);
         // Suspend current Thread
         if (osRtxThreadWaitEnter(osRtxThreadWaitingMutex, timeout)) {
-          osRtxThreadListPut((os_object_t*)mutex, runnig_thread);
+          osRtxThreadListPut(osRtxObject(mutex), runnig_thread);
         } else {
           EvrRtxMutexAcquireTimeout(mutex);
         }
@@ -232,11 +233,11 @@ static osStatus_t svcRtxMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
 /// Release a Mutex that was acquired by osMutexAcquire.
 /// \note API identical to osMutexRelease
 static osStatus_t svcRtxMutexRelease (osMutexId_t mutex_id) {
-  os_mutex_t  *mutex = (os_mutex_t *)mutex_id;
-  os_mutex_t  *mutex0;
-  os_thread_t *thread;
-  os_thread_t *runnig_thread;
-  int8_t       priority;
+        os_mutex_t  *mutex = osRtxMutexId(mutex_id);
+  const os_mutex_t  *mutex0;
+        os_thread_t *thread;
+        os_thread_t *runnig_thread;
+        int8_t       priority;
 
   // Check running thread
   runnig_thread = osRtxThreadGetRunning();
@@ -304,7 +305,7 @@ static osStatus_t svcRtxMutexRelease (osMutexId_t mutex_id) {
     // Check if Thread is waiting for a Mutex
     if (mutex->thread_list != NULL) {
       // Wakeup waiting Thread with highest Priority
-      thread = osRtxThreadListGet((os_object_t*)mutex);
+      thread = osRtxThreadListGet(osRtxObject(mutex));
       osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
       // Thread is the new Mutex owner
       mutex->owner_thread = thread;
@@ -324,7 +325,7 @@ static osStatus_t svcRtxMutexRelease (osMutexId_t mutex_id) {
 /// Get Thread which owns a Mutex object.
 /// \note API identical to osMutexGetOwner
 static osThreadId_t svcRtxMutexGetOwner (osMutexId_t mutex_id) {
-  os_mutex_t *mutex = (os_mutex_t *)mutex_id;
+  os_mutex_t *mutex = osRtxMutexId(mutex_id);
 
   // Check parameters
   if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
@@ -352,10 +353,10 @@ static osThreadId_t svcRtxMutexGetOwner (osMutexId_t mutex_id) {
 /// Delete a Mutex object.
 /// \note API identical to osMutexDelete
 static osStatus_t svcRtxMutexDelete (osMutexId_t mutex_id) {
-  os_mutex_t  *mutex = (os_mutex_t *)mutex_id;
-  os_mutex_t  *mutex0;
-  os_thread_t *thread;
-  int8_t       priority;
+        os_mutex_t  *mutex = osRtxMutexId(mutex_id);
+  const os_mutex_t  *mutex0;
+        os_thread_t *thread;
+        int8_t       priority;
 
   // Check parameters
   if ((mutex == NULL) || (mutex->id != osRtxIdMutex)) {
@@ -408,7 +409,7 @@ static osStatus_t svcRtxMutexDelete (osMutexId_t mutex_id) {
     // Unblock waiting threads
     if (mutex->thread_list != NULL) {
       do {
-        thread = osRtxThreadListGet((os_object_t*)mutex);
+        thread = osRtxThreadListGet(osRtxObject(mutex));
         osRtxThreadWaitExit(thread, (uint32_t)osErrorResource, FALSE);
       } while (mutex->thread_list != NULL);
     }

+ 12 - 12
CMSIS/RTOS2/RTX/Source/rtx_semaphore.c

@@ -111,7 +111,7 @@ static void osRtxSemaphorePostProcess (os_semaphore_t *semaphore) {
     // Try to acquire token
     if (SemaphoreTokenDecrement(semaphore) != 0U) {
       // Wakeup waiting Thread with highest Priority
-      thread = osRtxThreadListGet((os_object_t*)semaphore);
+      thread = osRtxThreadListGet(osRtxObject(semaphore));
       osRtxThreadWaitExit(thread, (uint32_t)osOK, FALSE);
       EvrRtxSemaphoreAcquired(semaphore);
     }
@@ -190,7 +190,7 @@ static osSemaphoreId_t svcRtxSemaphoreNew (uint32_t max_count, uint32_t initial_
 /// Get name of a Semaphore object.
 /// \note API identical to osSemaphoreGetName
 static const char *svcRtxSemaphoreGetName (osSemaphoreId_t semaphore_id) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
 
   // Check parameters
   if ((semaphore == NULL) || (semaphore->id != osRtxIdSemaphore)) {
@@ -212,7 +212,7 @@ static const char *svcRtxSemaphoreGetName (osSemaphoreId_t semaphore_id) {
 /// Acquire a Semaphore token or timeout if no tokens are available.
 /// \note API identical to osSemaphoreAcquire
 static osStatus_t svcRtxSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
   osStatus_t      status;
 
   // Check parameters
@@ -237,7 +237,7 @@ static osStatus_t svcRtxSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t
       EvrRtxSemaphoreAcquirePending(semaphore, timeout);
       // Suspend current Thread
       if (osRtxThreadWaitEnter(osRtxThreadWaitingSemaphore, timeout)) {
-        osRtxThreadListPut((os_object_t*)semaphore, osRtxThreadGetRunning());
+        osRtxThreadListPut(osRtxObject(semaphore), osRtxThreadGetRunning());
       } else {
         EvrRtxSemaphoreAcquireTimeout(semaphore);
       }
@@ -254,7 +254,7 @@ static osStatus_t svcRtxSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t
 /// Release a Semaphore token that was acquired by osSemaphoreAcquire.
 /// \note API identical to osSemaphoreRelease
 static osStatus_t svcRtxSemaphoreRelease (osSemaphoreId_t semaphore_id) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
   os_thread_t    *thread;
   osStatus_t      status;
 
@@ -274,7 +274,7 @@ static osStatus_t svcRtxSemaphoreRelease (osSemaphoreId_t semaphore_id) {
   if (semaphore->thread_list != NULL) {
     EvrRtxSemaphoreReleased(semaphore);
     // Wakeup waiting Thread with highest Priority
-    thread = osRtxThreadListGet((os_object_t*)semaphore);
+    thread = osRtxThreadListGet(osRtxObject(semaphore));
     osRtxThreadWaitExit(thread, (uint32_t)osOK, TRUE);
     EvrRtxSemaphoreAcquired(semaphore);
     status = osOK;
@@ -295,7 +295,7 @@ static osStatus_t svcRtxSemaphoreRelease (osSemaphoreId_t semaphore_id) {
 /// Get current Semaphore token count.
 /// \note API identical to osSemaphoreGetCount
 static uint32_t svcRtxSemaphoreGetCount (osSemaphoreId_t semaphore_id) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
 
   // Check parameters
   if ((semaphore == NULL) || (semaphore->id != osRtxIdSemaphore)) {
@@ -317,7 +317,7 @@ static uint32_t svcRtxSemaphoreGetCount (osSemaphoreId_t semaphore_id) {
 /// Delete a Semaphore object.
 /// \note API identical to osSemaphoreDelete
 static osStatus_t svcRtxSemaphoreDelete (osSemaphoreId_t semaphore_id) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
   os_thread_t    *thread;
 
   // Check parameters
@@ -338,7 +338,7 @@ static osStatus_t svcRtxSemaphoreDelete (osSemaphoreId_t semaphore_id) {
   // Unblock waiting threads
   if (semaphore->thread_list != NULL) {
     do {
-      thread = osRtxThreadListGet((os_object_t*)semaphore);
+      thread = osRtxThreadListGet(osRtxObject(semaphore));
       osRtxThreadWaitExit(thread, (uint32_t)osErrorResource, FALSE);
     } while (semaphore->thread_list != NULL);
     osRtxThreadDispatch(NULL);
@@ -373,7 +373,7 @@ SVC0_1(SemaphoreDelete,   osStatus_t,      osSemaphoreId_t)
 /// \note API identical to osSemaphoreAcquire
 __STATIC_INLINE
 osStatus_t isrRtxSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
   osStatus_t      status;
 
   // Check parameters
@@ -405,7 +405,7 @@ osStatus_t isrRtxSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeou
 /// \note API identical to osSemaphoreRelease
 __STATIC_INLINE
 osStatus_t isrRtxSemaphoreRelease (osSemaphoreId_t semaphore_id) {
-  os_semaphore_t *semaphore = (os_semaphore_t *)semaphore_id;
+  os_semaphore_t *semaphore = osRtxSemaphoreId(semaphore_id);
   osStatus_t      status;
 
   // Check parameters
@@ -423,7 +423,7 @@ osStatus_t isrRtxSemaphoreRelease (osSemaphoreId_t semaphore_id) {
   // Try to release token
   if (SemaphoreTokenIncrement(semaphore) != 0U) {
     // Register post ISR processing
-    osRtxPostProcess((os_object_t *)semaphore);
+    osRtxPostProcess(osRtxObject(semaphore));
     EvrRtxSemaphoreReleased(semaphore);
     status = osOK;
   } else {

+ 14 - 13
CMSIS/RTOS2/RTX/Source/rtx_system.c

@@ -31,7 +31,7 @@
 /// Put Object into ISR Queue.
 /// \param[in]  object          object.
 /// \return 1 - success, 0 - failure.
-static uint32_t isr_queue_put (void *object) {
+static uint32_t isr_queue_put (os_object_t *object) {
 #if (EXCLUSIVE_ACCESS == 0)
   uint32_t primask = __get_PRIMASK();
 #else
@@ -74,14 +74,14 @@ static uint32_t isr_queue_put (void *object) {
 
 /// Get Object from ISR Queue.
 /// \return object or NULL.
-static void *isr_queue_get (void) {
+static os_object_t *isr_queue_get (void) {
 #if (EXCLUSIVE_ACCESS == 0)
-  uint32_t primask = __get_PRIMASK();
+  uint32_t     primask = __get_PRIMASK();
 #else
-  uint32_t n;
+  uint32_t     n;
 #endif
-  uint16_t max;
-  void    *ret;
+  uint16_t     max;
+  os_object_t *ret;
 
   max = osRtxInfo.isr_queue.max;
 
@@ -90,7 +90,7 @@ static void *isr_queue_get (void) {
 
   if (osRtxInfo.isr_queue.cnt != 0U) {
     osRtxInfo.isr_queue.cnt--;
-    ret = osRtxInfo.isr_queue.data[osRtxInfo.isr_queue.out];
+    ret = osRtxObject(osRtxInfo.isr_queue.data[osRtxInfo.isr_queue.out]);
     if (++osRtxInfo.isr_queue.out == max) {
       osRtxInfo.isr_queue.out = 0U;
     }
@@ -104,7 +104,7 @@ static void *isr_queue_get (void) {
 #else
   if (atomic_dec16_nz(&osRtxInfo.isr_queue.cnt) != 0U) {
     n = atomic_inc16_lim(&osRtxInfo.isr_queue.out, max);
-    ret = osRtxInfo.isr_queue.data[n];
+    ret = osRtxObject(osRtxInfo.isr_queue.data[n]);
   } else {
     ret = NULL;
   }
@@ -172,21 +172,22 @@ void osRtxPendSV_Handler (void) {
     }
     switch (object->id) {
       case osRtxIdThread:
-        osRtxInfo.post_process.thread((os_thread_t *)object);
+        osRtxInfo.post_process.thread(osRtxThreadObject(object));
         break;
       case osRtxIdEventFlags:
-        osRtxInfo.post_process.event_flags((os_event_flags_t *)object);
+        osRtxInfo.post_process.event_flags(osRtxEventFlagsObject(object));
         break;
       case osRtxIdSemaphore:
-        osRtxInfo.post_process.semaphore((os_semaphore_t *)object);
+        osRtxInfo.post_process.semaphore(osRtxSemaphoreObject(object));
         break;
       case osRtxIdMemoryPool:
-        osRtxInfo.post_process.memory_pool((os_memory_pool_t *)object);
+        osRtxInfo.post_process.memory_pool(osRtxMemoryPoolObject(object));
         break;
       case osRtxIdMessage:
-        osRtxInfo.post_process.message_queue((os_message_t *)object);
+        osRtxInfo.post_process.message(osRtxMessageObject(object));
         break;
       default:
+        // Should never come here
         break;
     }
   }

+ 87 - 64
CMSIS/RTOS2/RTX/Source/rtx_thread.c

@@ -131,7 +131,7 @@ static uint32_t ThreadFlagsCheck (os_thread_t *thread, uint32_t flags, uint32_t
 /// Put a Thread into specified Object list sorted by Priority (Highest at Head).
 /// \param[in]  object          generic object.
 /// \param[in]  thread          thread object.
-void osRtxThreadListPut (volatile os_object_t *object, os_thread_t *thread) {
+void osRtxThreadListPut (os_object_t *object, os_thread_t *thread) {
   os_thread_t *prev, *next;
   int32_t      priority;
 
@@ -141,7 +141,7 @@ void osRtxThreadListPut (volatile os_object_t *object, os_thread_t *thread) {
 
   priority = thread->priority;
 
-  prev = (os_thread_t *)(uint32_t)object;
+  prev = osRtxThreadObject(object);
   next = object->thread_list;
   while ((next != NULL) && (next->priority >= priority)) {
     prev = next;
@@ -158,14 +158,14 @@ void osRtxThreadListPut (volatile os_object_t *object, os_thread_t *thread) {
 /// Get a Thread with Highest Priority from specified Object list and remove it.
 /// \param[in]  object          generic object.
 /// \return thread object. 
-os_thread_t *osRtxThreadListGet (volatile os_object_t *object) {
+os_thread_t *osRtxThreadListGet (os_object_t *object) {
   os_thread_t *thread;
 
   thread = object->thread_list;
   if (thread != NULL) {
     object->thread_list = thread->thread_next;
     if (thread->thread_next != NULL) {
-      thread->thread_next->thread_prev = (os_thread_t *)(uint32_t)object;
+      thread->thread_next->thread_prev = osRtxThreadObject(object);
     }
     thread->thread_prev = NULL;
   }
@@ -198,7 +198,7 @@ void osRtxThreadListSort (os_thread_t *thread) {
   while ((thread0 != NULL) && (thread0->id == osRtxIdThread)) {
     thread0 = thread0->thread_prev;
   }
-  object = (os_object_t *)thread0;
+  object = osRtxObject(thread0);
 
   if (object != NULL) {
     osRtxThreadListRemove(thread);
@@ -257,15 +257,12 @@ static void osRtxThreadDelayInsert (os_thread_t *thread, uint32_t delay) {
     }
     thread->delay = delay;
     thread->delay_prev = prev;
-    thread->delay_next = next;
+    thread->delay_next = NULL;
     if (prev != NULL) {
       prev->delay_next = thread;
     } else {
       osRtxInfo.thread.wait_list = thread;
     }
-    if (next != NULL) {
-      next->delay_prev = thread;
-    }
   } else {
     prev = NULL;
     next = osRtxInfo.thread.delay_list;
@@ -360,6 +357,7 @@ void osRtxThreadDelayTick (void) {
           EvrRtxMessageQueuePutTimeout((osMessageQueueId_t)osRtxThreadListRoot(thread));
           break;
         default:
+          // Invalid
           break;
       }
       EvrRtxThreadUnblocked(thread, (osRtxThreadRegPtr(thread))[0]);
@@ -377,8 +375,9 @@ void osRtxThreadDelayTick (void) {
 /// Get pointer to Thread registers (R0..R3)
 /// \param[in]  thread          thread object.
 /// \return pointer to registers R0-R3.
-uint32_t *osRtxThreadRegPtr (os_thread_t *thread) {
-  return ((uint32_t *)(thread->sp + StackOffsetR0(thread->stack_frame)));
+uint32_t *osRtxThreadRegPtr (const os_thread_t *thread) {
+  uint32_t addr = thread->sp + StackOffsetR0(thread->stack_frame);
+  return ((uint32_t *)addr);
 }
 
 /// Block running Thread execution and register it as Ready to Run.
@@ -391,7 +390,7 @@ static void osRtxThreadBlock (os_thread_t *thread) {
 
   priority = thread->priority;
 
-  prev = (os_thread_t *)(uint32_t)&osRtxInfo.thread.ready;
+  prev = osRtxThreadObject(&osRtxInfo.thread.ready);
   next = prev->thread_next;
 
   while ((next != NULL) && (next->priority > priority)) {
@@ -423,6 +422,7 @@ void osRtxThreadSwitch (os_thread_t *thread) {
 void osRtxThreadDispatch (os_thread_t *thread) {
   uint8_t      kernel_state;
   os_thread_t *thread_running;
+  os_thread_t *thread_ready;
 
   kernel_state   = osRtxKernelGetState();
   thread_running = osRtxThreadGetRunning();
@@ -434,14 +434,14 @@ void osRtxThreadDispatch (os_thread_t *thread) {
 #endif
 
   if (thread == NULL) {
-    thread = osRtxInfo.thread.ready.thread_list;
+    thread_ready = osRtxInfo.thread.ready.thread_list;
     if ((kernel_state == osRtxKernelRunning) &&
-        (thread_running != NULL) && (thread != NULL) && 
-        (thread->priority > thread_running->priority)) {
+        (thread_running != NULL) && (thread_ready != NULL) &&
+        (thread_ready->priority > thread_running->priority)) {
       // Preempt running Thread
-      osRtxThreadListRemove(thread);
+      osRtxThreadListRemove(thread_ready);
       osRtxThreadBlock(thread_running);
-      osRtxThreadSwitch(thread);
+      osRtxThreadSwitch(thread_ready);
     }
   } else {
     if ((kernel_state == osRtxKernelRunning) &&
@@ -531,6 +531,7 @@ __WEAK void osRtxThreadStackCheck (void) {
 static void osRtxThreadPostProcess (os_thread_t *thread) {
   uint32_t thread_flags;
 
+  // Check thread state
   if ((thread->state == osRtxThreadInactive) ||
       (thread->state == osRtxThreadTerminated)) {
     return;
@@ -717,29 +718,30 @@ static osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const
     thread->stack_size    = stack_size;
     thread->sp            = (uint32_t)stack_mem + stack_size - 64U;
     thread->thread_addr   = (uint32_t)func;
-#if (DOMAIN_NS == 1)
+  #if (DOMAIN_NS == 1)
     thread->tz_memory     = tz_memory;
-#endif
+  #endif
 
     // Initialize stack
-     ptr   = (uint32_t *)stack_mem;
-    *ptr++ = osRtxStackMagicWord;
+    ptr = (uint32_t *)stack_mem;
+    ptr[0] = osRtxStackMagicWord;
     if ((osRtxConfig.flags & osRtxConfigStackWatermark) != 0U) {
       for (n = (stack_size/4U) - (16U + 1U); n != 0U; n--) {
-        *ptr++ = osRtxStackFillPattern;
+         ptr++;
+        *ptr = osRtxStackFillPattern;
       }
     }
     ptr = (uint32_t *)thread->sp;
     for (n = 0U; n != 13U; n++) {
-      *ptr++ = 0U;                      // R4..R11, R0..R3, R12
+      ptr[n] = 0U;                      // R4..R11, R0..R3, R12
     }
-    *ptr++   = (uint32_t)osThreadExit;  // LR
-    *ptr++   = (uint32_t)func;          // PC
-    *ptr++   = xPSR_InitVal(
-                (osRtxConfig.flags & osRtxConfigPrivilegedMode),
-                ((uint32_t)func & 1U)
-               );                       // xPSR
-    *(ptr-8) = (uint32_t)argument;      // R0
+    ptr[13] = (uint32_t)osThreadExit;   // LR
+    ptr[14] = (uint32_t)func;           // PC
+    ptr[15] = xPSR_InitVal(
+                (bool_t)((osRtxConfig.flags & osRtxConfigPrivilegedMode) != 0U),
+                (bool_t)(((uint32_t)func & 1U) != 0U)
+              );                        // xPSR
+    ptr[8]  = (uint32_t)argument;       // R0
 
     // Register post ISR processing function
     osRtxInfo.post_process.thread = osRtxThreadPostProcess;
@@ -759,7 +761,7 @@ static osThreadId_t svcRtxThreadNew (osThreadFunc_t func, void *argument, const
 /// Get name of a thread.
 /// \note API identical to osThreadGetName
 static const char *svcRtxThreadGetName (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -791,7 +793,8 @@ static osThreadId_t svcRtxThreadGetId (void) {
 /// Get current thread state of a thread.
 /// \note API identical to osThreadGetState
 static osThreadState_t svcRtxThreadGetState (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t    *thread = osRtxThreadId(thread_id);
+  osThreadState_t state;
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -799,15 +802,17 @@ static osThreadState_t svcRtxThreadGetState (osThreadId_t thread_id) {
     return osThreadError;
   }
 
-  EvrRtxThreadGetState(thread, (osThreadState_t)(thread->state & osRtxThreadStateMask));
+  state = osRtxThreadState(thread);
+
+  EvrRtxThreadGetState(thread, state);
 
-  return ((osThreadState_t)(thread->state & osRtxThreadStateMask));
+  return state;
 }
 
 /// Get stack size of a thread.
 /// \note API identical to osThreadGetStackSize
 static uint32_t svcRtxThreadGetStackSize (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -829,9 +834,9 @@ static uint32_t svcRtxThreadGetStackSize (osThreadId_t thread_id) {
 /// Get available stack space of a thread based on stack watermark recording during execution.
 /// \note API identical to osThreadGetStackSpace
 static uint32_t svcRtxThreadGetStackSpace (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
-  uint32_t    *stack;
-  uint32_t     space;
+  os_thread_t    *thread = osRtxThreadId(thread_id);
+  const uint32_t *stack;
+        uint32_t  space;
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -869,7 +874,7 @@ static uint32_t svcRtxThreadGetStackSpace (osThreadId_t thread_id) {
 /// Change priority of a thread.
 /// \note API identical to osThreadSetPriority
 static osStatus_t svcRtxThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread) ||
@@ -898,7 +903,8 @@ static osStatus_t svcRtxThreadSetPriority (osThreadId_t thread_id, osPriority_t
 /// Get current priority of a thread.
 /// \note API identical to osThreadGetPriority
 static osPriority_t svcRtxThreadGetPriority (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
+  osPriority_t priority;
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -913,9 +919,11 @@ static osPriority_t svcRtxThreadGetPriority (osThreadId_t thread_id) {
     return osPriorityError;
   }
 
-  EvrRtxThreadGetPriority(thread, (osPriority_t)thread->priority);
+  priority = osRtxThreadPriority(thread);
+
+  EvrRtxThreadGetPriority(thread, priority);
 
-  return ((osPriority_t)thread->priority);
+  return priority;
 }
 
 /// Pass control to next thread that is in state READY.
@@ -943,7 +951,7 @@ static osStatus_t svcRtxThreadYield (void) {
 /// Suspend execution of a thread.
 /// \note API identical to osThreadSuspend
 static osStatus_t svcRtxThreadSuspend (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
   osStatus_t   status;
 
   // Check parameters
@@ -1000,7 +1008,7 @@ static osStatus_t svcRtxThreadSuspend (osThreadId_t thread_id) {
 /// Resume execution of a thread.
 /// \note API identical to osThreadResume
 static osStatus_t svcRtxThreadResume (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -1060,7 +1068,7 @@ static void osRtxThreadFree (os_thread_t *thread) {
 /// Detach a thread (thread storage can be reclaimed when thread terminates).
 /// \note API identical to osThreadDetach
 static osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
 
   // Check parameters
   if ((thread == NULL) || (thread->id != osRtxIdThread)) {
@@ -1095,7 +1103,7 @@ static osStatus_t svcRtxThreadDetach (osThreadId_t thread_id) {
 /// Wait for specified thread to terminate.
 /// \note API identical to osThreadJoin
 static osStatus_t svcRtxThreadJoin (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
   osStatus_t   status;
 
   // Check parameters
@@ -1186,7 +1194,7 @@ static void svcRtxThreadExit (void) {
 /// Terminate execution of a thread.
 /// \note API identical to osThreadTerminate
 static osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
   osStatus_t   status;
 
   // Check parameters
@@ -1264,23 +1272,29 @@ static osStatus_t svcRtxThreadTerminate (osThreadId_t thread_id) {
 /// Get number of active threads.
 /// \note API identical to osThreadGetCount
 static uint32_t svcRtxThreadGetCount (void) {
-  os_thread_t *thread;
-  uint32_t     count;
+  const os_thread_t *thread;
+        uint32_t     count;
 
   // Running Thread
   count = 1U;
 
   // Ready List
   for (thread = osRtxInfo.thread.ready.thread_list;
-       (thread != NULL); thread = thread->thread_next, count++) {};
+       thread != NULL; thread = thread->thread_next) {
+    count++;
+  }
 
   // Delay List
   for (thread = osRtxInfo.thread.delay_list;
-       (thread != NULL); thread = thread->delay_next,  count++) {};
+       thread != NULL; thread = thread->delay_next) {
+    count++;
+  }
 
   // Wait List
   for (thread = osRtxInfo.thread.wait_list;
-       (thread != NULL); thread = thread->delay_next,  count++) {};
+       thread != NULL; thread = thread->delay_next) {
+    count++;
+  }
 
   EvrRtxThreadGetCount(count);
 
@@ -1300,25 +1314,32 @@ static uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t arra
   }
 
   // Running Thread
-  *thread_array++ = osRtxThreadGetRunning();
-  count = 1U;
+  *thread_array = osRtxThreadGetRunning();
+   thread_array++;
+   count = 1U;
 
   // Ready List
   for (thread = osRtxInfo.thread.ready.thread_list;
-       (thread != NULL) && (count < array_items); thread = thread->thread_next, count++) {
-    *thread_array++ = thread;
+       (thread != NULL) && (count < array_items); thread = thread->thread_next) {
+    *thread_array = thread;
+     thread_array++;
+     count++;
   }
 
   // Delay List
   for (thread = osRtxInfo.thread.delay_list;
-       (thread != NULL) && (count < array_items); thread = thread->delay_next,  count++) {
-    *thread_array++ = thread;
+       (thread != NULL) && (count < array_items); thread = thread->delay_next) {
+    *thread_array = thread;
+     thread_array++;
+     count++;
   }
 
   // Wait List
   for (thread = osRtxInfo.thread.wait_list;
-       (thread != NULL) && (count < array_items); thread = thread->delay_next,  count++) {
-    *thread_array++ = thread;
+       (thread != NULL) && (count < array_items); thread = thread->delay_next) {
+    *thread_array = thread;
+     thread_array++;
+     count++;
   }
 
   EvrRtxThreadEnumerate(thread_array - count, array_items, count);
@@ -1329,7 +1350,7 @@ static uint32_t svcRtxThreadEnumerate (osThreadId_t *thread_array, uint32_t arra
 /// Set the specified Thread Flags of a thread.
 /// \note API identical to osThreadFlagsSet
 static uint32_t svcRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
   uint32_t     thread_flags;
   uint32_t     thread_flags0;
 
@@ -1375,6 +1396,7 @@ static uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
   os_thread_t *thread;
   uint32_t     thread_flags;
 
+  // Check running thread
   thread = osRtxThreadGetRunning();
   if (thread == NULL) {
     EvrRtxThreadError(NULL, osRtxErrorKernelNotRunning);
@@ -1405,8 +1427,9 @@ static uint32_t svcRtxThreadFlagsClear (uint32_t flags) {
 /// Get the current Thread Flags of current running thread.
 /// \note API identical to osThreadFlagsGet
 static uint32_t svcRtxThreadFlagsGet (void) {
-  os_thread_t *thread;
+  const os_thread_t *thread;
 
+  // Check running thread
   thread = osRtxThreadGetRunning();
   if (thread == NULL) {
     EvrRtxThreadFlagsGet(0U);
@@ -1498,7 +1521,7 @@ SVC0_3 (ThreadFlagsWait,     uint32_t,        uint32_t, uint32_t, uint32_t)
 /// \note API identical to osThreadFlagsSet
 __STATIC_INLINE
 uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
-  os_thread_t *thread = (os_thread_t *)thread_id;
+  os_thread_t *thread = osRtxThreadId(thread_id);
   uint32_t     thread_flags;
 
   // Check parameters
@@ -1519,7 +1542,7 @@ uint32_t isrRtxThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
   thread_flags = ThreadFlagsSet(thread, flags);
 
   // Register post ISR processing
-  osRtxPostProcess((os_object_t *)thread);
+  osRtxPostProcess(osRtxObject(thread));
 
   EvrRtxThreadFlagsSetDone(thread, thread_flags);
 

+ 11 - 9
CMSIS/RTOS2/RTX/Source/rtx_timer.c

@@ -115,7 +115,9 @@ __WEAK void osRtxTimerThread (void *argument) {
   osStatus_t       status;
   (void)           argument;
 
-  osRtxInfo.timer.mq = osMessageQueueNew(osRtxConfig.timer_mq_mcnt, sizeof(os_timer_finfo_t), osRtxConfig.timer_mq_attr);
+  osRtxInfo.timer.mq = osRtxMessageQueueId(
+    osMessageQueueNew(osRtxConfig.timer_mq_mcnt, sizeof(os_timer_finfo_t), osRtxConfig.timer_mq_attr)
+  );
   if (osRtxInfo.timer.mq == NULL) {
     return;
   }
@@ -123,8 +125,8 @@ __WEAK void osRtxTimerThread (void *argument) {
   for (;;) {
     status = osMessageQueueGet(osRtxInfo.timer.mq, &finfo, NULL, osWaitForever);
     if (status == osOK) {
-      EvrRtxTimerCallback(*(osTimerFunc_t)finfo.fp, finfo.arg);
-      (*(osTimerFunc_t)finfo.fp)(finfo.arg);
+      EvrRtxTimerCallback(finfo.func, finfo.arg);
+      (finfo.func)(finfo.arg);
     }
   }
 }
@@ -187,7 +189,7 @@ static osTimerId_t svcRtxTimerNew (osTimerFunc_t func, osTimerType_t type, void
     timer->next       = NULL;
     timer->tick       = 0U;
     timer->load       = 0U;
-    timer->finfo.fp   = (void *)func;
+    timer->finfo.func = func;
     timer->finfo.arg  = argument;
 
     EvrRtxTimerCreated(timer, timer->name);
@@ -201,7 +203,7 @@ static osTimerId_t svcRtxTimerNew (osTimerFunc_t func, osTimerType_t type, void
 /// Get name of a timer.
 /// \note API identical to osTimerGetName
 static const char *svcRtxTimerGetName (osTimerId_t timer_id) {
-  os_timer_t *timer = (os_timer_t *)timer_id;
+  os_timer_t *timer = osRtxTimerId(timer_id);
 
   // Check parameters
   if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
@@ -223,7 +225,7 @@ static const char *svcRtxTimerGetName (osTimerId_t timer_id) {
 /// Start or restart a timer.
 /// \note API identical to osTimerStart
 static osStatus_t svcRtxTimerStart (osTimerId_t timer_id, uint32_t ticks) {
-  os_timer_t *timer = (os_timer_t *)timer_id;
+  os_timer_t *timer = osRtxTimerId(timer_id);
 
   // Check parameters
   if ((timer == NULL) || (timer->id != osRtxIdTimer) || (ticks == 0U)) {
@@ -258,7 +260,7 @@ static osStatus_t svcRtxTimerStart (osTimerId_t timer_id, uint32_t ticks) {
 /// Stop a timer.
 /// \note API identical to osTimerStop
 static osStatus_t svcRtxTimerStop (osTimerId_t timer_id) {
-  os_timer_t *timer = (os_timer_t *)timer_id;
+  os_timer_t *timer = osRtxTimerId(timer_id);
 
   // Check parameters
   if ((timer == NULL) || (timer->id != osRtxIdTimer)) {
@@ -284,7 +286,7 @@ static osStatus_t svcRtxTimerStop (osTimerId_t timer_id) {
 /// Check if a timer is running.
 /// \note API identical to osTimerIsRunning
 static uint32_t svcRtxTimerIsRunning (osTimerId_t timer_id) {
-  os_timer_t *timer = (os_timer_t *)timer_id;
+  os_timer_t *timer = osRtxTimerId(timer_id);
   uint32_t    is_running;
 
   // Check parameters
@@ -308,7 +310,7 @@ static uint32_t svcRtxTimerIsRunning (osTimerId_t timer_id) {
 /// Delete a timer.
 /// \note API identical to osTimerDelete
 static osStatus_t svcRtxTimerDelete (osTimerId_t timer_id) {
-  os_timer_t *timer = (os_timer_t *)timer_id;
+  os_timer_t *timer = osRtxTimerId(timer_id);
 
   // Check parameters
   if ((timer == NULL) || (timer->id != osRtxIdTimer)) {