1 /*
2  * Copyright 2018-2019, 2023 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_common.h"
9 
10 /*
11  * The OSA_USED macro can only be defined when the OSA component is used.
12  * If the source code of the OSA component does not exist, the OSA_USED cannot be defined.
13  * OR, If OSA component is not added into project event the OSA source code exists, the OSA_USED
14  * also cannot be defined.
15  * The source code path of the OSA component is <MCUXpresso_SDK>/components/osa.
16  *
17  */
18 #if defined(OSA_USED)
19 #if (defined(COMMON_TASK_ENABLE) && (COMMON_TASK_ENABLE > 0U))
20 
21 #include "fsl_os_abstraction.h"
22 
23 #include "fsl_component_common_task.h"
24 
25 /*******************************************************************************
26  * Definitions
27  ******************************************************************************/
28 typedef struct _common_task_state
29 {
30     OSA_TASK_HANDLE_DEFINE(commonTaskHandle);
31     OSA_MSGQ_HANDLE_DEFINE(msgqhandle, COMMON_TASK_MAX_MSGQ_COUNT, sizeof(void *));
32     uint8_t isInitialized;
33 } common_task_state_t;
34 
35 /*******************************************************************************
36  * Prototypes
37  ******************************************************************************/
38 
39 void COMMON_TASK_task(osa_task_param_t param);
40 
41 /*******************************************************************************
42  * Variables
43  ******************************************************************************/
44 
45 extern const uint8_t gUseRtos_c;
46 static common_task_state_t s_commonTaskState[1];
47 OSA_TASK_DEFINE(COMMON_TASK_task, COMMON_TASK_PRIORITY, 1, COMMON_TASK_STACK_SIZE, false);
48 
49 /*******************************************************************************
50  * Code
51  ******************************************************************************/
52 
COMMON_TASK_task(osa_task_param_t param)53 void COMMON_TASK_task(osa_task_param_t param)
54 {
55     common_task_state_t *commonTaskStateHandle = (common_task_state_t *)param;
56     common_task_message_t *msg;
57     do
58     {
59         if (KOSA_StatusSuccess ==
60             OSA_MsgQGet((osa_msgq_handle_t)commonTaskStateHandle->msgqhandle, &msg, osaWaitForever_c))
61         {
62             if (msg->callback)
63             {
64                 msg->callback(msg->callbackParam);
65             }
66         }
67     } while (gUseRtos_c);
68 }
69 
COMMON_TASK_init(void)70 common_task_status_t COMMON_TASK_init(void)
71 {
72     osa_status_t status;
73 
74     if (s_commonTaskState->isInitialized)
75     {
76         return kStatus_COMMON_TASK_Error;
77     }
78     s_commonTaskState->isInitialized = 1U;
79 
80     status =
81         OSA_MsgQCreate((osa_msgq_handle_t)s_commonTaskState->msgqhandle, COMMON_TASK_MAX_MSGQ_COUNT, sizeof(void *));
82     assert(KOSA_StatusSuccess == status);
83 
84     status = OSA_TaskCreate((osa_task_handle_t)s_commonTaskState->commonTaskHandle, OSA_TASK(COMMON_TASK_task),
85                             s_commonTaskState);
86     assert(KOSA_StatusSuccess == status);
87     (void)status;
88 
89     return kStatus_COMMON_TASK_Success;
90 }
91 
COMMON_TASK_deinit(void)92 common_task_status_t COMMON_TASK_deinit(void)
93 {
94     if (!s_commonTaskState->isInitialized)
95     {
96         return kStatus_COMMON_TASK_Error;
97     }
98 
99     OSA_MsgQDestroy((osa_msgq_handle_t)s_commonTaskState->msgqhandle);
100     OSA_TaskDestroy((osa_task_handle_t)s_commonTaskState->commonTaskHandle);
101     s_commonTaskState->isInitialized = 0U;
102 
103     return kStatus_COMMON_TASK_Success;
104 }
105 
COMMON_TASK_post_message(common_task_message_t * msg)106 common_task_status_t COMMON_TASK_post_message(common_task_message_t *msg)
107 {
108     assert(msg);
109     assert(msg->callback);
110     assert(s_commonTaskState->isInitialized);
111 
112     if (KOSA_StatusSuccess != OSA_MsgQPut((osa_msgq_handle_t)s_commonTaskState->msgqhandle, &msg))
113     {
114         return kStatus_COMMON_TASK_Error;
115     }
116     return kStatus_COMMON_TASK_Success;
117 }
118 
COMMON_TASK_get_instance(void)119 void *COMMON_TASK_get_instance(void)
120 {
121     return (void *)&s_commonTaskState[0];
122 }
123 
COMMON_TASK_get_pending_message_count(void)124 int COMMON_TASK_get_pending_message_count(void)
125 {
126     if (!s_commonTaskState->isInitialized)
127     {
128         return -1;
129     }
130 
131     return OSA_MsgQAvailableMsgs((osa_msgq_handle_t)s_commonTaskState->msgqhandle);
132 }
133 #endif
134 #endif
135