TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
os_wrapper_cmsis_rtos_v2.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "os_wrapper/thread.h"
9 #include "os_wrapper/mutex.h"
10 #include "os_wrapper/semaphore.h"
11 
12 #include <string.h>
13 #include "cmsis_os2.h"
14 
15 /* This is an example OS abstraction layer for CMSIS-RTOSv2 */
16 
17 void *os_wrapper_thread_new(const char *name, int32_t stack_size,
18  os_wrapper_thread_func func, void *arg,
19  uint32_t priority)
20 {
21  osThreadAttr_t task_attribs = {.tz_module = 1};
22 
23  /* By default, the thread starts as osThreadDetached */
24  if (stack_size != OS_WRAPPER_DEFAULT_STACK_SIZE) {
25  task_attribs.stack_size = stack_size;
26  }
27  task_attribs.name = name;
28  task_attribs.priority = (osPriority_t) priority;
29 
30  return (void *)osThreadNew(func, arg, &task_attribs);
31 }
32 
33 void *os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count,
34  const char *name)
35 {
36  osSemaphoreAttr_t sema_attrib = {0};
37 
38  sema_attrib.name = name;
39 
40  return (void *)osSemaphoreNew(max_count, initial_count, &sema_attrib);
41 }
42 
43 uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout)
44 {
45  osStatus_t status;
46 
47  status = osSemaphoreAcquire((osSemaphoreId_t)handle,
48  (timeout == OS_WRAPPER_WAIT_FOREVER) ?
49  osWaitForever : timeout);
50  if (status != osOK) {
51  return OS_WRAPPER_ERROR;
52  }
53 
54  return OS_WRAPPER_SUCCESS;
55 }
56 
57 uint32_t os_wrapper_semaphore_release(void *handle)
58 {
59  osStatus_t status;
60 
61  status = osSemaphoreRelease((osSemaphoreId_t)handle);
62  if (status != osOK) {
63  return OS_WRAPPER_ERROR;
64  }
65 
66  return OS_WRAPPER_SUCCESS;
67 }
68 
69 uint32_t os_wrapper_semaphore_delete(void *handle)
70 {
71  osStatus_t status;
72 
73  status = osSemaphoreDelete((osSemaphoreId_t)handle);
74  if (status != osOK) {
75  return OS_WRAPPER_ERROR;
76  }
77 
78  return OS_WRAPPER_SUCCESS;
79 }
80 
82 {
83  const osMutexAttr_t attr = {
84  .name = NULL,
85  .attr_bits = osMutexPrioInherit, /* Priority inheritance is recommended
86  * to enable if it is supported.
87  * For recursive mutex and the ability
88  * of auto release when owner being
89  * terminated is not required.
90  */
91  .cb_mem = NULL,
92  .cb_size = 0U
93  };
94 
95  return (void *)osMutexNew(&attr);
96 }
97 
98 uint32_t os_wrapper_mutex_acquire(void *handle, uint32_t timeout)
99 {
100  osStatus_t status = osOK;
101 
102  if (!handle) {
103  return OS_WRAPPER_ERROR;
104  }
105 
106  status = osMutexAcquire((osMutexId_t)handle,
107  (timeout == OS_WRAPPER_WAIT_FOREVER) ?
108  osWaitForever : timeout);
109  if (status != osOK) {
110  return OS_WRAPPER_ERROR;
111  }
112 
113  return OS_WRAPPER_SUCCESS;
114 }
115 
116 uint32_t os_wrapper_mutex_release(void *handle)
117 {
118  osStatus_t status = osOK;
119 
120  if (!handle) {
121  return OS_WRAPPER_ERROR;
122  }
123 
124  status = osMutexRelease((osMutexId_t)handle);
125  if (status != osOK) {
126  return OS_WRAPPER_ERROR;
127  }
128 
129  return OS_WRAPPER_SUCCESS;
130 }
131 
132 uint32_t os_wrapper_mutex_delete(void *handle)
133 {
134  osStatus_t status = osOK;
135 
136  if (!handle) {
137  return OS_WRAPPER_ERROR;
138  }
139 
140  status = osMutexDelete((osMutexId_t)handle);
141  if (status != osOK) {
142  return OS_WRAPPER_ERROR;
143  }
144 
145  return OS_WRAPPER_SUCCESS;
146 }
147 
149 {
150  return (void *)osThreadGetId();
151 }
152 
153 uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority)
154 {
155  osPriority_t prio;
156 
157  prio = osThreadGetPriority((osThreadId_t)handle);
158  if (prio == osPriorityError) {
159  return OS_WRAPPER_ERROR;
160  }
161 
162  *priority = (uint32_t)prio;
163 
164  return OS_WRAPPER_SUCCESS;
165 }
166 
168 {
169  osThreadExit();
170 }
171 
172 uint32_t os_wrapper_thread_set_flag(void *handle, uint32_t flags)
173 {
174  uint32_t ret;
175 
176  ret = osThreadFlagsSet((osThreadId_t)handle, flags);
177  if (ret & osFlagsError) {
178  return OS_WRAPPER_ERROR;
179  }
180 
181  return OS_WRAPPER_SUCCESS;
182 }
183 
184 /*
185  * According to the description of CMSIS-RTOS v2 Thread Flags,
186  * osThreadFlagsSet() can be called inside Interrupt Service Routine.
187  */
188 uint32_t os_wrapper_thread_set_flag_isr(void *handle, uint32_t flags)
189 {
190  uint32_t ret;
191 
192  ret = osThreadFlagsSet((osThreadId_t)handle, flags);
193  if (ret & osFlagsError) {
194  return OS_WRAPPER_ERROR;
195  }
196 
197  return OS_WRAPPER_SUCCESS;
198 }
199 
200 uint32_t os_wrapper_thread_wait_flag(uint32_t flags, uint32_t timeout)
201 {
202  uint32_t ret;
203 
204  ret = osThreadFlagsWait(flags, osFlagsWaitAll,
205  (timeout == OS_WRAPPER_WAIT_FOREVER) ?
206  osWaitForever : timeout);
207  if (ret & osFlagsError) {
208  return OS_WRAPPER_ERROR;
209  }
210 
211  return OS_WRAPPER_SUCCESS;
212 }
213 
214 uint32_t os_wrapper_get_tick(void)
215 {
216  return osKernelGetTickCount();
217 }
uint32_t os_wrapper_mutex_delete(void *handle)
Deletes a mutex that is created by os_wrapper_mutex_create()
uint32_t os_wrapper_semaphore_delete(void *handle)
Deletes the semaphore.
void * os_wrapper_thread_get_handle(void)
Gets current thread handle.
uint32_t os_wrapper_semaphore_acquire(void *handle, uint32_t timeout)
Acquires the semaphore.
void(* os_wrapper_thread_func)(void *argument)
Definition: thread.h:18
#define OS_WRAPPER_DEFAULT_STACK_SIZE
Definition: common.h:20
uint32_t os_wrapper_thread_set_flag_isr(void *handle, uint32_t flags)
Set the event flags in an interrupt handler for synchronizing a thread specified by handle...
void os_wrapper_thread_exit(void)
Exits the calling thread.
uint32_t os_wrapper_thread_wait_flag(uint32_t flags, uint32_t timeout)
Wait for the event flags for synchronizing threads.
uint32_t os_wrapper_semaphore_release(void *handle)
Releases the semaphore.
#define OS_WRAPPER_WAIT_FOREVER
Definition: common.h:19
uint32_t os_wrapper_mutex_acquire(void *handle, uint32_t timeout)
Acquires a mutex that is created by os_wrapper_mutex_create()
#define OS_WRAPPER_ERROR
Definition: common.h:18
uint32_t os_wrapper_thread_get_priority(void *handle, uint32_t *priority)
Gets thread priority.
void * os_wrapper_semaphore_create(uint32_t max_count, uint32_t initial_count, const char *name)
Creates a new semaphore.
void * os_wrapper_mutex_create(void)
Creates a mutex for mutual exclusion of resources.
#define OS_WRAPPER_SUCCESS
Definition: common.h:17
uint32_t os_wrapper_thread_set_flag(void *handle, uint32_t flags)
Set the event flags for synchronizing a thread specified by handle.
uint32_t os_wrapper_mutex_release(void *handle)
Releases the mutex acquired previously.
void * os_wrapper_thread_new(const char *name, int32_t stack_size, os_wrapper_thread_func func, void *arg, uint32_t priority)
Creates a new thread.
uint32_t os_wrapper_get_tick(void)
Return RTOS current tick count.