SAM3S8/SD8 Software Package: libraries/libchip_sam3s/source/dacc.c Source File

SAM3S8/SD8 Software Package 1.2

# dacc.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2009, 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 /** \addtogroup dacc_module Working with DACC
    00031  *  \ingroup peripherals_module
    00032  * The DACC driver provides the interface to configure and use the DACC peripheral.\n
    00033  *
    00034  * The DACC(Digital-to-Analog Converter Controller) converts digital code to analog output.
    00035  * The data to be converted are sent in a common register for all channels. It offers up to 2
    00036  * analog outputs.The output voltage ranges from (1/6)ADVREF to (5/6)ADVREF.
    00037  *
    00038  * To Enable a DACC conversion,the user has to follow these few steps:
    00039  * <ul>
    00040  * <li> Select an appropriate reference voltage on ADVREF   </li>
    00041  * <li> Configure the DACC according to its requirements and special needs,which could be
    00042         broken down into several parts:
    00043  * -#   Enable DACC in free running mode by clearing TRGEN in DACC_MR;
    00044  * -#   Configure Startup Time and Refresh Period through setting STARTUP and REFRESH fields
    00045  *      in DACC_MR; The refresh mechanism is used to protect the output analog value from
    00046  *      decreasing.
    00047  * -#   Enable channels and write digital code to DACC_CDR,in free running mode, the conversion
    00048  *      is started right after at least one channel is enabled and data is written .
    00049    </li>
    00050  * </ul>
    00051  *
    00052  * For more accurate information, please look at the DACC section of the
    00053  * Datasheet.
    00054  *
    00055  * Related files :\n
    00056  * \ref DACC.c\n
    00057  * \ref DACC.h\n
    00058 */
    00059 /*@{*/
    00060 /*@}*/
    00061 /**
    00062  * \file
    00063  *
    00064  * Implementation of Digital-to-Analog Converter Controller (DACC).
    00065  *
    00066  */
    00067 /*----------------------------------------------------------------------------
    00068  *        Headers
    00069  *----------------------------------------------------------------------------*/
    00070
    00071 #include "chip.h"
    00072
    00073 #include <stdint.h>
    00074 #include <assert.h>
    00075
    00076 /*----------------------------------------------------------------------------
    00077  *        Exported functions
    00078  *----------------------------------------------------------------------------*/
    00079
    00080 /**
    00081   * \brief Initialize the DACC controller
    00082   * \param pDACC Pointer to an DACC instance.
    00083   * \param idDACC identifier of DAC peripheral
    00084   * \param trgEn trigger mode, free running mode or external Hardware trigger
    00085   * \param word transfer size,word or half word
    00086   * \param trgSel hardware trigger selection
    00087   * \param sleepMode sleep mode selection
    00088   * \param mck value of MCK in Hz
    00089   * \param refresh refresh period
    00090   * \param user_sel user channel selection ,0 or 1
    00091   * \param tag_mode tag for channel number
    00092   * \param startup value of the start up time (in DACCClock) (see datasheet)
    00093 */
    00094 extern void DACC_Initialize( Dacc* pDACC,
    00095                              uint8_t idDACC,
    00096                              uint8_t trgEn,
    00097                              uint8_t trgSel,
    00098                              uint8_t word,
    00099                              uint8_t sleepMode,
    00100                              uint32_t mck,
    00101                              uint8_t refresh,    /* refresh period */
    00102                              uint8_t user_sel,   /* user channel selection */
    00103                              uint32_t tag_mode,  /* using tag for channel number */
    00104                              uint32_t startup
    00105                             )
    00106 {
    00107     /* Stop warning */
    00108     mck = mck;
    00109
    00110     /* Enable peripheral clock*/
    00111     PMC->PMC_PCER0 = 1 << idDACC;
    00112
    00113     /*  Reset the controller */
    00114     DACC_SoftReset(pDACC);
    00115
    00116     /*  Write to the MR register */
    00117     DACC_CfgModeReg( pDACC,
    00118           ( trgEn & DACC_MR_TRGEN)
    00119         |   DACC_MR_TRGSEL(trgSel)
    00120         | ( word & DACC_MR_WORD)
    00121         | ( sleepMode & DACC_MR_SLEEP)
    00122         |   DACC_MR_REFRESH(refresh)
    00123         | ( user_sel & DACC_MR_USER_SEL_Msk)
    00124         | ( tag_mode &  DACC_MR_TAG)
    00125         | ( startup & DACC_MR_STARTUP_Msk));
    00126 }
    00127
    00128
    00129 /**
    00130  * Set the Conversion Data
    00131  * \param pDACC Pointer to an Dacc instance.
    00132  * \param data  date to be converted.
    00133  */
    00134 extern void DACC_SetConversionData( Dacc* pDACC, uint32_t dwData )
    00135 {
    00136     uint32_t dwMR = pDACC->DACC_MR ;
    00137
    00138     if ( dwMR & DACC_MR_WORD )
    00139     {
    00140         pDACC->DACC_CDR = dwData ;
    00141     }
    00142     else
    00143     {
    00144         pDACC->DACC_CDR = (dwData&0xFFFF) ;
    00145     }
    00146 }
    00147
    00148
    00149 /**
    00150   * \brief Write converted data through PDC channel
    00151   * \param pDACC the pointer of DACC peripheral
    00152   * \param pBuffer the destination buffer
    00153   * \param size the size of the buffer
    00154 */
    00155 extern uint32_t DACC_WriteBuffer( Dacc* pDACC, uint16_t *pwBuffer, uint32_t dwSize )
    00156 {
    00157
    00158     /* Check if the first PDC bank is free*/
    00159     if ( (pDACC->DACC_TCR == 0) && (pDACC->DACC_TNCR == 0) )
    00160     {
    00161         pDACC->DACC_TPR = (uint32_t)pwBuffer ;
    00162         pDACC->DACC_TCR = dwSize ;
    00163         pDACC->DACC_PTCR = DACC_PTCR_TXTEN ;
    00164
    00165         return 1 ;
    00166     }
    00167     /* Check if the second PDC bank is free*/
    00168     else
    00169     {
    00170         if (pDACC->DACC_TNCR == 0)
    00171         {
    00172             pDACC->DACC_TNPR = (uint32_t)pwBuffer ;
    00173             pDACC->DACC_TNCR = dwSize ;
    00174
    00175             return 1 ;
    00176         }
    00177         else
    00178         {
    00179             return 0 ;
    00180         }
    00181     }
    00182
    00183 }
    00184

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines