/***************************************************************************//**
* @file ADF5754R.c
* @brief Implementation of ADF5754R Driver.
* @author DNechita
********************************************************************************
* Copyright 2012(c) Analog Devices, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* - Neither the name of Analog Devices, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* - The use of this software may or may not infringe the patent rights
* of one or more patent holders. This license does not release you
* from the requirement that you obtain separate licenses from these
* patent holders to use this software.
* - Use of the software either in source or binary form, must be run
* on or directly connected to an Analog Devices Inc. component.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************
* SVN Revision: $WCREV$
*******************************************************************************/
/******************************************************************************/
/* Include Files */
/******************************************************************************/
#include "AD5754R.h" // AD5754R definitions.
/***************************************************************************//**
* @brief Initializes the SPI peripheral.
*
* @param None.
*
* @return status - Result of the initialization procedure.
* Example: 0x0 - SPI peripheral was not initialized.
* 0x1 - SPI peripheral is initialized.
*******************************************************************************/
unsigned char AD5754R_Init(void)
{
unsigned char status = 0;
GPIO1_PIN_OUT;
GPIO2_PIN_OUT;
AD5754R_LDAC_LOW;
AD5754R_CLR_HIGH;
status = SPI_Init(0, 1000000, 1, 1);
return(status);
}
/***************************************************************************//**
* @brief Writes data into a register.
*
* @param regBits - Address of the register.
* @param adrBits - Address of a channel or of an option.
* @param registerValue - Data value to write.
*
* @return None.
*******************************************************************************/
void AD5754R_SetRegisterValue(unsigned char regBits,
unsigned char adrBits,
unsigned short registerValue)
{
unsigned char registerWord[4] = {0};
registerWord[0] = 0x01; // AD5754R CS.
registerWord[1] = (regBits << 3) + adrBits;
registerWord[2] = (registerValue & 0xFF00) >> 8;
registerWord[3] = (registerValue & 0x00FF) >> 0;
SPI_Write(registerWord,3);
}
/***************************************************************************//**
* @brief Reads the value of a register.
*
* @param registerAddress - Address of the register.
*
* @return *dataPointer - Value of the register.
*******************************************************************************/
unsigned short AD5754R_GetRegisterValue(unsigned char regBits,
unsigned char adrBits)
{
unsigned char registerWord[4] = {0};
unsigned short data = 0;
registerWord[0] = 0x01; // AD5754R CS.
registerWord[1] = (1 << 7) + (regBits << 3) + adrBits;
registerWord[2] = 0;
registerWord[3] = 0;
SPI_Write(registerWord,3);
registerWord[0] = 0x01; // AD5754R CS.
registerWord[1] = 0x18; // NOP instruction used in readback operations.
registerWord[2] = 0;
registerWord[3] = 0;
SPI_Read(registerWord,3);
data = (registerWord[1] << 8) + registerWord[2];
return(data);
}
/***************************************************************************//**
* @brief Enables/disables indicated DAC channels.
*
* @param dacChannel - DAC channel or channels to be enabled/disabled.
* Example: AD5754R_DAC_A - Channel A.
* AD5754R_DAC_B - Channel B.
* AD5754R_DAC_C - Channel C.
* AD5754R_DAC_D - Channel D.
* AD5754R_DAC_ALL - Channel ALL.
* @param onOff - Enable or disable channel.
* Example: 0x0 - Off.
* 0x1 - On.
*
* @return None.
*******************************************************************************/
void AD5754R_SetPower(unsigned char dacChannel, unsigned char onOff)
{
unsigned short oldPowerCtl = 0;
unsigned short newPowerCtl = 0;
if(dacChannel == AD5754R_DAC_ALL)
{
dacChannel = 0x0F;
}
else
{
dacChannel = (1 << dacChannel);
}
oldPowerCtl = AD5754R_GetRegisterValue(AD5754R_REG_POWER_CONTROL,0x00);
newPowerCtl = oldPowerCtl & ~dacChannel;
newPowerCtl = newPowerCtl | (onOff * dacChannel);
AD5754R_SetRegisterValue(AD5754R_REG_POWER_CONTROL,0x00,newPowerCtl);
}
/***************************************************************************//**
* @brief Writes data into a DAC channel register.
*
* @param dacChannel - DAC channel or channels to be written.
* @param dacValue - Value written in DAC register.
*
* @return None.
*******************************************************************************/
void AD5754R_LoadDac(unsigned char dacChannel, unsigned short dacValue)
{
AD5754R_SetRegisterValue(AD5754R_REG_DAC, dacChannel, dacValue);
}
/***************************************************************************//**
* @brief Selects the output range of a DAC channel.
*
* @param dacChannel - DAC channel.
* @param outputRange - Output range of a DAC channel.
*
* @return None.
*******************************************************************************/
void AD5754R_RangeSelect(unsigned char dacChannel, unsigned char outputRange)
{
unsigned short registerData = 0;
registerData |= outputRange;
AD5754R_SetRegisterValue(AD5754R_REG_RANGE_SELECT,dacChannel,registerData);
}
/***************************************************************************//**
* @brief Selects an option of Control Register.
*
* @param option - control function selected
* @param bitValue - indicates a set or a clear of the following
* bits: SDO disable
* CRL select
* Clamp enable
* TSD enable
*
* @return None.
***
评论2