SAMV71 Xplained Ultra Software Package 1.5

afe_dma.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
00028 /* ---------------------------------------------------------------------------- */
00029 
00030 /** \addtogroup afe_dma_module Working with AFE (DMA support)
00031  *  \ingroup peripherals_module
00032  * The afec driver provides the interface to configure and use the afecC
00033  * peripheral with DMA support.\n
00034  *
00035  * For more accurate information, please look at the AFEC section of the
00036  * Datasheet.
00037  *
00038  * Related files :\n
00039  * \ref afe_dma.c\n
00040  * \ref afe_dma.h\n
00041  */
00042 /*@{*/
00043 /*@}*/
00044 /**
00045  * \file
00046  *
00047  *
00048  */
00049 /*----------------------------------------------------------------------------
00050  *        Headers
00051  *----------------------------------------------------------------------------*/
00052 
00053 #include "chip.h"
00054 #include "afe_dma.h"
00055 #include "xdmad.h"
00056 
00057 #include <stdint.h>
00058 #include <assert.h>
00059 
00060 /*  DMA driver instance */
00061 static uint32_t afeDmaRxChannel;
00062 
00063 /*----------------------------------------------------------------------------
00064  *        Local functions
00065  *----------------------------------------------------------------------------*/
00066 
00067 /**
00068  * \brief AFE xDMA Rx callback
00069  * Invoked on AFE DMA reception done.
00070  * \param channel DMA channel.
00071  * \param pArg Pointer to callback argument - Pointer to AfeDma instance.
00072  */
00073 static void Afe_Rx_Cb(uint32_t channel, AfeDma *pArg)
00074 {
00075     AfeCmd *pAfedCmd = pArg->pCurrentCommand;
00076 
00077     if (channel != afeDmaRxChannel)
00078         return;
00079 
00080     /* Configure and enable interrupt on RC compare */
00081     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00082     NVIC_DisableIRQ(XDMAC_IRQn);
00083 
00084     /* Release the DMA channels */
00085     XDMAD_FreeChannel(pArg->pXdmad, afeDmaRxChannel);
00086     SCB_InvalidateDCache_by_Addr(pAfedCmd->pRxBuff, pAfedCmd->RxSize);
00087     /* Release the dataflash semaphore */
00088     pArg->semaphore++;
00089 
00090     /* Invoke the callback associated with the current command */
00091     if (pAfedCmd && pAfedCmd->callback)
00092         pAfedCmd->callback(0, pAfedCmd->pArgument);
00093 }
00094 
00095 /**
00096  * \brief Configure the DMA Channels: 0 RX.
00097  * Channels are disabled after configure.
00098  * \param pXdmad Pointer to a AfeDma instance
00099  * \returns 0 if the dma channel configuration successfully; otherwise returns
00100  * AFE_ERROR_XXX.
00101  */
00102 static uint8_t _AfeConfigureDmaChannels(AfeDma *pAfed)
00103 {
00104 
00105     /* Driver initialize */
00106     XDMAD_Initialize(pAfed->pXdmad, 0);
00107 
00108     XDMAD_FreeChannel(pAfed->pXdmad, afeDmaRxChannel);
00109 
00110     /* Allocate a DMA channel for AFE0/1 RX. */
00111     afeDmaRxChannel =
00112         XDMAD_AllocateChannel(pAfed->pXdmad, pAfed->afeId, XDMAD_TRANSFER_MEMORY);
00113 
00114     if (afeDmaRxChannel == XDMAD_ALLOC_FAILED)
00115         return AFE_ERROR;
00116 
00117     /* Setup callbacks for AFE0/1 RX */
00118     XDMAD_SetCallback(pAfed->pXdmad, afeDmaRxChannel,
00119                       (XdmadTransferCallback)Afe_Rx_Cb, pAfed);
00120 
00121     if (XDMAD_PrepareChannel(pAfed->pXdmad, afeDmaRxChannel))
00122         return AFE_ERROR;
00123 
00124     return AFE_OK;
00125 }
00126 
00127 /**
00128  * \brief Configure the DMA source and destination with Linker List mode.
00129  * \param pXdmad Pointer to a AfeDma instance
00130  * \param pCommand Pointer to AfeCmd instance
00131  * \param AfeCmd Pointer to command
00132  */
00133 
00134 static uint8_t _Afe_configureLinkList(Afec *pAfeHw, void *pXdmad,
00135                                       AfeCmd *pCommand)
00136 {
00137     uint32_t xdmaCndc, xdmaInt;
00138     sXdmadCfg xdmadRxCfg;
00139     uint32_t afeId;
00140 
00141     if ((unsigned int)pAfeHw == (unsigned int)AFEC0) afeId = ID_AFEC0;
00142 
00143     if ((unsigned int)pAfeHw == (unsigned int)AFEC1) afeId = ID_AFEC1;
00144 
00145     /* Setup RX Link List */
00146     xdmadRxCfg.mbr_ubc = XDMA_UBC_NVIEW_NDV0 |
00147                          XDMA_UBC_NDE_FETCH_DIS |
00148                          XDMA_UBC_NDEN_UPDATED |
00149                          pCommand->RxSize;
00150 
00151     xdmadRxCfg.mbr_da = (uint32_t)pCommand->pRxBuff;
00152     xdmadRxCfg.mbr_sa = (uint32_t) & (pAfeHw->AFEC_LCDR);
00153     xdmadRxCfg.mbr_cfg = XDMAC_CC_TYPE_PER_TRAN |
00154                          XDMAC_CC_MBSIZE_SINGLE |
00155                          XDMAC_CC_DSYNC_PER2MEM |
00156                          XDMAC_CC_CSIZE_CHK_1 |
00157                          XDMAC_CC_DWIDTH_WORD |
00158                          XDMAC_CC_SIF_AHB_IF1 |
00159                          XDMAC_CC_DIF_AHB_IF1 |
00160                          XDMAC_CC_SAM_FIXED_AM |
00161                          XDMAC_CC_DAM_INCREMENTED_AM |
00162                          XDMAC_CC_PERID(
00163                              XDMAIF_Get_ChannelNumber(afeId, XDMAD_TRANSFER_RX));
00164 
00165     xdmadRxCfg.mbr_bc = 0;
00166     xdmadRxCfg.mbr_sus = 0;
00167     xdmadRxCfg.mbr_dus = 0;
00168 
00169     xdmaInt =  (XDMAC_CIE_BIE   |
00170                 XDMAC_CIE_DIE   |
00171                 XDMAC_CIE_FIE   |
00172                 XDMAC_CIE_RBIE  |
00173                 XDMAC_CIE_WBIE  |
00174                 XDMAC_CIE_ROIE);
00175     xdmaCndc = 0;
00176 
00177     if (XDMAD_ConfigureTransfer(pXdmad, afeDmaRxChannel,
00178                                  &xdmadRxCfg, xdmaCndc, 0, xdmaInt))
00179         return AFE_ERROR;
00180 
00181     return AFE_OK;
00182 }
00183 
00184 /*----------------------------------------------------------------------------
00185  *        Exported functions
00186  *----------------------------------------------------------------------------*/
00187 
00188 
00189 /**
00190  * \brief Initializes the AfeDma structure and the corresponding AFE & DMA .
00191  * hardware select value.
00192  * The driver will uses DMA channel 0 for RX .
00193  * The DMA channels are freed automatically when no DMA command processing.
00194  *
00195  * \param pAfed  Pointer to a AfeDma instance.
00196  * \param pAfeHw Associated Afe peripheral.
00197  * \param AfeId  Afe peripheral identifier.
00198  * \param pDmad  Pointer to a Dmad instance.
00199  */
00200 uint32_t Afe_ConfigureDma(AfeDma *pAfed ,
00201                         Afec *pAfeHw ,
00202                         uint8_t AfeId,
00203                         sXdmad *pXdmad)
00204 {
00205     /* Initialize the Afe structure */
00206     pAfed->pAfeHw = pAfeHw;
00207     pAfed->afeId  = AfeId;
00208     pAfed->semaphore = 1;
00209     pAfed->pCurrentCommand = 0;
00210     pAfed->pXdmad = pXdmad;
00211     return 0;
00212 }
00213 
00214 /**
00215  * \brief Starts a AFE transfer. This is a non blocking function. It will
00216  *  return as soon as the transfer is started.
00217  *
00218  * \param pAfed  Pointer to a AfeDma instance.
00219  * \param pCommand Pointer to the Afe command to execute.
00220  * \returns 0 if the transfer has been started successfully; otherwise returns
00221  * AFE_ERROR_LOCK is the driver is in use, or AFE_ERROR if the command is not
00222  * valid.
00223  */
00224 uint32_t Afe_SendData(AfeDma *pAfed, AfeCmd *pCommand)
00225 {
00226     Afec *pAfeHw = pAfed->pAfeHw;
00227 
00228     /* Try to get the dataflash semaphore */
00229     if (pAfed->semaphore == 0)
00230 
00231         return AFE_ERROR_LOCK;
00232 
00233     pAfed->semaphore--;
00234 
00235     // Initialize the callback
00236     pAfed->pCurrentCommand = pCommand;
00237 
00238     /* Initialize DMA controller using channel 0 for RX. */
00239     if (_AfeConfigureDmaChannels(pAfed))
00240         return AFE_ERROR_LOCK;
00241 
00242     /* Configure and enable interrupt on RC compare */
00243     NVIC_ClearPendingIRQ(XDMAC_IRQn);
00244     NVIC_SetPriority(XDMAC_IRQn , 1);
00245     NVIC_EnableIRQ(XDMAC_IRQn);
00246 
00247     if (_Afe_configureLinkList(pAfeHw, pAfed->pXdmad, pCommand))
00248         return AFE_ERROR_LOCK;
00249 
00250     AFEC_StartConversion(pAfeHw);
00251 
00252     /* Start DMA 0(RX) */
00253     if (XDMAD_StartTransfer(pAfed->pXdmad, afeDmaRxChannel))
00254         return AFE_ERROR_LOCK;
00255 
00256     return AFE_OK;;
00257 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines