/***************************************************************************//**
* @file AD717X.c
* @brief AD717X implementation file.
* @devices AD7172-2, AD7172-4, AD7173-8, AD7175-2, AD7175-8, AD7176-2
* AD7177-2
* @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 "Communication.h"
#include "AD717x.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 Searches through the list of registers of the driver instance and
* retrieves a pointer to the register that matches the given address.
* @param device - The handler of the instance of the driver.
* @param reg_address - The address to be used to find the register.
*
* @return A pointer to the register if found or 0.
*******************************************************************************/
ad717x_st_reg *AD717X_GetReg(struct ad717x_device *device, uint8_t reg_address)
{
uint8_t i;
ad717x_st_reg *reg = 0;
if (!device || !device->regs)
return 0;
for (i = 0; i < device->num_regs; i++)
{
if (device->regs[i].addr == reg_address)
{
reg = &device->regs[i];
break;
}
}
return reg;
}
/***************************************************************************//**
* @brief Reads the value of the specified register.
*
* @param device - The handler of the instance of the driver.
* @addr - The address of the register to be read. The value will be stored
* inside the register structure that holds info about this register.
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD717X_ReadRegister(struct ad717x_device *device, uint8_t addr)
{
int32_t ret = 0;
uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;
uint8_t check8 = 0;
uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
ad717x_st_reg *pReg;
if(!device)
return INVALID_VAL;
pReg = AD717X_GetReg(device, addr);
if (!pReg)
return INVALID_VAL;
/* Build the Command word */
buffer[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD |
AD717X_COMM_REG_RA(pReg->addr);
/* Read data from the device */
ret = SPI_Read(device->slave_select_id,
buffer,
((device->useCRC != AD717X_DISABLE) ? pReg->size + 1
: pReg->size) + 1);
if(ret < 0)
return ret;
/* Check the CRC */
if(device->useCRC == AD717X_USE_CRC)
{
msgBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD |
AD717X_COMM_REG_RA(pReg->addr);
for(i = 1; i < pReg->size + 2; ++i)
{
msgBuf[i] = buffer[i];
}
check8 = AD717X_ComputeCRC8(msgBuf, pReg->size + 2);
}
if(device->useCRC == AD717X_USE_XOR)
{
msgBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD |
AD717X_COMM_REG_RA(pReg->addr);
for(i = 1; i < pReg->size + 2; ++i)
{
msgBuf[i] = buffer[i];
}
check8 = AD717X_ComputeXOR8(msgBuf, pReg->size + 2);
}
if(check8 != 0)
{
/* ReadRegister checksum failed. */
return COMM_ERR;
}
/* Build the result */
pReg->value = 0;
for(i = 1; i < pReg->size + 1; i++)
{
pReg->value <<= 8;
pReg->value += buffer[i];
}
return ret;
}
/***************************************************************************//**
* @brief Writes the value of the specified register.
*
* @param device - The handler of the instance of the driver.
* @param reg - Register structure holding info about the register to be written
* @param addr - The address of the register to be written with the value stored
* inside the register structure that holds info about this
* register.
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD717X_WriteRegister(struct ad717x_device *device, uint8_t addr)
{
int32_t ret = 0;
int32_t regValue = 0;
uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint8_t i = 0;
uint8_t crc8 = 0;
ad717x_st_reg *preg;
if(!device)
return INVALID_VAL;
preg = AD717X_GetReg(device, addr);
if (!preg)
return INVALID_VAL;
/* Build the Command word */
wrBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_WR |
AD717X_COMM_REG_RA(preg->addr);
/* Fill the write buffer */
regValue = preg->value;
for(i = 0; i < preg->size; i++)
{
wrBuf[preg->size - i] = regValue & 0xFF;
regValue >>= 8;
}
/* Compute the CRC */
if(device->useCRC != AD717X_DISABLE)
{
crc8 = AD717X_ComputeCRC8(wrBuf, preg->size + 1);
wrBuf[preg->size + 1] = crc8;
}
/* Write data to the device */
ret = SPI_Write(device->slave_select_id,
wrBuf,
(device->useCRC != AD717X_DISABLE) ? preg->size + 2
: preg->size + 1);
return ret;
}
/***************************************************************************//**
* @brief Resets the device.
*
* @param device - The handler of the instance of the driver.
*
* @return Returns 0 for success or negative error code.
*******************************************************************************/
int32_t AD717X_Reset(struct ad717x_device *device)
{
int32_t ret = 0;
uint8_t wrBuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
if(!device)
return INVALID_VAL;
ret = SPI_Write(device->slave_select_id, wrBuf, 8);
return ret;
}
AD7177-2 AD7175-2, AD7172-2 AD717X-X多路复用模数转换器驱动C源码.zip
版权申诉
5星 · 超过95%的资源 87 浏览量
2022-04-12
11:13:02
上传
评论 1
收藏 12KB ZIP 举报
探索者我有我路向
- 粉丝: 283
- 资源: 1816
最新资源
- Windows 常见运行运行库32+64
- 基于3KW光伏并网单相逆变器设计(TMS320F28035控制板+显示板+STM32F103功率板)硬件(原理图+PCB)工程
- 正点原子HAL库 STM32F4 外部中断(学习自用附源码)
- delphi rzcombobox DropDownList 灰色背景改为白色
- sap sd.docsap sd.doc
- torch-1.10.2-cp38-cp38-win-amd64.whl
- 菜单栏实现增加数据,修改数据,查询数据,删除数据
- 全国省市区三级联动json文件,带code
- C8_全局&局部&static.zip
- Unity和安卓交互插件Unity调Android Native Goodies PRO
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页