Driver for the AD7790 16 bit ADC

Dependents:   ad7790-helloworld CN0357

For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all

Committer:
adisuciu
Date:
Tue May 03 12:56:17 2016 +0000
Revision:
0:c7cfa6877e1d
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adisuciu 0:c7cfa6877e1d 1 /**
adisuciu 0:c7cfa6877e1d 2 * @file AD7790.h
adisuciu 0:c7cfa6877e1d 3 * @brief Header file for AD7790 ADC
adisuciu 0:c7cfa6877e1d 4 * @author Analog Devices Inc.
adisuciu 0:c7cfa6877e1d 5 *
adisuciu 0:c7cfa6877e1d 6 * For support please go to:
adisuciu 0:c7cfa6877e1d 7 * Github: https://github.com/analogdevicesinc/mbed-adi
adisuciu 0:c7cfa6877e1d 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
adisuciu 0:c7cfa6877e1d 9 * Product: http://www.analog.com/ad7790
adisuciu 0:c7cfa6877e1d 10 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
adisuciu 0:c7cfa6877e1d 11
adisuciu 0:c7cfa6877e1d 12 ********************************************************************************
adisuciu 0:c7cfa6877e1d 13 * Copyright 2016(c) Analog Devices, Inc.
adisuciu 0:c7cfa6877e1d 14 *
adisuciu 0:c7cfa6877e1d 15 * All rights reserved.
adisuciu 0:c7cfa6877e1d 16 *
adisuciu 0:c7cfa6877e1d 17 * Redistribution and use in source and binary forms, with or without
adisuciu 0:c7cfa6877e1d 18 * modification, are permitted provided that the following conditions are met:
adisuciu 0:c7cfa6877e1d 19 * - Redistributions of source code must retain the above copyright
adisuciu 0:c7cfa6877e1d 20 * notice, this list of conditions and the following disclaimer.
adisuciu 0:c7cfa6877e1d 21 * - Redistributions in binary form must reproduce the above copyright
adisuciu 0:c7cfa6877e1d 22 * notice, this list of conditions and the following disclaimer in
adisuciu 0:c7cfa6877e1d 23 * the documentation and/or other materials provided with the
adisuciu 0:c7cfa6877e1d 24 * distribution.
adisuciu 0:c7cfa6877e1d 25 * - Neither the name of Analog Devices, Inc. nor the names of its
adisuciu 0:c7cfa6877e1d 26 * contributors may be used to endorse or promote products derived
adisuciu 0:c7cfa6877e1d 27 * from this software without specific prior written permission.
adisuciu 0:c7cfa6877e1d 28 * - The use of this software may or may not infringe the patent rights
adisuciu 0:c7cfa6877e1d 29 * of one or more patent holders. This license does not release you
adisuciu 0:c7cfa6877e1d 30 * from the requirement that you obtain separate licenses from these
adisuciu 0:c7cfa6877e1d 31 * patent holders to use this software.
adisuciu 0:c7cfa6877e1d 32 * - Use of the software either in source or binary form, must be run
adisuciu 0:c7cfa6877e1d 33 * on or directly connected to an Analog Devices Inc. component.
adisuciu 0:c7cfa6877e1d 34 *
adisuciu 0:c7cfa6877e1d 35 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
adisuciu 0:c7cfa6877e1d 36 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
adisuciu 0:c7cfa6877e1d 37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
adisuciu 0:c7cfa6877e1d 38 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
adisuciu 0:c7cfa6877e1d 39 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
adisuciu 0:c7cfa6877e1d 40 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
adisuciu 0:c7cfa6877e1d 41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
adisuciu 0:c7cfa6877e1d 42 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
adisuciu 0:c7cfa6877e1d 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
adisuciu 0:c7cfa6877e1d 44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adisuciu 0:c7cfa6877e1d 45 *
adisuciu 0:c7cfa6877e1d 46 ********************************************************************************/
adisuciu 0:c7cfa6877e1d 47
adisuciu 0:c7cfa6877e1d 48 #ifndef AD7790_H
adisuciu 0:c7cfa6877e1d 49 #define AD7790_H
adisuciu 0:c7cfa6877e1d 50
adisuciu 0:c7cfa6877e1d 51 #include "mbed.h"
adisuciu 0:c7cfa6877e1d 52
adisuciu 0:c7cfa6877e1d 53 /**
adisuciu 0:c7cfa6877e1d 54 * Comment this line if you want to turn off the debug mode.
adisuciu 0:c7cfa6877e1d 55 * The debug mode will send a message if an exception occurs within AD7790 driver
adisuciu 0:c7cfa6877e1d 56 */
adisuciu 0:c7cfa6877e1d 57
adisuciu 0:c7cfa6877e1d 58 #define AD7790_DEBUG_MODE
adisuciu 0:c7cfa6877e1d 59
adisuciu 0:c7cfa6877e1d 60 /**
adisuciu 0:c7cfa6877e1d 61 * @brief Analog Devices AD7790 SPI 16-bit Buffered Sigma-Delta ADC
adisuciu 0:c7cfa6877e1d 62 */
adisuciu 0:c7cfa6877e1d 63 class AD7790
adisuciu 0:c7cfa6877e1d 64 {
adisuciu 0:c7cfa6877e1d 65 public:
adisuciu 0:c7cfa6877e1d 66 /// AD7790 registers
adisuciu 0:c7cfa6877e1d 67 typedef enum {
adisuciu 0:c7cfa6877e1d 68 COMMUNICATION_REG = 0, ///< Communication register
adisuciu 0:c7cfa6877e1d 69 STATUS_REG = 0, ///< Status register
adisuciu 0:c7cfa6877e1d 70 MODE_REG, ///< Mode register
adisuciu 0:c7cfa6877e1d 71 FILTER_REG, ///< Filter Register
adisuciu 0:c7cfa6877e1d 72 DATA_REG ///< Data register
adisuciu 0:c7cfa6877e1d 73 } AD7790Register_t;
adisuciu 0:c7cfa6877e1d 74
adisuciu 0:c7cfa6877e1d 75 /// AD7790 channel configuration
adisuciu 0:c7cfa6877e1d 76 typedef enum {
adisuciu 0:c7cfa6877e1d 77 DIFFERENTIAL = 0, ///< AIN(+)-AIN(-)
adisuciu 0:c7cfa6877e1d 78 RESERVED, ///< reserved
adisuciu 0:c7cfa6877e1d 79 SHORT, ///< AIN(-)-AIN(-)
adisuciu 0:c7cfa6877e1d 80 VDDMONITOR ///< Monitor VDD
adisuciu 0:c7cfa6877e1d 81 } AD7790Channel_t;
adisuciu 0:c7cfa6877e1d 82
adisuciu 0:c7cfa6877e1d 83 typedef enum {
adisuciu 0:c7cfa6877e1d 84 CONTINOUS_CONVERSION_MODE = 0,
adisuciu 0:c7cfa6877e1d 85 SINGLE_CONVERSION_MODE = 0x80,
adisuciu 0:c7cfa6877e1d 86 SHUTDOWN_MODE = 0xC0
adisuciu 0:c7cfa6877e1d 87 } AD7790Mode_t;
adisuciu 0:c7cfa6877e1d 88
adisuciu 0:c7cfa6877e1d 89 typedef enum {
adisuciu 0:c7cfa6877e1d 90 MD1 = 0x80, ///< Mode Select Bit 1
adisuciu 0:c7cfa6877e1d 91 MD0 = 0x40, ///< Mode Select Bit 0
adisuciu 0:c7cfa6877e1d 92 G1 = 0x20, ///< Range bit 1
adisuciu 0:c7cfa6877e1d 93 G0 = 0x10, ///< Range bit 0
adisuciu 0:c7cfa6877e1d 94 BO = 0x08, ///< Burnout Current Enable bit
adisuciu 0:c7cfa6877e1d 95 BUF = 0x02, ///< Buffered mode bit
adisuciu 0:c7cfa6877e1d 96 } ModeRegisterBits_t;
adisuciu 0:c7cfa6877e1d 97
adisuciu 0:c7cfa6877e1d 98 typedef enum {
adisuciu 0:c7cfa6877e1d 99 CLKDIV1 = 0x40, ///< Clock divider bit 1
adisuciu 0:c7cfa6877e1d 100 CLKDIV0 = 0x20, ///< Clock divider bit 0
adisuciu 0:c7cfa6877e1d 101 FS2 = 0x04, ///< Update rate bit 2
adisuciu 0:c7cfa6877e1d 102 FS1 = 0x02, ///< Update rate bit 1
adisuciu 0:c7cfa6877e1d 103 FS0 = 0x01, ///< Update rate bit 0
adisuciu 0:c7cfa6877e1d 104 } FilterRegisterBits_t;
adisuciu 0:c7cfa6877e1d 105
adisuciu 0:c7cfa6877e1d 106 typedef enum {
adisuciu 0:c7cfa6877e1d 107 RANGE_VREF = 0,
adisuciu 0:c7cfa6877e1d 108 RANGE_VREF_DIV_2,
adisuciu 0:c7cfa6877e1d 109 RANGE_VREF_DIV_4,
adisuciu 0:c7cfa6877e1d 110 RANGE_VREF_DIV_8,
adisuciu 0:c7cfa6877e1d 111 } AnalogInputRange_t;
adisuciu 0:c7cfa6877e1d 112
adisuciu 0:c7cfa6877e1d 113 /** SPI configuration & constructor */
adisuciu 0:c7cfa6877e1d 114 AD7790( float reference_voltage, PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK);
adisuciu 0:c7cfa6877e1d 115 void frequency(int hz);
adisuciu 0:c7cfa6877e1d 116
adisuciu 0:c7cfa6877e1d 117 /** Low level SPI bus comm methods */
adisuciu 0:c7cfa6877e1d 118 void reset(void);
adisuciu 0:c7cfa6877e1d 119
adisuciu 0:c7cfa6877e1d 120 /** Register access methods*/
adisuciu 0:c7cfa6877e1d 121 void set_channel(AD7790Channel_t channel);
adisuciu 0:c7cfa6877e1d 122 void set_conversion_mode(AD7790Mode_t mode);
adisuciu 0:c7cfa6877e1d 123 uint16_t read_data_reg();
adisuciu 0:c7cfa6877e1d 124 uint8_t read_status_reg(void);
adisuciu 0:c7cfa6877e1d 125 void write_filter_reg(uint8_t regVal);
adisuciu 0:c7cfa6877e1d 126 uint8_t read_filter_reg(void);
adisuciu 0:c7cfa6877e1d 127 void write_mode_reg(uint8_t regVal);
adisuciu 0:c7cfa6877e1d 128 uint8_t read_mode_reg(void);
adisuciu 0:c7cfa6877e1d 129 void set_range(AnalogInputRange_t range);
adisuciu 0:c7cfa6877e1d 130 AnalogInputRange_t get_range(void);
adisuciu 0:c7cfa6877e1d 131
adisuciu 0:c7cfa6877e1d 132 /** Reference voltage methods */
adisuciu 0:c7cfa6877e1d 133 void set_reference_voltage(float ref);
adisuciu 0:c7cfa6877e1d 134 float get_reference_voltage(void);
adisuciu 0:c7cfa6877e1d 135
adisuciu 0:c7cfa6877e1d 136 /** Voltage read methods */
adisuciu 0:c7cfa6877e1d 137 float read_voltage(void);
adisuciu 0:c7cfa6877e1d 138 float data_to_voltage(uint16_t data);
adisuciu 0:c7cfa6877e1d 139 uint16_t voltage_to_data(float voltage);
adisuciu 0:c7cfa6877e1d 140
adisuciu 0:c7cfa6877e1d 141 /** AnalogIn API */
adisuciu 0:c7cfa6877e1d 142 float read(void);
adisuciu 0:c7cfa6877e1d 143 uint16_t read_u16(void);
adisuciu 0:c7cfa6877e1d 144
adisuciu 0:c7cfa6877e1d 145 #ifdef MBED_OPERATORS
adisuciu 0:c7cfa6877e1d 146 operator float();
adisuciu 0:c7cfa6877e1d 147 #endif
adisuciu 0:c7cfa6877e1d 148
adisuciu 0:c7cfa6877e1d 149 private:
adisuciu 0:c7cfa6877e1d 150 DigitalIn miso;///< DigitalIn must be initialized before SPI to prevent pin MUX overwrite
adisuciu 0:c7cfa6877e1d 151 SPI ad7790; ///< SPI instance of the AD7790
adisuciu 0:c7cfa6877e1d 152 DigitalOut cs; ///< DigitalOut instance for the chipselect of the AD7790
adisuciu 0:c7cfa6877e1d 153
adisuciu 0:c7cfa6877e1d 154 float _vref;
adisuciu 0:c7cfa6877e1d 155 uint8_t _PGA_gain;
adisuciu 0:c7cfa6877e1d 156 bool _continous_conversion;
adisuciu 0:c7cfa6877e1d 157 AD7790Channel_t _channel;
adisuciu 0:c7cfa6877e1d 158
adisuciu 0:c7cfa6877e1d 159 void write_reg(AD7790Register_t regAddress, uint8_t regValue);
adisuciu 0:c7cfa6877e1d 160 uint16_t write_spi(uint16_t data);
adisuciu 0:c7cfa6877e1d 161 uint16_t read_reg (AD7790Register_t regAddress);
adisuciu 0:c7cfa6877e1d 162
adisuciu 0:c7cfa6877e1d 163 const static uint16_t _SINGLE_CONVERSION_TIMEOUT = 0xFFFF; // in 10us = 100ms
adisuciu 0:c7cfa6877e1d 164 const static uint16_t _CONTINOUS_CONVERSION_TIMEOUT = 0xFFFF;
adisuciu 0:c7cfa6877e1d 165 const static uint16_t _RESOLUTION = 0xFFFF;
adisuciu 0:c7cfa6877e1d 166 const static uint8_t _RESET = 0xFF;
adisuciu 0:c7cfa6877e1d 167 const static uint8_t _DUMMY_BYTE = 0xFF;
adisuciu 0:c7cfa6877e1d 168 const static uint16_t _READ_FLAG = 0x0800;
adisuciu 0:c7cfa6877e1d 169 const static uint8_t _DATA_READ = 0x38; // Read from the Data Register
adisuciu 0:c7cfa6877e1d 170 const static uint8_t _DELAY_TIMING = 0x02;
adisuciu 0:c7cfa6877e1d 171 const static uint8_t _SPI_MODE = 3;
adisuciu 0:c7cfa6877e1d 172 };
adisuciu 0:c7cfa6877e1d 173
adisuciu 0:c7cfa6877e1d 174 #endif