AD7172 library

Committer:
lucastanio
Date:
Fri Jun 19 14:04:13 2020 +0000
Revision:
7:ba79fe65707d
Parent:
6:694cb68ae500
Child:
8:8466106f2e1b
Commented code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lucastanio 7:ba79fe65707d 1 /*****************************************************************************
lucastanio 7:ba79fe65707d 2 * @file AD7172.h
lucastanio 7:ba79fe65707d 3 * @brief AD7172 header file.
lucastanio 7:ba79fe65707d 4 * @devices AD7172-2
lucastanio 7:ba79fe65707d 5 *
lucastanio 7:ba79fe65707d 6 * @author
lucastanio 7:ba79fe65707d 7 *
lucastanio 7:ba79fe65707d 8 * Mauricio Donatti - mauricio.donatti@lnls.br
lucastanio 7:ba79fe65707d 9 * Lucas Tanio - lucas.tanio@lnls.br
lucastanio 7:ba79fe65707d 10 *
lucastanio 7:ba79fe65707d 11 * LNLS - Brazilian Synchrotron Light Source
lucastanio 7:ba79fe65707d 12 * GIE - Electronics Instrumentation Group
lucastanio 7:ba79fe65707d 13 *
lucastanio 7:ba79fe65707d 14 * 2020, April
lucastanio 7:ba79fe65707d 15 *
lucastanio 7:ba79fe65707d 16 * Future Implementation:
lucastanio 7:ba79fe65707d 17 * -CRC and Checksum Support
lucastanio 7:ba79fe65707d 18 *
lucastanio 7:ba79fe65707d 19 ******************************************************************************/
mmdonatti 0:e258a1597fe1 20
lucastanio 7:ba79fe65707d 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 ******************************************************************************/
mmdonatti 0:e258a1597fe1 61
mmdonatti 0:e258a1597fe1 62 #ifndef AD7172_H
mmdonatti 0:e258a1597fe1 63 #define AD7172_H
mmdonatti 0:e258a1597fe1 64
mmdonatti 0:e258a1597fe1 65 #include "mbed.h"
mmdonatti 0:e258a1597fe1 66
mmdonatti 0:e258a1597fe1 67
mmdonatti 0:e258a1597fe1 68 #ifdef DEBUG
mmdonatti 0:e258a1597fe1 69
mmdonatti 0:e258a1597fe1 70 extern Serial pc;
lucastanio 7:ba79fe65707d 71
lucastanio 7:ba79fe65707d 72 //Config enabled DEBUG
mmdonatti 0:e258a1597fe1 73 #define AD7172_PRINTF(fmt, ...) pc.printf("AD7172: " fmt "\r\n", ##__VA_ARGS__);
mmdonatti 0:e258a1597fe1 74 #else
mmdonatti 0:e258a1597fe1 75 #define AD7172_PRINTF(fmt, ...) __NOP()
lucastanio 7:ba79fe65707d 76 #endif
mmdonatti 0:e258a1597fe1 77
lucastanio 7:ba79fe65707d 78 //Creating 32 bits variable
lucastanio 7:ba79fe65707d 79 typedef union Data32
lucastanio 7:ba79fe65707d 80 {
mmdonatti 0:e258a1597fe1 81 uint32_t data;
mmdonatti 0:e258a1597fe1 82 uint8_t bytes[4];
mmdonatti 0:e258a1597fe1 83 }data32;
mmdonatti 0:e258a1597fe1 84
lucastanio 7:ba79fe65707d 85 //Creating 16 bits variable
lucastanio 7:ba79fe65707d 86 typedef union Data16
lucastanio 7:ba79fe65707d 87 {
mmdonatti 0:e258a1597fe1 88 uint16_t data;
mmdonatti 0:e258a1597fe1 89 uint8_t bytes[2];
mmdonatti 0:e258a1597fe1 90 }data16;
mmdonatti 0:e258a1597fe1 91
lucastanio 1:6d78a35bedd0 92 // AD7172-2 Register Map
lucastanio 7:ba79fe65707d 93 #define AD7172_COMM_REG 0x00 //Comunication register address
lucastanio 7:ba79fe65707d 94 #define AD7172_STATUS_REG 0x00 //Status register address
lucastanio 7:ba79fe65707d 95 #define AD7172_ADCMODE_REG 0x01 //ADCMODE register address
lucastanio 7:ba79fe65707d 96 #define AD7172_IFMODE_REG 0x02 //IFMODE register address
lucastanio 7:ba79fe65707d 97 #define AD7172_REGCHECK_REG 0x03 //Register check address
lucastanio 7:ba79fe65707d 98 #define AD7172_DATA_REG 0x04 //Data register address
lucastanio 7:ba79fe65707d 99 #define AD7172_GPIOCON_REG 0x06 //GPIO communication register address
lucastanio 7:ba79fe65707d 100 #define AD7172_ID_REG 0x07 //ID register address
lucastanio 7:ba79fe65707d 101 #define AD7172_CHMAP0_REG 0x10 //CHx register address
mmdonatti 0:e258a1597fe1 102 #define AD7172_CHMAP1_REG 0x11
mmdonatti 0:e258a1597fe1 103 #define AD7172_CHMAP2_REG 0x12
mmdonatti 0:e258a1597fe1 104 #define AD7172_CHMAP3_REG 0x13
lucastanio 7:ba79fe65707d 105 #define AD7172_SETUPCON0_REG 0x20 //SETUPCONx register address
mmdonatti 0:e258a1597fe1 106 #define AD7172_SETUPCON1_REG 0x21
mmdonatti 0:e258a1597fe1 107 #define AD7172_SETUPCON2_REG 0x22
mmdonatti 0:e258a1597fe1 108 #define AD7172_SETUPCON3_REG 0x23
lucastanio 7:ba79fe65707d 109 #define AD7172_FILTCON0_REG 0x28 //FILTCONx register address
mmdonatti 0:e258a1597fe1 110 #define AD7172_FILTCON1_REG 0x29
mmdonatti 0:e258a1597fe1 111 #define AD7172_FILTCON2_REG 0x2A
mmdonatti 0:e258a1597fe1 112 #define AD7172_FILTCON3_REG 0x2B
lucastanio 7:ba79fe65707d 113 #define AD7172_OFFSET0_REG 0x30 //OFFSETx register address
mmdonatti 0:e258a1597fe1 114 #define AD7172_OFFSET1_REG 0x31
mmdonatti 0:e258a1597fe1 115 #define AD7172_OFFSET2_REG 0x32
mmdonatti 0:e258a1597fe1 116 #define AD7172_OFFSET3_REG 0x33
lucastanio 7:ba79fe65707d 117 #define AD7172_GAIN0_REG 0x38 //GAINx register address
mmdonatti 0:e258a1597fe1 118 #define AD7172_GAIN1_REG 0x39
mmdonatti 0:e258a1597fe1 119 #define AD7172_GAIN2_REG 0x3A
mmdonatti 0:e258a1597fe1 120 #define AD7172_GAIN3_REG 0x3B
mmdonatti 0:e258a1597fe1 121
lucastanio 1:6d78a35bedd0 122 // Communication Register bits
lucastanio 7:ba79fe65707d 123 #define AD7172_COMM_REG_RD (1 << 6) //R/W' - This bit determines if the command is a read or write operation
mmdonatti 0:e258a1597fe1 124
lucastanio 1:6d78a35bedd0 125 // Status Register bits
lucastanio 7:ba79fe65707d 126 #define AD7172_STATUS_REG_RDY (1 << 7) //RDY' - This bit goes low when the ADC has written a new result to the data register
mmdonatti 0:e258a1597fe1 127 #define AD7172_STATUS_REG_ADC_ERR (1 << 6)
mmdonatti 0:e258a1597fe1 128 #define AD7172_STATUS_REG_CRC_ERR (1 << 5)
mmdonatti 0:e258a1597fe1 129 #define AD7172_STATUS_REG_REG_ERR (1 << 4)
mmdonatti 0:e258a1597fe1 130 #define AD7172_STATUS_REG_CH(x) ((x) & 0x03)
mmdonatti 0:e258a1597fe1 131
lucastanio 1:6d78a35bedd0 132 // ADC Mode Register bits
lucastanio 7:ba79fe65707d 133 #define AD7172_ADCMODE_REG_REF_EN (1 << 15) //Enables internal reference
mmdonatti 0:e258a1597fe1 134 #define AD7172_ADCMODE_REG_HIDE_DELAY (1 << 14)
lucastanio 7:ba79fe65707d 135 #define AD7172_ADCMODE_SING_CYC (1 << 13)
lucastanio 7:ba79fe65707d 136 #define AD7172_ADCMODE_REG_DELAY(x) (((x) & 0x7) << 8)
lucastanio 7:ba79fe65707d 137 #define AD7172_ADCMODE_REG_MODE(x) (((x) & 0x7) << 4)
lucastanio 7:ba79fe65707d 138 #define AD7172_ADCMODE_REG_CLKSEL(x) (((x) & 0x3) << 2) //Clock selection
mmdonatti 0:e258a1597fe1 139
lucastanio 1:6d78a35bedd0 140 // Interface Mode Register bits
mmdonatti 0:e258a1597fe1 141 #define AD7172_IFMODE_REG_ALT_SYNC (1 << 12)
mmdonatti 0:e258a1597fe1 142 #define AD7172_IFMODE_REG_IOSTRENGTH (1 << 11)
mmdonatti 0:e258a1597fe1 143 #define AD7172_IFMODE_REG_HIDE_DELAY (1 << 10)
mmdonatti 0:e258a1597fe1 144 #define AD7172_IFMODE_REG_DOUT_RESET (1 << 8)
lucastanio 7:ba79fe65707d 145 #define AD7172_IFMODE_REG_CONT_READ (1 << 7) //Enables a continuous read of the ADC data register
lucastanio 7:ba79fe65707d 146 #define AD7172_IFMODE_REG_DATA_STAT (1 << 6) //Append the status register to data register
mmdonatti 0:e258a1597fe1 147 #define AD7172_IFMODE_REG_REG_CHECK (1 << 5)
mmdonatti 0:e258a1597fe1 148 #define AD7172_IFMODE_REG_XOR_EN (0x01 << 2)
mmdonatti 0:e258a1597fe1 149 #define AD7172_IFMODE_REG_CRC_EN (0x02 << 2)
mmdonatti 0:e258a1597fe1 150 #define AD7172_IFMODE_REG_XOR_STAT(x) (((x) & AD7172_IFMODE_REG_XOR_EN) == AD7172_IFMODE_REG_XOR_EN)
mmdonatti 0:e258a1597fe1 151 #define AD7172_IFMODE_REG_CRC_STAT(x) (((x) & AD7172_IFMODE_REG_CRC_EN) == AD7172_IFMODE_REG_CRC_EN)
mmdonatti 0:e258a1597fe1 152 #define AD7172_IFMODE_REG_DATA_WL16 (1 << 0)
mmdonatti 0:e258a1597fe1 153
lucastanio 1:6d78a35bedd0 154 // GPIO Configuration Register bits
mmdonatti 0:e258a1597fe1 155 #define AD7172_GPIOCON_REG_MUX_IO (1 << 12)
mmdonatti 0:e258a1597fe1 156 #define AD7172_GPIOCON_REG_SYNC_EN (1 << 11)
mmdonatti 0:e258a1597fe1 157 #define AD7172_GPIOCON_REG_ERR_EN(x) (((x) & 0x3) << 9)
mmdonatti 0:e258a1597fe1 158 #define AD7172_GPIOCON_REG_ERR_DAT (1 << 8)
mmdonatti 0:e258a1597fe1 159 #define AD7172_GPIOCON_REG_IP_EN1 (1 << 5)
mmdonatti 0:e258a1597fe1 160 #define AD7172_GPIOCON_REG_IP_EN0 (1 << 4)
mmdonatti 0:e258a1597fe1 161 #define AD7172_GPIOCON_REG_OP_EN1 (1 << 3)
mmdonatti 0:e258a1597fe1 162 #define AD7172_GPIOCON_REG_OP_EN0 (1 << 2)
mmdonatti 0:e258a1597fe1 163 #define AD7172_GPIOCON_REG_DATA1 (1 << 1)
mmdonatti 0:e258a1597fe1 164 #define AD7172_GPIOCON_REG_DATA0 (1 << 0)
mmdonatti 0:e258a1597fe1 165
lucastanio 1:6d78a35bedd0 166 // Channel Map Register 0-3 bits
mmdonatti 0:e258a1597fe1 167 #define AD7172_CHMAP_REG_CH_EN (1 << 15)
mmdonatti 0:e258a1597fe1 168 #define AD7172_CHMAP_REG_SETUP_SEL(x) (((x) & 0x3) << 12)
mmdonatti 0:e258a1597fe1 169 #define AD7172_CHMAP_REG_AINPOS(x) (((x) & 0x1F) << 5)
mmdonatti 0:e258a1597fe1 170 #define AD7172_CHMAP_REG_AINNEG(x) (((x) & 0x1F) << 0)
mmdonatti 0:e258a1597fe1 171
lucastanio 1:6d78a35bedd0 172 // Setup Configuration Register 0-3 bits
lucastanio 7:ba79fe65707d 173 #define AD7172_SETUP_CONF_REG_BI_UNIPOLAR (1 << 12) //This bit sets the output coding of the ADC - 0-Unipolar, 1-Bipolar
lucastanio 7:ba79fe65707d 174 #define AD7172_SETUP_CONF_REG_REFBUF_P (1 << 11)
lucastanio 7:ba79fe65707d 175 #define AD7172_SETUP_CONF_REG_REFBUF_N (1 << 10)
lucastanio 7:ba79fe65707d 176 #define AD7172_SETUP_CONF_REG_AINBUF_P (1 << 9)
lucastanio 7:ba79fe65707d 177 #define AD7172_SETUP_CONF_REG_AINBUF_N (1 << 8)
lucastanio 7:ba79fe65707d 178 #define AD7172_SETUP_CONF_REG_REF_SEL(x) (((x) & 0x3) << 4) //Reference selection
mmdonatti 0:e258a1597fe1 179
mmdonatti 0:e258a1597fe1 180 // Filter Configuration Register 0-3 bits
mmdonatti 0:e258a1597fe1 181 #define AD7172_FILT_CONF_REG_SINC3_MAP (1 << 15)
lucastanio 7:ba79fe65707d 182 #define AD7172_FILT_CONF_REG_ENHFILTEN (1 << 11)
lucastanio 7:ba79fe65707d 183 #define AD7172_FILT_CONF_REG_ENHFILT(x) (((x) & 0x7) << 8) //Postfilters for enhanced 50 Hz and 60 Hz rejection
lucastanio 7:ba79fe65707d 184 #define AD7172_FILT_CONF_REG_ORDER(x) (((x) & 0x3) << 5) //Digital filter
lucastanio 7:ba79fe65707d 185 #define AD7172_FILT_CONF_REG_ODR(x) (((x) & 0x1F) << 0) //Sample rate
mmdonatti 0:e258a1597fe1 186
lucastanio 1:6d78a35bedd0 187 // Class Declaration
mmdonatti 0:e258a1597fe1 188 class AD7172
mmdonatti 0:e258a1597fe1 189 {
mmdonatti 0:e258a1597fe1 190 public:
lucastanio 1:6d78a35bedd0 191 // Class Constructor
mmdonatti 3:ae9ae6b0b8e0 192 AD7172(SPI& p_spi,PinName slave_select,DigitalIn& p_rdy);
mmdonatti 0:e258a1597fe1 193
lucastanio 1:6d78a35bedd0 194 // Enable Device - CS goes low
mmdonatti 0:e258a1597fe1 195 void enable();
mmdonatti 0:e258a1597fe1 196
lucastanio 1:6d78a35bedd0 197 // Disable Device - CS goes high
mmdonatti 0:e258a1597fe1 198 void disable();
mmdonatti 0:e258a1597fe1 199
lucastanio 6:694cb68ae500 200 // Start Continuous Convertion Mode
mmdonatti 3:ae9ae6b0b8e0 201 void start_continuous();
mmdonatti 0:e258a1597fe1 202
lucastanio 6:694cb68ae500 203 // Stop Continuous Convertion Mode
mmdonatti 3:ae9ae6b0b8e0 204 void stop_continuous();
mmdonatti 3:ae9ae6b0b8e0 205
lucastanio 1:6d78a35bedd0 206 // Read ID function (update ID variable) - Communication Test
mmdonatti 0:e258a1597fe1 207 void ReadID();
mmdonatti 0:e258a1597fe1 208
lucastanio 1:6d78a35bedd0 209 // Read Status register and update status and ready public variables
mmdonatti 0:e258a1597fe1 210 void ReadStatus();
mmdonatti 0:e258a1597fe1 211
lucastanio 1:6d78a35bedd0 212 // Reads the value of the specified register.
mmdonatti 0:e258a1597fe1 213 void ReadRegister(uint8_t reg,uint8_t bytes);
mmdonatti 0:e258a1597fe1 214
lucastanio 1:6d78a35bedd0 215 // Writes the value of the specified register.
mmdonatti 0:e258a1597fe1 216 void WriteRegister(uint8_t reg,uint8_t bytes);
mmdonatti 0:e258a1597fe1 217
lucastanio 1:6d78a35bedd0 218 // Resets the device
mmdonatti 0:e258a1597fe1 219 void Reset();
mmdonatti 0:e258a1597fe1 220
lucastanio 1:6d78a35bedd0 221 // Waits until a new conversion result is available
mmdonatti 0:e258a1597fe1 222 void WaitForReady(uint32_t timeout);
mmdonatti 0:e258a1597fe1 223
lucastanio 1:6d78a35bedd0 224 // Reads the conversion result from the device
mmdonatti 0:e258a1597fe1 225 void ReadDataRegister();
mmdonatti 0:e258a1597fe1 226
lucastanio 1:6d78a35bedd0 227 // Reads the conversion result from the device with status reg appended
mmdonatti 0:e258a1597fe1 228 void ReadDataRegisterStatus();
mmdonatti 0:e258a1597fe1 229
lucastanio 1:6d78a35bedd0 230 // Reads data for continuous read mode
mmdonatti 0:e258a1597fe1 231 void ReadDataContinuous();
mmdonatti 0:e258a1597fe1 232
lucastanio 1:6d78a35bedd0 233 // Reads data for continuous read mode if DATA STAT bit is set
mmdonatti 0:e258a1597fe1 234 void ReadDataContinuousStatus();
lucastanio 7:ba79fe65707d 235
mmdonatti 0:e258a1597fe1 236 uint8_t status;
mmdonatti 0:e258a1597fe1 237 uint8_t sw_ready;
mmdonatti 0:e258a1597fe1 238 uint8_t bytes;
mmdonatti 0:e258a1597fe1 239 uint8_t channel;
mmdonatti 0:e258a1597fe1 240 uint8_t addr;
mmdonatti 0:e258a1597fe1 241
mmdonatti 0:e258a1597fe1 242 uint8_t res;
mmdonatti 0:e258a1597fe1 243
mmdonatti 0:e258a1597fe1 244 data32 data;
mmdonatti 0:e258a1597fe1 245 data16 id;
mmdonatti 0:e258a1597fe1 246 DigitalIn &_rdy;
mmdonatti 0:e258a1597fe1 247
mmdonatti 0:e258a1597fe1 248 protected:
mmdonatti 0:e258a1597fe1 249 SPI &_spi;
mmdonatti 0:e258a1597fe1 250 DigitalOut* cs;
mmdonatti 0:e258a1597fe1 251
mmdonatti 0:e258a1597fe1 252 private:
mmdonatti 0:e258a1597fe1 253 int i;
mmdonatti 0:e258a1597fe1 254 uint8_t aux;
mmdonatti 5:eeec01a423be 255 uint8_t continuous_on;
mmdonatti 0:e258a1597fe1 256 };
mmdonatti 0:e258a1597fe1 257
mmdonatti 0:e258a1597fe1 258 #endif //AD7172_H