Example program for EVAL-ADMX2001
Dependencies: ADMX2001
calibrate.cpp@15:fca7551aaf0a, 2021-11-17 (annotated)
- Committer:
- nsheth
- Date:
- Wed Nov 17 18:15:37 2021 +0000
- Revision:
- 15:fca7551aaf0a
- Parent:
- 10:186e097fb1e3
Updating default .lib file for eval platform
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nsheth | 5:746e78113d7d | 1 | /* Copyright (c) 2021 Analog Devices, Inc. All rights reserved. |
nsheth | 5:746e78113d7d | 2 | |
nsheth | 5:746e78113d7d | 3 | Redistribution and use in source and binary forms, with or without modification, |
nsheth | 5:746e78113d7d | 4 | are permitted provided that the following conditions are met: |
nsheth | 5:746e78113d7d | 5 | - Redistributions of source code must retain the above copyright notice, |
nsheth | 5:746e78113d7d | 6 | this list of conditions and the following disclaimer. |
nsheth | 5:746e78113d7d | 7 | - Redistributions in binary form must reproduce the above copyright notice, |
nsheth | 5:746e78113d7d | 8 | this list of conditions and the following disclaimer in the documentation |
nsheth | 5:746e78113d7d | 9 | and/or other materials provided with the distribution. |
nsheth | 5:746e78113d7d | 10 | - Modified versions of the software must be conspicuously marked as such. |
nsheth | 5:746e78113d7d | 11 | - This software is licensed solely and exclusively for use with processors/products |
nsheth | 5:746e78113d7d | 12 | manufactured by or for Analog Devices, Inc. |
nsheth | 5:746e78113d7d | 13 | - This software may not be combined or merged with other code in any manner |
nsheth | 5:746e78113d7d | 14 | that would cause the software to become subject to terms and conditions which |
nsheth | 5:746e78113d7d | 15 | differ from those listed here. |
nsheth | 5:746e78113d7d | 16 | - Neither the name of Analog Devices, Inc. nor the names of its contributors |
nsheth | 5:746e78113d7d | 17 | may be used to endorse or promote products derived from this software without |
nsheth | 5:746e78113d7d | 18 | specific prior written permission. |
nsheth | 5:746e78113d7d | 19 | - The use of this software may or may not infringe the patent rights of one or |
nsheth | 5:746e78113d7d | 20 | more patent holders. This license does not release you from the requirement |
nsheth | 5:746e78113d7d | 21 | that you obtain separate licenses from these patent holders to use this software. |
nsheth | 5:746e78113d7d | 22 | |
nsheth | 5:746e78113d7d | 23 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND |
nsheth | 5:746e78113d7d | 24 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
nsheth | 5:746e78113d7d | 25 | TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
nsheth | 5:746e78113d7d | 26 | NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
nsheth | 5:746e78113d7d | 27 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
nsheth | 5:746e78113d7d | 28 | (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL |
nsheth | 5:746e78113d7d | 29 | PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
nsheth | 5:746e78113d7d | 30 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
nsheth | 5:746e78113d7d | 31 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
nsheth | 5:746e78113d7d | 32 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
nsheth | 5:746e78113d7d | 33 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nsheth | 5:746e78113d7d | 34 | |
nsheth | 5:746e78113d7d | 35 | 2021-01-10-7CBSD SLA |
nsheth | 5:746e78113d7d | 36 | */ |
nsheth | 5:746e78113d7d | 37 | |
nsheth | 5:746e78113d7d | 38 | /** |
nsheth | 5:746e78113d7d | 39 | * @file calibrate.cpp |
nsheth | 5:746e78113d7d | 40 | * @brief This file contains calibrate related APIs for sdpk1 host |
nsheth | 5:746e78113d7d | 41 | */ |
nsheth | 5:746e78113d7d | 42 | |
nsheth | 5:746e78113d7d | 43 | /*============= I N C L U D E S =============*/ |
nsheth | 5:746e78113d7d | 44 | #include <stdio.h> |
nsheth | 5:746e78113d7d | 45 | #include <stdlib.h> |
nsheth | 5:746e78113d7d | 46 | #include <string.h> |
nsheth | 5:746e78113d7d | 47 | |
nsheth | 5:746e78113d7d | 48 | #include "ADMX2001.h" |
nsheth | 5:746e78113d7d | 49 | #include "ADMX2001_commands.h" |
nsheth | 5:746e78113d7d | 50 | #include "calibrate.h" |
nsheth | 5:746e78113d7d | 51 | #include "measure.h" |
nsheth | 5:746e78113d7d | 52 | |
nsheth | 5:746e78113d7d | 53 | /*============= D A T A T Y P E S =============*/ |
nsheth | 5:746e78113d7d | 54 | |
nsheth | 5:746e78113d7d | 55 | /** maximum length for buffer for storing measurement values */ |
nsheth | 5:746e78113d7d | 56 | #define MAX_CALIBRATE_BUFFER 512 |
nsheth | 5:746e78113d7d | 57 | |
nsheth | 5:746e78113d7d | 58 | /** maximum length for buffer for string operation */ |
nsheth | 5:746e78113d7d | 59 | #define MAX_CAL_DISPLAY_LENGTH 128 |
nsheth | 5:746e78113d7d | 60 | /*============= D A T A =============*/ |
nsheth | 5:746e78113d7d | 61 | /** buffer for string operation */ |
nsheth | 5:746e78113d7d | 62 | extern char displayString[MAX_CAL_DISPLAY_LENGTH]; |
nsheth | 8:bd0b93e35392 | 63 | |
nsheth | 5:746e78113d7d | 64 | extern Admx200xDev admx200xDev; |
nsheth | 5:746e78113d7d | 65 | /** Array to store measurement values*/ |
nsheth | 5:746e78113d7d | 66 | double calibrateBuffer[MAX_CALIBRATE_BUFFER]; |
nsheth | 5:746e78113d7d | 67 | /** Array to store sweep points*/ |
nsheth | 5:746e78113d7d | 68 | extern double sweepPoints[MAX_CALIBRATE_BUFFER]; |
nsheth | 5:746e78113d7d | 69 | |
nsheth | 5:746e78113d7d | 70 | static void DisplayCalibratedValue(double displayVal1, double displayVal2, |
nsheth | 5:746e78113d7d | 71 | double sweepVar); |
nsheth | 5:746e78113d7d | 72 | |
nsheth | 5:746e78113d7d | 73 | static int32_t ReadAndDisplayCalibratedValue(); |
nsheth | 5:746e78113d7d | 74 | |
nsheth | 10:186e097fb1e3 | 75 | |
nsheth | 10:186e097fb1e3 | 76 | |
nsheth | 5:746e78113d7d | 77 | /** |
nsheth | 5:746e78113d7d | 78 | * @brief Command to perform various calibration types. |
nsheth | 5:746e78113d7d | 79 | * @param [in] calType - Type of Calibrate |
nsheth | 5:746e78113d7d | 80 | * @param [in] stdR - stdR value |
nsheth | 5:746e78113d7d | 81 | * @param [in] stdX - stdX value |
nsheth | 9:29db35656fcb | 82 | * @return Returns 0 for success or negative error code. |
nsheth | 5:746e78113d7d | 83 | */ |
nsheth | 5:746e78113d7d | 84 | int32_t Calibrate(CAL_TYPE calType, float stdR, float stdX) |
nsheth | 5:746e78113d7d | 85 | { |
nsheth | 8:bd0b93e35392 | 86 | int32_t status = ADMX_STATUS_FAILED; |
nsheth | 5:746e78113d7d | 87 | uint8_t cmdID; |
nsheth | 5:746e78113d7d | 88 | uint16_t addr; |
nsheth | 5:746e78113d7d | 89 | uint32_t statReg; |
nsheth | 5:746e78113d7d | 90 | uint32_t data; |
nsheth | 5:746e78113d7d | 91 | uint32_t *pStdR = (uint32_t *)&stdR; |
nsheth | 5:746e78113d7d | 92 | uint32_t data1 = *pStdR; |
nsheth | 5:746e78113d7d | 93 | uint32_t *pStdX = (uint32_t *)&stdX; |
nsheth | 5:746e78113d7d | 94 | uint32_t data2 = *pStdX; |
nsheth | 5:746e78113d7d | 95 | uint32_t calStatus; |
nsheth | 5:746e78113d7d | 96 | /** Pointer to the handle*/ |
nsheth | 5:746e78113d7d | 97 | Admx200xDev *pAdmx200x = &admx200xDev; |
nsheth | 5:746e78113d7d | 98 | |
nsheth | 5:746e78113d7d | 99 | cmdID = CMD_CALIBRATE; |
nsheth | 5:746e78113d7d | 100 | if (calType == CAL_TYPE_LOAD) |
nsheth | 5:746e78113d7d | 101 | { |
nsheth | 5:746e78113d7d | 102 | /** Send the RT data */ |
nsheth | 5:746e78113d7d | 103 | addr = 4; |
nsheth | 5:746e78113d7d | 104 | status = Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data1, &statReg); |
nsheth | 5:746e78113d7d | 105 | if (status == ADMX_STATUS_SUCCESS) |
nsheth | 5:746e78113d7d | 106 | { |
nsheth | 5:746e78113d7d | 107 | status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM); |
nsheth | 5:746e78113d7d | 108 | /** Send the XT data */ |
nsheth | 5:746e78113d7d | 109 | addr = 5; |
nsheth | 5:746e78113d7d | 110 | status = |
nsheth | 5:746e78113d7d | 111 | Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data2, &statReg); |
nsheth | 5:746e78113d7d | 112 | if (status == ADMX_STATUS_SUCCESS) |
nsheth | 5:746e78113d7d | 113 | { |
nsheth | 5:746e78113d7d | 114 | status = (int32_t)(statReg & ADMX200X_STATUS_CODE_BITM); |
nsheth | 10:186e097fb1e3 | 115 | /** Send the Load Calibration command*/ |
nsheth | 10:186e097fb1e3 | 116 | // This calibration will use the previously set Rt and Xt data |
nsheth | 5:746e78113d7d | 117 | addr = 3; |
nsheth | 5:746e78113d7d | 118 | status = |
nsheth | 5:746e78113d7d | 119 | Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data, &statReg); |
nsheth | 5:746e78113d7d | 120 | if (status == ADMX_STATUS_SUCCESS) |
nsheth | 5:746e78113d7d | 121 | { |
nsheth | 5:746e78113d7d | 122 | status = Admx200xWaitForStatus( |
nsheth | 5:746e78113d7d | 123 | pAdmx200x, SPI_TIMEOUT, |
nsheth | 5:746e78113d7d | 124 | ADMX200X_STATUS_MEASURE_DONE_BITM, &calStatus); |
nsheth | 5:746e78113d7d | 125 | } |
nsheth | 5:746e78113d7d | 126 | ReadAndDisplayCalibratedValue(); |
nsheth | 5:746e78113d7d | 127 | } |
nsheth | 5:746e78113d7d | 128 | } |
nsheth | 5:746e78113d7d | 129 | } |
nsheth | 5:746e78113d7d | 130 | else if (calType == CAL_TYPE_NO_CAL) |
nsheth | 5:746e78113d7d | 131 | { |
nsheth | 8:bd0b93e35392 | 132 | status = ADMX_STATUS_FAILED; |
nsheth | 5:746e78113d7d | 133 | } |
nsheth | 5:746e78113d7d | 134 | else |
nsheth | 5:746e78113d7d | 135 | { |
nsheth | 5:746e78113d7d | 136 | if (calType == CAL_TYPE_SHORT) |
nsheth | 5:746e78113d7d | 137 | { |
nsheth | 5:746e78113d7d | 138 | addr = 1; |
nsheth | 5:746e78113d7d | 139 | } |
nsheth | 5:746e78113d7d | 140 | else |
nsheth | 5:746e78113d7d | 141 | { |
nsheth | 5:746e78113d7d | 142 | addr = 2; |
nsheth | 5:746e78113d7d | 143 | } |
nsheth | 5:746e78113d7d | 144 | status = Admx200xSendCmd(pAdmx200x, &cmdID, &addr, &data, &statReg); |
nsheth | 5:746e78113d7d | 145 | if (status == ADMX_STATUS_SUCCESS) |
nsheth | 5:746e78113d7d | 146 | { |
nsheth | 5:746e78113d7d | 147 | status = Admx200xWaitForStatus(pAdmx200x, SPI_TIMEOUT, |
nsheth | 5:746e78113d7d | 148 | ADMX200X_STATUS_MEASURE_DONE_BITM, |
nsheth | 5:746e78113d7d | 149 | &calStatus); |
nsheth | 5:746e78113d7d | 150 | } |
nsheth | 5:746e78113d7d | 151 | ReadAndDisplayCalibratedValue(); |
nsheth | 5:746e78113d7d | 152 | } |
nsheth | 5:746e78113d7d | 153 | |
nsheth | 5:746e78113d7d | 154 | return status; |
nsheth | 5:746e78113d7d | 155 | } |
nsheth | 5:746e78113d7d | 156 | |
nsheth | 5:746e78113d7d | 157 | /** |
nsheth | 5:746e78113d7d | 158 | * @brief Function to read and display calibrated values |
nsheth | 9:29db35656fcb | 159 | * @return Returns 0 for success or negative error code. |
nsheth | 5:746e78113d7d | 160 | */ |
nsheth | 5:746e78113d7d | 161 | int32_t ReadAndDisplayCalibratedValue() |
nsheth | 5:746e78113d7d | 162 | { |
nsheth | 5:746e78113d7d | 163 | int32_t i; |
nsheth | 5:746e78113d7d | 164 | int32_t status = ADMX_STATUS_SUCCESS; |
nsheth | 5:746e78113d7d | 165 | |
nsheth | 5:746e78113d7d | 166 | Admx200xDev *pAdmx200x = &admx200xDev; |
nsheth | 5:746e78113d7d | 167 | uint8_t cmdId = 0; |
nsheth | 5:746e78113d7d | 168 | uint16_t addr = 0; |
nsheth | 5:746e78113d7d | 169 | uint32_t statReg; |
nsheth | 5:746e78113d7d | 170 | |
nsheth | 5:746e78113d7d | 171 | int32_t numValues = sizeof(calibrateBuffer) / sizeof(calibrateBuffer[0]); |
nsheth | 10:186e097fb1e3 | 172 | // Read measurement values into the calibrate buffer |
nsheth | 5:746e78113d7d | 173 | if (status == 0) |
nsheth | 5:746e78113d7d | 174 | { |
nsheth | 5:746e78113d7d | 175 | status = Admx200xReadFifo(pAdmx200x, &calibrateBuffer[0], &numValues); |
nsheth | 5:746e78113d7d | 176 | } |
nsheth | 10:186e097fb1e3 | 177 | // Populate the fifo with sweep points |
nsheth | 5:746e78113d7d | 178 | if (status == 0) |
nsheth | 5:746e78113d7d | 179 | { |
nsheth | 5:746e78113d7d | 180 | cmdId = CMD_READ_SWEEP_POINTS; |
nsheth | 5:746e78113d7d | 181 | numValues /= 2; |
nsheth | 5:746e78113d7d | 182 | status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr, |
nsheth | 5:746e78113d7d | 183 | (uint32_t *)&numValues, &statReg); |
nsheth | 5:746e78113d7d | 184 | } |
nsheth | 10:186e097fb1e3 | 185 | // Read sweep points into the sweep points buffer |
nsheth | 5:746e78113d7d | 186 | if (status == 0) |
nsheth | 5:746e78113d7d | 187 | { |
nsheth | 5:746e78113d7d | 188 | status = Admx200xReadFifo(pAdmx200x, &sweepPoints[0], &numValues); |
nsheth | 5:746e78113d7d | 189 | } |
nsheth | 5:746e78113d7d | 190 | |
nsheth | 5:746e78113d7d | 191 | if (status == 0) |
nsheth | 5:746e78113d7d | 192 | { |
nsheth | 5:746e78113d7d | 193 | for (i = 0; i < numValues; i++) |
nsheth | 5:746e78113d7d | 194 | { |
nsheth | 5:746e78113d7d | 195 | DisplayCalibratedValue(calibrateBuffer[2 * i], |
nsheth | 5:746e78113d7d | 196 | calibrateBuffer[2 * i + 1], sweepPoints[i]); |
nsheth | 5:746e78113d7d | 197 | } |
nsheth | 5:746e78113d7d | 198 | } |
nsheth | 5:746e78113d7d | 199 | |
nsheth | 5:746e78113d7d | 200 | return status; |
nsheth | 5:746e78113d7d | 201 | } |
nsheth | 5:746e78113d7d | 202 | |
nsheth | 5:746e78113d7d | 203 | /** |
nsheth | 9:29db35656fcb | 204 | * @brief Displays the output value |
nsheth | 9:29db35656fcb | 205 | * @param [in] displayVal1 - display value 1 |
nsheth | 9:29db35656fcb | 206 | * @param [in] displayVal2 - display value 2 |
nsheth | 9:29db35656fcb | 207 | * @param [in] sweepVar - sweep variable |
nsheth | 5:746e78113d7d | 208 | */ |
nsheth | 5:746e78113d7d | 209 | void DisplayCalibratedValue(double displayVal1, double displayVal2, |
nsheth | 5:746e78113d7d | 210 | double sweepVar) |
nsheth | 5:746e78113d7d | 211 | { |
nsheth | 10:186e097fb1e3 | 212 | // Format the measured value and sweep variable |
nsheth | 5:746e78113d7d | 213 | snprintf(&displayString[0], MAX_CAL_DISPLAY_LENGTH, |
nsheth | 8:bd0b93e35392 | 214 | "Count = %d, Value = (%0.6e) + (%0.6e) j\n\r", |
nsheth | 8:bd0b93e35392 | 215 | (int32_t)sweepVar, displayVal1, displayVal2); |
nsheth | 10:186e097fb1e3 | 216 | // Print the formated string |
nsheth | 5:746e78113d7d | 217 | printf("%s", &displayString[0]); |
nsheth | 5:746e78113d7d | 218 | } |