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:
Suciu
Date:
Wed Mar 30 17:35:04 2016 +0300
Revision:
1:c1f0670bb370
Child:
3:1a8c14043a4e
Added CN0357-example project and related drivers

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Suciu 1:c1f0670bb370 1 /**
Suciu 1:c1f0670bb370 2 * @file CN0357.cpp
Suciu 1:c1f0670bb370 3 * @brief Source file for CN0357 toxic gas sensor shield
Suciu 1:c1f0670bb370 4 * @version V0.1
Suciu 1:c1f0670bb370 5 * @author ADI
Suciu 1:c1f0670bb370 6 * @date March 2015
Suciu 1:c1f0670bb370 7 **/
Suciu 1:c1f0670bb370 8
Suciu 1:c1f0670bb370 9
Suciu 1:c1f0670bb370 10 #include "mbed.h"
Suciu 1:c1f0670bb370 11 #include "AD7790.h"
Suciu 1:c1f0670bb370 12 #include "AD5270.h"
Suciu 1:c1f0670bb370 13 #include "cn0357.h"
Suciu 1:c1f0670bb370 14
Suciu 1:c1f0670bb370 15 /**
Suciu 1:c1f0670bb370 16 * @brief CN0357 constructor
Suciu 1:c1f0670bb370 17 * @param CSAD7790 - (optional)chip select of the AD7790
Suciu 1:c1f0670bb370 18 * @param CSAD5270 - (optional)chip select of the AD5270
Suciu 1:c1f0670bb370 19 * @param MOSI - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 20 * @param MISO - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 21 * @param SCK - (optional)pin of the SPI interface
Suciu 1:c1f0670bb370 22 */
Suciu 1:c1f0670bb370 23 CN0357::CN0357(PinName CSAD7790, PinName CSAD5270, PinName MOSI, PinName MISO,
Suciu 1:c1f0670bb370 24 PinName SCK) :
Suciu 1:c1f0670bb370 25 ad7790(CSAD7790, MOSI, MISO, SCK), ad5270(CSAD5270, 20000.0, MOSI, MISO, SCK) ,
Suciu 1:c1f0670bb370 26 _sensor_sensitivity(0), _sensor_range(0), _RDACvalue(0)
Suciu 1:c1f0670bb370 27 {
Suciu 1:c1f0670bb370 28 }
Suciu 1:c1f0670bb370 29
Suciu 1:c1f0670bb370 30 /**
Suciu 1:c1f0670bb370 31 * @brief initializes the AD7790 and the AD5270
Suciu 1:c1f0670bb370 32 * @param range - range of the sensor used (in ppm)
Suciu 1:c1f0670bb370 33 * @param sensitivity - sensitivity of the sensor (A/ppm)
Suciu 1:c1f0670bb370 34 * @param jp - (optional)jumper configuration of the CN0357
Suciu 1:c1f0670bb370 35 * @param mode_val - (optional)if jp is set to InternalADC, configures the mode register of the Internal ADC
Suciu 1:c1f0670bb370 36 * @param filter_val - (optional)if jp is set to InternalADC, configures the filter register of the Internal ADC
Suciu 1:c1f0670bb370 37 */
Suciu 1:c1f0670bb370 38 void CN0357::init(float range, float sensitivity,JumperConfig_t jp,uint8_t mode_val, uint8_t filter_val)
Suciu 1:c1f0670bb370 39 {
Suciu 1:c1f0670bb370 40 ad5270.frequency(500000);
Suciu 1:c1f0670bb370 41 ad7790.frequency(500000);
Suciu 1:c1f0670bb370 42
Suciu 1:c1f0670bb370 43 float resistance = set_sensor_parameters(range,sensitivity);
Suciu 1:c1f0670bb370 44 _rdac_init(resistance);
Suciu 1:c1f0670bb370 45 if(jp == INTERNAL_AD7790) {
Suciu 1:c1f0670bb370 46 _AD7790_init(mode_val, filter_val);
Suciu 1:c1f0670bb370 47 }
Suciu 1:c1f0670bb370 48
Suciu 1:c1f0670bb370 49 }
Suciu 1:c1f0670bb370 50
Suciu 1:c1f0670bb370 51 /**
Suciu 1:c1f0670bb370 52 * @brief initializes the RDAC and sets SDO to HiZ
Suciu 1:c1f0670bb370 53 * @param resistance - resistance value to initialize the RDAC
Suciu 1:c1f0670bb370 54 * @return
Suciu 1:c1f0670bb370 55 */
Suciu 1:c1f0670bb370 56 void CN0357::_rdac_init(float resistance)
Suciu 1:c1f0670bb370 57 {
Suciu 1:c1f0670bb370 58 /* RDAC initialization*/
Suciu 1:c1f0670bb370 59 /* Compute for the nearest RDAC value from given resistance and save data to the structure */
Suciu 1:c1f0670bb370 60 set_RDAC_value(resistance);
Suciu 1:c1f0670bb370 61 /* Set AD5270 SDO to Hi-Z */
Suciu 1:c1f0670bb370 62 ad5270.set_SDO_HiZ();
Suciu 1:c1f0670bb370 63 }
Suciu 1:c1f0670bb370 64
Suciu 1:c1f0670bb370 65 /**
Suciu 1:c1f0670bb370 66 * @brief initializes the AD7790
Suciu 1:c1f0670bb370 67 * @param mode_val - configures the mode register of the Internal ADC
Suciu 1:c1f0670bb370 68 * @param filter_val - configures the filter register of the Internal ADC
Suciu 1:c1f0670bb370 69 */
Suciu 1:c1f0670bb370 70 void CN0357::_AD7790_init(uint8_t mode_val, uint8_t filter_val)
Suciu 1:c1f0670bb370 71 {
Suciu 1:c1f0670bb370 72 ad7790.reset();
Suciu 1:c1f0670bb370 73 // wait_ms(1000);
Suciu 1:c1f0670bb370 74 ad7790.write_reg(AD7790::MODE_REG, mode_val);
Suciu 1:c1f0670bb370 75 ad7790.write_reg(AD7790::FILTER_REG, filter_val);
Suciu 1:c1f0670bb370 76
Suciu 1:c1f0670bb370 77 }
Suciu 1:c1f0670bb370 78
Suciu 1:c1f0670bb370 79 /**
Suciu 1:c1f0670bb370 80 * @brief reads the status register of the AD7790
Suciu 1:c1f0670bb370 81 * @return status register value
Suciu 1:c1f0670bb370 82 */
Suciu 1:c1f0670bb370 83 uint8_t CN0357::read_adc_status(void)
Suciu 1:c1f0670bb370 84 {
Suciu 1:c1f0670bb370 85 return ad7790.read_reg(AD7790::STATUS_REG);
Suciu 1:c1f0670bb370 86 }
Suciu 1:c1f0670bb370 87
Suciu 1:c1f0670bb370 88 /**
Suciu 1:c1f0670bb370 89 * @brief reads the ADC and computes the sensor voltage
Suciu 1:c1f0670bb370 90 * @return sensor voltage
Suciu 1:c1f0670bb370 91 */
Suciu 1:c1f0670bb370 92 float CN0357::read_sensor_voltage(void)
Suciu 1:c1f0670bb370 93 {
Suciu 1:c1f0670bb370 94 return data_to_voltage(read_sensor());
Suciu 1:c1f0670bb370 95 }
Suciu 1:c1f0670bb370 96
Suciu 1:c1f0670bb370 97 /**
Suciu 1:c1f0670bb370 98 * @brief reads the data register of the AD7790
Suciu 1:c1f0670bb370 99 * @return data register value
Suciu 1:c1f0670bb370 100 */
Suciu 1:c1f0670bb370 101 uint16_t CN0357::read_sensor(void)
Suciu 1:c1f0670bb370 102 {
Suciu 1:c1f0670bb370 103 return ad7790.read_data();
Suciu 1:c1f0670bb370 104 }
Suciu 1:c1f0670bb370 105
Suciu 1:c1f0670bb370 106 /**
Suciu 1:c1f0670bb370 107 * @brief reads and computes the sensor reading in PPM
Suciu 1:c1f0670bb370 108 * @return value of the sensor reading in PPM
Suciu 1:c1f0670bb370 109 */
Suciu 1:c1f0670bb370 110 float CN0357::read_ppm()
Suciu 1:c1f0670bb370 111 {
Suciu 1:c1f0670bb370 112 return calc_ppm(read_sensor_voltage()); /* Convert voltage to Gas concentration*/
Suciu 1:c1f0670bb370 113 }
Suciu 1:c1f0670bb370 114
Suciu 1:c1f0670bb370 115 /**
Suciu 1:c1f0670bb370 116 * @brief computes a value in PPM from a reading received as a param
Suciu 1:c1f0670bb370 117 * @param adcVoltage - voltage to be converted to PPM
Suciu 1:c1f0670bb370 118 * @return sensor value in PPM
Suciu 1:c1f0670bb370 119 */
Suciu 1:c1f0670bb370 120 float CN0357::calc_ppm(float adcVoltage)
Suciu 1:c1f0670bb370 121 {
Suciu 1:c1f0670bb370 122 float fConcentration = 0;
Suciu 1:c1f0670bb370 123 fConcentration = (fabs(adcVoltage) / _RDACvalue) / _sensor_sensitivity;
Suciu 1:c1f0670bb370 124 return fConcentration;
Suciu 1:c1f0670bb370 125 }
Suciu 1:c1f0670bb370 126
Suciu 1:c1f0670bb370 127 /**
Suciu 1:c1f0670bb370 128 * @brief computes voltage from a 16 bit ADC value received as a parameter
Suciu 1:c1f0670bb370 129 * @param data - ADC value
Suciu 1:c1f0670bb370 130 * @return sensor voltage
Suciu 1:c1f0670bb370 131 */
Suciu 1:c1f0670bb370 132 float CN0357::data_to_voltage(uint16_t data)
Suciu 1:c1f0670bb370 133 {
Suciu 1:c1f0670bb370 134 return ((static_cast<float>(data) / pow(2, 15)) - 1) * 1.2; /* Bipolar voltage computation from ADC code */
Suciu 1:c1f0670bb370 135 }
Suciu 1:c1f0670bb370 136
Suciu 1:c1f0670bb370 137 /**
Suciu 1:c1f0670bb370 138 * @brief sets a new value for the RDAC
Suciu 1:c1f0670bb370 139 * @param resistance new value for the resistance
Suciu 1:c1f0670bb370 140 * @return none
Suciu 1:c1f0670bb370 141 */
Suciu 1:c1f0670bb370 142 void CN0357::set_RDAC_value(float resistance)
Suciu 1:c1f0670bb370 143 {
Suciu 1:c1f0670bb370 144 // Compute for the RDAC code nearest to the required feedback resistance
Suciu 1:c1f0670bb370 145 uint16_t RDAC_val = (resistance / 20000.0) * 1024.0;
Suciu 1:c1f0670bb370 146 // Compute for the constants used in voltage and PPM conversion computation
Suciu 1:c1f0670bb370 147 _RDACvalue = ((static_cast<float> (RDAC_val) * 20000.0) / 1024.0);
Suciu 1:c1f0670bb370 148 ad5270.write_cmd(AD5270::WRITE_CTRL_REG, 0x02); // RDAC register write protect - allow update of wiper position through digital interface
Suciu 1:c1f0670bb370 149 ad5270.write_cmd(AD5270::WRITE_RDAC, RDAC_val); // write data to the RDAC register
Suciu 1:c1f0670bb370 150 }
Suciu 1:c1f0670bb370 151
Suciu 1:c1f0670bb370 152 /**
Suciu 1:c1f0670bb370 153 * @brief getter method for RDAC value
Suciu 1:c1f0670bb370 154 * @return value of the RDAC in ohms
Suciu 1:c1f0670bb370 155 */
Suciu 1:c1f0670bb370 156 float CN0357::get_RDAC_value()
Suciu 1:c1f0670bb370 157 {
Suciu 1:c1f0670bb370 158 return _RDACvalue;
Suciu 1:c1f0670bb370 159 }
Suciu 1:c1f0670bb370 160
Suciu 1:c1f0670bb370 161 /**
Suciu 1:c1f0670bb370 162 * @brief set sensor range and sensitivity
Suciu 1:c1f0670bb370 163 * sets sensor range, sensitivity
Suciu 1:c1f0670bb370 164 * returns suggested resistance value for feedback resistor
Suciu 1:c1f0670bb370 165 * @param range - range of the sensor used (in ppm)
Suciu 1:c1f0670bb370 166 * @param sensitivity - sensitivity of the sensor (in A/ppm)
Suciu 1:c1f0670bb370 167 * @return suggested resistance value for feedback resistor
Suciu 1:c1f0670bb370 168 */
Suciu 1:c1f0670bb370 169 float CN0357::set_sensor_parameters(float range, float sensitivity)
Suciu 1:c1f0670bb370 170 {
Suciu 1:c1f0670bb370 171 _sensor_sensitivity = static_cast<float>(sensitivity);
Suciu 1:c1f0670bb370 172 _sensor_range = range;
Suciu 1:c1f0670bb370 173 return (1.2 / (static_cast<float>(_sensor_range * _sensor_sensitivity)));
Suciu 1:c1f0670bb370 174 }
Suciu 1:c1f0670bb370 175
Suciu 1:c1f0670bb370 176 /**
Suciu 1:c1f0670bb370 177 * @brief getter method for sensor sensitivity
Suciu 1:c1f0670bb370 178 * @return sensor sensitivity (in A/ppm)
Suciu 1:c1f0670bb370 179 */
Suciu 1:c1f0670bb370 180 float CN0357::get_sensor_sensitivity()
Suciu 1:c1f0670bb370 181 {
Suciu 1:c1f0670bb370 182 return _sensor_sensitivity;
Suciu 1:c1f0670bb370 183 }
Suciu 1:c1f0670bb370 184
Suciu 1:c1f0670bb370 185 /**
Suciu 1:c1f0670bb370 186 * @brief getter method for sensor range
Suciu 1:c1f0670bb370 187 * @return sensor range (in ppm)
Suciu 1:c1f0670bb370 188 */
Suciu 1:c1f0670bb370 189 float CN0357::get_sensor_range()
Suciu 1:c1f0670bb370 190 {
Suciu 1:c1f0670bb370 191 return _sensor_range;
Suciu 1:c1f0670bb370 192 }