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:
Wed May 18 16:57:57 2016 +0300
Revision:
24:dae7123d432a
Improved compatibility with Linux systems

Who changed what in which revision?

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