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/CN0357/CN0357.cpp@11:2e67c719ce37, 2016-04-21 (annotated)
- Committer:
- Adrian Suciu
- Date:
- Thu Apr 21 18:01:41 2016 +0300
- Revision:
- 11:2e67c719ce37
Fixed filename case sensitivity issues
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Adrian Suciu |
11:2e67c719ce37 | 1 | /** |
Adrian Suciu |
11:2e67c719ce37 | 2 | * @file cn0357.cpp |
Adrian Suciu |
11:2e67c719ce37 | 3 | * @brief Source file for CN0357 |
Adrian Suciu |
11:2e67c719ce37 | 4 | * @author Analog Devices Inc. |
Adrian Suciu |
11:2e67c719ce37 | 5 | * |
Adrian Suciu |
11:2e67c719ce37 | 6 | * For support please go to: |
Adrian Suciu |
11:2e67c719ce37 | 7 | * Github: https://github.com/analogdevicesinc/mbed-adi |
Adrian Suciu |
11:2e67c719ce37 | 8 | * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers |
Adrian Suciu |
11:2e67c719ce37 | 9 | * Product: www.analog.com/EVAL-CN0357-ARDZ |
Adrian Suciu |
11:2e67c719ce37 | 10 | * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all |
Adrian Suciu |
11:2e67c719ce37 | 11 | |
Adrian Suciu |
11:2e67c719ce37 | 12 | ******************************************************************************** |
Adrian Suciu |
11:2e67c719ce37 | 13 | * Copyright 2016(c) Analog Devices, Inc. |
Adrian Suciu |
11:2e67c719ce37 | 14 | * |
Adrian Suciu |
11:2e67c719ce37 | 15 | * All rights reserved. |
Adrian Suciu |
11:2e67c719ce37 | 16 | * |
Adrian Suciu |
11:2e67c719ce37 | 17 | * Redistribution and use in source and binary forms, with or without |
Adrian Suciu |
11:2e67c719ce37 | 18 | * modification, are permitted provided that the following conditions are met: |
Adrian Suciu |
11:2e67c719ce37 | 19 | * - Redistributions of source code must retain the above copyright |
Adrian Suciu |
11:2e67c719ce37 | 20 | * notice, this list of conditions and the following disclaimer. |
Adrian Suciu |
11:2e67c719ce37 | 21 | * - Redistributions in binary form must reproduce the above copyright |
Adrian Suciu |
11:2e67c719ce37 | 22 | * notice, this list of conditions and the following disclaimer in |
Adrian Suciu |
11:2e67c719ce37 | 23 | * the documentation and/or other materials provided with the |
Adrian Suciu |
11:2e67c719ce37 | 24 | * distribution. |
Adrian Suciu |
11:2e67c719ce37 | 25 | * - Neither the name of Analog Devices, Inc. nor the names of its |
Adrian Suciu |
11:2e67c719ce37 | 26 | * contributors may be used to endorse or promote products derived |
Adrian Suciu |
11:2e67c719ce37 | 27 | * from this software without specific prior written permission. |
Adrian Suciu |
11:2e67c719ce37 | 28 | * - The use of this software may or may not infringe the patent rights |
Adrian Suciu |
11:2e67c719ce37 | 29 | * of one or more patent holders. This license does not release you |
Adrian Suciu |
11:2e67c719ce37 | 30 | * from the requirement that you obtain separate licenses from these |
Adrian Suciu |
11:2e67c719ce37 | 31 | * patent holders to use this software. |
Adrian Suciu |
11:2e67c719ce37 | 32 | * - Use of the software either in source or binary form, must be run |
Adrian Suciu |
11:2e67c719ce37 | 33 | * on or directly connected to an Analog Devices Inc. component. |
Adrian Suciu |
11:2e67c719ce37 | 34 | * |
Adrian Suciu |
11:2e67c719ce37 | 35 | * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR |
Adrian Suciu |
11:2e67c719ce37 | 36 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
Adrian Suciu |
11:2e67c719ce37 | 37 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
Adrian Suciu |
11:2e67c719ce37 | 38 | * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, |
Adrian Suciu |
11:2e67c719ce37 | 39 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
Adrian Suciu |
11:2e67c719ce37 | 40 | * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR |
Adrian Suciu |
11:2e67c719ce37 | 41 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Adrian Suciu |
11:2e67c719ce37 | 42 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Adrian Suciu |
11:2e67c719ce37 | 43 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Adrian Suciu |
11:2e67c719ce37 | 44 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Adrian Suciu |
11:2e67c719ce37 | 45 | * |
Adrian Suciu |
11:2e67c719ce37 | 46 | ********************************************************************************/ |
Adrian Suciu |
11:2e67c719ce37 | 47 | |
Adrian Suciu |
11:2e67c719ce37 | 48 | #include "mbed.h" |
Adrian Suciu |
11:2e67c719ce37 | 49 | #include "AD7790.h" |
Adrian Suciu |
11:2e67c719ce37 | 50 | #include "AD5270.h" |
Adrian Suciu |
11:2e67c719ce37 | 51 | #include "CN0357.h" |
Adrian Suciu |
11:2e67c719ce37 | 52 | |
Adrian Suciu |
11:2e67c719ce37 | 53 | /** |
Adrian Suciu |
11:2e67c719ce37 | 54 | * @brief CN0357 constructor |
Adrian Suciu |
11:2e67c719ce37 | 55 | * @param CSAD7790 - (optional)chip select of the AD7790 |
Adrian Suciu |
11:2e67c719ce37 | 56 | * @param CSAD5270 - (optional)chip select of the AD5270 |
Adrian Suciu |
11:2e67c719ce37 | 57 | * @param MOSI - (optional)pin of the SPI interface |
Adrian Suciu |
11:2e67c719ce37 | 58 | * @param MISO - (optional)pin of the SPI interface |
Adrian Suciu |
11:2e67c719ce37 | 59 | * @param SCK - (optional)pin of the SPI interface |
Adrian Suciu |
11:2e67c719ce37 | 60 | */ |
Adrian Suciu |
11:2e67c719ce37 | 61 | CN0357::CN0357(PinName CSAD7790, PinName CSAD5270, PinName MOSI, PinName MISO, |
Adrian Suciu |
11:2e67c719ce37 | 62 | PinName SCK) : |
Adrian Suciu |
11:2e67c719ce37 | 63 | _vref(1.2), _sensor_sensitivity(0), _sensor_range(0), _RDACvalue(0), |
Adrian Suciu |
11:2e67c719ce37 | 64 | ad7790(_vref, CSAD7790, MOSI, MISO, SCK), ad5270(CSAD5270, 20000.0, MOSI, MISO, SCK) |
Adrian Suciu |
11:2e67c719ce37 | 65 | |
Adrian Suciu |
11:2e67c719ce37 | 66 | { |
Adrian Suciu |
11:2e67c719ce37 | 67 | } |
Adrian Suciu |
11:2e67c719ce37 | 68 | |
Adrian Suciu |
11:2e67c719ce37 | 69 | /** |
Adrian Suciu |
11:2e67c719ce37 | 70 | * @brief initializes the AD7790 and the AD5270 |
Adrian Suciu |
11:2e67c719ce37 | 71 | * @param range - range of the sensor used (in ppm) |
Adrian Suciu |
11:2e67c719ce37 | 72 | * @param sensitivity - sensitivity of the sensor (A/ppm) |
Adrian Suciu |
11:2e67c719ce37 | 73 | * @param jp - (optional)jumper configuration of the CN0357 |
Adrian Suciu |
11:2e67c719ce37 | 74 | * @param mode_val - (optional)if jp is set to InternalADC, configures the mode register of the Internal ADC |
Adrian Suciu |
11:2e67c719ce37 | 75 | * @param filter_val - (optional)if jp is set to InternalADC, configures the filter register of the Internal ADC |
Adrian Suciu |
11:2e67c719ce37 | 76 | */ |
Adrian Suciu |
11:2e67c719ce37 | 77 | void CN0357::init(float range, float sensitivity, JumperConfig_t jp, uint8_t mode_val, uint8_t filter_val) |
Adrian Suciu |
11:2e67c719ce37 | 78 | { |
Adrian Suciu |
11:2e67c719ce37 | 79 | ad5270.frequency(500000); |
Adrian Suciu |
11:2e67c719ce37 | 80 | ad7790.frequency(500000); |
Adrian Suciu |
11:2e67c719ce37 | 81 | |
Adrian Suciu |
11:2e67c719ce37 | 82 | float resistance = set_sensor_parameters(range, sensitivity); |
Adrian Suciu |
11:2e67c719ce37 | 83 | |
Adrian Suciu |
11:2e67c719ce37 | 84 | if(jp == INTERNAL_AD7790) { |
Adrian Suciu |
11:2e67c719ce37 | 85 | _AD7790_init(mode_val, filter_val); |
Adrian Suciu |
11:2e67c719ce37 | 86 | } |
Adrian Suciu |
11:2e67c719ce37 | 87 | _rdac_init(resistance); |
Adrian Suciu |
11:2e67c719ce37 | 88 | } |
Adrian Suciu |
11:2e67c719ce37 | 89 | |
Adrian Suciu |
11:2e67c719ce37 | 90 | /** |
Adrian Suciu |
11:2e67c719ce37 | 91 | * @brief initializes the RDAC and sets SDO to HiZ |
Adrian Suciu |
11:2e67c719ce37 | 92 | * @param resistance - resistance value to initialize the RDAC |
Adrian Suciu |
11:2e67c719ce37 | 93 | * @return |
Adrian Suciu |
11:2e67c719ce37 | 94 | */ |
Adrian Suciu |
11:2e67c719ce37 | 95 | void CN0357::_rdac_init(float resistance) |
Adrian Suciu |
11:2e67c719ce37 | 96 | { |
Adrian Suciu |
11:2e67c719ce37 | 97 | /* RDAC initialization*/ |
Adrian Suciu |
11:2e67c719ce37 | 98 | /* Compute for the nearest RDAC value from given resistance and save data to the structure */ |
Adrian Suciu |
11:2e67c719ce37 | 99 | set_RDAC_value(resistance); |
Adrian Suciu |
11:2e67c719ce37 | 100 | /* Set AD5270 SDO to Hi-Z */ |
Adrian Suciu |
11:2e67c719ce37 | 101 | ad5270.set_SDO_HiZ(); |
Adrian Suciu |
11:2e67c719ce37 | 102 | } |
Adrian Suciu |
11:2e67c719ce37 | 103 | |
Adrian Suciu |
11:2e67c719ce37 | 104 | /** |
Adrian Suciu |
11:2e67c719ce37 | 105 | * @brief initializes the AD7790 |
Adrian Suciu |
11:2e67c719ce37 | 106 | * @param mode_val - configures the mode register of the Internal ADC |
Adrian Suciu |
11:2e67c719ce37 | 107 | * @param filter_val - configures the filter register of the Internal ADC |
Adrian Suciu |
11:2e67c719ce37 | 108 | */ |
Adrian Suciu |
11:2e67c719ce37 | 109 | void CN0357::_AD7790_init(uint8_t mode_val, uint8_t filter_val) |
Adrian Suciu |
11:2e67c719ce37 | 110 | { |
Adrian Suciu |
11:2e67c719ce37 | 111 | ad7790.reset(); |
Adrian Suciu |
11:2e67c719ce37 | 112 | wait_ms(50); |
Adrian Suciu |
11:2e67c719ce37 | 113 | |
Adrian Suciu |
11:2e67c719ce37 | 114 | ad7790.write_mode_reg(mode_val); |
Adrian Suciu |
11:2e67c719ce37 | 115 | wait_us(2); |
Adrian Suciu |
11:2e67c719ce37 | 116 | |
Adrian Suciu |
11:2e67c719ce37 | 117 | ad7790.write_filter_reg(filter_val); |
Adrian Suciu |
11:2e67c719ce37 | 118 | wait_ms(50); |
Adrian Suciu |
11:2e67c719ce37 | 119 | } |
Adrian Suciu |
11:2e67c719ce37 | 120 | |
Adrian Suciu |
11:2e67c719ce37 | 121 | /** |
Adrian Suciu |
11:2e67c719ce37 | 122 | * @brief reads the status register of the AD7790 |
Adrian Suciu |
11:2e67c719ce37 | 123 | * @return status register value |
Adrian Suciu |
11:2e67c719ce37 | 124 | */ |
Adrian Suciu |
11:2e67c719ce37 | 125 | uint8_t CN0357::read_adc_status(void) |
Adrian Suciu |
11:2e67c719ce37 | 126 | { |
Adrian Suciu |
11:2e67c719ce37 | 127 | return ad7790.read_status_reg(); |
Adrian Suciu |
11:2e67c719ce37 | 128 | } |
Adrian Suciu |
11:2e67c719ce37 | 129 | |
Adrian Suciu |
11:2e67c719ce37 | 130 | /** |
Adrian Suciu |
11:2e67c719ce37 | 131 | * @brief reads the ADC and computes the sensor voltage |
Adrian Suciu |
11:2e67c719ce37 | 132 | * @return sensor voltage |
Adrian Suciu |
11:2e67c719ce37 | 133 | */ |
Adrian Suciu |
11:2e67c719ce37 | 134 | float CN0357::read_sensor_voltage(void) |
Adrian Suciu |
11:2e67c719ce37 | 135 | { |
Adrian Suciu |
11:2e67c719ce37 | 136 | return ad7790.read_voltage(); |
Adrian Suciu |
11:2e67c719ce37 | 137 | } |
Adrian Suciu |
11:2e67c719ce37 | 138 | |
Adrian Suciu |
11:2e67c719ce37 | 139 | /** |
Adrian Suciu |
11:2e67c719ce37 | 140 | * @brief reads the data register of the AD7790 |
Adrian Suciu |
11:2e67c719ce37 | 141 | * @return data register value |
Adrian Suciu |
11:2e67c719ce37 | 142 | */ |
Adrian Suciu |
11:2e67c719ce37 | 143 | uint16_t CN0357::read_sensor(void) |
Adrian Suciu |
11:2e67c719ce37 | 144 | { |
Adrian Suciu |
11:2e67c719ce37 | 145 | return ad7790.read_u16(); |
Adrian Suciu |
11:2e67c719ce37 | 146 | } |
Adrian Suciu |
11:2e67c719ce37 | 147 | |
Adrian Suciu |
11:2e67c719ce37 | 148 | /** |
Adrian Suciu |
11:2e67c719ce37 | 149 | * @brief reads and computes the sensor reading in PPM |
Adrian Suciu |
11:2e67c719ce37 | 150 | * @return value of the sensor reading in PPM |
Adrian Suciu |
11:2e67c719ce37 | 151 | */ |
Adrian Suciu |
11:2e67c719ce37 | 152 | float CN0357::read_ppm() |
Adrian Suciu |
11:2e67c719ce37 | 153 | { |
Adrian Suciu |
11:2e67c719ce37 | 154 | return calc_ppm(ad7790.read_voltage()); /* Convert voltage to Gas concentration*/ |
Adrian Suciu |
11:2e67c719ce37 | 155 | } |
Adrian Suciu |
11:2e67c719ce37 | 156 | |
Adrian Suciu |
11:2e67c719ce37 | 157 | /** |
Adrian Suciu |
11:2e67c719ce37 | 158 | * @brief computes a value in PPM from a reading received as a param |
Adrian Suciu |
11:2e67c719ce37 | 159 | * @param adcVoltage - voltage to be converted to PPM |
Adrian Suciu |
11:2e67c719ce37 | 160 | * @return sensor value in PPM |
Adrian Suciu |
11:2e67c719ce37 | 161 | */ |
Adrian Suciu |
11:2e67c719ce37 | 162 | float CN0357::calc_ppm(float adcVoltage) |
Adrian Suciu |
11:2e67c719ce37 | 163 | { |
Adrian Suciu |
11:2e67c719ce37 | 164 | float fConcentration = 0; |
Adrian Suciu |
11:2e67c719ce37 | 165 | fConcentration = (fabs(adcVoltage) / _RDACvalue) / _sensor_sensitivity; |
Adrian Suciu |
11:2e67c719ce37 | 166 | return fConcentration; |
Adrian Suciu |
11:2e67c719ce37 | 167 | } |
Adrian Suciu |
11:2e67c719ce37 | 168 | |
Adrian Suciu |
11:2e67c719ce37 | 169 | /** |
Adrian Suciu |
11:2e67c719ce37 | 170 | * @brief computes voltage from a 16 bit ADC value received as a parameter |
Adrian Suciu |
11:2e67c719ce37 | 171 | * @param data - ADC value |
Adrian Suciu |
11:2e67c719ce37 | 172 | * @return sensor voltage |
Adrian Suciu |
11:2e67c719ce37 | 173 | */ |
Adrian Suciu |
11:2e67c719ce37 | 174 | float CN0357::data_to_voltage(uint16_t data) |
Adrian Suciu |
11:2e67c719ce37 | 175 | { |
Adrian Suciu |
11:2e67c719ce37 | 176 | return ad7790.data_to_voltage(data); |
Adrian Suciu |
11:2e67c719ce37 | 177 | } |
Adrian Suciu |
11:2e67c719ce37 | 178 | |
Adrian Suciu |
11:2e67c719ce37 | 179 | /** |
Adrian Suciu |
11:2e67c719ce37 | 180 | * @brief sets a new value for the RDAC |
Adrian Suciu |
11:2e67c719ce37 | 181 | * @param resistance new value for the resistance |
Adrian Suciu |
11:2e67c719ce37 | 182 | * @return none |
Adrian Suciu |
11:2e67c719ce37 | 183 | */ |
Adrian Suciu |
11:2e67c719ce37 | 184 | void CN0357::set_RDAC_value(float resistance) |
Adrian Suciu |
11:2e67c719ce37 | 185 | { |
Adrian Suciu |
11:2e67c719ce37 | 186 | _RDACvalue = ad5270.write_RDAC(resistance); |
Adrian Suciu |
11:2e67c719ce37 | 187 | } |
Adrian Suciu |
11:2e67c719ce37 | 188 | |
Adrian Suciu |
11:2e67c719ce37 | 189 | /** |
Adrian Suciu |
11:2e67c719ce37 | 190 | * @brief getter method for RDAC value |
Adrian Suciu |
11:2e67c719ce37 | 191 | * @return value of the RDAC in ohms |
Adrian Suciu |
11:2e67c719ce37 | 192 | */ |
Adrian Suciu |
11:2e67c719ce37 | 193 | float CN0357::get_RDAC_value() |
Adrian Suciu |
11:2e67c719ce37 | 194 | { |
Adrian Suciu |
11:2e67c719ce37 | 195 | return _RDACvalue; |
Adrian Suciu |
11:2e67c719ce37 | 196 | } |
Adrian Suciu |
11:2e67c719ce37 | 197 | |
Adrian Suciu |
11:2e67c719ce37 | 198 | /** |
Adrian Suciu |
11:2e67c719ce37 | 199 | * @brief set sensor range and sensitivity |
Adrian Suciu |
11:2e67c719ce37 | 200 | * sets sensor range, sensitivity |
Adrian Suciu |
11:2e67c719ce37 | 201 | * returns suggested resistance value for feedback resistor |
Adrian Suciu |
11:2e67c719ce37 | 202 | * @param range - range of the sensor used (in ppm) |
Adrian Suciu |
11:2e67c719ce37 | 203 | * @param sensitivity - sensitivity of the sensor (in A/ppm) |
Adrian Suciu |
11:2e67c719ce37 | 204 | * @return suggested resistance value for feedback resistor |
Adrian Suciu |
11:2e67c719ce37 | 205 | */ |
Adrian Suciu |
11:2e67c719ce37 | 206 | float CN0357::set_sensor_parameters(float range, float sensitivity) |
Adrian Suciu |
11:2e67c719ce37 | 207 | { |
Adrian Suciu |
11:2e67c719ce37 | 208 | _sensor_sensitivity = static_cast<float>(sensitivity); |
Adrian Suciu |
11:2e67c719ce37 | 209 | _sensor_range = range; |
Adrian Suciu |
11:2e67c719ce37 | 210 | return (_vref / (static_cast<float>(_sensor_range * _sensor_sensitivity))); |
Adrian Suciu |
11:2e67c719ce37 | 211 | } |
Adrian Suciu |
11:2e67c719ce37 | 212 | |
Adrian Suciu |
11:2e67c719ce37 | 213 | /** |
Adrian Suciu |
11:2e67c719ce37 | 214 | * @brief getter method for sensor sensitivity |
Adrian Suciu |
11:2e67c719ce37 | 215 | * @return sensor sensitivity (in A/ppm) |
Adrian Suciu |
11:2e67c719ce37 | 216 | */ |
Adrian Suciu |
11:2e67c719ce37 | 217 | float CN0357::get_sensor_sensitivity() |
Adrian Suciu |
11:2e67c719ce37 | 218 | { |
Adrian Suciu |
11:2e67c719ce37 | 219 | return _sensor_sensitivity; |
Adrian Suciu |
11:2e67c719ce37 | 220 | } |
Adrian Suciu |
11:2e67c719ce37 | 221 | |
Adrian Suciu |
11:2e67c719ce37 | 222 | /** |
Adrian Suciu |
11:2e67c719ce37 | 223 | * @brief getter method for sensor range |
Adrian Suciu |
11:2e67c719ce37 | 224 | * @return sensor range (in ppm) |
Adrian Suciu |
11:2e67c719ce37 | 225 | */ |
Adrian Suciu |
11:2e67c719ce37 | 226 | float CN0357::get_sensor_range() |
Adrian Suciu |
11:2e67c719ce37 | 227 | { |
Adrian Suciu |
11:2e67c719ce37 | 228 | return _sensor_range; |
Adrian Suciu |
11:2e67c719ce37 | 229 | } |