ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
kevin1990
Date:
Mon Nov 27 13:10:11 2017 +0000
Revision:
12:97457cf77bcb
v1.0 release

Who changed what in which revision?

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