AD7172 library

Committer:
lucastanio
Date:
Tue May 05 17:57:07 2020 +0000
Revision:
1:6d78a35bedd0
Parent:
0:e258a1597fe1
Child:
2:8c5d9f069f11
Sync pin  - SINGCONV

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 0:e258a1597fe1 71 AD7172::AD7172(SPI& p_spi,PinName slave_select,DigitalIn& p_rdy,PinName sync_pin)
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
lucastanio 1:6d78a35bedd0 79 sync = new DigitalOut(sync_pin); // Define sync as digital out variable referred to sync_pin
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 0:e258a1597fe1 104 void AD7172::config_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 /******************************************************************************
lucastanio 1:6d78a35bedd0 114 * @AD7172 Configure Single Convertion Mode
mmdonatti 0:e258a1597fe1 115 *
mmdonatti 0:e258a1597fe1 116 *******************************************************************************/
mmdonatti 0:e258a1597fe1 117 void AD7172::config_single()
mmdonatti 0:e258a1597fe1 118 {
lucastanio 1:6d78a35bedd0 119 //AGUARDAR SYNC
lucastanio 1:6d78a35bedd0 120 data.data = AD7172_IFMODE_REG_DATA_STAT|AD7172_IFMODE_REG_ALT_SYNC; //CONTREAD = 0;
mmdonatti 0:e258a1597fe1 121 AD7172_PRINTF("SINGCONV");
lucastanio 1:6d78a35bedd0 122 AD7172_PRINTF("Register: IFMODE\tWrite: 0x%04X",data.data);
lucastanio 1:6d78a35bedd0 123 WriteRegister(AD7172_IFMODE_REG,2); // Writing to IFMODE register
lucastanio 1:6d78a35bedd0 124 enable(); // *cs = 0
mmdonatti 0:e258a1597fe1 125 }
mmdonatti 0:e258a1597fe1 126
mmdonatti 0:e258a1597fe1 127 /******************************************************************************
lucastanio 1:6d78a35bedd0 128 * @AD7172 Sync Enable
lucastanio 1:6d78a35bedd0 129 *
lucastanio 1:6d78a35bedd0 130 *******************************************************************************/
lucastanio 1:6d78a35bedd0 131 void AD7172::sync_enable()
lucastanio 1:6d78a35bedd0 132 {
lucastanio 1:6d78a35bedd0 133 *sync = 0;
lucastanio 1:6d78a35bedd0 134 }
lucastanio 1:6d78a35bedd0 135
lucastanio 1:6d78a35bedd0 136 /******************************************************************************
lucastanio 1:6d78a35bedd0 137 * @AD7172 Sync Disable
lucastanio 1:6d78a35bedd0 138 *
lucastanio 1:6d78a35bedd0 139 *******************************************************************************/
lucastanio 1:6d78a35bedd0 140 void AD7172::sync_disable()
lucastanio 1:6d78a35bedd0 141 {
lucastanio 1:6d78a35bedd0 142 *sync = 1;
lucastanio 1:6d78a35bedd0 143 }
lucastanio 1:6d78a35bedd0 144
lucastanio 1:6d78a35bedd0 145 /******************************************************************************
lucastanio 1:6d78a35bedd0 146 * @AD7172 Single Convertion Mode Conversion Driver
mmdonatti 0:e258a1597fe1 147 *
mmdonatti 0:e258a1597fe1 148 *******************************************************************************/
mmdonatti 0:e258a1597fe1 149 void AD7172::start_single_conv()
mmdonatti 0:e258a1597fe1 150 {
mmdonatti 0:e258a1597fe1 151 *sync = 0;
lucastanio 1:6d78a35bedd0 152 wait_ms(1);
lucastanio 1:6d78a35bedd0 153 *sync = 1;
mmdonatti 0:e258a1597fe1 154 }
mmdonatti 0:e258a1597fe1 155
mmdonatti 0:e258a1597fe1 156 /******************************************************************************
lucastanio 1:6d78a35bedd0 157 * @AD7172 Read Device ID - Communication Test.
mmdonatti 0:e258a1597fe1 158 *
mmdonatti 0:e258a1597fe1 159 *******************************************************************************/
mmdonatti 0:e258a1597fe1 160 void AD7172::ReadID()
mmdonatti 0:e258a1597fe1 161 {
mmdonatti 0:e258a1597fe1 162 *cs=0;
mmdonatti 0:e258a1597fe1 163 _spi.write(0x40|AD7172_ID_REG);
mmdonatti 0:e258a1597fe1 164 id.bytes[1] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 165 id.bytes[0] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 166 *cs=1;
mmdonatti 0:e258a1597fe1 167 }
mmdonatti 0:e258a1597fe1 168
mmdonatti 0:e258a1597fe1 169 /******************************************************************************
lucastanio 1:6d78a35bedd0 170 * @AD7172 Read Device Status.
mmdonatti 0:e258a1597fe1 171 *
mmdonatti 0:e258a1597fe1 172 *******************************************************************************/
mmdonatti 0:e258a1597fe1 173 void AD7172::ReadStatus()
mmdonatti 0:e258a1597fe1 174 {
mmdonatti 0:e258a1597fe1 175 *cs=0;
mmdonatti 0:e258a1597fe1 176 _spi.write(0x40|AD7172_STATUS_REG);
mmdonatti 0:e258a1597fe1 177 status = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 178 sw_ready = (status>>7)^1;
mmdonatti 0:e258a1597fe1 179 *cs=1;
mmdonatti 0:e258a1597fe1 180 }
mmdonatti 0:e258a1597fe1 181
mmdonatti 0:e258a1597fe1 182 /***************************************************************************//**
mmdonatti 0:e258a1597fe1 183 * @brief Reads the value of the specified register.
mmdonatti 0:e258a1597fe1 184 *
mmdonatti 0:e258a1597fe1 185 * @reg - The address of the register to be read. The value will be stored
mmdonatti 0:e258a1597fe1 186 * inside the register structure that holds info about this register.
mmdonatti 0:e258a1597fe1 187 *
mmdonatti 0:e258a1597fe1 188 * @bytes - The number of bytes to be readed
mmdonatti 0:e258a1597fe1 189 *******************************************************************************/
lucastanio 1:6d78a35bedd0 190 void AD7172::ReadRegister(uint8_t reg, uint8_t bytes)
mmdonatti 0:e258a1597fe1 191 {
lucastanio 1:6d78a35bedd0 192 *cs = 0;
mmdonatti 0:e258a1597fe1 193 _spi.write(0x40|reg);
lucastanio 1:6d78a35bedd0 194 data.data = 0;
mmdonatti 0:e258a1597fe1 195 for(i=bytes-1;i>=0;i--)
mmdonatti 0:e258a1597fe1 196 data.bytes[i] = _spi.write(0x00);
lucastanio 1:6d78a35bedd0 197 *cs = 1;
mmdonatti 0:e258a1597fe1 198 }
mmdonatti 0:e258a1597fe1 199
mmdonatti 0:e258a1597fe1 200 /***************************************************************************//**
mmdonatti 0:e258a1597fe1 201 * @brief Reads the value of the specified register.
mmdonatti 0:e258a1597fe1 202 *
mmdonatti 0:e258a1597fe1 203 * @reg - The address of the register to be read. The value will be stored
mmdonatti 0:e258a1597fe1 204 * inside the register structure that holds info about this register.
mmdonatti 0:e258a1597fe1 205 *
mmdonatti 0:e258a1597fe1 206 * @bytes - The number of bytes to be stored (data saved on data variable)
mmdonatti 0:e258a1597fe1 207 *******************************************************************************/
lucastanio 1:6d78a35bedd0 208 void AD7172::WriteRegister(uint8_t reg, uint8_t bytes)
mmdonatti 0:e258a1597fe1 209 {
lucastanio 1:6d78a35bedd0 210 *cs = 0;
mmdonatti 0:e258a1597fe1 211 _spi.write(reg);
mmdonatti 0:e258a1597fe1 212 for(i=bytes-1;i>=0;i--)
mmdonatti 0:e258a1597fe1 213 _spi.write(data.bytes[i]);
lucastanio 1:6d78a35bedd0 214 *cs = 1;
mmdonatti 0:e258a1597fe1 215 }
mmdonatti 0:e258a1597fe1 216
mmdonatti 0:e258a1597fe1 217 /***************************************************************************//**
mmdonatti 0:e258a1597fe1 218 * @brief Resets the device.
mmdonatti 0:e258a1597fe1 219 *
mmdonatti 0:e258a1597fe1 220 *******************************************************************************/
mmdonatti 0:e258a1597fe1 221 void AD7172::Reset()
mmdonatti 0:e258a1597fe1 222 {
mmdonatti 0:e258a1597fe1 223 *cs=0;
mmdonatti 0:e258a1597fe1 224 for(i=0;i<=7;i++)
mmdonatti 0:e258a1597fe1 225 _spi.write(0xFF);
mmdonatti 0:e258a1597fe1 226 *cs=1;
mmdonatti 0:e258a1597fe1 227
mmdonatti 0:e258a1597fe1 228 }
mmdonatti 0:e258a1597fe1 229
mmdonatti 0:e258a1597fe1 230 /***************************************************************************//**
mmdonatti 0:e258a1597fe1 231 * @brief Waits until a new conversion result is available.
mmdonatti 0:e258a1597fe1 232 *
mmdonatti 0:e258a1597fe1 233 * @param device - The handler of the instance of the driver.
mmdonatti 0:e258a1597fe1 234 *
mmdonatti 0:e258a1597fe1 235 *******************************************************************************/
mmdonatti 0:e258a1597fe1 236 void AD7172::WaitForReady(uint32_t timeout)
mmdonatti 0:e258a1597fe1 237 {
mmdonatti 0:e258a1597fe1 238 while(sw_ready==0 && --timeout)
mmdonatti 0:e258a1597fe1 239 {
mmdonatti 0:e258a1597fe1 240 //Read the value of the Status Register, updating ready variable
mmdonatti 0:e258a1597fe1 241 ReadStatus();
mmdonatti 0:e258a1597fe1 242 }
mmdonatti 0:e258a1597fe1 243 }
mmdonatti 0:e258a1597fe1 244
lucastanio 1:6d78a35bedd0 245 /***************************************************************************//****************************************************************************
mmdonatti 0:e258a1597fe1 246 * @brief Reads the conversion result from the device using data register.
mmdonatti 0:e258a1597fe1 247 *
mmdonatti 0:e258a1597fe1 248 *******************************************************************************/
mmdonatti 0:e258a1597fe1 249 void AD7172::ReadDataRegister()
mmdonatti 0:e258a1597fe1 250 {
mmdonatti 0:e258a1597fe1 251 _spi.write(0x40|AD7172_DATA_REG);
mmdonatti 0:e258a1597fe1 252 data.data=0;
mmdonatti 0:e258a1597fe1 253 for(i=3;i>0;i--)
mmdonatti 0:e258a1597fe1 254 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 255 *sync = 1;
mmdonatti 0:e258a1597fe1 256 }
mmdonatti 0:e258a1597fe1 257
mmdonatti 0:e258a1597fe1 258 /***************************************************************************//**
mmdonatti 0:e258a1597fe1 259 * @brief Reads the conversion result from the device using data register.
mmdonatti 0:e258a1597fe1 260 *
mmdonatti 0:e258a1597fe1 261 *******************************************************************************/
mmdonatti 0:e258a1597fe1 262 void AD7172::ReadDataRegisterStatus()
mmdonatti 0:e258a1597fe1 263 {
mmdonatti 0:e258a1597fe1 264 _spi.write(0x40|AD7172_DATA_REG);
mmdonatti 0:e258a1597fe1 265 data.data=0;
mmdonatti 0:e258a1597fe1 266 for(i=3;i>0;i--)
mmdonatti 0:e258a1597fe1 267 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 268 status = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 269 channel = status&0b11;
mmdonatti 0:e258a1597fe1 270 *sync = 1;
mmdonatti 0:e258a1597fe1 271 }
mmdonatti 0:e258a1597fe1 272
lucastanio 1:6d78a35bedd0 273 /***************************************************************************//********************************************************************************
mmdonatti 0:e258a1597fe1 274 * @brief Reads the conversion result from the device for continuous read mode
mmdonatti 0:e258a1597fe1 275 *
mmdonatti 0:e258a1597fe1 276 *******************************************************************************/
mmdonatti 0:e258a1597fe1 277 void AD7172::ReadDataContinuous()
mmdonatti 0:e258a1597fe1 278 {
mmdonatti 0:e258a1597fe1 279 data.data=0;
mmdonatti 0:e258a1597fe1 280 for(i=3;i>0;i--)
mmdonatti 0:e258a1597fe1 281 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 282 }
mmdonatti 0:e258a1597fe1 283
mmdonatti 0:e258a1597fe1 284 /***************************************************************************//**
mmdonatti 0:e258a1597fe1 285 * @brief Reads the conversion result from the device for continuous read mode if DATA_STAT bit set in IFMODE register
mmdonatti 0:e258a1597fe1 286 *
mmdonatti 0:e258a1597fe1 287 *******************************************************************************/
mmdonatti 0:e258a1597fe1 288 void AD7172::ReadDataContinuousStatus()
mmdonatti 0:e258a1597fe1 289 {
mmdonatti 0:e258a1597fe1 290 data.data=0;
mmdonatti 0:e258a1597fe1 291 for(i=3;i>0;i--)
mmdonatti 0:e258a1597fe1 292 data.bytes[i] = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 293 status = _spi.write(0x00);
mmdonatti 0:e258a1597fe1 294 channel = status&0b11;
mmdonatti 0:e258a1597fe1 295 }