AD7172 library

Revision:
6:694cb68ae500
Parent:
5:eeec01a423be
Child:
7:ba79fe65707d
--- a/ad7172.cpp	Fri May 08 15:12:40 2020 +0000
+++ b/ad7172.cpp	Tue May 19 19:41:36 2020 +0000
@@ -18,60 +18,56 @@
 //
 //////////////////////////////////////////////////////////////////////////////////
 
-/*********************************************************************************
-* Based on Analog Devices AD717X library, focused on performance improvements
-*
-* Special thanks to original authors:
-*		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.
-******************************************************************************/
+//////////////////////////////////////////////////////////////////////////////////
+// Based on Analog Devices AD717X library, focused on performance improvements
+//
+// Special thanks to original authors:
+//		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.
+//////////////////////////////////////////////////////////////////////////////////
+
 #define DEBUG
 #include "ad7172.h"
 
-/******************************************************************************
-* @AD7172 class constructor.
-*
-* @param spi - The handler of the instance of the driver.
-* @param slave_select - The Slave Chip Select Id to be passed to the SPI calls.
-*
-*******************************************************************************/
+// @AD7172 class constructor.
+// @param spi - The handler of the instance of the driver.
+// @param slave_select - The Slave Chip Select Id to be passed to the SPI calls.
 AD7172::AD7172(SPI& p_spi,PinName slave_select,DigitalIn& p_rdy) 
 	: _spi(p_spi), _rdy(p_rdy)
 {
-	
 	_spi.format(8,3); // 8 bits ; POL=1 ; PHASE=1
 	_spi.frequency(10000000); // 10 MHz SPI clock rate
 	continuous_on = 0;
@@ -80,27 +76,20 @@
 	Reset(); // Calling the AD7172-2 restart function
 }
 
-/******************************************************************************
-* @AD7172 Enable Device - CS goes low.
-*
-*******************************************************************************/
+// @AD7172 Enable Device - CS goes low.
+// This function enables the AD7172-2
 void AD7172::enable(){
-	*cs=0;	
+	*cs=0;
 }
 
-/******************************************************************************
-* @AD7172 Disable Device - CS goes high.
-*
-*******************************************************************************/
+// @AD7172 Disable Device - CS goes high.
+// This function disables the AD7172-2
 void AD7172::disable(){
 	*cs=1;
 }
 
-
-/******************************************************************************
-* @AD7172 Configure Continuous Convertion Mode
-*
-*******************************************************************************/
+// @AD7172 Configure Continuous Convertion Mode
+// This function configures the AD7172-2 as continuous read mode. 
 void AD7172::start_continuous()
 {
     if(continuous_on == 0)
@@ -114,10 +103,9 @@
     }
 }
 
-/******************************************************************************
-* @AD7172 Configure Continuous Convertion Mode
-*
-*******************************************************************************/
+// @AD7172 Configure Continuous Convertion Mode
+// This function performs the AD7172-2 exit from the continuous read mode by 
+// doing a dummy read of data register when the ready pin is 0
 void AD7172::stop_continuous()
 {
 	if(continuous_on == 1)
@@ -130,10 +118,9 @@
 	}
 }
 
-/******************************************************************************
-* @AD7172 Read Device ID - Communication Test.
-*
-*******************************************************************************/
+// @AD7172 Read Device ID - Communication Test.
+// This function reads the ID register which has a defined value. 
+// This is the first communicaton test using SPI communication between uC and AD7172-2
 void AD7172::ReadID()
 {
 	*cs=0;
@@ -143,10 +130,8 @@
 	*cs=1;
 }
 
-/******************************************************************************
-* @AD7172 Read Device Status.
-*
-*******************************************************************************/
+// @AD7172 Read Device Status.
+// This function save the value of status register attached with the data regiter
 void AD7172::ReadStatus()
 {
 	*cs=0;
@@ -156,14 +141,9 @@
 	*cs=1;
 }
 
-/***************************************************************************//**
-* @brief Reads the value of the specified register.
-*
-* @reg - The address of the register to be read. The value will be stored
-*         inside the register structure that holds info about this register.
-*
-* @bytes - The number of bytes to be readed
-*******************************************************************************/
+// @AD7172-2 Reads the value of the specified register.
+// @reg - The address of the register to be read.
+// @bytes - The number of bytes to be readed
 void AD7172::ReadRegister(uint8_t reg, uint8_t bytes)
 {
 	data.data = 0;
@@ -179,14 +159,9 @@
 	*cs = 1;
 }
 
-/**************************************************************************//**
-* @brief Reads the value of the specified register.
-*
-* @reg - The address of the register to be read. The value will be stored
-*         inside the register structure that holds info about this register.
-*
-* @bytes - The number of bytes to be stored (data saved on data variable)
-*******************************************************************************/
+// @AD7172-2 Writes a value to the specified register.
+// @reg - The address of the register to be read.
+// @bytes - The number of bytes to be stored (data saved on data variable)
 void AD7172::WriteRegister(uint8_t reg, uint8_t bytes)
 {
 	*cs = 0;
@@ -196,10 +171,7 @@
 	*cs = 1;
 }
 
-/***************************************************************************//**
-* @brief Resets the device.
-*
-*******************************************************************************/
+// @AD7172-2 Resets the device.
 void AD7172::Reset()
 {
 	*cs=0;
@@ -209,12 +181,9 @@
 
 }
 
-/***************************************************************************//**
-* @brief Waits until a new conversion result is available.
-*
-* @param device - The handler of the instance of the driver.
-*
-*******************************************************************************/
+///////////////////////////////////////////////////////////////////////////////
+// @brief Waits until a new conversion result is available.
+// @param device - The handler of the instance of the driver.
 void AD7172::WaitForReady(uint32_t timeout)
 {
 	while(sw_ready==0 && --timeout)
@@ -224,10 +193,8 @@
 	}
 }
 
-/***************************************************************************//****************************************************************************
-* @brief Reads the conversion result from the device using data register.
-*
-*******************************************************************************/
+////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// @AD7172-2 Reads the conversion result from the device using data register.
 void AD7172::ReadDataRegister()
 {
 	_spi.write(0x40|AD7172_DATA_REG);
@@ -236,10 +203,8 @@
 		data.bytes[i] = _spi.write(0x00);
 }
 
-/***************************************************************************//**
-* @brief Reads the conversion result from the device using data register.
-*
-*******************************************************************************/
+// @AD7172-2 Reads the conversion result from the device using data register and
+// the status register. The channel which the conversion is from is also retrieved
 void AD7172::ReadDataRegisterStatus()
 {
 	_spi.write(0x40|AD7172_DATA_REG);
@@ -250,10 +215,8 @@
 	channel = status&0b11;
 }
 
-/***************************************************************************//********************************************************************************
-* @brief Reads the conversion result from the device for continuous read mode
-*
-*******************************************************************************/
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+// @brief Reads the conversion result from the device for continuous read mode
 void AD7172::ReadDataContinuous()
 {
 	data.data=0;
@@ -261,10 +224,9 @@
 		data.bytes[i] = _spi.write(0x00);
 }
 
-/***************************************************************************//**
-* @brief Reads the conversion result from the device for continuous read mode if DATA_STAT bit set in IFMODE register
-*
-*******************************************************************************/
+// @AD7172-2 Reads the conversion result from the device for continuous read mode 
+// if DATA_STAT bit set in IFMODE register. The channel which the conversion is
+// from is also retrieved
 void AD7172::ReadDataContinuousStatus()
 {
 	data.data=0;