Ross O'Halloran / Mbed OS AdiSense1000_SmartBabySeat

Fork of AdiSense1000_SmartBabySeat by SDMP_IOT

Committer:
giancarloDotta
Date:
Wed Jul 18 19:01:56 2018 +0000
Branch:
gd_dbg2142
Revision:
32:119f86d3d9ed
Parent:
31:de49744b57a6
Testing for bug #2142 unexpected wdt rst

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