1 /**************************************************************************/ /**
2  * @file
3  * @brief OS abstraction primitives for the platform/security components
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 #ifndef SLI_PSEC_OSAL_BAREMETAL_H
32 #define SLI_PSEC_OSAL_BAREMETAL_H
33 
34 // -----------------------------------------------------------------------------
35 // Includes
36 #include "sl_common.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 // -----------------------------------------------------------------------------
43 // Defines
44 
45 /// In order to wait forever in blocking functions the user can pass the
46 /// following value.
47 #define SLI_PSEC_OSAL_WAIT_FOREVER  (-1)
48 /// In order to return immediately in blocking functions the user can pass the
49 /// following value.
50 #define SLI_PSEC_OSAL_NON_BLOCKING  (0)
51 
52 // -----------------------------------------------------------------------------
53 // Typedefs
54 
55 /// Completion type used to wait for and signal end of operation.
56 typedef volatile unsigned int sli_psec_osal_completion_t;
57 
58 /// SLI PSEC lock definition for Baremetal.
59 typedef volatile unsigned int sli_psec_osal_lock_t;
60 
61 // -----------------------------------------------------------------------------
62 // Globals
63 
64 #if defined(SLI_PSEC_OSAL_TEST)
65 /// Global variable to keep track of ticks in bare metal test apps.
66 extern unsigned int sli_sli_psec_test_ticks;
67 #endif
68 
69 // -----------------------------------------------------------------------------
70 // Functions
71 
72 /***************************************************************************//**
73  * @brief          Initialize a completion object.
74  *
75  * @param p_comp   Pointer to an sli_psec_osal_completion_t object allocated
76  *                 by the user.
77  *
78  * @return         Status code, @ref sl_status.h.
79  *****************************************************************************/
80 __STATIC_INLINE
sli_psec_osal_init_completion(sli_psec_osal_completion_t * p_comp)81 sl_status_t sli_psec_osal_init_completion(sli_psec_osal_completion_t *p_comp)
82 {
83   *p_comp = 0;
84   return SL_STATUS_OK;
85 }
86 
87 /***************************************************************************//**
88  * @brief          Free a completion object.
89  *
90  * @param p_comp   Pointer to an sli_psec_osal_completion_t object.
91  *
92  * @return         Status code, @ref sl_status.h.
93  *****************************************************************************/
94 __STATIC_INLINE
sli_psec_osal_free_completion(sli_psec_osal_completion_t * p_comp)95 sl_status_t sli_psec_osal_free_completion(sli_psec_osal_completion_t *p_comp)
96 {
97   *p_comp = 0;
98   return SL_STATUS_OK;
99 }
100 
101 /***************************************************************************//**
102  * @brief          Wait for completion event.
103  *
104  * @param p_comp   Pointer to completion object which must be initialized by
105  *                 calling sli_psec_osal_completion_init before calling this
106  *                 function.
107  *
108  * @param ticks    Ticks to wait for the completion.
109  *                 Pass a value of SLI_PSEC_OSAL_WAIT_FOREVER in order to
110  *                 wait forever.
111  *                 Pass a value of SLI_PSEC_OSAL_NON_BLOCKING in order to
112  *                 return immediately.
113  *
114  * @return         Status code, @ref sl_status.h. Typcally SL_STATUS_OK if success,
115  *                 or SL_STATUS_TIMEOUT if no completion within the given ticks.
116  *****************************************************************************/
117 __STATIC_INLINE sl_status_t
sli_psec_osal_wait_completion(sli_psec_osal_completion_t * p_comp,int ticks)118 sli_psec_osal_wait_completion(sli_psec_osal_completion_t *p_comp, int ticks)
119 {
120   int ret = SL_STATUS_TIMEOUT;
121 
122   if (ticks == SLI_PSEC_OSAL_WAIT_FOREVER) {
123     while ( *p_comp == 0 ) {
124 #if defined(SLI_PSEC_OSAL_TEST)
125       sli_sli_psec_test_ticks++;
126 #endif
127     }
128     *p_comp = 0;
129     ret = SL_STATUS_OK;
130   } else {
131     while ((*p_comp == 0) && (ticks > 0)) {
132       ticks--;
133 #if defined(SLI_PSEC_OSAL_TEST)
134       sli_sli_psec_test_ticks++;
135 #endif
136     }
137     if (*p_comp == 1) {
138       *p_comp = 0;
139       ret = SL_STATUS_OK;
140     }
141   }
142 
143   return ret;
144 }
145 
146 /***************************************************************************//**
147  * @brief          Signal completion.
148  *
149  * @param p_comp   Pointer to completion object which must be initialized by
150  *                 calling sli_psec_osal_completion_init before calling this
151  *                 function.
152  *
153  * @return         Status code, @ref sl_status.h.
154  *****************************************************************************/
155 __STATIC_INLINE
sli_psec_osal_complete(sli_psec_osal_completion_t * p_comp)156 sl_status_t sli_psec_osal_complete(sli_psec_osal_completion_t* p_comp)
157 {
158   *p_comp = 1;
159   return SL_STATUS_OK;
160 }
161 
162 #ifdef __cplusplus
163 }
164 #endif
165 
166 #endif // SLI_PSEC_OSAL_BAREMETAL_H
167