Mauricio Donatti / AD7172-2
Committer:
mmdonatti
Date:
Tue May 05 20:13:43 2020 +0000
Revision:
3:ae9ae6b0b8e0
Parent:
2:8c5d9f069f11
Child:
5:eeec01a423be
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 AD7172.h
mmdonatti 0:e258a1597fe1 3 // @brief AD7172 header file.
mmdonatti 0:e258a1597fe1 4 // @devices AD7172-2
mmdonatti 0:e258a1597fe1 5 //
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 ******************************************************************************/
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;
mmdonatti 0:e258a1597fe1 71 #define AD7172_PRINTF(fmt, ...) pc.printf("AD7172: " fmt "\r\n", ##__VA_ARGS__);
mmdonatti 0:e258a1597fe1 72
mmdonatti 0:e258a1597fe1 73 #else
mmdonatti 0:e258a1597fe1 74
mmdonatti 0:e258a1597fe1 75 #define AD7172_PRINTF(fmt, ...) __NOP()
mmdonatti 0:e258a1597fe1 76
lucastanio 1:6d78a35bedd0 77 #endif // Config enabled DEBUG
mmdonatti 0:e258a1597fe1 78
mmdonatti 0:e258a1597fe1 79 typedef union Data32 {
mmdonatti 0:e258a1597fe1 80 uint32_t data;
mmdonatti 0:e258a1597fe1 81 uint8_t bytes[4];
mmdonatti 0:e258a1597fe1 82 }data32;
mmdonatti 0:e258a1597fe1 83
mmdonatti 0:e258a1597fe1 84 typedef union Data16 {
mmdonatti 0:e258a1597fe1 85 uint16_t data;
mmdonatti 0:e258a1597fe1 86 uint8_t bytes[2];
mmdonatti 0:e258a1597fe1 87 }data16;
mmdonatti 0:e258a1597fe1 88
lucastanio 1:6d78a35bedd0 89 // AD7172-2 Register Map
mmdonatti 0:e258a1597fe1 90 #define AD7172_COMM_REG 0x00
mmdonatti 0:e258a1597fe1 91 #define AD7172_STATUS_REG 0x00
mmdonatti 0:e258a1597fe1 92 #define AD7172_ADCMODE_REG 0x01
mmdonatti 0:e258a1597fe1 93 #define AD7172_IFMODE_REG 0x02
mmdonatti 0:e258a1597fe1 94 #define AD7172_REGCHECK_REG 0x03
mmdonatti 0:e258a1597fe1 95 #define AD7172_DATA_REG 0x04
mmdonatti 0:e258a1597fe1 96 #define AD7172_GPIOCON_REG 0x06
mmdonatti 0:e258a1597fe1 97 #define AD7172_ID_REG 0x07
mmdonatti 0:e258a1597fe1 98 #define AD7172_CHMAP0_REG 0x10
mmdonatti 0:e258a1597fe1 99 #define AD7172_CHMAP1_REG 0x11
mmdonatti 0:e258a1597fe1 100 #define AD7172_CHMAP2_REG 0x12
mmdonatti 0:e258a1597fe1 101 #define AD7172_CHMAP3_REG 0x13
mmdonatti 0:e258a1597fe1 102 #define AD7172_SETUPCON0_REG 0x20
mmdonatti 0:e258a1597fe1 103 #define AD7172_SETUPCON1_REG 0x21
mmdonatti 0:e258a1597fe1 104 #define AD7172_SETUPCON2_REG 0x22
mmdonatti 0:e258a1597fe1 105 #define AD7172_SETUPCON3_REG 0x23
mmdonatti 0:e258a1597fe1 106
mmdonatti 0:e258a1597fe1 107 #define AD7172_FILTCON0_REG 0x28
mmdonatti 0:e258a1597fe1 108 #define AD7172_FILTCON1_REG 0x29
mmdonatti 0:e258a1597fe1 109 #define AD7172_FILTCON2_REG 0x2A
mmdonatti 0:e258a1597fe1 110 #define AD7172_FILTCON3_REG 0x2B
mmdonatti 0:e258a1597fe1 111
mmdonatti 0:e258a1597fe1 112 #define AD7172_OFFSET0_REG 0x30
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
mmdonatti 0:e258a1597fe1 116
mmdonatti 0:e258a1597fe1 117 #define AD7172_GAIN0_REG 0x38
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
mmdonatti 0:e258a1597fe1 123 #define AD7172_COMM_REG_RD (1 << 6)
mmdonatti 0:e258a1597fe1 124
lucastanio 1:6d78a35bedd0 125 // Status Register bits
mmdonatti 0:e258a1597fe1 126 #define AD7172_STATUS_REG_RDY (1 << 7)
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
mmdonatti 0:e258a1597fe1 133 #define AD7172_ADCMODE_REG_REF_EN (1 << 15)
mmdonatti 0:e258a1597fe1 134 #define AD7172_ADCMODE_REG_HIDE_DELAY (1 << 14)
mmdonatti 0:e258a1597fe1 135 #define AD7172_ADCMODE_SING_CYC (1 << 13)
mmdonatti 0:e258a1597fe1 136 #define AD7172_ADCMODE_REG_DELAY(x) (((x) & 0x7) << 8)
mmdonatti 0:e258a1597fe1 137 #define AD7172_ADCMODE_REG_MODE(x) (((x) & 0x7) << 4)
mmdonatti 0:e258a1597fe1 138 #define AD7172_ADCMODE_REG_CLKSEL(x) (((x) & 0x3) << 2)
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)
mmdonatti 0:e258a1597fe1 145 #define AD7172_IFMODE_REG_CONT_READ (1 << 7)
mmdonatti 0:e258a1597fe1 146 #define AD7172_IFMODE_REG_DATA_STAT (1 << 6)
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
mmdonatti 0:e258a1597fe1 173 #define AD7172_SETUP_CONF_REG_BI_UNIPOLAR (1 << 12)
mmdonatti 0:e258a1597fe1 174 #define AD7172_SETUP_CONF_REG_REFBUF_P (1 << 11)
mmdonatti 0:e258a1597fe1 175 #define AD7172_SETUP_CONF_REG_REFBUF_N (1 << 10)
mmdonatti 0:e258a1597fe1 176 #define AD7172_SETUP_CONF_REG_AINBUF_P (1 << 9)
mmdonatti 0:e258a1597fe1 177 #define AD7172_SETUP_CONF_REG_AINBUF_N (1 << 8)
mmdonatti 0:e258a1597fe1 178 #define AD7172_SETUP_CONF_REG_REF_SEL(x) (((x) & 0x3) << 4)
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)
mmdonatti 0:e258a1597fe1 182 #define AD7172_FILT_CONF_REG_ENHFILTEN (1 << 11)
mmdonatti 0:e258a1597fe1 183 #define AD7172_FILT_CONF_REG_ENHFILT(x) (((x) & 0x7) << 8)
mmdonatti 0:e258a1597fe1 184 #define AD7172_FILT_CONF_REG_ORDER(x) (((x) & 0x3) << 5)
mmdonatti 0:e258a1597fe1 185 #define AD7172_FILT_CONF_REG_ODR(x) (((x) & 0x1F) << 0)
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 1:6d78a35bedd0 200 // Configure Continuous Convertion Mode
mmdonatti 3:ae9ae6b0b8e0 201 void start_continuous();
mmdonatti 0:e258a1597fe1 202
mmdonatti 3:ae9ae6b0b8e0 203 void stop_continuous();
mmdonatti 3:ae9ae6b0b8e0 204
lucastanio 1:6d78a35bedd0 205 // Read ID function (update ID variable) - Communication Test
mmdonatti 0:e258a1597fe1 206 void ReadID();
mmdonatti 0:e258a1597fe1 207
lucastanio 1:6d78a35bedd0 208 // Read Status register and update status and ready public variables
mmdonatti 0:e258a1597fe1 209 void ReadStatus();
mmdonatti 0:e258a1597fe1 210
lucastanio 1:6d78a35bedd0 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 1:6d78a35bedd0 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 1:6d78a35bedd0 217 // Resets the device
mmdonatti 0:e258a1597fe1 218 void Reset();
mmdonatti 0:e258a1597fe1 219
lucastanio 1:6d78a35bedd0 220 // Waits until a new conversion result is available
mmdonatti 0:e258a1597fe1 221 void WaitForReady(uint32_t timeout);
mmdonatti 0:e258a1597fe1 222
lucastanio 1:6d78a35bedd0 223 // Reads the conversion result from the device
mmdonatti 0:e258a1597fe1 224 void ReadDataRegister();
mmdonatti 0:e258a1597fe1 225
lucastanio 1:6d78a35bedd0 226 // Reads the conversion result from the device with status reg appended
mmdonatti 0:e258a1597fe1 227 void ReadDataRegisterStatus();
mmdonatti 0:e258a1597fe1 228
lucastanio 1:6d78a35bedd0 229 // Reads data for continuous read mode
mmdonatti 0:e258a1597fe1 230 void ReadDataContinuous();
mmdonatti 0:e258a1597fe1 231
lucastanio 1:6d78a35bedd0 232 // Reads data for continuous read mode if DATA STAT bit is set
mmdonatti 0:e258a1597fe1 233 void ReadDataContinuousStatus();
mmdonatti 0:e258a1597fe1 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
mmdonatti 0:e258a1597fe1 243 data32 data;
mmdonatti 0:e258a1597fe1 244 data16 id;
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 0:e258a1597fe1 254 };
mmdonatti 0:e258a1597fe1 255
mmdonatti 0:e258a1597fe1 256 #endif //AD7172_H