Mauricio Donatti / AD7172-2
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ad7172.cpp Source File

ad7172.cpp

00001 /*********************************************************************
00002 *   @file    AD7172.cpp
00003 *   @brief   AD7172 implementation file.
00004 *   @devices AD7172-2
00005 *   @author  
00006 *
00007 *       Mauricio Donatti - mauricio.donatti@lnls.br
00008 *       Lucas Tanio - lucas.tanio@lnls.br
00009 *
00010 *       LNLS - Brazilian Synchrotron Light Source
00011 *       GIE - Electronics Instrumentation Group
00012 *
00013 *       2020, April
00014 *
00015 *   Future Implementation:
00016 *
00017 *********************************************************************/
00018 
00019 /*********************************************************************
00020 * Based on Analog Devices AD717X library, focused on performance improvements
00021 *
00022 * Special thanks to original authors:
00023 *       acozma (andrei.cozma@analog.com)
00024 *       dnechita (dan.nechita@analog.com)
00025 *
00026 * Copyright 2015(c) Analog Devices, Inc.
00027 *
00028 * All rights reserved.
00029 *
00030 * Redistribution and use in source and binary forms, with or without modification,
00031 * are permitted provided that the following conditions are met:
00032 *  - Redistributions of source code must retain the above copyright
00033 *    notice, this list of conditions and the following disclaimer.
00034 *  - Redistributions in binary form must reproduce the above copyright
00035 *    notice, this list of conditions and the following disclaimer in
00036 *    the documentation and/or other materials provided with the
00037 *    distribution.
00038 *  - Neither the name of Analog Devices, Inc. nor the names of its
00039 *    contributors may be used to endorse or promote products derived
00040 *    from this software without specific prior written permission.
00041 *  - The use of this software may or may not infringe the patent rights
00042 *    of one or more patent holders.  This license does not release you
00043 *    from the requirement that you obtain separate licenses from these
00044 *    patent holders to use this software.
00045 *  - Use of the software either in source or binary form, must be run
00046 *    on or directly connected to an Analog Devices Inc. component.
00047 *
00048 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
00049 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
00050 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00051 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00052 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00053 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00054 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00055 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00056 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00057 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00058 *********************************************************************/
00059 
00060 //#define DEBUG //Debug interface
00061 
00062 #include "ad7172.h" //Importing AD7172 library
00063 
00064 //@AD7172 class constructor.
00065 //@param spi - The handler of the instance of the driver.
00066 //@param slave_select - The Slave Chip Select Id to be passed to the SPI calls.
00067 AD7172::AD7172(SPI& p_spi,PinName slave_select,DigitalIn& p_rdy)
00068     : _spi(p_spi), _rdy(p_rdy)
00069 {
00070     _spi.format(8,3); //8 bits ; POL=1 ; PHASE=1
00071     _spi.frequency(10000000); //20 MHz SPI clock rate
00072     continuous_on = 0; //Continuous read mode flag
00073     cs = new DigitalOut(slave_select); //Define cs as digital out variable referred to slave_select pin
00074     Reset(); //Calling the AD7172-2 restart function
00075 }
00076 
00077 //@AD7172 Enable Device - CS goes low.
00078 //This function enables the AD7172-2
00079 void AD7172::enable(){
00080     *cs=0;
00081 }
00082 
00083 //@AD7172 Disable Device - CS goes high.
00084 //This function disables the AD7172-2
00085 void AD7172::disable(){
00086     *cs=1;
00087 }
00088 
00089 //@AD7172 Configure Continuous Read Mode
00090 //This function configures the AD7172-2 as continuous read mode. 
00091 void AD7172::start_continuous()
00092 {
00093     if(continuous_on == 0)
00094     {
00095         data.data = (AD7172_IFMODE_REG_CONT_READ)|AD7172_IFMODE_REG_DATA_STAT;
00096         AD7172_PRINTF("CONTCONV");
00097         AD7172_PRINTF("Register: IFMODE\tWrite: 0x%04X",data.data);
00098         WriteRegister(AD7172_IFMODE_REG,2); // Writing to IFMODE register
00099         enable(); // *cs = 0
00100         continuous_on = 1;
00101     }
00102 }
00103 
00104 //@AD7172 Configure Continuous Convertion Mode
00105 //This function performs the AD7172-2 exit from the continuous read mode by 
00106 //doing a dummy read of data register when the ready pin is 0
00107 void AD7172::stop_continuous()
00108 {
00109     if(continuous_on == 1)
00110     {
00111         *cs=0;
00112         while(_rdy == 1){}
00113         _spi.write(0x44);
00114         *cs=1;
00115         continuous_on =0;
00116     }
00117     
00118 }
00119 
00120 //@AD7172 Read Device ID - Communication Test.
00121 //This function reads the ID register which has a defined value. 
00122 //This is the first communicaton test using SPI communication between uC and AD7172-2
00123 void AD7172::ReadID()
00124 {
00125     *cs=0;
00126     _spi.write(0x40|AD7172_ID_REG); //Configures the communication register to read the ID address
00127     id.bytes[1] = _spi.write(0x00); //Read the two most significant bytes 
00128     id.bytes[0] = _spi.write(0x00); //Read the two lesat significant bytes 
00129     *cs=1;
00130 }
00131 
00132 //@AD7172 Read Device Status.
00133 //This function save the value of status register attached with the data regiter
00134 void AD7172::ReadStatus()
00135 {
00136     *cs=0;
00137     _spi.write(0x40|AD7172_STATUS_REG);
00138     status = _spi.write(0x00);
00139     sw_ready = (status>>7)^1;
00140     *cs=1;
00141 }
00142 
00143 //@AD7172-2 Reads the data of the specified register.
00144 //@reg - Register address to be read.
00145 //@bytes - The number of bytes to be read
00146 void AD7172::ReadRegister(uint8_t reg, uint8_t bytes)
00147 {
00148     data.data = 0;
00149     *cs = 0;
00150     _spi.write(0x40|reg);
00151     for(i=bytes-1;i>=0;i--)
00152     {
00153         data.bytes[i] = _spi.write(0x00);
00154         //AD7172_PRINTF("Data: 0x%02X",data.bytes[i]);
00155     }
00156     //AD7172_PRINTF("data.data 0x%02X", data.data);
00157     *cs = 1;
00158 }
00159 
00160 //@AD7172-2 Writes a value to the specified register.
00161 //@reg - The address of the register to be read.
00162 //@bytes - The number of bytes to be stored (data saved on data variable)
00163 void AD7172::WriteRegister(uint8_t reg, uint8_t bytes)
00164 {
00165     *cs = 0;
00166     _spi.write(reg);
00167     for(i=bytes-1;i>=0;i--)
00168         _spi.write(data.bytes[i]);
00169     *cs = 1;
00170 }
00171 
00172 //@AD7172-2 Resets the device.
00173 void AD7172::Reset()
00174 {
00175     *cs=0;
00176     for(i=0;i<=7;i++)
00177         _spi.write(0xFF);
00178     *cs=1;
00179 }
00180 
00181 //@brief Waits until a new conversion result is available.
00182 //@param device - The handler of the instance of the driver.
00183 void AD7172::WaitForReady(uint32_t timeout)
00184 {
00185     while(sw_ready==0 && --timeout)
00186     {
00187         //Read the value of the Status Register, updating ready variable
00188         ReadStatus();
00189     }
00190 }
00191 
00192 //@AD7172-2 Reads the conversion result from the device using data register.
00193 void AD7172::ReadDataRegister()
00194 {
00195     _spi.write(0x40|AD7172_DATA_REG);
00196     data.data=0;
00197     for(i=2;i>=0;i--)
00198         data.bytes[i] = _spi.write(0x00);
00199 }
00200 
00201 //@AD7172-2 Reads the conversion result from the device using data register and
00202 //the status register. The channel which the conversion is from is also retrieved
00203 void AD7172::ReadDataRegisterStatus()
00204 {
00205     _spi.write(0x40|AD7172_DATA_REG);
00206     data.data=0;
00207     for(i=2;i>=0;i--)
00208         data.bytes[i] = _spi.write(0x00);
00209     status = _spi.write(0x00);
00210     channel = status&0b11;
00211 }
00212 
00213 //@AD7172-2 Reads the conversion result in continuous read mode
00214 void AD7172::ReadDataContinuous()
00215 {
00216     data.data=0;
00217     for(i=2;i>=0;i--)
00218         data.bytes[i] = _spi.write(0x00);
00219 }
00220 
00221 //@AD7172-2 Reads the conversion result in continuous read mode when the status
00222 //byte is appended to the data bytes (DATA_STAT bit set in IFMODE register).
00223 void AD7172::ReadDataContinuousStatus()
00224 {
00225     data.data=0;
00226     for(i=2;i>=0;i--)
00227         data.bytes[i] = _spi.write(0x00);
00228     status = _spi.write(0x00);
00229     channel = status&0b11;
00230 }