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 afe_temp_sensor AFE temp sensor Example
00032  *
00033  * \section Purpose
00034  *
00035  * The example is aimed to demonstrate the temperature sensor feature
00036  * inside the device. The channel 11 is connected to the sensor by default.
00037  *
00038  * \section Requirements
00039  *
00040  * This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00041  *
00042  * \section Description
00043  *
00044  * The temperature sensor provides an output voltage (VT) that is proportional
00045  * to absolute temperature (PTAT). The relationship between measured voltage and
00046  * actual temperature could be found in Electrical Characteristics part of the
00047  * datasheet.
00048  *
00049  *  \section Usage
00050  *
00051  * -# Build the program and download it inside the board.
00052  * Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00053  * -# On the computer, open and configure a terminal application
00054  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00055  *   - 115200 baud rate
00056  *   - 8 bits of data
00057  *   - No parity
00058  *   - 1 stop bit
00059  *   - No flow control
00060  * -# In the terminal window, the
00061  *     following text should appear (values depend on the board and chip used):
00062  *    \code
00063  *     -- AFE12 Example xxx --
00064  *     -- xxxxxx-xx
00065  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00066  *    \endcode
00067  * -# The application will output converted value to hyper-terminal and display
00068  *    a menu for user to show current temperature.
00069  *
00070  * \section References
00071  * - afe_temp_sensor /main.c
00072  */
00073 
00074 /** \file
00075  *
00076  *  This file contains all the specific code for the AFE12 example.
00077  */
00078 
00079 /*----------------------------------------------------------------------------
00080  *        Headers
00081  *----------------------------------------------------------------------------*/
00082 #include "board.h"
00083 #include <string.h>
00084 
00085 /*----------------------------------------------------------------------------
00086  *        Definition
00087  *----------------------------------------------------------------------------*/
00088 
00089 /** SAMPLES per cycle*/
00090 #define SAMPLES (100)
00091 
00092 /** Reference voltage for AFEC in mv. */
00093 #define VOLT_REF        (3300)
00094 
00095 /** The maximal digital value */
00096 #define MAX_DIGITAL_12_BIT     (4095UL)
00097 #define AFEC_TEMPERATURE_SENSOR  11
00098 
00099 #define AFEC_ACR_PGA0_ON     (0x1u << 2)
00100 #define AFEC_ACR_PGA1_ON     (0x1u << 3)
00101 
00102 /*----------------------------------------------------------------------------
00103  *        Local variables
00104  *----------------------------------------------------------------------------*/
00105 /** Global DMA driver for all transfer */
00106 static sXdmad dmad;
00107 
00108 /** Global AFE DMA instance */
00109 static AfeDma Afed;
00110 
00111 /** AFE command instance */
00112 static AfeCmd AfeCommand;
00113 
00114 /** AFE output value */
00115 COMPILER_ALIGNED(32) uint32_t afeOutput;
00116 
00117 /*----------------------------------------------------------------------------
00118  *        Local Functions
00119  *----------------------------------------------------------------------------*/
00120 /**
00121  *  \brief xDMA interrupt handler.
00122  *
00123  */
00124 void XDMAC_Handler(void)
00125 {
00126     XDMAD_Handler(&dmad);
00127 }
00128 
00129 /**
00130  *  \brief Initialize AFE.
00131  *
00132  */
00133 static void _afe_initialization(void) {
00134     AFEC_Initialize( AFEC0, ID_AFEC0 );
00135 
00136     AFEC_SetModeReg(AFEC0, 0
00137             | AFEC_EMR_RES_NO_AVERAGE
00138             | (1 << AFEC_MR_TRANSFER_Pos)
00139             | (2 << AFEC_MR_TRACKTIM_Pos)
00140             | AFEC_MR_ONE
00141             | AFEC_MR_SETTLING_AST3
00142             | AFEC_MR_STARTUP_SUT64);
00143 
00144     AFEC_SetClock(AFEC0, 6000000, BOARD_MCK);
00145 
00146     AFEC_SetExtModeReg(AFEC0, 0
00147             | AFEC_EMR_RES_NO_AVERAGE
00148             | AFEC_EMR_TAG
00149             | AFEC_EMR_STM );
00150 
00151     AFEC_EnableChannel(AFEC0, AFEC_TEMPERATURE_SENSOR);
00152     AFEC0->AFEC_ACR = AFEC_ACR_IBCTL(2)
00153         | (1 << 4)
00154         | AFEC_ACR_PGA0_ON
00155         | AFEC_ACR_PGA1_ON;
00156 
00157     AFEC_SetChannelGain(AFEC0, AFEC_CGR_GAIN11(0));
00158     AFEC_SetAnalogOffset(AFEC0, AFEC_TEMPERATURE_SENSOR, 0x200);
00159 }
00160 
00161 /**
00162  *  \brief Callback function for AFE interrupt
00163  *
00164  */
00165 static void _afe_Callback(int dummy, void* pArg)
00166 {
00167     uint32_t afeValue;
00168     dummy = dummy;
00169     pArg = pArg;
00170     afeValue = (afeOutput & AFEC_LCDR_LDATA_Msk) * VOLT_REF / MAX_DIGITAL_12_BIT;
00171     /* According to datasheet, The output voltage VT = 0.72V at 27C and the
00172         temperature slope dVT/dT = 2.33 mV/C */
00173     printf("\n\r Temperature is: %4d ", (int) (afeValue - 720) * 100 / 233 + 27);
00174 }
00175 
00176 /**
00177  *  \brief Configure AFE DMA and start DMA transfer.
00178  *
00179  */
00180 static void _afe_dmaTransfer(void)
00181 {
00182     AfeCommand.RxSize= 1;
00183     AfeCommand.pRxBuff = &afeOutput;
00184     AfeCommand.callback = (AfeCallback)_afe_Callback;
00185     Afe_ConfigureDma(&Afed, AFEC0, ID_AFEC0, &dmad);
00186     Afe_SendData(&Afed, &AfeCommand);
00187 }
00188 
00189 /**
00190  *  \brief afe_temp_sensor Application entry point.
00191  *
00192  *  \return Unused (ANSI-C compatibility).
00193  */
00194 int main( void )
00195 {
00196     uint8_t key;
00197     /* Disable watchdog */
00198     WDT_Disable(WDT);
00199 
00200     /* Enable I and D cache */
00201     SCB_EnableICache();
00202     SCB_EnableDCache();
00203 
00204     /* Output example information */
00205     printf("\n\r-- AFE Temperature Sensor Example %s --\n\r", SOFTPACK_VERSION);
00206     printf("-- %s\n\r", BOARD_NAME );
00207     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME);
00208 
00209     _afe_initialization();
00210     printf("\n\r Press 't' to get current temperature");
00211     for(;;) {
00212         key = DBG_GetChar();
00213         if ((key == 't')) {
00214             _afe_dmaTransfer();
00215         }
00216     }
00217 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines