Example program for EVAL-ADMX2001

Dependencies:   ADMX2001

Committer:
nsheth
Date:
Wed Oct 27 21:18:12 2021 +0000
Revision:
8:bd0b93e35392
Parent:
5:746e78113d7d
Child:
9:29db35656fcb
Refactoring and restructuring

Who changed what in which revision?

UserRevisionLine numberNew 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 /*============= I N C L U D E S =============*/
nsheth 5:746e78113d7d 39 #include "ADMX2001.h"
nsheth 5:746e78113d7d 40 #include "ADMX2001_commands.h"
nsheth 5:746e78113d7d 41 #include "message.h"
nsheth 5:746e78113d7d 42 #include "measure.h"
nsheth 5:746e78113d7d 43 #include <stdint.h>
nsheth 5:746e78113d7d 44 #include <stdio.h>
nsheth 5:746e78113d7d 45 #include <stdlib.h>
nsheth 5:746e78113d7d 46
nsheth 5:746e78113d7d 47 /** maximum length for buffer for string operation */
nsheth 8:bd0b93e35392 48 #define MAX_DISPLAY_LENGTH 256
nsheth 5:746e78113d7d 49 /** maximum length for buffer for storing measurement values */
nsheth 5:746e78113d7d 50 #define MAX_MEASUREMENT_BUFFER 512
nsheth 5:746e78113d7d 51 /** buffer for string operation */
nsheth 5:746e78113d7d 52 char displayString[MAX_DISPLAY_LENGTH];
nsheth 5:746e78113d7d 53 /** Array to store measurement values*/
nsheth 5:746e78113d7d 54 double measurementBuffer[MAX_MEASUREMENT_BUFFER];
nsheth 5:746e78113d7d 55 /** Array to store sweep points*/
nsheth 5:746e78113d7d 56 double sweepPoints[MAX_MEASUREMENT_BUFFER];
nsheth 5:746e78113d7d 57
nsheth 5:746e78113d7d 58 extern Admx200xDev admx200xDev;
nsheth 5:746e78113d7d 59
nsheth 8:bd0b93e35392 60 static int32_t ReadAndDisplay();
nsheth 5:746e78113d7d 61
nsheth 5:746e78113d7d 62 static void DisplayMeasuredValue(double displayVal1, double displayVal2,
nsheth 5:746e78113d7d 63 double sweepVar);
nsheth 5:746e78113d7d 64
nsheth 5:746e78113d7d 65 /**
nsheth 5:746e78113d7d 66 * @brief Dsiplays the output value
nsheth 5:746e78113d7d 67 * \ref DisplayMeasuredValue
nsheth 5:746e78113d7d 68 */
nsheth 5:746e78113d7d 69 void DisplayMeasuredValue(double displayVal1, double displayVal2,
nsheth 5:746e78113d7d 70 double sweepVar)
nsheth 5:746e78113d7d 71 {
nsheth 5:746e78113d7d 72 snprintf(&displayString[0], MAX_DISPLAY_LENGTH,
nsheth 8:bd0b93e35392 73 "Count = %d, Value = (%0.6e) + (%0.6e) j\n\r",
nsheth 8:bd0b93e35392 74 (int32_t)sweepVar, displayVal1, displayVal2);
nsheth 5:746e78113d7d 75 printf("%s", &displayString[0]);
nsheth 5:746e78113d7d 76 }
nsheth 5:746e78113d7d 77
nsheth 5:746e78113d7d 78 /**
nsheth 5:746e78113d7d 79 * @brief Function to read and display measured values
nsheth 5:746e78113d7d 80 */
nsheth 8:bd0b93e35392 81 int32_t ReadAndDisplay()
nsheth 5:746e78113d7d 82 {
nsheth 5:746e78113d7d 83 int32_t i;
nsheth 5:746e78113d7d 84 int32_t status = ADMX_STATUS_SUCCESS;
nsheth 5:746e78113d7d 85
nsheth 5:746e78113d7d 86 Admx200xDev *pAdmx200x = &admx200xDev;
nsheth 5:746e78113d7d 87 uint8_t cmdId = 0;
nsheth 5:746e78113d7d 88 uint16_t addr = 0;
nsheth 5:746e78113d7d 89 uint32_t statReg;
nsheth 5:746e78113d7d 90
nsheth 5:746e78113d7d 91 int32_t numValues =
nsheth 5:746e78113d7d 92 sizeof(measurementBuffer) / sizeof(measurementBuffer[0]);
nsheth 5:746e78113d7d 93
nsheth 5:746e78113d7d 94 if (status == 0)
nsheth 5:746e78113d7d 95 {
nsheth 5:746e78113d7d 96 status = Admx200xReadFifo(pAdmx200x, &measurementBuffer[0], &numValues);
nsheth 5:746e78113d7d 97 }
nsheth 5:746e78113d7d 98 // Read sweep points
nsheth 5:746e78113d7d 99 if (status == 0)
nsheth 5:746e78113d7d 100 {
nsheth 5:746e78113d7d 101 cmdId = CMD_READ_SWEEP_POINTS;
nsheth 5:746e78113d7d 102 numValues /= 2;
nsheth 5:746e78113d7d 103 status = Admx200xSendCmd(pAdmx200x, &cmdId, &addr,
nsheth 5:746e78113d7d 104 (uint32_t *)&numValues, &statReg);
nsheth 5:746e78113d7d 105 }
nsheth 5:746e78113d7d 106 // Read sweep points
nsheth 5:746e78113d7d 107 if (status == 0)
nsheth 5:746e78113d7d 108 {
nsheth 5:746e78113d7d 109 status = Admx200xReadFifo(pAdmx200x, &sweepPoints[0], &numValues);
nsheth 5:746e78113d7d 110 }
nsheth 5:746e78113d7d 111
nsheth 5:746e78113d7d 112 if (status == 0)
nsheth 5:746e78113d7d 113 {
nsheth 5:746e78113d7d 114 for (i = 0; i < numValues; i++)
nsheth 5:746e78113d7d 115 {
nsheth 5:746e78113d7d 116 DisplayMeasuredValue(measurementBuffer[2 * i],
nsheth 5:746e78113d7d 117 measurementBuffer[2 * i + 1], sweepPoints[i]);
nsheth 5:746e78113d7d 118 }
nsheth 5:746e78113d7d 119 }
nsheth 5:746e78113d7d 120
nsheth 5:746e78113d7d 121 return status;
nsheth 5:746e78113d7d 122 }
nsheth 5:746e78113d7d 123
nsheth 8:bd0b93e35392 124
nsheth 8:bd0b93e35392 125 /**
nsheth 8:bd0b93e35392 126 * Sets up, measures and displays Impedance
nsheth 8:bd0b93e35392 127 */
nsheth 8:bd0b93e35392 128 int32_t MeasureAndDisplay()
nsheth 5:746e78113d7d 129 {
nsheth 5:746e78113d7d 130 int32_t status = 0;
nsheth 5:746e78113d7d 131 uint8_t cmdId;
nsheth 5:746e78113d7d 132 uint16_t addr;
nsheth 5:746e78113d7d 133 uint32_t data;
nsheth 5:746e78113d7d 134 uint32_t statReg;
nsheth 5:746e78113d7d 135 Admx200xDev *pAdmx200x = &admx200xDev;
nsheth 5:746e78113d7d 136 uint32_t measureStatus;
nsheth 5:746e78113d7d 137 addr = 0;
nsheth 5:746e78113d7d 138 data = 0;
nsheth 8:bd0b93e35392 139
nsheth 8:bd0b93e35392 140 // set Ig = 0?
nsheth 8:bd0b93e35392 141 // set count to 1?/3?
nsheth 5:746e78113d7d 142
nsheth 8:bd0b93e35392 143 cmdId = CMD_Z;
nsheth 8:bd0b93e35392 144 status =
nsheth 8:bd0b93e35392 145 Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 8:bd0b93e35392 146 if (status == ADMX_STATUS_INVALID_CURRENT_GAIN ||
nsheth 8:bd0b93e35392 147 status == ADMX_STATUS_INVALID_DISPLAY_MODE ||
nsheth 8:bd0b93e35392 148 status == ADMX_STATUS_UNCOMMITED_CAL ||
nsheth 8:bd0b93e35392 149 status == ADMX_STATUS_SUCCESS)
nsheth 5:746e78113d7d 150 {
nsheth 8:bd0b93e35392 151 status = Admx200xWaitForStatus(
nsheth 8:bd0b93e35392 152 pAdmx200x, SPI_TIMEOUT, ADMX200X_STATUS_MEASURE_DONE_BITM,
nsheth 8:bd0b93e35392 153 &measureStatus);
nsheth 5:746e78113d7d 154 }
nsheth 5:746e78113d7d 155 else
nsheth 5:746e78113d7d 156 {
nsheth 8:bd0b93e35392 157 status = ADMX_STATUS_FAILED;
nsheth 8:bd0b93e35392 158 }
nsheth 8:bd0b93e35392 159
nsheth 8:bd0b93e35392 160 if (status == ADMX_STATUS_INVALID_CURRENT_GAIN ||
nsheth 8:bd0b93e35392 161 status == ADMX_STATUS_INVALID_DISPLAY_MODE ||
nsheth 8:bd0b93e35392 162 status == ADMX_STATUS_UNCOMMITED_CAL ||
nsheth 8:bd0b93e35392 163 status == ADMX_STATUS_SUCCESS)
nsheth 8:bd0b93e35392 164 {
nsheth 8:bd0b93e35392 165 status = ReadAndDisplay();
nsheth 8:bd0b93e35392 166 }
nsheth 8:bd0b93e35392 167 else
nsheth 8:bd0b93e35392 168 {
nsheth 8:bd0b93e35392 169 status = ADMX_STATUS_FAILED;
nsheth 8:bd0b93e35392 170 }
nsheth 8:bd0b93e35392 171
nsheth 8:bd0b93e35392 172 if (status != ADMX_STATUS_SUCCESS)
nsheth 8:bd0b93e35392 173 {
nsheth 8:bd0b93e35392 174 status = ADMX_STATUS_FAILED;
nsheth 8:bd0b93e35392 175 ERROR_MSG("Measurement failed");
nsheth 5:746e78113d7d 176 // Clear the error
nsheth 5:746e78113d7d 177 Admx200xClearSPI(pAdmx200x);
nsheth 5:746e78113d7d 178 // Send abort command to bring state machine to IDLE if it got stuck
nsheth 5:746e78113d7d 179 cmdId = CMD_ABORT;
nsheth 5:746e78113d7d 180 Admx200xSendCmd(pAdmx200x, &cmdId, &addr, &data, &statReg);
nsheth 5:746e78113d7d 181 }
nsheth 5:746e78113d7d 182
nsheth 5:746e78113d7d 183 return status;
nsheth 5:746e78113d7d 184 }
nsheth 5:746e78113d7d 185
nsheth 5:746e78113d7d 186
nsheth 5:746e78113d7d 187
nsheth 8:bd0b93e35392 188