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 16:19:00 2016 +0300
Revision:
13:66c8e4ce4ff1
Parent:
10:b210cc8d6d41
Added driver for AD7791 and example for CN0216

Who changed what in which revision?

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