1 /*
2 * SPDX-FileCopyrightText: 2016 STMicroelectronics
3 * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd
4 *
5 * SPDX-License-Identifier: BSD-3-Clause AND Apache-2.0
6 */
7
8 #include "bf0_hal.h"
9
10 /** @addtogroup BF0_HAL_Driver
11 * @{
12 */
13
14 /** @addtogroup MAILBOX
15 * @{
16 */
17
18 #if defined(HAL_MAILBOX_MODULE_ENABLED)||defined(_SIFLI_DOXYGEN_)
19
20 /* Private typedef -----------------------------------------------------------*/
21 /* Private define ------------------------------------------------------------*/
22 /** @defgroup MAILBOX_Private_Constants MAILBOX Private Constants
23 * @{
24 */
25
26 /**
27 * @}
28 */
29
30 /* Private macros ------------------------------------------------------------*/
31 /* Private variables ---------------------------------------------------------*/
32 /* Private function prototypes -----------------------------------------------*/
33 /** @defgroup MAILBOX_Private_Functions MAILBOX Private Functions
34 * @{
35 */
36
37 /**
38 * @}
39 */
40
41 /** @addtogroup MAILBOX_Exported_Functions
42 * @{
43 */
44
45 /** @addtogroup MAILBOX_Exported_Functions_Group1
46 * @brief Initialization and de-initialization functions
47 *
48 @verbatim
49 ===============================================================================
50 ##### Initialization and de-initialization functions #####
51 ===============================================================================
52 [..] This subsection provides a set of functions allowing to initialize and
53 deinitialize the MAILBOX peripheral:
54
55 (+) User must Implement HAL_MAILBOX_MspInit() function in which he configures
56 all related peripherals resources (CLOCK and NVIC ).
57
58 (+) Call the function HAL_MAILBOX_Init() to configure the MAILBOX register.
59
60 (+) Call the function HAL_PKA_DeInit() to restore the default configuration
61 of the selected MAILBOX peripheral.
62
63 @endverbatim
64 * @{
65 */
66
67 /**
68 * @brief Initialize the MAILBOX peripheral.
69 * @param hmailbox MAILBOX handle
70 * @retval HAL status
71 */
HAL_MAILBOX_Init(MAILBOX_HandleTypeDef * hmailbox)72 __HAL_ROM_USED HAL_StatusTypeDef HAL_MAILBOX_Init(MAILBOX_HandleTypeDef *hmailbox)
73 {
74 HAL_StatusTypeDef err = HAL_OK;
75
76
77 return err;
78 }
79
80 /**
81 * @brief DeInitialize the MAILBOX peripheral.
82 * @param hmailbox MAILBOX handle
83 * @retval HAL status
84 */
HAL_MAILBOX_DeInit(MAILBOX_HandleTypeDef * hmailbox)85 __HAL_ROM_USED HAL_StatusTypeDef HAL_MAILBOX_DeInit(MAILBOX_HandleTypeDef *hmailbox)
86 {
87 HAL_StatusTypeDef err = HAL_OK;
88
89
90 return err;
91 }
92
93 /**
94 * @brief Initialize the MAILBOX MSP.
95 * @param hmailbox MAILBOX handle
96 * @retval None
97 */
HAL_MAILBOX_MspInit(MAILBOX_HandleTypeDef * hmailbox)98 __weak void HAL_MAILBOX_MspInit(MAILBOX_HandleTypeDef *hmailbox)
99 {
100 /* Prevent unused argument(s) compilation warning */
101 UNUSED(hmailbox);
102
103 /* NOTE : This function should not be modified. When the callback is needed
104 the HAL_MAILBOX_MspInit should be implemented in the user file
105 */
106 }
107
108 /**
109 * @brief MAILBOX MSP DeInit
110 * @param hmailbox MAILBOX handle
111 * @retval None
112 */
HAL_MAILBOX_MspDeInit(MAILBOX_HandleTypeDef * hmailbox)113 __weak void HAL_MAILBOX_MspDeInit(MAILBOX_HandleTypeDef *hmailbox)
114 {
115 /* Prevent unused argument(s) compilation warning */
116 UNUSED(hmailbox);
117
118 /* NOTE : This function should not be modified. When the callback is needed
119 the HAL_MAILBOX_MspDeInit should be implemented in the user file
120 */
121 }
122
123 /**
124 * @}
125 */
126
127
128 /** @addtogroup MAILBOX_Exported_Functions_Group3
129 * @brief MAILBOX Peripheral State and Error functions
130 *
131 @verbatim
132 ==============================================================================
133 ##### Peripheral State and Error functions #####
134 ==============================================================================
135 [..]
136 This subsection permit to get in run-time the status of the peripheral
137 and the data flow.
138
139 @endverbatim
140 * @{
141 */
142
143 /**
144 * @brief Return the MAILBOX handle state.
145 * @param hmailbox MAILBOX handle
146 * @retval MAILBOX handle state
147 */
HAL_MAILBOX_GetState(MAILBOX_HandleTypeDef const * const hmailbox)148 __HAL_ROM_USED HAL_MAILBOX_StateTypeDef HAL_MAILBOX_GetState(MAILBOX_HandleTypeDef const *const hmailbox)
149 {
150 return hmailbox->State;
151 }
152
153
HAL_MAILBOX_GetMutex(uint8_t core_id,uint8_t ch_id)154 __HAL_ROM_USED MUTEX_CH_TypeDef *HAL_MAILBOX_GetMutex(uint8_t core_id, uint8_t ch_id)
155 {
156 MUTEX_CH_TypeDef *mutex = NULL;
157 uint8_t max_chan_num;
158
159 max_chan_num = (core_id == CORE_ID_HCPU) ? HMUTEX_CHANNLE_NUM : LMUTEX_CHANNLE_NUM;
160
161 if (ch_id >= max_chan_num)
162 {
163 return mutex;
164 }
165
166 if (core_id == CORE_ID_HCPU)
167 {
168 mutex = HMUTEX_CH1;
169 }
170 else if (core_id == CORE_ID_LCPU)
171 {
172 mutex = LMUTEX_CH1;
173 }
174 else
175 {
176 HAL_ASSERT(0);
177 }
178
179 if (mutex)
180 {
181 mutex = mutex + ch_id;
182 }
183
184 return mutex;
185 }
186
HAL_MAILBOX_Lock(MUTEX_HandleTypeDef const * const hmutex,uint8_t ch_id)187 __HAL_ROM_USED MUTEX_LockCoreIdTypeDef HAL_MAILBOX_Lock(MUTEX_HandleTypeDef const *const hmutex, uint8_t ch_id)
188 {
189 MUTEX_LockCoreIdTypeDef core = MUTEX_LOCK_CORE_INVALID;
190 MUTEX_CH_TypeDef *mutex = NULL;
191
192 if (hmutex)
193 mutex = hmutex->Instance;
194 else
195 {
196 mutex = HAL_MAILBOX_GetMutex(CORE_ID_CURRENT, ch_id);
197 }
198 if (mutex)
199 {
200 uint32_t exr = mutex->CxEXR;
201 if (exr & MAILBOX_C1EXR_EX)
202 {
203 core = MUTEX_UNLOCKED;
204 }
205 else
206 {
207 core = GET_REG_VAL(exr, MAILBOX_C2EXR_ID_Msk, MAILBOX_C2EXR_ID_Pos);
208 }
209 }
210 return core;
211 }
212
HAL_MAILBOX_LockEx(MUTEX_HandleTypeDef const * const hmutex,uint8_t ch_id,uint32_t timeout_ms)213 __HAL_ROM_USED MUTEX_LockCoreIdTypeDef HAL_MAILBOX_LockEx(MUTEX_HandleTypeDef const *const hmutex, uint8_t ch_id, uint32_t timeout_ms)
214 {
215 MUTEX_LockCoreIdTypeDef core = MUTEX_LOCK_CORE_INVALID;
216 while (1)
217 {
218 core = HAL_MAILBOX_Lock(hmutex, ch_id);
219 if (core == MUTEX_UNLOCKED)
220 {
221 break;
222 }
223 if (timeout_ms == 0)
224 {
225 break;
226 }
227 timeout_ms--;
228 HAL_Delay(1);
229 }
230 return core;
231 }
232
HAL_MAILBOX_UnLock(MUTEX_HandleTypeDef const * const hmutex,uint8_t ch_id)233 __HAL_ROM_USED void HAL_MAILBOX_UnLock(MUTEX_HandleTypeDef const *const hmutex, uint8_t ch_id)
234 {
235 MUTEX_CH_TypeDef *mutex = NULL;
236
237 if (hmutex)
238 mutex = hmutex->Instance;
239 else
240 mutex = HAL_MAILBOX_GetMutex(CORE_ID_CURRENT, ch_id);
241 if (mutex)
242 mutex->CxEXR |= MAILBOX_C1EXR_EX;
243 }
244
245 /**
246 * @}
247 */
248
249
250
251 /** @brief mailbox irq handler
252 *
253 * @param[in] hmailbox mailbox handle
254 *
255 */
HAL_MAILBOX_IRQHandler(MAILBOX_HandleTypeDef * hmailbox)256 __HAL_ROM_USED void HAL_MAILBOX_IRQHandler(MAILBOX_HandleTypeDef *hmailbox)
257 {
258 uint32_t status;
259 uint32_t clear_status;
260 uint8_t q_idx;
261 MAILBOX_CH_TypeDef *mailbox;
262
263 mailbox = hmailbox->Instance;
264
265 status = mailbox->CxMISR;
266 clear_status = status;
267 mailbox->CxICR = clear_status;
268 q_idx = 0;
269 while (status)
270 {
271 if (1 & status)
272 {
273 if (hmailbox->NotificationCallback)
274 {
275 hmailbox->NotificationCallback(hmailbox, q_idx);
276 }
277 }
278 status >>= 1;
279 q_idx++;
280 }
281 }
282
283 /**
284 * @}
285 */
286
287 /** @addtogroup MAILBOX_Private_Functions
288 * @{
289 */
290
291
292
293 /**
294 * @}
295 */
296
297 #endif /* HAL_MAILBOX_MODULE_ENABLED */
298
299 /**
300 * @}
301 */
302
303 /**
304 * @}
305 */