SAMV71 Xplained Ultra Software Package 1.5

main.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 /**
00031  * \page afe12_dma AFE12 DMA Example
00032  *
00033  * \section Purpose
00034  *
00035  * The AFE12 example demonstrates how to use AFE peripheral with several modes.
00036  *
00037  * \section Requirements
00038  *
00039  * This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00040  * To enable full scale measurement connect DAC to AFE.
00041  *
00042  * \section Connection
00043  * PB13 -- PB2
00044  *
00045  * \section Description
00046  *
00047  * This application shows how to use the AFE in 12-bit free run mode with DMA
00048  * enabled.
00049  *
00050  *  \section Usage
00051  *
00052  * -# Build the program and download it inside the board.
00053  * Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00054  * -# On the computer, open and configure a terminal application
00055  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00056  *   - 115200 baud rate
00057  *   - 8 bits of data
00058  *   - No parity
00059  *   - 1 stop bit
00060  *   - No flow control
00061  * -# In the terminal window, the
00062  *    following text should appear (values depend on the board and chip used):
00063  *    \code
00064  *     -- AFE12 Example xxx --
00065  *     -- xxxxxx-xx
00066  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00067  *    \endcode
00068  * -# The application will output converted value to hyperterminal and display
00069  *    them.
00070  *
00071  * \section References
00072  * - afe12_dma/main.c
00073  * - afe_dma.h
00074  * - afec.h
00075  */
00076 
00077 /** \file
00078  *
00079  *  This file contains all the specific code for the AFE12 example.
00080  */
00081 
00082 /*----------------------------------------------------------------------------
00083  *        Headers
00084  *----------------------------------------------------------------------------*/
00085 
00086 #include "board.h"
00087 #include <string.h>
00088 
00089 /** SAMPLES per cycle*/
00090 #define SAMPLES         100
00091 #define TEST_CHANNEL    5
00092 #define AFE_CLK         2200000
00093 
00094 /* sine wave data */
00095 const uint16_t sine_data[SAMPLES] = {
00096     1,    2,      5,     10,     17,     26,     37,     50,     65,     81,
00097     99,  119,    140,    163,    187,    212,    239,    266,    295,    325,
00098     355,  385,    417,    449,    481,    512,    543,    575,    607,    639,
00099     669,  699,    729,    758,    785,    812,    837,    861,    884,    905,
00100     925,  943,    959,    974,    987,    998,   1007,   1014,   1019,   1022,
00101     1023, 1022,   1019,   1014,   1007,    998,    987,    974,    959,    943,
00102     925,  905,    884,    861,    837,    812,    785,    758,    729,    699,
00103     669,  639,    607,    575,    543,    512,    481,    449,    417,    385,
00104     355,  325,    295,    266,    239,    212,    187,    163,    140,    119,
00105     99,   81,     65,     50,     37,     26,     17,     10,      5,      2 };
00106 
00107 uint32_t afeBuffer[SAMPLES];
00108 uint32_t dacBuffer[SAMPLES];
00109 
00110 /** Global DMA driver for all transfer */
00111 static sXdmad dmad;
00112 
00113 /** Global AFE DMA instance */
00114 static AfeDma Afed;
00115 /** AFE command instance */
00116 static AfeCmd AfeCommand;
00117 
00118 /** Global DAC DMA instance */
00119 static DacDma Dacd;
00120 /** DAC command instance */
00121 static DacCmd DacCommand;
00122 
00123 /*----------------------------------------------------------------------------
00124  *        Local definitions
00125  *----------------------------------------------------------------------------*/
00126 
00127 /**
00128  *  \brief xDMA interrupt handler.
00129  *
00130  */
00131 void XDMAC_Handler(void)
00132 {
00133     XDMAD_Handler(&dmad);
00134 }
00135 
00136 /**
00137  *  \brief Callback function for AFE interrupt
00138  *
00139  */
00140 static void _afe_Callback(int dummy, void* pArg)
00141 {
00142     uint32_t i;
00143     uint32_t ch;
00144     uint32_t voltage;
00145     dummy = dummy;
00146     pArg = pArg;
00147     printf("\n\rCH  AFE   Voltage(mV) \n\r");
00148     for (i = 0; i < SAMPLES; i++) {
00149         ch = (afeBuffer[i] & AFEC_LCDR_CHNB_Msk) >> AFEC_LCDR_CHNB_Pos;
00150         voltage = ((afeBuffer[i] & 0xFFFF ) - 0x800) * 1650 / 2047;
00151         printf("%02u  %04x  %04u\n\r" ,(unsigned int)ch,
00152             (unsigned int)(afeBuffer[i] & 0xFFFF) ,(unsigned int)voltage);
00153     }
00154 }
00155 
00156 /**
00157  *  \brief Initialize DAC.
00158  *
00159  */
00160 static void _dac_initialization(void) {
00161     uint32_t i;
00162     PMC_EnablePeripheral(ID_DACC);
00163     DACC_SoftReset(DACC);
00164     DACC_EnableChannel(DACC, 0);
00165     for (i = 0; i < SAMPLES; i++)
00166         dacBuffer[i] = sine_data[i] << 1;
00167 }
00168 
00169 /**
00170  *  \brief Configure DAC DMA and start DMA transfer.
00171  *
00172  */
00173 static void _dac_dmaTransfer(void)
00174 {
00175     DacCommand.dacChannel = DACC_CHANNEL_0;
00176     DacCommand.TxSize = SAMPLES;
00177     DacCommand.pTxBuff = (uint8_t *)dacBuffer;
00178     DacCommand.loopback = 1;
00179     Dac_ConfigureDma(&Dacd, DACC, ID_DACC, &dmad);
00180     Dac_SendData(&Dacd, &DacCommand);
00181 }
00182 
00183 /**
00184  *  \brief Initialize AFE.
00185  *
00186  */
00187 static void _afe_initialization(void) {
00188     AFEC_Initialize(AFEC0, ID_AFEC0);
00189     AFEC_SetModeReg(AFEC0,
00190             AFEC_MR_FREERUN_ON
00191             | AFEC_EMR_RES_NO_AVERAGE
00192             | (1 << AFEC_MR_TRANSFER_Pos)
00193             | (2 << AFEC_MR_TRACKTIM_Pos)
00194             | AFEC_MR_ONE
00195             | AFEC_MR_SETTLING_AST3
00196             | AFEC_MR_STARTUP_SUT64);
00197 
00198     AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK);
00199     AFEC_SetExtModeReg(AFEC0,
00200             0
00201             | AFEC_EMR_RES_NO_AVERAGE
00202             | AFEC_EMR_TAG
00203             | AFEC_EMR_STM);
00204     AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, 0x800);
00205     AFEC_SetAnalogControl(AFEC0, AFEC_ACR_IBCTL(1) | AFEC_ACR_PGA0_ON |
00206             AFEC_ACR_PGA1_ON);
00207     AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
00208 }
00209 
00210 /**
00211  *  \brief Configure AFE DMA and start DMA transfer.
00212  *
00213  */
00214 static void _afe_dmaTransfer(void)
00215 {
00216     AfeCommand.RxSize= SAMPLES;
00217     AfeCommand.pRxBuff = afeBuffer;
00218     AfeCommand.callback = (AfeCallback)_afe_Callback;
00219     Afe_ConfigureDma(&Afed, AFEC0, ID_AFEC0, &dmad);
00220     Afe_SendData(&Afed, &AfeCommand);
00221 }
00222 
00223 /**
00224  *  \brief AFE12 Application entry point.
00225  *
00226  *  \return Unused (ANSI-C compatibility).
00227  */
00228 int main( void )
00229 {
00230     /* Disable watchdog */
00231     WDT_Disable(WDT);
00232 
00233     /* Enable I and D cache */
00234     SCB_EnableICache();
00235     SCB_EnableDCache();
00236 
00237     /* Output example information */
00238     printf("\n\r-- AFE12_dma Example %s --\n\r", SOFTPACK_VERSION);
00239     printf("-- %s\n\r", BOARD_NAME );
00240     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00241 
00242     /* Initialize DAC */
00243     _dac_initialization();
00244     _dac_dmaTransfer();
00245 
00246     /* Initialize AFE */
00247     _afe_initialization();
00248     _afe_dmaTransfer();
00249 
00250     while (1);
00251 }
00252 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines