/***************************************************************************//**
* @file AD7190.c
* @brief Implementation of AD7190 Driver.
* @author DNechita (Dan.Nechita@analog.com)
********************************************************************************
* 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: 903
*******************************************************************************/
/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
#include "AD7190.h" // AD7190 definitions.
#include "TIME.h" // TIME definitions.
/***************************************************************************//**
* @brief Writes data into a register.
*
* @param registerAddress - Address of the register.
* @param registerValue - Data value to write.
* @param bytesNumber - Number of bytes to be written.
* @param modifyCS - Allows Chip Select to be modified.
*
* @return none.
*******************************************************************************/
void AD7190_SetRegisterValue(unsigned char registerAddress,
unsigned long registerValue,
unsigned char bytesNumber,
unsigned char modifyCS)
{
unsigned char writeCommand[5] = {0, 0, 0, 0, 0};
unsigned char* dataPointer = (unsigned char*)®isterValue;
unsigned char bytesNr = bytesNumber;
writeCommand[0] = AD7190_COMM_WRITE |
AD7190_COMM_ADDR(registerAddress);
while(bytesNr > 0)
{
writeCommand[bytesNr] = *dataPointer;
dataPointer ++;
bytesNr --;
}
SPI_Write(AD7190_SLAVE_ID * modifyCS, writeCommand, bytesNumber + 1);
}
/***************************************************************************//**
* @brief Reads the value of a register.
*
* @param registerAddress - Address of the register.
* @param bytesNumber - Number of bytes that will be read.
* @param modifyCS - Allows Chip Select to be modified.
*
* @return buffer - Value of the register.
*******************************************************************************/
unsigned long AD7190_GetRegisterValue(unsigned char registerAddress,
unsigned char bytesNumber,
unsigned char modifyCS)
{
unsigned char registerWord[5] = {0, 0, 0, 0, 0};
unsigned long buffer = 0x0;
unsigned char i = 0;
registerWord[0] = AD7190_COMM_READ |
AD7190_COMM_ADDR(registerAddress);
SPI_Read(AD7190_SLAVE_ID * modifyCS, registerWord, bytesNumber + 1);
for(i = 1; i < bytesNumber + 1; i++)
{
buffer = (buffer << 8) + registerWord[i];
}
return buffer;
}
/***************************************************************************//**
* @brief Checks if the AD7190 part is present.
*
* @return status - Indicates if the part is present or not.
*******************************************************************************/
unsigned char AD7190_Init(void)
{
unsigned char status = 1;
unsigned char regVal = 0;
SPI_Init(0, 1000000, 1, 0);
AD7190_Reset();
/* Allow at least 500 us before accessing any of the on-chip registers. */
TIME_DelayMs(1);
regVal = AD7190_GetRegisterValue(AD7190_REG_ID, 1, 1);
if( (regVal & AD7190_ID_MASK) != ID_AD7190)
{
status = 0;
}
return status ;
}
/***************************************************************************//**
* @brief Resets the device.
*
* @return none.
*******************************************************************************/
void AD7190_Reset(void)
{
unsigned char registerWord[7];
registerWord[0] = 0x01;
registerWord[1] = 0xFF;
registerWord[2] = 0xFF;
registerWord[3] = 0xFF;
registerWord[4] = 0xFF;
registerWord[5] = 0xFF;
registerWord[6] = 0xFF;
SPI_Write(AD7190_SLAVE_ID, registerWord, 7);
}
/***************************************************************************//**
* @brief Set device to idle or power-down.
*
* @param pwrMode - Selects idle mode or power-down mode.
* Example: 0 - power-down
* 1 - idle
*
* @return none.
*******************************************************************************/
void AD7190_SetPower(unsigned char pwrMode)
{
unsigned long oldPwrMode = 0x0;
unsigned long newPwrMode = 0x0;
oldPwrMode = AD7190_GetRegisterValue(AD7190_REG_MODE, 3, 1);
oldPwrMode &= ~(AD7190_MODE_SEL(0x7));
newPwrMode = oldPwrMode |
AD7190_MODE_SEL((pwrMode * (AD7190_MODE_IDLE)) |
(!pwrMode * (AD7190_MODE_PWRDN)));
AD7190_SetRegisterValue(AD7190_REG_MODE, newPwrMode, 3, 1);
}
/***************************************************************************//**
* @brief Waits for RDY pin to go low.
*
* @return none.
*******************************************************************************/
void AD7190_WaitRdyGoLow(void)
{
unsigned long timeOutCnt = 0xFFFFF;
while(AD7190_RDY_STATE && timeOutCnt--)
{
;
}
}
/***************************************************************************//**
* @brief Selects the channel to be enabled.
*
* @param channel - Selects a channel.
*
* @return none.
*******************************************************************************/
void AD7190_ChannelSelect(unsigned short channel)
{
unsigned long oldRegValue = 0x0;
unsigned long newRegValue = 0x0;
oldRegValue = AD71