A collection of Analog Devices drivers for the mbed platform

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

Committer:
Adrian Suciu
Date:
Fri Apr 29 17:34:07 2016 +0300
Revision:
17:b8356808e8ad
Parent:
13:66c8e4ce4ff1
Fixed code style and added doxygen tags

Who changed what in which revision?

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