mbed TLS v2.2.0
slpal_baremetal.h
Go to the documentation of this file.
1 /*
2  * Platform Abstraction Layer interface for bare-metal applications.
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_BAREMETAL_H
21 #define MBEDTLS_SLPAL_BAREMETAL_H
22 
23 #if !defined(MBEDTLS_CONFIG_FILE)
24 #include "config.h"
25 #else
26 #include MBEDTLS_CONFIG_FILE
27 #endif
28 
29 /* Need to include emlib configuration file, if present,
30  in order to determine CORE_ATOMIC_BASE_PRIORITY_LEVEL. */
31 #if defined(EMLIB_USER_CONFIG)
32 #include "emlib_config.h"
33 #endif
34 
35 #include "slpal_common.h"
36 #include "em_assert.h"
37 #include "em_core.h"
38 #include <stdbool.h>
39 
40 /*******************************************************************************
41  ****************************** Defines **********************************
42  ******************************************************************************/
43 
44 /* In order to wait forever in blocking functions the user can pass the
45  following value. */
46 #define SLPAL_WAIT_FOREVER (-1)
47 /* In order to return immediately in blocking functions the user can pass the
48  following value. */
49 #define SLPAL_NON_BLOCKING (0)
50 
51 #if !defined(CORE_ATOMIC_BASE_PRIORITY_LEVEL)
52 #define CORE_ATOMIC_BASE_PRIORITY_LEVEL (3) /* Same as in emlib/em_core.c */
53 #endif
54 
55 /* Priority to use for CRYPTO IRQ */
56 #if defined(MBEDTLS_CRYPTO_IRQ_PRIORITY)
57 #define SLPAL_CRYPTO_IRQ_PRIORITY MBEDTLS_CRYPTO_IRQ_PRIORITY
58 #else
59 #define SLPAL_CRYPTO_IRQ_PRIORITY (0x0)
60 #endif
61 
62 /*******************************************************************************
63  ****************************** TYPEDEFS **********************************
64  ******************************************************************************/
65 
66 /* Completion type used to wait for and signal end of operation. */
67 typedef volatile bool SLPAL_Completion_t;
68 /* Mutex object used for mutual exclusion, e.g. locking resources. */
69 typedef volatile unsigned int SLPAL_Mutex_t;
70 
71 /*******************************************************************************
72  ****************************** GLOBALS **********************************
73  ******************************************************************************/
74 #if defined(SLPAL_TEST)
75 /* Global variable to keep track of ticks in bare metal test apps. */
76 extern unsigned int gTicks;
77 #endif
78 
79 /*******************************************************************************
80  ****************************** FUNCTIONS **********************************
81  ******************************************************************************/
82 
83 /***************************************************************************/
97 {
98  return (SLPAL_irqState_t) CORE_EnterAtomic();
99 }
100 
101 /***************************************************************************/
114 __STATIC_INLINE void SLPAL_CriticalExit(SLPAL_irqState_t irqState)
115 {
116  CORE_ExitAtomic((CORE_irqState_t) irqState);
117 }
118 
119 /***************************************************************************/
127 __STATIC_INLINE void SLPAL_IsrEnter(void)
128 {
129 }
130 
131 /***************************************************************************/
139 __STATIC_INLINE void SLPAL_IsrExit(void)
140 {
141 }
142 
143 /***************************************************************************/
154 __STATIC_INLINE void* SLPAL_ThreadIdGet(void)
155 {
156  return 0;
157 }
158 
159 /***************************************************************************/
171 __STATIC_INLINE unsigned long SLPAL_ThreadPriorityGet(void)
172 {
173  return (unsigned long) RUNNING_AT_INTERRUPT_LEVEL ? 1 : 0;
174 }
175 
176 /***************************************************************************/
187 __STATIC_INLINE void SLPAL_ThreadResume(void* threadId)
188 {
189  (void)threadId;
190 }
191 
192 /***************************************************************************/
203 __STATIC_INLINE void SLPAL_ThreadSuspend(void* threadId)
204 {
205  (void)threadId;
206 }
207 
208 /*******************************************************************************
209  * @brief
210  * Initialize a completion object.
211  *
212  * @param pComp
213  * Pointer to an SLPAL_Completion_t object allocated by the user.
214  *
215  * @return
216  * Always 0 for success
217 *******************************************************************************/
218 __STATIC_INLINE int SLPAL_InitCompletion(SLPAL_Completion_t *pComp)
219 {
220  *pComp = false;
221  return (0);
222 }
223 
224 /*******************************************************************************
225  * @brief
226  * Free a completion object.
227  *
228  * @param pComp
229  * Pointer to an SLPAL_Completion_t object.
230  *
231  * @return
232  * Always 0 for success
233 *******************************************************************************/
234 __STATIC_INLINE int SLPAL_FreeCompletion(SLPAL_Completion_t *pComp)
235 {
236  *pComp = false;
237  return (0);
238 }
239 
240 /*******************************************************************************
241  * @brief
242  * Wait for completion event.
243  *
244  * @param[in] pComp
245  * Pointer to completion object which must be initialized by calling
246  * SLPAL_CompletionInit before calling this function.
247  *
248  * @param[in] ticks
249  * Ticks to wait for the completion.
250  * Pass a value of SLPAL_WAIT_FOREVER in order to wait forever.
251  * Pass a value of SLPAL_NON_BLOCKING in order to return immediately.
252  *
253  * @return
254  * 0 if success, and SLPAL_ERROR_TIMEOUT if the completion was not completed
255  * within the timeout.
256 ********************************************************************************/
257 __STATIC_INLINE int SLPAL_WaitForCompletion(SLPAL_Completion_t *pComp, int ticks)
258 {
259  int ret;
260  if (ticks == SLPAL_WAIT_FOREVER)
261  {
262  while( *pComp == false )
263  {
264 #if defined(SLPAL_TEST)
265  gTicks++;
266 #endif
267  }
268  *pComp = false;
269  ret = 0;
270  }
271  else
272  {
273  while ((*pComp == false) && (ticks>0))
274  {
275  ticks--;
276 #if defined(SLPAL_TEST)
277  gTicks++;
278 #endif
279  }
280  if (*pComp == true)
281  {
282  *pComp = false;
283  ret = 0;
284  }
285  else
286  {
287  ret = SLPAL_ERROR_TIMEOUT;
288  }
289  }
290 
291  return( ret );
292 }
293 
294 /*******************************************************************************
295  * @brief
296  * Signal completion.
297  *
298  * @param[in] pComp
299  * Pointer to completion object which must be initialized by calling
300  * SLPAL_CompletionInit before calling this function.
301  *
302  * @return
303  * 0 for success
304 ********************************************************************************/
305 __STATIC_INLINE int SLPAL_Complete(SLPAL_Completion_t* pComp)
306 {
307  *pComp = true;
308  return ( 0 );
309 }
310 
311 /*******************************************************************************
312  * @brief
313  * Initialize a mutex object.
314  *
315  * @param pMutex
316  * Pointer to an SLPAL_Mutex_t object allocated by the user.
317  *
318  * @return
319  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
320 *******************************************************************************/
321 __STATIC_INLINE int SLPAL_InitMutex( SLPAL_Mutex_t *pMutex )
322 {
323  *pMutex = 1;
324  return( 0 );
325 }
326 
327 /*******************************************************************************
328  * @brief
329  * Free a mutex object.
330  *
331  * @param pMutex
332  * Pointer to an SLPAL_Mutex_t object.
333  *
334  * @return
335  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
336 *******************************************************************************/
337 __STATIC_INLINE int SLPAL_FreeMutex(SLPAL_Mutex_t *pMutex)
338 {
339  *pMutex = 0;
340  return( 0 );
341 }
342 
343 /*******************************************************************************
344  * @brief
345  * Take (and optionally wait for) a mutex to be given.
346  *
347  * @param[in] pMutex
348  * Pointer to mutex object which must be initialized by calling
349  * SLPAL_MutexInit before calling this function.
350  *
351  * @param[in] ticks
352  * Ticks to wait for the mutex.
353  * Pass a value of SLPAL_WAIT_FOREVER in order to wait forever.
354  * Pass a value of SLPAL_NON_BLOCKING in order to return immediately.
355  *
356  * @return
357  * 0 if success, and SLPAL_ERROR_TIMEOUT if the mutex was not given
358  * within the timeout.
359 ********************************************************************************/
360 __STATIC_INLINE int SLPAL_TakeMutex(SLPAL_Mutex_t *pMutex, int ticks)
361 {
362  int ret;
363  if (ticks == SLPAL_WAIT_FOREVER)
364  {
365  while( *pMutex == 0 )
366  {
367 #if defined(SLPAL_TEST)
368  gTicks++;
369 #endif
370  }
371  *pMutex = 0;
372  ret = 0;
373  }
374  else
375  {
376  while ( (*pMutex == 0) && (ticks>0) )
377  {
378  ticks--;
379 #if defined(SLPAL_TEST)
380  gTicks++;
381 #endif
382  }
383  if (*pMutex > 0)
384  {
385  *pMutex = 0;
386  ret = 0;
387  }
388  else
389  {
390  ret = SLPAL_ERROR_TIMEOUT;
391  }
392  }
393 
394  return( ret );
395 }
396 
397 /*******************************************************************************
398  * @brief
399  * Give mutex.
400  *
401  * @param[in] pMutex
402  * Pointer to mutex object which must be initialized by calling
403  * SLPAL_MutexInit before calling this function.
404  *
405  * @return
406  * 0 for success, SLPAL_ERROR_OS_SPECIFIC for error.
407 ********************************************************************************/
408 __STATIC_INLINE int SLPAL_GiveMutex(SLPAL_Mutex_t* pMutex)
409 {
410  *pMutex = 1;
411  return ( 0 );
412 }
413 
414 #endif /* MBEDTLS_SLPAL_BAREMETAL_H */
#define RUNNING_AT_INTERRUPT_LEVEL
Definition: slpal_common.h:37
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
__STATIC_INLINE void SLPAL_IsrEnter(void)
Enter an ISR.
#define SLPAL_WAIT_FOREVER
__STATIC_INLINE int SLPAL_TakeMutex(SLPAL_Mutex_t *pMutex, int ticks)
Compatibility names (set of defines)
#define SLPAL_ERROR_TIMEOUT
Definition: slpal_common.h:29
__STATIC_INLINE void * SLPAL_ThreadIdGet(void)
Get thread identifier of calling thread.
__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_ThreadResume(void *threadId)
Resume a thread which may have been suspended.
__STATIC_INLINE void SLPAL_ThreadSuspend(void *threadId)
Suspend a thread.
__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)