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