1 /*
2  * Copyright 2018 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_common.h"
10 #include "fsl_usart.h"
11 #include "fsl_flexcomm.h"
12 
13 #include "fsl_adapter_uart.h"
14 
15 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
16 #include "fsl_component_timer_manager.h"
17 #include "fsl_usart_dma.h"
18 #endif /* HAL_UART_DMA_ENABLE */
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 #ifndef NDEBUG
24 #if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
25 #undef assert
26 #define assert(n)
27 #endif
28 #endif
29 
30 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
31 /*! @brief uart RX state structure. */
32 typedef struct _hal_uart_dma_receive_state
33 {
34     uint8_t *volatile buffer;
35     volatile uint32_t bufferLength;
36     volatile uint32_t bufferSofar;
37     volatile uint32_t timeout;
38     volatile bool receiveAll;
39 } hal_uart_dma_receive_state_t;
40 
41 /*! @brief uart TX state structure. */
42 typedef struct _hal_uart_dma_send_state
43 {
44     uint8_t *volatile buffer;
45     volatile uint32_t bufferLength;
46     volatile uint32_t bufferSofar;
47     volatile uint32_t timeout;
48 } hal_uart_dma_send_state_t;
49 
50 typedef struct _hal_uart_dma_state
51 {
52     struct _hal_uart_dma_state *next;
53     uint8_t instance; /* USART instance */
54     hal_uart_dma_transfer_callback_t dma_callback;
55     void *dma_callback_param;
56     usart_dma_handle_t dmaHandle;
57     dma_handle_t txDmaHandle;
58     dma_handle_t rxDmaHandle;
59     hal_uart_dma_receive_state_t dma_rx;
60     hal_uart_dma_send_state_t dma_tx;
61 } hal_uart_dma_state_t;
62 
63 typedef struct _uart_dma_list
64 {
65     TIMER_MANAGER_HANDLE_DEFINE(timerManagerHandle);
66     hal_uart_dma_state_t *dma_list;
67     volatile int8_t activeCount;
68 } hal_uart_dma_list_t;
69 
70 static hal_uart_dma_list_t s_dmaHandleList;
71 #endif /* HAL_UART_DMA_ENABLE */
72 
73 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
74 /*! @brief uart RX state structure. */
75 typedef struct _hal_uart_receive_state
76 {
77     volatile uint8_t *buffer;
78     volatile uint32_t bufferLength;
79     volatile uint32_t bufferSofar;
80 } hal_uart_receive_state_t;
81 
82 /*! @brief uart TX state structure. */
83 typedef struct _hal_uart_send_state
84 {
85     volatile uint8_t *buffer;
86     volatile uint32_t bufferLength;
87     volatile uint32_t bufferSofar;
88 } hal_uart_send_state_t;
89 #endif
90 /*! @brief uart state structure. */
91 typedef struct _hal_uart_state
92 {
93 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
94     hal_uart_transfer_callback_t callback;
95     void *callbackParam;
96 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
97     usart_handle_t hardwareHandle;
98 #endif
99     hal_uart_receive_state_t rx;
100     hal_uart_send_state_t tx;
101 #endif
102     uint8_t instance;
103 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
104     hal_uart_dma_state_t *dmaHandle;
105 #endif /* HAL_UART_DMA_ENABLE */
106 #if (defined(HAL_UART_ADAPTER_LOWPOWER) && (HAL_UART_ADAPTER_LOWPOWER > 0U))
107     hal_uart_config_t config;
108 #endif
109 } hal_uart_state_t;
110 
111 /*******************************************************************************
112  * Prototypes
113  ******************************************************************************/
114 
115 /*******************************************************************************
116  * Variables
117  ******************************************************************************/
118 static USART_Type *const s_UsartAdapterBase[] = USART_BASE_PTRS;
119 
120 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
121 
122 #if !(defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
123 /* Array of USART IRQ number. */
124 static const IRQn_Type s_UsartIRQ[] = USART_IRQS;
125 #endif
126 
127 #endif
128 
129 /*******************************************************************************
130  * Code
131  ******************************************************************************/
132 
133 #if ((defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U)) || \
134      (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U)))
HAL_UartGetStatus(status_t status)135 static hal_uart_status_t HAL_UartGetStatus(status_t status)
136 {
137     hal_uart_status_t uartStatus = kStatus_HAL_UartError;
138     switch (status)
139     {
140         case kStatus_Success:
141             uartStatus = kStatus_HAL_UartSuccess;
142             break;
143         case kStatus_USART_TxBusy:
144             uartStatus = kStatus_HAL_UartTxBusy;
145             break;
146         case kStatus_USART_RxBusy:
147             uartStatus = kStatus_HAL_UartRxBusy;
148             break;
149         case kStatus_USART_TxIdle:
150             uartStatus = kStatus_HAL_UartTxIdle;
151             break;
152         case kStatus_USART_RxIdle:
153             uartStatus = kStatus_HAL_UartRxIdle;
154             break;
155         case kStatus_USART_BaudrateNotSupport:
156             uartStatus = kStatus_HAL_UartBaudrateNotSupport;
157             break;
158         case kStatus_USART_NoiseError:
159         case kStatus_USART_FramingError:
160         case kStatus_USART_ParityError:
161             uartStatus = kStatus_HAL_UartProtocolError;
162             break;
163         default:
164             /* This comments for MISRA C-2012 Rule 16.4 */
165             break;
166     }
167     return uartStatus;
168 }
169 #else
HAL_UartGetStatus(status_t status)170 static hal_uart_status_t HAL_UartGetStatus(status_t status)
171 {
172     if (kStatus_Success == status)
173     {
174         return kStatus_HAL_UartSuccess;
175     }
176     else
177     {
178         return kStatus_HAL_UartError;
179     }
180 }
181 #endif
182 
183 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
184 
185 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
HAL_UartCallback(USART_Type * base,usart_handle_t * handle,status_t status,void * callbackParam)186 static void HAL_UartCallback(USART_Type *base, usart_handle_t *handle, status_t status, void *callbackParam)
187 {
188     hal_uart_state_t *uartHandle;
189     hal_uart_status_t uartStatus = HAL_UartGetStatus(status);
190     assert(callbackParam);
191 
192     uartHandle = (hal_uart_state_t *)callbackParam;
193 
194     if (kStatus_HAL_UartProtocolError == uartStatus)
195     {
196         if (0U != uartHandle->hardwareHandle.rxDataSize)
197         {
198             uartStatus = kStatus_HAL_UartError;
199         }
200     }
201 
202     if (NULL != uartHandle->callback)
203     {
204         uartHandle->callback(uartHandle, uartStatus, uartHandle->callbackParam);
205     }
206 }
207 
208 #else
HAL_UartInterruptHandle(USART_Type * base,void * handle)209 static void HAL_UartInterruptHandle(USART_Type *base, void *handle)
210 {
211     hal_uart_state_t *uartHandle = (hal_uart_state_t *)handle;
212     uint32_t status;
213     uint8_t instance;
214 
215     if (NULL == uartHandle)
216     {
217         return;
218     }
219     instance = uartHandle->instance;
220 
221     status = USART_GetStatusFlags(s_UsartAdapterBase[instance]);
222 
223     /* Receive data register full */
224     if ((0U != (USART_FIFOSTAT_RXNOTEMPTY_MASK & status)) &&
225         (0U != (USART_GetEnabledInterrupts(s_UsartAdapterBase[instance]) & USART_FIFOINTENSET_RXLVL_MASK)))
226     {
227         if (NULL != uartHandle->rx.buffer)
228         {
229             uartHandle->rx.buffer[uartHandle->rx.bufferSofar++] = USART_ReadByte(s_UsartAdapterBase[instance]);
230             if (uartHandle->rx.bufferSofar >= uartHandle->rx.bufferLength)
231             {
232                 USART_DisableInterrupts(s_UsartAdapterBase[instance],
233                                         USART_FIFOINTENCLR_RXLVL_MASK | USART_FIFOINTENCLR_RXERR_MASK);
234                 uartHandle->rx.buffer = NULL;
235                 if (NULL != uartHandle->callback)
236                 {
237                     uartHandle->callback(uartHandle, kStatus_HAL_UartRxIdle, uartHandle->callbackParam);
238                 }
239             }
240         }
241     }
242 
243     /* Send data register empty and the interrupt is enabled. */
244     if ((0U != (USART_FIFOSTAT_TXNOTFULL_MASK & status)) &&
245         (0U != (USART_GetEnabledInterrupts(s_UsartAdapterBase[instance]) & USART_FIFOINTENSET_TXLVL_MASK)))
246     {
247         if (NULL != uartHandle->tx.buffer)
248         {
249             USART_WriteByte(s_UsartAdapterBase[instance], uartHandle->tx.buffer[uartHandle->tx.bufferSofar++]);
250             if (uartHandle->tx.bufferSofar >= uartHandle->tx.bufferLength)
251             {
252                 USART_DisableInterrupts(s_UsartAdapterBase[instance], USART_FIFOINTENCLR_TXLVL_MASK);
253                 uartHandle->tx.buffer = NULL;
254                 if (NULL != uartHandle->callback)
255                 {
256                     uartHandle->callback(uartHandle, kStatus_HAL_UartTxIdle, uartHandle->callbackParam);
257                 }
258             }
259         }
260     }
261 
262 #if 1
263     USART_ClearStatusFlags(s_UsartAdapterBase[instance], status);
264 #endif
265 }
266 
HAL_UartInterruptHandle_Wapper(void * base,void * handle)267 static void HAL_UartInterruptHandle_Wapper(void *base, void *handle)
268 {
269     HAL_UartInterruptHandle((USART_Type *)base, handle);
270 }
271 #endif
272 
273 #endif
274 
HAL_UartInitCommon(hal_uart_handle_t handle,const hal_uart_config_t * config)275 static hal_uart_status_t HAL_UartInitCommon(hal_uart_handle_t handle, const hal_uart_config_t *config)
276 {
277     usart_config_t usartConfig;
278     status_t status;
279 
280     assert(handle);
281     assert(config);
282     assert(config->instance < (sizeof(s_UsartAdapterBase) / sizeof(USART_Type *)));
283     assert(s_UsartAdapterBase[config->instance]);
284     assert(HAL_UART_HANDLE_SIZE >= sizeof(hal_uart_state_t));
285 
286     USART_GetDefaultConfig(&usartConfig);
287     usartConfig.baudRate_Bps = config->baudRate_Bps;
288 
289     if ((0U != config->enableRxRTS) || (0U != config->enableTxCTS))
290     {
291         usartConfig.enableHardwareFlowControl = true;
292     }
293 
294     if (kHAL_UartParityEven == config->parityMode)
295     {
296         usartConfig.parityMode = kUSART_ParityEven;
297     }
298     else if (kHAL_UartParityOdd == config->parityMode)
299     {
300         usartConfig.parityMode = kUSART_ParityOdd;
301     }
302     else
303     {
304         usartConfig.parityMode = kUSART_ParityDisabled;
305     }
306 
307     if (kHAL_UartTwoStopBit == config->stopBitCount)
308     {
309         usartConfig.stopBitCount = kUSART_TwoStopBit;
310     }
311     else
312     {
313         usartConfig.stopBitCount = kUSART_OneStopBit;
314     }
315     usartConfig.enableRx    = (bool)config->enableRx;
316     usartConfig.enableTx    = (bool)config->enableTx;
317     usartConfig.txWatermark = kUSART_TxFifo0;
318     usartConfig.rxWatermark = kUSART_RxFifo1;
319 
320     status = USART_Init(s_UsartAdapterBase[config->instance], &usartConfig, config->srcClock_Hz);
321 
322     if (kStatus_Success != status)
323     {
324         return HAL_UartGetStatus(status);
325     }
326 
327     return kStatus_HAL_UartSuccess;
328 }
329 
HAL_UartInit(hal_uart_handle_t handle,const hal_uart_config_t * uart_config)330 hal_uart_status_t HAL_UartInit(hal_uart_handle_t handle, const hal_uart_config_t *uart_config)
331 {
332     hal_uart_state_t *uartHandle;
333     hal_uart_status_t status;
334 
335     /* Init serial port */
336     status = HAL_UartInitCommon(handle, uart_config);
337     if (kStatus_HAL_UartSuccess != status)
338     {
339         return status;
340     }
341 
342     uartHandle           = (hal_uart_state_t *)handle;
343     uartHandle->instance = uart_config->instance;
344 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
345     uartHandle->dmaHandle = NULL;
346 #endif /* HAL_UART_DMA_ENABLE */
347 #if (defined(HAL_UART_ADAPTER_LOWPOWER) && (HAL_UART_ADAPTER_LOWPOWER > 0U))
348     (void)memcpy(&uartHandle->config, uart_config, sizeof(hal_uart_config_t));
349 #endif
350 
351 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
352 
353 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
354     USART_TransferCreateHandle(s_UsartAdapterBase[uart_config->instance], &uartHandle->hardwareHandle,
355                                (usart_transfer_callback_t)HAL_UartCallback, handle);
356 #else
357     /* Enable interrupt in NVIC. */
358     FLEXCOMM_SetIRQHandler(s_UsartAdapterBase[uart_config->instance], HAL_UartInterruptHandle_Wapper, handle);
359     NVIC_SetPriority((IRQn_Type)s_UsartIRQ[uart_config->instance], HAL_UART_ISR_PRIORITY);
360     (void)EnableIRQ(s_UsartIRQ[uart_config->instance]);
361 #endif
362 
363 #endif
364 
365     return kStatus_HAL_UartSuccess;
366 }
367 
HAL_UartDeinit(hal_uart_handle_t handle)368 hal_uart_status_t HAL_UartDeinit(hal_uart_handle_t handle)
369 {
370     hal_uart_state_t *uartHandle;
371 
372     assert(handle);
373 
374     uartHandle = (hal_uart_state_t *)handle;
375 
376     USART_Deinit(s_UsartAdapterBase[uartHandle->instance]);
377 
378     return kStatus_HAL_UartSuccess;
379 }
380 
HAL_UartReceiveBlocking(hal_uart_handle_t handle,uint8_t * data,size_t length)381 hal_uart_status_t HAL_UartReceiveBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length)
382 {
383     hal_uart_state_t *uartHandle;
384     status_t status;
385     assert(handle);
386     assert(data);
387     assert(length);
388 
389     uartHandle = (hal_uart_state_t *)handle;
390 
391 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
392     if (NULL != uartHandle->rx.buffer)
393     {
394         return kStatus_HAL_UartRxBusy;
395     }
396 #endif
397 
398     status = USART_ReadBlocking(s_UsartAdapterBase[uartHandle->instance], data, length);
399 
400     return HAL_UartGetStatus(status);
401 }
402 
HAL_UartSendBlocking(hal_uart_handle_t handle,const uint8_t * data,size_t length)403 hal_uart_status_t HAL_UartSendBlocking(hal_uart_handle_t handle, const uint8_t *data, size_t length)
404 {
405     hal_uart_state_t *uartHandle;
406     assert(handle);
407     assert(data);
408     assert(length);
409 
410     uartHandle = (hal_uart_state_t *)handle;
411 
412 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
413     if (NULL != uartHandle->tx.buffer)
414     {
415         return kStatus_HAL_UartTxBusy;
416     }
417 #endif
418 
419     (void)USART_WriteBlocking(s_UsartAdapterBase[uartHandle->instance], data, length);
420 
421     return kStatus_HAL_UartSuccess;
422 }
423 
HAL_UartEnterLowpower(hal_uart_handle_t handle)424 hal_uart_status_t HAL_UartEnterLowpower(hal_uart_handle_t handle)
425 {
426     assert(handle);
427 
428     return kStatus_HAL_UartSuccess;
429 }
430 
HAL_UartExitLowpower(hal_uart_handle_t handle)431 hal_uart_status_t HAL_UartExitLowpower(hal_uart_handle_t handle)
432 {
433 #if (defined(HAL_UART_ADAPTER_LOWPOWER) && (HAL_UART_ADAPTER_LOWPOWER > 0U))
434     hal_uart_state_t *uartHandle;
435     assert(handle);
436 
437     uartHandle = (hal_uart_state_t *)handle;
438 
439     (void)HAL_UartInit(handle, &uartHandle->config);
440 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
441     USART_EnableInterrupts(s_UsartAdapterBase[uartHandle->instance], USART_FIFOINTENSET_RXLVL_MASK);
442 #endif
443 #endif
444     return kStatus_HAL_UartSuccess;
445 }
446 
447 #if (defined(UART_ADAPTER_NON_BLOCKING_MODE) && (UART_ADAPTER_NON_BLOCKING_MODE > 0U))
448 
449 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
450 
HAL_UartTransferInstallCallback(hal_uart_handle_t handle,hal_uart_transfer_callback_t callback,void * callbackParam)451 hal_uart_status_t HAL_UartTransferInstallCallback(hal_uart_handle_t handle,
452                                                   hal_uart_transfer_callback_t callback,
453                                                   void *callbackParam)
454 {
455     hal_uart_state_t *uartHandle;
456 
457     assert(handle);
458     assert(0U != HAL_UART_TRANSFER_MODE);
459 
460     uartHandle = (hal_uart_state_t *)handle;
461 
462     uartHandle->callbackParam = callbackParam;
463     uartHandle->callback      = callback;
464 
465     return kStatus_HAL_UartSuccess;
466 }
467 
HAL_UartTransferReceiveNonBlocking(hal_uart_handle_t handle,hal_uart_transfer_t * transfer)468 hal_uart_status_t HAL_UartTransferReceiveNonBlocking(hal_uart_handle_t handle, hal_uart_transfer_t *transfer)
469 {
470     hal_uart_state_t *uartHandle;
471     status_t status;
472     assert(handle);
473     assert(transfer);
474     assert(0U != HAL_UART_TRANSFER_MODE);
475 
476     uartHandle = (hal_uart_state_t *)handle;
477 
478     status = USART_TransferReceiveNonBlocking(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle,
479                                               (usart_transfer_t *)transfer, NULL);
480 
481     return HAL_UartGetStatus(status);
482 }
483 
HAL_UartTransferSendNonBlocking(hal_uart_handle_t handle,hal_uart_transfer_t * transfer)484 hal_uart_status_t HAL_UartTransferSendNonBlocking(hal_uart_handle_t handle, hal_uart_transfer_t *transfer)
485 {
486     hal_uart_state_t *uartHandle;
487     status_t status;
488     assert(handle);
489     assert(transfer);
490     assert(0U != HAL_UART_TRANSFER_MODE);
491 
492     uartHandle = (hal_uart_state_t *)handle;
493 
494     status = USART_TransferSendNonBlocking(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle,
495                                            (usart_transfer_t *)transfer);
496 
497     return HAL_UartGetStatus(status);
498 }
499 
HAL_UartTransferGetReceiveCount(hal_uart_handle_t handle,uint32_t * count)500 hal_uart_status_t HAL_UartTransferGetReceiveCount(hal_uart_handle_t handle, uint32_t *count)
501 {
502     hal_uart_state_t *uartHandle;
503     status_t status;
504     assert(handle);
505     assert(count);
506     assert(0U != HAL_UART_TRANSFER_MODE);
507 
508     uartHandle = (hal_uart_state_t *)handle;
509 
510     status =
511         USART_TransferGetReceiveCount(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle, count);
512 
513     return HAL_UartGetStatus(status);
514 }
515 
HAL_UartTransferGetSendCount(hal_uart_handle_t handle,uint32_t * count)516 hal_uart_status_t HAL_UartTransferGetSendCount(hal_uart_handle_t handle, uint32_t *count)
517 {
518     hal_uart_state_t *uartHandle;
519     status_t status;
520     assert(handle);
521     assert(count);
522     assert(0U != HAL_UART_TRANSFER_MODE);
523 
524     uartHandle = (hal_uart_state_t *)handle;
525 
526     status = USART_TransferGetSendCount(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle, count);
527 
528     return HAL_UartGetStatus(status);
529 }
530 
HAL_UartTransferAbortReceive(hal_uart_handle_t handle)531 hal_uart_status_t HAL_UartTransferAbortReceive(hal_uart_handle_t handle)
532 {
533     hal_uart_state_t *uartHandle;
534     assert(handle);
535     assert(0U != HAL_UART_TRANSFER_MODE);
536 
537     uartHandle = (hal_uart_state_t *)handle;
538 
539     USART_TransferAbortReceive(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle);
540 
541     return kStatus_HAL_UartSuccess;
542 }
543 
HAL_UartTransferAbortSend(hal_uart_handle_t handle)544 hal_uart_status_t HAL_UartTransferAbortSend(hal_uart_handle_t handle)
545 {
546     hal_uart_state_t *uartHandle;
547     assert(handle);
548     assert(0U != HAL_UART_TRANSFER_MODE);
549 
550     uartHandle = (hal_uart_state_t *)handle;
551 
552     USART_TransferAbortSend(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle);
553 
554     return kStatus_HAL_UartSuccess;
555 }
556 
557 #else
558 
559 /* None transactional API with non-blocking mode. */
HAL_UartInstallCallback(hal_uart_handle_t handle,hal_uart_transfer_callback_t callback,void * callbackParam)560 hal_uart_status_t HAL_UartInstallCallback(hal_uart_handle_t handle,
561                                           hal_uart_transfer_callback_t callback,
562                                           void *callbackParam)
563 {
564     hal_uart_state_t *uartHandle;
565 
566     assert(handle);
567     assert(0U == HAL_UART_TRANSFER_MODE);
568 
569     uartHandle = (hal_uart_state_t *)handle;
570 
571     uartHandle->callbackParam = callbackParam;
572     uartHandle->callback      = callback;
573 
574     return kStatus_HAL_UartSuccess;
575 }
576 
HAL_UartReceiveNonBlocking(hal_uart_handle_t handle,uint8_t * data,size_t length)577 hal_uart_status_t HAL_UartReceiveNonBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length)
578 {
579     hal_uart_state_t *uartHandle;
580     assert(handle);
581     assert(data);
582     assert(length);
583     assert(0U == HAL_UART_TRANSFER_MODE);
584 
585     uartHandle = (hal_uart_state_t *)handle;
586 
587     if (NULL != uartHandle->rx.buffer)
588     {
589         return kStatus_HAL_UartRxBusy;
590     }
591 
592     uartHandle->rx.bufferLength = length;
593     uartHandle->rx.bufferSofar  = 0;
594     uartHandle->rx.buffer       = data;
595     USART_EnableInterrupts(s_UsartAdapterBase[uartHandle->instance], USART_FIFOINTENSET_RXLVL_MASK);
596     return kStatus_HAL_UartSuccess;
597 }
598 
HAL_UartSendNonBlocking(hal_uart_handle_t handle,uint8_t * data,size_t length)599 hal_uart_status_t HAL_UartSendNonBlocking(hal_uart_handle_t handle, uint8_t *data, size_t length)
600 {
601     hal_uart_state_t *uartHandle;
602     assert(handle);
603     assert(data);
604     assert(length);
605     assert(0U == HAL_UART_TRANSFER_MODE);
606 
607     uartHandle = (hal_uart_state_t *)handle;
608 
609     if (NULL != uartHandle->tx.buffer)
610     {
611         return kStatus_HAL_UartTxBusy;
612     }
613     uartHandle->tx.bufferLength = length;
614     uartHandle->tx.bufferSofar  = 0;
615     uartHandle->tx.buffer       = (volatile uint8_t *)data;
616     USART_EnableInterrupts(s_UsartAdapterBase[uartHandle->instance], USART_FIFOINTENSET_TXLVL_MASK);
617     return kStatus_HAL_UartSuccess;
618 }
619 
HAL_UartGetReceiveCount(hal_uart_handle_t handle,uint32_t * reCount)620 hal_uart_status_t HAL_UartGetReceiveCount(hal_uart_handle_t handle, uint32_t *reCount)
621 {
622     hal_uart_state_t *uartHandle;
623     assert(handle);
624     assert(reCount);
625     assert(0U == HAL_UART_TRANSFER_MODE);
626 
627     uartHandle = (hal_uart_state_t *)handle;
628 
629     if (NULL != uartHandle->rx.buffer)
630     {
631         *reCount = uartHandle->rx.bufferSofar;
632         return kStatus_HAL_UartSuccess;
633     }
634     return kStatus_HAL_UartError;
635 }
636 
HAL_UartGetSendCount(hal_uart_handle_t handle,uint32_t * seCount)637 hal_uart_status_t HAL_UartGetSendCount(hal_uart_handle_t handle, uint32_t *seCount)
638 {
639     hal_uart_state_t *uartHandle;
640     assert(handle);
641     assert(seCount);
642     assert(0U == HAL_UART_TRANSFER_MODE);
643 
644     uartHandle = (hal_uart_state_t *)handle;
645 
646     if (NULL != uartHandle->tx.buffer)
647     {
648         *seCount = uartHandle->tx.bufferSofar;
649         return kStatus_HAL_UartSuccess;
650     }
651     return kStatus_HAL_UartError;
652 }
653 
HAL_UartAbortReceive(hal_uart_handle_t handle)654 hal_uart_status_t HAL_UartAbortReceive(hal_uart_handle_t handle)
655 {
656     hal_uart_state_t *uartHandle;
657     assert(handle);
658     assert(0U == HAL_UART_TRANSFER_MODE);
659 
660     uartHandle = (hal_uart_state_t *)handle;
661 
662     if (NULL != uartHandle->rx.buffer)
663     {
664         USART_DisableInterrupts(s_UsartAdapterBase[uartHandle->instance],
665                                 USART_FIFOINTENCLR_RXLVL_MASK | USART_FIFOINTENCLR_RXERR_MASK);
666         uartHandle->rx.buffer = NULL;
667     }
668 
669     return kStatus_HAL_UartSuccess;
670 }
671 
HAL_UartAbortSend(hal_uart_handle_t handle)672 hal_uart_status_t HAL_UartAbortSend(hal_uart_handle_t handle)
673 {
674     hal_uart_state_t *uartHandle;
675     assert(handle);
676     assert(0U == HAL_UART_TRANSFER_MODE);
677 
678     uartHandle = (hal_uart_state_t *)handle;
679 
680     if (NULL != uartHandle->tx.buffer)
681     {
682         USART_DisableInterrupts(s_UsartAdapterBase[uartHandle->instance], USART_FIFOINTENCLR_TXLVL_MASK);
683         uartHandle->tx.buffer = NULL;
684     }
685 
686     return kStatus_HAL_UartSuccess;
687 }
688 
689 #endif
690 
691 #if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
692 
HAL_UartIsrFunction(hal_uart_handle_t handle)693 void HAL_UartIsrFunction(hal_uart_handle_t handle)
694 {
695     hal_uart_state_t *uartHandle;
696     assert(handle);
697     assert(0U != HAL_UART_TRANSFER_MODE);
698 
699     uartHandle = (hal_uart_state_t *)handle;
700 
701 #if 0
702     DisableIRQ(s_UsartIRQ[uartHandle->instance]);
703 #endif
704     USART_TransferHandleIRQ(s_UsartAdapterBase[uartHandle->instance], &uartHandle->hardwareHandle);
705 #if 0
706     NVIC_SetPriority((IRQn_Type)s_UsartIRQ[uartHandle->instance], HAL_UART_ISR_PRIORITY);
707     EnableIRQ(s_UsartIRQ[uartHandle->instance]);
708 #endif
709 }
710 
711 #else
712 
HAL_UartIsrFunction(hal_uart_handle_t handle)713 void HAL_UartIsrFunction(hal_uart_handle_t handle)
714 {
715     hal_uart_state_t *uartHandle;
716     assert(handle);
717     assert(0U == HAL_UART_TRANSFER_MODE);
718 
719     uartHandle = (hal_uart_state_t *)handle;
720 
721 #if 0
722     DisableIRQ(s_UsartIRQ[uartHandle->instance]);
723 #endif
724     HAL_UartInterruptHandle(s_UsartAdapterBase[uartHandle->instance], (void *)uartHandle);
725 #if 0
726     NVIC_SetPriority((IRQn_Type)s_UsartIRQ[uartHandle->instance], HAL_UART_ISR_PRIORITY);
727     EnableIRQ(s_UsartIRQ[uartHandle->instance]);
728 #endif
729 }
730 
731 #endif
732 
733 #endif
734 
735 #if (defined(HAL_UART_DMA_ENABLE) && (HAL_UART_DMA_ENABLE > 0U))
USART_DMACallbacks(USART_Type * base,usart_dma_handle_t * handle,status_t status,void * userData)736 static void USART_DMACallbacks(USART_Type *base, usart_dma_handle_t *handle, status_t status, void *userData)
737 {
738     hal_uart_dma_state_t *uartDmaHandle;
739     hal_uart_status_t uartStatus = HAL_UartGetStatus(status);
740     hal_dma_callback_msg_t msg;
741     assert(handle);
742 
743     uartDmaHandle = (hal_uart_dma_state_t *)userData;
744 
745     if (NULL != uartDmaHandle->dma_callback)
746     {
747         if (kStatus_HAL_UartTxIdle == uartStatus)
748         {
749             msg.status                   = kStatus_HAL_UartDmaTxIdle;
750             msg.data                     = uartDmaHandle->dma_tx.buffer;
751             msg.dataSize                 = uartDmaHandle->dma_tx.bufferLength;
752             uartDmaHandle->dma_tx.buffer = NULL;
753         }
754         else if (kStatus_HAL_UartRxIdle == uartStatus)
755         {
756             msg.status                   = kStatus_HAL_UartDmaRxIdle;
757             msg.data                     = uartDmaHandle->dma_rx.buffer;
758             msg.dataSize                 = uartDmaHandle->dma_rx.bufferLength;
759             uartDmaHandle->dma_rx.buffer = NULL;
760         }
761 
762         uartDmaHandle->dma_callback(uartDmaHandle, &msg, uartDmaHandle->dma_callback_param);
763     }
764 }
765 
TimeoutTimer_Callbcak(void * param)766 static void TimeoutTimer_Callbcak(void *param)
767 {
768     hal_uart_dma_list_t *uartDmaHandleList;
769     hal_uart_dma_state_t *uartDmaHandle;
770     hal_dma_callback_msg_t msg;
771     uint32_t newReceived = 0U;
772 
773     uartDmaHandleList = &s_dmaHandleList;
774     uartDmaHandle     = uartDmaHandleList->dma_list;
775 
776     while (NULL != uartDmaHandle)
777     {
778         if ((NULL != uartDmaHandle->dma_rx.buffer) && (false == uartDmaHandle->dma_rx.receiveAll))
779         {
780             /* HAL_UartDMAGetReceiveCount(uartDmaHandle, &msg.dataSize); */
781             USART_TransferGetReceiveCountDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle,
782                                              &msg.dataSize);
783             newReceived                       = msg.dataSize - uartDmaHandle->dma_rx.bufferSofar;
784             uartDmaHandle->dma_rx.bufferSofar = msg.dataSize;
785 
786             /* 1, If it is in idle state. */
787             if ((0U == newReceived) && (0U < uartDmaHandle->dma_rx.bufferSofar))
788             {
789                 uartDmaHandle->dma_rx.timeout++;
790                 if (uartDmaHandle->dma_rx.timeout >= HAL_UART_DMA_IDLELINE_TIMEOUT)
791                 {
792                     /* HAL_UartDMAAbortReceive(uartDmaHandle); */
793                     USART_TransferAbortReceiveDMA(s_UsartAdapterBase[uartDmaHandle->instance],
794                                                   &uartDmaHandle->dmaHandle);
795                     msg.data                     = uartDmaHandle->dma_rx.buffer;
796                     msg.status                   = kStatus_HAL_UartDmaIdleline;
797                     uartDmaHandle->dma_rx.buffer = NULL;
798                     uartDmaHandle->dma_callback(uartDmaHandle, &msg, uartDmaHandle->dma_callback_param);
799                 }
800             }
801             /* 2, If got new data again. */
802             if ((0U < newReceived) && (0U < uartDmaHandle->dma_rx.bufferSofar))
803             {
804                 uartDmaHandle->dma_rx.timeout = 0U;
805             }
806         }
807 
808         uartDmaHandle = uartDmaHandle->next;
809     }
810 }
811 
HAL_UartDMAInit(hal_uart_handle_t handle,hal_uart_dma_handle_t dmaHandle,hal_uart_dma_config_t * dmaConfig)812 hal_uart_dma_status_t HAL_UartDMAInit(hal_uart_handle_t handle,
813                                       hal_uart_dma_handle_t dmaHandle,
814                                       hal_uart_dma_config_t *dmaConfig)
815 {
816     hal_uart_state_t *uartHandle;
817     hal_uart_dma_state_t *uartDmaHandle;
818 
819     assert(handle);
820     assert(dmaHandle);
821 
822     /* DMA init process. */
823     uartHandle    = (hal_uart_state_t *)handle;
824     uartDmaHandle = (hal_uart_dma_state_t *)dmaHandle;
825 
826     uartHandle->dmaHandle = uartDmaHandle;
827 
828     uartDmaHandle->instance = dmaConfig->uart_instance;
829 
830     DMA_Type *dmaBases[] = DMA_BASE_PTRS;
831     DMA_EnableChannel(dmaBases[dmaConfig->dma_instance], dmaConfig->tx_channel);
832     DMA_EnableChannel(dmaBases[dmaConfig->dma_instance], dmaConfig->rx_channel);
833 
834     DMA_CreateHandle(&uartDmaHandle->txDmaHandle, dmaBases[dmaConfig->dma_instance], dmaConfig->tx_channel);
835     DMA_CreateHandle(&uartDmaHandle->rxDmaHandle, dmaBases[dmaConfig->dma_instance], dmaConfig->rx_channel);
836 
837     /* Timeout timer init. */
838     if (0U == s_dmaHandleList.activeCount)
839     {
840         s_dmaHandleList.dma_list = uartDmaHandle;
841         uartDmaHandle->next      = NULL;
842         s_dmaHandleList.activeCount++;
843 
844         timer_status_t timerStatus;
845         timerStatus = TM_Open((timer_handle_t)s_dmaHandleList.timerManagerHandle);
846         assert(kStatus_TimerSuccess == timerStatus);
847 
848         timerStatus =
849             TM_InstallCallback((timer_handle_t)s_dmaHandleList.timerManagerHandle, TimeoutTimer_Callbcak, NULL);
850         assert(kStatus_TimerSuccess == timerStatus);
851 
852         (void)TM_Start((timer_handle_t)s_dmaHandleList.timerManagerHandle, (uint8_t)kTimerModeIntervalTimer, 1);
853 
854         (void)timerStatus;
855     }
856     else
857     {
858         uartDmaHandle->next      = s_dmaHandleList.dma_list;
859         s_dmaHandleList.dma_list = uartDmaHandle;
860     }
861 
862     return kStatus_HAL_UartDmaSuccess;
863 }
864 
HAL_UartDMADeinit(hal_uart_handle_t handle)865 hal_uart_dma_status_t HAL_UartDMADeinit(hal_uart_handle_t handle)
866 {
867     hal_uart_state_t *uartHandle;
868     hal_uart_dma_state_t *uartDmaHandle;
869     hal_uart_dma_state_t *prev;
870     hal_uart_dma_state_t *curr;
871 
872     assert(handle);
873 
874     uartHandle    = (hal_uart_state_t *)handle;
875     uartDmaHandle = uartHandle->dmaHandle;
876 
877     uartHandle->dmaHandle = NULL;
878 
879     assert(uartDmaHandle);
880 
881     /* Abort rx/tx */
882     /* Here we should not abort before create transfer handle. */
883     if (NULL != uartDmaHandle->dmaHandle.txDmaHandle)
884     {
885         USART_TransferAbortSendDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle);
886     }
887     if (NULL != uartDmaHandle->dmaHandle.rxDmaHandle)
888     {
889         USART_TransferAbortReceiveDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle);
890     }
891 
892     /* Disable rx/tx channels */
893     /* Here we should not disable before create transfer handle. */
894     if (NULL != uartDmaHandle->dmaHandle.txDmaHandle)
895     {
896         DMA_DisableChannel(uartDmaHandle->txDmaHandle.base, uartDmaHandle->txDmaHandle.channel);
897     }
898     if (NULL != uartDmaHandle->dmaHandle.rxDmaHandle)
899     {
900         DMA_DisableChannel(uartDmaHandle->rxDmaHandle.base, uartDmaHandle->rxDmaHandle.channel);
901     }
902 
903     /* Remove handle from list */
904     prev = NULL;
905     curr = s_dmaHandleList.dma_list;
906     while (curr != NULL)
907     {
908         if (curr == uartDmaHandle)
909         {
910             /* 1, if it is the first one */
911             if (prev == NULL)
912             {
913                 s_dmaHandleList.dma_list = curr->next;
914             }
915             /* 2, if it is the last one */
916             else if (curr->next == NULL)
917             {
918                 prev->next = NULL;
919             }
920             /* 3, if it is in the middle */
921             else
922             {
923                 prev->next = curr->next;
924             }
925             break;
926         }
927 
928         prev = curr;
929         curr = curr->next;
930     }
931 
932     /* Reset all handle data. */
933     (void)memset(uartDmaHandle, 0, sizeof(hal_uart_dma_state_t));
934 
935     s_dmaHandleList.activeCount = (s_dmaHandleList.activeCount > 0) ? (s_dmaHandleList.activeCount - 1) : 0;
936     if (0 == s_dmaHandleList.activeCount)
937     {
938         (void)TM_Close((timer_handle_t)s_dmaHandleList.timerManagerHandle);
939     }
940 
941     return kStatus_HAL_UartDmaSuccess;
942 }
943 
HAL_UartDMATransferInstallCallback(hal_uart_handle_t handle,hal_uart_dma_transfer_callback_t callback,void * callbackParam)944 hal_uart_dma_status_t HAL_UartDMATransferInstallCallback(hal_uart_handle_t handle,
945                                                          hal_uart_dma_transfer_callback_t callback,
946                                                          void *callbackParam)
947 {
948     hal_uart_state_t *uartHandle;
949     hal_uart_dma_state_t *uartDmaHandle;
950 
951     assert(handle);
952 
953     uartHandle    = (hal_uart_state_t *)handle;
954     uartDmaHandle = uartHandle->dmaHandle;
955 
956     assert(uartDmaHandle);
957 
958     uartDmaHandle->dma_callback       = callback;
959     uartDmaHandle->dma_callback_param = callbackParam;
960 
961     USART_TransferCreateHandleDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle,
962                                   USART_DMACallbacks, uartDmaHandle, &uartDmaHandle->txDmaHandle,
963                                   &uartDmaHandle->rxDmaHandle);
964 
965     return kStatus_HAL_UartDmaSuccess;
966 }
967 
HAL_UartDMATransferReceive(hal_uart_handle_t handle,uint8_t * data,size_t length,bool receiveAll)968 hal_uart_dma_status_t HAL_UartDMATransferReceive(hal_uart_handle_t handle,
969                                                  uint8_t *data,
970                                                  size_t length,
971                                                  bool receiveAll)
972 {
973     hal_uart_state_t *uartHandle;
974     hal_uart_dma_state_t *uartDmaHandle;
975     usart_transfer_t xfer;
976 
977     assert(handle);
978     assert(data);
979 
980     uartHandle    = (hal_uart_state_t *)handle;
981     uartDmaHandle = uartHandle->dmaHandle;
982 
983     assert(uartDmaHandle);
984 
985     if (NULL == uartDmaHandle->dma_rx.buffer)
986     {
987         uartDmaHandle->dma_rx.buffer       = data;
988         uartDmaHandle->dma_rx.bufferLength = length;
989         uartDmaHandle->dma_rx.timeout      = 0U;
990         uartDmaHandle->dma_rx.receiveAll   = receiveAll;
991     }
992     else
993     {
994         /* Already in reading process. */
995         return kStatus_HAL_UartDmaRxBusy;
996     }
997 
998     xfer.data     = data;
999     xfer.dataSize = length;
1000 
1001     USART_TransferReceiveDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle, &xfer);
1002 
1003     return kStatus_HAL_UartDmaSuccess;
1004 }
1005 
HAL_UartDMATransferSend(hal_uart_handle_t handle,uint8_t * data,size_t length)1006 hal_uart_dma_status_t HAL_UartDMATransferSend(hal_uart_handle_t handle, uint8_t *data, size_t length)
1007 {
1008     hal_uart_state_t *uartHandle;
1009     hal_uart_dma_state_t *uartDmaHandle;
1010     usart_transfer_t xfer;
1011 
1012     assert(handle);
1013     assert(data);
1014 
1015     uartHandle    = (hal_uart_state_t *)handle;
1016     uartDmaHandle = uartHandle->dmaHandle;
1017 
1018     assert(uartDmaHandle);
1019 
1020     if (NULL == uartDmaHandle->dma_tx.buffer)
1021     {
1022         uartDmaHandle->dma_tx.buffer       = data;
1023         uartDmaHandle->dma_tx.bufferLength = length;
1024         uartDmaHandle->dma_tx.bufferSofar  = 0U;
1025         uartDmaHandle->dma_tx.timeout      = 0U;
1026     }
1027     else
1028     {
1029         /* Already in writing process. */
1030         return kStatus_HAL_UartDmaTxBusy;
1031     }
1032 
1033     xfer.data     = data;
1034     xfer.dataSize = length;
1035 
1036     USART_TransferSendDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle, &xfer);
1037 
1038     return kStatus_HAL_UartDmaSuccess;
1039 }
1040 
HAL_UartDMAGetReceiveCount(hal_uart_handle_t handle,uint32_t * reCount)1041 hal_uart_dma_status_t HAL_UartDMAGetReceiveCount(hal_uart_handle_t handle, uint32_t *reCount)
1042 {
1043     hal_uart_state_t *uartHandle;
1044     hal_uart_dma_state_t *uartDmaHandle;
1045 
1046     assert(handle);
1047 
1048     uartHandle    = (hal_uart_state_t *)handle;
1049     uartDmaHandle = uartHandle->dmaHandle;
1050 
1051     assert(uartDmaHandle);
1052 
1053     if (kStatus_Success != USART_TransferGetReceiveCountDMA(s_UsartAdapterBase[uartDmaHandle->instance],
1054                                                             &uartDmaHandle->dmaHandle, reCount))
1055     {
1056         return kStatus_HAL_UartDmaError;
1057     }
1058 
1059     return kStatus_HAL_UartDmaSuccess;
1060 }
1061 
HAL_UartDMAGetSendCount(hal_uart_handle_t handle,uint32_t * seCount)1062 hal_uart_dma_status_t HAL_UartDMAGetSendCount(hal_uart_handle_t handle, uint32_t *seCount)
1063 {
1064     /* No get send count API */
1065     return kStatus_HAL_UartDmaError;
1066 }
1067 
HAL_UartDMAAbortReceive(hal_uart_handle_t handle)1068 hal_uart_dma_status_t HAL_UartDMAAbortReceive(hal_uart_handle_t handle)
1069 {
1070     hal_uart_state_t *uartHandle;
1071     hal_uart_dma_state_t *uartDmaHandle;
1072 
1073     assert(handle);
1074 
1075     uartHandle    = (hal_uart_state_t *)handle;
1076     uartDmaHandle = uartHandle->dmaHandle;
1077 
1078     assert(uartDmaHandle);
1079 
1080     USART_TransferAbortReceiveDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle);
1081 
1082     return kStatus_HAL_UartDmaSuccess;
1083 }
1084 
HAL_UartDMAAbortSend(hal_uart_handle_t handle)1085 hal_uart_dma_status_t HAL_UartDMAAbortSend(hal_uart_handle_t handle)
1086 {
1087     hal_uart_state_t *uartHandle;
1088     hal_uart_dma_state_t *uartDmaHandle;
1089 
1090     assert(handle);
1091 
1092     uartHandle    = (hal_uart_state_t *)handle;
1093     uartDmaHandle = uartHandle->dmaHandle;
1094 
1095     assert(uartDmaHandle);
1096 
1097     USART_TransferAbortSendDMA(s_UsartAdapterBase[uartDmaHandle->instance], &uartDmaHandle->dmaHandle);
1098 
1099     return kStatus_HAL_UartDmaSuccess;
1100 }
1101 #endif /* HAL_UART_DMA_ENABLE */
1102