ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
kevin1990
Date:
Tue Dec 05 19:08:53 2017 +0000
Revision:
16:e4f2689363bb
Child:
27:567abf893938
Child:
28:4eb837cd71df
v1.0_RC2 Release

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