Code clean up and handling FIFO errors

Committer:
Vkadaba
Date:
Tue Apr 14 06:43:47 2020 +0000
Revision:
63:2655a04c00f5
Parent:
56:38b36e947602
Child:
65:d91e1d63153d
Code clean up - part 1 working; ADMW1001_config.h; ADMW1001_lut_data; ADMW1001_sensors_types.h;

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 63:2655a04c00f5 74
ADIJake 0:85855ecd3257 75 }
ADIJake 0:85855ecd3257 76
Vkadaba 23:bb685f35b08b 77 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT) {
Vkadaba 5:0728bde67bdb 78 ADMW_LOG_INFO("\tActive Alerts - ATTENTION REQUIRED:");
Vkadaba 5:0728bde67bdb 79 ADMW_LOG_INFO("\t\tLast Alert Code: %u (0x%X)",
Vkadaba 23:bb685f35b08b 80 pStatus->alertCode, pStatus->alertCode);
ADIJake 0:85855ecd3257 81
Vkadaba 23:bb685f35b08b 82 for (unsigned i = 0; i < ADMW1001_MAX_CHANNELS; i++) {
ADIJake 0:85855ecd3257 83 if (pStatus->channelAlerts[i] == 0)
ADIJake 0:85855ecd3257 84 continue;
ADIJake 0:85855ecd3257 85
Vkadaba 5:0728bde67bdb 86 ADMW_LOG_INFO("\t\tChannel #%u:", i);
Vkadaba 5:0728bde67bdb 87 ADMW_LOG_INFO("\t\t\tLast Alert Code: %u (0x%X)",
Vkadaba 23:bb685f35b08b 88 pStatus->channelAlertCodes[i],
Vkadaba 23:bb685f35b08b 89 pStatus->channelAlertCodes[i]);
Vkadaba 32:52445bef314d 90 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_ADC_NEAR_OVERRANGE)
Vkadaba 32:52445bef314d 91 ADMW_LOG_INFO("\t\t\ADC near overrange detected");
Vkadaba 32:52445bef314d 92 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_SENSOR_UNDERRANGE)
Vkadaba 32:52445bef314d 93 ADMW_LOG_INFO("\t\t\tSensor underrange detected");
Vkadaba 32:52445bef314d 94 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_SENSOR_OVERRANGE)
Vkadaba 32:52445bef314d 95 ADMW_LOG_INFO("\t\t\tSensor overrange detected");
Vkadaba 32:52445bef314d 96 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_CJ_SOFT_FAULT)
Vkadaba 32:52445bef314d 97 ADMW_LOG_INFO("\t\t\tCJC soft fault alert detected");
Vkadaba 32:52445bef314d 98 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_CJ_HARD_FAULT)
Vkadaba 32:52445bef314d 99 ADMW_LOG_INFO("\t\t\tCJC hard fault alert detected");
Vkadaba 32:52445bef314d 100 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_ADC_INPUT_OVERRANGE)
Vkadaba 32:52445bef314d 101 ADMW_LOG_INFO("\t\t\tADC input overranage alert detected");
Vkadaba 32:52445bef314d 102 if (pStatus->channelAlerts[i] & ADMW_ALERT_DETAIL_CH_SENSOR_HARDFAULT)
Vkadaba 32:52445bef314d 103 ADMW_LOG_INFO("\t\t\tChannel sensor hardfault alert detected");
ADIJake 0:85855ecd3257 104 }
ADIJake 0:85855ecd3257 105 }
ADIJake 0:85855ecd3257 106
Vkadaba 5:0728bde67bdb 107 if ((pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) ||
Vkadaba 23:bb685f35b08b 108 (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT)) {
Vkadaba 5:0728bde67bdb 109 ADMW_LOG_INFO("\t\tLast Debug Code: %u-%u",
Vkadaba 23:bb685f35b08b 110 (pStatus->debugCode >> 16) & 0xFFFF,
Vkadaba 23:bb685f35b08b 111 (pStatus->debugCode >> 0) & 0xFFFF);
ADIJake 0:85855ecd3257 112 }
ADIJake 0:85855ecd3257 113 }
ADIJake 0:85855ecd3257 114 }
ADIJake 0:85855ecd3257 115
ADIJake 0:85855ecd3257 116 void utils_printSamples(
Vkadaba 5:0728bde67bdb 117 ADMW_DATA_SAMPLE *pSampleBuffer,
ADIJake 0:85855ecd3257 118 uint32_t nNumSamples,
Vkadaba 5:0728bde67bdb 119 ADMW_MEASUREMENT_MODE eMeasurementMode)
ADIJake 0:85855ecd3257 120 {
Vkadaba 23:bb685f35b08b 121 for (uint32_t i = 0; i < nNumSamples; i++) {
Vkadaba 56:38b36e947602 122 ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Raw %f :: Processed %f :: flags: ERROR:%X , ALERT:%X",
Vkadaba 23:bb685f35b08b 123 i+1,
Vkadaba 23:bb685f35b08b 124 pSampleBuffer[i].channelId,
Vkadaba 56:38b36e947602 125 pSampleBuffer[i].rawValue,
Vkadaba 23:bb685f35b08b 126 pSampleBuffer[i].processedValue,
Vkadaba 56:38b36e947602 127 pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR,
Vkadaba 56:38b36e947602 128 pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT );
Vkadaba 10:14954555be2a 129 }
ADIJake 0:85855ecd3257 130 }
ADIJake 0:85855ecd3257 131
Vkadaba 5:0728bde67bdb 132 static void gpioCallbackFn(ADMW_GPIO_PIN ePinId, void * pArg)
ADIJake 0:85855ecd3257 133 {
ADIJake 0:85855ecd3257 134 volatile bool *pbFlag = (volatile bool *)pArg;
ADIJake 0:85855ecd3257 135 *pbFlag = true;
ADIJake 0:85855ecd3257 136 }
ADIJake 0:85855ecd3257 137
Vkadaba 5:0728bde67bdb 138 ADMW_RESULT utils_registerCallbacks(
Vkadaba 5:0728bde67bdb 139 ADMW_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 140 volatile bool *pbDataReady,
ADIJake 0:85855ecd3257 141 volatile bool *pbError,
ADIJake 0:85855ecd3257 142 volatile bool *pbAlert)
ADIJake 0:85855ecd3257 143 {
Vkadaba 5:0728bde67bdb 144 ADMW_RESULT res;
ADIJake 0:85855ecd3257 145 bool state;
ADIJake 0:85855ecd3257 146
Vkadaba 5:0728bde67bdb 147 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
Vkadaba 23:bb685f35b08b 148 gpioCallbackFn, (void *)pbDataReady);
Vkadaba 23:bb685f35b08b 149 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 150 ADMW_LOG_ERROR("Failed to register DATAREADY callback");
ADIJake 0:85855ecd3257 151 return res;
ADIJake 0:85855ecd3257 152 }
ADIJake 0:85855ecd3257 153
Vkadaba 5:0728bde67bdb 154 res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, &state);
Vkadaba 23:bb685f35b08b 155 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 156 ADMW_LOG_ERROR("Failed to get current ERROR state");
ADIJake 0:85855ecd3257 157 return res;
ADIJake 0:85855ecd3257 158 }
Vkadaba 23:bb685f35b08b 159 if (state) {
Vkadaba 5:0728bde67bdb 160 ADMW_LOG_ERROR("ERROR signal already asserted");
Vkadaba 5:0728bde67bdb 161 return ADMW_FAILURE;
ADIJake 0:85855ecd3257 162 }
Vkadaba 5:0728bde67bdb 163 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 164 gpioCallbackFn, (void *)pbError);
Vkadaba 23:bb685f35b08b 165 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 166 ADMW_LOG_ERROR("Failed to register ERROR callback");
ADIJake 0:85855ecd3257 167 return res;
ADIJake 0:85855ecd3257 168 }
ADIJake 0:85855ecd3257 169
Vkadaba 5:0728bde67bdb 170 /*res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT, &state);
Vkadaba 5:0728bde67bdb 171 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 172 {
Vkadaba 5:0728bde67bdb 173 ADMW_LOG_ERROR("Failed to get current ALERT state");
ADIJake 0:85855ecd3257 174 return res;
ADIJake 0:85855ecd3257 175 }
ADIJake 0:85855ecd3257 176 if (state)
ADIJake 0:85855ecd3257 177 {
Vkadaba 5:0728bde67bdb 178 ADMW_LOG_ERROR("ALERT signal already asserted");
Vkadaba 5:0728bde67bdb 179 return ADMW_FAILURE;
ADIJake 1:63dd03580de4 180 }*/
Vkadaba 5:0728bde67bdb 181 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 182 gpioCallbackFn, (void *)pbAlert);
Vkadaba 23:bb685f35b08b 183 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 184 ADMW_LOG_ERROR("Failed to register ALERT callback");
ADIJake 0:85855ecd3257 185 return res;
ADIJake 0:85855ecd3257 186 }
ADIJake 0:85855ecd3257 187
Vkadaba 5:0728bde67bdb 188 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 189 }
ADIJake 0:85855ecd3257 190
Vkadaba 5:0728bde67bdb 191 ADMW_RESULT utils_deregisterCallbacks(
Vkadaba 5:0728bde67bdb 192 ADMW_DEVICE_HANDLE hDevice)
ADIJake 0:85855ecd3257 193 {
Vkadaba 5:0728bde67bdb 194 ADMW_RESULT res;
ADIJake 0:85855ecd3257 195
Vkadaba 5:0728bde67bdb 196 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
Vkadaba 23:bb685f35b08b 197 NULL, NULL);
Vkadaba 23:bb685f35b08b 198 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 199 ADMW_LOG_ERROR("Failed to deregister DATAREADY callback");
ADIJake 0:85855ecd3257 200 return res;
ADIJake 0:85855ecd3257 201 }
ADIJake 0:85855ecd3257 202
Vkadaba 5:0728bde67bdb 203 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 204 NULL, NULL);
Vkadaba 23:bb685f35b08b 205 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 206 ADMW_LOG_ERROR("Failed to deregister ERROR callback");
ADIJake 0:85855ecd3257 207 return res;
ADIJake 0:85855ecd3257 208 }
ADIJake 0:85855ecd3257 209
Vkadaba 5:0728bde67bdb 210 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
Vkadaba 23:bb685f35b08b 211 NULL, NULL);
Vkadaba 23:bb685f35b08b 212 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 213 ADMW_LOG_INFO("Failed to deregister ALERT callback");
ADIJake 0:85855ecd3257 214 return res;
ADIJake 0:85855ecd3257 215 }
ADIJake 0:85855ecd3257 216
Vkadaba 5:0728bde67bdb 217 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 218 }
ADIJake 0:85855ecd3257 219
Vkadaba 5:0728bde67bdb 220 ADMW_RESULT utils_runMeasurement(
Vkadaba 5:0728bde67bdb 221 ADMW_DEVICE_HANDLE hDevice,
Vkadaba 5:0728bde67bdb 222 ADMW_MEASUREMENT_MODE eMeasurementMode)
ADIJake 0:85855ecd3257 223 {
Vkadaba 5:0728bde67bdb 224 ADMW_RESULT res;
ADIJake 0:85855ecd3257 225 volatile bool bDataReady = false;
ADIJake 0:85855ecd3257 226 volatile bool bError = false;
ADIJake 0:85855ecd3257 227 volatile bool bAlert = false;
ADIJake 0:85855ecd3257 228 res = utils_registerCallbacks(hDevice, &bDataReady, &bError, &bAlert);
Vkadaba 5:0728bde67bdb 229 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 230 return res;
ADIJake 0:85855ecd3257 231
ADIJake 0:85855ecd3257 232 /*
ADIJake 0:85855ecd3257 233 * Retrieve the number of samples per cycle, per DATAREADY pulse, etc. for
ADIJake 0:85855ecd3257 234 * this configuration.
ADIJake 0:85855ecd3257 235 */
Vkadaba 5:0728bde67bdb 236 ADMW1001_OPERATING_MODE eOperatingMode;
Vkadaba 5:0728bde67bdb 237 ADMW1001_DATAREADY_MODE eDataReadyMode;
ADIJake 0:85855ecd3257 238 uint32_t nSamplesPerDataready;
ADIJake 0:85855ecd3257 239 uint32_t nSamplesPerCycle;
ADIJake 0:85855ecd3257 240 uint8_t nBytesPerSample;
Vkadaba 5:0728bde67bdb 241 res = admw1001_GetDataReadyModeInfo(hDevice,
Vkadaba 23:bb685f35b08b 242 eMeasurementMode,
Vkadaba 23:bb685f35b08b 243 &eOperatingMode,
Vkadaba 23:bb685f35b08b 244 &eDataReadyMode,
Vkadaba 23:bb685f35b08b 245 &nSamplesPerDataready,
Vkadaba 23:bb685f35b08b 246 &nSamplesPerCycle,
Vkadaba 23:bb685f35b08b 247 &nBytesPerSample);
Vkadaba 5:0728bde67bdb 248 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 249 return res;
ADIJake 0:85855ecd3257 250
ADIJake 0:85855ecd3257 251 /*
ADIJake 0:85855ecd3257 252 * Allocate a buffer to store the samples retrieved on each DATAREADY pulse
ADIJake 0:85855ecd3257 253 */
Vkadaba 5:0728bde67bdb 254 ADMW_DATA_SAMPLE *pSampleBuffer;
Vkadaba 5:0728bde67bdb 255 pSampleBuffer = malloc(sizeof(ADMW_DATA_SAMPLE) *
ADIJake 0:85855ecd3257 256 nSamplesPerDataready);
Vkadaba 23:bb685f35b08b 257 if (pSampleBuffer == NULL) {
Vkadaba 5:0728bde67bdb 258 ADMW_LOG_ERROR("Failed to allocate sample buffer");
Vkadaba 5:0728bde67bdb 259 return ADMW_NO_MEM;
ADIJake 0:85855ecd3257 260 }
ADIJake 0:85855ecd3257 261
ADIJake 0:85855ecd3257 262 /*
ADIJake 0:85855ecd3257 263 * Kick off the measurement cycle(s) here
ADIJake 0:85855ecd3257 264 */
Vkadaba 5:0728bde67bdb 265 ADMW_LOG_INFO("Starting measurement");
Vkadaba 5:0728bde67bdb 266 res = admw_StartMeasurement(hDevice, eMeasurementMode);
Vkadaba 23:bb685f35b08b 267 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 268 ADMW_LOG_ERROR("Failed to start measurement");
ADIJake 0:85855ecd3257 269 return res;
ADIJake 0:85855ecd3257 270 }
ADIJake 0:85855ecd3257 271
ADIJake 0:85855ecd3257 272 /*
ADIJake 0:85855ecd3257 273 * Loop continuously unless operating mode is single-cycle
ADIJake 0:85855ecd3257 274 */
ADIJake 0:85855ecd3257 275 uint32_t nSampleCount = 0;
ADIJake 0:85855ecd3257 276 uint32_t nReturned;
Vkadaba 23:bb685f35b08b 277 while (true) {
Vkadaba 5:0728bde67bdb 278 ADMW_STATUS status;
ADIJake 0:85855ecd3257 279
ADIJake 0:85855ecd3257 280 /*
ADIJake 0:85855ecd3257 281 * Wait until the next batch of 1 or more samples is ready, continuously
ADIJake 0:85855ecd3257 282 * checking DATAREADY until it is asserted
ADIJake 0:85855ecd3257 283 */
Vkadaba 39:e255f52072fc 284 while (! (bDataReady || bError));
ADIJake 0:85855ecd3257 285
Vkadaba 39:e255f52072fc 286 if (!bError) {
Vkadaba 39:e255f52072fc 287 /*
Vkadaba 39:e255f52072fc 288 * Get data samples from the measurement cycle, if no error has occurred
Vkadaba 39:e255f52072fc 289 */
Vkadaba 39:e255f52072fc 290 bDataReady = false;
Vkadaba 39:e255f52072fc 291 res = admw_GetData(hDevice, eMeasurementMode, pSampleBuffer,
Vkadaba 39:e255f52072fc 292 nBytesPerSample, nSamplesPerDataready,
Vkadaba 39:e255f52072fc 293 &nReturned);
Vkadaba 39:e255f52072fc 294 if (res != ADMW_SUCCESS) {
Vkadaba 39:e255f52072fc 295 if (res == ADMW_INCOMPLETE) {
Vkadaba 39:e255f52072fc 296 /*
Vkadaba 39:e255f52072fc 297 * This is expected in cases where cycleSkipCount may
Vkadaba 39:e255f52072fc 298 * be non-zero for some channels, resulting in
Vkadaba 39:e255f52072fc 299 * variable-length sequences
Vkadaba 39:e255f52072fc 300 */
Vkadaba 55:215da406282b 301
Vkadaba 39:e255f52072fc 302 ADMW_LOG_DEBUG("Retrieved %u of %u requested data samples",
Vkadaba 56:38b36e947602 303 nReturned, nSamplesPerDataready);
Vkadaba 39:e255f52072fc 304 continue;
Vkadaba 39:e255f52072fc 305 } else {
Vkadaba 39:e255f52072fc 306 ADMW_LOG_WARN("Failed to get data samples from device");
Vkadaba 39:e255f52072fc 307 return res;
Vkadaba 39:e255f52072fc 308 }
ADIJake 0:85855ecd3257 309 }
Vkadaba 39:e255f52072fc 310
Vkadaba 39:e255f52072fc 311 /*
Vkadaba 39:e255f52072fc 312 * Display the data samples.
Vkadaba 39:e255f52072fc 313 *
Vkadaba 39:e255f52072fc 314 * NOTE: this requires a sufficient idle time between subsequent
Vkadaba 39:e255f52072fc 315 * DATAREADY pulses to allow printing to occur. Otherwise,
Vkadaba 39:e255f52072fc 316 * subsequent samples may be missed if not retrieved promptly when
Vkadaba 39:e255f52072fc 317 * the next DATAREADY assertion occurs.
Vkadaba 39:e255f52072fc 318 */
Vkadaba 39:e255f52072fc 319 utils_printSamples(pSampleBuffer, nReturned, eMeasurementMode);
Vkadaba 39:e255f52072fc 320 nSampleCount += nReturned;
ADIJake 0:85855ecd3257 321 }
ADIJake 0:85855ecd3257 322
ADIJake 0:85855ecd3257 323 /*
ADIJake 0:85855ecd3257 324 * Check and print device status if errors/alerts have been triggered
ADIJake 0:85855ecd3257 325 */
Vkadaba 23:bb685f35b08b 326 if (bError || bAlert) {
Vkadaba 5:0728bde67bdb 327 res = admw_GetStatus(hDevice, &status);
Vkadaba 23:bb685f35b08b 328 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 329 ADMW_LOG_ERROR("Failed to retrieve device status");
ADIJake 0:85855ecd3257 330 return res;
ADIJake 0:85855ecd3257 331 }
ADIJake 0:85855ecd3257 332
ADIJake 0:85855ecd3257 333 if (status.deviceStatus &
Vkadaba 23:bb685f35b08b 334 (ADMW_DEVICE_STATUS_ERROR | ADMW_DEVICE_STATUS_ALERT)) {
ADIJake 0:85855ecd3257 335 utils_printStatus(&status);
ADIJake 0:85855ecd3257 336
ADIJake 0:85855ecd3257 337 /* Break out of the loop if any errors are raised */
ADIJake 0:85855ecd3257 338 if (bError)
ADIJake 0:85855ecd3257 339 break;
ADIJake 0:85855ecd3257 340 }
ADIJake 0:85855ecd3257 341 }
ADIJake 0:85855ecd3257 342
Vkadaba 23:bb685f35b08b 343 if (eOperatingMode == ADMW1001_OPERATING_MODE_SINGLECYCLE) {
ADIJake 0:85855ecd3257 344 /*
ADIJake 0:85855ecd3257 345 * In this mode, break out of the loop when the measurement command
ADIJake 0:85855ecd3257 346 * has completed.
ADIJake 0:85855ecd3257 347 *
ADIJake 0:85855ecd3257 348 * One option is to check for the expected number of samples to be
ADIJake 0:85855ecd3257 349 * returned for the cycle. However, cycles may have variable-length
ADIJake 0:85855ecd3257 350 * sequences if the cycleSkipCount option is non-zero for any of the
ADIJake 0:85855ecd3257 351 * channels.
ADIJake 0:85855ecd3257 352 *
ADIJake 0:85855ecd3257 353 * So, instead, we check for the command-running status, which
ADIJake 0:85855ecd3257 354 * will de-assert in this mode when the measurement command has
ADIJake 0:85855ecd3257 355 * completed a single cycle.
ADIJake 0:85855ecd3257 356 */
ADIJake 0:85855ecd3257 357 bool bCommandRunning;
Vkadaba 5:0728bde67bdb 358 res = admw_GetCommandRunningState(hDevice, &bCommandRunning);
Vkadaba 23:bb685f35b08b 359 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 360 ADMW_LOG_ERROR("Failed to get command-running status");
ADIJake 0:85855ecd3257 361 return res;
ADIJake 0:85855ecd3257 362 }
ADIJake 0:85855ecd3257 363
ADIJake 0:85855ecd3257 364 if (!bCommandRunning && !bDataReady)
ADIJake 0:85855ecd3257 365 break;
ADIJake 0:85855ecd3257 366 }
ADIJake 0:85855ecd3257 367 }
ADIJake 0:85855ecd3257 368
Vkadaba 5:0728bde67bdb 369 ADMW_LOG_INFO("Stopping measurement");
Vkadaba 5:0728bde67bdb 370 res = admw_StopMeasurement(hDevice);
Vkadaba 23:bb685f35b08b 371 if (res != ADMW_SUCCESS) {
Vkadaba 5:0728bde67bdb 372 ADMW_LOG_ERROR("Failed to send stop measurement");
ADIJake 0:85855ecd3257 373 return res;
ADIJake 0:85855ecd3257 374 }
ADIJake 0:85855ecd3257 375
ADIJake 0:85855ecd3257 376 free(pSampleBuffer);
ADIJake 0:85855ecd3257 377
ADIJake 0:85855ecd3257 378 res = utils_deregisterCallbacks(hDevice);
Vkadaba 5:0728bde67bdb 379 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 380 return res;
ADIJake 0:85855ecd3257 381
Vkadaba 5:0728bde67bdb 382 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 383 }
ADIJake 0:85855ecd3257 384