AD7172 library

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