Code clean up and handling FIFO errors

Committer:
abrophy
Date:
Fri Apr 26 15:57:57 2019 +0000
Revision:
2:e391aa125721
Parent:
1:63dd03580de4
Child:
5:0728bde67bdb
Changed logging of samples to record hex and floating point value for raw sample.

Who changed what in which revision?

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