1 /*
2  * Copyright 2018 -2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_component_serial_manager.h"
10 #include "fsl_component_serial_port_internal.h"
11 
12 #if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U)) || \
13     (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
14 #include "fsl_adapter_uart.h"
15 
16 #include "fsl_component_serial_port_uart.h"
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 #ifndef NDEBUG
22 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
23 #undef assert
24 #define assert(n)
25 #else
26 /* MISRA C-2012 Rule 17.2 */
27 #undef assert
28 #define assert(n) \
29     while (!(n))  \
30     {             \
31         ;         \
32     }
33 #endif
34 #endif
35 
36 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
37 #define SERIAL_PORT_UART_RECEIVE_DATA_LENGTH 1U
38 typedef struct _serial_uart_send_state
39 {
40     uint8_t *buffer;
41     uint32_t length;
42     serial_manager_callback_t callback;
43     void *callbackParam;
44     volatile uint8_t busy;
45 } serial_uart_send_state_t;
46 
47 typedef struct _serial_uart_recv_state
48 {
49     serial_manager_callback_t callback;
50     void *callbackParam;
51     volatile uint8_t busy;
52     volatile uint8_t rxEnable;
53     uint8_t readBuffer[SERIAL_PORT_UART_RECEIVE_DATA_LENGTH];
54 } serial_uart_recv_state_t;
55 
56 typedef struct _serial_uart_dma_recv_state
57 {
58     serial_manager_callback_t callback;
59     void *callbackParam;
60     volatile uint8_t busy;
61     volatile uint8_t rxEnable;
62     uint8_t readBuffer[SERIAL_PORT_UART_DMA_RECEIVE_DATA_LENGTH];
63 } serial_uart_dma_recv_state_t;
64 
65 typedef struct _serial_uart_block_state
66 {
67     UART_HANDLE_DEFINE(usartHandleBuffer);
68 } serial_uart_block_state_t;
69 #endif
70 
71 typedef struct _serial_uart_state
72 {
73     UART_HANDLE_DEFINE(usartHandleBuffer);
74 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
75     serial_uart_send_state_t tx;
76     serial_uart_recv_state_t rx;
77 #endif
78 } serial_uart_state_t;
79 #endif
80 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
81 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
82 typedef struct _serial_uart_dma_state
83 {
84     UART_HANDLE_DEFINE(usartHandleBuffer);
85     UART_DMA_HANDLE_DEFINE(uartDmaHandle);
86 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
87     serial_uart_send_state_t tx;
88     serial_uart_dma_recv_state_t rx;
89 #endif
90 } serial_uart_dma_state_t;
91 #endif
92 #endif
93 
94 /*******************************************************************************
95  * Prototypes
96  ******************************************************************************/
97 
98 /*******************************************************************************
99  * Code
100  ******************************************************************************/
101 
102 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
Serial_UartEnableReceiving(serial_uart_state_t * serialUartHandle)103 static serial_manager_status_t Serial_UartEnableReceiving(serial_uart_state_t *serialUartHandle)
104 {
105 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
106     hal_uart_transfer_t transfer;
107 #endif
108     if (1U == serialUartHandle->rx.rxEnable)
109     {
110         serialUartHandle->rx.busy = 1U;
111 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
112         transfer.data     = &serialUartHandle->rx.readBuffer[0];
113         transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
114         if (kStatus_HAL_UartSuccess !=
115             HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
116 #else
117         if (kStatus_HAL_UartSuccess !=
118             HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
119                                        &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
120 #endif
121         {
122             serialUartHandle->rx.busy = 0U;
123             return kStatus_SerialManager_Error;
124         }
125     }
126     return kStatus_SerialManager_Success;
127 }
128 
129 /* UART user callback */
Serial_UartCallback(hal_uart_handle_t handle,hal_uart_status_t status,void * userData)130 static void Serial_UartCallback(hal_uart_handle_t handle, hal_uart_status_t status, void *userData)
131 {
132     serial_uart_state_t *serialUartHandle;
133     serial_manager_callback_message_t serialMsg;
134 
135     assert(userData);
136     serialUartHandle = (serial_uart_state_t *)userData;
137 
138     if ((hal_uart_status_t)kStatus_HAL_UartRxIdle == status)
139     {
140 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
141         (void)HAL_UartTransferAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
142 #else
143         (void)HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
144 #endif
145         if ((NULL != serialUartHandle->rx.callback))
146         {
147             serialMsg.buffer = &serialUartHandle->rx.readBuffer[0];
148             serialMsg.length = sizeof(serialUartHandle->rx.readBuffer);
149             serialUartHandle->rx.callback(serialUartHandle->rx.callbackParam, &serialMsg,
150                                           kStatus_SerialManager_Success);
151         }
152     }
153     else if ((hal_uart_status_t)kStatus_HAL_UartTxIdle == status)
154     {
155         if (0U != serialUartHandle->tx.busy)
156         {
157             serialUartHandle->tx.busy = 0U;
158             if ((NULL != serialUartHandle->tx.callback))
159             {
160                 serialMsg.buffer = serialUartHandle->tx.buffer;
161                 serialMsg.length = serialUartHandle->tx.length;
162                 serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &serialMsg,
163                                               kStatus_SerialManager_Success);
164             }
165         }
166     }
167     else
168     {
169     }
170 }
171 #endif
172 
Serial_UartInit(serial_handle_t serialHandle,void * serialConfig)173 serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig)
174 {
175     serial_uart_state_t *serialUartHandle;
176 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
177     serial_port_uart_config_t *uartConfig = (serial_port_uart_config_t *)serialConfig;
178 #endif
179     serial_manager_status_t serialManagerStatus = kStatus_SerialManager_Success;
180 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
181 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
182 #if 0 /* Not used below! */
183     hal_uart_transfer_t transfer;
184 #endif
185 #endif
186 #endif
187 
188     assert(serialConfig);
189     assert(serialHandle);
190     assert(SERIAL_PORT_UART_HANDLE_SIZE >= sizeof(serial_uart_state_t));
191 
192     serialUartHandle    = (serial_uart_state_t *)serialHandle;
193     serialManagerStatus = (serial_manager_status_t)HAL_UartInit(
194         ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), (const hal_uart_config_t *)serialConfig);
195     assert(kStatus_SerialManager_Success == serialManagerStatus);
196     (void)serialManagerStatus;
197 
198 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
199     serialUartHandle->rx.rxEnable = uartConfig->enableRx;
200 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
201 
202     (void)HAL_UartTransferInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
203                                           Serial_UartCallback, serialUartHandle);
204 #else
205     (void)HAL_UartInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), Serial_UartCallback,
206                                   serialUartHandle);
207 #endif
208 #endif
209 
210     return serialManagerStatus;
211 }
212 
Serial_UartDeinit(serial_handle_t serialHandle)213 serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle)
214 {
215     serial_uart_state_t *serialUartHandle;
216 
217     assert(serialHandle);
218 
219     serialUartHandle = (serial_uart_state_t *)serialHandle;
220 
221 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
222 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
223     (void)HAL_UartTransferAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
224 #else
225     (void)HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
226 #endif
227 #endif
228     (void)HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
229 
230 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
231     serialUartHandle->tx.busy = 0U;
232     serialUartHandle->rx.busy = 0U;
233 #endif
234 
235     return kStatus_SerialManager_Success;
236 }
237 
238 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
239 
Serial_UartWrite(serial_handle_t serialHandle,uint8_t * buffer,uint32_t length)240 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
241 {
242     serial_uart_state_t *serialUartHandle;
243     hal_uart_status_t uartstatus;
244 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
245     hal_uart_transfer_t transfer;
246 #endif
247 
248     assert(serialHandle);
249     assert(buffer);
250     assert(length);
251 
252     serialUartHandle = (serial_uart_state_t *)serialHandle;
253     if (0U != serialUartHandle->tx.busy)
254     {
255         return kStatus_SerialManager_Busy;
256     }
257     serialUartHandle->tx.busy = 1U;
258 
259     serialUartHandle->tx.buffer = buffer;
260     serialUartHandle->tx.length = length;
261 
262 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
263     transfer.data     = buffer;
264     transfer.dataSize = length;
265     uartstatus =
266         HAL_UartTransferSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer);
267 #else
268 
269     uartstatus = HAL_UartSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
270 #endif
271     assert(kStatus_HAL_UartSuccess == uartstatus);
272     (void)uartstatus;
273 
274     return kStatus_SerialManager_Success;
275 }
276 
Serial_UartRead(serial_handle_t serialHandle,uint8_t * buffer,uint32_t length)277 serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
278 {
279     assert(serialHandle);
280     (void)buffer;
281     (void)length;
282     return (serial_manager_status_t)Serial_UartEnableReceiving(serialHandle);
283 }
284 
285 #else
286 
Serial_UartWrite(serial_handle_t serialHandle,uint8_t * buffer,uint32_t length)287 serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
288 {
289     serial_uart_state_t *serialUartHandle;
290 
291     assert(serialHandle);
292     assert(buffer);
293     assert(length);
294 
295     serialUartHandle = (serial_uart_state_t *)serialHandle;
296 
297     return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
298                                                          buffer, length);
299 }
300 
Serial_UartRead(serial_handle_t serialHandle,uint8_t * buffer,uint32_t length)301 serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
302 {
303     serial_uart_state_t *serialUartHandle;
304 
305     assert(serialHandle);
306     assert(buffer);
307     assert(length);
308 
309     serialUartHandle = (serial_uart_state_t *)serialHandle;
310 
311     return (serial_manager_status_t)HAL_UartReceiveBlocking(
312         ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
313 }
314 
315 #endif
316 
317 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
Serial_UartCancelWrite(serial_handle_t serialHandle)318 serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle)
319 {
320     serial_uart_state_t *serialUartHandle;
321     serial_manager_callback_message_t serialMsg;
322     uint32_t primask;
323     uint8_t isBusy = 0U;
324 
325     assert(serialHandle);
326 
327     serialUartHandle = (serial_uart_state_t *)serialHandle;
328 
329     primask                   = DisableGlobalIRQ();
330     isBusy                    = serialUartHandle->tx.busy;
331     serialUartHandle->tx.busy = 0U;
332     EnableGlobalIRQ(primask);
333 
334 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
335     (void)HAL_UartTransferAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
336 #else
337     (void)HAL_UartAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
338 #endif
339     if (0U != isBusy)
340     {
341         if ((NULL != serialUartHandle->tx.callback))
342         {
343             serialMsg.buffer = serialUartHandle->tx.buffer;
344             serialMsg.length = serialUartHandle->tx.length;
345             serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &serialMsg,
346                                           kStatus_SerialManager_Canceled);
347         }
348     }
349     return kStatus_SerialManager_Success;
350 }
351 
Serial_UartInstallTxCallback(serial_handle_t serialHandle,serial_manager_callback_t callback,void * callbackParam)352 serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
353                                                      serial_manager_callback_t callback,
354                                                      void *callbackParam)
355 {
356     serial_uart_state_t *serialUartHandle;
357 
358     assert(serialHandle);
359 
360     serialUartHandle = (serial_uart_state_t *)serialHandle;
361 
362     serialUartHandle->tx.callback      = callback;
363     serialUartHandle->tx.callbackParam = callbackParam;
364 
365     return kStatus_SerialManager_Success;
366 }
367 
Serial_UartInstallRxCallback(serial_handle_t serialHandle,serial_manager_callback_t callback,void * callbackParam)368 serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
369                                                      serial_manager_callback_t callback,
370                                                      void *callbackParam)
371 {
372     serial_uart_state_t *serialUartHandle;
373 
374     assert(serialHandle);
375 
376     serialUartHandle = (serial_uart_state_t *)serialHandle;
377 
378     serialUartHandle->rx.callback      = callback;
379     serialUartHandle->rx.callbackParam = callbackParam;
380 
381 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
382     (void)Serial_UartEnableReceiving(serialUartHandle);
383 #endif
384     return kStatus_SerialManager_Success;
385 }
386 
Serial_UartIsrFunction(serial_handle_t serialHandle)387 void Serial_UartIsrFunction(serial_handle_t serialHandle)
388 {
389     serial_uart_state_t *serialUartHandle;
390 
391     assert(serialHandle);
392 
393     serialUartHandle = (serial_uart_state_t *)serialHandle;
394 
395     HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
396 }
397 #endif
398 
Serial_UartEnterLowpower(serial_handle_t serialHandle)399 serial_manager_status_t Serial_UartEnterLowpower(serial_handle_t serialHandle)
400 {
401     serial_uart_state_t *serialUartHandle;
402     hal_uart_status_t uartstatus;
403 
404     assert(serialHandle);
405 
406     serialUartHandle = (serial_uart_state_t *)serialHandle;
407 
408     uartstatus = HAL_UartEnterLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
409     assert(kStatus_HAL_UartSuccess == uartstatus);
410     (void)uartstatus;
411 
412     return kStatus_SerialManager_Success;
413 }
414 
Serial_UartExitLowpower(serial_handle_t serialHandle)415 serial_manager_status_t Serial_UartExitLowpower(serial_handle_t serialHandle)
416 {
417     serial_uart_state_t *serialUartHandle;
418     serial_manager_status_t status = kStatus_SerialManager_Success;
419     hal_uart_status_t uartstatus;
420 
421     assert(serialHandle);
422 
423     serialUartHandle = (serial_uart_state_t *)serialHandle;
424 
425     uartstatus = HAL_UartExitLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
426     assert(kStatus_HAL_UartSuccess == uartstatus);
427     (void)uartstatus;
428 
429 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
430     status = Serial_UartEnableReceiving(serialUartHandle);
431 #endif
432 
433     return status;
434 }
435 
436 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
437 #if (defined(SERIAL_PORT_TYPE_UART_DMA) && (SERIAL_PORT_TYPE_UART_DMA > 0U))
438 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
Serial_UartDmaEnableReceiving(serial_uart_dma_state_t * serialUartHandle)439 static serial_manager_status_t Serial_UartDmaEnableReceiving(serial_uart_dma_state_t *serialUartHandle)
440 {
441     if (1U == serialUartHandle->rx.rxEnable)
442     {
443         serialUartHandle->rx.busy = 1U;
444         if (kStatus_HAL_UartDmaSuccess !=
445             HAL_UartDMATransferReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
446                                        &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer),
447                                        false))
448 
449         {
450             serialUartHandle->rx.busy = 0U;
451             return kStatus_SerialManager_Error;
452         }
453     }
454     return kStatus_SerialManager_Success;
455 }
456 
457 /* UART user callback */
Serial_UartDmaCallback(hal_uart_dma_handle_t handle,hal_dma_callback_msg_t * dmaMsg,void * callbackParam)458 static void Serial_UartDmaCallback(hal_uart_dma_handle_t handle, hal_dma_callback_msg_t *dmaMsg, void *callbackParam)
459 {
460     serial_uart_dma_state_t *serialUartHandle;
461     serial_manager_callback_message_t cb_msg;
462 
463     assert(callbackParam);
464     serialUartHandle = (serial_uart_dma_state_t *)callbackParam;
465 
466     if (((hal_uart_dma_status_t)kStatus_HAL_UartDmaRxIdle == dmaMsg->status) ||
467         (kStatus_HAL_UartDmaIdleline == dmaMsg->status))
468     {
469         if ((NULL != serialUartHandle->rx.callback))
470         {
471             cb_msg.buffer = dmaMsg->data;
472             cb_msg.length = dmaMsg->dataSize;
473             serialUartHandle->rx.callback(serialUartHandle->rx.callbackParam, &cb_msg, kStatus_SerialManager_Success);
474         }
475 
476         if (kStatus_HAL_UartDmaSuccess ==
477             HAL_UartDMATransferReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
478                                        &serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer),
479                                        false))
480         {
481             serialUartHandle->rx.busy = 1U;
482         }
483     }
484     else if (kStatus_HAL_UartDmaTxIdle == dmaMsg->status)
485     {
486         if (0U != serialUartHandle->tx.busy)
487         {
488             serialUartHandle->tx.busy = 0U;
489             if ((NULL != serialUartHandle->tx.callback))
490             {
491                 cb_msg.buffer = dmaMsg->data;
492                 cb_msg.length = dmaMsg->dataSize;
493                 serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &cb_msg,
494                                               kStatus_SerialManager_Success);
495             }
496         }
497     }
498     else
499     {
500     }
501 }
502 
503 #endif
504 
Serial_UartDmaInit(serial_handle_t serialHandle,void * serialConfig)505 serial_manager_status_t Serial_UartDmaInit(serial_handle_t serialHandle, void *serialConfig)
506 {
507     serial_uart_dma_state_t *serialUartHandle;
508 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
509     serial_port_uart_dma_config_t *uartConfig = (serial_port_uart_dma_config_t *)serialConfig;
510 #endif
511     serial_manager_status_t serialManagerStatus = kStatus_SerialManager_Success;
512 
513     assert(serialConfig);
514     assert(serialHandle);
515 
516     assert(SERIAL_PORT_UART_DMA_HANDLE_SIZE >= sizeof(serial_uart_dma_state_t));
517 
518     serialUartHandle    = (serial_uart_dma_state_t *)serialHandle;
519     serialManagerStatus = (serial_manager_status_t)HAL_UartInit(
520         ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), (const hal_uart_config_t *)serialConfig);
521     assert(kStatus_SerialManager_Success == serialManagerStatus);
522     (void)serialManagerStatus;
523 
524 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
525 
526     hal_uart_dma_config_t dmaConfig;
527 
528     dmaConfig.uart_instance             = uartConfig->instance;
529     dmaConfig.dma_instance              = uartConfig->dma_instance;
530     dmaConfig.rx_channel                = uartConfig->rx_channel;
531     dmaConfig.tx_channel                = uartConfig->tx_channel;
532     dmaConfig.dma_mux_configure         = uartConfig->dma_mux_configure;
533     dmaConfig.dma_channel_mux_configure = uartConfig->dma_channel_mux_configure;
534 
535     // Init uart dma
536     (void)HAL_UartDMAInit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
537                           (hal_uart_dma_handle_t *)serialUartHandle->uartDmaHandle, &dmaConfig);
538 
539 #endif
540 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
541 
542     serialUartHandle->rx.rxEnable = uartConfig->enableRx;
543     (void)HAL_UartDMATransferInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
544                                              Serial_UartDmaCallback, serialUartHandle);
545 
546 #endif
547 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
548     serialManagerStatus = Serial_UartDmaEnableReceiving(serialUartHandle);
549 #endif
550 
551     return serialManagerStatus;
552 }
553 
Serial_UartDmaDeinit(serial_handle_t serialHandle)554 serial_manager_status_t Serial_UartDmaDeinit(serial_handle_t serialHandle)
555 {
556     serial_uart_dma_state_t *serialUartHandle;
557 
558     assert(serialHandle);
559 
560     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
561 
562     (void)HAL_UartDMAAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
563 
564     (void)HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
565     (void)HAL_UartDMADeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
566 
567 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
568     serialUartHandle->tx.busy = 0U;
569     serialUartHandle->rx.busy = 0U;
570 #endif
571 
572     return kStatus_SerialManager_Success;
573 }
574 
575 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
576 
Serial_UartDmaWrite(serial_handle_t serialHandle,uint8_t * buffer,uint32_t length)577 serial_manager_status_t Serial_UartDmaWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
578 {
579     serial_uart_dma_state_t *serialUartHandle;
580     hal_uart_status_t uartstatus;
581 
582     assert(serialHandle);
583     assert(buffer);
584     assert(length);
585 
586     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
587 
588     if (0U != serialUartHandle->tx.busy)
589     {
590         return kStatus_SerialManager_Busy;
591     }
592     serialUartHandle->tx.busy = 1U;
593 
594     serialUartHandle->tx.buffer = buffer;
595     serialUartHandle->tx.length = length;
596 
597     uartstatus = (hal_uart_status_t)HAL_UartDMATransferSend(
598         ((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
599 
600     assert(kStatus_HAL_UartSuccess == uartstatus);
601     (void)uartstatus;
602 
603     return kStatus_SerialManager_Success;
604 }
605 
606 #endif
607 
608 #if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
Serial_UartDmaCancelWrite(serial_handle_t serialHandle)609 serial_manager_status_t Serial_UartDmaCancelWrite(serial_handle_t serialHandle)
610 {
611     serial_uart_dma_state_t *serialUartHandle;
612     serial_manager_callback_message_t serialMsg;
613     uint32_t primask;
614     uint8_t isBusy = 0U;
615 
616     assert(serialHandle);
617 
618     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
619 
620     primask                   = DisableGlobalIRQ();
621     isBusy                    = serialUartHandle->tx.busy;
622     serialUartHandle->tx.busy = 0U;
623     EnableGlobalIRQ(primask);
624 
625     (void)HAL_UartDMAAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
626 
627     if (0U != isBusy)
628     {
629         if ((NULL != serialUartHandle->tx.callback))
630         {
631             serialMsg.buffer = serialUartHandle->tx.buffer;
632             serialMsg.length = serialUartHandle->tx.length;
633             serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &serialMsg,
634                                           kStatus_SerialManager_Canceled);
635         }
636     }
637     return kStatus_SerialManager_Success;
638 }
639 
Serial_UartDmaInstallTxCallback(serial_handle_t serialHandle,serial_manager_callback_t callback,void * callbackParam)640 serial_manager_status_t Serial_UartDmaInstallTxCallback(serial_handle_t serialHandle,
641                                                         serial_manager_callback_t callback,
642                                                         void *callbackParam)
643 {
644     serial_uart_dma_state_t *serialUartHandle;
645 
646     assert(serialHandle);
647 
648     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
649 
650     serialUartHandle->tx.callback      = callback;
651     serialUartHandle->tx.callbackParam = callbackParam;
652 
653     return kStatus_SerialManager_Success;
654 }
655 
Serial_UartDmaInstallRxCallback(serial_handle_t serialHandle,serial_manager_callback_t callback,void * callbackParam)656 serial_manager_status_t Serial_UartDmaInstallRxCallback(serial_handle_t serialHandle,
657                                                         serial_manager_callback_t callback,
658                                                         void *callbackParam)
659 {
660     serial_uart_dma_state_t *serialUartHandle;
661 
662     assert(serialHandle);
663 
664     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
665 
666     serialUartHandle->rx.callback      = callback;
667     serialUartHandle->rx.callbackParam = callbackParam;
668 
669     return kStatus_SerialManager_Success;
670 }
671 
Serial_UartDmaIsrFunction(serial_handle_t serialHandle)672 void Serial_UartDmaIsrFunction(serial_handle_t serialHandle)
673 {
674     serial_uart_dma_state_t *serialUartHandle;
675 
676     assert(serialHandle);
677 
678     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
679 
680     HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
681 }
682 #endif
683 
Serial_UartDmaEnterLowpower(serial_handle_t serialHandle)684 serial_manager_status_t Serial_UartDmaEnterLowpower(serial_handle_t serialHandle)
685 {
686     serial_uart_dma_state_t *serialUartHandle;
687     hal_uart_status_t uartstatus;
688 
689     assert(serialHandle);
690 
691     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
692 
693     uartstatus = HAL_UartEnterLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
694     assert(kStatus_HAL_UartSuccess == uartstatus);
695     (void)uartstatus;
696 
697     return kStatus_SerialManager_Success;
698 }
699 
Serial_UartDmaExitLowpower(serial_handle_t serialHandle)700 serial_manager_status_t Serial_UartDmaExitLowpower(serial_handle_t serialHandle)
701 {
702     serial_uart_dma_state_t *serialUartHandle;
703     serial_manager_status_t status = kStatus_SerialManager_Success;
704     hal_uart_status_t uartstatus;
705 
706     assert(serialHandle);
707 
708     serialUartHandle = (serial_uart_dma_state_t *)serialHandle;
709 
710     uartstatus = HAL_UartExitLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
711     assert(kStatus_HAL_UartSuccess == uartstatus);
712     (void)uartstatus;
713 
714     return status;
715 }
716 #endif
717 #endif
718