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@3:1a8c14043a4e, 2016-04-01 (annotated)
- Committer:
- Suciu
- Date:
- Fri Apr 01 10:53:41 2016 +0300
- Revision:
- 3:1a8c14043a4e
- Parent:
- 1:c1f0670bb370
- Child:
- 4:5c1b28aff7e1
Added license, file headers and fixed missing whitespaces
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Suciu |
1:c1f0670bb370 | 1 | /** |
| Suciu |
3:1a8c14043a4e | 2 | * @file cn0357.cpp |
| Suciu |
3:1a8c14043a4e | 3 | * @brief Source file for CN0357 |
| 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: www.analog.com/EVAL-CN0357-ARDZ |
| Suciu |
3:1a8c14043a4e | 10 | * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all |
| Suciu |
1:c1f0670bb370 | 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 "mbed.h" |
| Suciu |
1:c1f0670bb370 | 49 | #include "AD7790.h" |
| Suciu |
1:c1f0670bb370 | 50 | #include "AD5270.h" |
| Suciu |
1:c1f0670bb370 | 51 | #include "cn0357.h" |
| Suciu |
1:c1f0670bb370 | 52 | |
| Suciu |
1:c1f0670bb370 | 53 | /** |
| Suciu |
1:c1f0670bb370 | 54 | * @brief CN0357 constructor |
| Suciu |
1:c1f0670bb370 | 55 | * @param CSAD7790 - (optional)chip select of the AD7790 |
| Suciu |
1:c1f0670bb370 | 56 | * @param CSAD5270 - (optional)chip select of the AD5270 |
| Suciu |
1:c1f0670bb370 | 57 | * @param MOSI - (optional)pin of the SPI interface |
| Suciu |
1:c1f0670bb370 | 58 | * @param MISO - (optional)pin of the SPI interface |
| Suciu |
1:c1f0670bb370 | 59 | * @param SCK - (optional)pin of the SPI interface |
| Suciu |
1:c1f0670bb370 | 60 | */ |
| Suciu |
1:c1f0670bb370 | 61 | CN0357::CN0357(PinName CSAD7790, PinName CSAD5270, PinName MOSI, PinName MISO, |
| Suciu |
1:c1f0670bb370 | 62 | PinName SCK) : |
| Suciu |
1:c1f0670bb370 | 63 | ad7790(CSAD7790, MOSI, MISO, SCK), ad5270(CSAD5270, 20000.0, MOSI, MISO, SCK) , |
| Suciu |
1:c1f0670bb370 | 64 | _sensor_sensitivity(0), _sensor_range(0), _RDACvalue(0) |
| Suciu |
1:c1f0670bb370 | 65 | { |
| Suciu |
1:c1f0670bb370 | 66 | } |
| Suciu |
1:c1f0670bb370 | 67 | |
| Suciu |
1:c1f0670bb370 | 68 | /** |
| Suciu |
1:c1f0670bb370 | 69 | * @brief initializes the AD7790 and the AD5270 |
| Suciu |
1:c1f0670bb370 | 70 | * @param range - range of the sensor used (in ppm) |
| Suciu |
1:c1f0670bb370 | 71 | * @param sensitivity - sensitivity of the sensor (A/ppm) |
| Suciu |
1:c1f0670bb370 | 72 | * @param jp - (optional)jumper configuration of the CN0357 |
| Suciu |
1:c1f0670bb370 | 73 | * @param mode_val - (optional)if jp is set to InternalADC, configures the mode register of the Internal ADC |
| Suciu |
1:c1f0670bb370 | 74 | * @param filter_val - (optional)if jp is set to InternalADC, configures the filter register of the Internal ADC |
| Suciu |
1:c1f0670bb370 | 75 | */ |
| Suciu |
3:1a8c14043a4e | 76 | void CN0357::init(float range, float sensitivity, JumperConfig_t jp, uint8_t mode_val, uint8_t filter_val) |
| Suciu |
1:c1f0670bb370 | 77 | { |
| Suciu |
1:c1f0670bb370 | 78 | ad5270.frequency(500000); |
| Suciu |
1:c1f0670bb370 | 79 | ad7790.frequency(500000); |
| Suciu |
1:c1f0670bb370 | 80 | |
| Suciu |
3:1a8c14043a4e | 81 | float resistance = set_sensor_parameters(range, sensitivity); |
| Suciu |
1:c1f0670bb370 | 82 | _rdac_init(resistance); |
| Suciu |
1:c1f0670bb370 | 83 | if(jp == INTERNAL_AD7790) { |
| Suciu |
1:c1f0670bb370 | 84 | _AD7790_init(mode_val, filter_val); |
| Suciu |
1:c1f0670bb370 | 85 | } |
| Suciu |
1:c1f0670bb370 | 86 | |
| Suciu |
1:c1f0670bb370 | 87 | } |
| Suciu |
1:c1f0670bb370 | 88 | |
| Suciu |
1:c1f0670bb370 | 89 | /** |
| Suciu |
1:c1f0670bb370 | 90 | * @brief initializes the RDAC and sets SDO to HiZ |
| Suciu |
1:c1f0670bb370 | 91 | * @param resistance - resistance value to initialize the RDAC |
| Suciu |
1:c1f0670bb370 | 92 | * @return |
| Suciu |
1:c1f0670bb370 | 93 | */ |
| Suciu |
1:c1f0670bb370 | 94 | void CN0357::_rdac_init(float resistance) |
| Suciu |
1:c1f0670bb370 | 95 | { |
| Suciu |
1:c1f0670bb370 | 96 | /* RDAC initialization*/ |
| Suciu |
1:c1f0670bb370 | 97 | /* Compute for the nearest RDAC value from given resistance and save data to the structure */ |
| Suciu |
1:c1f0670bb370 | 98 | set_RDAC_value(resistance); |
| Suciu |
1:c1f0670bb370 | 99 | /* Set AD5270 SDO to Hi-Z */ |
| Suciu |
1:c1f0670bb370 | 100 | ad5270.set_SDO_HiZ(); |
| Suciu |
1:c1f0670bb370 | 101 | } |
| Suciu |
1:c1f0670bb370 | 102 | |
| Suciu |
1:c1f0670bb370 | 103 | /** |
| Suciu |
1:c1f0670bb370 | 104 | * @brief initializes the AD7790 |
| Suciu |
1:c1f0670bb370 | 105 | * @param mode_val - configures the mode register of the Internal ADC |
| Suciu |
1:c1f0670bb370 | 106 | * @param filter_val - configures the filter register of the Internal ADC |
| Suciu |
1:c1f0670bb370 | 107 | */ |
| Suciu |
1:c1f0670bb370 | 108 | void CN0357::_AD7790_init(uint8_t mode_val, uint8_t filter_val) |
| Suciu |
1:c1f0670bb370 | 109 | { |
| Suciu |
1:c1f0670bb370 | 110 | ad7790.reset(); |
| Suciu |
1:c1f0670bb370 | 111 | // wait_ms(1000); |
| Suciu |
1:c1f0670bb370 | 112 | ad7790.write_reg(AD7790::MODE_REG, mode_val); |
| Suciu |
1:c1f0670bb370 | 113 | ad7790.write_reg(AD7790::FILTER_REG, filter_val); |
| Suciu |
1:c1f0670bb370 | 114 | |
| Suciu |
1:c1f0670bb370 | 115 | } |
| Suciu |
1:c1f0670bb370 | 116 | |
| Suciu |
1:c1f0670bb370 | 117 | /** |
| Suciu |
1:c1f0670bb370 | 118 | * @brief reads the status register of the AD7790 |
| Suciu |
1:c1f0670bb370 | 119 | * @return status register value |
| Suciu |
1:c1f0670bb370 | 120 | */ |
| Suciu |
1:c1f0670bb370 | 121 | uint8_t CN0357::read_adc_status(void) |
| Suciu |
1:c1f0670bb370 | 122 | { |
| Suciu |
1:c1f0670bb370 | 123 | return ad7790.read_reg(AD7790::STATUS_REG); |
| Suciu |
1:c1f0670bb370 | 124 | } |
| Suciu |
1:c1f0670bb370 | 125 | |
| Suciu |
1:c1f0670bb370 | 126 | /** |
| Suciu |
1:c1f0670bb370 | 127 | * @brief reads the ADC and computes the sensor voltage |
| Suciu |
1:c1f0670bb370 | 128 | * @return sensor voltage |
| Suciu |
1:c1f0670bb370 | 129 | */ |
| Suciu |
1:c1f0670bb370 | 130 | float CN0357::read_sensor_voltage(void) |
| Suciu |
1:c1f0670bb370 | 131 | { |
| Suciu |
1:c1f0670bb370 | 132 | return data_to_voltage(read_sensor()); |
| Suciu |
1:c1f0670bb370 | 133 | } |
| Suciu |
1:c1f0670bb370 | 134 | |
| Suciu |
1:c1f0670bb370 | 135 | /** |
| Suciu |
1:c1f0670bb370 | 136 | * @brief reads the data register of the AD7790 |
| Suciu |
1:c1f0670bb370 | 137 | * @return data register value |
| Suciu |
1:c1f0670bb370 | 138 | */ |
| Suciu |
1:c1f0670bb370 | 139 | uint16_t CN0357::read_sensor(void) |
| Suciu |
1:c1f0670bb370 | 140 | { |
| Suciu |
1:c1f0670bb370 | 141 | return ad7790.read_data(); |
| Suciu |
1:c1f0670bb370 | 142 | } |
| Suciu |
1:c1f0670bb370 | 143 | |
| Suciu |
1:c1f0670bb370 | 144 | /** |
| Suciu |
1:c1f0670bb370 | 145 | * @brief reads and computes the sensor reading in PPM |
| Suciu |
1:c1f0670bb370 | 146 | * @return value of the sensor reading in PPM |
| Suciu |
1:c1f0670bb370 | 147 | */ |
| Suciu |
1:c1f0670bb370 | 148 | float CN0357::read_ppm() |
| Suciu |
1:c1f0670bb370 | 149 | { |
| Suciu |
1:c1f0670bb370 | 150 | return calc_ppm(read_sensor_voltage()); /* Convert voltage to Gas concentration*/ |
| Suciu |
1:c1f0670bb370 | 151 | } |
| Suciu |
1:c1f0670bb370 | 152 | |
| Suciu |
1:c1f0670bb370 | 153 | /** |
| Suciu |
1:c1f0670bb370 | 154 | * @brief computes a value in PPM from a reading received as a param |
| Suciu |
1:c1f0670bb370 | 155 | * @param adcVoltage - voltage to be converted to PPM |
| Suciu |
1:c1f0670bb370 | 156 | * @return sensor value in PPM |
| Suciu |
1:c1f0670bb370 | 157 | */ |
| Suciu |
1:c1f0670bb370 | 158 | float CN0357::calc_ppm(float adcVoltage) |
| Suciu |
1:c1f0670bb370 | 159 | { |
| Suciu |
1:c1f0670bb370 | 160 | float fConcentration = 0; |
| Suciu |
1:c1f0670bb370 | 161 | fConcentration = (fabs(adcVoltage) / _RDACvalue) / _sensor_sensitivity; |
| Suciu |
1:c1f0670bb370 | 162 | return fConcentration; |
| Suciu |
1:c1f0670bb370 | 163 | } |
| Suciu |
1:c1f0670bb370 | 164 | |
| Suciu |
1:c1f0670bb370 | 165 | /** |
| Suciu |
1:c1f0670bb370 | 166 | * @brief computes voltage from a 16 bit ADC value received as a parameter |
| Suciu |
1:c1f0670bb370 | 167 | * @param data - ADC value |
| Suciu |
1:c1f0670bb370 | 168 | * @return sensor voltage |
| Suciu |
1:c1f0670bb370 | 169 | */ |
| Suciu |
1:c1f0670bb370 | 170 | float CN0357::data_to_voltage(uint16_t data) |
| Suciu |
1:c1f0670bb370 | 171 | { |
| Suciu |
1:c1f0670bb370 | 172 | return ((static_cast<float>(data) / pow(2, 15)) - 1) * 1.2; /* Bipolar voltage computation from ADC code */ |
| Suciu |
1:c1f0670bb370 | 173 | } |
| Suciu |
1:c1f0670bb370 | 174 | |
| Suciu |
1:c1f0670bb370 | 175 | /** |
| Suciu |
1:c1f0670bb370 | 176 | * @brief sets a new value for the RDAC |
| Suciu |
1:c1f0670bb370 | 177 | * @param resistance new value for the resistance |
| Suciu |
1:c1f0670bb370 | 178 | * @return none |
| Suciu |
1:c1f0670bb370 | 179 | */ |
| Suciu |
1:c1f0670bb370 | 180 | void CN0357::set_RDAC_value(float resistance) |
| Suciu |
1:c1f0670bb370 | 181 | { |
| Suciu |
1:c1f0670bb370 | 182 | // Compute for the RDAC code nearest to the required feedback resistance |
| Suciu |
1:c1f0670bb370 | 183 | uint16_t RDAC_val = (resistance / 20000.0) * 1024.0; |
| Suciu |
1:c1f0670bb370 | 184 | // Compute for the constants used in voltage and PPM conversion computation |
| Suciu |
1:c1f0670bb370 | 185 | _RDACvalue = ((static_cast<float> (RDAC_val) * 20000.0) / 1024.0); |
| Suciu |
1:c1f0670bb370 | 186 | ad5270.write_cmd(AD5270::WRITE_CTRL_REG, 0x02); // RDAC register write protect - allow update of wiper position through digital interface |
| Suciu |
1:c1f0670bb370 | 187 | ad5270.write_cmd(AD5270::WRITE_RDAC, RDAC_val); // write data to the RDAC register |
| Suciu |
1:c1f0670bb370 | 188 | } |
| Suciu |
1:c1f0670bb370 | 189 | |
| Suciu |
1:c1f0670bb370 | 190 | /** |
| Suciu |
1:c1f0670bb370 | 191 | * @brief getter method for RDAC value |
| Suciu |
1:c1f0670bb370 | 192 | * @return value of the RDAC in ohms |
| Suciu |
1:c1f0670bb370 | 193 | */ |
| Suciu |
1:c1f0670bb370 | 194 | float CN0357::get_RDAC_value() |
| Suciu |
1:c1f0670bb370 | 195 | { |
| Suciu |
1:c1f0670bb370 | 196 | return _RDACvalue; |
| Suciu |
1:c1f0670bb370 | 197 | } |
| Suciu |
1:c1f0670bb370 | 198 | |
| Suciu |
1:c1f0670bb370 | 199 | /** |
| Suciu |
1:c1f0670bb370 | 200 | * @brief set sensor range and sensitivity |
| Suciu |
1:c1f0670bb370 | 201 | * sets sensor range, sensitivity |
| Suciu |
1:c1f0670bb370 | 202 | * returns suggested resistance value for feedback resistor |
| Suciu |
1:c1f0670bb370 | 203 | * @param range - range of the sensor used (in ppm) |
| Suciu |
1:c1f0670bb370 | 204 | * @param sensitivity - sensitivity of the sensor (in A/ppm) |
| Suciu |
1:c1f0670bb370 | 205 | * @return suggested resistance value for feedback resistor |
| Suciu |
1:c1f0670bb370 | 206 | */ |
| Suciu |
1:c1f0670bb370 | 207 | float CN0357::set_sensor_parameters(float range, float sensitivity) |
| Suciu |
1:c1f0670bb370 | 208 | { |
| Suciu |
1:c1f0670bb370 | 209 | _sensor_sensitivity = static_cast<float>(sensitivity); |
| Suciu |
1:c1f0670bb370 | 210 | _sensor_range = range; |
| Suciu |
1:c1f0670bb370 | 211 | return (1.2 / (static_cast<float>(_sensor_range * _sensor_sensitivity))); |
| Suciu |
1:c1f0670bb370 | 212 | } |
| Suciu |
1:c1f0670bb370 | 213 | |
| Suciu |
1:c1f0670bb370 | 214 | /** |
| Suciu |
1:c1f0670bb370 | 215 | * @brief getter method for sensor sensitivity |
| Suciu |
1:c1f0670bb370 | 216 | * @return sensor sensitivity (in A/ppm) |
| Suciu |
1:c1f0670bb370 | 217 | */ |
| Suciu |
1:c1f0670bb370 | 218 | float CN0357::get_sensor_sensitivity() |
| Suciu |
1:c1f0670bb370 | 219 | { |
| Suciu |
1:c1f0670bb370 | 220 | return _sensor_sensitivity; |
| Suciu |
1:c1f0670bb370 | 221 | } |
| Suciu |
1:c1f0670bb370 | 222 | |
| Suciu |
1:c1f0670bb370 | 223 | /** |
| Suciu |
1:c1f0670bb370 | 224 | * @brief getter method for sensor range |
| Suciu |
1:c1f0670bb370 | 225 | * @return sensor range (in ppm) |
| Suciu |
1:c1f0670bb370 | 226 | */ |
| Suciu |
1:c1f0670bb370 | 227 | float CN0357::get_sensor_range() |
| Suciu |
1:c1f0670bb370 | 228 | { |
| Suciu |
1:c1f0670bb370 | 229 | return _sensor_range; |
| Suciu |
1:c1f0670bb370 | 230 | } |
CN0357 - Toxic gas measurement
CN0216 - Weight Scale