1 /***************************************************************************//**
2  * @file sl_cmsis_os2_common.h
3  * @brief OS-agnostic header to provide CMSIS OS-Specific APIs like typedefs.
4  * @version x.y.z
5  *******************************************************************************
6  * # License
7  * <b>Copyright 2020 Silicon Laboratories Inc. www.silabs.com</b>
8  *******************************************************************************
9  *
10  * SPDX-License-Identifier: Zlib
11  *
12  * The licensor of this software is Silicon Laboratories Inc.
13  *
14  * This software is provided 'as-is', without any express or implied
15  * warranty. In no event will the authors be held liable for any damages
16  * arising from the use of this software.
17  *
18  * Permission is granted to anyone to use this software for any purpose,
19  * including commercial applications, and to alter it and redistribute it
20  * freely, subject to the following restrictions:
21  *
22  * 1. The origin of this software must not be misrepresented; you must not
23  *    claim that you wrote the original software. If you use this software
24  *    in a product, an acknowledgment in the product documentation would be
25  *    appreciated but is not required.
26  * 2. Altered source versions must be plainly marked as such, and must not be
27  *    misrepresented as being the original software.
28  * 3. This notice may not be removed or altered from any source distribution.
29  *
30  ******************************************************************************/
31 
32 #ifndef SL_CMSIS_OS2_COMMON_H
33 #define SL_CMSIS_OS2_COMMON_H
34 
35 #include <stdint.h>
36 #include "cmsis_os2.h"
37 #include "sl_status.h"
38 
39 #if defined(SL_COMPONENT_CATALOG_PRESENT)
40 #include "sl_component_catalog.h"
41 #endif
42 
43 // Validate the chosen RTOS
44 #if !defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT) && !defined(SL_CATALOG_MICRIUMOS_KERNEL_PRESENT)
45 #error "The extended CMSIS RTOS2 API currently only supports FreeRTOS or MicriumOS"
46 #endif
47 
48 #if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT)
49 #include "FreeRTOS.h"
50 #elif defined(SL_CATALOG_MICRIUMOS_KERNEL_PRESENT)
51 #include "os.h"
52 #endif
53 
54 #if defined(SL_CATALOG_FREERTOS_KERNEL_PRESENT)
55 
56 #define   osEventFlagsCbSize          sizeof(StaticEventGroup_t)
57 #define   osThreadCbSize              sizeof(StaticTask_t)
58 #define   osTimerCbSize               sizeof(StaticTimer_t)
59 #define   osMutexCbSize               sizeof(StaticSemaphore_t)
60 #define   osSemaphoreCbSize           sizeof(StaticSemaphore_t)
61 #define   osMessageQueueCbSize        sizeof(StaticQueue_t)
62 #define   osAlignment                 (portBYTE_ALIGNMENT)
63 
64 typedef StaticEventGroup_t osEventFlags_t;
65 typedef StaticTask_t       osThread_t;
66 typedef StaticTimer_t      osTimer_t;
67 typedef StaticSemaphore_t  osMutex_t;
68 typedef StaticSemaphore_t  osSemaphore_t;
69 typedef StaticQueue_t      osMessageQueue_t;
70 
71 #elif defined(SL_CATALOG_MICRIUMOS_KERNEL_PRESENT)
72 
73 typedef struct {
74   OS_TCB      tcb; // This must be the first element, used by OSTCBCurPtr
75 #if (OS_CFG_FLAG_EN == DEF_ENABLED)
76   OS_FLAG_GRP flag_grp;
77 #endif
78 #if (OS_CFG_MUTEX_EN == DEF_ENABLED)
79   OS_MUTEX    join_mutex;
80 #endif
81   uint8_t     obj_dyn_alloc;
82   uint8_t     stack_dyn_alloc;
83   uint32_t    attr_bits;
84 } osThread_t;
85 
86 #if (CMSIS_RTOS2_TIMER_TASK_EN == DEF_ENABLED)
87 typedef struct {
88   sl_sleeptimer_timer_handle_t  handle;
89   osTimerFunc_t                 callback;
90   void                          *callback_data;
91   osTimerType_t                 type;
92   const char                    *name;
93   uint8_t                       dyn_alloc;
94 } osTimer_t;
95 #endif
96 
97 #if (OS_CFG_FLAG_EN == DEF_ENABLED)
98 typedef struct {
99   OS_FLAG_GRP flag_grp;
100   uint8_t     dyn_alloc;
101   uint32_t    flags;
102 } osEventFlags_t;
103 #endif
104 
105 #if (OS_CFG_MUTEX_EN == DEF_ENABLED)
106 typedef struct {
107   OS_MUTEX    mutex;
108   uint8_t     dyn_alloc;
109   uint8_t     recursive;
110 } osMutex_t;
111 #endif
112 
113 #if (OS_CFG_SEM_EN == DEF_ENABLED)
114 typedef struct {
115   OS_SEM      sem;
116   uint8_t     dyn_alloc;
117   uint32_t    max_ctr;
118 } osSemaphore_t;
119 #endif
120 
121 #if (OS_CFG_SEM_EN == DEF_ENABLED)
122 typedef struct {
123   OS_SEM      sem_put;
124   OS_SEM      sem_get;
125   uint8_t    *buf;
126   uint8_t     obj_dyn_alloc;
127   uint8_t     buf_dyn_alloc;
128   uint32_t    msg_count;
129   uint32_t    msg_size;
130   uint32_t    msg_queued;
131   uint32_t    msg_head;
132   uint32_t    msg_tail;
133 } osMessageQueue_t;
134 #endif
135 
136 #if (OS_CFG_SEM_EN == DEF_ENABLED)
137 typedef struct {
138   OS_SEM      sem;
139   uint8_t    *buf;
140   uint8_t     obj_dyn_alloc;
141   uint8_t     buf_dyn_alloc;
142   uint32_t    block_count;
143   uint32_t    block_size;
144   uint32_t    free_count;
145   uint32_t    free_head;
146 } osMemoryPool_t;
147 #endif
148 
149 #if (OS_CFG_FLAG_EN == DEF_ENABLED)
150 #define   osEventFlagsCbSize            sizeof(osEventFlags_t)
151 #endif
152 
153 #define   osThreadCbSize                sizeof(osThread_t)
154 
155 #if (OS_CFG_TMR_EN == DEF_ENABLED)
156 #define   osTimerCbSize                 sizeof(osTimer_t)
157 #endif
158 
159 #if (OS_CFG_MUTEX_EN == DEF_ENABLED)
160 #define   osMutexCbSize                 sizeof(osMutex_t)
161 #endif
162 
163 #if (OS_CFG_SEM_EN == DEF_ENABLED)
164 #define   osSemaphoreCbSize             sizeof(osSemaphore_t)
165 #endif
166 
167 #if (OS_CFG_SEM_EN == DEF_ENABLED)
168 #define   osMessageQueueCbSize          sizeof(osMessageQueue_t)
169 #endif
170 
171 #if (OS_CFG_SEM_EN == DEF_ENABLED)
172 #define   osMemoryPoolCbSize            sizeof(osMemoryPool_t)
173 #endif
174 
175 #define   osAlignment                   sizeof(CPU_ALIGN)
176 #endif // SL_CATALOG_MICRIUMOS_KERNEL_PRESENT
177 
178 // -----------------------------------------------------------------------------
179 // Functions
180 
181 #ifdef __cplusplus
182 extern "C" {
183 #endif
184 
185 /********************************************************************************************************
186  *                                  sl_cmsis_os_convert_status()
187  *
188  * @brief    Convert OsStatus from CMSIS-RTOS2 to sl_status type.
189  *
190  * @param    os_status  The OS status code returned by CMSIS-RTOS2 API.
191  *
192  * @return    Status code converted to sl_status.
193  *******************************************************************************************************/
194 sl_status_t sl_cmsis_os_convert_status(osStatus_t os_status);
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif // SL_CMSIS_OS2_COMMON_H
201