diff options
Diffstat (limited to 'lib/chibios-contrib/ext/mcux-sdk/components/osa/fsl_os_abstraction_bm.c')
-rw-r--r-- | lib/chibios-contrib/ext/mcux-sdk/components/osa/fsl_os_abstraction_bm.c | 1323 |
1 files changed, 1323 insertions, 0 deletions
diff --git a/lib/chibios-contrib/ext/mcux-sdk/components/osa/fsl_os_abstraction_bm.c b/lib/chibios-contrib/ext/mcux-sdk/components/osa/fsl_os_abstraction_bm.c new file mode 100644 index 000000000..bac5ec50d --- /dev/null +++ b/lib/chibios-contrib/ext/mcux-sdk/components/osa/fsl_os_abstraction_bm.c | |||
@@ -0,0 +1,1323 @@ | |||
1 | /*! | ||
2 | * Copyright (c) 2015, Freescale Semiconductor, Inc. | ||
3 | * Copyright 2016-2019 NXP | ||
4 | * | ||
5 | * | ||
6 | * This is the source file for the OS Abstraction layer for MQXLite. | ||
7 | * | ||
8 | * SPDX-License-Identifier: BSD-3-Clause | ||
9 | */ | ||
10 | |||
11 | /*! ********************************************************************************* | ||
12 | ************************************************************************************* | ||
13 | * Include | ||
14 | ************************************************************************************* | ||
15 | ********************************************************************************** */ | ||
16 | #include "fsl_component_generic_list.h" | ||
17 | #include "fsl_os_abstraction.h" | ||
18 | #include "fsl_os_abstraction_bm.h" | ||
19 | #include <string.h> | ||
20 | |||
21 | /*! ********************************************************************************* | ||
22 | ************************************************************************************* | ||
23 | * Private macros | ||
24 | ************************************************************************************* | ||
25 | ********************************************************************************** */ | ||
26 | |||
27 | /* Weak function. */ | ||
28 | #if defined(__GNUC__) | ||
29 | #define __WEAK_FUNC __attribute__((weak)) | ||
30 | #elif defined(__ICCARM__) | ||
31 | #define __WEAK_FUNC __weak | ||
32 | #elif defined(__CC_ARM) || defined(__ARMCC_VERSION) | ||
33 | #define __WEAK_FUNC __attribute__((weak)) | ||
34 | #endif | ||
35 | |||
36 | #ifdef DEBUG_ASSERT | ||
37 | #define OS_ASSERT(condition) \ | ||
38 | if (!(condition)) \ | ||
39 | while (1) \ | ||
40 | ; | ||
41 | #else | ||
42 | #define OS_ASSERT(condition) (void)(condition); | ||
43 | #endif | ||
44 | |||
45 | /************************************************************************************ | ||
46 | ************************************************************************************* | ||
47 | * Private type definitions | ||
48 | ************************************************************************************* | ||
49 | ************************************************************************************/ | ||
50 | |||
51 | /*! @brief Type for an semaphore */ | ||
52 | typedef struct Semaphore | ||
53 | { | ||
54 | uint32_t time_start; /*!< The time to start timeout */ | ||
55 | uint32_t timeout; /*!< Timeout to wait in milliseconds */ | ||
56 | volatile uint8_t isWaiting; /*!< Is any task waiting for a timeout on this object */ | ||
57 | volatile uint8_t semCount; /*!< The count value of the object */ | ||
58 | |||
59 | } semaphore_t; | ||
60 | |||
61 | /*! @brief Type for a mutex */ | ||
62 | typedef struct Mutex | ||
63 | { | ||
64 | uint32_t time_start; /*!< The time to start timeout */ | ||
65 | uint32_t timeout; /*!< Timeout to wait in milliseconds */ | ||
66 | volatile uint8_t isWaiting; /*!< Is any task waiting for a timeout on this mutex */ | ||
67 | volatile uint8_t isLocked; /*!< Is the object locked or not */ | ||
68 | } mutex_t; | ||
69 | |||
70 | #define gIdleTaskPriority_c ((task_priority_t)0) | ||
71 | #define gInvalidTaskPriority_c ((task_priority_t)-1) | ||
72 | |||
73 | /*! @brief Type for a task handler, returned by the OSA_TaskCreate function */ | ||
74 | typedef void (*task_t)(task_param_t param); | ||
75 | /*! @brief Task control block for bare metal. */ | ||
76 | typedef struct TaskControlBlock | ||
77 | { | ||
78 | list_element_t link; | ||
79 | osa_task_ptr_t p_func; /*!< Task's entry */ | ||
80 | osa_task_priority_t priority; /*!< Task's priority */ | ||
81 | osa_task_param_t param; /*!< Task's parameter */ | ||
82 | uint8_t haveToRun; /*!< Task was signaled */ | ||
83 | } task_control_block_t; | ||
84 | |||
85 | /*! @brief Type for a task pointer */ | ||
86 | typedef task_control_block_t *task_handler_t; | ||
87 | |||
88 | /*! @brief Type for a task stack */ | ||
89 | typedef uint32_t task_stack_t; | ||
90 | |||
91 | /*! @brief Type for an event object */ | ||
92 | typedef struct Event | ||
93 | { | ||
94 | uint32_t time_start; /*!< The time to start timeout */ | ||
95 | uint32_t timeout; /*!< Timeout to wait in milliseconds */ | ||
96 | volatile event_flags_t flags; /*!< The flags status */ | ||
97 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
98 | task_handler_t waitingTask; /*!< Handler to the waiting task */ | ||
99 | #endif | ||
100 | uint8_t autoClear; /*!< Auto clear or manual clear */ | ||
101 | volatile uint8_t isWaiting; /*!< Is any task waiting for a timeout on this event */ | ||
102 | } event_t; | ||
103 | |||
104 | /*! @brief Type for a message queue */ | ||
105 | typedef struct MsgQueue | ||
106 | { | ||
107 | volatile uint8_t isWaiting; /*!< Is any task waiting for a timeout */ | ||
108 | uint32_t time_start; /*!< The time to start timeout */ | ||
109 | uint32_t timeout; /*!< Timeout to wait in milliseconds */ | ||
110 | uint32_t size; /*!< The size(byte) of a single message */ | ||
111 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
112 | task_handler_t waitingTask; /*!< Handler to the waiting task */ | ||
113 | #endif | ||
114 | uint8_t *queueMem; /*!< Points to the queue memory */ | ||
115 | uint16_t number; /*!< The number of messages in the queue */ | ||
116 | uint16_t max; /*!< The max number of queue messages */ | ||
117 | uint16_t head; /*!< Index of the next message to be read */ | ||
118 | uint16_t tail; /*!< Index of the next place to write to */ | ||
119 | } msg_queue_t; | ||
120 | |||
121 | /*! @brief Type for a message queue handler */ | ||
122 | typedef msg_queue_t *msg_queue_handler_t; | ||
123 | |||
124 | /*! @brief State structure for bm osa manager. */ | ||
125 | typedef struct _osa_state | ||
126 | { | ||
127 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
128 | list_label_t taskList; | ||
129 | task_handler_t curTaskHandler; | ||
130 | #endif | ||
131 | volatile uint32_t interruptDisableCount; | ||
132 | volatile uint32_t tickCounter; | ||
133 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
134 | OSA_TASK_HANDLE_DEFINE(mainTaskHandle); | ||
135 | #endif | ||
136 | } osa_state_t; | ||
137 | |||
138 | /*! ********************************************************************************* | ||
139 | ************************************************************************************* | ||
140 | * Private prototypes | ||
141 | ************************************************************************************* | ||
142 | ********************************************************************************** */ | ||
143 | __WEAK_FUNC void main_task(osa_task_param_t arg); | ||
144 | __WEAK_FUNC void main_task(osa_task_param_t arg) | ||
145 | { | ||
146 | } | ||
147 | __WEAK_FUNC void OSA_TimeInit(void); | ||
148 | __WEAK_FUNC uint32_t OSA_TimeDiff(uint32_t time_start, uint32_t time_end); | ||
149 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
150 | osa_status_t OSA_Init(void); | ||
151 | #endif /* FSL_OSA_TASK_ENABLE */ | ||
152 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
153 | void OSA_Start(void); | ||
154 | #endif /* FSL_OSA_TASK_ENABLE */ | ||
155 | |||
156 | /*! ********************************************************************************* | ||
157 | ************************************************************************************* | ||
158 | * Public memory declarations | ||
159 | ************************************************************************************* | ||
160 | ********************************************************************************** */ | ||
161 | const uint8_t gUseRtos_c = USE_RTOS; /* USE_RTOS = 0 for BareMetal and 1 for OS */ | ||
162 | |||
163 | /*! ********************************************************************************* | ||
164 | ************************************************************************************* | ||
165 | * Private memory declarations | ||
166 | ************************************************************************************* | ||
167 | ********************************************************************************** */ | ||
168 | static osa_state_t s_osaState; | ||
169 | |||
170 | /*! ********************************************************************************* | ||
171 | ************************************************************************************* | ||
172 | * Public functions | ||
173 | ************************************************************************************* | ||
174 | ********************************************************************************** */ | ||
175 | /*FUNCTION********************************************************************** | ||
176 | * | ||
177 | * Function Name : OSA_MemoryAllocate | ||
178 | * Description : Reserves the requested amount of memory in bytes. | ||
179 | * | ||
180 | *END**************************************************************************/ | ||
181 | void *OSA_MemoryAllocate(uint32_t length) | ||
182 | { | ||
183 | void *p = (void *)malloc(length); | ||
184 | |||
185 | if (NULL != p) | ||
186 | { | ||
187 | (void)memset(p, 0, length); | ||
188 | } | ||
189 | |||
190 | return p; | ||
191 | } | ||
192 | |||
193 | /*FUNCTION********************************************************************** | ||
194 | * | ||
195 | * Function Name : OSA_MemoryFree | ||
196 | * Description : Frees the memory previously reserved. | ||
197 | * | ||
198 | *END**************************************************************************/ | ||
199 | void OSA_MemoryFree(void *p) | ||
200 | { | ||
201 | free(p); | ||
202 | } | ||
203 | |||
204 | void OSA_EnterCritical(uint32_t *sr) | ||
205 | { | ||
206 | *sr = DisableGlobalIRQ(); | ||
207 | } | ||
208 | |||
209 | void OSA_ExitCritical(uint32_t sr) | ||
210 | { | ||
211 | EnableGlobalIRQ(sr); | ||
212 | } | ||
213 | |||
214 | /*FUNCTION********************************************************************** | ||
215 | * | ||
216 | * Function Name : OSA_EnableIRQGlobal | ||
217 | * Description : Disable system interrupt. | ||
218 | * | ||
219 | *END**************************************************************************/ | ||
220 | void OSA_EnableIRQGlobal(void) | ||
221 | { | ||
222 | if (s_osaState.interruptDisableCount > 0U) | ||
223 | { | ||
224 | s_osaState.interruptDisableCount--; | ||
225 | |||
226 | if (0U == s_osaState.interruptDisableCount) | ||
227 | { | ||
228 | __enable_irq(); | ||
229 | } | ||
230 | /* call core API to enable the global interrupt*/ | ||
231 | } | ||
232 | } | ||
233 | |||
234 | /*FUNCTION********************************************************************** | ||
235 | * | ||
236 | * Function Name : OSA_DisableIRQGlobal | ||
237 | * Description : Disable system interrupt | ||
238 | * This function will disable the global interrupt by calling the core API | ||
239 | * | ||
240 | *END**************************************************************************/ | ||
241 | void OSA_DisableIRQGlobal(void) | ||
242 | { | ||
243 | /* call core API to disable the global interrupt*/ | ||
244 | __disable_irq(); | ||
245 | |||
246 | /* update counter*/ | ||
247 | s_osaState.interruptDisableCount++; | ||
248 | } | ||
249 | |||
250 | /*FUNCTION********************************************************************** | ||
251 | * | ||
252 | * Function Name : OSA_TaskGetCurrentHandle | ||
253 | * Description : This function is used to get current active task's handler. | ||
254 | * | ||
255 | *END**************************************************************************/ | ||
256 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
257 | osa_task_handle_t OSA_TaskGetCurrentHandle(void) | ||
258 | { | ||
259 | return (osa_task_handle_t)s_osaState.curTaskHandler; | ||
260 | } | ||
261 | #endif | ||
262 | /*FUNCTION********************************************************************** | ||
263 | * | ||
264 | * Function Name : OSA_EXT_TaskYield | ||
265 | * Description : When a task calls this function, it will give up CPU and put | ||
266 | * itself to the tail of ready list. | ||
267 | * | ||
268 | *END**************************************************************************/ | ||
269 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
270 | osa_status_t OSA_TaskYield(void) | ||
271 | { | ||
272 | return KOSA_StatusSuccess; | ||
273 | } | ||
274 | #endif | ||
275 | /*FUNCTION********************************************************************** | ||
276 | * | ||
277 | * Function Name : OSA_TaskGetPriority | ||
278 | * Description : This function returns task's priority by task handler. | ||
279 | * | ||
280 | *END**************************************************************************/ | ||
281 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
282 | osa_task_priority_t OSA_TaskGetPriority(osa_task_handle_t taskHandle) | ||
283 | { | ||
284 | assert(taskHandle); | ||
285 | task_handler_t handler = (task_handler_t)taskHandle; | ||
286 | return handler->priority; | ||
287 | } | ||
288 | #endif | ||
289 | |||
290 | /*FUNCTION********************************************************************** | ||
291 | * | ||
292 | * Function Name : OSA_TaskSetPriority | ||
293 | * Description : This function sets task's priority by task handler. | ||
294 | * | ||
295 | *END**************************************************************************/ | ||
296 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
297 | osa_status_t OSA_TaskSetPriority(osa_task_handle_t taskHandle, osa_task_priority_t taskPriority) | ||
298 | { | ||
299 | assert(taskHandle); | ||
300 | list_element_handle_t list_element; | ||
301 | task_control_block_t *tcb = NULL; | ||
302 | #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U)) | ||
303 | task_control_block_t *preTcb = NULL; | ||
304 | #endif | ||
305 | task_control_block_t *ptaskStruct = (task_control_block_t *)taskHandle; | ||
306 | uint32_t regPrimask; | ||
307 | |||
308 | ptaskStruct->priority = taskPriority; | ||
309 | (void)LIST_RemoveElement(&ptaskStruct->link); | ||
310 | /* Insert task control block into the task list. */ | ||
311 | list_element = LIST_GetHead(&s_osaState.taskList); | ||
312 | while (NULL != list_element) | ||
313 | { | ||
314 | tcb = (task_control_block_t *)(void *)list_element; | ||
315 | if (ptaskStruct->priority <= tcb->priority) | ||
316 | { | ||
317 | #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U)) | ||
318 | if (preTcb == NULL) | ||
319 | { | ||
320 | (&tcb->link)->list->head = (struct list_element_tag *)(void *)ptaskStruct; | ||
321 | } | ||
322 | else | ||
323 | { | ||
324 | (&preTcb->link)->next = (struct list_element_tag *)(void *)ptaskStruct; | ||
325 | } | ||
326 | (&ptaskStruct->link)->list = (&tcb->link)->list; | ||
327 | (&ptaskStruct->link)->next = (struct list_element_tag *)(void *)tcb; | ||
328 | (&ptaskStruct->link)->list->size++; | ||
329 | #else | ||
330 | (void)LIST_AddPrevElement(&tcb->link, &ptaskStruct->link); | ||
331 | #endif | ||
332 | break; | ||
333 | } | ||
334 | #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U)) | ||
335 | preTcb = tcb; | ||
336 | #endif | ||
337 | list_element = LIST_GetNext(list_element); | ||
338 | } | ||
339 | if (ptaskStruct->priority > tcb->priority) | ||
340 | { | ||
341 | OSA_EnterCritical(®Primask); | ||
342 | (void)LIST_AddTail(&s_osaState.taskList, (list_element_handle_t)(void *)&(ptaskStruct->link)); | ||
343 | OSA_ExitCritical(regPrimask); | ||
344 | } | ||
345 | |||
346 | return KOSA_StatusSuccess; | ||
347 | } | ||
348 | #endif | ||
349 | |||
350 | /*FUNCTION********************************************************************** | ||
351 | * | ||
352 | * Function Name : OSA_TaskCreate | ||
353 | * Description : This function is used to create a task and make it ready. | ||
354 | * Param[in] : threadDef - Definition of the thread. | ||
355 | * task_param - Parameter to pass to the new thread. | ||
356 | * Return Thread handle of the new thread, or NULL if failed. | ||
357 | * | ||
358 | *END**************************************************************************/ | ||
359 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
360 | osa_status_t OSA_TaskCreate(osa_task_handle_t taskHandle, const osa_task_def_t *thread_def, osa_task_param_t task_param) | ||
361 | { | ||
362 | list_element_handle_t list_element; | ||
363 | |||
364 | task_control_block_t *tcb = NULL; | ||
365 | #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U)) | ||
366 | task_control_block_t *preTcb = NULL; | ||
367 | #endif | ||
368 | list_status_t listStatus; | ||
369 | |||
370 | task_control_block_t *ptaskStruct = (task_control_block_t *)taskHandle; | ||
371 | uint32_t regPrimask; | ||
372 | assert(sizeof(task_control_block_t) == OSA_TASK_HANDLE_SIZE); | ||
373 | assert(taskHandle); | ||
374 | |||
375 | ptaskStruct->p_func = thread_def->pthread; | ||
376 | ptaskStruct->haveToRun = 1U; | ||
377 | ptaskStruct->priority = (uint16_t)PRIORITY_OSA_TO_RTOS(thread_def->tpriority); | ||
378 | ptaskStruct->param = task_param; | ||
379 | |||
380 | list_element = LIST_GetHead(&s_osaState.taskList); | ||
381 | while (NULL != list_element) | ||
382 | { | ||
383 | tcb = (task_control_block_t *)(void *)list_element; | ||
384 | if (ptaskStruct->priority <= tcb->priority) | ||
385 | { | ||
386 | OSA_EnterCritical(®Primask); | ||
387 | #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U)) | ||
388 | if (preTcb == NULL) | ||
389 | { | ||
390 | (&tcb->link)->list->head = (struct list_element_tag *)(void *)ptaskStruct; | ||
391 | } | ||
392 | else | ||
393 | { | ||
394 | (&preTcb->link)->next = (struct list_element_tag *)(void *)ptaskStruct; | ||
395 | } | ||
396 | (&ptaskStruct->link)->list = (&tcb->link)->list; | ||
397 | (&ptaskStruct->link)->next = (struct list_element_tag *)(void *)tcb; | ||
398 | (&ptaskStruct->link)->list->size++; | ||
399 | OSA_ExitCritical(regPrimask); | ||
400 | return KOSA_StatusSuccess; | ||
401 | #else | ||
402 | listStatus = LIST_AddPrevElement(&tcb->link, &ptaskStruct->link); | ||
403 | OSA_ExitCritical(regPrimask); | ||
404 | if (listStatus == (list_status_t)kLIST_DuplicateError) | ||
405 | { | ||
406 | return KOSA_StatusError; | ||
407 | } | ||
408 | break; | ||
409 | #endif | ||
410 | } | ||
411 | #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U)) | ||
412 | preTcb = tcb; | ||
413 | #endif | ||
414 | list_element = LIST_GetNext(list_element); | ||
415 | } | ||
416 | |||
417 | if ((NULL == tcb) || (ptaskStruct->priority > tcb->priority)) | ||
418 | { | ||
419 | OSA_EnterCritical(®Primask); | ||
420 | listStatus = LIST_AddTail(&s_osaState.taskList, (list_element_handle_t)(void *)&(ptaskStruct->link)); | ||
421 | (void)listStatus; | ||
422 | assert(listStatus == kLIST_Ok); | ||
423 | OSA_ExitCritical(regPrimask); | ||
424 | } | ||
425 | |||
426 | return KOSA_StatusSuccess; | ||
427 | } | ||
428 | #endif | ||
429 | |||
430 | /*FUNCTION********************************************************************** | ||
431 | * | ||
432 | * Function Name : OSA_TaskDestroy | ||
433 | * Description : This function destroy a task. | ||
434 | * Param[in] :taskHandle - Thread handle. | ||
435 | * Return KOSA_StatusSuccess if the task is destroied, otherwise return KOSA_StatusError. | ||
436 | * | ||
437 | *END**************************************************************************/ | ||
438 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
439 | osa_status_t OSA_TaskDestroy(osa_task_handle_t taskHandle) | ||
440 | { | ||
441 | uint32_t regPrimask; | ||
442 | assert(taskHandle); | ||
443 | |||
444 | OSA_EnterCritical(®Primask); | ||
445 | (void)LIST_RemoveElement(taskHandle); | ||
446 | OSA_ExitCritical(regPrimask); | ||
447 | return KOSA_StatusSuccess; | ||
448 | } | ||
449 | #endif | ||
450 | |||
451 | /*FUNCTION********************************************************************** | ||
452 | * | ||
453 | * Function Name : OSA_TimeInit | ||
454 | * Description : This function initializes the timer used in BM OSA, the | ||
455 | * functions such as OSA_TimeDelay, OSA_TimeGetMsec, and the timeout are all | ||
456 | * based on this timer. | ||
457 | * | ||
458 | *END**************************************************************************/ | ||
459 | __WEAK_FUNC void OSA_TimeInit(void) | ||
460 | { | ||
461 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
462 | SysTick->CTRL &= ~(SysTick_CTRL_ENABLE_Msk); | ||
463 | SysTick->LOAD = (uint32_t)(SystemCoreClock / 1000U - 1U); | ||
464 | SysTick->VAL = 0; | ||
465 | SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_CLKSOURCE_Msk; | ||
466 | #endif | ||
467 | } | ||
468 | |||
469 | /*FUNCTION********************************************************************** | ||
470 | * | ||
471 | * Function Name : OSA_TimeDiff | ||
472 | * Description : This function gets the difference between two time stamp, | ||
473 | * time overflow is considered. | ||
474 | * | ||
475 | *END**************************************************************************/ | ||
476 | __WEAK_FUNC uint32_t OSA_TimeDiff(uint32_t time_start, uint32_t time_end) | ||
477 | { | ||
478 | if (time_end >= time_start) | ||
479 | { | ||
480 | return time_end - time_start; | ||
481 | } | ||
482 | else | ||
483 | { | ||
484 | return FSL_OSA_TIME_RANGE - time_start + time_end + 1UL; | ||
485 | } | ||
486 | } | ||
487 | |||
488 | /*FUNCTION********************************************************************** | ||
489 | * | ||
490 | * Function Name : OSA__TimeDelay | ||
491 | * Description : This function is used to suspend the active thread for the given number of milliseconds. | ||
492 | * | ||
493 | *END**************************************************************************/ | ||
494 | void OSA_TimeDelay(uint32_t millisec) | ||
495 | { | ||
496 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
497 | uint32_t currTime, timeStart; | ||
498 | |||
499 | timeStart = OSA_TimeGetMsec(); | ||
500 | |||
501 | do | ||
502 | { | ||
503 | currTime = OSA_TimeGetMsec(); /* Get current time stamp */ | ||
504 | } while (millisec >= OSA_TimeDiff(timeStart, currTime)); | ||
505 | #endif | ||
506 | } | ||
507 | /*FUNCTION********************************************************************** | ||
508 | * | ||
509 | * Function Name : OSA_TimeGetMsec | ||
510 | * Description : This function gets current time in milliseconds. | ||
511 | * | ||
512 | *END**************************************************************************/ | ||
513 | __WEAK_FUNC uint32_t OSA_TimeGetMsec(void) | ||
514 | { | ||
515 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
516 | return s_osaState.tickCounter; | ||
517 | #else | ||
518 | return 0; | ||
519 | #endif | ||
520 | } | ||
521 | /*FUNCTION********************************************************************** | ||
522 | * | ||
523 | * Function Name : OSA_SemaphoreCreate | ||
524 | * Description : This function is used to create a semaphore. | ||
525 | * Return : Semaphore handle of the new semaphore, or NULL if failed. | ||
526 | * | ||
527 | *END**************************************************************************/ | ||
528 | |||
529 | osa_status_t OSA_SemaphoreCreate(osa_semaphore_handle_t semaphoreHandle, uint32_t initValue) | ||
530 | { | ||
531 | semaphore_t *pSemStruct = (semaphore_t *)semaphoreHandle; | ||
532 | assert(sizeof(semaphore_t) == OSA_SEM_HANDLE_SIZE); | ||
533 | assert(semaphoreHandle); | ||
534 | |||
535 | pSemStruct->semCount = (uint8_t)initValue; | ||
536 | pSemStruct->isWaiting = 0U; | ||
537 | pSemStruct->time_start = 0U; | ||
538 | pSemStruct->timeout = 0U; | ||
539 | return KOSA_StatusSuccess; | ||
540 | } | ||
541 | /*FUNCTION********************************************************************** | ||
542 | * | ||
543 | * Function Name : OSA_SemaphoreDestroy | ||
544 | * Description : This function is used to destroy a semaphore. | ||
545 | * Return : KOSA_StatusSuccess if the semaphore is destroyed successfully, otherwise return KOSA_StatusError. | ||
546 | * | ||
547 | *END**************************************************************************/ | ||
548 | osa_status_t OSA_SemaphoreDestroy(osa_semaphore_handle_t semaphoreHandle) | ||
549 | { | ||
550 | assert(semaphoreHandle); | ||
551 | semaphore_t *pSemStruct = (semaphore_t *)semaphoreHandle; | ||
552 | |||
553 | /* Destroy semaphoreHandle's data */ | ||
554 | (void)memset(pSemStruct, 0, sizeof(semaphore_t)); | ||
555 | |||
556 | return KOSA_StatusSuccess; | ||
557 | } | ||
558 | /*FUNCTION********************************************************************** | ||
559 | * | ||
560 | * Function Name : OSA_SemaphoreWait | ||
561 | * Description : This function checks the semaphore's counting value, if it is | ||
562 | * positive, decreases it and returns KOSA_StatusSuccess, otherwise, timeout | ||
563 | * will be used for wait. The parameter timeout indicates how long should wait | ||
564 | * in milliseconds. Pass osaWaitForever_c to wait indefinitely, pass 0 will | ||
565 | * return KOSA_StatusTimeout immediately if semaphore is not positive. | ||
566 | * This function returns KOSA_StatusSuccess if the semaphore is received, returns | ||
567 | * KOSA_StatusTimeout if the semaphore is not received within the specified | ||
568 | * 'timeout', returns KOSA_StatusError if any errors occur during waiting. | ||
569 | * | ||
570 | *END**************************************************************************/ | ||
571 | osa_status_t OSA_SemaphoreWait(osa_semaphore_handle_t semaphoreHandle, uint32_t millisec) | ||
572 | { | ||
573 | semaphore_t *pSemStruct = (semaphore_t *)semaphoreHandle; | ||
574 | uint32_t regPrimask; | ||
575 | assert(semaphoreHandle); | ||
576 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
577 | uint32_t currentTime; | ||
578 | #endif | ||
579 | /* Check the sem count first. Deal with timeout only if not already set */ | ||
580 | |||
581 | if (0U != pSemStruct->semCount) | ||
582 | { | ||
583 | OSA_EnterCritical(®Primask); | ||
584 | pSemStruct->semCount--; | ||
585 | pSemStruct->isWaiting = 0U; | ||
586 | OSA_ExitCritical(regPrimask); | ||
587 | return KOSA_StatusSuccess; | ||
588 | } | ||
589 | else | ||
590 | { | ||
591 | if (0U == millisec) | ||
592 | { | ||
593 | /* If timeout is 0 and semaphore is not available, return kStatus_OSA_Timeout. */ | ||
594 | return KOSA_StatusTimeout; | ||
595 | } | ||
596 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
597 | else if (0U != pSemStruct->isWaiting) | ||
598 | { | ||
599 | /* Check for timeout */ | ||
600 | currentTime = OSA_TimeGetMsec(); | ||
601 | if (pSemStruct->timeout < OSA_TimeDiff(pSemStruct->time_start, currentTime)) | ||
602 | { | ||
603 | OSA_EnterCritical(®Primask); | ||
604 | pSemStruct->isWaiting = 0U; | ||
605 | OSA_ExitCritical(regPrimask); | ||
606 | return KOSA_StatusTimeout; | ||
607 | } | ||
608 | } | ||
609 | else if (millisec != osaWaitForever_c) /* If don't wait forever, start the timer */ | ||
610 | { | ||
611 | /* Start the timeout counter */ | ||
612 | OSA_EnterCritical(®Primask); | ||
613 | pSemStruct->isWaiting = 1U; | ||
614 | OSA_ExitCritical(regPrimask); | ||
615 | pSemStruct->time_start = OSA_TimeGetMsec(); | ||
616 | pSemStruct->timeout = millisec; | ||
617 | } | ||
618 | #endif | ||
619 | else | ||
620 | { | ||
621 | ; | ||
622 | } | ||
623 | } | ||
624 | |||
625 | return KOSA_StatusIdle; | ||
626 | } | ||
627 | /*FUNCTION********************************************************************** | ||
628 | * | ||
629 | * Function Name : OSA_SemaphorePost | ||
630 | * Description : This function is used to wake up one task that wating on the | ||
631 | * semaphore. If no task is waiting, increase the semaphore. The function returns | ||
632 | * KOSA_StatusSuccess if the semaphre is post successfully, otherwise returns | ||
633 | * KOSA_StatusError. | ||
634 | * | ||
635 | *END**************************************************************************/ | ||
636 | osa_status_t OSA_SemaphorePost(osa_semaphore_handle_t semaphoreHandle) | ||
637 | { | ||
638 | semaphore_t *pSemStruct = (semaphore_t *)semaphoreHandle; | ||
639 | uint32_t regPrimask; | ||
640 | assert(semaphoreHandle); | ||
641 | |||
642 | /* The max value is 0xFF */ | ||
643 | if (0xFFU == pSemStruct->semCount) | ||
644 | { | ||
645 | return KOSA_StatusError; | ||
646 | } | ||
647 | OSA_EnterCritical(®Primask); | ||
648 | ++pSemStruct->semCount; | ||
649 | OSA_ExitCritical(regPrimask); | ||
650 | |||
651 | return KOSA_StatusSuccess; | ||
652 | } | ||
653 | |||
654 | /*FUNCTION********************************************************************** | ||
655 | * | ||
656 | * Function Name : OSA_MutexCreate | ||
657 | * Description : This function is used to create a mutex. | ||
658 | * Return : Mutex handle of the new mutex, or NULL if failed. | ||
659 | * | ||
660 | *END**************************************************************************/ | ||
661 | osa_status_t OSA_MutexCreate(osa_mutex_handle_t mutexHandle) | ||
662 | { | ||
663 | mutex_t *pMutexStruct = (mutex_t *)mutexHandle; | ||
664 | assert(sizeof(mutex_t) == OSA_MUTEX_HANDLE_SIZE); | ||
665 | assert(mutexHandle); | ||
666 | |||
667 | pMutexStruct->isLocked = 0U; | ||
668 | pMutexStruct->isWaiting = 0U; | ||
669 | pMutexStruct->time_start = 0u; | ||
670 | pMutexStruct->timeout = 0u; | ||
671 | return KOSA_StatusSuccess; | ||
672 | } | ||
673 | |||
674 | /*FUNCTION********************************************************************** | ||
675 | * | ||
676 | * Function Name : OSA_MutexLock | ||
677 | * Description : This function checks the mutex's status, if it is unlocked, | ||
678 | * lock it and returns KOSA_StatusSuccess, otherwise, wait for the mutex. | ||
679 | * MQX does not support timeout to wait for a mutex. | ||
680 | * This function returns KOSA_StatusSuccess if the mutex is obtained, returns | ||
681 | * KOSA_StatusError if any errors occur during waiting. If the mutex has been | ||
682 | * locked, pass 0 as timeout will return KOSA_StatusTimeout immediately. | ||
683 | * | ||
684 | *END**************************************************************************/ | ||
685 | osa_status_t OSA_MutexLock(osa_mutex_handle_t mutexHandle, uint32_t millisec) | ||
686 | { | ||
687 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
688 | uint32_t currentTime; | ||
689 | #endif | ||
690 | mutex_t *pMutexStruct = (mutex_t *)mutexHandle; | ||
691 | uint32_t regPrimask; | ||
692 | |||
693 | /* Always check first. Deal with timeout only if not available. */ | ||
694 | if (0U == pMutexStruct->isLocked) | ||
695 | { | ||
696 | /* Get the lock and return success */ | ||
697 | OSA_EnterCritical(®Primask); | ||
698 | pMutexStruct->isLocked = 1U; | ||
699 | pMutexStruct->isWaiting = 0U; | ||
700 | OSA_ExitCritical(regPrimask); | ||
701 | return KOSA_StatusSuccess; | ||
702 | } | ||
703 | else | ||
704 | { | ||
705 | if (0U == millisec) | ||
706 | { | ||
707 | /* If timeout is 0 and mutex is not available, return kStatus_OSA_Timeout. */ | ||
708 | return KOSA_StatusTimeout; | ||
709 | } | ||
710 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
711 | else if (pMutexStruct->isWaiting != 0U) | ||
712 | { | ||
713 | /* Check for timeout */ | ||
714 | currentTime = OSA_TimeGetMsec(); | ||
715 | if (pMutexStruct->timeout < OSA_TimeDiff(pMutexStruct->time_start, currentTime)) | ||
716 | { | ||
717 | OSA_EnterCritical(®Primask); | ||
718 | pMutexStruct->isWaiting = 0U; | ||
719 | OSA_ExitCritical(regPrimask); | ||
720 | return KOSA_StatusTimeout; | ||
721 | } | ||
722 | } | ||
723 | else if (millisec != osaWaitForever_c) /* If dont't wait forever, start timer. */ | ||
724 | { | ||
725 | /* Start the timeout counter */ | ||
726 | OSA_EnterCritical(®Primask); | ||
727 | pMutexStruct->isWaiting = 1U; | ||
728 | OSA_ExitCritical(regPrimask); | ||
729 | pMutexStruct->time_start = OSA_TimeGetMsec(); | ||
730 | pMutexStruct->timeout = millisec; | ||
731 | } | ||
732 | #endif | ||
733 | else | ||
734 | { | ||
735 | ; | ||
736 | } | ||
737 | } | ||
738 | |||
739 | return KOSA_StatusIdle; | ||
740 | } | ||
741 | /*FUNCTION********************************************************************** | ||
742 | * | ||
743 | * Function Name : OSA_MutexUnlock | ||
744 | * Description : This function is used to unlock a mutex. | ||
745 | * | ||
746 | *END**************************************************************************/ | ||
747 | osa_status_t OSA_MutexUnlock(osa_mutex_handle_t mutexHandle) | ||
748 | { | ||
749 | mutex_t *pMutexStruct = (mutex_t *)mutexHandle; | ||
750 | uint32_t regPrimask; | ||
751 | assert(mutexHandle); | ||
752 | |||
753 | OSA_EnterCritical(®Primask); | ||
754 | pMutexStruct->isLocked = 0U; | ||
755 | OSA_ExitCritical(regPrimask); | ||
756 | return KOSA_StatusSuccess; | ||
757 | } | ||
758 | /*FUNCTION********************************************************************** | ||
759 | * | ||
760 | * Function Name : OSA_MutexDestroy | ||
761 | * Description : This function is used to destroy a mutex. | ||
762 | * Return : KOSA_StatusSuccess if the lock object is destroyed successfully, otherwise return KOSA_StatusError. | ||
763 | * | ||
764 | *END**************************************************************************/ | ||
765 | osa_status_t OSA_MutexDestroy(osa_mutex_handle_t mutexHandle) | ||
766 | { | ||
767 | assert(mutexHandle); | ||
768 | mutex_t *pMutexStruct = (mutex_t *)mutexHandle; | ||
769 | |||
770 | /* Destory mutexHandle's data */ | ||
771 | (void)memset(pMutexStruct, 0, sizeof(mutex_t)); | ||
772 | |||
773 | return KOSA_StatusSuccess; | ||
774 | } | ||
775 | /*FUNCTION********************************************************************** | ||
776 | * | ||
777 | * Function Name : OSA_EventCreate | ||
778 | * Description : This function is used to create a event object. | ||
779 | * Return : Event handle of the new event, or NULL if failed. | ||
780 | * | ||
781 | *END**************************************************************************/ | ||
782 | osa_status_t OSA_EventCreate(osa_event_handle_t eventHandle, uint8_t autoClear) | ||
783 | { | ||
784 | event_t *pEventStruct = eventHandle; | ||
785 | assert(sizeof(event_t) == OSA_EVENT_HANDLE_SIZE); | ||
786 | assert(eventHandle); | ||
787 | |||
788 | pEventStruct->isWaiting = 0U; | ||
789 | pEventStruct->flags = 0; | ||
790 | pEventStruct->autoClear = autoClear; | ||
791 | pEventStruct->time_start = 0u; | ||
792 | pEventStruct->timeout = 0u; | ||
793 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
794 | pEventStruct->waitingTask = NULL; | ||
795 | #endif | ||
796 | return KOSA_StatusSuccess; | ||
797 | } | ||
798 | /*FUNCTION********************************************************************** | ||
799 | * | ||
800 | * Function Name : OSA_EventSet | ||
801 | * Description : Set one or more event flags of an event object. | ||
802 | * Return : KOSA_StatusSuccess if set successfully, KOSA_StatusError if failed. | ||
803 | * | ||
804 | *END**************************************************************************/ | ||
805 | osa_status_t OSA_EventSet(osa_event_handle_t eventHandle, osa_event_flags_t flagsToSet) | ||
806 | { | ||
807 | event_t *pEventStruct; | ||
808 | uint32_t regPrimask; | ||
809 | pEventStruct = (event_t *)eventHandle; | ||
810 | /* Set flags ensuring atomic operation */ | ||
811 | OSA_EnterCritical(®Primask); | ||
812 | pEventStruct->flags |= flagsToSet; | ||
813 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
814 | if (pEventStruct->waitingTask != NULL) | ||
815 | { | ||
816 | pEventStruct->waitingTask->haveToRun = 1U; | ||
817 | } | ||
818 | #endif | ||
819 | OSA_ExitCritical(regPrimask); | ||
820 | |||
821 | return KOSA_StatusSuccess; | ||
822 | } | ||
823 | /*FUNCTION********************************************************************** | ||
824 | * | ||
825 | * Function Name : OSA_EventClear | ||
826 | * Description : Clear one or more event flags of an event object. | ||
827 | * Return :KOSA_StatusSuccess if clear successfully, KOSA_StatusError if failed. | ||
828 | * | ||
829 | *END**************************************************************************/ | ||
830 | osa_status_t OSA_EventClear(osa_event_handle_t eventHandle, osa_event_flags_t flagsToClear) | ||
831 | { | ||
832 | event_t *pEventStruct; | ||
833 | uint32_t regPrimask; | ||
834 | pEventStruct = (event_t *)eventHandle; | ||
835 | /* Clear flags ensuring atomic operation */ | ||
836 | OSA_EnterCritical(®Primask); | ||
837 | pEventStruct->flags &= ~flagsToClear; | ||
838 | if (0U != pEventStruct->flags) | ||
839 | { | ||
840 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
841 | if (NULL != pEventStruct->waitingTask) | ||
842 | { | ||
843 | pEventStruct->waitingTask->haveToRun = 1U; | ||
844 | } | ||
845 | #endif | ||
846 | } | ||
847 | OSA_ExitCritical(regPrimask); | ||
848 | |||
849 | return KOSA_StatusSuccess; | ||
850 | } | ||
851 | /*FUNCTION********************************************************************** | ||
852 | * | ||
853 | * Function Name : OSA_EventGet | ||
854 | * Description : This function is used to get event's flags that specified by prameter | ||
855 | * flagsMask, and the flags (user specified) are obatianed by parameter pFlagsOfEvent. So | ||
856 | * you should pass the parameter 0xffffffff to specify you want to get all. | ||
857 | * Return :KOSA_StatusSuccess if event flags were successfully got, KOSA_StatusError if failed. | ||
858 | * | ||
859 | *END**************************************************************************/ | ||
860 | osa_status_t OSA_EventGet(osa_event_handle_t eventHandle, osa_event_flags_t flagsMask, osa_event_flags_t *pFlagsOfEvent) | ||
861 | { | ||
862 | event_t *pEventStruct; | ||
863 | pEventStruct = (event_t *)eventHandle; | ||
864 | OSA_SR_ALLOC(); | ||
865 | |||
866 | if (NULL == pFlagsOfEvent) | ||
867 | { | ||
868 | return KOSA_StatusError; | ||
869 | } | ||
870 | |||
871 | OSA_ENTER_CRITICAL(); | ||
872 | *pFlagsOfEvent = pEventStruct->flags & flagsMask; | ||
873 | OSA_EXIT_CRITICAL(); | ||
874 | |||
875 | return KOSA_StatusSuccess; | ||
876 | } | ||
877 | /*FUNCTION********************************************************************** | ||
878 | * | ||
879 | * Function Name : OSA_EventWait | ||
880 | * Description : This function checks the event's status, if it meets the wait | ||
881 | * condition, return KOSA_StatusSuccess, otherwise, timeout will be used for | ||
882 | * wait. The parameter timeout indicates how long should wait in milliseconds. | ||
883 | * Pass osaWaitForever_c to wait indefinitely, pass 0 will return the value | ||
884 | * KOSA_StatusTimeout immediately if wait condition is not met. The event flags | ||
885 | * will be cleared if the event is auto clear mode. Flags that wakeup waiting | ||
886 | * task could be obtained from the parameter setFlags. | ||
887 | * This function returns KOSA_StatusSuccess if wait condition is met, returns | ||
888 | * KOSA_StatusTimeout if wait condition is not met within the specified | ||
889 | * 'timeout', returns KOSA_StatusError if any errors occur during waiting. | ||
890 | * | ||
891 | *END**************************************************************************/ | ||
892 | osa_status_t OSA_EventWait(osa_event_handle_t eventHandle, | ||
893 | osa_event_flags_t flagsToWait, | ||
894 | uint8_t waitAll, | ||
895 | uint32_t millisec, | ||
896 | osa_event_flags_t *pSetFlags) | ||
897 | { | ||
898 | event_t *pEventStruct; | ||
899 | uint32_t regPrimask; | ||
900 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
901 | uint32_t currentTime; | ||
902 | #endif | ||
903 | osa_status_t retVal = KOSA_StatusIdle; | ||
904 | if (NULL == pSetFlags) | ||
905 | { | ||
906 | return KOSA_StatusError; | ||
907 | } | ||
908 | |||
909 | pEventStruct = (event_t *)eventHandle; | ||
910 | |||
911 | OSA_EnterCritical(®Primask); | ||
912 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
913 | #if (TASK_MAX_NUM > 0) | ||
914 | pEventStruct->waitingTask = OSA_TaskGetCurrentHandle(); | ||
915 | #endif | ||
916 | #endif | ||
917 | |||
918 | *pSetFlags = pEventStruct->flags & flagsToWait; | ||
919 | |||
920 | /* Check the event flag first, if does not meet wait condition, deal with timeout. */ | ||
921 | if (((0U == waitAll) && (0U != *pSetFlags)) || (*pSetFlags == flagsToWait)) | ||
922 | { | ||
923 | pEventStruct->isWaiting = 0U; | ||
924 | if (1U == pEventStruct->autoClear) | ||
925 | { | ||
926 | pEventStruct->flags &= ~flagsToWait; | ||
927 | } | ||
928 | retVal = KOSA_StatusSuccess; | ||
929 | } | ||
930 | else | ||
931 | { | ||
932 | if (0U == millisec) | ||
933 | { | ||
934 | /* If timeout is 0 and wait condition is not met, return kStatus_OSA_Timeout. */ | ||
935 | retVal = KOSA_StatusTimeout; | ||
936 | } | ||
937 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
938 | else if (0U != pEventStruct->isWaiting) | ||
939 | { | ||
940 | /* Check for timeout */ | ||
941 | currentTime = OSA_TimeGetMsec(); | ||
942 | if (pEventStruct->timeout < OSA_TimeDiff(pEventStruct->time_start, currentTime)) | ||
943 | { | ||
944 | pEventStruct->isWaiting = 0U; | ||
945 | retVal = KOSA_StatusTimeout; | ||
946 | } | ||
947 | } | ||
948 | else if (millisec != osaWaitForever_c) /* If no timeout, don't start the timer */ | ||
949 | { | ||
950 | /* Start the timeout counter */ | ||
951 | pEventStruct->isWaiting = 1U; | ||
952 | pEventStruct->time_start = OSA_TimeGetMsec(); | ||
953 | pEventStruct->timeout = millisec; | ||
954 | } | ||
955 | #endif | ||
956 | else | ||
957 | { | ||
958 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
959 | pEventStruct->waitingTask->haveToRun = 0U; | ||
960 | #endif | ||
961 | } | ||
962 | } | ||
963 | |||
964 | OSA_ExitCritical(regPrimask); | ||
965 | |||
966 | return retVal; | ||
967 | } | ||
968 | /*FUNCTION********************************************************************** | ||
969 | * | ||
970 | * Function Name : OSA_EventDestroy | ||
971 | * Description : This function is used to destroy a event object. Return | ||
972 | * KOSA_StatusSuccess if the event object is destroyed successfully, otherwise | ||
973 | * return KOSA_StatusError. | ||
974 | * | ||
975 | *END**************************************************************************/ | ||
976 | osa_status_t OSA_EventDestroy(osa_event_handle_t eventHandle) | ||
977 | { | ||
978 | assert(eventHandle); | ||
979 | event_t *pEventStruct = (event_t *)eventHandle; | ||
980 | |||
981 | /* Destroy eventHandle's data */ | ||
982 | (void)memset(pEventStruct, 0, sizeof(event_t)); | ||
983 | |||
984 | return KOSA_StatusSuccess; | ||
985 | } | ||
986 | |||
987 | /*FUNCTION********************************************************************** | ||
988 | * | ||
989 | * Function Name : OSA_MsgQCreate | ||
990 | * Description : This function is used to create a message queue. | ||
991 | * Return : the handle to the message queue if create successfully, otherwise | ||
992 | * return NULL. | ||
993 | * | ||
994 | *END**************************************************************************/ | ||
995 | osa_status_t OSA_MsgQCreate(osa_msgq_handle_t msgqHandle, uint32_t msgNo, uint32_t msgSize) | ||
996 | { | ||
997 | msg_queue_t *pMsgQStruct = msgqHandle; | ||
998 | assert(sizeof(msg_queue_t) == OSA_MSGQ_HANDLE_SIZE); | ||
999 | assert(msgqHandle); | ||
1000 | |||
1001 | pMsgQStruct->max = (uint16_t)msgNo; | ||
1002 | pMsgQStruct->number = 0; | ||
1003 | pMsgQStruct->head = 0; | ||
1004 | pMsgQStruct->tail = 0; | ||
1005 | pMsgQStruct->size = msgSize; | ||
1006 | pMsgQStruct->queueMem = (uint8_t *)((uint8_t *)msgqHandle + sizeof(msg_queue_t)); | ||
1007 | return KOSA_StatusSuccess; | ||
1008 | } | ||
1009 | |||
1010 | /*FUNCTION********************************************************************** | ||
1011 | * | ||
1012 | * Function Name : OSA_MsgQPut | ||
1013 | * Description : This function is used to put a message to a message queue. | ||
1014 | * Return : KOSA_StatusSuccess if the message is put successfully, otherwise return KOSA_StatusError. | ||
1015 | * | ||
1016 | *END**************************************************************************/ | ||
1017 | osa_status_t OSA_MsgQPut(osa_msgq_handle_t msgqHandle, osa_msg_handle_t pMessage) | ||
1018 | { | ||
1019 | assert(msgqHandle); | ||
1020 | msg_queue_t *pQueue; | ||
1021 | osa_status_t status = KOSA_StatusSuccess; | ||
1022 | uint32_t regPrimask; | ||
1023 | |||
1024 | uint8_t *pMsgArray; | ||
1025 | |||
1026 | pQueue = (msg_queue_t *)msgqHandle; | ||
1027 | |||
1028 | if (NULL == pQueue->queueMem) | ||
1029 | { | ||
1030 | return KOSA_StatusError; | ||
1031 | } | ||
1032 | |||
1033 | OSA_EnterCritical(®Primask); | ||
1034 | if (pQueue->number >= pQueue->max) | ||
1035 | { | ||
1036 | status = KOSA_StatusError; | ||
1037 | } | ||
1038 | else | ||
1039 | { | ||
1040 | pMsgArray = &pQueue->queueMem[pQueue->tail]; | ||
1041 | for (uint32_t i = 0; i < pQueue->size; i++) | ||
1042 | { | ||
1043 | pMsgArray[i] = *((uint8_t *)pMessage + i); | ||
1044 | } | ||
1045 | |||
1046 | pQueue->number++; | ||
1047 | pQueue->tail += (uint16_t)pQueue->size; | ||
1048 | |||
1049 | if (pQueue->tail >= (pQueue->max * pQueue->size)) | ||
1050 | { | ||
1051 | pQueue->tail = 0; | ||
1052 | } | ||
1053 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
1054 | if (NULL != pQueue->waitingTask) | ||
1055 | { | ||
1056 | pQueue->waitingTask->haveToRun = 1U; | ||
1057 | } | ||
1058 | #endif | ||
1059 | } | ||
1060 | OSA_ExitCritical(regPrimask); | ||
1061 | return status; | ||
1062 | } | ||
1063 | /*FUNCTION********************************************************************** | ||
1064 | * | ||
1065 | * Function Name : OSA_MsgQGet | ||
1066 | * Description : This function checks the queue's status, if it is not empty, | ||
1067 | * get message from it and return KOSA_StatusSuccess, otherwise, timeout will | ||
1068 | * be used for wait. The parameter timeout indicates how long should wait in | ||
1069 | * milliseconds. Pass osaWaitForever_c to wait indefinitely, pass 0 will return | ||
1070 | * KOSA_StatusTimeout immediately if queue is empty. | ||
1071 | * This function returns KOSA_StatusSuccess if message is got successfully, | ||
1072 | * returns KOSA_StatusTimeout if message queue is empty within the specified | ||
1073 | * 'timeout', returns KOSA_StatusError if any errors occur during waiting. | ||
1074 | * | ||
1075 | *END**************************************************************************/ | ||
1076 | osa_status_t OSA_MsgQGet(osa_msgq_handle_t msgqHandle, osa_msg_handle_t pMessage, uint32_t millisec) | ||
1077 | { | ||
1078 | assert(msgqHandle); | ||
1079 | msg_queue_t *pQueue; | ||
1080 | osa_status_t status = KOSA_StatusSuccess; | ||
1081 | uint32_t regPrimask; | ||
1082 | |||
1083 | uint8_t *pMsgArray; | ||
1084 | |||
1085 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
1086 | uint32_t currentTime; | ||
1087 | #endif | ||
1088 | |||
1089 | pQueue = (msg_queue_t *)msgqHandle; | ||
1090 | |||
1091 | if (NULL == pQueue->queueMem) | ||
1092 | { | ||
1093 | return KOSA_StatusError; | ||
1094 | } | ||
1095 | |||
1096 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
1097 | pQueue->waitingTask = OSA_TaskGetCurrentHandle(); | ||
1098 | #endif | ||
1099 | |||
1100 | OSA_EnterCritical(®Primask); | ||
1101 | if (0U != pQueue->number) | ||
1102 | { | ||
1103 | pMsgArray = (uint8_t *)pMessage; | ||
1104 | for (uint32_t i = 0; i < pQueue->size; i++) | ||
1105 | { | ||
1106 | pMsgArray[i] = pQueue->queueMem[pQueue->head + i]; | ||
1107 | } | ||
1108 | |||
1109 | pQueue->number--; | ||
1110 | pQueue->head += (uint16_t)pQueue->size; | ||
1111 | pQueue->isWaiting = 0U; | ||
1112 | |||
1113 | if (pQueue->head >= (pQueue->max * pQueue->size)) | ||
1114 | { | ||
1115 | pQueue->head = 0; | ||
1116 | } | ||
1117 | status = KOSA_StatusSuccess; | ||
1118 | } | ||
1119 | else | ||
1120 | { | ||
1121 | if (0U == millisec) | ||
1122 | { | ||
1123 | /* If timeout is 0 and wait condition is not met, return kStatus_OSA_Timeout. */ | ||
1124 | status = KOSA_StatusTimeout; | ||
1125 | } | ||
1126 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
1127 | else if (0U != pQueue->isWaiting) | ||
1128 | { | ||
1129 | /* Check for timeout */ | ||
1130 | status = KOSA_StatusIdle; /* Before a timeout, the status should be idle. */ | ||
1131 | currentTime = OSA_TimeGetMsec(); | ||
1132 | if (pQueue->timeout < OSA_TimeDiff(pQueue->time_start, currentTime)) | ||
1133 | { | ||
1134 | pQueue->isWaiting = 0U; | ||
1135 | status = KOSA_StatusTimeout; | ||
1136 | } | ||
1137 | } | ||
1138 | else if (millisec != osaWaitForever_c) /* If no timeout, don't start the timer */ | ||
1139 | { | ||
1140 | /* Start the timeout counter */ | ||
1141 | pQueue->isWaiting = 1U; | ||
1142 | pQueue->time_start = OSA_TimeGetMsec(); | ||
1143 | pQueue->timeout = millisec; | ||
1144 | status = KOSA_StatusIdle; | ||
1145 | } | ||
1146 | #endif | ||
1147 | else | ||
1148 | { | ||
1149 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
1150 | pQueue->waitingTask->haveToRun = 0U; | ||
1151 | #endif | ||
1152 | status = KOSA_StatusIdle; | ||
1153 | } | ||
1154 | } | ||
1155 | OSA_ExitCritical(regPrimask); | ||
1156 | |||
1157 | return status; | ||
1158 | } | ||
1159 | |||
1160 | /*FUNCTION********************************************************************** | ||
1161 | * | ||
1162 | * Function Name : OSA_MsgQAvailableMsgs | ||
1163 | * Description : This function is used to get the available message. | ||
1164 | * Return : Available message count | ||
1165 | * | ||
1166 | *END**************************************************************************/ | ||
1167 | int OSA_MsgQAvailableMsgs(osa_msgq_handle_t msgqHandle) | ||
1168 | { | ||
1169 | assert(msgqHandle); | ||
1170 | msg_queue_t *pQueue = (msg_queue_t *)msgqHandle; | ||
1171 | |||
1172 | return (int)pQueue->number; | ||
1173 | } | ||
1174 | |||
1175 | /*FUNCTION********************************************************************** | ||
1176 | * | ||
1177 | * Function Name : OSA_EXT_MsgQDestroy | ||
1178 | * Description : This function is used to destroy the message queue. | ||
1179 | * Return : KOSA_StatusSuccess if the message queue is destroyed successfully, otherwise return KOSA_StatusError. | ||
1180 | * | ||
1181 | *END**************************************************************************/ | ||
1182 | osa_status_t OSA_MsgQDestroy(osa_msgq_handle_t msgqHandle) | ||
1183 | { | ||
1184 | assert(msgqHandle); | ||
1185 | msg_queue_t *pQueue = (msg_queue_t *)msgqHandle; | ||
1186 | |||
1187 | /* Destory msgqHandle's data */ | ||
1188 | /* OSA_MsgQGet() & OSA_MsgQPut() will check queueMem, if NULL will return an error. */ | ||
1189 | (void)memset(pQueue, 0, sizeof(msg_queue_t)); | ||
1190 | |||
1191 | return KOSA_StatusSuccess; | ||
1192 | } | ||
1193 | |||
1194 | /*FUNCTION********************************************************************** | ||
1195 | * | ||
1196 | * Function Name : OSA_InterruptEnable | ||
1197 | * Description : self explanatory. | ||
1198 | * | ||
1199 | *END**************************************************************************/ | ||
1200 | void OSA_InterruptEnable(void) | ||
1201 | { | ||
1202 | OSA_EnableIRQGlobal(); | ||
1203 | } | ||
1204 | /*FUNCTION********************************************************************** | ||
1205 | * | ||
1206 | * Function Name : OSA_InterruptDisable | ||
1207 | * Description : self explanatory. | ||
1208 | * | ||
1209 | *END**************************************************************************/ | ||
1210 | void OSA_InterruptDisable(void) | ||
1211 | { | ||
1212 | OSA_DisableIRQGlobal(); | ||
1213 | } | ||
1214 | |||
1215 | /*FUNCTION********************************************************************** | ||
1216 | * | ||
1217 | * Function Name : OSA_InstallIntHandler | ||
1218 | * Description : This function is used to install interrupt handler. | ||
1219 | * | ||
1220 | *END**************************************************************************/ | ||
1221 | void OSA_InstallIntHandler(uint32_t IRQNumber, void (*handler)(void)) | ||
1222 | { | ||
1223 | #if defined(__IAR_SYSTEMS_ICC__) | ||
1224 | _Pragma("diag_suppress = Pm138") | ||
1225 | #endif | ||
1226 | #if defined(ENABLE_RAM_VECTOR_TABLE) | ||
1227 | (void) InstallIRQHandler((IRQn_Type)IRQNumber, (uint32_t) * (uint32_t *)&handler); | ||
1228 | #endif /* ENABLE_RAM_VECTOR_TABLE. */ | ||
1229 | #if defined(__IAR_SYSTEMS_ICC__) | ||
1230 | _Pragma("diag_remark = PM138") | ||
1231 | #endif | ||
1232 | } | ||
1233 | |||
1234 | /*! ********************************************************************************* | ||
1235 | ************************************************************************************* | ||
1236 | * Private functions | ||
1237 | ************************************************************************************* | ||
1238 | ********************************************************************************** */ | ||
1239 | #if ((defined(FSL_OSA_TASK_ENABLE)) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
1240 | |||
1241 | static OSA_TASK_DEFINE(main_task, gMainThreadPriority_c, 1, gMainThreadStackSize_c, 0); | ||
1242 | |||
1243 | int main(void) | ||
1244 | { | ||
1245 | extern void BOARD_InitHardware(void); | ||
1246 | (void)OSA_Init(); | ||
1247 | /* Initialize MCU clock */ | ||
1248 | BOARD_InitHardware(); | ||
1249 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
1250 | OSA_TimeInit(); | ||
1251 | #endif | ||
1252 | (void)OSA_TaskCreate((osa_task_handle_t)s_osaState.mainTaskHandle, OSA_TASK(main_task), NULL); | ||
1253 | OSA_Start(); | ||
1254 | |||
1255 | return 0; | ||
1256 | } | ||
1257 | #endif /* FSL_OSA_TASK_ENABLE */ | ||
1258 | |||
1259 | /*FUNCTION********************************************************************** | ||
1260 | * | ||
1261 | * Function Name : OSA_Init | ||
1262 | * Description : This function is used to setup the basic services, it should | ||
1263 | * be called first in function main. Return kStatus_OSA_Success if services | ||
1264 | * are initialized successfully, otherwise return kStatus_OSA_Error. | ||
1265 | * | ||
1266 | *END**************************************************************************/ | ||
1267 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
1268 | osa_status_t OSA_Init(void) | ||
1269 | { | ||
1270 | LIST_Init((&s_osaState.taskList), 0); | ||
1271 | return KOSA_StatusSuccess; | ||
1272 | } | ||
1273 | #endif | ||
1274 | |||
1275 | /*FUNCTION********************************************************************** | ||
1276 | * | ||
1277 | * Function Name : OSA_Start | ||
1278 | * Description : This function is used to start RTOS scheduler. | ||
1279 | * | ||
1280 | *END**************************************************************************/ | ||
1281 | #if (defined(FSL_OSA_TASK_ENABLE) && (FSL_OSA_TASK_ENABLE > 0U)) | ||
1282 | void OSA_Start(void) | ||
1283 | { | ||
1284 | list_element_handle_t list_element; | ||
1285 | task_control_block_t *tcb; | ||
1286 | |||
1287 | for (;;) | ||
1288 | { | ||
1289 | list_element = LIST_GetHead(&s_osaState.taskList); | ||
1290 | while (NULL != list_element) | ||
1291 | { | ||
1292 | tcb = (task_control_block_t *)(void *)list_element; | ||
1293 | s_osaState.curTaskHandler = (osa_task_handle_t)tcb; | ||
1294 | if (0U != tcb->haveToRun) | ||
1295 | { | ||
1296 | if (NULL != tcb->p_func) | ||
1297 | { | ||
1298 | tcb->p_func(tcb->param); | ||
1299 | } | ||
1300 | list_element = LIST_GetHead(&s_osaState.taskList); | ||
1301 | } | ||
1302 | else | ||
1303 | { | ||
1304 | list_element = LIST_GetNext(list_element); | ||
1305 | } | ||
1306 | } | ||
1307 | } | ||
1308 | } | ||
1309 | #endif | ||
1310 | |||
1311 | /*FUNCTION********************************************************************** | ||
1312 | * | ||
1313 | * Function Name : SysTick_Handler | ||
1314 | * Description : This ISR of the SYSTICK timer. | ||
1315 | * | ||
1316 | *END**************************************************************************/ | ||
1317 | #if (FSL_OSA_BM_TIMER_CONFIG != FSL_OSA_BM_TIMER_NONE) | ||
1318 | void SysTick_Handler(void); | ||
1319 | void SysTick_Handler(void) | ||
1320 | { | ||
1321 | s_osaState.tickCounter++; | ||
1322 | } | ||
1323 | #endif | ||