AD7172 library

Committer:
mmdonatti
Date:
Mon Nov 16 13:59:15 2020 +0000
Revision:
11:ea7a6f42c9bf
Parent:
10:62e78c16bb4d
new version with debug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lucastanio 7:ba79fe65707d 1 /*********************************************************************
lucastanio 7:ba79fe65707d 2 * @file AD7172.cpp
lucastanio 7:ba79fe65707d 3 * @brief AD7172 implementation file.
lucastanio 7:ba79fe65707d 4 * @devices AD7172-2
lucastanio 7:ba79fe65707d 5 * @author
lucastanio 7:ba79fe65707d 6 *
lucastanio 7:ba79fe65707d 7 * Mauricio Donatti - mauricio.donatti@lnls.br
lucastanio 7:ba79fe65707d 8 * Lucas Tanio - lucas.tanio@lnls.br
lucastanio 7:ba79fe65707d 9 *
lucastanio 7:ba79fe65707d 10 * LNLS - Brazilian Synchrotron Light Source
lucastanio 7:ba79fe65707d 11 * GIE - Electronics Instrumentation Group
lucastanio 7:ba79fe65707d 12 *
lucastanio 7:ba79fe65707d 13 * 2020, April
lucastanio 7:ba79fe65707d 14 *
lucastanio 7:ba79fe65707d 15 * Future Implementation:
lucastanio 7:ba79fe65707d 16 *
lucastanio 7:ba79fe65707d 17 *********************************************************************/
mmdonatti 0:e258a1597fe1 18
lucastanio 7:ba79fe65707d 19 /*********************************************************************
lucastanio 7:ba79fe65707d 20 * Based on Analog Devices AD717X library, focused on performance improvements
lucastanio 7:ba79fe65707d 21 *
lucastanio 7:ba79fe65707d 22 * Special thanks to original authors:
lucastanio 7:ba79fe65707d 23 * acozma (andrei.cozma@analog.com)
lucastanio 7:ba79fe65707d 24 * dnechita (dan.nechita@analog.com)
lucastanio 7:ba79fe65707d 25 *
lucastanio 7:ba79fe65707d 26 * Copyright 2015(c) Analog Devices, Inc.
lucastanio 7:ba79fe65707d 27 *
lucastanio 7:ba79fe65707d 28 * All rights reserved.
lucastanio 7:ba79fe65707d 29 *
lucastanio 7:ba79fe65707d 30 * Redistribution and use in source and binary forms, with or without modification,
lucastanio 7:ba79fe65707d 31 * are permitted provided that the following conditions are met:
lucastanio 7:ba79fe65707d 32 * - Redistributions of source code must retain the above copyright
lucastanio 7:ba79fe65707d 33 * notice, this list of conditions and the following disclaimer.
lucastanio 7:ba79fe65707d 34 * - Redistributions in binary form must reproduce the above copyright
lucastanio 7:ba79fe65707d 35 * notice, this list of conditions and the following disclaimer in
lucastanio 7:ba79fe65707d 36 * the documentation and/or other materials provided with the
lucastanio 7:ba79fe65707d 37 * distribution.
lucastanio 7:ba79fe65707d 38 * - Neither the name of Analog Devices, Inc. nor the names of its
lucastanio 7:ba79fe65707d 39 * contributors may be used to endorse or promote products derived
lucastanio 7:ba79fe65707d 40 * from this software without specific prior written permission.
lucastanio 7:ba79fe65707d 41 * - The use of this software may or may not infringe the patent rights
lucastanio 7:ba79fe65707d 42 * of one or more patent holders. This license does not release you
lucastanio 7:ba79fe65707d 43 * from the requirement that you obtain separate licenses from these
lucastanio 7:ba79fe65707d 44 * patent holders to use this software.
lucastanio 7:ba79fe65707d 45 * - Use of the software either in source or binary form, must be run
lucastanio 7:ba79fe65707d 46 * on or directly connected to an Analog Devices Inc. component.
lucastanio 7:ba79fe65707d 47 *
lucastanio 7:ba79fe65707d 48 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
lucastanio 7:ba79fe65707d 49 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
lucastanio 7:ba79fe65707d 50 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
lucastanio 7:ba79fe65707d 51 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
lucastanio 7:ba79fe65707d 52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
lucastanio 7:ba79fe65707d 53 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
lucastanio 7:ba79fe65707d 54 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
lucastanio 7:ba79fe65707d 55 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
lucastanio 7:ba79fe65707d 56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
lucastanio 7:ba79fe65707d 57 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lucastanio 7:ba79fe65707d 58 *********************************************************************/
lucastanio 6:694cb68ae500 59
lucastanio 10:62e78c16bb4d 60 //#define DEBUG //Debug interface
lucastanio 7:ba79fe65707d 61
lucastanio 7:ba79fe65707d 62 #include "ad7172.h" //Importing AD7172 library
mmdonatti 0:e258a1597fe1 63
lucastanio 7:ba79fe65707d 64 //@AD7172 class constructor.
lucastanio 7:ba79fe65707d 65 //@param spi - The handler of the instance of the driver.
lucastanio 7:ba79fe65707d 66 //@param slave_select - The Slave Chip Select Id to be passed to the SPI calls.
lucastanio 8:8466106f2e1b 67 AD7172::AD7172(SPI& p_spi,PinName slave_select,DigitalIn& p_rdy)
mmdonatti 0:e258a1597fe1 68 : _spi(p_spi), _rdy(p_rdy)
mmdonatti 0:e258a1597fe1 69 {
lucastanio 7:ba79fe65707d 70 _spi.format(8,3); //8 bits ; POL=1 ; PHASE=1
mmdonatti 11:ea7a6f42c9bf 71 _spi.frequency(10000000); //20 MHz SPI clock rate
lucastanio 8:8466106f2e1b 72 continuous_on = 0; //Continuous read mode flag
lucastanio 7:ba79fe65707d 73 cs = new DigitalOut(slave_select); //Define cs as digital out variable referred to slave_select pin
lucastanio 7:ba79fe65707d 74 Reset(); //Calling the AD7172-2 restart function
mmdonatti 0:e258a1597fe1 75 }
mmdonatti 0:e258a1597fe1 76
lucastanio 7:ba79fe65707d 77 //@AD7172 Enable Device - CS goes low.
lucastanio 7:ba79fe65707d 78 //This function enables the AD7172-2
mmdonatti 0:e258a1597fe1 79 void AD7172::enable(){
lucastanio 6:694cb68ae500 80 *cs=0;
mmdonatti 0:e258a1597fe1 81 }
mmdonatti 0:e258a1597fe1 82
lucastanio 7:ba79fe65707d 83 //@AD7172 Disable Device - CS goes high.
lucastanio 7:ba79fe65707d 84 //This function disables the AD7172-2
mmdonatti 0:e258a1597fe1 85 void AD7172::disable(){
lucastanio 1:6d78a35bedd0 86 *cs=1;
mmdonatti 0:e258a1597fe1 87 }
mmdonatti 0:e258a1597fe1 88
lucastanio 7:ba79fe65707d 89 //@AD7172 Configure Continuous Read Mode
lucastanio 7:ba79fe65707d 90 //This function configures the AD7172-2 as continuous read mode.
mmdonatti 3:ae9ae6b0b8e0 91 void AD7172::start_continuous()
mmdonatti 0:e258a1597fe1 92 {
mmdonatti 5:eeec01a423be 93 if(continuous_on == 0)
mmdonatti 5:eeec01a423be 94 {
mmdonatti 5:eeec01a423be 95 data.data = (AD7172_IFMODE_REG_CONT_READ)|AD7172_IFMODE_REG_DATA_STAT;
mmdonatti 5:eeec01a423be 96 AD7172_PRINTF("CONTCONV");
mmdonatti 5:eeec01a423be 97 AD7172_PRINTF("Register: IFMODE\tWrite: 0x%04X",data.data);
mmdonatti 5:eeec01a423be 98 WriteRegister(AD7172_IFMODE_REG,2); // Writing to IFMODE register
mmdonatti 5:eeec01a423be 99 enable(); // *cs = 0
mmdonatti 5:eeec01a423be 100 continuous_on = 1;
mmdonatti 5:eeec01a423be 101 }
mmdonatti 0:e258a1597fe1 102 }
mmdonatti 0:e258a1597fe1 103
lucastanio 7:ba79fe65707d 104 //@AD7172 Configure Continuous Convertion Mode
lucastanio 7:ba79fe65707d 105 //This function performs the AD7172-2 exit from the continuous read mode by
lucastanio 7:ba79fe65707d 106 //doing a dummy read of data register when the ready pin is 0
mmdonatti 3:ae9ae6b0b8e0 107 void AD7172::stop_continuous()
mmdonatti 0:e258a1597fe1 108 {
mmdonatti 5:eeec01a423be 109 if(continuous_on == 1)
lucastanio 4:c4a844a34c19 110 {
mmdonatti 5:eeec01a423be 111 *cs=0;
mmdonatti 5:eeec01a423be 112 while(_rdy == 1){}
lucastanio 4:c4a844a34c19 113 _spi.write(0x44);
mmdonatti 5:eeec01a423be 114 *cs=1;
mmdonatti 5:eeec01a423be 115 continuous_on =0;
lucastanio 4:c4a844a34c19 116 }
lucastanio 10:62e78c16bb4d 117
lucastanio 1:6d78a35bedd0 118 }
lucastanio 1:6d78a35bedd0 119
lucastanio 7:ba79fe65707d 120 //@AD7172 Read Device ID - Communication Test.
lucastanio 7:ba79fe65707d 121 //This function reads the ID register which has a defined value.
lucastanio 7:ba79fe65707d 122 //This is the first communicaton test using SPI communication between uC and AD7172-2
mmdonatti 0:e258a1597fe1 123 void AD7172::ReadID()
mmdonatti 0:e258a1597fe1 124 {
mmdonatti 0:e258a1597fe1 125 *cs=0;
lucastanio 8:8466106f2e1b 126 _spi.write(0x40|AD7172_ID_REG); //Configures the communication register to read the ID address
lucastanio 8:8466106f2e1b 127 id.bytes[1] = _spi.write(0x00); //Read the two most significant bytes
lucastanio 8:8466106f2e1b 128 id.bytes[0] = _spi.write(0x00); //Read the two lesat significant bytes
mmdonatti 0:e258a1597fe1 129 *cs=1;
mmdonatti 0:e258a1597fe1 130 }
mmdonatti 0:e258a1597fe1 131
lucastanio 7:ba79fe65707d 132 //@AD7172 Read Device Status.
lucastanio 7:ba79fe65707d 133 //This function save the value of status register attached with the data regiter
mmdonatti 0:e258a1597fe1 134 void AD7172::ReadStatus()
mmdonatti 0:e258a1597fe1 135 {
mmdonatti 0:e258a1597fe1 136 *cs=0;
mmdonatti 0:e258a1597fe1 137 _spi.write(0x40|AD7172_STATUS_REG);
mmdonatti 0:e258a1597fe1 138 status = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 139 sw_ready = (status>>7)^1;
mmdonatti 0:e258a1597fe1 140 *cs=1;
mmdonatti 0:e258a1597fe1 141 }
mmdonatti 0:e258a1597fe1 142
lucastanio 7:ba79fe65707d 143 //@AD7172-2 Reads the data of the specified register.
lucastanio 7:ba79fe65707d 144 //@reg - Register address to be read.
lucastanio 7:ba79fe65707d 145 //@bytes - The number of bytes to be read
lucastanio 1:6d78a35bedd0 146 void AD7172::ReadRegister(uint8_t reg, uint8_t bytes)
mmdonatti 0:e258a1597fe1 147 {
lucastanio 4:c4a844a34c19 148 data.data = 0;
lucastanio 1:6d78a35bedd0 149 *cs = 0;
mmdonatti 0:e258a1597fe1 150 _spi.write(0x40|reg);
mmdonatti 0:e258a1597fe1 151 for(i=bytes-1;i>=0;i--)
lucastanio 4:c4a844a34c19 152 {
mmdonatti 0:e258a1597fe1 153 data.bytes[i] = _spi.write(0x00);
lucastanio 4:c4a844a34c19 154 //AD7172_PRINTF("Data: 0x%02X",data.bytes[i]);
lucastanio 4:c4a844a34c19 155 }
lucastanio 4:c4a844a34c19 156 //AD7172_PRINTF("data.data 0x%02X", data.data);
lucastanio 1:6d78a35bedd0 157 *cs = 1;
mmdonatti 0:e258a1597fe1 158 }
mmdonatti 0:e258a1597fe1 159
lucastanio 7:ba79fe65707d 160 //@AD7172-2 Writes a value to the specified register.
lucastanio 7:ba79fe65707d 161 //@reg - The address of the register to be read.
lucastanio 7:ba79fe65707d 162 //@bytes - The number of bytes to be stored (data saved on data variable)
lucastanio 1:6d78a35bedd0 163 void AD7172::WriteRegister(uint8_t reg, uint8_t bytes)
mmdonatti 0:e258a1597fe1 164 {
lucastanio 1:6d78a35bedd0 165 *cs = 0;
mmdonatti 0:e258a1597fe1 166 _spi.write(reg);
mmdonatti 0:e258a1597fe1 167 for(i=bytes-1;i>=0;i--)
mmdonatti 0:e258a1597fe1 168 _spi.write(data.bytes[i]);
lucastanio 1:6d78a35bedd0 169 *cs = 1;
mmdonatti 0:e258a1597fe1 170 }
mmdonatti 0:e258a1597fe1 171
lucastanio 7:ba79fe65707d 172 //@AD7172-2 Resets the device.
mmdonatti 0:e258a1597fe1 173 void AD7172::Reset()
mmdonatti 0:e258a1597fe1 174 {
mmdonatti 0:e258a1597fe1 175 *cs=0;
mmdonatti 0:e258a1597fe1 176 for(i=0;i<=7;i++)
mmdonatti 0:e258a1597fe1 177 _spi.write(0xFF);
mmdonatti 0:e258a1597fe1 178 *cs=1;
mmdonatti 0:e258a1597fe1 179 }
mmdonatti 0:e258a1597fe1 180
lucastanio 7:ba79fe65707d 181 //@brief Waits until a new conversion result is available.
lucastanio 7:ba79fe65707d 182 //@param device - The handler of the instance of the driver.
mmdonatti 0:e258a1597fe1 183 void AD7172::WaitForReady(uint32_t timeout)
mmdonatti 0:e258a1597fe1 184 {
mmdonatti 0:e258a1597fe1 185 while(sw_ready==0 && --timeout)
mmdonatti 0:e258a1597fe1 186 {
mmdonatti 0:e258a1597fe1 187 //Read the value of the Status Register, updating ready variable
mmdonatti 0:e258a1597fe1 188 ReadStatus();
mmdonatti 0:e258a1597fe1 189 }
mmdonatti 0:e258a1597fe1 190 }
mmdonatti 0:e258a1597fe1 191
lucastanio 7:ba79fe65707d 192 //@AD7172-2 Reads the conversion result from the device using data register.
mmdonatti 0:e258a1597fe1 193 void AD7172::ReadDataRegister()
mmdonatti 0:e258a1597fe1 194 {
mmdonatti 0:e258a1597fe1 195 _spi.write(0x40|AD7172_DATA_REG);
mmdonatti 0:e258a1597fe1 196 data.data=0;
lucastanio 9:e71f2bdd2f83 197 for(i=2;i>=0;i--)
mmdonatti 0:e258a1597fe1 198 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 199 }
mmdonatti 0:e258a1597fe1 200
lucastanio 7:ba79fe65707d 201 //@AD7172-2 Reads the conversion result from the device using data register and
lucastanio 7:ba79fe65707d 202 //the status register. The channel which the conversion is from is also retrieved
mmdonatti 0:e258a1597fe1 203 void AD7172::ReadDataRegisterStatus()
mmdonatti 0:e258a1597fe1 204 {
mmdonatti 0:e258a1597fe1 205 _spi.write(0x40|AD7172_DATA_REG);
mmdonatti 0:e258a1597fe1 206 data.data=0;
lucastanio 9:e71f2bdd2f83 207 for(i=2;i>=0;i--)
mmdonatti 0:e258a1597fe1 208 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 209 status = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 210 channel = status&0b11;
mmdonatti 0:e258a1597fe1 211 }
mmdonatti 0:e258a1597fe1 212
lucastanio 7:ba79fe65707d 213 //@AD7172-2 Reads the conversion result in continuous read mode
mmdonatti 0:e258a1597fe1 214 void AD7172::ReadDataContinuous()
mmdonatti 0:e258a1597fe1 215 {
mmdonatti 0:e258a1597fe1 216 data.data=0;
lucastanio 9:e71f2bdd2f83 217 for(i=2;i>=0;i--)
mmdonatti 0:e258a1597fe1 218 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 219 }
mmdonatti 0:e258a1597fe1 220
lucastanio 7:ba79fe65707d 221 //@AD7172-2 Reads the conversion result in continuous read mode when the status
lucastanio 7:ba79fe65707d 222 //byte is appended to the data bytes (DATA_STAT bit set in IFMODE register).
mmdonatti 0:e258a1597fe1 223 void AD7172::ReadDataContinuousStatus()
mmdonatti 0:e258a1597fe1 224 {
mmdonatti 0:e258a1597fe1 225 data.data=0;
lucastanio 9:e71f2bdd2f83 226 for(i=2;i>=0;i--)
mmdonatti 0:e258a1597fe1 227 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 228 status = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 229 channel = status&0b11;
mmdonatti 0:e258a1597fe1 230 }