Analog Devices / mbed-drivers
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CN0398_Diag.cpp Source File

CN0398_Diag.cpp

00001 /**
00002  *   @file     ad7124_diag.cpp
00003  *   @brief    Source file for the AD7124 wrapper used by the driver diag
00004  *   @author   Analog Devices Inc.
00005  *
00006  * For support please go to:
00007  * Github: https://github.com/analogdevicesinc/mbed-adi
00008  * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
00009  * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
00010 
00011  ********************************************************************************
00012  * Copyright 2016(c) Analog Devices, Inc.
00013  *
00014  * All rights reserved.
00015  *
00016  * Redistribution and use in source and binary forms, with or without
00017  * modification, are permitted provided that the following conditions are met:
00018  *  - Redistributions of source code must retain the above copyright
00019  *    notice, this list of conditions and the following disclaimer.
00020  *  - Redistributions in binary form must reproduce the above copyright
00021  *    notice, this list of conditions and the following disclaimer in
00022  *    the documentation and/or other materials provided with the
00023  *    distribution.
00024  *  - Neither the name of Analog Devices, Inc. nor the names of its
00025  *    contributors may be used to endorse or promote products derived
00026  *    from this software without specific prior written permission.
00027  *  - The use of this software may or may not infringe the patent rights
00028  *    of one or more patent holders.  This license does not release you
00029  *    from the requirement that you obtain separate licenses from these
00030  *    patent holders to use this software.
00031  *  - Use of the software either in source or binary form, must be run
00032  *    on or directly connected to an Analog Devices Inc. component.
00033  *
00034  * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00035  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00036  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00037  * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00038  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00039  * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00040  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00041  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00042  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00043  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00044  *
00045  ********************************************************************************/
00046 
00047 #include "mbed.h"
00048 #include <stdio.h>
00049 #include <vector>
00050 #include <string>
00051 #include "CN0398_Diag.h"
00052 
00053 #include "Thermocouple.h"
00054 
00055 
00056 extern Serial pc;
00057 extern vector<string> cmdbuffer;
00058 
00059 //#define CALIBRATION
00060 #define VH400
00061 //#define EC5
00062 
00063 
00064 CN0398_Diag::CN0398_Diag(CN0398& ad) :
00065     dut(ad), offset_voltage(default_offset_voltage)
00066 {
00067     calibration_ph[0][0] = default_calibration_ph[0][0];
00068     calibration_ph[0][1] = default_calibration_ph[0][1];
00069     calibration_ph[1][0] = default_calibration_ph[1][0];
00070     calibration_ph[1][1] = default_calibration_ph[1][1];
00071 }
00072 
00073 void CN0398_Diag::init()
00074 {
00075     dut.reset();
00076     dut.setup();
00077     dut.init();
00078 }
00079 
00080 
00081 void CN0398_Diag::write_reg()
00082 {
00083     uint8_t reg = strtol(cmdbuffer[1].c_str(), NULL, 16);
00084     uint32_t regData = strtol(cmdbuffer[2].c_str(), NULL, 16);
00085     //dut.write_reg(reg, regData);
00086     dut.ad7124.WriteDeviceRegister(static_cast<AD7124::ad7124_registers>(reg),
00087                                    regData);
00088     pc.printf("Wrote mode");
00089 }
00090 void CN0398_Diag::read_reg()
00091 {
00092     uint8_t regVal = strtol(cmdbuffer[1].c_str(), NULL, 16);
00093     pc.printf("Mode reg: %x ",
00094               dut.ad7124.ReadDeviceRegister(
00095                   static_cast<AD7124::ad7124_registers>(regVal)));
00096 }
00097 void CN0398_Diag::reset()
00098 {
00099     dut.ad7124.frequency(500000);
00100     dut.ad7124.Reset();
00101     pc.printf("Reseted AD7124");
00102     wait_ms(500);
00103 }
00104 
00105 float CN0398_Diag::data_to_voltage(uint32_t data)
00106 {
00107     data = data & 0xFFFFFF;
00108     return ((data / static_cast<float>(0xFFFFFF / 2)) - 1) * (2.5 / 1);
00109 }
00110 
00111 void CN0398_Diag::enable_channel(int channel)
00112 {
00113     dut.enable_channel(channel);
00114 }
00115 
00116 void CN0398_Diag::disable_channel(int channel)
00117 {
00118     dut.disable_channel(channel);
00119 }
00120 
00121 void CN0398_Diag::enable_current_source()
00122 {
00123     dut.enable_current_source0(11);
00124     dut.enable_current_source1(12);
00125     pc.printf("Enabled 500uA current sources on channel 11, 12\r\n");
00126 }
00127 
00128 void CN0398_Diag::toggle_output(int channel, uint8_t state)
00129 {
00130     dut.set_digital_output(static_cast<CN0398::ad_digital_output_t>(channel), state);
00131 }
00132 
00133 
00134 void CN0398_Diag::readt()
00135 {
00136 
00137     //enable_current_source();
00138     enable_channel(2);
00139     //wait_ms(100);
00140     start_single_conversion();
00141     //wait_ms(100);
00142     if (dut.ad7124.WaitForConvReady(10000) == -3) {
00143         pc.printf("TIMEOUT");
00144         return;
00145     }
00146     int32_t data;
00147     dut.ad7124.ReadData(&data);
00148     disable_channel(2);
00149 
00150     pc.printf("Channel: %d\r\n", data & 0xff);
00151     pc.printf("Data reg: %x \r\n", data);
00152     float volt = dut.data_to_voltage(data >> 8, 16);
00153     pc.printf("Voltage = %f\r\n", volt);
00154     data = (data >> 8) & 0x00ffffff;
00155     float a1 = (static_cast<float>(data) - (1 << 23));
00156     float a2 = a1 * 5000;
00157     float a3 = 16.0 * (1 << 23);
00158 
00159     float resistance = a2 / a3; //((data- (1<<23)) * 5000.0) / 16.0*(1<<23) ;
00160     pc.printf("Resistance: %f\r\n", resistance);
00161     pc.printf("Temperature = %f\r\n", (resistance - 100.0) / 0.385);
00162 
00163 }
00164 
00165 void CN0398_Diag::readm()
00166 {
00167 
00168     toggle_output(0, 1);
00169 #ifdef EC5
00170     wait_ms(10);
00171 #endif
00172     enable_channel(1);
00173     //wait_ms(100);
00174     start_single_conversion();
00175     //wait_ms(100);
00176     if (dut.ad7124.WaitForConvReady(10000) == -3) {
00177         pc.printf("TIMEOUT");
00178         return;
00179     }
00180     int32_t data;
00181     dut.ad7124.ReadData(&data);
00182     disable_channel(1);
00183     toggle_output(0, 0);
00184 
00185     pc.printf("Channel: %d\r\n", data & 0xff);
00186     pc.printf("Data reg: %x \r\n", data);
00187     float volt = dut.data_to_voltage(data >> 8, 16);
00188     pc.printf("Voltage = %f\r\n", volt);
00189     data = (data >> 8) & 0x00ffffff;
00190 
00191 
00192     float moisture;
00193 #ifdef VH400
00194     moisture = -1.18467 + 21.5371 * volt - 110.996 * (pow(volt, 2)) + 397.025 * (pow(volt, 3)) - 666.986 * (pow(volt, 4)) + 569.236 * (pow(volt, 5)) - 246.005 * (pow(volt, 6)) + 49.4867 * (pow(volt, 7)) - 3.37077 * (pow(volt, 8));
00195 #elif EC5
00196     moisture = 0.000992 * (volt * 1000) - 0.45;
00197 #endif
00198     pc.printf("Moisture = %f\r\n", moisture);
00199 
00200 
00201 
00202 }
00203 
00204 void CN0398_Diag::offsetph()
00205 {
00206     enable_channel(0);
00207     //wait_ms(100);
00208     start_single_conversion();
00209     //wait_ms(100);
00210     if (dut.ad7124.WaitForConvReady(10000) == -3) {
00211         pc.printf("TIMEOUT");
00212         return;
00213     }
00214     int32_t data;
00215     dut.ad7124.ReadData(&data);
00216     disable_channel(0);
00217     pc.printf("Channel: %d\r\n", data & 0xff);
00218     pc.printf("Data reg: %x \r\n", data);
00219     float volt = dut.data_to_voltage(data >> 8, 1);
00220     pc.printf("Voltage = %f\r\n", volt);
00221     offset_voltage = volt;
00222 
00223 }
00224 
00225 void CN0398_Diag::calibp(int point)
00226 {
00227     calibration_ph[point][0] =  strtof(cmdbuffer[1].c_str(), NULL);
00228     enable_channel(0);
00229     start_single_conversion();
00230     if (dut.ad7124.WaitForConvReady(10000) == -3) {
00231         pc.printf("TIMEOUT");
00232         return;
00233     }
00234     int32_t data;
00235     dut.ad7124.ReadData(&data);
00236     disable_channel(0);
00237     pc.printf("Channel: %d\r\n", data & 0xff);
00238     pc.printf("Data reg: %x \r\n", data);
00239     float volt = dut.data_to_voltage(data >> 8, 1);
00240     pc.printf("Voltage = %f\r\n", volt);
00241     calibration_ph[point][1] =  volt;
00242 
00243 }
00244 void CN0398_Diag::readp()
00245 {
00246     enable_channel(0);
00247     start_single_conversion();
00248     if (dut.ad7124.WaitForConvReady(10000) == -3) {
00249         pc.printf("TIMEOUT");
00250         return;
00251     }
00252     int32_t data;
00253     dut.ad7124.ReadData(&data);
00254     disable_channel(0);
00255     pc.printf("Channel: %d\r\n", data & 0xff);
00256     pc.printf("Data reg: %x \r\n", data);
00257     float volt = dut.data_to_voltage(data >> 8, 1);
00258     pc.printf("Voltage = %f\r\n", volt);
00259     float m =  (calibration_ph[1][0] - calibration_ph[0][0]) / (calibration_ph[1][1] - calibration_ph[0][1]);
00260     pc.printf("pH = %f", m * (volt - calibration_ph[1][1]) + calibration_ph[1][0]);
00261 
00262 }
00263 
00264 
00265 void CN0398_Diag::start_single_conversion()
00266 {
00267     dut.start_single_conversion();
00268 }
00269 
00270 /*void CN0398_Diag::read_data()
00271 {
00272     int32_t data;
00273     float volt;
00274     float cal_current = 0;
00275 
00276     for(int i = 0; i < 8; i++) {
00277         if(i % 2 == 0) {
00278             //enable_current_source(i+1);
00279         } else {
00280             enable_current_source(i);
00281             enable_channel(8); // calibration channel
00282             start_single_conversion();
00283             if (dut.WaitForConvReady(10000) == -3) {
00284                 pc.printf("TIMEOUT");
00285                 return;
00286             }
00287 
00288             dut.ReadData(&data);
00289             disable_channel(8);
00290 
00291             pc.printf("Channel: %d\r\n", data & 0xff);
00292             pc.printf("Data reg: %x \r\n", data);
00293             volt = data_to_voltage(data >> 8);
00294             pc.printf("Voltage = %f\r\n", volt);
00295             cal_current = conversion_fkt[8](volt, 0);
00296             conversion_results[8] = cal_current;
00297 
00298         }
00299 
00300         enable_channel(i);
00301         start_single_conversion();
00302 
00303 
00304         if (dut.WaitForConvReady(10000) == -3) {
00305             pc.printf("TIMEOUT");
00306             return;
00307         }
00308 
00309         dut.ReadData(&data);
00310 
00311         disable_channel(i);
00312 
00313 
00314         pc.printf("Channel: %d\r\n", data & 0xff);
00315         pc.printf("Data reg: %x \r\n", data);
00316         volt = data_to_voltage(data >> 8);
00317         pc.printf("Voltage = %f\r\n", volt);
00318         conversion_results[i] = conversion_fkt[i](volt, cal_current);
00319         pc.printf("\r\n");
00320 
00321     }
00322 }
00323 
00324 void CN0398_Diag::read_volt()
00325 {
00326     uint8_t regVal = strtol(cmdbuffer[1].c_str(), NULL, 16);
00327     pc.printf("Data reg: %x ",
00328               dut.ReadDeviceRegister(
00329                   static_cast<AD7124::ad7124_registers>(regVal)));
00330 }
00331 */