Analog Devices / Mbed OS EVAL-ADMX2001

Dependencies:   ADMX2001

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers measure.cpp Source File

measure.cpp

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 /*=============  I N C L U D E S   =============*/
00039 #include "ADMX2001.h"
00040 #include "ADMX2001_commands.h"
00041 #include "message.h"
00042 #include "measure.h"
00043 #include <stdint.h>
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 
00047 /*=============  D A T A T Y P E S  =============*/
00048 
00049 /**  maximum length for buffer for string operation */
00050 #define MAX_DISPLAY_LENGTH 256
00051 /**  maximum length for buffer for storing measurement values */
00052 #define MAX_MEASUREMENT_BUFFER 512
00053 
00054 /*=============  D A T A  =============*/
00055 
00056 /**  buffer for string operation */
00057 char displayString[MAX_DISPLAY_LENGTH];
00058 /** Array to store measurement values*/
00059 double measurementBuffer[MAX_MEASUREMENT_BUFFER];
00060 /** Array to store sweep points*/
00061 double sweepPoints[MAX_MEASUREMENT_BUFFER];
00062 
00063 extern Admx200xDev admx200xDev;
00064 
00065 static int32_t ReadAndDisplay();
00066 
00067 static void DisplayMeasuredValue(double displayVal1, double displayVal2,
00068                                  double sweepVar);
00069 
00070 /*=============  C O D E  =============*/
00071 
00072 /**
00073  * @brief  Dsiplays the output value
00074  * @param [in] displayVal1 - display value 1
00075  * @param [in] displayVal2 - display value 2
00076  * @param [in] sweepVar - sweep variable
00077  */
00078 void DisplayMeasuredValue(double displayVal1, double displayVal2,
00079                           double sweepVar)
00080 {
00081     // Format the measured value and sweep variable
00082     snprintf(&displayString[0], MAX_DISPLAY_LENGTH,
00083              "Count = %d, Value = (%0.6e) + (%0.6e) j\n\r",
00084              (int32_t)sweepVar, displayVal1, displayVal2);
00085     // Print the formated string
00086     printf("%s", &displayString[0]);
00087 }
00088 
00089 /**
00090  * @brief  Function to read and display measured values
00091  * @return Returns 0 for success or error code.
00092  */
00093 int32_t ReadAndDisplay()
00094 {
00095     int32_t i;
00096     int32_t status = ADMX_STATUS_SUCCESS;
00097 
00098     Admx200xDev *pAdmx200x = &admx200xDev;
00099     uint8_t cmdId = 0;
00100     uint16_t addr = 0;
00101     uint32_t statReg;
00102 
00103     int32_t numValues =
00104         sizeof(measurementBuffer) / sizeof(measurementBuffer[0]);
00105     // Read measurement values into the measurement buffer
00106     if (status == 0)
00107     {
00108         status = Admx200xReadFifo(pAdmx200x, &measurementBuffer[0], &numValues);
00109     }
00110     // Populate the fifo with sweep points
00111     if (status == 0)
00112     {
00113         cmdId = CMD_READ_SWEEP_POINTS;
00114         numValues /= 2;
00115         status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr,
00116                                  (uint32_t *)&numValues, &statReg);
00117     }
00118     // Read sweep points into the sweep points buffer
00119     if (status == 0)
00120     {
00121         status = Admx200xReadFifo(pAdmx200x, &sweepPoints[0], &numValues);
00122     }
00123 
00124     if (status == 0)
00125     {
00126         for (i = 0; i < numValues; i++)
00127         {
00128             DisplayMeasuredValue(measurementBuffer[2 * i],
00129                                  measurementBuffer[2 * i + 1], sweepPoints[i]);
00130         }
00131     }
00132 
00133     return status;
00134 }
00135 
00136 
00137 /**
00138  * Sets up, measures and displays Impedance
00139  * @return Returns 0 for success or error code.
00140  */
00141 int32_t MeasureAndDisplay()
00142 {
00143     int32_t status = 0;
00144     uint8_t cmdId;
00145     uint16_t addr;
00146     uint32_t data;
00147     uint32_t statReg;
00148     Admx200xDev *pAdmx200x = &admx200xDev;
00149     uint32_t measureStatus;
00150     addr = 0;
00151     data = 0;
00152     
00153     // Initiate measurement
00154     cmdId = CMD_Z;
00155     status =
00156         Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
00157     if (status == ADMX_STATUS_INVALID_CURRENT_GAIN ||
00158         status == ADMX_STATUS_INVALID_DISPLAY_MODE ||
00159         status == ADMX_STATUS_UNCOMMITED_CAL ||
00160         status == ADMX_STATUS_SUCCESS)
00161     {
00162         // Wait for measurement to complete successfully 
00163         status = Admx200xWaitForStatus(
00164             pAdmx200x, SPI_TIMEOUT, ADMX200X_STATUS_MEASURE_DONE_BITM,
00165             &measureStatus);
00166     }
00167     else
00168     {
00169         status = ADMX_STATUS_FAILED;
00170     }
00171 
00172     if (status == ADMX_STATUS_INVALID_CURRENT_GAIN ||
00173         status == ADMX_STATUS_INVALID_DISPLAY_MODE ||
00174         status == ADMX_STATUS_UNCOMMITED_CAL ||
00175         status == ADMX_STATUS_SUCCESS)
00176     {
00177         status = ReadAndDisplay();
00178     }
00179     else
00180     {
00181         status = ADMX_STATUS_FAILED;
00182     }
00183 
00184     if (status != ADMX_STATUS_SUCCESS)
00185     {
00186         status = ADMX_STATUS_FAILED;
00187         ERROR_MSG("Measurement failed");
00188         // Clear the error
00189         Admx200xClearSPI(pAdmx200x);
00190         // Send abort command to bring state machine to IDLE if it got stuck
00191         cmdId = CMD_ABORT;
00192         Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
00193     }
00194 
00195     return status;
00196 }
00197 
00198 
00199 
00200