SAMV71 Xplained Ultra Software Package 1.3

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
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 SAM V71 Xplained Ultra board. To enable full 
00040  *  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 SAM V71 Xplained Ultra board. 
00053  *  Please refer to the Getting Started with SAM V71 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/main.c
00073  *  - AFE.h
00074  */
00075 
00076 /** \file
00077  *
00078  *  This file contains all the specific code for the AFE12 example.
00079  */
00080 
00081 /*----------------------------------------------------------------------------
00082  *        Headers
00083  *----------------------------------------------------------------------------*/
00084 
00085 #include "board.h"
00086 #include <string.h>
00087 
00088 /** SAMPLES per cycle*/
00089 #define SAMPLES         100
00090 #define TEST_CHANNEL    5
00091 #define AFE_CLK         2200000
00092 
00093 /* sine wave data */
00094 const uint16_t sine_data[SAMPLES] = {
00095     1,    2,      5,     10,     17,     26,     37,     50,     65,     81,
00096     99,  119,    140,    163,    187,    212,    239,    266,    295,    325,
00097     355,  385,    417,    449,    481,    512,    543,    575,    607,    639,
00098     669,  699,    729,    758,    785,    812,    837,    861,    884,    905,
00099     925,  943,    959,    974,    987,    998,   1007,   1014,   1019,   1022,
00100     1023, 1022,   1019,   1014,   1007,    998,    987,    974,    959,    943,
00101     925,  905,    884,    861,    837,    812,    785,    758,    729,    699,
00102     669,  639,    607,    575,    543,    512,    481,    449,    417,    385,
00103     355,  325,    295,    266,    239,    212,    187,    163,    140,    119,
00104     99,   81,     65,     50,     37,     26,     17,     10,      5,      2 };
00105 
00106 uint32_t afeBuffer[SAMPLES];
00107 uint32_t dacBuffer[SAMPLES];
00108 
00109 /** Global DMA driver for all transfer */
00110 static sXdmad dmad;
00111 
00112 /** Global AFE DMA instance */
00113 static AfeDma Afed;
00114 /** AFE command instance */
00115 static AfeCmd AfeCommand;
00116 
00117 /** Global DAC DMA instance */
00118 static DacDma Dacd;
00119 /** DAC command instance */
00120 static DacCmd DacCommand;
00121 
00122 /*----------------------------------------------------------------------------
00123  *        Local definitions
00124  *----------------------------------------------------------------------------*/
00125 
00126 /**
00127  *  \brief xDMA interrupt handler.
00128  *
00129  */ 
00130 void XDMAC_Handler(void)
00131 {
00132     XDMAD_Handler(&dmad);
00133 }
00134 
00135 /**
00136  *  \brief Callback function for AFE interrupt
00137  *
00138  */ 
00139 static void _afe_Callback(int dummy, void* pArg)
00140 {
00141     uint32_t i;
00142     uint32_t ch;
00143     uint32_t voltage;
00144     dummy = dummy;
00145     pArg = pArg;
00146     printf("\n\rCH  AFE   Voltage(mV) \n\r");
00147     for ( i = 0; i < SAMPLES; i++ ) {
00148         ch = (afeBuffer[i] & AFEC_LCDR_CHNB_Msk ) >> AFEC_LCDR_CHNB_Pos;
00149         voltage = ((afeBuffer[i] & 0xFFFF ) - 0x800) * 1650 / 2047;
00150         printf("%02u  %04x  %04u\n\r" ,(unsigned int)ch,
00151             (unsigned int)(afeBuffer[i]& 0xFFFF) , (unsigned int)voltage);
00152     }
00153 }
00154 
00155 /**
00156  *  \brief Initialize DAC.
00157  *
00158  */
00159 static void _dac_initialization(void) {
00160     uint32_t i;
00161     PMC_EnablePeripheral(ID_DACC);
00162     DACC_SoftReset(DACC);
00163     DACC_EnableChannel(DACC, 0);
00164     for ( i = 0; i < SAMPLES; i++ )
00165         dacBuffer[i] = sine_data[i] <<1;
00166 }
00167 
00168 /**
00169  *  \brief Configure DAC DMA and start DMA transfer.
00170  *
00171  */
00172 static void _dac_dmaTransfer(void)
00173 { 
00174     DacCommand.dacChannel = DACC_CHANNEL_0;
00175     DacCommand.TxSize= SAMPLES;
00176     DacCommand.pTxBuff = (uint8_t *)dacBuffer;
00177     DacCommand.loopback = 1;
00178     Dac_ConfigureDma(&Dacd, DACC, ID_DACC, &dmad);
00179     Dac_SendData(&Dacd, &DacCommand);
00180 }
00181 
00182 /**
00183  *  \brief Initialize AFE.
00184  *
00185  */
00186 static void _afe_initialization(void) {
00187     AFEC_Initialize( AFEC0, ID_AFEC0 );
00188     AFEC_SetModeReg(AFEC0,
00189             AFEC_MR_FREERUN_ON
00190             | AFEC_EMR_RES_NO_AVERAGE
00191             |(1 << AFEC_MR_TRANSFER_Pos)
00192             |(2 << AFEC_MR_TRACKTIM_Pos) 
00193             | AFEC_MR_ONE
00194             | AFEC_MR_SETTLING_AST3
00195             | AFEC_MR_STARTUP_SUT64);
00196 
00197     AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
00198     AFEC_SetExtModeReg(AFEC0, 
00199             0
00200             | AFEC_EMR_RES_NO_AVERAGE
00201             | AFEC_EMR_TAG
00202             | AFEC_EMR_STM );
00203     AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, 0x800);
00204     AFEC_SetAnalogControl(AFEC0, AFEC_ACR_IBCTL(1) | AFEC_ACR_PGA0_ON | 
00205             AFEC_ACR_PGA1_ON ); 
00206     AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
00207 }
00208 
00209 /**
00210  *  \brief Configure AFE DMA and start DMA transfer.
00211  *
00212  */
00213 static void _afe_dmaTransfer(void)
00214 { 
00215     AfeCommand.RxSize= SAMPLES;
00216     AfeCommand.pRxBuff = afeBuffer;
00217     AfeCommand.callback = (AfeCallback)_afe_Callback;
00218     Afe_ConfigureDma(&Afed, AFEC0, ID_AFEC0, &dmad);
00219     Afe_SendData(&Afed, &AfeCommand);
00220 }
00221 
00222 /**
00223  *  \brief AFE12 Application entry point.
00224  *
00225  *  \return Unused (ANSI-C compatibility).
00226  */
00227 int main( void )
00228 {
00229     /* Disable watchdog */
00230     WDT_Disable( WDT ) ;
00231   
00232     /* Enable I and D cache */
00233     SCB_EnableICache();
00234     SCB_EnableDCache();
00235     
00236     /* Output example information */
00237     printf( "\n\r-- AFE12_dma Started Example %s --\n\r", SOFTPACK_VERSION );
00238     printf( "-- %s\n\r", BOARD_NAME ) ;
00239     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME);
00240 
00241     /* Initialize DAC */
00242     _dac_initialization();
00243     _dac_dmaTransfer();
00244 
00245     /* Initialize AFE */
00246     _afe_initialization();
00247     _afe_dmaTransfer();
00248 
00249     while(1);
00250 }
00251 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines