A collection of Analog Devices drivers for the mbed platform

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AD7791.h Source File

AD7791.h

Go to the documentation of this file.
00001 /**
00002 *   @file     AD7791.h
00003 *   @brief    Header file for AD7791 ADC
00004 *   @author   Analog Devices Inc.
00005 *
00006 * For support please go to:
00007 * Github: https://github.com/analogdevicesinc/mbed-adi
00008 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
00009 * Product: http://www.analog.com/ad7791
00010 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
00011 
00012 ********************************************************************************
00013 * Copyright 2016(c) Analog Devices, Inc.
00014 *
00015 * All rights reserved.
00016 *
00017 * Redistribution and use in source and binary forms, with or without
00018 * modification, are permitted provided that the following conditions are met:
00019 *  - Redistributions of source code must retain the above copyright
00020 *    notice, this list of conditions and the following disclaimer.
00021 *  - Redistributions in binary form must reproduce the above copyright
00022 *    notice, this list of conditions and the following disclaimer in
00023 *    the documentation and/or other materials provided with the
00024 *    distribution.
00025 *  - Neither the name of Analog Devices, Inc. nor the names of its
00026 *    contributors may be used to endorse or promote products derived
00027 *    from this software without specific prior written permission.
00028 *  - The use of this software may or may not infringe the patent rights
00029 *    of one or more patent holders.  This license does not release you
00030 *    from the requirement that you obtain separate licenses from these
00031 *    patent holders to use this software.
00032 *  - Use of the software either in source or binary form, must be run
00033 *    on or directly connected to an Analog Devices Inc. component.
00034 *
00035 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00036 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00037 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00038 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00039 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00040 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00041 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00042 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00043 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00044 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045 *
00046 ********************************************************************************/
00047 
00048 #ifndef AD7791_H
00049 #define AD7791_H
00050 
00051 #include "mbed.h"
00052 
00053 /**
00054  * Comment this line if you want to turn off the debug mode.
00055  * The debug mode will send a message if an exception occurs within AD7791 driver
00056  */
00057 
00058 #define AD7791_DEBUG_MODE
00059 
00060 /**
00061  * @brief Analog Devices AD7791 SPI 16-bit Buffered Sigma-Delta ADC
00062  */
00063 class AD7791
00064 {
00065 public:
00066     /// AD7791 registers
00067     typedef enum {
00068         COMMUNICATION_REG = 0, ///< Communication register
00069         STATUS_REG = 0,        ///< Status register
00070         MODE_REG,              ///< Mode register
00071         FILTER_REG,            ///< Filter Register
00072         DATA_REG               ///< Data register
00073     } AD7791Register_t;
00074 
00075     /// AD7791 channel configuration
00076     typedef enum {
00077         DIFFERENTIAL = 0, ///< AIN(+)-AIN(-)
00078         RESERVED,     ///< reserved
00079         SHORT,        ///< AIN(-)-AIN(-)
00080         VDDMONITOR    ///< Monitor VDD
00081     } AD7791Channel_t;
00082 
00083     typedef enum {
00084         CONTINOUS_CONVERSION_MODE = 0,
00085         SINGLE_CONVERSION_MODE = 0x80,
00086         SHUTDOWN_MODE = 0xC0
00087     } AD7791Mode_t;
00088 
00089     typedef enum    {
00090         MD1 = 0x80, ///< Mode Select Bit 1
00091         MD0 = 0x40, ///< Mode Select Bit 0
00092 //      G1 = 0x20,  ///< Range bit 1
00093 //      G0 = 0x10,  ///< Range bit 0
00094         BO = 0x08,  ///< Burnout Current Enable bit
00095         UB = 0x04,  ///< Unipolar/Bipolar bit
00096         BUF = 0x02, ///< Buffered mode bit
00097     } ModeRegisterBits_t ;
00098 
00099     typedef enum {
00100         CLKDIV1 = 0x40, ///< Clock divider bit 1
00101         CLKDIV0 = 0x20, ///< Clock divider bit 0
00102         FS2 = 0x04, ///< Update rate bit 2
00103         FS1 = 0x02, ///< Update rate bit 1
00104         FS0 = 0x01, ///< Update rate bit 0
00105     } FilterRegisterBits_t ;
00106 
00107     /*  typedef enum    {
00108             RANGE_VREF = 0,
00109             RANGE_VREF_DIV_2,
00110             RANGE_VREF_DIV_4,
00111             RANGE_VREF_DIV_8,
00112         } AnalogInputRange_t;
00113     */
00114     /** SPI configuration & constructor */
00115     AD7791( float reference_voltage, PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK);
00116     void frequency(int hz);
00117 
00118     /** Low level SPI bus comm methods */
00119     void reset(void);
00120 
00121     /** Register access methods*/
00122     void set_channel(AD7791Channel_t channel);
00123     void set_conversion_mode(AD7791Mode_t mode);
00124     uint32_t read_data_reg();
00125     uint8_t read_status_reg(void);
00126     void write_filter_reg(uint8_t regVal);
00127     uint8_t read_filter_reg(void);
00128     void write_mode_reg(uint8_t regVal);
00129     uint8_t read_mode_reg(void);
00130 //  void  set_range(AnalogInputRange_t range);
00131 //  AnalogInputRange_t get_range(void);
00132 
00133     /** Reference voltage methods */
00134     void  set_reference_voltage(float ref);
00135     float get_reference_voltage(void);
00136 
00137     /** Voltage read methods */
00138     float read_voltage(void);
00139     float data_to_voltage(uint32_t data);
00140     uint32_t voltage_to_data(float voltage);
00141 
00142     /** AnalogIn API */
00143     float read(void);
00144     uint32_t read_u32(void);
00145     uint16_t read_u16(void);
00146 
00147 #ifdef MBED_OPERATORS
00148     operator float();
00149 #endif
00150 
00151 private:
00152     DigitalIn miso;
00153     SPI ad7791;    ///< SPI instance of the AD7791
00154     DigitalOut cs; ///< DigitalOut instance for the chipselect of the AD7791
00155 
00156     float _vref;
00157 //  uint8_t _PGA_gain;
00158     bool _continous_conversion;
00159     AD7791Channel_t _channel;
00160 
00161     void write_reg(AD7791Register_t regAddress, uint8_t regValue);
00162     uint16_t write_spi(uint16_t data);
00163     uint16_t read_reg (AD7791Register_t regAddress);
00164 
00165     const static uint16_t _SINGLE_CONVERSION_TIMEOUT = 0xFFFF; // in 10us = 100ms
00166     const static uint16_t _CONTINOUS_CONVERSION_TIMEOUT = 0xFFFF;
00167     const static uint32_t _RESOLUTION = 0xFFFFFF;
00168     const static uint8_t _RESET = 0xFF;
00169     const static uint8_t _DUMMY_BYTE = 0xFF;
00170     const static uint16_t _READ_FLAG = 0x0800;
00171     const static uint8_t _DATA_READ    = 0x38;             // Read from the Data Register
00172     const static uint8_t _DELAY_TIMING = 0x02;
00173     const static uint8_t _SPI_MODE = 3;
00174 };
00175 
00176 #endif