1 /* ----------------------------------------------------------------------------
\r
2 * SAM Software Package License
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2014, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
31 * \addtogroup usart_dma_module USART xDMA driver
\r
35 * <li> USARTD_Configure() initializes and configures the USART peripheral and xDMA for data transfer.</li>
\r
36 * <li> Configures the parameters for the device corresponding to the cs value by USARTD_ConfigureCS(). </li>
\r
44 * Implementation for the USART with xDMA driver.
\r
49 /*----------------------------------------------------------------------------
\r
51 *----------------------------------------------------------------------------*/
\r
55 /*----------------------------------------------------------------------------
\r
57 *----------------------------------------------------------------------------*/
\r
60 /** xDMA Link List size for usart transmition*/
\r
61 #define DMA_USART_LLI 2
\r
63 /*----------------------------------------------------------------------------
\r
65 *----------------------------------------------------------------------------*/
\r
67 /*----------------------------------------------------------------------------
\r
69 *----------------------------------------------------------------------------*/
\r
71 /*----------------------------------------------------------------------------
\r
73 *----------------------------------------------------------------------------*/
\r
77 * \brief USART xDMA Rx callback
\r
78 * Invoked on USART DMA reception done.
\r
79 * \param channel DMA channel.
\r
80 * \param pArg Pointer to callback argument - Pointer to USARTDma instance.
\r
82 static void USARTD_Rx_Cb(uint32_t channel, UsartDma* pArg)
\r
85 UsartChannel *pUsartdCh = pArg->pRxChannel;
\r
86 if (channel != pUsartdCh->ChNum)
\r
89 // NVIC_ClearPendingIRQ(XDMAC_IRQn);
\r
91 /* Release the DMA channels */
\r
92 XDMAD_FreeChannel(pArg->pXdmad, pUsartdCh->ChNum);
\r
94 /* Invoke the callback associated with the current command */
\r
95 if (pUsartdCh && pUsartdCh->callback) {
\r
96 pUsartdCh->callback(0, pUsartdCh->pArgument);
\r
98 pUsartdCh->Done = 1;
\r
103 * \brief USART xDMA Rx callback
\r
104 * Invoked on USART DMA reception done.
\r
105 * \param channel DMA channel.
\r
106 * \param pArg Pointer to callback argument - Pointer to USARTDma instance.
\r
108 static void USARTD_Tx_Cb(uint32_t channel, UsartDma* pArg)
\r
110 UsartChannel *pUsartdCh = pArg->pTxChannel;
\r
111 if (channel != pUsartdCh->ChNum)
\r
114 // NVIC_ClearPendingIRQ(XDMAC_IRQn);
\r
116 /* Release the DMA channels */
\r
117 XDMAD_FreeChannel(pArg->pXdmad, pUsartdCh->ChNum);
\r
119 /* Invoke the callback associated with the current command */
\r
120 if (pUsartdCh && pUsartdCh->callback) {
\r
121 pUsartdCh->callback(0, pUsartdCh->pArgument);
\r
123 pUsartdCh->Done = 1;
\r
128 * \brief Configure the USART Rx DMA Destination with Linker List mode.
\r
130 * \param UsartChannel Pointer to USART dma channel
\r
131 * \returns 0 if the dma multibuffer configuration successfully; otherwise returns
\r
132 * USARTD_ERROR_XXX.
\r
134 static uint8_t _configureRxLinkList(Usart *pUsartHw, void *pXdmad, UsartChannel *pUsartRx)
\r
136 sXdmadCfg xdmadRxCfg;
\r
139 if ((unsigned int)pUsartHw == (unsigned int)USART0 ) usartId = ID_USART0;
\r
140 if ((unsigned int)pUsartHw == (unsigned int)USART1 ) usartId = ID_USART1;
\r
141 if ((unsigned int)pUsartHw == (unsigned int)USART2 ) usartId = ID_USART2;
\r
143 /* Setup RX Link List */
\r
144 xdmadRxCfg.mbr_ubc = XDMA_UBC_NVIEW_NDV0 |
\r
145 XDMA_UBC_NDE_FETCH_DIS|
\r
146 XDMA_UBC_NDEN_UPDATED |
\r
147 pUsartRx->BuffSize;
\r
148 xdmadRxCfg.mbr_da = (uint32_t)pUsartRx->pBuff;
\r
150 xdmadRxCfg.mbr_sa = (uint32_t)&pUsartHw->US_RHR;
\r
151 xdmadRxCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN |
\r
152 XDMAC_CC_MBSIZE_SINGLE |
\r
153 XDMAC_CC_DSYNC_PER2MEM |
\r
154 XDMAC_CC_CSIZE_CHK_1 |
\r
155 XDMAC_CC_DWIDTH_BYTE |
\r
156 XDMAC_CC_SIF_AHB_IF1 |
\r
157 XDMAC_CC_DIF_AHB_IF0 |
\r
158 XDMAC_CC_SAM_FIXED_AM |
\r
159 XDMAC_CC_DAM_INCREMENTED_AM |
\r
160 XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber( usartId, XDMAD_TRANSFER_RX ));
\r
162 xdmadRxCfg.mbr_bc = 0;
\r
163 xdmadRxCfg.mbr_sus = 0;
\r
164 xdmadRxCfg.mbr_dus =0;
\r
166 if (XDMAD_ConfigureTransfer( pXdmad, pUsartRx->ChNum, &xdmadRxCfg, xdmaCndc, 0))
\r
167 return USARTD_ERROR;
\r
174 * \brief Configure the USART tx DMA source with Linker List mode.
\r
176 * \param UsartChannel Pointer to USART dma channel
\r
177 * \returns 0 if the dma multibuffer configuration successfully; otherwise returns
\r
178 * USARTD_ERROR_XXX.
\r
180 static uint8_t _configureTxLinkList(Usart *pUsartHw, void *pXdmad, UsartChannel *pUsartTx)
\r
182 sXdmadCfg xdmadTxCfg;
\r
185 if ((unsigned int)pUsartHw == (unsigned int)USART0 ) usartId = ID_USART0;
\r
186 if ((unsigned int)pUsartHw == (unsigned int)USART1 ) usartId = ID_USART1;
\r
187 if ((unsigned int)pUsartHw == (unsigned int)USART2 ) usartId = ID_USART2;
\r
188 /* Setup TX Link List */
\r
189 xdmadTxCfg.mbr_ubc = XDMA_UBC_NVIEW_NDV0 |
\r
190 XDMA_UBC_NDE_FETCH_DIS|
\r
191 XDMA_UBC_NSEN_UPDATED | pUsartTx->BuffSize;
\r
193 xdmadTxCfg.mbr_sa = (uint32_t)pUsartTx->pBuff;
\r
194 xdmadTxCfg.mbr_da = (uint32_t)&pUsartHw->US_THR;
\r
195 xdmadTxCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN |
\r
196 XDMAC_CC_MBSIZE_SINGLE |
\r
197 XDMAC_CC_DSYNC_MEM2PER |
\r
198 XDMAC_CC_CSIZE_CHK_1 |
\r
199 XDMAC_CC_DWIDTH_BYTE|
\r
200 XDMAC_CC_SIF_AHB_IF0 |
\r
201 XDMAC_CC_DIF_AHB_IF1 |
\r
202 XDMAC_CC_SAM_INCREMENTED_AM |
\r
203 XDMAC_CC_DAM_FIXED_AM |
\r
204 XDMAC_CC_PERID(XDMAIF_Get_ChannelNumber( usartId, XDMAD_TRANSFER_TX ));
\r
206 xdmadTxCfg.mbr_bc = 0;
\r
207 xdmadTxCfg.mbr_sus = 0;
\r
208 xdmadTxCfg.mbr_dus =0;
\r
211 if (XDMAD_ConfigureTransfer( pXdmad, pUsartTx->ChNum, &xdmadTxCfg, xdmaCndc, 0))
\r
212 return USARTD_ERROR;
\r
217 /*----------------------------------------------------------------------------
\r
218 * Exported functions
\r
219 *----------------------------------------------------------------------------*/
\r
221 * \brief Initializes the USARTDma structure and the corresponding USART & DMA hardware.
\r
223 * The driver will uses DMA channel 0 for RX and DMA channel 1 for TX.
\r
224 * The DMA channels are freed automatically when no USART command processing.
\r
226 * \param pUSARTd Pointer to a UsartDma instance.
\r
227 * \param pUsartHw Associated USART peripheral.
\r
228 * \param usartId USART peripheral identifier.
\r
229 * \param UsartClk USART clock.
\r
230 * \param pXdmad Pointer to a Dmad instance.
\r
232 uint32_t USARTD_Configure( UsartDma *pUsartd ,
\r
235 uint32_t UsartMode,
\r
239 /* Initialize the USART structure */
\r
240 pUsartd->pUsartHw = pUsartHw;
\r
241 pUsartd->usartId = usartId;
\r
242 pUsartd->pRxChannel = 0;
\r
243 pUsartd->pTxChannel = 0;
\r
244 pUsartd->pXdmad = pXdmad;
\r
246 /* Enable the USART Peripheral ,Execute a software reset of the USART, Configure USART in Master Mode*/
\r
247 USART_Configure ( pUsartHw, UsartMode, UsartClk, BOARD_MCK);
\r
249 /* Driver initialize */
\r
250 XDMAD_Initialize( pUsartd->pXdmad, 0 );
\r
251 /* Configure and enable interrupt on RC compare */
\r
252 NVIC_ClearPendingIRQ(XDMAC_IRQn);
\r
253 NVIC_SetPriority( XDMAC_IRQn ,1);
\r
258 * \brief Enables USART Rx DMA channel
\r
260 * The driver will uses DMA channel 0 for RX and DMA channel 1 for TX.
\r
261 * The DMA channels are freed automatically when no USART command processing.
\r
263 * \param pUSARTd Pointer to a UsartDma instance.
\r
264 * \param pUsartHw Associated USART peripheral.
\r
265 * \param usartId USART peripheral identifier.
\r
266 * \param UsartClk USART clock.
\r
267 * \param pDmad Pointer to a Dmad instance.
\r
270 uint32_t USARTD_EnableRxChannels( UsartDma *pUsartd, UsartChannel *pRxCh)
\r
272 Usart *pUsartHw = pUsartd->pUsartHw;
\r
274 // Initialize the callback
\r
275 pUsartd->pRxChannel = pRxCh;
\r
277 /* Enables the USART to receive data. */
\r
278 USART_SetReceiverEnabled ( pUsartHw , 1);
\r
280 XDMAD_FreeChannel( pUsartd->pXdmad, pRxCh->ChNum);
\r
282 /* Allocate a DMA channel for USART0/1 RX. */
\r
283 pRxCh->ChNum = XDMAD_AllocateChannel( pUsartd->pXdmad, pUsartd->usartId, XDMAD_TRANSFER_MEMORY);
\r
284 if ( pRxCh->ChNum == XDMAD_ALLOC_FAILED )
\r
286 return USARTD_ERROR;
\r
289 /* Setup callbacks for USART0/1 RX */
\r
290 XDMAD_SetCallback(pUsartd->pXdmad, pRxCh->ChNum, (XdmadTransferCallback)USARTD_Rx_Cb, pUsartd);
\r
291 if (XDMAD_PrepareChannel( pUsartd->pXdmad, pRxCh->ChNum ))
\r
292 return USARTD_ERROR;
\r
294 /* Enable interrupt */
\r
295 NVIC_EnableIRQ(XDMAC_IRQn);
\r
297 if (_configureRxLinkList(pUsartHw, pUsartd->pXdmad, pRxCh))
\r
298 return USARTD_ERROR_LOCK;
\r
305 uint32_t USARTD_EnableTxChannels( UsartDma *pUsartd, UsartChannel *pTxCh)
\r
307 Usart *pUsartHw = pUsartd->pUsartHw;
\r
309 // Initialize the callback
\r
310 pUsartd->pTxChannel = pTxCh;
\r
312 /* Enables the USART to transfer data. */
\r
313 USART_SetTransmitterEnabled ( pUsartHw , 1);
\r
315 XDMAD_FreeChannel( pUsartd->pXdmad, pTxCh->ChNum);
\r
317 /* Allocate a DMA channel for USART0/1 TX. */
\r
318 pTxCh->ChNum = XDMAD_AllocateChannel( pUsartd->pXdmad, XDMAD_TRANSFER_MEMORY, pUsartd->usartId);
\r
319 if ( pTxCh->ChNum == XDMAD_ALLOC_FAILED )
\r
321 return USARTD_ERROR;
\r
324 /* Setup callbacks for USART0/1 TX */
\r
325 XDMAD_SetCallback(pUsartd->pXdmad, pTxCh->ChNum, (XdmadTransferCallback)USARTD_Tx_Cb, pUsartd);
\r
326 if ( XDMAD_PrepareChannel( pUsartd->pXdmad, pTxCh->ChNum ))
\r
327 return USARTD_ERROR;
\r
329 /* Enable interrupt */
\r
330 NVIC_EnableIRQ(XDMAC_IRQn);
\r
332 if (_configureTxLinkList(pUsartHw, pUsartd->pXdmad, pTxCh))
\r
333 return USARTD_ERROR_LOCK;
\r
339 * \brief Starts a USART master transfer. This is a non blocking function. It will
\r
340 * return as soon as the transfer is started.
\r
342 * \param pUSARTd Pointer to a USARTDma instance.
\r
343 * \param pCommand Pointer to the USART command to execute.
\r
344 * \returns 0 if the transfer has been started successfully; otherwise returns
\r
345 * USARTD_ERROR_LOCK is the driver is in use, or USARTD_ERROR if the command is not
\r
348 uint32_t USARTD_SendData( UsartDma *pUsartd)
\r
351 /* Start DMA 0(RX) && 1(TX) */
\r
352 while(!pUsartd->pTxChannel->Done);
\r
353 if (XDMAD_StartTransfer( pUsartd->pXdmad, pUsartd->pTxChannel->ChNum ))
\r
354 return USARTD_ERROR_LOCK;
\r
355 pUsartd->pTxChannel->Done=0;
\r
361 * \brief Starts a USART master transfer. This is a non blocking function. It will
\r
362 * return as soon as the transfer is started.
\r
364 * \param pUSARTd Pointer to a USARTDma instance.
\r
365 * \param pCommand Pointer to the USART command to execute.
\r
366 * \returns 0 if the transfer has been started successfully; otherwise returns
\r
367 * USARTD_ERROR_LOCK is the driver is in use, or USARTD_ERROR if the command is not
\r
370 uint32_t USARTD_RcvData( UsartDma *pUsartd)
\r
373 while(!pUsartd->pRxChannel->Done);
\r
374 /* Start DMA 0(RX) && 1(TX) */
\r
375 if (XDMAD_StartTransfer( pUsartd->pXdmad, pUsartd->pRxChannel->ChNum ))
\r
376 return USARTD_ERROR_LOCK;
\r
377 pUsartd->pRxChannel->Done=0;
\r