mbed TLS v2.2.0
slpal_ucos3.h
Go to the documentation of this file.
1 /*
2  * Platform Abstraction Layer interface for uC/OS-III.
3  *
4  * Copyright (C) 2016, Silicon Labs, http://www.silabs.com
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may
8  * not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MBEDTLS_SLPAL_UCOS3_H
21 #define MBEDTLS_SLPAL_UCOS3_H
22 
23 #if !defined(MBEDTLS_CONFIG_FILE)
24 #include "config.h"
25 #else
26 #include MBEDTLS_CONFIG_FILE
27 #endif
28 
29 #if defined( MBEDTLS_UCOS3 )
30 
31 #include "slpal_common.h"
32 #include "includes.h"
33 
34 /*******************************************************************************
35  ****************************** Defines **********************************
36  ******************************************************************************/
37 
38 /* In order to wait forever in blocking functions the user can pass the
39  following value. */
40 #define SLPAL_WAIT_FOREVER (0)
41 /* In order to return immediately in blocking functions the user can pass the
42  following value. */
43 #define SLPAL_NON_BLOCKING (-1)
44 
45 /* Priority to use for CRYPTO IRQ */
46 #if defined(MBEDTLS_CRYPTO_IRQ_PRIORITY)
47 #define SLPAL_CRYPTO_IRQ_PRIORITY MBEDTLS_CRYPTO_IRQ_PRIORITY
48 #else
49 #define SLPAL_CRYPTO_IRQ_PRIORITY ( 0 )
50 #endif
51 
52 /* The lowest priority of uC/OS-III is OS_CFG_PRIO_MAX, and 0 is highest.
53  In SLPAL_ThreadPriorityGet we inverse the order of priorities.
54  I.e. 0 is lowest in mbedtls crypto preemption. */
55 #define UCOS3_LOWEST_TASK_PRIO (OS_CFG_PRIO_MAX-2)
56 
57 /* Max thread priority of SLPAL interface, corresponds to the lowest
58  UCOS3 priority we allow inside this abstractaion module. */
59 #define SLPAL_MAX_PRIORITY (UCOS3_LOWEST_TASK_PRIO)
60 
61 /*******************************************************************************
62  ****************************** TYPEDEFS **********************************
63  ******************************************************************************/
64 
65 /* Completion type used to wait for and signal end of operation. */
66 typedef OS_SEM SLPAL_Completion_t;
67 /* Mutex object used for mutual exclusion, e.g. locking resources.
68  Shall support priority inheritance. */
69 typedef OS_MUTEX SLPAL_Mutex_t;
70 
71 /*******************************************************************************
72  ****************************** FUNCTIONS **********************************
73  ******************************************************************************/
74 
75 /***************************************************************************/
88 __STATIC_INLINE SLPAL_irqState_t SLPAL_CriticalEnter(void)
89 {
90  /* The CPU_CRITICAL_ENTER() macro requires a variable called cpu_sr which is
91  set to the return value of __get_PRIMASK(). */
92  CPU_SR cpu_sr;
93  CPU_CRITICAL_ENTER();
94  return (SLPAL_irqState_t) cpu_sr;
95 }
96 
97 /***************************************************************************/
110 __STATIC_INLINE void SLPAL_CriticalExit(SLPAL_irqState_t irqState)
111 {
112  /* The CPU_CRITICAL_EXIT() macro requires a variable called cpu_sr which is
113  given to __set_PRIMASK(cpu_sr). I.e. cpu_sr is the same value as returned
114  from CPU_CRITICAL_ENTER.
115  */
116  CPU_SR cpu_sr = (CPU_SR) irqState;
117  CPU_CRITICAL_EXIT();
118 }
119 
120 /***************************************************************************/
128 __STATIC_INLINE void SLPAL_IsrEnter(void)
129 {
130  OSIntEnter();
131 }
132 
133 /***************************************************************************/
141 __STATIC_INLINE void SLPAL_IsrExit(void)
142 {
143  OSIntExit();
144 }
145 
146 /***************************************************************************/
156 __STATIC_INLINE unsigned long SLPAL_ThreadPriorityGet(void)
157 {
158  return (unsigned long) (UCOS3_LOWEST_TASK_PRIO - OSTCBCurPtr->Prio);
159 }
160 
161 /*******************************************************************************
162  * @brief
163  * Initialize a completion object.
164  *
165  * @param pComp
166  * Pointer to an SLPAL_Completion_t object allocated by the user.
167  *
168  * @return
169  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
170 *******************************************************************************/
171 __STATIC_INLINE int SLPAL_InitCompletion(SLPAL_Completion_t *pComp)
172 {
173  OS_ERR err;
174  OSSemCreate((OS_SEM *) pComp, NULL, (OS_SEM_CTR) 0, &err);
175  EFM_ASSERT(err == OS_ERR_NONE);
176  return (err == OS_ERR_NONE ? 0 : SLPAL_ERROR_OS_SPECIFIC);
177 }
178 
179 /*******************************************************************************
180  * @brief
181  * Free a completion object.
182  *
183  * @param pComp
184  * Pointer to an SLPAL_Completion_t object.
185  *
186  * @return
187  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
188 *******************************************************************************/
189 __STATIC_INLINE int SLPAL_FreeCompletion(SLPAL_Completion_t *pComp)
190 {
191  OS_ERR err;
192  OSSemDel((OS_SEM *) pComp, OS_OPT_DEL_ALWAYS, &err);
193  EFM_ASSERT(err == OS_ERR_NONE);
194  return (err == OS_ERR_NONE ? 0 : SLPAL_ERROR_OS_SPECIFIC);
195 }
196 
197 /*******************************************************************************
198  * @brief
199  * Wait for completion event.
200  *
201  * @param[in] pComp
202  * Pointer to completion object which must be initialized by calling
203  * SLPAL_CompletionInit before calling this function.
204  *
205  * @param[in] ticks
206  * Ticks to wait for the completion.
207  * Pass a value of SLPAL_WAIT_FOREVER in order to wait forever.
208  * Pass a value of SLPAL_NON_BLOCKING in order to return immediately.
209  *
210  * @return
211  * 0 if success, and SLPAL_ERROR_TIMEOUT if the completion was not completed
212  * within the timeout.
213 ********************************************************************************/
214 __STATIC_INLINE int SLPAL_WaitForCompletion(SLPAL_Completion_t *pComp, int ticks)
215 {
216  OS_ERR err;
217  int ret;
218  OSSemPend((OS_SEM *) pComp,
219  (OS_TICK) ticks,
220  (OS_OPT) (ticks==SLPAL_NON_BLOCKING ?
221  OS_OPT_PEND_NON_BLOCKING : OS_OPT_PEND_BLOCKING),
222  NULL,
223  &err);
224  if (err == OS_ERR_NONE)
225  {
226  ret = 0;
227  }
228  else
229  {
230  if ( (err == OS_ERR_TIMEOUT) || (err == OS_ERR_PEND_WOULD_BLOCK) )
231  {
232  ret = SLPAL_ERROR_TIMEOUT;
233  }
234  else
235  {
236  /* Assert that no other error codes occur. */
237  EFM_ASSERT( false );
239  }
240  }
241  return ret;
242 }
243 
244 /*******************************************************************************
245  * @brief
246  * Signal completion.
247  *
248  * @param[in] pComp
249  * Pointer to completion object which must be initialized by calling
250  * SLPAL_CompletionInit before calling this function.
251  *
252  * @return
253  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
254 ********************************************************************************/
255 __STATIC_INLINE int SLPAL_Complete(SLPAL_Completion_t* pComp)
256 {
257  OS_ERR err;
258  OSSemPost((OS_SEM*) pComp, (OS_OPT) OS_OPT_POST_1, &err);
259  EFM_ASSERT(err == OS_ERR_NONE);
260  return (err == OS_ERR_NONE ? 0 : SLPAL_ERROR_OS_SPECIFIC);
261 }
262 
263 /*******************************************************************************
264  * @brief
265  * Initialize a mutex object with support for priority inheritance.
266  *
267  * @param pSem
268  * Pointer to an SLPAL_Mutex_t object allocated by the user.
269  *
270  * @return
271  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
272 *******************************************************************************/
273 __STATIC_INLINE int SLPAL_InitMutex(SLPAL_Mutex_t *pMutex)
274 {
275  OS_ERR err;
276  OSMutexCreate((OS_MUTEX *) pMutex, NULL, &err);
277  EFM_ASSERT(err == OS_ERR_NONE);
278  return (err == OS_ERR_NONE ? 0 : SLPAL_ERROR_OS_SPECIFIC);
279 }
280 
281 /*******************************************************************************
282  * @brief
283  * Free a mutex object.
284  *
285  * @param pMutex
286  * Pointer to an SLPAL_Mutex_t object.
287  *
288  * @return
289  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
290 *******************************************************************************/
291 __STATIC_INLINE int SLPAL_FreeMutex(SLPAL_Mutex_t *pMutex)
292 {
293  OS_ERR err;
294  OSMutexDel((OS_MUTEX *) pMutex, OS_OPT_DEL_ALWAYS, &err);
295  EFM_ASSERT(err == OS_ERR_NONE);
296  return (err == OS_ERR_NONE ? 0 : SLPAL_ERROR_OS_SPECIFIC);
297 }
298 
299 /*******************************************************************************
300  * @brief
301  * Take (and optionally wait for) a mutex to be given.
302  *
303  * @param[in] pMutex
304  * Pointer to mutex object which must be initialized by calling
305  * SLPAL_MutexInit before calling this function.
306  *
307  * @param[in] ticks
308  * Ticks to wait for the mutex.
309  * Pass a value of SLPAL_WAIT_FOREVER in order to wait forever.
310  * Pass a value of SLPAL_NON_BLOCKING in order to return immediately.
311  *
312  * @return
313  * 0 if success, and SLPAL_ERROR_TIMEOUT if the mutex was not given
314  * within the timeout.
315 ********************************************************************************/
316 __STATIC_INLINE int SLPAL_TakeMutex(SLPAL_Mutex_t *pMutex, int ticks)
317 {
318  OS_ERR err;
319  int ret;
320  OSMutexPend((OS_MUTEX *) pMutex,
321  (OS_TICK) ticks,
322  (OS_OPT) (ticks==SLPAL_NON_BLOCKING ?
323  OS_OPT_PEND_NON_BLOCKING : OS_OPT_PEND_BLOCKING),
324  NULL,
325  &err);
326  if (err == OS_ERR_NONE)
327  {
328  ret = 0;
329  }
330  else
331  {
332  if ( (err == OS_ERR_TIMEOUT) || (err == OS_ERR_PEND_WOULD_BLOCK) )
333  {
334  ret = SLPAL_ERROR_TIMEOUT;
335  }
336  else
337  {
338  /* Assert that no other error codes occur. */
339  EFM_ASSERT( false );
341  }
342  }
343  return ret;
344 }
345 
346 /*******************************************************************************
347  * @brief
348  * Give mutex.
349  *
350  * @param[in] pMutex
351  * Pointer to mutex object which must be initialized by calling
352  * SLPAL_MutexInit before calling this function.
353  *
354  * @return
355  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
356 ********************************************************************************/
357 __STATIC_INLINE int SLPAL_GiveMutex(SLPAL_Mutex_t* pMutex)
358 {
359  OS_ERR err;
360  OSMutexPost((OS_MUTEX*) pMutex, (OS_OPT) OS_OPT_POST_NONE, &err);
361  EFM_ASSERT(err == OS_ERR_NONE);
362  return (err == OS_ERR_NONE ? 0 : SLPAL_ERROR_OS_SPECIFIC);
363 }
364 
365 #endif /* MBEDTLS_UCOS3 */
366 
367 #endif /* MBEDTLS_SLPAL_UCOS3_H */
volatile unsigned int SLPAL_Mutex_t
uint32_t SLPAL_irqState_t
Storage for PRIMASK or BASEPRI value used for SLPAL critical regions.
Definition: slpal_common.h:46
#define SLPAL_ERROR_OS_SPECIFIC
Definition: slpal_common.h:30
__STATIC_INLINE void SLPAL_IsrEnter(void)
Enter an ISR.
__STATIC_INLINE int SLPAL_TakeMutex(SLPAL_Mutex_t *pMutex, int ticks)
Compatibility names (set of defines)
#define SLPAL_NON_BLOCKING
#define SLPAL_ERROR_TIMEOUT
Definition: slpal_common.h:29
__STATIC_INLINE void SLPAL_CriticalExit(SLPAL_irqState_t irqState)
Exit a critical region.
__STATIC_INLINE SLPAL_irqState_t SLPAL_CriticalEnter(void)
Enter a critical region.
__STATIC_INLINE int SLPAL_InitMutex(SLPAL_Mutex_t *pMutex)
volatile bool SLPAL_Completion_t
__STATIC_INLINE void SLPAL_IsrExit(void)
Exit an ISR.
__STATIC_INLINE int SLPAL_GiveMutex(SLPAL_Mutex_t *pMutex)
__STATIC_INLINE int SLPAL_InitCompletion(SLPAL_Completion_t *pComp)
__STATIC_INLINE unsigned long SLPAL_ThreadPriorityGet(void)
Get thread priority of calling thread.
__STATIC_INLINE int SLPAL_Complete(SLPAL_Completion_t *pComp)
__STATIC_INLINE int SLPAL_FreeMutex(SLPAL_Mutex_t *pMutex)
__STATIC_INLINE int SLPAL_WaitForCompletion(SLPAL_Completion_t *pComp, int ticks)
__STATIC_INLINE int SLPAL_FreeCompletion(SLPAL_Completion_t *pComp)