AD7172 library

Committer:
mmdonatti
Date:
Tue May 05 20:13:43 2020 +0000
Revision:
3:ae9ae6b0b8e0
Parent:
2:8c5d9f069f11
Child:
4:c4a844a34c19
aborting triggering functions with SYNC pin

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