S32 SDK
osif_baremetal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
50 #include "osif.h"
51 #include "devassert.h"
52 #include <stddef.h>
53 
54 #include "interrupt_manager.h"
55 #include "clock_manager.h"
56 
57 #if defined (USING_OS_FREERTOS)
58 #error "Wrong OSIF selected. Please define symbol USING_OS_BAREMETAL (or no OS define) in project settings or change the OSIF variant"
59 #endif
60 
61 /*******************************************************************************
62  * Variables
63  ******************************************************************************/
64 
65 /*******************************************************************************
66  * Private Functions
67  ******************************************************************************/
68 
72 #define MSEC_TO_TICK(msec) (msec)
73 
74 static volatile uint32_t s_osif_tick_cnt = 0u;
75 
76 void SysTick_Handler(void);
77 
78 void SysTick_Handler(void)
79 {
80  s_osif_tick_cnt++;
81 }
82 
83 static inline uint32_t osif_GetCurrentTickCount(void)
84 {
85  return s_osif_tick_cnt;
86 }
87 
88 static inline void osif_UpdateSystickConfig(void)
89 {
90  uint32_t core_freq = 0u;
91  status_t clk_status = CLOCK_SYS_GetFreq(CORE_CLOCK, &core_freq);
92  DEV_ASSERT(clk_status == STATUS_SUCCESS);
93  DEV_ASSERT(core_freq > 0u);
94  (void)clk_status;
95 
96  S32_SysTick->RVR = S32_SysTick_RVR_RELOAD(core_freq / 1000u);
98 }
99 
102 /*******************************************************************************
103  * Code
104  ******************************************************************************/
105 
106 /*FUNCTION**********************************************************************
107  *
108  * Function Name : OSIF_TimeDelay
109  * Description : This function blocks execution for a number of milliseconds.
110  *
111  * Implements : OSIF_TimeDelay_baremetal_Activity
112  *END**************************************************************************/
113 void OSIF_TimeDelay(const uint32_t delay)
114 {
115  osif_UpdateSystickConfig();
116  uint32_t start = osif_GetCurrentTickCount();
117  uint32_t crt_ticks = osif_GetCurrentTickCount();
118  uint32_t delta = crt_ticks - start;
119  uint32_t delay_ticks = MSEC_TO_TICK(delay);
120  while (delta < delay_ticks)
121  {
122  crt_ticks = osif_GetCurrentTickCount();
123  delta = crt_ticks - start;
124  }
125 }
126 
127 /*FUNCTION**********************************************************************
128  *
129  * Function Name : OSIF_GetMilliseconds
130  * Description : This function returns the number of miliseconds elapsed since
131  * starting the internal timer. To initialize the internal timer
132  * (Systick) in bare-metal, call either OSIF_TimeDelay or
133  * OSIF_SemaWait functions. Calling OSIF_TimeDelay(0) will initialize
134  * the timer without any side-effects (no delay).
135  *
136  * Implements : OSIF_GetMilliseconds_baremetal_Activity
137  *END**************************************************************************/
138 uint32_t OSIF_GetMilliseconds(void)
139 {
140  /*
141  * Please make sure the timer is initialized before calling this function.
142  * For example, calling OSIF_TimeDelay(0) ensures that the timer is initialized
143  * without any other side-effects. If OSIF_TimeDelay or OSIF_SemaWait functions
144  * have been called, the timer is already initialized.
145  */
146  return osif_GetCurrentTickCount(); /* This assumes that 1 tick = 1 millisecond */
147 }
148 
149 /*FUNCTION**********************************************************************
150  *
151  * Function Name : OSIF_MutexLock
152  * Description : This function locks a mutex (mock operation in baremetal case).
153  *
154  * Implements : OSIF_MutexLock_baremetal_Activity
155  *END**************************************************************************/
156 status_t OSIF_MutexLock(const mutex_t * const pMutex,
157  const uint32_t timeout)
158 {
159  (void)pMutex;
160  (void)timeout;
161 
162  return STATUS_SUCCESS;
163 }
164 
165 /*FUNCTION**********************************************************************
166  *
167  * Function Name : OSIF_MutexUnlock
168  * Description : This function unlocks a mutex (mock operation in baremetal case).
169  *
170  * Implements : OSIF_MutexUnlock_baremetal_Activity
171  *END**************************************************************************/
172 status_t OSIF_MutexUnlock(const mutex_t * const pMutex)
173 {
174  (void)pMutex;
175 
176  return STATUS_SUCCESS;
177 }
178 
179 /*FUNCTION**********************************************************************
180  *
181  * Function Name : OSIF_MutexCreate
182  * Description : This function creates a mutex (mock operation in baremetal case).
183  *
184  * Implements : OSIF_MutexCreate_baremetal_Activity
185  *END**************************************************************************/
186 status_t OSIF_MutexCreate(mutex_t * const pMutex)
187 {
188  (void)pMutex;
189 
190  return STATUS_SUCCESS;
191 }
192 
193 /*FUNCTION**********************************************************************
194  *
195  * Function Name : OSIF_MutexDestroy
196  * Description : This function destroys a mutex (mock operation in baremetal case).
197  *
198  * Implements : OSIF_MutexDestroy_baremetal_Activity
199  *END**************************************************************************/
200 status_t OSIF_MutexDestroy(const mutex_t * const pMutex)
201 {
202  (void)pMutex;
203 
204  return STATUS_SUCCESS;
205 }
206 
207 /*FUNCTION**********************************************************************
208  *
209  * Function Name : OSIF_SemaWait
210  * Description : This function performs the 'wait' (decrement) operation on a semaphore.
211  *
212  * Implements : OSIF_SemaWait_baremetal_Activity
213  *END**************************************************************************/
214 status_t OSIF_SemaWait(semaphore_t * const pSem,
215  const uint32_t timeout)
216 {
217  DEV_ASSERT(pSem != NULL);
218 
219  uint32_t timeoutTicks;
220  status_t osif_ret_code = STATUS_SUCCESS;
221 
222  osif_UpdateSystickConfig();
223  /* Convert timeout from milliseconds to ticks. */
224  if (timeout == OSIF_WAIT_FOREVER)
225  {
226  timeoutTicks = OSIF_WAIT_FOREVER;
227  }
228  else
229  {
230  timeoutTicks = MSEC_TO_TICK(timeout);
231  }
232 
233  uint32_t start = osif_GetCurrentTickCount();
234  uint32_t end = (uint32_t)(start + timeoutTicks);
235  uint32_t max = end - start;
236  while (*pSem == 0u)
237  {
238  uint32_t crt_ticks = osif_GetCurrentTickCount();
239  uint32_t delta = crt_ticks - start;
240  if ((timeoutTicks != OSIF_WAIT_FOREVER) && (delta > max))
241  {
242  /* Timeout occured, stop waiting and return fail code */
243  osif_ret_code = STATUS_TIMEOUT;
244  break;
245  }
246  }
247 
248  if (osif_ret_code == STATUS_SUCCESS)
249  {
251  --(*pSem);
253  }
254 
255  return osif_ret_code;
256 }
257 
258 /*FUNCTION**********************************************************************
259  *
260  * Function Name : OSIF_SemaPost
261  * Description : This function performs the 'post' (increment) operation on a semaphore.
262  *
263  * Implements : OSIF_SemaPost_baremetal_Activity
264  *END**************************************************************************/
265 status_t OSIF_SemaPost(semaphore_t * const pSem)
266 {
267  DEV_ASSERT(pSem != NULL);
268 
269  status_t osif_ret_code = STATUS_SUCCESS;
271  if (*pSem != 255u)
272  {
273  ++(*pSem);
274  }
275  else
276  {
277  osif_ret_code = STATUS_ERROR;
278  }
279 
281 
282  return osif_ret_code;
283 }
284 
285 /*FUNCTION**********************************************************************
286  *
287  * Function Name : OSIF_SemaCreate
288  * Description : This function creates (initializes) a semaphore.
289  *
290  * Implements : OSIF_SemaCreate_baremetal_Activity
291  *END**************************************************************************/
292 status_t OSIF_SemaCreate(semaphore_t * const pSem,
293  const uint8_t initValue)
294 {
295  DEV_ASSERT(pSem != NULL);
296 
298  *pSem = initValue;
300 
301  return STATUS_SUCCESS;
302 }
303 
304 /*FUNCTION**********************************************************************
305  *
306  * Function Name : OSIF_SemaDestroy
307  * Description : This function destroys a semaphore object (mock operation in baremetal case).
308  *
309  * Implements : OSIF_SemaDestroy_baremetal_Activity
310  *END**************************************************************************/
311 status_t OSIF_SemaDestroy(const semaphore_t * const pSem)
312 {
313  DEV_ASSERT(pSem != NULL);
314 
315  (void)pSem;
316 
317  return STATUS_SUCCESS;
318 }
319 
320 /*******************************************************************************
321  * EOF
322  ******************************************************************************/
#define S32_SysTick
Definition: S32K144.h:10278
status_t OSIF_MutexDestroy(const mutex_t *const pMutex)
Destroys a previously created mutex.
status_t OSIF_SemaDestroy(const semaphore_t *const pSem)
Destroys a previously created semaphore.
#define S32_SysTick_RVR_RELOAD(x)
Definition: S32K144.h:10320
void OSIF_TimeDelay(const uint32_t delay)
Delays execution for a number of milliseconds.
status_t OSIF_MutexUnlock(const mutex_t *const pMutex)
Unlocks a previously locked mutex.
#define S32_SysTick_CSR_ENABLE(x)
Definition: S32K144.h:10303
status_t OSIF_MutexLock(const mutex_t *const pMutex, const uint32_t timeout)
Waits for a mutex and locks it.
uint32_t OSIF_GetMilliseconds(void)
Returns the number of miliseconds elapsed since starting the internal timer or starting the scheduler...
status_t OSIF_SemaCreate(semaphore_t *const pSem, const uint8_t initValue)
Creates a semaphore with a given value.
void INT_SYS_DisableIRQGlobal(void)
Disable system interrupt.
#define DEV_ASSERT(x)
Definition: devassert.h:78
status_t OSIF_MutexCreate(mutex_t *const pMutex)
Create an unlocked mutex.
status_t CLOCK_SYS_GetFreq(clock_names_t clockName, uint32_t *frequency)
Gets the clock frequency for a specific clock name.
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
void INT_SYS_EnableIRQGlobal(void)
Enables system interrupt.
status_t OSIF_SemaWait(semaphore_t *const pSem, const uint32_t timeout)
Decrement a semaphore with timeout.
#define S32_SysTick_CSR_TICKINT(x)
Definition: S32K144.h:10307
#define OSIF_WAIT_FOREVER
Definition: osif.h:65
status_t OSIF_SemaPost(semaphore_t *const pSem)
Increment a semaphore.