Analog Devices / Mbed OS EVAL-ADMX2001

Dependencies:   ADMX2001

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers calibrate.cpp Source File

calibrate.cpp

Go to the documentation of this file.
00001 /* Copyright (c) 2021 Analog Devices, Inc.  All rights reserved.
00002 
00003 Redistribution and use in source and binary forms, with or without modification,
00004 are permitted provided that the following conditions are met:
00005   - Redistributions of source code must retain the above copyright notice,
00006   this list of conditions and the following disclaimer.
00007   - Redistributions in binary form must reproduce the above copyright notice,
00008   this list of conditions and the following disclaimer in the documentation
00009   and/or other materials provided with the distribution.
00010   - Modified versions of the software must be conspicuously marked as such.
00011   - This software is licensed solely and exclusively for use with processors/products
00012   manufactured by or for Analog Devices, Inc.
00013   - This software may not be combined or merged with other code in any manner
00014   that would cause the software to become subject to terms and conditions which
00015   differ from those listed here.
00016   - Neither the name of Analog Devices, Inc. nor the names of its contributors
00017   may be used to endorse or promote products derived from this software without
00018   specific prior written permission.
00019   - The use of this software may or may not infringe the patent rights of one or
00020   more patent holders.  This license does not release you from the requirement
00021   that you obtain separate licenses from these patent holders to use this software.
00022 
00023 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND
00024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00025 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00026 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00027 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
00028 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
00029 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00030 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00032 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00033 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 
00035 2021-01-10-7CBSD SLA
00036 */
00037 
00038 /**
00039  * @file     calibrate.cpp
00040  * @brief    This file contains calibrate related APIs for sdpk1 host
00041  */
00042 
00043 /*=============  I N C L U D E S   =============*/
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 
00048 #include "ADMX2001.h"
00049 #include "ADMX2001_commands.h"
00050 #include "calibrate.h"
00051 #include "measure.h"
00052 
00053 /*=============  D A T A T Y P E S  =============*/
00054 
00055 /**  maximum length for buffer for storing measurement values */
00056 #define MAX_CALIBRATE_BUFFER 512
00057 
00058 /**  maximum length for buffer for string operation */
00059 #define MAX_CAL_DISPLAY_LENGTH 128
00060 /*=============  D A T A  =============*/
00061 /**  buffer for string operation */
00062 extern char displayString[MAX_CAL_DISPLAY_LENGTH];
00063 
00064 extern Admx200xDev admx200xDev;
00065 /** Array to store measurement values*/
00066 double calibrateBuffer[MAX_CALIBRATE_BUFFER];
00067 /** Array to store sweep points*/
00068 extern double sweepPoints[MAX_CALIBRATE_BUFFER];
00069 
00070 static void DisplayCalibratedValue(double displayVal1, double displayVal2,
00071                             double sweepVar);
00072                             
00073 static int32_t ReadAndDisplayCalibratedValue();
00074 
00075 
00076 
00077 /**
00078  * @brief  Command to perform various calibration types.
00079  * @param [in] calType - Type of Calibrate
00080  * @param [in] stdR - stdR value
00081  * @param [in] stdX - stdX value
00082  * @return Returns 0 for success or negative error code.
00083  */
00084 int32_t Calibrate(CAL_TYPE calType, float stdR, float stdX)
00085 {
00086     int32_t status = ADMX_STATUS_FAILED;
00087     uint8_t cmdID;
00088     uint16_t addr;
00089     uint32_t statReg;
00090     uint32_t data;
00091     uint32_t *pStdR = (uint32_t *)&stdR;
00092     uint32_t data1 = *pStdR;
00093     uint32_t *pStdX = (uint32_t *)&stdX;
00094     uint32_t data2 = *pStdX;
00095     uint32_t calStatus;
00096     /** Pointer to the handle*/
00097     Admx200xDev *pAdmx200x = &admx200xDev;
00098 
00099     cmdID = CMD_CALIBRATE;
00100     if (calType == CAL_TYPE_LOAD)
00101     {
00102         /** Send the RT data */
00103         addr = 4;
00104         status = Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data1, &statReg);
00105         if (status == ADMX_STATUS_SUCCESS)
00106         {
00107             status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
00108             /** Send the XT data */
00109             addr = 5;
00110             status =
00111                 Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data2, &statReg);
00112             if (status == ADMX_STATUS_SUCCESS)
00113             {
00114                 status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM);
00115                 /** Send the Load Calibration command*/
00116                 // This calibration will use the previously set Rt and Xt data
00117                 addr = 3;
00118                 status =
00119                     Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data, &statReg);
00120                 if (status == ADMX_STATUS_SUCCESS)
00121                 {
00122                     status = Admx200xWaitForStatus(
00123                         pAdmx200x, SPI_TIMEOUT,
00124                         ADMX200X_STATUS_MEASURE_DONE_BITM, &calStatus);
00125                 }
00126                 ReadAndDisplayCalibratedValue();
00127             }
00128         }
00129     }
00130     else if (calType == CAL_TYPE_NO_CAL)
00131     {
00132         status = ADMX_STATUS_FAILED;
00133     }
00134     else
00135     {
00136         if (calType == CAL_TYPE_SHORT)
00137         {
00138             addr = 1;
00139         }
00140         else
00141         {
00142             addr = 2;
00143         }
00144         status = Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data, &statReg);
00145         if (status == ADMX_STATUS_SUCCESS)
00146         {
00147             status = Admx200xWaitForStatus(pAdmx200x, SPI_TIMEOUT,
00148                                            ADMX200X_STATUS_MEASURE_DONE_BITM,
00149                                            &calStatus);
00150         }
00151         ReadAndDisplayCalibratedValue();
00152     }
00153 
00154     return status;
00155 }
00156 
00157 /**
00158  * @brief  Function to read and display calibrated values
00159  * @return Returns 0 for success or negative error code.
00160  */
00161 int32_t ReadAndDisplayCalibratedValue()
00162 {
00163     int32_t i;
00164     int32_t status = ADMX_STATUS_SUCCESS;
00165 
00166     Admx200xDev *pAdmx200x = &admx200xDev;
00167     uint8_t cmdId = 0;
00168     uint16_t addr = 0;
00169     uint32_t statReg;
00170 
00171     int32_t numValues = sizeof(calibrateBuffer) / sizeof(calibrateBuffer[0]);
00172     // Read measurement values into the calibrate buffer
00173     if (status == 0)
00174     {
00175         status = Admx200xReadFifo(pAdmx200x, &calibrateBuffer[0], &numValues);
00176     }
00177     // Populate the fifo with sweep points
00178     if (status == 0)
00179     {
00180         cmdId = CMD_READ_SWEEP_POINTS;
00181         numValues /= 2;
00182         status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr,
00183                                  (uint32_t *)&numValues, &statReg);
00184     }
00185     // Read sweep points into the sweep points buffer
00186     if (status == 0)
00187     {
00188         status = Admx200xReadFifo(pAdmx200x, &sweepPoints[0], &numValues);
00189     }
00190 
00191     if (status == 0)
00192     {
00193         for (i = 0; i < numValues; i++)
00194         {
00195             DisplayCalibratedValue(calibrateBuffer[2 * i],
00196                                    calibrateBuffer[2 * i + 1], sweepPoints[i]);
00197         }
00198     }
00199 
00200     return status;
00201 }
00202 
00203 /**
00204  * @brief  Displays the output value
00205  * @param [in] displayVal1 - display value 1
00206  * @param [in] displayVal2 - display value 2
00207  * @param [in] sweepVar - sweep variable
00208  */
00209 void DisplayCalibratedValue(double displayVal1, double displayVal2,
00210                             double sweepVar)
00211 {
00212     // Format the measured value and sweep variable
00213     snprintf(&displayString[0], MAX_CAL_DISPLAY_LENGTH,
00214              "Count = %d, Value = (%0.6e) + (%0.6e) j\n\r",
00215              (int32_t)sweepVar, displayVal1, displayVal2);
00216     // Print the formated string
00217     printf("%s", &displayString[0]);
00218 }