1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2022 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #include "fsl_lpuart_dma.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 
14 /* Component ID definition, used by tools. */
15 #ifndef FSL_COMPONENT_ID
16 #define FSL_COMPONENT_ID "platform.drivers.lpuart_dma"
17 #endif
18 
19 /*<! Structure definition for lpuart_dma_handle_t. The structure is private. */
20 typedef struct _lpuart_dma_private_handle
21 {
22     LPUART_Type *base;
23     lpuart_dma_handle_t *handle;
24 } lpuart_dma_private_handle_t;
25 
26 /* LPUART DMA transfer handle. */
27 enum
28 {
29     kLPUART_TxIdle, /* TX idle. */
30     kLPUART_TxBusy, /* TX busy. */
31     kLPUART_RxIdle, /* RX idle. */
32     kLPUART_RxBusy  /* RX busy. */
33 };
34 
35 /*******************************************************************************
36  * Variables
37  ******************************************************************************/
38 
39 /* Array of LPUART handle. */
40 #if (defined(LPUART12))
41 #define LPUART_HANDLE_ARRAY_SIZE 13
42 #else /* LPUART12 */
43 #if (defined(LPUART11))
44 #define LPUART_HANDLE_ARRAY_SIZE 12
45 #else /* LPUART11 */
46 #if (defined(LPUART10))
47 #define LPUART_HANDLE_ARRAY_SIZE 11
48 #else /* LPUART10 */
49 #if (defined(LPUART9))
50 #define LPUART_HANDLE_ARRAY_SIZE 10
51 #else /* LPUART9 */
52 #if (defined(LPUART8))
53 #define LPUART_HANDLE_ARRAY_SIZE 9
54 #else /* LPUART8 */
55 #if (defined(LPUART7))
56 #define LPUART_HANDLE_ARRAY_SIZE 8
57 #else /* LPUART7 */
58 #if (defined(LPUART6))
59 #define LPUART_HANDLE_ARRAY_SIZE 7
60 #else /* LPUART6 */
61 #if (defined(LPUART5))
62 #define LPUART_HANDLE_ARRAY_SIZE 6
63 #else /* LPUART5 */
64 #if (defined(LPUART4))
65 #define LPUART_HANDLE_ARRAY_SIZE 5
66 #else /* LPUART4 */
67 #if (defined(LPUART3))
68 #define LPUART_HANDLE_ARRAY_SIZE 4
69 #else /* LPUART3 */
70 #if (defined(LPUART2))
71 #define LPUART_HANDLE_ARRAY_SIZE 3
72 #else /* LPUART2 */
73 #if (defined(LPUART1))
74 #define LPUART_HANDLE_ARRAY_SIZE 2
75 #else /* LPUART1 */
76 #if (defined(LPUART0))
77 #define LPUART_HANDLE_ARRAY_SIZE 1
78 #else /* LPUART0 */
79 #define LPUART_HANDLE_ARRAY_SIZE FSL_FEATURE_SOC_LPUART_COUNT
80 #endif /* LPUART 0 */
81 #endif /* LPUART 1 */
82 #endif /* LPUART 2 */
83 #endif /* LPUART 3 */
84 #endif /* LPUART 4 */
85 #endif /* LPUART 5 */
86 #endif /* LPUART 6 */
87 #endif /* LPUART 7 */
88 #endif /* LPUART 8 */
89 #endif /* LPUART 9 */
90 #endif /* LPUART 10 */
91 #endif /* LPUART 11 */
92 #endif /* LPUART 12 */
93 
94 /*<! Private handle only used for internally. */
95 static lpuart_dma_private_handle_t s_dmaPrivateHandle[LPUART_HANDLE_ARRAY_SIZE];
96 
97 /*******************************************************************************
98  * Prototypes
99  ******************************************************************************/
100 
101 /*!
102  * @brief LPUART DMA send finished callback function.
103  *
104  * This function is called when LPUART DMA send finished. It disables the LPUART
105  * TX DMA request and sends @ref kStatus_LPUART_TxIdle to LPUART callback.
106  *
107  * @param handle The DMA handle.
108  * @param param Callback function parameter.
109  */
110 static void LPUART_TransferSendDMACallback(dma_handle_t *handle, void *param);
111 
112 /*!
113  * @brief LPUART DMA receive finished callback function.
114  *
115  * This function is called when LPUART DMA receive finished. It disables the LPUART
116  * RX DMA request and sends @ref kStatus_LPUART_RxIdle to LPUART callback.
117  *
118  * @param handle The DMA handle.
119  * @param param Callback function parameter.
120  */
121 static void LPUART_TransferReceiveDMACallback(dma_handle_t *handle, void *param);
122 
123 /*******************************************************************************
124  * Code
125  ******************************************************************************/
126 
LPUART_TransferSendDMACallback(dma_handle_t * handle,void * param)127 static void LPUART_TransferSendDMACallback(dma_handle_t *handle, void *param)
128 {
129     assert(handle != NULL);
130     assert(param != NULL);
131 
132     lpuart_dma_private_handle_t *lpuartPrivateHandle = (lpuart_dma_private_handle_t *)param;
133 
134     /* Disable LPUART TX DMA. */
135     LPUART_EnableTxDMA(lpuartPrivateHandle->base, false);
136 
137     /* Disable interrupt. */
138     DMA_DisableInterrupts(lpuartPrivateHandle->handle->txDmaHandle->base,
139                           lpuartPrivateHandle->handle->txDmaHandle->channel);
140 
141     /* Enable tx complete interrupt */
142     LPUART_EnableInterrupts(lpuartPrivateHandle->base, (uint32_t)kLPUART_TransmissionCompleteInterruptEnable);
143 }
144 
LPUART_TransferReceiveDMACallback(dma_handle_t * handle,void * param)145 static void LPUART_TransferReceiveDMACallback(dma_handle_t *handle, void *param)
146 {
147     assert(handle != NULL);
148     assert(param != NULL);
149 
150     lpuart_dma_private_handle_t *lpuartPrivateHandle = (lpuart_dma_private_handle_t *)param;
151 
152     /* Disable LPUART RX DMA. */
153     LPUART_EnableRxDMA(lpuartPrivateHandle->base, false);
154 
155     /* Disable interrupt. */
156     DMA_DisableInterrupts(lpuartPrivateHandle->handle->rxDmaHandle->base,
157                           lpuartPrivateHandle->handle->rxDmaHandle->channel);
158 
159     lpuartPrivateHandle->handle->rxState = (uint8_t)kLPUART_RxIdle;
160 
161     if (lpuartPrivateHandle->handle->callback != NULL)
162     {
163         lpuartPrivateHandle->handle->callback(lpuartPrivateHandle->base, lpuartPrivateHandle->handle,
164                                               kStatus_LPUART_RxIdle, lpuartPrivateHandle->handle->userData);
165     }
166 }
167 
168 /*!
169  * brief Initializes the LPUART handle which is used in transactional functions.
170  *
171  * note This function disables all LPUART interrupts.
172  *
173  * param base LPUART peripheral base address.
174  * param handle Pointer to lpuart_dma_handle_t structure.
175  * param callback Callback function.
176  * param userData User data.
177  * param txDmaHandle User-requested DMA handle for TX DMA transfer.
178  * param rxDmaHandle User-requested DMA handle for RX DMA transfer.
179  */
LPUART_TransferCreateHandleDMA(LPUART_Type * base,lpuart_dma_handle_t * handle,lpuart_dma_transfer_callback_t callback,void * userData,dma_handle_t * txDmaHandle,dma_handle_t * rxDmaHandle)180 void LPUART_TransferCreateHandleDMA(LPUART_Type *base,
181                                     lpuart_dma_handle_t *handle,
182                                     lpuart_dma_transfer_callback_t callback,
183                                     void *userData,
184                                     dma_handle_t *txDmaHandle,
185                                     dma_handle_t *rxDmaHandle)
186 {
187     assert(handle != NULL);
188 
189     uint32_t instance = LPUART_GetInstance(base);
190 
191     (void)memset(handle, 0, sizeof(lpuart_dma_handle_t));
192 
193     s_dmaPrivateHandle[instance].base   = base;
194     s_dmaPrivateHandle[instance].handle = handle;
195 
196     handle->rxState = (uint8_t)kLPUART_RxIdle;
197     handle->txState = (uint8_t)kLPUART_TxIdle;
198 
199     handle->callback = callback;
200     handle->userData = userData;
201 
202 #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
203     /* Note:
204        Take care of the RX FIFO, DMA request only assert when received bytes
205        equal or more than RX water mark, there is potential issue if RX water
206        mark larger than 1.
207        For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
208        5 bytes are received. the last byte will be saved in FIFO but not trigger
209        DMA transfer because the water mark is 2.
210      */
211     if (rxDmaHandle != NULL)
212     {
213         base->WATER &= (~LPUART_WATER_RXWATER_MASK);
214     }
215 #endif
216 
217     handle->rxDmaHandle = rxDmaHandle;
218     handle->txDmaHandle = txDmaHandle;
219 
220     /* Save the handle in global variables to support the double weak mechanism. */
221     s_lpuartHandle[instance] = handle;
222     /* Set LPUART_TransferDMAHandleIRQ as DMA IRQ handler */
223     s_lpuartIsr[instance] = LPUART_TransferDMAHandleIRQ;
224     /* Disable all LPUART internal interrupts */
225     LPUART_DisableInterrupts(base, (uint32_t)kLPUART_AllInterruptEnable);
226     /* Enable interrupt in NVIC. */
227 #if defined(FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ) && FSL_FEATURE_LPUART_HAS_SEPARATE_RX_TX_IRQ
228     (void)EnableIRQ(s_lpuartTxIRQ[instance]);
229 #else
230     (void)EnableIRQ(s_lpuartIRQ[instance]);
231 #endif
232 
233     /* Configure TX. */
234     if (txDmaHandle != NULL)
235     {
236         DMA_SetCallback(txDmaHandle, LPUART_TransferSendDMACallback, &s_dmaPrivateHandle[instance]);
237     }
238 
239     /* Configure RX. */
240     if (rxDmaHandle != NULL)
241     {
242         DMA_SetCallback(rxDmaHandle, LPUART_TransferReceiveDMACallback, &s_dmaPrivateHandle[instance]);
243     }
244 }
245 
246 /*!
247  * brief Sends data using DMA.
248  *
249  * This function sends data using DMA. This is a non-blocking function, which returns
250  * right away. When all data is sent, the send callback function is called.
251  *
252  * param base LPUART peripheral base address.
253  * param handle LPUART handle pointer.
254  * param xfer LPUART DMA transfer structure. See #lpuart_transfer_t.
255  * retval kStatus_Success if succeed, others failed.
256  * retval kStatus_LPUART_TxBusy Previous transfer on going.
257  * retval kStatus_InvalidArgument Invalid argument.
258  */
LPUART_TransferSendDMA(LPUART_Type * base,lpuart_dma_handle_t * handle,lpuart_transfer_t * xfer)259 status_t LPUART_TransferSendDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, lpuart_transfer_t *xfer)
260 {
261     assert(handle != NULL);
262     assert(handle->txDmaHandle != NULL);
263     assert(xfer != NULL);
264     assert(xfer->data != NULL);
265     assert(xfer->dataSize != 0U);
266 
267     status_t status;
268     dma_transfer_config_t xferConfig;
269 
270     /* If previous TX not finished. */
271     if ((uint8_t)kLPUART_TxBusy == handle->txState)
272     {
273         status = kStatus_LPUART_TxBusy;
274     }
275     else
276     {
277         handle->txState       = (uint8_t)kLPUART_TxBusy;
278         handle->txDataSizeAll = xfer->dataSize;
279 
280         /* Prepare transfer. */
281         uint32_t address = LPUART_GetDataRegisterAddress(base);
282         DMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint8_t), (uint32_t *)address, sizeof(uint8_t),
283                             xfer->dataSize, kDMA_MemoryToPeripheral);
284 
285         /* Submit transfer. */
286         (void)DMA_SubmitTransfer(handle->txDmaHandle, &xferConfig, (uint32_t)kDMA_EnableInterrupt);
287         DMA_StartTransfer(handle->txDmaHandle);
288 
289         /* Enable LPUART TX DMA. */
290         LPUART_EnableTxDMA(base, true);
291 
292         status = kStatus_Success;
293     }
294 
295     return status;
296 }
297 
298 /*!
299  * brief Receives data using DMA.
300  *
301  * This function receives data using DMA. This is a non-blocking function, which returns
302  * right away. When all data is received, the receive callback function is called.
303  *
304  * param base LPUART peripheral base address.
305  * param handle Pointer to lpuart_dma_handle_t structure.
306  * param xfer LPUART DMA transfer structure. See #lpuart_transfer_t.
307  * retval kStatus_Success if succeed, others failed.
308  * retval kStatus_LPUART_RxBusy Previous transfer on going.
309  * retval kStatus_InvalidArgument Invalid argument.
310  */
LPUART_TransferReceiveDMA(LPUART_Type * base,lpuart_dma_handle_t * handle,lpuart_transfer_t * xfer)311 status_t LPUART_TransferReceiveDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, lpuart_transfer_t *xfer)
312 {
313     assert(handle != NULL);
314     assert(handle->rxDmaHandle != NULL);
315     assert(xfer != NULL);
316     assert(xfer->data != NULL);
317     assert(xfer->dataSize != 0U);
318 
319     status_t status;
320     dma_transfer_config_t xferConfig;
321 
322     /* If previous RX not finished. */
323     if ((uint8_t)kLPUART_RxBusy == handle->rxState)
324     {
325         status = kStatus_LPUART_RxBusy;
326     }
327     else
328     {
329         handle->rxState       = (uint8_t)kLPUART_RxBusy;
330         handle->rxDataSizeAll = xfer->dataSize;
331 
332         /* Prepare transfer. */
333         uint32_t address = LPUART_GetDataRegisterAddress(base);
334         DMA_PrepareTransfer(&xferConfig, (uint32_t *)address, sizeof(uint8_t), xfer->data, sizeof(uint8_t),
335                             xfer->dataSize, kDMA_PeripheralToMemory);
336 
337         /* Submit transfer. */
338         (void)DMA_SubmitTransfer(handle->rxDmaHandle, &xferConfig, (uint32_t)kDMA_EnableInterrupt);
339         DMA_StartTransfer(handle->rxDmaHandle);
340 
341         /* Enable LPUART RX DMA. */
342         LPUART_EnableRxDMA(base, true);
343 
344         status = kStatus_Success;
345     }
346 
347     return status;
348 }
349 
350 /*!
351  * brief Aborts the sent data using DMA.
352  *
353  * This function aborts send data using DMA.
354  *
355  * param base LPUART peripheral base address
356  * param handle Pointer to lpuart_dma_handle_t structure
357  */
LPUART_TransferAbortSendDMA(LPUART_Type * base,lpuart_dma_handle_t * handle)358 void LPUART_TransferAbortSendDMA(LPUART_Type *base, lpuart_dma_handle_t *handle)
359 {
360     assert(handle != NULL);
361     assert(handle->txDmaHandle != NULL);
362 
363     /* Disable LPUART TX DMA. */
364     LPUART_EnableTxDMA(base, false);
365 
366     /* Stop transfer. */
367     DMA_AbortTransfer(handle->txDmaHandle);
368 
369     /* Write DMA->DSR[DONE] to abort transfer and clear status. */
370     DMA_ClearChannelStatusFlags(handle->txDmaHandle->base, handle->txDmaHandle->channel,
371                                 (uint32_t)kDMA_TransactionsDoneFlag);
372 
373     handle->txState = (uint8_t)kLPUART_TxIdle;
374 }
375 
376 /*!
377  * brief Aborts the received data using DMA.
378  *
379  * This function aborts the received data using DMA.
380  *
381  * param base LPUART peripheral base address
382  * param handle Pointer to lpuart_dma_handle_t structure
383  */
LPUART_TransferAbortReceiveDMA(LPUART_Type * base,lpuart_dma_handle_t * handle)384 void LPUART_TransferAbortReceiveDMA(LPUART_Type *base, lpuart_dma_handle_t *handle)
385 {
386     assert(handle != NULL);
387     assert(handle->rxDmaHandle != NULL);
388 
389     /* Disable LPUART RX DMA. */
390     LPUART_EnableRxDMA(base, false);
391 
392     /* Stop transfer. */
393     DMA_AbortTransfer(handle->rxDmaHandle);
394 
395     /* Write DMA->DSR[DONE] to abort transfer and clear status. */
396     DMA_ClearChannelStatusFlags(handle->rxDmaHandle->base, handle->rxDmaHandle->channel,
397                                 (uint32_t)kDMA_TransactionsDoneFlag);
398 
399     handle->rxState = (uint8_t)kLPUART_RxIdle;
400 }
401 
402 /*!
403  * brief Gets the number of bytes written to the LPUART TX register.
404  *
405  * This function gets the number of bytes that have been written to LPUART TX
406  * register by DMA.
407  *
408  * param base LPUART peripheral base address.
409  * param handle LPUART handle pointer.
410  * param count Send bytes count.
411  * retval kStatus_NoTransferInProgress No send in progress.
412  * retval kStatus_InvalidArgument Parameter is invalid.
413  * retval kStatus_Success Get successfully through the parameter \p count;
414  */
LPUART_TransferGetSendCountDMA(LPUART_Type * base,lpuart_dma_handle_t * handle,uint32_t * count)415 status_t LPUART_TransferGetSendCountDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, uint32_t *count)
416 {
417     assert(handle != NULL);
418     assert(handle->txDmaHandle != NULL);
419     assert(count != NULL);
420 
421     if ((uint8_t)kLPUART_TxIdle == handle->txState)
422     {
423         return kStatus_NoTransferInProgress;
424     }
425 
426     *count = handle->txDataSizeAll - DMA_GetRemainingBytes(handle->txDmaHandle->base, handle->txDmaHandle->channel);
427 
428     return kStatus_Success;
429 }
430 
431 /*!
432  * brief Gets the number of received bytes.
433  *
434  * This function gets the number of received bytes.
435  *
436  * param base LPUART peripheral base address.
437  * param handle LPUART handle pointer.
438  * param count Receive bytes count.
439  * retval kStatus_NoTransferInProgress No receive in progress.
440  * retval kStatus_InvalidArgument Parameter is invalid.
441  * retval kStatus_Success Get successfully through the parameter \p count;
442  */
LPUART_TransferGetReceiveCountDMA(LPUART_Type * base,lpuart_dma_handle_t * handle,uint32_t * count)443 status_t LPUART_TransferGetReceiveCountDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, uint32_t *count)
444 {
445     assert(handle != NULL);
446     assert(handle->rxDmaHandle != NULL);
447     assert(count != NULL);
448 
449     if ((uint8_t)kLPUART_RxIdle == handle->rxState)
450     {
451         return kStatus_NoTransferInProgress;
452     }
453 
454     *count = handle->rxDataSizeAll - DMA_GetRemainingBytes(handle->rxDmaHandle->base, handle->rxDmaHandle->channel);
455 
456     return kStatus_Success;
457 }
458 
459 /*!
460  * brief LPUART DMA IRQ handle function.
461  *
462  * This function handles the LPUART tx complete IRQ request and invoke user callback.
463  * note This function is used as default IRQ handler by double weak mechanism.
464  * If user's specific IRQ handler is implemented, make sure this function is invoked in the handler.
465  *
466  * param base LPUART peripheral base address.
467  * param lpuartDmaHandle LPUART handle pointer.
468  */
LPUART_TransferDMAHandleIRQ(LPUART_Type * base,void * lpuartDmaHandle)469 void LPUART_TransferDMAHandleIRQ(LPUART_Type *base, void *lpuartDmaHandle)
470 {
471     assert(lpuartDmaHandle != NULL);
472 
473     if (((uint32_t)kLPUART_TransmissionCompleteFlag & LPUART_GetStatusFlags(base)) != 0U)
474     {
475         lpuart_dma_handle_t *handle = (lpuart_dma_handle_t *)lpuartDmaHandle;
476 
477         /* Disable tx complete interrupt */
478         LPUART_DisableInterrupts(base, (uint32_t)kLPUART_TransmissionCompleteInterruptEnable);
479 
480         handle->txState = (uint8_t)kLPUART_TxIdle;
481 
482         if (handle->callback != NULL)
483         {
484             handle->callback(base, handle, kStatus_LPUART_TxIdle, handle->userData);
485         }
486     }
487 }
488