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:
Fri Apr 29 17:34:07 2016 +0300
Revision:
17:b8356808e8ad
Parent:
11:2e67c719ce37
Fixed code style and added doxygen tags

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Suciu 1:c1f0670bb370 1 /**
Suciu 1:c1f0670bb370 2 * @file main.cpp
Suciu 3:1a8c14043a4e 3 * @brief Main file for the CN0357-example project
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 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 #include "mbed.h"
Adrian Suciu 11:2e67c719ce37 48 #include "CN0357.h"
Suciu 1:c1f0670bb370 49
Suciu 1:c1f0670bb370 50 /** @mainpage
Suciu 1:c1f0670bb370 51 * CN0357 single-supply, low noise, portable gas detector circuit using an
Suciu 1:c1f0670bb370 52 * electrochemical sensor. The Alphasense CO-AX carbon monoxide sensor is used
Suciu 1:c1f0670bb370 53 * in this example. Electrochemical sensors offer several advantages for
Suciu 1:c1f0670bb370 54 * instruments that detect or measure the concentration of many toxic gases.
Suciu 1:c1f0670bb370 55 * Most sensors are gas specific and have usable resolutions under one part per
Suciu 1:c1f0670bb370 56 * million (ppm) of gas concentration.
Suciu 1:c1f0670bb370 57 *
Suciu 1:c1f0670bb370 58 * EVAL-CN0357 example program for MBED platforms. To achieve connection between
Suciu 1:c1f0670bb370 59 * the shield and the ST Nucleo, the following connections need to be made:
Suciu 1:c1f0670bb370 60 *
Suciu 1:c1f0670bb370 61 * - ICSP pin 1 -> pin 12 of the Arduino header
Suciu 1:c1f0670bb370 62 * - ICSP pin 3 -> pin 13 of the Arduino header
Suciu 1:c1f0670bb370 63 * - ICSP pin 4 -> pin 11 of the Arduino header
Suciu 1:c1f0670bb370 64 * - if VIN is not supplied, 5V to VIN
Suciu 1:c1f0670bb370 65 *
Suciu 1:c1f0670bb370 66 * @page CN0357 CN0357 Analog Devices wiki page
Suciu 1:c1f0670bb370 67 * @brief link->
Suciu 1:c1f0670bb370 68 * https://wiki.analog.com/resources/eval/user-guides/eval-adicup360/hardware/cn0357
Suciu 1:c1f0670bb370 69 * @page AD7790 AD7790 datasheet
Suciu 1:c1f0670bb370 70 * @brief link ->
Suciu 1:c1f0670bb370 71 * http://www.analog.com/media/en/technical-documentation/data-sheets/AD7790.pdf
Suciu 1:c1f0670bb370 72 * @page AD5270 AD5270 datasheet
Suciu 1:c1f0670bb370 73 * @brief link ->
Suciu 1:c1f0670bb370 74 * http://www.analog.com/media/en/technical-documentation/data-sheets/AD5270_5271.pdf
Suciu 1:c1f0670bb370 75 *
Suciu 1:c1f0670bb370 76 */
Suciu 1:c1f0670bb370 77 const float SENSOR_RANGE = 2000; ///< gas sensor range in PPM
Suciu 1:c1f0670bb370 78 const float SENSOR_SENSITIVITY = (65 * pow(10, -9)); ///< gas sensor sensitivity in A/ppm - 65 nA/ppm
Suciu 1:c1f0670bb370 79
Suciu 1:c1f0670bb370 80 Serial pc(USBTX, USBRX); ///< Serial interface to the pc
Suciu 1:c1f0670bb370 81
Suciu 1:c1f0670bb370 82 /**
Suciu 1:c1f0670bb370 83 @brief Displays CN0357 circuit readings and data to the UART
Suciu 1:c1f0670bb370 84
Suciu 1:c1f0670bb370 85 @param ui16Data - ADC data register value to be displayed
Suciu 1:c1f0670bb370 86 @param fData1 - ADC input voltage reading to be displayed
Suciu 1:c1f0670bb370 87 @param fdata2 - Gas Concentration reading to be displayed
Suciu 1:c1f0670bb370 88
Suciu 1:c1f0670bb370 89 **/
Adrian Suciu 4:5c1b28aff7e1 90 void display_data(uint8_t ui8Status_Reg, uint16_t ui16Data, float fData1, float fdata2)
Suciu 1:c1f0670bb370 91 {
Adrian Suciu 4:5c1b28aff7e1 92 pc.printf("\r\nStatus Register value: 0x%x", ui8Status_Reg);
Suciu 1:c1f0670bb370 93 pc.printf("\r\nADC Data Register Value = %#08x", ui16Data); /** Send valid ADC data register value*/
Suciu 1:c1f0670bb370 94 pc.printf("\r\nADC Input Voltage input = %f V", fData1); /** Send valid voltage input value */
Suciu 1:c1f0670bb370 95 pc.printf("\r\nGas Concentration = %f PPM", fdata2); /** Send valid gas concentration value */
Suciu 1:c1f0670bb370 96
Suciu 1:c1f0670bb370 97 pc.printf("\r\n");
Suciu 1:c1f0670bb370 98 }
Suciu 1:c1f0670bb370 99
Suciu 1:c1f0670bb370 100 /**
Suciu 1:c1f0670bb370 101 * Project entry-point - initializes the CN0357 shield, reads the data when the ADC is ready and outputs the sensor
Suciu 1:c1f0670bb370 102 * value in PPM
Suciu 1:c1f0670bb370 103 */
Adrian Suciu 4:5c1b28aff7e1 104
Adrian Suciu 4:5c1b28aff7e1 105 #define SINGLE_CONVERSION
Adrian Suciu 4:5c1b28aff7e1 106 //#define CONTINOUS_CONVERSION
Adrian Suciu 4:5c1b28aff7e1 107
Suciu 1:c1f0670bb370 108 int main()
Suciu 1:c1f0670bb370 109 {
Suciu 1:c1f0670bb370 110 /* Main variables */
Adrian Suciu 4:5c1b28aff7e1 111 CN0357 cn0357;
Adrian Suciu 4:5c1b28aff7e1 112 uint8_t ui8Status_Reg = 0;
Adrian Suciu 4:5c1b28aff7e1 113 #ifdef SINGLE_CONVERSION
Adrian Suciu 4:5c1b28aff7e1 114 cn0357.init(SENSOR_RANGE, SENSOR_SENSITIVITY);
Adrian Suciu 4:5c1b28aff7e1 115 #elif defined CONTINOUS_CONVERSION
Adrian Suciu 4:5c1b28aff7e1 116 cn0357.init(SENSOR_RANGE, SENSOR_SENSITIVITY, CN0357::INTERNAL_AD7790, 0x00, 0x07);
Adrian Suciu 4:5c1b28aff7e1 117 #else
Adrian Suciu 4:5c1b28aff7e1 118 #error define SINGLE_CONVERSION or CONTINOUS_CONVERSION, but not both
Adrian Suciu 4:5c1b28aff7e1 119 #endif
Suciu 1:c1f0670bb370 120
Suciu 1:c1f0670bb370 121
Suciu 1:c1f0670bb370 122 /* Infinite loop */
Suciu 1:c1f0670bb370 123 while (1) {
Adrian Suciu 4:5c1b28aff7e1 124 wait_ms(1000);
Adrian Suciu 4:5c1b28aff7e1 125 #ifdef CONTINOUS_CONVERSION
Adrian Suciu 4:5c1b28aff7e1 126 ui8Status_Reg = cn0357.read_adc_status(); // Read ADC Status Register //
Suciu 1:c1f0670bb370 127
Adrian Suciu 4:5c1b28aff7e1 128 if (ui8Status_Reg != 0x08) { // Checks if ADC data is available
Adrian Suciu 4:5c1b28aff7e1 129 pc.printf("\r\nStatus Register value: 0x%x", ui8Status_Reg);
Adrian Suciu 4:5c1b28aff7e1 130 } else
Adrian Suciu 4:5c1b28aff7e1 131 #endif
Adrian Suciu 4:5c1b28aff7e1 132 {
Suciu 1:c1f0670bb370 133 uint16_t ui16Adcdata = cn0357.read_sensor();
Suciu 1:c1f0670bb370 134 float fAdcVoltage = cn0357.data_to_voltage(ui16Adcdata); // Convert ADC data to voltage
Suciu 1:c1f0670bb370 135 float fConcentration = cn0357.calc_ppm(fAdcVoltage); // Convert voltage to Gas concentration
Adrian Suciu 4:5c1b28aff7e1 136 display_data(ui8Status_Reg, ui16Adcdata, fAdcVoltage, fConcentration); // Display data thru UART
Suciu 1:c1f0670bb370 137 }
Adrian Suciu 4:5c1b28aff7e1 138 }
Suciu 1:c1f0670bb370 139
Suciu 1:c1f0670bb370 140
Suciu 1:c1f0670bb370 141 /* Infinite loop, never returns. */
Suciu 1:c1f0670bb370 142 }
Suciu 1:c1f0670bb370 143