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.cpp@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.cpp |
Adrian Suciu |
24:dae7123d432a | 3 | * @brief Source 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 | #include <stdint.h> |
Adrian Suciu |
24:dae7123d432a | 49 | #include "mbed.h" |
Adrian Suciu |
24:dae7123d432a | 50 | #include "AD7791.h" |
Adrian Suciu |
24:dae7123d432a | 51 | |
Adrian Suciu |
24:dae7123d432a | 52 | /** |
Adrian Suciu |
24:dae7123d432a | 53 | * @brief AD7791 constructor, sets CS pin and SPI format |
Adrian Suciu |
24:dae7123d432a | 54 | * @param reference_voltage - the reference voltage to be used in computation |
Adrian Suciu |
24:dae7123d432a | 55 | * @param CS - (optional)chip select of the AD7791 |
Adrian Suciu |
24:dae7123d432a | 56 | * @param MOSI - (optional)pin of the SPI interface |
Adrian Suciu |
24:dae7123d432a | 57 | * @param MISO - (optional)pin of the SPI interface |
Adrian Suciu |
24:dae7123d432a | 58 | * @param SCK - (optional)pin of the SPI interface |
Adrian Suciu |
24:dae7123d432a | 59 | */ |
Adrian Suciu |
24:dae7123d432a | 60 | AD7791::AD7791(float reference_voltage, |
Adrian Suciu |
24:dae7123d432a | 61 | PinName CS, |
Adrian Suciu |
24:dae7123d432a | 62 | PinName MOSI, |
Adrian Suciu |
24:dae7123d432a | 63 | PinName MISO, |
Adrian Suciu |
24:dae7123d432a | 64 | PinName SCK) : |
Adrian Suciu |
24:dae7123d432a | 65 | miso(MISO), ad7791(MOSI, MISO, SCK), cs(CS), _vref(reference_voltage) |
Adrian Suciu |
24:dae7123d432a | 66 | { |
Adrian Suciu |
24:dae7123d432a | 67 | cs = true; // cs is active low |
Adrian Suciu |
24:dae7123d432a | 68 | ad7791.format(8, _SPI_MODE); |
Adrian Suciu |
24:dae7123d432a | 69 | _continous_conversion = true; |
Adrian Suciu |
24:dae7123d432a | 70 | _channel = DIFFERENTIAL; |
Adrian Suciu |
24:dae7123d432a | 71 | } |
Adrian Suciu |
24:dae7123d432a | 72 | |
Adrian Suciu |
24:dae7123d432a | 73 | /** |
Adrian Suciu |
24:dae7123d432a | 74 | * @brief Set AD7791 SPI frequency |
Adrian Suciu |
24:dae7123d432a | 75 | * @param hz - SPI bus frequency in hz |
Adrian Suciu |
24:dae7123d432a | 76 | * @return none |
Adrian Suciu |
24:dae7123d432a | 77 | */ |
Adrian Suciu |
24:dae7123d432a | 78 | void AD7791::frequency(int hz) |
Adrian Suciu |
24:dae7123d432a | 79 | { |
Adrian Suciu |
24:dae7123d432a | 80 | ad7791.frequency(hz); |
Adrian Suciu |
24:dae7123d432a | 81 | } |
Adrian Suciu |
24:dae7123d432a | 82 | |
Adrian Suciu |
24:dae7123d432a | 83 | /** |
Adrian Suciu |
24:dae7123d432a | 84 | * @brief Resets the AD7791 |
Adrian Suciu |
24:dae7123d432a | 85 | * @return none |
Adrian Suciu |
24:dae7123d432a | 86 | */ |
Adrian Suciu |
24:dae7123d432a | 87 | void AD7791::reset() |
Adrian Suciu |
24:dae7123d432a | 88 | { |
Adrian Suciu |
24:dae7123d432a | 89 | ad7791.format(8, _SPI_MODE); |
Adrian Suciu |
24:dae7123d432a | 90 | cs = false; |
Adrian Suciu |
24:dae7123d432a | 91 | wait_us(_DELAY_TIMING); |
Adrian Suciu |
24:dae7123d432a | 92 | ad7791.write(_RESET); |
Adrian Suciu |
24:dae7123d432a | 93 | ad7791.write(_RESET); |
Adrian Suciu |
24:dae7123d432a | 94 | ad7791.write(_RESET); |
Adrian Suciu |
24:dae7123d432a | 95 | ad7791.write(_RESET); |
Adrian Suciu |
24:dae7123d432a | 96 | wait_us(_DELAY_TIMING); |
Adrian Suciu |
24:dae7123d432a | 97 | cs = true; |
Adrian Suciu |
24:dae7123d432a | 98 | _continous_conversion = true; |
Adrian Suciu |
24:dae7123d432a | 99 | } |
Adrian Suciu |
24:dae7123d432a | 100 | |
Adrian Suciu |
24:dae7123d432a | 101 | /** |
Adrian Suciu |
24:dae7123d432a | 102 | * Sets the mode register. Also sets continous mode and range based on the value |
Adrian Suciu |
24:dae7123d432a | 103 | * written in reg_val |
Adrian Suciu |
24:dae7123d432a | 104 | * @param reg_val |
Adrian Suciu |
24:dae7123d432a | 105 | */ |
Adrian Suciu |
24:dae7123d432a | 106 | void AD7791::write_mode_reg(uint8_t reg_val) |
Adrian Suciu |
24:dae7123d432a | 107 | { |
Adrian Suciu |
24:dae7123d432a | 108 | write_reg(MODE_REG, reg_val); |
Adrian Suciu |
24:dae7123d432a | 109 | uint8_t continous_mode = (reg_val & 0xC0); |
Adrian Suciu |
24:dae7123d432a | 110 | if(continous_mode == 0x00) { |
Adrian Suciu |
24:dae7123d432a | 111 | _continous_conversion = true; |
Adrian Suciu |
24:dae7123d432a | 112 | } else { |
Adrian Suciu |
24:dae7123d432a | 113 | _continous_conversion = false; |
Adrian Suciu |
24:dae7123d432a | 114 | } |
Adrian Suciu |
24:dae7123d432a | 115 | /* uint8_t range = (reg_val & 0x30); |
Adrian Suciu |
24:dae7123d432a | 116 | _PGA_gain = 1 << (range >> 4);*/ |
Adrian Suciu |
24:dae7123d432a | 117 | |
Adrian Suciu |
24:dae7123d432a | 118 | } |
Adrian Suciu |
24:dae7123d432a | 119 | |
Adrian Suciu |
24:dae7123d432a | 120 | /** |
Adrian Suciu |
24:dae7123d432a | 121 | * Reads the mode register and returns its value |
Adrian Suciu |
24:dae7123d432a | 122 | * @return value of the mode register |
Adrian Suciu |
24:dae7123d432a | 123 | */ |
Adrian Suciu |
24:dae7123d432a | 124 | uint8_t AD7791::read_mode_reg() |
Adrian Suciu |
24:dae7123d432a | 125 | { |
Adrian Suciu |
24:dae7123d432a | 126 | return read_reg(MODE_REG); |
Adrian Suciu |
24:dae7123d432a | 127 | } |
Adrian Suciu |
24:dae7123d432a | 128 | |
Adrian Suciu |
24:dae7123d432a | 129 | /** |
Adrian Suciu |
24:dae7123d432a | 130 | * Writes the filter register |
Adrian Suciu |
24:dae7123d432a | 131 | * @param regValue value to be written. |
Adrian Suciu |
24:dae7123d432a | 132 | */ |
Adrian Suciu |
24:dae7123d432a | 133 | void AD7791::write_filter_reg(uint8_t reg_val) |
Adrian Suciu |
24:dae7123d432a | 134 | { |
Adrian Suciu |
24:dae7123d432a | 135 | write_reg(FILTER_REG, reg_val); |
Adrian Suciu |
24:dae7123d432a | 136 | } |
Adrian Suciu |
24:dae7123d432a | 137 | |
Adrian Suciu |
24:dae7123d432a | 138 | /** |
Adrian Suciu |
24:dae7123d432a | 139 | * Reads the filter register and returns its value |
Adrian Suciu |
24:dae7123d432a | 140 | * @return the value of the filter register |
Adrian Suciu |
24:dae7123d432a | 141 | */ |
Adrian Suciu |
24:dae7123d432a | 142 | uint8_t AD7791::read_filter_reg() |
Adrian Suciu |
24:dae7123d432a | 143 | { |
Adrian Suciu |
24:dae7123d432a | 144 | return read_reg(FILTER_REG); |
Adrian Suciu |
24:dae7123d432a | 145 | } |
Adrian Suciu |
24:dae7123d432a | 146 | |
Adrian Suciu |
24:dae7123d432a | 147 | /** |
Adrian Suciu |
24:dae7123d432a | 148 | * Reads the data register and returns its value |
Adrian Suciu |
24:dae7123d432a | 149 | * @return value of the data register |
Adrian Suciu |
24:dae7123d432a | 150 | */ |
Adrian Suciu |
24:dae7123d432a | 151 | uint32_t AD7791::read_data_reg() |
Adrian Suciu |
24:dae7123d432a | 152 | { |
Adrian Suciu |
24:dae7123d432a | 153 | uint32_t data_result; |
Adrian Suciu |
24:dae7123d432a | 154 | ad7791.format(8, _SPI_MODE); |
Adrian Suciu |
24:dae7123d432a | 155 | cs = false; |
Adrian Suciu |
24:dae7123d432a | 156 | ad7791.write(_DATA_READ | (static_cast<uint8_t>(_channel))); |
Adrian Suciu |
24:dae7123d432a | 157 | data_result = ((ad7791.write(_DUMMY_BYTE)) << 16); |
Adrian Suciu |
24:dae7123d432a | 158 | data_result |= ((ad7791.write(_DUMMY_BYTE)) << 8 ); |
Adrian Suciu |
24:dae7123d432a | 159 | data_result |= (ad7791.write(_DUMMY_BYTE)); |
Adrian Suciu |
24:dae7123d432a | 160 | cs = true; |
Adrian Suciu |
24:dae7123d432a | 161 | return data_result; |
Adrian Suciu |
24:dae7123d432a | 162 | } |
Adrian Suciu |
24:dae7123d432a | 163 | |
Adrian Suciu |
24:dae7123d432a | 164 | /** |
Adrian Suciu |
24:dae7123d432a | 165 | * Reads the status register of the ADC and returns its value |
Adrian Suciu |
24:dae7123d432a | 166 | * @return value of the status reg |
Adrian Suciu |
24:dae7123d432a | 167 | */ |
Adrian Suciu |
24:dae7123d432a | 168 | uint8_t AD7791::read_status_reg() |
Adrian Suciu |
24:dae7123d432a | 169 | { |
Adrian Suciu |
24:dae7123d432a | 170 | return read_reg(STATUS_REG); |
Adrian Suciu |
24:dae7123d432a | 171 | } |
Adrian Suciu |
24:dae7123d432a | 172 | |
Adrian Suciu |
24:dae7123d432a | 173 | |
Adrian Suciu |
24:dae7123d432a | 174 | /** |
Adrian Suciu |
24:dae7123d432a | 175 | * @brief Enables/disables continous_conversion mode |
Adrian Suciu |
24:dae7123d432a | 176 | * In Single Conversion mode, read_u16 method will read the MODE register of the ADC, |
Adrian Suciu |
24:dae7123d432a | 177 | * then write the Start single conversion bit and wait for the DOUT/RDY pin to go low, |
Adrian Suciu |
24:dae7123d432a | 178 | * When the pin is driven low, data register is read back from the ADC. |
Adrian Suciu |
24:dae7123d432a | 179 | * |
Adrian Suciu |
24:dae7123d432a | 180 | * In Continous conversion mode, read_u16 method will poll the DOUT/RDY pin, if it is low, |
Adrian Suciu |
24:dae7123d432a | 181 | * the data register is read back from the ADC. |
Adrian Suciu |
24:dae7123d432a | 182 | * |
Adrian Suciu |
24:dae7123d432a | 183 | * @param mode |
Adrian Suciu |
24:dae7123d432a | 184 | * true - continous conversion mode enabled |
Adrian Suciu |
24:dae7123d432a | 185 | * false - single conversion mode enabled |
Adrian Suciu |
24:dae7123d432a | 186 | */ |
Adrian Suciu |
24:dae7123d432a | 187 | void AD7791::set_conversion_mode(AD7791Mode_t mode) |
Adrian Suciu |
24:dae7123d432a | 188 | { |
Adrian Suciu |
24:dae7123d432a | 189 | uint8_t mode_reg_val; |
Adrian Suciu |
24:dae7123d432a | 190 | mode_reg_val = read_mode_reg() & 0x3F; |
Adrian Suciu |
24:dae7123d432a | 191 | mode_reg_val = mode_reg_val | (static_cast<uint8_t>(mode)); |
Adrian Suciu |
24:dae7123d432a | 192 | write_mode_reg(mode); |
Adrian Suciu |
24:dae7123d432a | 193 | } |
Adrian Suciu |
24:dae7123d432a | 194 | |
Adrian Suciu |
24:dae7123d432a | 195 | /** |
Adrian Suciu |
24:dae7123d432a | 196 | * - From mbed AnalogIn API - |
Adrian Suciu |
24:dae7123d432a | 197 | * @brief Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
Adrian Suciu |
24:dae7123d432a | 198 | * Depending on the conversion mode, this method will have different behavior. Conversion mode is set using |
Adrian Suciu |
24:dae7123d432a | 199 | * set_continous_conversion_mode(bool). |
Adrian Suciu |
24:dae7123d432a | 200 | * |
Adrian Suciu |
24:dae7123d432a | 201 | * In Single Conversion mode, read_u16 method will read the MODE register of the ADC, |
Adrian Suciu |
24:dae7123d432a | 202 | * then write the Start single conversion bit and wait for the DOUT/RDY pin to go low, |
Adrian Suciu |
24:dae7123d432a | 203 | * When the pin is driven low, data register is read back from the ADC. |
Adrian Suciu |
24:dae7123d432a | 204 | * |
Adrian Suciu |
24:dae7123d432a | 205 | * In Continous conversion mode, read_u16 method will poll the DOUT/RDY pin, if it is low, |
Adrian Suciu |
24:dae7123d432a | 206 | * the data register is read back from the ADC. |
Adrian Suciu |
24:dae7123d432a | 207 | * |
Adrian Suciu |
24:dae7123d432a | 208 | * @return 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value |
Adrian Suciu |
24:dae7123d432a | 209 | * returns -1 (0xFFFF) along with a debug message if conversion failed. |
Adrian Suciu |
24:dae7123d432a | 210 | */ |
Adrian Suciu |
24:dae7123d432a | 211 | uint32_t AD7791::read_u32(void) |
Adrian Suciu |
24:dae7123d432a | 212 | { |
Adrian Suciu |
24:dae7123d432a | 213 | uint32_t data_result = 0; |
Adrian Suciu |
24:dae7123d432a | 214 | ad7791.format(8, _SPI_MODE); |
Adrian Suciu |
24:dae7123d432a | 215 | cs = false; |
Adrian Suciu |
24:dae7123d432a | 216 | uint16_t timeout_cnt = 0; |
Adrian Suciu |
24:dae7123d432a | 217 | if(_continous_conversion == false) { |
Adrian Suciu |
24:dae7123d432a | 218 | |
Adrian Suciu |
24:dae7123d432a | 219 | uint8_t mode_reg = read_mode_reg(); |
Adrian Suciu |
24:dae7123d432a | 220 | wait_us(_DELAY_TIMING); |
Adrian Suciu |
24:dae7123d432a | 221 | |
Adrian Suciu |
24:dae7123d432a | 222 | cs = false; |
Adrian Suciu |
24:dae7123d432a | 223 | mode_reg = (mode_reg & 0x3F) | MD1; // mask single conversion bits |
Adrian Suciu |
24:dae7123d432a | 224 | ad7791.write((MODE_REG << 4) | (static_cast<uint8_t>(_channel))); // start single conversion |
Adrian Suciu |
24:dae7123d432a | 225 | ad7791.write(mode_reg); |
Adrian Suciu |
24:dae7123d432a | 226 | timeout_cnt = _SINGLE_CONVERSION_TIMEOUT; // starts timeout |
Adrian Suciu |
24:dae7123d432a | 227 | } else { |
Adrian Suciu |
24:dae7123d432a | 228 | timeout_cnt = _CONTINOUS_CONVERSION_TIMEOUT; // starts timeout |
Adrian Suciu |
24:dae7123d432a | 229 | } |
Adrian Suciu |
24:dae7123d432a | 230 | wait_us(1); |
Adrian Suciu |
24:dae7123d432a | 231 | |
Adrian Suciu |
24:dae7123d432a | 232 | while(miso) { // wait for the MISO pin to go low. |
Adrian Suciu |
24:dae7123d432a | 233 | if(timeout_cnt) { |
Adrian Suciu |
24:dae7123d432a | 234 | timeout_cnt--; |
Adrian Suciu |
24:dae7123d432a | 235 | } else { |
Adrian Suciu |
24:dae7123d432a | 236 | cs = true; |
Adrian Suciu |
24:dae7123d432a | 237 | #ifdef AD7791_DEBUG_MODE |
Adrian Suciu |
24:dae7123d432a | 238 | printf("timeout occurred reading the AD7791. "); // error, MISO line didn't toggle |
Adrian Suciu |
24:dae7123d432a | 239 | #endif |
Adrian Suciu |
24:dae7123d432a | 240 | return -1; // ERROR |
Adrian Suciu |
24:dae7123d432a | 241 | } |
Adrian Suciu |
24:dae7123d432a | 242 | wait_us(10); |
Adrian Suciu |
24:dae7123d432a | 243 | } |
Adrian Suciu |
24:dae7123d432a | 244 | |
Adrian Suciu |
24:dae7123d432a | 245 | ad7791.write(_DATA_READ | (static_cast<uint8_t>(_channel))); |
Adrian Suciu |
24:dae7123d432a | 246 | data_result = ((ad7791.write(_DUMMY_BYTE)) << 16); |
Adrian Suciu |
24:dae7123d432a | 247 | data_result |= ((ad7791.write(_DUMMY_BYTE)) << 8 ); |
Adrian Suciu |
24:dae7123d432a | 248 | data_result |= (ad7791.write(_DUMMY_BYTE)); |
Adrian Suciu |
24:dae7123d432a | 249 | cs = true; |
Adrian Suciu |
24:dae7123d432a | 250 | return data_result; |
Adrian Suciu |
24:dae7123d432a | 251 | } |
Adrian Suciu |
24:dae7123d432a | 252 | |
Adrian Suciu |
24:dae7123d432a | 253 | uint16_t AD7791::read_u16(void) |
Adrian Suciu |
24:dae7123d432a | 254 | { |
Adrian Suciu |
24:dae7123d432a | 255 | uint32_t data = read_u32(); |
Adrian Suciu |
24:dae7123d432a | 256 | return static_cast<uint16_t>((data & 0xffff00) >> 8); |
Adrian Suciu |
24:dae7123d432a | 257 | } |
Adrian Suciu |
24:dae7123d432a | 258 | |
Adrian Suciu |
24:dae7123d432a | 259 | /** |
Adrian Suciu |
24:dae7123d432a | 260 | * @brief Reads a register of the AD7791 |
Adrian Suciu |
24:dae7123d432a | 261 | * @param address - address of the register |
Adrian Suciu |
24:dae7123d432a | 262 | * @return value of the register |
Adrian Suciu |
24:dae7123d432a | 263 | */ |
Adrian Suciu |
24:dae7123d432a | 264 | uint16_t AD7791::read_reg(AD7791Register_t address) |
Adrian Suciu |
24:dae7123d432a | 265 | { |
Adrian Suciu |
24:dae7123d432a | 266 | uint16_t data = address << 12; |
Adrian Suciu |
24:dae7123d432a | 267 | data |= _DUMMY_BYTE; |
Adrian Suciu |
24:dae7123d432a | 268 | data |= _READ_FLAG; |
Adrian Suciu |
24:dae7123d432a | 269 | data |= (static_cast<uint8_t>(_channel) << 8); |
Adrian Suciu |
24:dae7123d432a | 270 | return write_spi(data); |
Adrian Suciu |
24:dae7123d432a | 271 | } |
Adrian Suciu |
24:dae7123d432a | 272 | |
Adrian Suciu |
24:dae7123d432a | 273 | /** |
Adrian Suciu |
24:dae7123d432a | 274 | * @brief Writes a register of the AD7791 |
Adrian Suciu |
24:dae7123d432a | 275 | * @param address - address of the register |
Adrian Suciu |
24:dae7123d432a | 276 | * @param reg_val - value to be written |
Adrian Suciu |
24:dae7123d432a | 277 | * @return none |
Adrian Suciu |
24:dae7123d432a | 278 | * |
Adrian Suciu |
24:dae7123d432a | 279 | */ |
Adrian Suciu |
24:dae7123d432a | 280 | void AD7791::write_reg(AD7791Register_t address, uint8_t reg_val) |
Adrian Suciu |
24:dae7123d432a | 281 | { |
Adrian Suciu |
24:dae7123d432a | 282 | uint16_t spi_data = address << 12; |
Adrian Suciu |
24:dae7123d432a | 283 | spi_data |= reg_val; |
Adrian Suciu |
24:dae7123d432a | 284 | spi_data |= (static_cast<uint8_t>(_channel) << 8); |
Adrian Suciu |
24:dae7123d432a | 285 | write_spi(spi_data); |
Adrian Suciu |
24:dae7123d432a | 286 | } |
Adrian Suciu |
24:dae7123d432a | 287 | |
Adrian Suciu |
24:dae7123d432a | 288 | /** |
Adrian Suciu |
24:dae7123d432a | 289 | * @brief Writes 16bit data to the AD7791 SPI interface |
Adrian Suciu |
24:dae7123d432a | 290 | * @param reg_val to be written |
Adrian Suciu |
24:dae7123d432a | 291 | * @return data returned by the AD7791 |
Adrian Suciu |
24:dae7123d432a | 292 | */ |
Adrian Suciu |
24:dae7123d432a | 293 | uint16_t AD7791::write_spi(uint16_t reg_val) |
Adrian Suciu |
24:dae7123d432a | 294 | { |
Adrian Suciu |
24:dae7123d432a | 295 | uint16_t data_result; |
Adrian Suciu |
24:dae7123d432a | 296 | uint8_t upper_byte = (reg_val >> 8) & 0xFF; |
Adrian Suciu |
24:dae7123d432a | 297 | uint8_t lower_byte = reg_val & 0xFF; |
Adrian Suciu |
24:dae7123d432a | 298 | ad7791.format(8, _SPI_MODE); |
Adrian Suciu |
24:dae7123d432a | 299 | cs = false; |
Adrian Suciu |
24:dae7123d432a | 300 | data_result = (ad7791.write(upper_byte) << 8); |
Adrian Suciu |
24:dae7123d432a | 301 | data_result |= ad7791.write(lower_byte); |
Adrian Suciu |
24:dae7123d432a | 302 | cs = true; |
Adrian Suciu |
24:dae7123d432a | 303 | return data_result; |
Adrian Suciu |
24:dae7123d432a | 304 | } |
Adrian Suciu |
24:dae7123d432a | 305 | |
Adrian Suciu |
24:dae7123d432a | 306 | /** |
Adrian Suciu |
24:dae7123d432a | 307 | * Sets the reference voltage of the AD7790 |
Adrian Suciu |
24:dae7123d432a | 308 | * @param ref reference voltage to be set |
Adrian Suciu |
24:dae7123d432a | 309 | */ |
Adrian Suciu |
24:dae7123d432a | 310 | void AD7791::set_reference_voltage(float ref) |
Adrian Suciu |
24:dae7123d432a | 311 | { |
Adrian Suciu |
24:dae7123d432a | 312 | _vref = ref; |
Adrian Suciu |
24:dae7123d432a | 313 | } |
Adrian Suciu |
24:dae7123d432a | 314 | |
Adrian Suciu |
24:dae7123d432a | 315 | /** |
Adrian Suciu |
24:dae7123d432a | 316 | * Gets the reference voltage of the AD7790 |
Adrian Suciu |
24:dae7123d432a | 317 | * @return reference voltage |
Adrian Suciu |
24:dae7123d432a | 318 | */ |
Adrian Suciu |
24:dae7123d432a | 319 | float AD7791::get_reference_voltage(void) |
Adrian Suciu |
24:dae7123d432a | 320 | { |
Adrian Suciu |
24:dae7123d432a | 321 | return _vref; |
Adrian Suciu |
24:dae7123d432a | 322 | } |
Adrian Suciu |
24:dae7123d432a | 323 | |
Adrian Suciu |
24:dae7123d432a | 324 | /** |
Adrian Suciu |
24:dae7123d432a | 325 | * Reads the data register of the ADC and converts the result to volts |
Adrian Suciu |
24:dae7123d432a | 326 | * Gain needs to be correctly set using set_gain in order to get accurate results |
Adrian Suciu |
24:dae7123d432a | 327 | * @return voltage of the ADC input |
Adrian Suciu |
24:dae7123d432a | 328 | */ |
Adrian Suciu |
24:dae7123d432a | 329 | float AD7791::read_voltage(void) |
Adrian Suciu |
24:dae7123d432a | 330 | { |
Adrian Suciu |
24:dae7123d432a | 331 | return data_to_voltage(read_u32()); |
Adrian Suciu |
24:dae7123d432a | 332 | } |
Adrian Suciu |
24:dae7123d432a | 333 | |
Adrian Suciu |
24:dae7123d432a | 334 | /** |
Adrian Suciu |
24:dae7123d432a | 335 | * Converts an uint16_t to voltage. |
Adrian Suciu |
24:dae7123d432a | 336 | * Gain needs to be correctly set using set_gain in order to get accurate results |
Adrian Suciu |
24:dae7123d432a | 337 | * @param data in uint16_t format |
Adrian Suciu |
24:dae7123d432a | 338 | * @return float value of voltage (in V) |
Adrian Suciu |
24:dae7123d432a | 339 | */ |
Adrian Suciu |
24:dae7123d432a | 340 | float AD7791::data_to_voltage(uint32_t data) |
Adrian Suciu |
24:dae7123d432a | 341 | { |
Adrian Suciu |
24:dae7123d432a | 342 | return ((data / static_cast<float>(_RESOLUTION / 2)) - 1) * (_vref ); |
Adrian Suciu |
24:dae7123d432a | 343 | } |
Adrian Suciu |
24:dae7123d432a | 344 | |
Adrian Suciu |
24:dae7123d432a | 345 | /** |
Adrian Suciu |
24:dae7123d432a | 346 | * Converts voltage to an uint16_t. |
Adrian Suciu |
24:dae7123d432a | 347 | * Gain needs to be correctly set using set_gain in order to get accurate results |
Adrian Suciu |
24:dae7123d432a | 348 | * @param voltage to be converted |
Adrian Suciu |
24:dae7123d432a | 349 | * @return data in uint16_t format |
Adrian Suciu |
24:dae7123d432a | 350 | */ |
Adrian Suciu |
24:dae7123d432a | 351 | uint32_t AD7791::voltage_to_data(float voltage) |
Adrian Suciu |
24:dae7123d432a | 352 | { |
Adrian Suciu |
24:dae7123d432a | 353 | return (((voltage / _vref) + 1) * static_cast<float>(_RESOLUTION / 2)); |
Adrian Suciu |
24:dae7123d432a | 354 | } |
Adrian Suciu |
24:dae7123d432a | 355 | |
Adrian Suciu |
24:dae7123d432a | 356 | /** |
Adrian Suciu |
24:dae7123d432a | 357 | * Sets the conversion channel. |
Adrian Suciu |
24:dae7123d432a | 358 | * @param channel |
Adrian Suciu |
24:dae7123d432a | 359 | */ |
Adrian Suciu |
24:dae7123d432a | 360 | void AD7791::set_channel(AD7791Channel_t channel) |
Adrian Suciu |
24:dae7123d432a | 361 | { |
Adrian Suciu |
24:dae7123d432a | 362 | _channel = channel; |
Adrian Suciu |
24:dae7123d432a | 363 | } |
Adrian Suciu |
24:dae7123d432a | 364 | |
Adrian Suciu |
24:dae7123d432a | 365 | /** |
Adrian Suciu |
24:dae7123d432a | 366 | * - From mbed AnalogIn API - |
Adrian Suciu |
24:dae7123d432a | 367 | * Read the input voltage, represented as a float in the range [0.0, 1.0] - uses the read_u16 method |
Adrian Suciu |
24:dae7123d432a | 368 | * @returns A floating-point value representing the current input voltage, measured as a percentage |
Adrian Suciu |
24:dae7123d432a | 369 | * returns 1.0 along with a debug message if the conversion failed |
Adrian Suciu |
24:dae7123d432a | 370 | */ |
Adrian Suciu |
24:dae7123d432a | 371 | float AD7791::read(void) |
Adrian Suciu |
24:dae7123d432a | 372 | { |
Adrian Suciu |
24:dae7123d432a | 373 | float percent; |
Adrian Suciu |
24:dae7123d432a | 374 | uint32_t data; |
Adrian Suciu |
24:dae7123d432a | 375 | data = read_u32(); |
Adrian Suciu |
24:dae7123d432a | 376 | percent = (data / static_cast<float>(_RESOLUTION) ); // translate bipolar conversion to [0.0, 1.0] domain |
Adrian Suciu |
24:dae7123d432a | 377 | return percent; |
Adrian Suciu |
24:dae7123d432a | 378 | } |
Adrian Suciu |
24:dae7123d432a | 379 | |
Adrian Suciu |
24:dae7123d432a | 380 | #ifdef MBED_OPERATORS |
Adrian Suciu |
24:dae7123d432a | 381 | |
Adrian Suciu |
24:dae7123d432a | 382 | /** |
Adrian Suciu |
24:dae7123d432a | 383 | * - From mbed AnalogIn API - |
Adrian Suciu |
24:dae7123d432a | 384 | * An operator shorthand for read() |
Adrian Suciu |
24:dae7123d432a | 385 | * The float() operator can be used as a shorthand for read() to simplify common code sequences |
Adrian Suciu |
24:dae7123d432a | 386 | */ |
Adrian Suciu |
24:dae7123d432a | 387 | AD7791::operator float() |
Adrian Suciu |
24:dae7123d432a | 388 | { |
Adrian Suciu |
24:dae7123d432a | 389 | return read(); |
Adrian Suciu |
24:dae7123d432a | 390 | } |
Adrian Suciu |
24:dae7123d432a | 391 | |
Adrian Suciu |
24:dae7123d432a | 392 | #endif |
Adrian Suciu |
24:dae7123d432a | 393 | |
Adrian Suciu |
24:dae7123d432a | 394 | |
Adrian Suciu |
24:dae7123d432a | 395 |