AD7172 library

Committer:
lucastanio
Date:
Tue May 19 19:41:36 2020 +0000
Revision:
6:694cb68ae500
Parent:
5:eeec01a423be
Child:
7:ba79fe65707d
Comment update

Who changed what in which revision?

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