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