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