AdiSense1000_V21 MBED API

Fork of AdiSense1000 by PoC_Team

Committer:
seanwilson10
Date:
Tue Jul 24 11:49:57 2018 +0000
Revision:
32:262fc8aeb486
Parent:
28:4eb837cd71df
v2.1 imported

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kevin1990 16:e4f2689363bb 1 #include <stdlib.h>
kevin1990 16:e4f2689363bb 2
kevin1990 16:e4f2689363bb 3 #include "utils.h"
kevin1990 16:e4f2689363bb 4 #include "inc/adi_sense_log.h"
kevin1990 16:e4f2689363bb 5
kevin1990 16:e4f2689363bb 6 void utils_printStatus(
kevin1990 16:e4f2689363bb 7 ADI_SENSE_STATUS *pStatus)
kevin1990 16:e4f2689363bb 8 {
kevin1990 16:e4f2689363bb 9 ADI_SENSE_LOG_INFO("Status Summary:");
kevin1990 16:e4f2689363bb 10
kevin1990 16:e4f2689363bb 11 if (pStatus->deviceStatus == 0)
kevin1990 16:e4f2689363bb 12 {
kevin1990 16:e4f2689363bb 13 ADI_SENSE_LOG_INFO("\tNo errors detected");
kevin1990 16:e4f2689363bb 14 }
kevin1990 16:e4f2689363bb 15 else
kevin1990 16:e4f2689363bb 16 {
kevin1990 16:e4f2689363bb 17 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_BUSY)
kevin1990 16:e4f2689363bb 18 ADI_SENSE_LOG_INFO("\tCommand running");
kevin1990 16:e4f2689363bb 19 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_DATAREADY)
kevin1990 16:e4f2689363bb 20 ADI_SENSE_LOG_INFO("\tData ready");
kevin1990 16:e4f2689363bb 21 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_ERROR)
kevin1990 16:e4f2689363bb 22 ADI_SENSE_LOG_INFO("\tActive Errors - RESET REQUIRED");
kevin1990 16:e4f2689363bb 23 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_FIFO_ERROR)
kevin1990 16:e4f2689363bb 24 ADI_SENSE_LOG_INFO("\tActive FIFO Errors - ATTENTION REQUIRED");
kevin1990 16:e4f2689363bb 25 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_CONFIG_ERROR)
kevin1990 16:e4f2689363bb 26 ADI_SENSE_LOG_INFO("\tActive Configuration Errors - ATTENTION REQUIRED");
kevin1990 16:e4f2689363bb 27 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_LUT_ERROR)
kevin1990 16:e4f2689363bb 28 ADI_SENSE_LOG_INFO("\tActive Look-Up Table Errors - ATTENTION REQUIRED");
kevin1990 28:4eb837cd71df 29 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_EXT_FLASH_ERROR)
kevin1990 28:4eb837cd71df 30 ADI_SENSE_LOG_INFO("\tActive External Flash Errors - ATTENTION REQUIRED");
kevin1990 16:e4f2689363bb 31
kevin1990 16:e4f2689363bb 32 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_ERROR)
kevin1990 16:e4f2689363bb 33 {
kevin1990 16:e4f2689363bb 34 ADI_SENSE_LOG_INFO("\tActive Errors - ATTENTION REQUIRED");
kevin1990 16:e4f2689363bb 35 ADI_SENSE_LOG_INFO("\t\tLast Error Code: %u (0x%X)",
kevin1990 16:e4f2689363bb 36 pStatus->errorCode, pStatus->errorCode);
kevin1990 16:e4f2689363bb 37
kevin1990 16:e4f2689363bb 38 if (pStatus->diagnosticsStatus == 0)
kevin1990 16:e4f2689363bb 39 {
kevin1990 16:e4f2689363bb 40 ADI_SENSE_LOG_INFO("\t\tNo diagnostics faults detected");
kevin1990 16:e4f2689363bb 41 }
kevin1990 16:e4f2689363bb 42 else
kevin1990 16:e4f2689363bb 43 {
kevin1990 16:e4f2689363bb 44 ADI_SENSE_LOG_INFO("\t\tActive diagnostics faults:");
kevin1990 16:e4f2689363bb 45
kevin1990 16:e4f2689363bb 46 if (pStatus->diagnosticsStatus & ADI_SENSE_DIAGNOSTICS_STATUS_CHECKSUM_ERROR)
kevin1990 16:e4f2689363bb 47 ADI_SENSE_LOG_INFO("\t\t\tInternal Checksum fault detected");
kevin1990 16:e4f2689363bb 48 if (pStatus->diagnosticsStatus & ADI_SENSE_DIAGNOSTICS_STATUS_COMMS_ERROR)
kevin1990 16:e4f2689363bb 49 ADI_SENSE_LOG_INFO("\t\t\tInternal Communications fault detected");
kevin1990 16:e4f2689363bb 50 if (pStatus->diagnosticsStatus & ADI_SENSE_DIAGNOSTICS_STATUS_SUPPLY_MONITOR_ERROR)
kevin1990 16:e4f2689363bb 51 ADI_SENSE_LOG_INFO("\t\t\tSupply Monitor fault detected");
kevin1990 16:e4f2689363bb 52 if (pStatus->diagnosticsStatus & ADI_SENSE_DIAGNOSTICS_STATUS_SUPPLY_CAP_ERROR)
kevin1990 16:e4f2689363bb 53 ADI_SENSE_LOG_INFO("\t\t\tSupply Regulator Capacitor fault detected");
kevin1990 16:e4f2689363bb 54 if (pStatus->diagnosticsStatus & ADI_SENSE_DIAGNOSTICS_STATUS_CONVERSION_ERROR)
kevin1990 16:e4f2689363bb 55 ADI_SENSE_LOG_INFO("\t\t\tInternal ADC Conversions fault detected");
kevin1990 16:e4f2689363bb 56 if (pStatus->diagnosticsStatus & ADI_SENSE_DIAGNOSTICS_STATUS_CALIBRATION_ERROR)
kevin1990 16:e4f2689363bb 57 ADI_SENSE_LOG_INFO("\t\t\tInternal Device Calibrations fault detected");
kevin1990 16:e4f2689363bb 58 }
kevin1990 16:e4f2689363bb 59 }
kevin1990 16:e4f2689363bb 60
kevin1990 16:e4f2689363bb 61 if (pStatus->deviceStatus & ADI_SENSE_DEVICE_STATUS_ALERT)
kevin1990 16:e4f2689363bb 62 {
kevin1990 16:e4f2689363bb 63 ADI_SENSE_LOG_INFO("\tActive Alerts - ATTENTION REQUIRED:");
kevin1990 16:e4f2689363bb 64 ADI_SENSE_LOG_INFO("\t\tLast Alert Code: %u (0x%X)",
kevin1990 16:e4f2689363bb 65 pStatus->alertCode, pStatus->alertCode);
kevin1990 16:e4f2689363bb 66
kevin1990 16:e4f2689363bb 67 for (unsigned i = 0; i < ADI_SENSE_1000_MAX_CHANNELS; i++)
kevin1990 16:e4f2689363bb 68 {
kevin1990 16:e4f2689363bb 69 if (pStatus->channelAlerts[i] == 0)
kevin1990 16:e4f2689363bb 70 continue;
kevin1990 16:e4f2689363bb 71
kevin1990 16:e4f2689363bb 72 ADI_SENSE_LOG_INFO("\t\tChannel #%u:", i);
kevin1990 16:e4f2689363bb 73 ADI_SENSE_LOG_INFO("\t\t\tLast Alert Code: %u (0x%X)",
kevin1990 16:e4f2689363bb 74 pStatus->channelAlertCodes[i],
kevin1990 16:e4f2689363bb 75 pStatus->channelAlertCodes[i]);
kevin1990 16:e4f2689363bb 76 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_TIMEOUT)
kevin1990 16:e4f2689363bb 77 ADI_SENSE_LOG_INFO("\t\t\tTimeout alert detected");
kevin1990 16:e4f2689363bb 78 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_UNDER_RANGE)
kevin1990 16:e4f2689363bb 79 ADI_SENSE_LOG_INFO("\t\t\tUnder Range alert detected");
kevin1990 16:e4f2689363bb 80 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_OVER_RANGE)
kevin1990 16:e4f2689363bb 81 ADI_SENSE_LOG_INFO("\t\t\tOver Range alert detected");
kevin1990 16:e4f2689363bb 82 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_LOW_LIMIT)
kevin1990 16:e4f2689363bb 83 ADI_SENSE_LOG_INFO("\t\t\tLow limit alert detected");
kevin1990 16:e4f2689363bb 84 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_HIGH_LIMIT)
kevin1990 16:e4f2689363bb 85 ADI_SENSE_LOG_INFO("\t\t\tHigh Limit alert detected");
kevin1990 16:e4f2689363bb 86 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_SENSOR_OPEN)
kevin1990 16:e4f2689363bb 87 ADI_SENSE_LOG_INFO("\t\t\tSensor Fault alert detected");
kevin1990 16:e4f2689363bb 88 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_REF_DETECT)
kevin1990 16:e4f2689363bb 89 ADI_SENSE_LOG_INFO("\t\t\tReference Detection alert detected");
kevin1990 16:e4f2689363bb 90 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_CONFIG_ERR)
kevin1990 16:e4f2689363bb 91 ADI_SENSE_LOG_INFO("\t\t\tConfiguration Error alert detected");
kevin1990 16:e4f2689363bb 92 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_LUT_ERR)
kevin1990 16:e4f2689363bb 93 ADI_SENSE_LOG_INFO("\t\t\tLook-Up Table Error alert detected");
kevin1990 16:e4f2689363bb 94 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_SENSOR_NOT_READY)
kevin1990 16:e4f2689363bb 95 ADI_SENSE_LOG_INFO("\t\t\tSensor Not Ready alert detected");
kevin1990 16:e4f2689363bb 96 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_COMP_NOT_READY)
kevin1990 16:e4f2689363bb 97 ADI_SENSE_LOG_INFO("\t\t\tCompensation Channel Not Ready alert detected");
kevin1990 16:e4f2689363bb 98 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_LUT_UNDER_RANGE)
kevin1990 16:e4f2689363bb 99 ADI_SENSE_LOG_INFO("\t\t\tUnder Look-Up Table Range alert detected");
kevin1990 16:e4f2689363bb 100 if (pStatus->channelAlerts[i] & ADI_SENSE_CHANNEL_ALERT_LUT_OVER_RANGE)
kevin1990 16:e4f2689363bb 101 ADI_SENSE_LOG_INFO("\t\t\tOver Look-Up Table Range alert detected");
kevin1990 16:e4f2689363bb 102 }
kevin1990 16:e4f2689363bb 103 }
kevin1990 16:e4f2689363bb 104 }
kevin1990 16:e4f2689363bb 105 }
kevin1990 16:e4f2689363bb 106
kevin1990 16:e4f2689363bb 107 void utils_printSamples(
kevin1990 16:e4f2689363bb 108 ADI_SENSE_DATA_SAMPLE *pSampleBuffer,
kevin1990 28:4eb837cd71df 109 uint32_t nNumSamples,
kevin1990 28:4eb837cd71df 110 ADI_SENSE_MEASUREMENT_MODE eMeasurementMode)
kevin1990 16:e4f2689363bb 111 {
kevin1990 28:4eb837cd71df 112 bool fftMode = (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_FFT);
kevin1990 28:4eb837cd71df 113
kevin1990 16:e4f2689363bb 114 for (uint32_t i = 0; i < nNumSamples; i++)
kevin1990 16:e4f2689363bb 115 {
kevin1990 28:4eb837cd71df 116 if (fftMode)
kevin1990 28:4eb837cd71df 117 {
kevin1990 28:4eb837cd71df 118 ADI_SENSE_LOG_INFO("Sample # %2d Channel # %2d :: Bin/Raw %8d :: Magnitude %e :: flags:%s%s",
kevin1990 28:4eb837cd71df 119 i+1,
kevin1990 28:4eb837cd71df 120 pSampleBuffer[i].channelId,
kevin1990 28:4eb837cd71df 121 pSampleBuffer[i].rawValue,
kevin1990 28:4eb837cd71df 122 pSampleBuffer[i].processedValue,
kevin1990 28:4eb837cd71df 123 pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ERROR ? " ERROR" : "",
kevin1990 28:4eb837cd71df 124 pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ALERT ? " ALERT" : "");
kevin1990 28:4eb837cd71df 125 }
kevin1990 28:4eb837cd71df 126 else
kevin1990 28:4eb837cd71df 127 {
kevin1990 28:4eb837cd71df 128 ADI_SENSE_LOG_INFO("Sample # %2d Channel # %2d :: Raw %8d :: Processed %f :: flags:%s%s",
kevin1990 28:4eb837cd71df 129 i+1,
kevin1990 28:4eb837cd71df 130 pSampleBuffer[i].channelId,
kevin1990 28:4eb837cd71df 131 pSampleBuffer[i].rawValue,
kevin1990 28:4eb837cd71df 132 pSampleBuffer[i].processedValue,
kevin1990 28:4eb837cd71df 133 pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ERROR ? " ERROR" : "",
kevin1990 28:4eb837cd71df 134 pSampleBuffer[i].status & ADI_SENSE_DEVICE_STATUS_ALERT ? " ALERT" : "");
kevin1990 28:4eb837cd71df 135 }
kevin1990 16:e4f2689363bb 136 }
kevin1990 16:e4f2689363bb 137 }
kevin1990 16:e4f2689363bb 138
kevin1990 16:e4f2689363bb 139 static void gpioCallbackFn(ADI_SENSE_GPIO_PIN ePinId, void * pArg)
kevin1990 16:e4f2689363bb 140 {
kevin1990 16:e4f2689363bb 141 volatile bool_t *pbFlag = (volatile bool_t *)pArg;
kevin1990 16:e4f2689363bb 142 *pbFlag = true;
kevin1990 16:e4f2689363bb 143 }
kevin1990 16:e4f2689363bb 144
kevin1990 16:e4f2689363bb 145 ADI_SENSE_RESULT utils_registerCallbacks(
kevin1990 16:e4f2689363bb 146 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 16:e4f2689363bb 147 volatile bool_t *pbDataReady,
kevin1990 16:e4f2689363bb 148 volatile bool_t *pbError,
kevin1990 16:e4f2689363bb 149 volatile bool_t *pbAlert)
kevin1990 16:e4f2689363bb 150 {
kevin1990 16:e4f2689363bb 151 ADI_SENSE_RESULT res;
kevin1990 16:e4f2689363bb 152
kevin1990 16:e4f2689363bb 153 res = adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_DATAREADY,
kevin1990 16:e4f2689363bb 154 gpioCallbackFn, (void *)pbDataReady);
kevin1990 16:e4f2689363bb 155 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 156 {
kevin1990 16:e4f2689363bb 157 ADI_SENSE_LOG_ERROR("Failed to register DATAREADY callback");
kevin1990 16:e4f2689363bb 158 return res;
kevin1990 16:e4f2689363bb 159 }
kevin1990 16:e4f2689363bb 160
kevin1990 16:e4f2689363bb 161 res = adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_ERROR,
kevin1990 16:e4f2689363bb 162 gpioCallbackFn, (void *)pbError);
kevin1990 16:e4f2689363bb 163 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 164 {
kevin1990 16:e4f2689363bb 165 ADI_SENSE_LOG_ERROR("Failed to register ERROR callback");
kevin1990 16:e4f2689363bb 166 return res;
kevin1990 16:e4f2689363bb 167 }
kevin1990 16:e4f2689363bb 168
kevin1990 16:e4f2689363bb 169 res = adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_ALERT,
kevin1990 16:e4f2689363bb 170 gpioCallbackFn, (void *)pbAlert);
kevin1990 16:e4f2689363bb 171 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 172 {
kevin1990 16:e4f2689363bb 173 ADI_SENSE_LOG_ERROR("Failed to register ALERT callback");
kevin1990 16:e4f2689363bb 174 return res;
kevin1990 16:e4f2689363bb 175 }
kevin1990 16:e4f2689363bb 176
kevin1990 16:e4f2689363bb 177 return ADI_SENSE_SUCCESS;
kevin1990 16:e4f2689363bb 178 }
kevin1990 16:e4f2689363bb 179
kevin1990 16:e4f2689363bb 180 ADI_SENSE_RESULT utils_deregisterCallbacks(
kevin1990 16:e4f2689363bb 181 ADI_SENSE_DEVICE_HANDLE hDevice)
kevin1990 16:e4f2689363bb 182 {
kevin1990 16:e4f2689363bb 183 ADI_SENSE_RESULT res;
kevin1990 16:e4f2689363bb 184
kevin1990 16:e4f2689363bb 185 res = adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_DATAREADY,
kevin1990 16:e4f2689363bb 186 NULL, NULL);
kevin1990 16:e4f2689363bb 187 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 188 {
kevin1990 16:e4f2689363bb 189 ADI_SENSE_LOG_ERROR("Failed to deregister DATAREADY callback");
kevin1990 16:e4f2689363bb 190 return res;
kevin1990 16:e4f2689363bb 191 }
kevin1990 16:e4f2689363bb 192
kevin1990 16:e4f2689363bb 193 res = adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_ERROR,
kevin1990 16:e4f2689363bb 194 NULL, NULL);
kevin1990 16:e4f2689363bb 195 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 196 {
kevin1990 16:e4f2689363bb 197 ADI_SENSE_LOG_ERROR("Failed to deregister ERROR callback");
kevin1990 16:e4f2689363bb 198 return res;
kevin1990 16:e4f2689363bb 199 }
kevin1990 16:e4f2689363bb 200
kevin1990 16:e4f2689363bb 201 res = adi_sense_RegisterGpioCallback(hDevice, ADI_SENSE_GPIO_PIN_ALERT,
kevin1990 16:e4f2689363bb 202 NULL, NULL);
kevin1990 16:e4f2689363bb 203 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 204 {
kevin1990 16:e4f2689363bb 205 ADI_SENSE_LOG_INFO("Failed to deregister ALERT callback");
kevin1990 16:e4f2689363bb 206 return res;
kevin1990 16:e4f2689363bb 207 }
kevin1990 16:e4f2689363bb 208
kevin1990 16:e4f2689363bb 209 return ADI_SENSE_SUCCESS;
kevin1990 16:e4f2689363bb 210 }
kevin1990 16:e4f2689363bb 211
kevin1990 16:e4f2689363bb 212 ADI_SENSE_RESULT utils_runMeasurement(
kevin1990 16:e4f2689363bb 213 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 16:e4f2689363bb 214 ADI_SENSE_MEASUREMENT_MODE eMeasurementMode)
kevin1990 16:e4f2689363bb 215 {
kevin1990 16:e4f2689363bb 216 ADI_SENSE_RESULT res;
kevin1990 16:e4f2689363bb 217
kevin1990 16:e4f2689363bb 218 volatile bool_t bDataReady = false;
kevin1990 16:e4f2689363bb 219 volatile bool_t bError = false;
kevin1990 16:e4f2689363bb 220 volatile bool_t bAlert = false;
kevin1990 16:e4f2689363bb 221 res = utils_registerCallbacks(hDevice, &bDataReady, &bError, &bAlert);
kevin1990 16:e4f2689363bb 222 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 223 return res;
kevin1990 16:e4f2689363bb 224
kevin1990 16:e4f2689363bb 225 /*
kevin1990 28:4eb837cd71df 226 * Retrieve the number of samples per cycle, per DATAREADY pulse, etc. for
kevin1990 28:4eb837cd71df 227 * this configuration.
kevin1990 16:e4f2689363bb 228 */
kevin1990 16:e4f2689363bb 229 ADI_SENSE_1000_OPERATING_MODE eOperatingMode;
kevin1990 16:e4f2689363bb 230 ADI_SENSE_1000_DATAREADY_MODE eDataReadyMode;
kevin1990 16:e4f2689363bb 231 uint32_t nSamplesPerDataready;
kevin1990 16:e4f2689363bb 232 uint32_t nSamplesPerCycle;
kevin1990 28:4eb837cd71df 233 uint8_t nBytesPerSample;
kevin1990 16:e4f2689363bb 234 res = adi_sense_1000_GetDataReadyModeInfo(hDevice,
kevin1990 16:e4f2689363bb 235 eMeasurementMode,
kevin1990 16:e4f2689363bb 236 &eOperatingMode,
kevin1990 16:e4f2689363bb 237 &eDataReadyMode,
kevin1990 16:e4f2689363bb 238 &nSamplesPerDataready,
kevin1990 28:4eb837cd71df 239 &nSamplesPerCycle,
kevin1990 28:4eb837cd71df 240 &nBytesPerSample);
kevin1990 16:e4f2689363bb 241 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 242 return res;
kevin1990 16:e4f2689363bb 243
kevin1990 16:e4f2689363bb 244 /*
kevin1990 16:e4f2689363bb 245 * Allocate a buffer to store the samples retrieved on each DATAREADY pulse
kevin1990 16:e4f2689363bb 246 */
kevin1990 16:e4f2689363bb 247 ADI_SENSE_DATA_SAMPLE *pSampleBuffer;
kevin1990 28:4eb837cd71df 248 pSampleBuffer = malloc(sizeof(ADI_SENSE_DATA_SAMPLE) *
kevin1990 28:4eb837cd71df 249 nSamplesPerDataready);
kevin1990 16:e4f2689363bb 250 if (pSampleBuffer == NULL)
kevin1990 16:e4f2689363bb 251 {
kevin1990 16:e4f2689363bb 252 ADI_SENSE_LOG_ERROR("Failed to allocate sample buffer");
kevin1990 16:e4f2689363bb 253 return ADI_SENSE_NO_MEM;
kevin1990 16:e4f2689363bb 254 }
kevin1990 16:e4f2689363bb 255
kevin1990 16:e4f2689363bb 256 /*
kevin1990 16:e4f2689363bb 257 * Kick off the measurement cycle(s) here
kevin1990 16:e4f2689363bb 258 */
kevin1990 16:e4f2689363bb 259 res = adi_sense_StartMeasurement(hDevice, eMeasurementMode);
kevin1990 16:e4f2689363bb 260 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 261 {
kevin1990 16:e4f2689363bb 262 ADI_SENSE_LOG_ERROR("Failed to start measurement");
kevin1990 16:e4f2689363bb 263 return res;
kevin1990 16:e4f2689363bb 264 }
kevin1990 16:e4f2689363bb 265
kevin1990 16:e4f2689363bb 266 /*
kevin1990 16:e4f2689363bb 267 * Loop continuously unless operating mode is single-cycle
kevin1990 16:e4f2689363bb 268 */
kevin1990 28:4eb837cd71df 269 uint32_t nSampleCount = 0;
kevin1990 28:4eb837cd71df 270 while (true)
kevin1990 28:4eb837cd71df 271 {
kevin1990 16:e4f2689363bb 272 ADI_SENSE_STATUS status;
kevin1990 16:e4f2689363bb 273 uint32_t nReturned;
kevin1990 16:e4f2689363bb 274
kevin1990 16:e4f2689363bb 275 /*
kevin1990 28:4eb837cd71df 276 * Wait until the next batch of 1 or more samples is ready, continuously
kevin1990 28:4eb837cd71df 277 * checking DATAREADY until it is asserted
kevin1990 16:e4f2689363bb 278 */
kevin1990 28:4eb837cd71df 279 while (! (bDataReady || bError))
kevin1990 28:4eb837cd71df 280 ;
kevin1990 28:4eb837cd71df 281
kevin1990 28:4eb837cd71df 282 if (!bError)
kevin1990 28:4eb837cd71df 283 {
kevin1990 16:e4f2689363bb 284 /*
kevin1990 28:4eb837cd71df 285 * Get data samples from the measurement cycle, if no error has occurred
kevin1990 16:e4f2689363bb 286 */
kevin1990 28:4eb837cd71df 287 bDataReady = false;
kevin1990 28:4eb837cd71df 288 res = adi_sense_GetData(hDevice, eMeasurementMode, pSampleBuffer,
kevin1990 28:4eb837cd71df 289 nBytesPerSample, nSamplesPerDataready,
kevin1990 28:4eb837cd71df 290 &nReturned);
kevin1990 28:4eb837cd71df 291 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 292 {
kevin1990 28:4eb837cd71df 293 if (res == ADI_SENSE_INCOMPLETE)
kevin1990 16:e4f2689363bb 294 {
kevin1990 28:4eb837cd71df 295 /*
kevin1990 28:4eb837cd71df 296 * This is expected in cases where cycleSkipCount may
kevin1990 28:4eb837cd71df 297 * be non-zero for some channels, resulting in
kevin1990 28:4eb837cd71df 298 * variable-length sequences
kevin1990 28:4eb837cd71df 299 */
kevin1990 28:4eb837cd71df 300 ADI_SENSE_LOG_DEBUG("Retrieved %u of %u requested data samples",
kevin1990 28:4eb837cd71df 301 nReturned, nSamplesPerDataready);
kevin1990 28:4eb837cd71df 302 }
kevin1990 28:4eb837cd71df 303 else
kevin1990 28:4eb837cd71df 304 {
kevin1990 28:4eb837cd71df 305 ADI_SENSE_LOG_WARN("Failed to get data samples from device");
kevin1990 28:4eb837cd71df 306 return res;
kevin1990 16:e4f2689363bb 307 }
kevin1990 16:e4f2689363bb 308 }
kevin1990 16:e4f2689363bb 309
kevin1990 28:4eb837cd71df 310 /*
kevin1990 28:4eb837cd71df 311 * Display the data samples.
kevin1990 28:4eb837cd71df 312 *
kevin1990 28:4eb837cd71df 313 * NOTE: this requires a sufficient idle time between subsequent
kevin1990 28:4eb837cd71df 314 * DATAREADY pulses to allow printing to occur. Otherwise,
kevin1990 28:4eb837cd71df 315 * subsequent samples may be missed if not retrieved promptly when
kevin1990 28:4eb837cd71df 316 * the next DATAREADY assertion occurs.
kevin1990 28:4eb837cd71df 317 */
kevin1990 28:4eb837cd71df 318 utils_printSamples(pSampleBuffer, nReturned, eMeasurementMode);
kevin1990 28:4eb837cd71df 319 nSampleCount += nReturned;
kevin1990 28:4eb837cd71df 320 }
kevin1990 16:e4f2689363bb 321
kevin1990 16:e4f2689363bb 322 /*
kevin1990 16:e4f2689363bb 323 * Check and print device status if errors/alerts have been triggered
kevin1990 16:e4f2689363bb 324 */
kevin1990 16:e4f2689363bb 325 if (bError || bAlert)
kevin1990 16:e4f2689363bb 326 {
kevin1990 16:e4f2689363bb 327 res = adi_sense_GetStatus(hDevice, &status);
kevin1990 16:e4f2689363bb 328 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 329 {
kevin1990 16:e4f2689363bb 330 ADI_SENSE_LOG_ERROR("Failed to retrieve device status");
kevin1990 16:e4f2689363bb 331 return res;
kevin1990 16:e4f2689363bb 332 }
kevin1990 16:e4f2689363bb 333
kevin1990 16:e4f2689363bb 334 if (status.deviceStatus &
kevin1990 16:e4f2689363bb 335 (ADI_SENSE_DEVICE_STATUS_ERROR | ADI_SENSE_DEVICE_STATUS_ALERT))
kevin1990 16:e4f2689363bb 336 {
kevin1990 16:e4f2689363bb 337 utils_printStatus(&status);
kevin1990 16:e4f2689363bb 338
kevin1990 16:e4f2689363bb 339 /* Break out of the loop if any errors are raised */
kevin1990 16:e4f2689363bb 340 if (bError)
kevin1990 16:e4f2689363bb 341 break;
kevin1990 16:e4f2689363bb 342 }
kevin1990 16:e4f2689363bb 343 }
kevin1990 28:4eb837cd71df 344
kevin1990 28:4eb837cd71df 345 if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE)
kevin1990 28:4eb837cd71df 346 {
kevin1990 28:4eb837cd71df 347 /*
kevin1990 28:4eb837cd71df 348 * In this mode, break out of the loop when the measurement command
kevin1990 28:4eb837cd71df 349 * has completed.
kevin1990 28:4eb837cd71df 350 *
kevin1990 28:4eb837cd71df 351 * One option is to check for the expected number of samples to be
kevin1990 28:4eb837cd71df 352 * returned for the cycle. However, cycles may have variable-length
kevin1990 28:4eb837cd71df 353 * sequences if the cycleSkipCount option is non-zero for any of the
kevin1990 28:4eb837cd71df 354 * channels.
kevin1990 28:4eb837cd71df 355 *
kevin1990 28:4eb837cd71df 356 * So, instead, we check for the command-running status, which
kevin1990 28:4eb837cd71df 357 * will de-assert in this mode when the measurement command has
kevin1990 28:4eb837cd71df 358 * completed a single cycle.
kevin1990 28:4eb837cd71df 359 */
kevin1990 28:4eb837cd71df 360 bool_t bCommandRunning;
kevin1990 28:4eb837cd71df 361 res = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning);
kevin1990 28:4eb837cd71df 362 if (res != ADI_SENSE_SUCCESS)
kevin1990 28:4eb837cd71df 363 {
kevin1990 28:4eb837cd71df 364 ADI_SENSE_LOG_ERROR("Failed to get command-running status");
kevin1990 28:4eb837cd71df 365 return res;
kevin1990 28:4eb837cd71df 366 }
kevin1990 28:4eb837cd71df 367
kevin1990 28:4eb837cd71df 368 if (!bCommandRunning && !bDataReady)
kevin1990 28:4eb837cd71df 369 break;
kevin1990 28:4eb837cd71df 370 }
kevin1990 28:4eb837cd71df 371 }
kevin1990 16:e4f2689363bb 372
kevin1990 16:e4f2689363bb 373 res = adi_sense_StopMeasurement(hDevice);
kevin1990 16:e4f2689363bb 374 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 375 {
kevin1990 16:e4f2689363bb 376 ADI_SENSE_LOG_ERROR("Failed to send stop measurement");
kevin1990 16:e4f2689363bb 377 return res;
kevin1990 16:e4f2689363bb 378 }
kevin1990 16:e4f2689363bb 379
kevin1990 16:e4f2689363bb 380 free(pSampleBuffer);
kevin1990 16:e4f2689363bb 381
kevin1990 16:e4f2689363bb 382 res = utils_deregisterCallbacks(hDevice);
kevin1990 16:e4f2689363bb 383 if (res != ADI_SENSE_SUCCESS)
kevin1990 16:e4f2689363bb 384 return res;
kevin1990 16:e4f2689363bb 385
kevin1990 16:e4f2689363bb 386 return ADI_SENSE_SUCCESS;
kevin1990 16:e4f2689363bb 387 }