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
libraries/AD7791/AD7791.h@33:c3ec596a29c2, 2016-11-07 (annotated)
- Committer:
- Adrian Suciu
- Date:
- Mon Nov 07 16:27:12 2016 +0200
- Revision:
- 33:c3ec596a29c2
- Parent:
- 24:dae7123d432a
Added CN0391, CN0396 and CN0397 shields
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Adrian Suciu |
24:dae7123d432a | 1 | /** |
Adrian Suciu |
24:dae7123d432a | 2 | * @file AD7791.h |
Adrian Suciu |
24:dae7123d432a | 3 | * @brief Header file for AD7791 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/ad7791 |
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 AD7791_H |
Adrian Suciu |
24:dae7123d432a | 49 | #define AD7791_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 AD7791 driver |
Adrian Suciu |
24:dae7123d432a | 56 | */ |
Adrian Suciu |
24:dae7123d432a | 57 | |
Adrian Suciu |
24:dae7123d432a | 58 | #define AD7791_DEBUG_MODE |
Adrian Suciu |
24:dae7123d432a | 59 | |
Adrian Suciu |
24:dae7123d432a | 60 | /** |
Adrian Suciu |
24:dae7123d432a | 61 | * @brief Analog Devices AD7791 SPI 16-bit Buffered Sigma-Delta ADC |
Adrian Suciu |
24:dae7123d432a | 62 | */ |
Adrian Suciu |
24:dae7123d432a | 63 | class AD7791 |
Adrian Suciu |
24:dae7123d432a | 64 | { |
Adrian Suciu |
24:dae7123d432a | 65 | public: |
Adrian Suciu |
24:dae7123d432a | 66 | /// AD7791 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 | } AD7791Register_t; |
Adrian Suciu |
24:dae7123d432a | 74 | |
Adrian Suciu |
24:dae7123d432a | 75 | /// AD7791 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 | } AD7791Channel_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 | } AD7791Mode_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 | UB = 0x04, ///< Unipolar/Bipolar bit |
Adrian Suciu |
24:dae7123d432a | 96 | BUF = 0x02, ///< Buffered mode bit |
Adrian Suciu |
24:dae7123d432a | 97 | } ModeRegisterBits_t; |
Adrian Suciu |
24:dae7123d432a | 98 | |
Adrian Suciu |
24:dae7123d432a | 99 | typedef enum { |
Adrian Suciu |
24:dae7123d432a | 100 | CLKDIV1 = 0x40, ///< Clock divider bit 1 |
Adrian Suciu |
24:dae7123d432a | 101 | CLKDIV0 = 0x20, ///< Clock divider bit 0 |
Adrian Suciu |
24:dae7123d432a | 102 | FS2 = 0x04, ///< Update rate bit 2 |
Adrian Suciu |
24:dae7123d432a | 103 | FS1 = 0x02, ///< Update rate bit 1 |
Adrian Suciu |
24:dae7123d432a | 104 | FS0 = 0x01, ///< Update rate bit 0 |
Adrian Suciu |
24:dae7123d432a | 105 | } FilterRegisterBits_t; |
Adrian Suciu |
24:dae7123d432a | 106 | |
Adrian Suciu |
24:dae7123d432a | 107 | /* typedef enum { |
Adrian Suciu |
24:dae7123d432a | 108 | RANGE_VREF = 0, |
Adrian Suciu |
24:dae7123d432a | 109 | RANGE_VREF_DIV_2, |
Adrian Suciu |
24:dae7123d432a | 110 | RANGE_VREF_DIV_4, |
Adrian Suciu |
24:dae7123d432a | 111 | RANGE_VREF_DIV_8, |
Adrian Suciu |
24:dae7123d432a | 112 | } AnalogInputRange_t; |
Adrian Suciu |
24:dae7123d432a | 113 | */ |
Adrian Suciu |
24:dae7123d432a | 114 | /** SPI configuration & constructor */ |
Adrian Suciu |
24:dae7123d432a | 115 | AD7791( float reference_voltage, PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO = SPI_MISO, PinName SCK = SPI_SCK); |
Adrian Suciu |
24:dae7123d432a | 116 | void frequency(int hz); |
Adrian Suciu |
24:dae7123d432a | 117 | |
Adrian Suciu |
24:dae7123d432a | 118 | /** Low level SPI bus comm methods */ |
Adrian Suciu |
24:dae7123d432a | 119 | void reset(void); |
Adrian Suciu |
24:dae7123d432a | 120 | |
Adrian Suciu |
24:dae7123d432a | 121 | /** Register access methods*/ |
Adrian Suciu |
24:dae7123d432a | 122 | void set_channel(AD7791Channel_t channel); |
Adrian Suciu |
24:dae7123d432a | 123 | void set_conversion_mode(AD7791Mode_t mode); |
Adrian Suciu |
24:dae7123d432a | 124 | uint32_t read_data_reg(); |
Adrian Suciu |
24:dae7123d432a | 125 | uint8_t read_status_reg(void); |
Adrian Suciu |
24:dae7123d432a | 126 | void write_filter_reg(uint8_t regVal); |
Adrian Suciu |
24:dae7123d432a | 127 | uint8_t read_filter_reg(void); |
Adrian Suciu |
24:dae7123d432a | 128 | void write_mode_reg(uint8_t regVal); |
Adrian Suciu |
24:dae7123d432a | 129 | uint8_t read_mode_reg(void); |
Adrian Suciu |
24:dae7123d432a | 130 | // void set_range(AnalogInputRange_t range); |
Adrian Suciu |
24:dae7123d432a | 131 | // AnalogInputRange_t get_range(void); |
Adrian Suciu |
24:dae7123d432a | 132 | |
Adrian Suciu |
24:dae7123d432a | 133 | /** Reference voltage methods */ |
Adrian Suciu |
24:dae7123d432a | 134 | void set_reference_voltage(float ref); |
Adrian Suciu |
24:dae7123d432a | 135 | float get_reference_voltage(void); |
Adrian Suciu |
24:dae7123d432a | 136 | |
Adrian Suciu |
24:dae7123d432a | 137 | /** Voltage read methods */ |
Adrian Suciu |
24:dae7123d432a | 138 | float read_voltage(void); |
Adrian Suciu |
24:dae7123d432a | 139 | float data_to_voltage(uint32_t data); |
Adrian Suciu |
24:dae7123d432a | 140 | uint32_t voltage_to_data(float voltage); |
Adrian Suciu |
24:dae7123d432a | 141 | |
Adrian Suciu |
24:dae7123d432a | 142 | /** AnalogIn API */ |
Adrian Suciu |
24:dae7123d432a | 143 | float read(void); |
Adrian Suciu |
24:dae7123d432a | 144 | uint32_t read_u32(void); |
Adrian Suciu |
24:dae7123d432a | 145 | uint16_t read_u16(void); |
Adrian Suciu |
24:dae7123d432a | 146 | |
Adrian Suciu |
24:dae7123d432a | 147 | #ifdef MBED_OPERATORS |
Adrian Suciu |
24:dae7123d432a | 148 | operator float(); |
Adrian Suciu |
24:dae7123d432a | 149 | #endif |
Adrian Suciu |
24:dae7123d432a | 150 | |
Adrian Suciu |
24:dae7123d432a | 151 | private: |
Adrian Suciu |
24:dae7123d432a | 152 | DigitalIn miso; |
Adrian Suciu |
24:dae7123d432a | 153 | SPI ad7791; ///< SPI instance of the AD7791 |
Adrian Suciu |
24:dae7123d432a | 154 | DigitalOut cs; ///< DigitalOut instance for the chipselect of the AD7791 |
Adrian Suciu |
24:dae7123d432a | 155 | |
Adrian Suciu |
24:dae7123d432a | 156 | float _vref; |
Adrian Suciu |
24:dae7123d432a | 157 | // uint8_t _PGA_gain; |
Adrian Suciu |
24:dae7123d432a | 158 | bool _continous_conversion; |
Adrian Suciu |
24:dae7123d432a | 159 | AD7791Channel_t _channel; |
Adrian Suciu |
24:dae7123d432a | 160 | |
Adrian Suciu |
24:dae7123d432a | 161 | void write_reg(AD7791Register_t regAddress, uint8_t regValue); |
Adrian Suciu |
24:dae7123d432a | 162 | uint16_t write_spi(uint16_t data); |
Adrian Suciu |
24:dae7123d432a | 163 | uint16_t read_reg (AD7791Register_t regAddress); |
Adrian Suciu |
24:dae7123d432a | 164 | |
Adrian Suciu |
24:dae7123d432a | 165 | const static uint16_t _SINGLE_CONVERSION_TIMEOUT = 0xFFFF; // in 10us = 100ms |
Adrian Suciu |
24:dae7123d432a | 166 | const static uint16_t _CONTINOUS_CONVERSION_TIMEOUT = 0xFFFF; |
Adrian Suciu |
24:dae7123d432a | 167 | const static uint32_t _RESOLUTION = 0xFFFFFF; |
Adrian Suciu |
24:dae7123d432a | 168 | const static uint8_t _RESET = 0xFF; |
Adrian Suciu |
24:dae7123d432a | 169 | const static uint8_t _DUMMY_BYTE = 0xFF; |
Adrian Suciu |
24:dae7123d432a | 170 | const static uint16_t _READ_FLAG = 0x0800; |
Adrian Suciu |
24:dae7123d432a | 171 | const static uint8_t _DATA_READ = 0x38; // Read from the Data Register |
Adrian Suciu |
24:dae7123d432a | 172 | const static uint8_t _DELAY_TIMING = 0x02; |
Adrian Suciu |
24:dae7123d432a | 173 | const static uint8_t _SPI_MODE = 3; |
Adrian Suciu |
24:dae7123d432a | 174 | }; |
Adrian Suciu |
24:dae7123d432a | 175 | |
Adrian Suciu |
24:dae7123d432a | 176 | #endif |