Code clean up and handling FIFO errors

Committer:
Vkadaba
Date:
Thu Sep 26 06:15:39 2019 +0000
Revision:
23:bb685f35b08b
Parent:
15:b5d57daeb7c0
Child:
32:52445bef314d
Fixing code alignment

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