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

Committer:
Adrian Suciu
Date:
Tue Apr 19 17:55:14 2016 +0300
Revision:
4:5c1b28aff7e1
Parent:
3:1a8c14043a4e
Driver and application redesign

Who changed what in which revision?

UserRevisionLine numberNew 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) :
Adrian Suciu 4:5c1b28aff7e1 63 _vref(1.2), _sensor_sensitivity(0), _sensor_range(0), _RDACvalue(0),
Adrian Suciu 4:5c1b28aff7e1 64 ad7790(_vref, CSAD7790, MOSI, MISO, SCK), ad5270(CSAD5270, 20000.0, MOSI, MISO, SCK)
Adrian Suciu 4:5c1b28aff7e1 65
Suciu 1:c1f0670bb370 66 {
Suciu 1:c1f0670bb370 67 }
Suciu 1:c1f0670bb370 68
Suciu 1:c1f0670bb370 69 /**
Suciu 1:c1f0670bb370 70 * @brief initializes the AD7790 and the AD5270
Suciu 1:c1f0670bb370 71 * @param range - range of the sensor used (in ppm)
Suciu 1:c1f0670bb370 72 * @param sensitivity - sensitivity of the sensor (A/ppm)
Suciu 1:c1f0670bb370 73 * @param jp - (optional)jumper configuration of the CN0357
Suciu 1:c1f0670bb370 74 * @param mode_val - (optional)if jp is set to InternalADC, configures the mode register of the Internal ADC
Suciu 1:c1f0670bb370 75 * @param filter_val - (optional)if jp is set to InternalADC, configures the filter register of the Internal ADC
Suciu 1:c1f0670bb370 76 */
Suciu 3:1a8c14043a4e 77 void CN0357::init(float range, float sensitivity, JumperConfig_t jp, uint8_t mode_val, uint8_t filter_val)
Suciu 1:c1f0670bb370 78 {
Suciu 1:c1f0670bb370 79 ad5270.frequency(500000);
Suciu 1:c1f0670bb370 80 ad7790.frequency(500000);
Suciu 1:c1f0670bb370 81
Suciu 3:1a8c14043a4e 82 float resistance = set_sensor_parameters(range, sensitivity);
Adrian Suciu 4:5c1b28aff7e1 83
Suciu 1:c1f0670bb370 84 if(jp == INTERNAL_AD7790) {
Suciu 1:c1f0670bb370 85 _AD7790_init(mode_val, filter_val);
Suciu 1:c1f0670bb370 86 }
Adrian Suciu 4:5c1b28aff7e1 87 _rdac_init(resistance);
Suciu 1:c1f0670bb370 88 }
Suciu 1:c1f0670bb370 89
Suciu 1:c1f0670bb370 90 /**
Suciu 1:c1f0670bb370 91 * @brief initializes the RDAC and sets SDO to HiZ
Suciu 1:c1f0670bb370 92 * @param resistance - resistance value to initialize the RDAC
Suciu 1:c1f0670bb370 93 * @return
Suciu 1:c1f0670bb370 94 */
Suciu 1:c1f0670bb370 95 void CN0357::_rdac_init(float resistance)
Suciu 1:c1f0670bb370 96 {
Suciu 1:c1f0670bb370 97 /* RDAC initialization*/
Suciu 1:c1f0670bb370 98 /* Compute for the nearest RDAC value from given resistance and save data to the structure */
Suciu 1:c1f0670bb370 99 set_RDAC_value(resistance);
Suciu 1:c1f0670bb370 100 /* Set AD5270 SDO to Hi-Z */
Suciu 1:c1f0670bb370 101 ad5270.set_SDO_HiZ();
Suciu 1:c1f0670bb370 102 }
Suciu 1:c1f0670bb370 103
Suciu 1:c1f0670bb370 104 /**
Suciu 1:c1f0670bb370 105 * @brief initializes the AD7790
Suciu 1:c1f0670bb370 106 * @param mode_val - configures the mode register of the Internal ADC
Suciu 1:c1f0670bb370 107 * @param filter_val - configures the filter register of the Internal ADC
Suciu 1:c1f0670bb370 108 */
Suciu 1:c1f0670bb370 109 void CN0357::_AD7790_init(uint8_t mode_val, uint8_t filter_val)
Suciu 1:c1f0670bb370 110 {
Suciu 1:c1f0670bb370 111 ad7790.reset();
Adrian Suciu 4:5c1b28aff7e1 112 wait_ms(50);
Suciu 1:c1f0670bb370 113
Adrian Suciu 4:5c1b28aff7e1 114 ad7790.write_mode_reg(mode_val);
Adrian Suciu 4:5c1b28aff7e1 115 wait_us(2);
Adrian Suciu 4:5c1b28aff7e1 116
Adrian Suciu 4:5c1b28aff7e1 117 ad7790.write_filter_reg(filter_val);
Adrian Suciu 4:5c1b28aff7e1 118 wait_ms(50);
Suciu 1:c1f0670bb370 119 }
Suciu 1:c1f0670bb370 120
Suciu 1:c1f0670bb370 121 /**
Suciu 1:c1f0670bb370 122 * @brief reads the status register of the AD7790
Suciu 1:c1f0670bb370 123 * @return status register value
Suciu 1:c1f0670bb370 124 */
Suciu 1:c1f0670bb370 125 uint8_t CN0357::read_adc_status(void)
Suciu 1:c1f0670bb370 126 {
Adrian Suciu 4:5c1b28aff7e1 127 return ad7790.read_status_reg();
Suciu 1:c1f0670bb370 128 }
Suciu 1:c1f0670bb370 129
Suciu 1:c1f0670bb370 130 /**
Suciu 1:c1f0670bb370 131 * @brief reads the ADC and computes the sensor voltage
Suciu 1:c1f0670bb370 132 * @return sensor voltage
Suciu 1:c1f0670bb370 133 */
Suciu 1:c1f0670bb370 134 float CN0357::read_sensor_voltage(void)
Suciu 1:c1f0670bb370 135 {
Adrian Suciu 4:5c1b28aff7e1 136 return ad7790.read_voltage();
Suciu 1:c1f0670bb370 137 }
Suciu 1:c1f0670bb370 138
Suciu 1:c1f0670bb370 139 /**
Suciu 1:c1f0670bb370 140 * @brief reads the data register of the AD7790
Suciu 1:c1f0670bb370 141 * @return data register value
Suciu 1:c1f0670bb370 142 */
Suciu 1:c1f0670bb370 143 uint16_t CN0357::read_sensor(void)
Suciu 1:c1f0670bb370 144 {
Adrian Suciu 4:5c1b28aff7e1 145 return ad7790.read_u16();
Suciu 1:c1f0670bb370 146 }
Suciu 1:c1f0670bb370 147
Suciu 1:c1f0670bb370 148 /**
Suciu 1:c1f0670bb370 149 * @brief reads and computes the sensor reading in PPM
Suciu 1:c1f0670bb370 150 * @return value of the sensor reading in PPM
Suciu 1:c1f0670bb370 151 */
Suciu 1:c1f0670bb370 152 float CN0357::read_ppm()
Suciu 1:c1f0670bb370 153 {
Adrian Suciu 4:5c1b28aff7e1 154 return calc_ppm(ad7790.read_voltage()); /* Convert voltage to Gas concentration*/
Suciu 1:c1f0670bb370 155 }
Suciu 1:c1f0670bb370 156
Suciu 1:c1f0670bb370 157 /**
Suciu 1:c1f0670bb370 158 * @brief computes a value in PPM from a reading received as a param
Suciu 1:c1f0670bb370 159 * @param adcVoltage - voltage to be converted to PPM
Suciu 1:c1f0670bb370 160 * @return sensor value in PPM
Suciu 1:c1f0670bb370 161 */
Suciu 1:c1f0670bb370 162 float CN0357::calc_ppm(float adcVoltage)
Suciu 1:c1f0670bb370 163 {
Suciu 1:c1f0670bb370 164 float fConcentration = 0;
Suciu 1:c1f0670bb370 165 fConcentration = (fabs(adcVoltage) / _RDACvalue) / _sensor_sensitivity;
Suciu 1:c1f0670bb370 166 return fConcentration;
Suciu 1:c1f0670bb370 167 }
Suciu 1:c1f0670bb370 168
Suciu 1:c1f0670bb370 169 /**
Suciu 1:c1f0670bb370 170 * @brief computes voltage from a 16 bit ADC value received as a parameter
Suciu 1:c1f0670bb370 171 * @param data - ADC value
Suciu 1:c1f0670bb370 172 * @return sensor voltage
Suciu 1:c1f0670bb370 173 */
Suciu 1:c1f0670bb370 174 float CN0357::data_to_voltage(uint16_t data)
Suciu 1:c1f0670bb370 175 {
Adrian Suciu 4:5c1b28aff7e1 176 return ad7790.data_to_voltage(data);
Suciu 1:c1f0670bb370 177 }
Suciu 1:c1f0670bb370 178
Suciu 1:c1f0670bb370 179 /**
Suciu 1:c1f0670bb370 180 * @brief sets a new value for the RDAC
Suciu 1:c1f0670bb370 181 * @param resistance new value for the resistance
Suciu 1:c1f0670bb370 182 * @return none
Suciu 1:c1f0670bb370 183 */
Suciu 1:c1f0670bb370 184 void CN0357::set_RDAC_value(float resistance)
Suciu 1:c1f0670bb370 185 {
Adrian Suciu 4:5c1b28aff7e1 186 _RDACvalue = ad5270.write_RDAC(resistance);
Suciu 1:c1f0670bb370 187 }
Suciu 1:c1f0670bb370 188
Suciu 1:c1f0670bb370 189 /**
Suciu 1:c1f0670bb370 190 * @brief getter method for RDAC value
Suciu 1:c1f0670bb370 191 * @return value of the RDAC in ohms
Suciu 1:c1f0670bb370 192 */
Suciu 1:c1f0670bb370 193 float CN0357::get_RDAC_value()
Suciu 1:c1f0670bb370 194 {
Suciu 1:c1f0670bb370 195 return _RDACvalue;
Suciu 1:c1f0670bb370 196 }
Suciu 1:c1f0670bb370 197
Suciu 1:c1f0670bb370 198 /**
Suciu 1:c1f0670bb370 199 * @brief set sensor range and sensitivity
Suciu 1:c1f0670bb370 200 * sets sensor range, sensitivity
Suciu 1:c1f0670bb370 201 * returns suggested resistance value for feedback resistor
Suciu 1:c1f0670bb370 202 * @param range - range of the sensor used (in ppm)
Suciu 1:c1f0670bb370 203 * @param sensitivity - sensitivity of the sensor (in A/ppm)
Suciu 1:c1f0670bb370 204 * @return suggested resistance value for feedback resistor
Suciu 1:c1f0670bb370 205 */
Suciu 1:c1f0670bb370 206 float CN0357::set_sensor_parameters(float range, float sensitivity)
Suciu 1:c1f0670bb370 207 {
Suciu 1:c1f0670bb370 208 _sensor_sensitivity = static_cast<float>(sensitivity);
Suciu 1:c1f0670bb370 209 _sensor_range = range;
Adrian Suciu 4:5c1b28aff7e1 210 return (_vref / (static_cast<float>(_sensor_range * _sensor_sensitivity)));
Suciu 1:c1f0670bb370 211 }
Suciu 1:c1f0670bb370 212
Suciu 1:c1f0670bb370 213 /**
Suciu 1:c1f0670bb370 214 * @brief getter method for sensor sensitivity
Suciu 1:c1f0670bb370 215 * @return sensor sensitivity (in A/ppm)
Suciu 1:c1f0670bb370 216 */
Suciu 1:c1f0670bb370 217 float CN0357::get_sensor_sensitivity()
Suciu 1:c1f0670bb370 218 {
Suciu 1:c1f0670bb370 219 return _sensor_sensitivity;
Suciu 1:c1f0670bb370 220 }
Suciu 1:c1f0670bb370 221
Suciu 1:c1f0670bb370 222 /**
Suciu 1:c1f0670bb370 223 * @brief getter method for sensor range
Suciu 1:c1f0670bb370 224 * @return sensor range (in ppm)
Suciu 1:c1f0670bb370 225 */
Suciu 1:c1f0670bb370 226 float CN0357::get_sensor_range()
Suciu 1:c1f0670bb370 227 {
Suciu 1:c1f0670bb370 228 return _sensor_range;
Suciu 1:c1f0670bb370 229 }