/***************************************************************************//**
* @file AD717X.c
* @brief AD717X implementation file.
* Devices: AD7172-2, AD7172-4, AD7173-8, AD7175-2, AD7175-8, AD7176-2
* AD7177-2, AD4111, AD4112, AD4114, AD4115
* @author acozma (andrei.cozma@analog.com)
* dnechita (dan.nechita@analog.com)
*
********************************************************************************
* Copyright 2015(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.
*******************************************************************************/
/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/
#include <stdlib.h>
#include "ad717x.h"
#include "no_os_error.h"
/* Error codes */
#define INVALID_VAL -1 /* Invalid argument */
#define COMM_ERR -2 /* Communication error on receive */
#define TIMEOUT -3 /* A timeout has occured */
/***************************************************************************//**
* @brief Set channel status - Enable/Disable
* @param device - AD717x Device descriptor.
* @param channel_id - Channel ID (number) of the channel whose status is to be set.
* @param channel_status - Required status of the channel-True in case of Enable
* and False in case of Disable
* @return Returns 0 for success or negative error code in case of failure.
*******************************************************************************/
int ad717x_set_channel_status(ad717x_dev *device, uint8_t channel_id,
bool channel_status)
{
ad717x_st_reg *chn_register;
int ret;
if (!device)
return -EINVAL;
/* Point to the Channel register */
chn_register = AD717X_GetReg(device, AD717X_CHMAP0_REG + channel_id);
if (!chn_register)
return -EINVAL;
if (channel_status)
/* Assign the Channel enable bit and write to channel register */
chn_register->value |= AD717X_CHMAP_REG_CH_EN;
else
chn_register->value &= ~(AD717X_CHMAP_REG_CH_EN);
ret = AD717X_WriteRegister(device, AD717X_CHMAP0_REG + channel_id);
if (ret < 0)
return ret;
device->chan_map[channel_id].channel_enable = channel_status;
return 0;
}
/***************************************************************************//**
* @brief Set ADC Mode
* @param device - AD717x Device Descriptor
* @param adc_mode - ADC Mode to be configured
* @return Returns 0 for success or negative error code in case of failure.
******************************************************************************/
int ad717x_set_adc_mode(ad717x_dev *device, enum ad717x_mode adc_mode)
{
ad717x_st_reg *adc_mode_reg;
if (!device)
return -EINVAL;
/* Retrieve the ADC Mode reigster */
adc_mode_reg = AD717X_GetReg(device, AD717X_ADCMODE_REG);
if (!adc_mode_reg)
return -EINVAL;
/* Clear the Mode[6:4] bits in the ADC Mode Register */
adc_mode_reg->value &= ~(AD717X_ADCMODE_REG_MODE_MSK);
/* Set the required conversion mode, write to register */
adc_mode_reg->value |= AD717X_ADCMODE_REG_MODE(adc_mode);
if (AD717X_WriteRegister(device, AD717X_ADCMODE_REG) < 0)
return -EINVAL;
device->mode = adc_mode;
return 0;
}
/***************************************************************************//**
* @brief Set Analog Inputs to channel
* @param device - AD717x Device Descriptor
* @param channel_id - Channel whose Analog input is to be configured
* @param analog_input - Analog Inputs to the Channel
* @return Returns 0 for success or negative error code in case of failure.
*****************************************************************************/
int ad717x_connect_analog_input(ad717x_dev *device, uint8_t channel_id,
union ad717x_analog_inputs analog_input)
{
ad717x_st_reg *channel_reg;
if (!device)
return -EINVAL;
/* Retrieve the channel register */
channel_reg = AD717X_GetReg(device, AD717X_CHMAP0_REG + channel_id);
if (!channel_reg)
return -EINVAL;
switch ((uint8_t)device->active_device) {
case ID_AD4111 :
case ID_AD4112 :
case ID_AD4114 :
case ID_AD4115 :
/* Clear and Set the required analog input pair to channel */
channel_reg->value &= ~AD717x_CHANNEL_INPUT_MASK;
channel_reg->value |= AD4111_CHMAP_REG_INPUT(analog_input.analog_input_pairs);
if (AD717X_WriteRegister(device, AD717X_CHMAP0_REG + channel_id) < 0)
return -EINVAL;
device->chan_map[channel_id].analog_inputs.analog_input_pairs =
analog_input.analog_input_pairs;
break;
case ID_AD7172_4:
case ID_AD7173_8:
case ID_AD7175_2:
case ID_AD7175_8:
case ID_AD7176_2:
case ID_AD7177_2:
case ID_AD7172_2:
/* Select the Positive Analog Input */
channel_reg->value &= ~AD717X_CHMAP_REG_AINPOS_MSK;
channel_reg->value |= AD717X_CHMAP_REG_AINPOS(
analog_input.ainp.pos_analog_input);
/* Select the Negative Analog Input */
channel_reg->value &= ~AD717X_CHMAP_REG_AINNEG_MSK;
channel_reg->value |= AD717X_CHMAP_REG_AINNEG(
analog_input.ainp.neg_analog_input);
if (AD717X_WriteRegister(device, AD717X_CHMAP0_REG + channel_id) < 0)
return -EINVAL;
device->chan_map[channel_id].analog_inputs.ainp.pos_analog_input =
analog_input.ainp.pos_analog_input;
device->chan_map[channel_id].analog_inputs.ainp.neg_analog_input =
analog_input.ainp.neg_analog_input;
break;
default :
return -EINVAL;
}
return 0;
}
/***************************************************************************//**
* @brief Assign Setup to Channel
* @param device - AD717x Device Descriptor
* @param channel_id - Channel ID (number)
* @param setup - Setup ID (number)
* @return Returns 0 for success or negative error code in case of failure.
******************************************************************************/
int ad717x_assign_setup(ad717x_dev *device, uint8_t channel_id, uint8_t setup)
{
ad717x_st_reg *p_register;
if (!device)
return -EINVAL;
/* Retrieve the Channel Register */
p_register = AD717X_GetReg(device, AD717X_CHMAP0_REG + channel_id);
if (!p_register)
return -EINVAL;
/*
评论5