Code clean up and handling FIFO errors

Committer:
Vkadaba
Date:
Thu Nov 21 13:36:43 2019 +0000
Revision:
39:e255f52072fc
Parent:
32:52445bef314d
Child:
50:d84305e5e1c0
added continue in utils.c to fix the wrong measurement from being displayed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vkadaba 15:b5d57daeb7c0 1 /*
Vkadaba 15:b5d57daeb7c0 2 Copyright 2019 (c) Analog Devices, Inc.
Vkadaba 15:b5d57daeb7c0 3
Vkadaba 15:b5d57daeb7c0 4 All rights reserved.
Vkadaba 15:b5d57daeb7c0 5
Vkadaba 15:b5d57daeb7c0 6 Redistribution and use in source and binary forms, with or without
Vkadaba 15:b5d57daeb7c0 7 modification, are permitted provided that the following conditions are met:
Vkadaba 15:b5d57daeb7c0 8 - Redistributions of source code must retain the above copyright
Vkadaba 15:b5d57daeb7c0 9 notice, this list of conditions and the following disclaimer.
Vkadaba 15:b5d57daeb7c0 10 - Redistributions in binary form must reproduce the above copyright
Vkadaba 15:b5d57daeb7c0 11 notice, this list of conditions and the following disclaimer in
Vkadaba 15:b5d57daeb7c0 12 the documentation and/or other materials provided with the
Vkadaba 15:b5d57daeb7c0 13 distribution.
Vkadaba 15:b5d57daeb7c0 14 - Neither the name of Analog Devices, Inc. nor the names of its
Vkadaba 15:b5d57daeb7c0 15 contributors may be used to endorse or promote products derived
Vkadaba 15:b5d57daeb7c0 16 from this software without specific prior written permission.
Vkadaba 15:b5d57daeb7c0 17 - The use of this software may or may not infringe the patent rights
Vkadaba 15:b5d57daeb7c0 18 of one or more patent holders. This license does not release you
Vkadaba 15:b5d57daeb7c0 19 from the requirement that you obtain separate licenses from these
Vkadaba 15:b5d57daeb7c0 20 patent holders to use this software.
Vkadaba 15:b5d57daeb7c0 21 - Use of the software either in source or binary form, must be run
Vkadaba 15:b5d57daeb7c0 22 on or directly connected to an Analog Devices Inc. component.
Vkadaba 15:b5d57daeb7c0 23
Vkadaba 15:b5d57daeb7c0 24 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
Vkadaba 15:b5d57daeb7c0 25 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
Vkadaba 15:b5d57daeb7c0 26 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Vkadaba 15:b5d57daeb7c0 27 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
Vkadaba 15:b5d57daeb7c0 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Vkadaba 15:b5d57daeb7c0 29 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
Vkadaba 15:b5d57daeb7c0 30 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Vkadaba 15:b5d57daeb7c0 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Vkadaba 15:b5d57daeb7c0 32 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Vkadaba 15:b5d57daeb7c0 33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Vkadaba 15:b5d57daeb7c0 34 */
Vkadaba 15:b5d57daeb7c0 35
Vkadaba 15:b5d57daeb7c0 36 /*!
Vkadaba 15:b5d57daeb7c0 37 ******************************************************************************
Vkadaba 15:b5d57daeb7c0 38 * @file: utils.c
Vkadaba 15:b5d57daeb7c0 39 * @brief: Interface Utilities for ADMW1001
Vkadaba 15:b5d57daeb7c0 40 *-----------------------------------------------------------------------------
Vkadaba 15:b5d57daeb7c0 41 */
ADIJake 0:85855ecd3257 42 #include <stdlib.h>
ADIJake 0:85855ecd3257 43
ADIJake 0:85855ecd3257 44 #include "utils.h"
Vkadaba 5:0728bde67bdb 45 #include "admw_log.h"
ADIJake 0:85855ecd3257 46
ADIJake 0:85855ecd3257 47 void utils_printStatus(
Vkadaba 5:0728bde67bdb 48 ADMW_STATUS *pStatus)
ADIJake 0:85855ecd3257 49 {
Vkadaba 5:0728bde67bdb 50 ADMW_LOG_INFO("Status Summary:");
ADIJake 0:85855ecd3257 51
Vkadaba 23:bb685f35b08b 52 if (pStatus->deviceStatus == 0) {
Vkadaba 5:0728bde67bdb 53 ADMW_LOG_INFO("\tNo errors detected");
Vkadaba 23:bb685f35b08b 54 } else {
Vkadaba 5:0728bde67bdb 55 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_BUSY)
Vkadaba 5:0728bde67bdb 56 ADMW_LOG_INFO("\tCommand running");
Vkadaba 5:0728bde67bdb 57 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_DATAREADY)
Vkadaba 5:0728bde67bdb 58 ADMW_LOG_INFO("\tData ready");
Vkadaba 5:0728bde67bdb 59 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR)
Vkadaba 5:0728bde67bdb 60 ADMW_LOG_INFO("\tActive Errors - RESET REQUIRED");
Vkadaba 5:0728bde67bdb 61 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_FIFO_ERROR)
Vkadaba 5:0728bde67bdb 62 ADMW_LOG_INFO("\tActive FIFO Errors - ATTENTION REQUIRED");
Vkadaba 5:0728bde67bdb 63 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_CONFIG_ERROR)
Vkadaba 5:0728bde67bdb 64 ADMW_LOG_INFO("\tActive Configuration Errors - ATTENTION REQUIRED");
Vkadaba 5:0728bde67bdb 65 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_LUT_ERROR)
Vkadaba 5:0728bde67bdb 66 ADMW_LOG_INFO("\tActive Look-Up Table Errors - ATTENTION REQUIRED");
Vkadaba 23:bb685f35b08b 67
ADIJake 0:85855ecd3257 68
Vkadaba 23:bb685f35b08b 69 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) {
Vkadaba 5:0728bde67bdb 70 ADMW_LOG_INFO("\tActive Errors - ATTENTION REQUIRED");
Vkadaba 5:0728bde67bdb 71 ADMW_LOG_INFO("\t\tLast Error Code: %u (0x%X)",
Vkadaba 23:bb685f35b08b 72 pStatus->errorCode, pStatus->errorCode);
ADIJake 0:85855ecd3257 73
Vkadaba 23:bb685f35b08b 74 if (pStatus->diagnosticsStatus == 0) {
Vkadaba 5:0728bde67bdb 75 ADMW_LOG_INFO("\t\tNo diagnostics faults detected");
Vkadaba 23:bb685f35b08b 76 } else {
Vkadaba 5:0728bde67bdb 77 ADMW_LOG_INFO("\t\tActive diagnostics faults:");
ADIJake 0:85855ecd3257 78
Vkadaba 5:0728bde67bdb 79 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CHECKSUM_ERROR)
Vkadaba 5:0728bde67bdb 80 ADMW_LOG_INFO("\t\t\tInternal Checksum fault detected");
Vkadaba 23:bb685f35b08b 81
Vkadaba 23:bb685f35b08b 82
Vkadaba 23:bb685f35b08b 83
Vkadaba 5:0728bde67bdb 84 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CONVERSION_ERROR)
Vkadaba 5:0728bde67bdb 85 ADMW_LOG_INFO("\t\t\tInternal ADC Conversions fault detected");
Vkadaba 5:0728bde67bdb 86 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CALIBRATION_ERROR)
Vkadaba 5:0728bde67bdb 87 ADMW_LOG_INFO("\t\t\tInternal Device Calibrations fault detected");
ADIJake 0:85855ecd3257 88 }
ADIJake 0:85855ecd3257 89 }
ADIJake 0:85855ecd3257 90
Vkadaba 23:bb685f35b08b 91 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT) {
Vkadaba 5:0728bde67bdb 92 ADMW_LOG_INFO("\tActive Alerts - ATTENTION REQUIRED:");
Vkadaba 5:0728bde67bdb 93 ADMW_LOG_INFO("\t\tLast Alert Code: %u (0x%X)",
Vkadaba 23:bb685f35b08b 94 pStatus->alertCode, pStatus->alertCode);
ADIJake 0:85855ecd3257 95
Vkadaba 23:bb685f35b08b 96 for (unsigned i = 0; i < ADMW1001_MAX_CHANNELS; i++) {
ADIJake 0:85855ecd3257 97 if (pStatus->channelAlerts[i] == 0)
ADIJake 0:85855ecd3257 98 continue;
ADIJake 0:85855ecd3257 99
Vkadaba 5:0728bde67bdb 100 ADMW_LOG_INFO("\t\tChannel #%u:", i);
Vkadaba 5:0728bde67bdb 101 ADMW_LOG_INFO("\t\t\tLast Alert Code: %u (0x%X)",
Vkadaba 23:bb685f35b08b 102 pStatus->channelAlertCodes[i],
Vkadaba 23:bb685f35b08b 103 pStatus->channelAlertCodes[i]);
Vkadaba 32:52445bef314d 104 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_ADC_NEAR_OVERRANGE)
Vkadaba 32:52445bef314d 105 ADMW_LOG_INFO("\t\t\ADC near overrange detected");
Vkadaba 32:52445bef314d 106 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_SENSOR_UNDERRANGE)
Vkadaba 32:52445bef314d 107 ADMW_LOG_INFO("\t\t\tSensor underrange detected");
Vkadaba 32:52445bef314d 108 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_SENSOR_OVERRANGE)
Vkadaba 32:52445bef314d 109 ADMW_LOG_INFO("\t\t\tSensor overrange detected");
Vkadaba 32:52445bef314d 110 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_CJ_SOFT_FAULT)
Vkadaba 32:52445bef314d 111 ADMW_LOG_INFO("\t\t\tCJC soft fault alert detected");
Vkadaba 32:52445bef314d 112 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_CJ_HARD_FAULT)
Vkadaba 32:52445bef314d 113 ADMW_LOG_INFO("\t\t\tCJC hard fault alert detected");
Vkadaba 32:52445bef314d 114 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_ADC_INPUT_OVERRANGE)
Vkadaba 32:52445bef314d 115 ADMW_LOG_INFO("\t\t\tADC input overranage alert detected");
Vkadaba 32:52445bef314d 116 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_SENSOR_HARDFAULT)
Vkadaba 32:52445bef314d 117 ADMW_LOG_INFO("\t\t\tChannel sensor hardfault alert detected");
ADIJake 0:85855ecd3257 118 }
ADIJake 0:85855ecd3257 119 }
ADIJake 0:85855ecd3257 120
Vkadaba 5:0728bde67bdb 121 if ((pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) ||
Vkadaba 23:bb685f35b08b 122 (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT)) {
Vkadaba 5:0728bde67bdb 123 ADMW_LOG_INFO("\t\tLast Debug Code: %u-%u",
Vkadaba 23:bb685f35b08b 124 (pStatus->debugCode >> 16) & 0xFFFF,
Vkadaba 23:bb685f35b08b 125 (pStatus->debugCode >> 0) & 0xFFFF);
ADIJake 0:85855ecd3257 126 }
ADIJake 0:85855ecd3257 127 }
ADIJake 0:85855ecd3257 128 }
ADIJake 0:85855ecd3257 129
ADIJake 0:85855ecd3257 130 void utils_printSamples(
Vkadaba 5:0728bde67bdb 131 ADMW_DATA_SAMPLE *pSampleBuffer,
ADIJake 0:85855ecd3257 132 uint32_t nNumSamples,
Vkadaba 5:0728bde67bdb 133 ADMW_MEASUREMENT_MODE eMeasurementMode)
ADIJake 0:85855ecd3257 134 {
Vkadaba 23:bb685f35b08b 135
Vkadaba 23:bb685f35b08b 136 for (uint32_t i = 0; i < nNumSamples; i++) {
Vkadaba 23:bb685f35b08b 137 ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Raw %0X %f :: Processed %f :: flags:%s%s",
Vkadaba 23:bb685f35b08b 138 i+1,
Vkadaba 23:bb685f35b08b 139 pSampleBuffer[i].channelId,
Vkadaba 23:bb685f35b08b 140 pSampleBuffer[i].rawValue, (pSampleBuffer[i].rawValue/1024.0),
Vkadaba 23:bb685f35b08b 141 pSampleBuffer[i].processedValue,
Vkadaba 23:bb685f35b08b 142 pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR ? " ERROR" : "",
Vkadaba 23:bb685f35b08b 143 pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT ? " ALERT" : "");
Vkadaba 10:14954555be2a 144 }
ADIJake 0:85855ecd3257 145 }
ADIJake 0:85855ecd3257 146
Vkadaba 5:0728bde67bdb 147 static void gpioCallbackFn(ADMW_GPIO_PIN ePinId, void * pArg)
ADIJake 0:85855ecd3257 148 {
ADIJake 0:85855ecd3257 149 volatile bool *pbFlag = (volatile bool *)pArg;
ADIJake 0:85855ecd3257 150 *pbFlag = true;
ADIJake 0:85855ecd3257 151 }
ADIJake 0:85855ecd3257 152
Vkadaba 5:0728bde67bdb 153 ADMW_RESULT utils_registerCallbacks(
Vkadaba 5:0728bde67bdb 154 ADMW_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 155 volatile bool *pbDataReady,
ADIJake 0:85855ecd3257 156 volatile bool *pbError,
ADIJake 0:85855ecd3257 157 volatile bool *pbAlert)
ADIJake 0:85855ecd3257 158 {
Vkadaba 5:0728bde67bdb 159 ADMW_RESULT res;
ADIJake 0:85855ecd3257 160 bool state;
ADIJake 0:85855ecd3257 161
Vkadaba 5:0728bde67bdb 162 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
Vkadaba 23:bb685f35b08b 163 gpioCallbackFn, (void *)pbDataReady);
Vkadaba 23:bb685f35b08b 164 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 165 ADMW_LOG_ERROR("Failed to register DATAREADY callback");
ADIJake 0:85855ecd3257 166 return res;
ADIJake 0:85855ecd3257 167 }
ADIJake 0:85855ecd3257 168
Vkadaba 5:0728bde67bdb 169 res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, &state);
Vkadaba 23:bb685f35b08b 170 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 171 ADMW_LOG_ERROR("Failed to get current ERROR state");
ADIJake 0:85855ecd3257 172 return res;
ADIJake 0:85855ecd3257 173 }
Vkadaba 23:bb685f35b08b 174 if (state) {
Vkadaba 5:0728bde67bdb 175 ADMW_LOG_ERROR("ERROR signal already asserted");
Vkadaba 5:0728bde67bdb 176 return ADMW_FAILURE;
ADIJake 0:85855ecd3257 177 }
Vkadaba 5:0728bde67bdb 178 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 179 gpioCallbackFn, (void *)pbError);
Vkadaba 23:bb685f35b08b 180 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 181 ADMW_LOG_ERROR("Failed to register ERROR callback");
ADIJake 0:85855ecd3257 182 return res;
ADIJake 0:85855ecd3257 183 }
ADIJake 0:85855ecd3257 184
Vkadaba 5:0728bde67bdb 185 /*res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT, &state);
Vkadaba 5:0728bde67bdb 186 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 187 {
Vkadaba 5:0728bde67bdb 188 ADMW_LOG_ERROR("Failed to get current ALERT state");
ADIJake 0:85855ecd3257 189 return res;
ADIJake 0:85855ecd3257 190 }
ADIJake 0:85855ecd3257 191 if (state)
ADIJake 0:85855ecd3257 192 {
Vkadaba 5:0728bde67bdb 193 ADMW_LOG_ERROR("ALERT signal already asserted");
Vkadaba 5:0728bde67bdb 194 return ADMW_FAILURE;
ADIJake 1:63dd03580de4 195 }*/
Vkadaba 5:0728bde67bdb 196 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 197 gpioCallbackFn, (void *)pbAlert);
Vkadaba 23:bb685f35b08b 198 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 199 ADMW_LOG_ERROR("Failed to register ALERT callback");
ADIJake 0:85855ecd3257 200 return res;
ADIJake 0:85855ecd3257 201 }
ADIJake 0:85855ecd3257 202
Vkadaba 5:0728bde67bdb 203 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 204 }
ADIJake 0:85855ecd3257 205
Vkadaba 5:0728bde67bdb 206 ADMW_RESULT utils_deregisterCallbacks(
Vkadaba 5:0728bde67bdb 207 ADMW_DEVICE_HANDLE hDevice)
ADIJake 0:85855ecd3257 208 {
Vkadaba 5:0728bde67bdb 209 ADMW_RESULT res;
ADIJake 0:85855ecd3257 210
Vkadaba 5:0728bde67bdb 211 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
Vkadaba 23:bb685f35b08b 212 NULL, NULL);
Vkadaba 23:bb685f35b08b 213 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 214 ADMW_LOG_ERROR("Failed to deregister DATAREADY callback");
ADIJake 0:85855ecd3257 215 return res;
ADIJake 0:85855ecd3257 216 }
ADIJake 0:85855ecd3257 217
Vkadaba 5:0728bde67bdb 218 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 219 NULL, NULL);
Vkadaba 23:bb685f35b08b 220 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 221 ADMW_LOG_ERROR("Failed to deregister ERROR callback");
ADIJake 0:85855ecd3257 222 return res;
ADIJake 0:85855ecd3257 223 }
ADIJake 0:85855ecd3257 224
Vkadaba 5:0728bde67bdb 225 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 226 NULL, NULL);
Vkadaba 23:bb685f35b08b 227 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 228 ADMW_LOG_INFO("Failed to deregister ALERT callback");
ADIJake 0:85855ecd3257 229 return res;
ADIJake 0:85855ecd3257 230 }
ADIJake 0:85855ecd3257 231
Vkadaba 5:0728bde67bdb 232 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 233 }
ADIJake 0:85855ecd3257 234
Vkadaba 5:0728bde67bdb 235 ADMW_RESULT utils_runMeasurement(
Vkadaba 5:0728bde67bdb 236 ADMW_DEVICE_HANDLE hDevice,
Vkadaba 5:0728bde67bdb 237 ADMW_MEASUREMENT_MODE eMeasurementMode)
ADIJake 0:85855ecd3257 238 {
Vkadaba 5:0728bde67bdb 239 ADMW_RESULT res;
ADIJake 0:85855ecd3257 240
ADIJake 0:85855ecd3257 241 volatile bool bDataReady = false;
ADIJake 0:85855ecd3257 242 volatile bool bError = false;
ADIJake 0:85855ecd3257 243 volatile bool bAlert = false;
ADIJake 0:85855ecd3257 244 res = utils_registerCallbacks(hDevice, &bDataReady, &bError, &bAlert);
Vkadaba 5:0728bde67bdb 245 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 246 return res;
ADIJake 0:85855ecd3257 247
ADIJake 0:85855ecd3257 248 /*
ADIJake 0:85855ecd3257 249 * Retrieve the number of samples per cycle, per DATAREADY pulse, etc. for
ADIJake 0:85855ecd3257 250 * this configuration.
ADIJake 0:85855ecd3257 251 */
Vkadaba 5:0728bde67bdb 252 ADMW1001_OPERATING_MODE eOperatingMode;
Vkadaba 5:0728bde67bdb 253 ADMW1001_DATAREADY_MODE eDataReadyMode;
ADIJake 0:85855ecd3257 254 uint32_t nSamplesPerDataready;
ADIJake 0:85855ecd3257 255 uint32_t nSamplesPerCycle;
ADIJake 0:85855ecd3257 256 uint8_t nBytesPerSample;
Vkadaba 5:0728bde67bdb 257 res = admw1001_GetDataReadyModeInfo(hDevice,
Vkadaba 23:bb685f35b08b 258 eMeasurementMode,
Vkadaba 23:bb685f35b08b 259 &eOperatingMode,
Vkadaba 23:bb685f35b08b 260 &eDataReadyMode,
Vkadaba 23:bb685f35b08b 261 &nSamplesPerDataready,
Vkadaba 23:bb685f35b08b 262 &nSamplesPerCycle,
Vkadaba 23:bb685f35b08b 263 &nBytesPerSample);
Vkadaba 5:0728bde67bdb 264 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 265 return res;
ADIJake 0:85855ecd3257 266
ADIJake 0:85855ecd3257 267 /*
ADIJake 0:85855ecd3257 268 * Allocate a buffer to store the samples retrieved on each DATAREADY pulse
ADIJake 0:85855ecd3257 269 */
Vkadaba 5:0728bde67bdb 270 ADMW_DATA_SAMPLE *pSampleBuffer;
Vkadaba 5:0728bde67bdb 271 pSampleBuffer = malloc(sizeof(ADMW_DATA_SAMPLE) *
ADIJake 0:85855ecd3257 272 nSamplesPerDataready);
Vkadaba 23:bb685f35b08b 273 if (pSampleBuffer == NULL) {
Vkadaba 5:0728bde67bdb 274 ADMW_LOG_ERROR("Failed to allocate sample buffer");
Vkadaba 5:0728bde67bdb 275 return ADMW_NO_MEM;
ADIJake 0:85855ecd3257 276 }
ADIJake 0:85855ecd3257 277
ADIJake 0:85855ecd3257 278 /*
ADIJake 0:85855ecd3257 279 * Kick off the measurement cycle(s) here
ADIJake 0:85855ecd3257 280 */
Vkadaba 5:0728bde67bdb 281 ADMW_LOG_INFO("Starting measurement");
Vkadaba 5:0728bde67bdb 282 res = admw_StartMeasurement(hDevice, eMeasurementMode);
Vkadaba 23:bb685f35b08b 283 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 284 ADMW_LOG_ERROR("Failed to start measurement");
ADIJake 0:85855ecd3257 285 return res;
ADIJake 0:85855ecd3257 286 }
ADIJake 0:85855ecd3257 287
ADIJake 0:85855ecd3257 288 /*
ADIJake 0:85855ecd3257 289 * Loop continuously unless operating mode is single-cycle
ADIJake 0:85855ecd3257 290 */
ADIJake 0:85855ecd3257 291 uint32_t nSampleCount = 0;
ADIJake 0:85855ecd3257 292 uint32_t nReturned;
Vkadaba 23:bb685f35b08b 293 while (true) {
Vkadaba 5:0728bde67bdb 294 ADMW_STATUS status;
ADIJake 0:85855ecd3257 295
ADIJake 0:85855ecd3257 296 /*
ADIJake 0:85855ecd3257 297 * Wait until the next batch of 1 or more samples is ready, continuously
ADIJake 0:85855ecd3257 298 * checking DATAREADY until it is asserted
ADIJake 0:85855ecd3257 299 */
Vkadaba 39:e255f52072fc 300 while (! (bDataReady || bError));
ADIJake 0:85855ecd3257 301
Vkadaba 39:e255f52072fc 302 if (!bError) {
Vkadaba 39:e255f52072fc 303 /*
Vkadaba 39:e255f52072fc 304 * Get data samples from the measurement cycle, if no error has occurred
Vkadaba 39:e255f52072fc 305 */
Vkadaba 39:e255f52072fc 306 bDataReady = false;
Vkadaba 39:e255f52072fc 307 res = admw_GetData(hDevice, eMeasurementMode, pSampleBuffer,
Vkadaba 39:e255f52072fc 308 nBytesPerSample, nSamplesPerDataready,
Vkadaba 39:e255f52072fc 309 &nReturned);
Vkadaba 39:e255f52072fc 310 if (res != ADMW_SUCCESS) {
Vkadaba 39:e255f52072fc 311 if (res == ADMW_INCOMPLETE) {
Vkadaba 39:e255f52072fc 312 /*
Vkadaba 39:e255f52072fc 313 * This is expected in cases where cycleSkipCount may
Vkadaba 39:e255f52072fc 314 * be non-zero for some channels, resulting in
Vkadaba 39:e255f52072fc 315 * variable-length sequences
Vkadaba 39:e255f52072fc 316 */
Vkadaba 39:e255f52072fc 317 ADMW_LOG_DEBUG("Retrieved %u of %u requested data samples",
Vkadaba 39:e255f52072fc 318 nReturned, nSamplesPerDataready);
Vkadaba 39:e255f52072fc 319 continue;
Vkadaba 39:e255f52072fc 320 } else {
Vkadaba 39:e255f52072fc 321 ADMW_LOG_WARN("Failed to get data samples from device");
Vkadaba 39:e255f52072fc 322 return res;
Vkadaba 39:e255f52072fc 323 }
ADIJake 0:85855ecd3257 324 }
Vkadaba 39:e255f52072fc 325
Vkadaba 39:e255f52072fc 326 /*
Vkadaba 39:e255f52072fc 327 * Display the data samples.
Vkadaba 39:e255f52072fc 328 *
Vkadaba 39:e255f52072fc 329 * NOTE: this requires a sufficient idle time between subsequent
Vkadaba 39:e255f52072fc 330 * DATAREADY pulses to allow printing to occur. Otherwise,
Vkadaba 39:e255f52072fc 331 * subsequent samples may be missed if not retrieved promptly when
Vkadaba 39:e255f52072fc 332 * the next DATAREADY assertion occurs.
Vkadaba 39:e255f52072fc 333 */
Vkadaba 39:e255f52072fc 334 utils_printSamples(pSampleBuffer, nReturned, eMeasurementMode);
Vkadaba 39:e255f52072fc 335 nSampleCount += nReturned;
ADIJake 0:85855ecd3257 336 }
ADIJake 0:85855ecd3257 337
ADIJake 0:85855ecd3257 338 /*
ADIJake 0:85855ecd3257 339 * Check and print device status if errors/alerts have been triggered
ADIJake 0:85855ecd3257 340 */
Vkadaba 23:bb685f35b08b 341 if (bError || bAlert) {
Vkadaba 5:0728bde67bdb 342 res = admw_GetStatus(hDevice, &status);
Vkadaba 23:bb685f35b08b 343 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 344 ADMW_LOG_ERROR("Failed to retrieve device status");
ADIJake 0:85855ecd3257 345 return res;
ADIJake 0:85855ecd3257 346 }
ADIJake 0:85855ecd3257 347
ADIJake 0:85855ecd3257 348 if (status.deviceStatus &
Vkadaba 23:bb685f35b08b 349 (ADMW_DEVICE_STATUS_ERROR | ADMW_DEVICE_STATUS_ALERT)) {
ADIJake 0:85855ecd3257 350 utils_printStatus(&status);
ADIJake 0:85855ecd3257 351
ADIJake 0:85855ecd3257 352 /* Break out of the loop if any errors are raised */
ADIJake 0:85855ecd3257 353 if (bError)
ADIJake 0:85855ecd3257 354 break;
ADIJake 0:85855ecd3257 355 }
ADIJake 0:85855ecd3257 356 }
ADIJake 0:85855ecd3257 357
Vkadaba 23:bb685f35b08b 358 if (eOperatingMode == ADMW1001_OPERATING_MODE_SINGLECYCLE) {
ADIJake 0:85855ecd3257 359 /*
ADIJake 0:85855ecd3257 360 * In this mode, break out of the loop when the measurement command
ADIJake 0:85855ecd3257 361 * has completed.
ADIJake 0:85855ecd3257 362 *
ADIJake 0:85855ecd3257 363 * One option is to check for the expected number of samples to be
ADIJake 0:85855ecd3257 364 * returned for the cycle. However, cycles may have variable-length
ADIJake 0:85855ecd3257 365 * sequences if the cycleSkipCount option is non-zero for any of the
ADIJake 0:85855ecd3257 366 * channels.
ADIJake 0:85855ecd3257 367 *
ADIJake 0:85855ecd3257 368 * So, instead, we check for the command-running status, which
ADIJake 0:85855ecd3257 369 * will de-assert in this mode when the measurement command has
ADIJake 0:85855ecd3257 370 * completed a single cycle.
ADIJake 0:85855ecd3257 371 */
ADIJake 0:85855ecd3257 372 bool bCommandRunning;
Vkadaba 5:0728bde67bdb 373 res = admw_GetCommandRunningState(hDevice, &bCommandRunning);
Vkadaba 23:bb685f35b08b 374 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 375 ADMW_LOG_ERROR("Failed to get command-running status");
ADIJake 0:85855ecd3257 376 return res;
ADIJake 0:85855ecd3257 377 }
ADIJake 0:85855ecd3257 378
ADIJake 0:85855ecd3257 379 if (!bCommandRunning && !bDataReady)
ADIJake 0:85855ecd3257 380 break;
ADIJake 0:85855ecd3257 381 }
ADIJake 0:85855ecd3257 382 }
ADIJake 0:85855ecd3257 383
Vkadaba 5:0728bde67bdb 384 ADMW_LOG_INFO("Stopping measurement");
Vkadaba 5:0728bde67bdb 385 res = admw_StopMeasurement(hDevice);
Vkadaba 23:bb685f35b08b 386 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 387 ADMW_LOG_ERROR("Failed to send stop measurement");
ADIJake 0:85855ecd3257 388 return res;
ADIJake 0:85855ecd3257 389 }
ADIJake 0:85855ecd3257 390
ADIJake 0:85855ecd3257 391 free(pSampleBuffer);
ADIJake 0:85855ecd3257 392
ADIJake 0:85855ecd3257 393 res = utils_deregisterCallbacks(hDevice);
Vkadaba 5:0728bde67bdb 394 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 395 return res;
ADIJake 0:85855ecd3257 396
Vkadaba 5:0728bde67bdb 397 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 398 }
ADIJake 0:85855ecd3257 399