Vybhav Kadaba
/
EV-PRO-MW1001_Development_20April
Code clean up and handling FIFO errors
common/utils.c@5:0728bde67bdb, 2019-06-05 (annotated)
- Committer:
- Vkadaba
- Date:
- Wed Jun 05 05:39:15 2019 +0000
- Revision:
- 5:0728bde67bdb
- Parent:
- 2:e391aa125721
- Child:
- 6:9d393a9677f4
Replaced all references to ADISense/ADISENSE1000/adi_sense with ADMW/ADMW1001/admw and the prject builds.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ADIJake | 0:85855ecd3257 | 1 | #include <stdlib.h> |
ADIJake | 0:85855ecd3257 | 2 | |
ADIJake | 0:85855ecd3257 | 3 | #include "utils.h" |
Vkadaba | 5:0728bde67bdb | 4 | #include "admw_log.h" |
ADIJake | 0:85855ecd3257 | 5 | |
ADIJake | 0:85855ecd3257 | 6 | void utils_printStatus( |
Vkadaba | 5:0728bde67bdb | 7 | ADMW_STATUS *pStatus) |
ADIJake | 0:85855ecd3257 | 8 | { |
Vkadaba | 5:0728bde67bdb | 9 | ADMW_LOG_INFO("Status Summary:"); |
ADIJake | 0:85855ecd3257 | 10 | |
ADIJake | 0:85855ecd3257 | 11 | if (pStatus->deviceStatus == 0) |
ADIJake | 0:85855ecd3257 | 12 | { |
Vkadaba | 5:0728bde67bdb | 13 | ADMW_LOG_INFO("\tNo errors detected"); |
ADIJake | 0:85855ecd3257 | 14 | } |
ADIJake | 0:85855ecd3257 | 15 | else |
ADIJake | 0:85855ecd3257 | 16 | { |
Vkadaba | 5:0728bde67bdb | 17 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_BUSY) |
Vkadaba | 5:0728bde67bdb | 18 | ADMW_LOG_INFO("\tCommand running"); |
Vkadaba | 5:0728bde67bdb | 19 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_DATAREADY) |
Vkadaba | 5:0728bde67bdb | 20 | ADMW_LOG_INFO("\tData ready"); |
Vkadaba | 5:0728bde67bdb | 21 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) |
Vkadaba | 5:0728bde67bdb | 22 | ADMW_LOG_INFO("\tActive Errors - RESET REQUIRED"); |
Vkadaba | 5:0728bde67bdb | 23 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_FIFO_ERROR) |
Vkadaba | 5:0728bde67bdb | 24 | ADMW_LOG_INFO("\tActive FIFO Errors - ATTENTION REQUIRED"); |
Vkadaba | 5:0728bde67bdb | 25 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_CONFIG_ERROR) |
Vkadaba | 5:0728bde67bdb | 26 | ADMW_LOG_INFO("\tActive Configuration Errors - ATTENTION REQUIRED"); |
Vkadaba | 5:0728bde67bdb | 27 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_LUT_ERROR) |
Vkadaba | 5:0728bde67bdb | 28 | ADMW_LOG_INFO("\tActive Look-Up Table Errors - ATTENTION REQUIRED"); |
Vkadaba | 5:0728bde67bdb | 29 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_EXT_FLASH_ERROR) |
Vkadaba | 5:0728bde67bdb | 30 | ADMW_LOG_INFO("\tActive External Flash Errors - ATTENTION REQUIRED"); |
ADIJake | 0:85855ecd3257 | 31 | |
Vkadaba | 5:0728bde67bdb | 32 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) |
ADIJake | 0:85855ecd3257 | 33 | { |
Vkadaba | 5:0728bde67bdb | 34 | ADMW_LOG_INFO("\tActive Errors - ATTENTION REQUIRED"); |
Vkadaba | 5:0728bde67bdb | 35 | ADMW_LOG_INFO("\t\tLast Error Code: %u (0x%X)", |
ADIJake | 0:85855ecd3257 | 36 | pStatus->errorCode, pStatus->errorCode); |
ADIJake | 0:85855ecd3257 | 37 | |
ADIJake | 0:85855ecd3257 | 38 | if (pStatus->diagnosticsStatus == 0) |
ADIJake | 0:85855ecd3257 | 39 | { |
Vkadaba | 5:0728bde67bdb | 40 | ADMW_LOG_INFO("\t\tNo diagnostics faults detected"); |
ADIJake | 0:85855ecd3257 | 41 | } |
ADIJake | 0:85855ecd3257 | 42 | else |
ADIJake | 0:85855ecd3257 | 43 | { |
Vkadaba | 5:0728bde67bdb | 44 | ADMW_LOG_INFO("\t\tActive diagnostics faults:"); |
ADIJake | 0:85855ecd3257 | 45 | |
Vkadaba | 5:0728bde67bdb | 46 | if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CHECKSUM_ERROR) |
Vkadaba | 5:0728bde67bdb | 47 | ADMW_LOG_INFO("\t\t\tInternal Checksum fault detected"); |
Vkadaba | 5:0728bde67bdb | 48 | if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_COMMS_ERROR) |
Vkadaba | 5:0728bde67bdb | 49 | ADMW_LOG_INFO("\t\t\tInternal Communications fault detected"); |
Vkadaba | 5:0728bde67bdb | 50 | if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_SUPPLY_MONITOR_ERROR) |
Vkadaba | 5:0728bde67bdb | 51 | ADMW_LOG_INFO("\t\t\tSupply Monitor fault detected"); |
Vkadaba | 5:0728bde67bdb | 52 | if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_SUPPLY_CAP_ERROR) |
Vkadaba | 5:0728bde67bdb | 53 | ADMW_LOG_INFO("\t\t\tSupply Regulator Capacitor fault detected"); |
Vkadaba | 5:0728bde67bdb | 54 | if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CONVERSION_ERROR) |
Vkadaba | 5:0728bde67bdb | 55 | ADMW_LOG_INFO("\t\t\tInternal ADC Conversions fault detected"); |
Vkadaba | 5:0728bde67bdb | 56 | if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CALIBRATION_ERROR) |
Vkadaba | 5:0728bde67bdb | 57 | ADMW_LOG_INFO("\t\t\tInternal Device Calibrations fault detected"); |
ADIJake | 0:85855ecd3257 | 58 | } |
ADIJake | 0:85855ecd3257 | 59 | } |
ADIJake | 0:85855ecd3257 | 60 | |
Vkadaba | 5:0728bde67bdb | 61 | if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT) |
ADIJake | 0:85855ecd3257 | 62 | { |
Vkadaba | 5:0728bde67bdb | 63 | ADMW_LOG_INFO("\tActive Alerts - ATTENTION REQUIRED:"); |
Vkadaba | 5:0728bde67bdb | 64 | ADMW_LOG_INFO("\t\tLast Alert Code: %u (0x%X)", |
ADIJake | 0:85855ecd3257 | 65 | pStatus->alertCode, pStatus->alertCode); |
ADIJake | 0:85855ecd3257 | 66 | |
Vkadaba | 5:0728bde67bdb | 67 | for (unsigned i = 0; i < ADMW1001_MAX_CHANNELS; i++) |
ADIJake | 0:85855ecd3257 | 68 | { |
ADIJake | 0:85855ecd3257 | 69 | if (pStatus->channelAlerts[i] == 0) |
ADIJake | 0:85855ecd3257 | 70 | continue; |
ADIJake | 0:85855ecd3257 | 71 | |
Vkadaba | 5:0728bde67bdb | 72 | ADMW_LOG_INFO("\t\tChannel #%u:", i); |
Vkadaba | 5:0728bde67bdb | 73 | ADMW_LOG_INFO("\t\t\tLast Alert Code: %u (0x%X)", |
ADIJake | 0:85855ecd3257 | 74 | pStatus->channelAlertCodes[i], |
ADIJake | 0:85855ecd3257 | 75 | pStatus->channelAlertCodes[i]); |
Vkadaba | 5:0728bde67bdb | 76 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_TIMEOUT) |
Vkadaba | 5:0728bde67bdb | 77 | ADMW_LOG_INFO("\t\t\tTimeout alert detected"); |
Vkadaba | 5:0728bde67bdb | 78 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_UNDER_RANGE) |
Vkadaba | 5:0728bde67bdb | 79 | ADMW_LOG_INFO("\t\t\tUnder Range alert detected"); |
Vkadaba | 5:0728bde67bdb | 80 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_OVER_RANGE) |
Vkadaba | 5:0728bde67bdb | 81 | ADMW_LOG_INFO("\t\t\tOver Range alert detected"); |
Vkadaba | 5:0728bde67bdb | 82 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LOW_LIMIT) |
Vkadaba | 5:0728bde67bdb | 83 | ADMW_LOG_INFO("\t\t\tLow limit alert detected"); |
Vkadaba | 5:0728bde67bdb | 84 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_HIGH_LIMIT) |
Vkadaba | 5:0728bde67bdb | 85 | ADMW_LOG_INFO("\t\t\tHigh Limit alert detected"); |
Vkadaba | 5:0728bde67bdb | 86 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_SENSOR_OPEN) |
Vkadaba | 5:0728bde67bdb | 87 | ADMW_LOG_INFO("\t\t\tSensor Fault alert detected"); |
Vkadaba | 5:0728bde67bdb | 88 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_REF_DETECT) |
Vkadaba | 5:0728bde67bdb | 89 | ADMW_LOG_INFO("\t\t\tReference Detection alert detected"); |
Vkadaba | 5:0728bde67bdb | 90 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_CONFIG_ERR) |
Vkadaba | 5:0728bde67bdb | 91 | ADMW_LOG_INFO("\t\t\tConfiguration Error alert detected"); |
Vkadaba | 5:0728bde67bdb | 92 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LUT_ERR) |
Vkadaba | 5:0728bde67bdb | 93 | ADMW_LOG_INFO("\t\t\tLook-Up Table Error alert detected"); |
Vkadaba | 5:0728bde67bdb | 94 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_SENSOR_NOT_READY) |
Vkadaba | 5:0728bde67bdb | 95 | ADMW_LOG_INFO("\t\t\tSensor Not Ready alert detected"); |
Vkadaba | 5:0728bde67bdb | 96 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_COMP_NOT_READY) |
Vkadaba | 5:0728bde67bdb | 97 | ADMW_LOG_INFO("\t\t\tCompensation Channel Not Ready alert detected"); |
Vkadaba | 5:0728bde67bdb | 98 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LUT_UNDER_RANGE) |
Vkadaba | 5:0728bde67bdb | 99 | ADMW_LOG_INFO("\t\t\tUnder Look-Up Table Range alert detected"); |
Vkadaba | 5:0728bde67bdb | 100 | if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LUT_OVER_RANGE) |
Vkadaba | 5:0728bde67bdb | 101 | ADMW_LOG_INFO("\t\t\tOver Look-Up Table Range alert detected"); |
ADIJake | 0:85855ecd3257 | 102 | } |
ADIJake | 0:85855ecd3257 | 103 | } |
ADIJake | 0:85855ecd3257 | 104 | |
Vkadaba | 5:0728bde67bdb | 105 | if ((pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) || |
Vkadaba | 5:0728bde67bdb | 106 | (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT)) |
ADIJake | 0:85855ecd3257 | 107 | { |
Vkadaba | 5:0728bde67bdb | 108 | ADMW_LOG_INFO("\t\tLast Debug Code: %u-%u", |
ADIJake | 0:85855ecd3257 | 109 | (pStatus->debugCode >> 16) & 0xFFFF, |
ADIJake | 0:85855ecd3257 | 110 | (pStatus->debugCode >> 0) & 0xFFFF); |
ADIJake | 0:85855ecd3257 | 111 | } |
ADIJake | 0:85855ecd3257 | 112 | } |
ADIJake | 0:85855ecd3257 | 113 | } |
ADIJake | 0:85855ecd3257 | 114 | |
ADIJake | 0:85855ecd3257 | 115 | void utils_printSamples( |
Vkadaba | 5:0728bde67bdb | 116 | ADMW_DATA_SAMPLE *pSampleBuffer, |
ADIJake | 0:85855ecd3257 | 117 | uint32_t nNumSamples, |
Vkadaba | 5:0728bde67bdb | 118 | ADMW_MEASUREMENT_MODE eMeasurementMode) |
ADIJake | 0:85855ecd3257 | 119 | { |
Vkadaba | 5:0728bde67bdb | 120 | bool fftMode = (eMeasurementMode == ADMW_MEASUREMENT_MODE_FFT); |
ADIJake | 0:85855ecd3257 | 121 | |
ADIJake | 0:85855ecd3257 | 122 | for (uint32_t i = 0; i < nNumSamples; i++) |
ADIJake | 0:85855ecd3257 | 123 | { |
ADIJake | 0:85855ecd3257 | 124 | if (fftMode) |
ADIJake | 0:85855ecd3257 | 125 | { |
Vkadaba | 5:0728bde67bdb | 126 | ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Bin/Raw %8d :: Magnitude %e :: flags:%s%s", |
ADIJake | 0:85855ecd3257 | 127 | i+1, |
ADIJake | 0:85855ecd3257 | 128 | pSampleBuffer[i].channelId, |
ADIJake | 0:85855ecd3257 | 129 | pSampleBuffer[i].rawValue, |
ADIJake | 0:85855ecd3257 | 130 | pSampleBuffer[i].processedValue, |
Vkadaba | 5:0728bde67bdb | 131 | pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR ? " ERROR" : "", |
Vkadaba | 5:0728bde67bdb | 132 | pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT ? " ALERT" : ""); |
ADIJake | 0:85855ecd3257 | 133 | } |
ADIJake | 0:85855ecd3257 | 134 | else |
ADIJake | 0:85855ecd3257 | 135 | { |
Vkadaba | 5:0728bde67bdb | 136 | ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Raw %0X %f :: Processed %f :: flags:%s%s", |
ADIJake | 0:85855ecd3257 | 137 | i+1, |
ADIJake | 0:85855ecd3257 | 138 | pSampleBuffer[i].channelId, |
abrophy | 2:e391aa125721 | 139 | pSampleBuffer[i].rawValue, (pSampleBuffer[i].rawValue/1024.0), |
ADIJake | 0:85855ecd3257 | 140 | pSampleBuffer[i].processedValue, |
Vkadaba | 5:0728bde67bdb | 141 | pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR ? " ERROR" : "", |
Vkadaba | 5:0728bde67bdb | 142 | pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT ? " ALERT" : ""); |
ADIJake | 0:85855ecd3257 | 143 | } |
ADIJake | 0:85855ecd3257 | 144 | } |
ADIJake | 0:85855ecd3257 | 145 | } |
ADIJake | 0:85855ecd3257 | 146 | |
Vkadaba | 5:0728bde67bdb | 147 | static void gpioCallbackFn(ADMW_GPIO_PIN ePinId, void * pArg) |
ADIJake | 0:85855ecd3257 | 148 | { |
ADIJake | 0:85855ecd3257 | 149 | volatile bool *pbFlag = (volatile bool *)pArg; |
ADIJake | 0:85855ecd3257 | 150 | *pbFlag = true; |
ADIJake | 0:85855ecd3257 | 151 | } |
ADIJake | 0:85855ecd3257 | 152 | |
Vkadaba | 5:0728bde67bdb | 153 | ADMW_RESULT utils_registerCallbacks( |
Vkadaba | 5:0728bde67bdb | 154 | ADMW_DEVICE_HANDLE hDevice, |
ADIJake | 0:85855ecd3257 | 155 | volatile bool *pbDataReady, |
ADIJake | 0:85855ecd3257 | 156 | volatile bool *pbError, |
ADIJake | 0:85855ecd3257 | 157 | volatile bool *pbAlert) |
ADIJake | 0:85855ecd3257 | 158 | { |
Vkadaba | 5:0728bde67bdb | 159 | ADMW_RESULT res; |
ADIJake | 0:85855ecd3257 | 160 | bool state; |
ADIJake | 0:85855ecd3257 | 161 | |
Vkadaba | 5:0728bde67bdb | 162 | res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY, |
ADIJake | 0:85855ecd3257 | 163 | gpioCallbackFn, (void *)pbDataReady); |
Vkadaba | 5:0728bde67bdb | 164 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 165 | { |
Vkadaba | 5:0728bde67bdb | 166 | ADMW_LOG_ERROR("Failed to register DATAREADY callback"); |
ADIJake | 0:85855ecd3257 | 167 | return res; |
ADIJake | 0:85855ecd3257 | 168 | } |
ADIJake | 0:85855ecd3257 | 169 | |
Vkadaba | 5:0728bde67bdb | 170 | res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, &state); |
Vkadaba | 5:0728bde67bdb | 171 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 172 | { |
Vkadaba | 5:0728bde67bdb | 173 | ADMW_LOG_ERROR("Failed to get current ERROR state"); |
ADIJake | 0:85855ecd3257 | 174 | return res; |
ADIJake | 0:85855ecd3257 | 175 | } |
ADIJake | 0:85855ecd3257 | 176 | if (state) |
ADIJake | 0:85855ecd3257 | 177 | { |
Vkadaba | 5:0728bde67bdb | 178 | ADMW_LOG_ERROR("ERROR signal already asserted"); |
Vkadaba | 5:0728bde67bdb | 179 | return ADMW_FAILURE; |
ADIJake | 0:85855ecd3257 | 180 | } |
Vkadaba | 5:0728bde67bdb | 181 | res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, |
ADIJake | 0:85855ecd3257 | 182 | gpioCallbackFn, (void *)pbError); |
Vkadaba | 5:0728bde67bdb | 183 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 184 | { |
Vkadaba | 5:0728bde67bdb | 185 | ADMW_LOG_ERROR("Failed to register ERROR callback"); |
ADIJake | 0:85855ecd3257 | 186 | return res; |
ADIJake | 0:85855ecd3257 | 187 | } |
ADIJake | 0:85855ecd3257 | 188 | |
Vkadaba | 5:0728bde67bdb | 189 | /*res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT, &state); |
Vkadaba | 5:0728bde67bdb | 190 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 191 | { |
Vkadaba | 5:0728bde67bdb | 192 | ADMW_LOG_ERROR("Failed to get current ALERT state"); |
ADIJake | 0:85855ecd3257 | 193 | return res; |
ADIJake | 0:85855ecd3257 | 194 | } |
ADIJake | 0:85855ecd3257 | 195 | if (state) |
ADIJake | 0:85855ecd3257 | 196 | { |
Vkadaba | 5:0728bde67bdb | 197 | ADMW_LOG_ERROR("ALERT signal already asserted"); |
Vkadaba | 5:0728bde67bdb | 198 | return ADMW_FAILURE; |
ADIJake | 1:63dd03580de4 | 199 | }*/ |
Vkadaba | 5:0728bde67bdb | 200 | res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, |
ADIJake | 0:85855ecd3257 | 201 | gpioCallbackFn, (void *)pbAlert); |
Vkadaba | 5:0728bde67bdb | 202 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 203 | { |
Vkadaba | 5:0728bde67bdb | 204 | ADMW_LOG_ERROR("Failed to register ALERT callback"); |
ADIJake | 0:85855ecd3257 | 205 | return res; |
ADIJake | 0:85855ecd3257 | 206 | } |
ADIJake | 0:85855ecd3257 | 207 | |
Vkadaba | 5:0728bde67bdb | 208 | return ADMW_SUCCESS; |
ADIJake | 0:85855ecd3257 | 209 | } |
ADIJake | 0:85855ecd3257 | 210 | |
Vkadaba | 5:0728bde67bdb | 211 | ADMW_RESULT utils_deregisterCallbacks( |
Vkadaba | 5:0728bde67bdb | 212 | ADMW_DEVICE_HANDLE hDevice) |
ADIJake | 0:85855ecd3257 | 213 | { |
Vkadaba | 5:0728bde67bdb | 214 | ADMW_RESULT res; |
ADIJake | 0:85855ecd3257 | 215 | |
Vkadaba | 5:0728bde67bdb | 216 | res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY, |
ADIJake | 0:85855ecd3257 | 217 | NULL, NULL); |
Vkadaba | 5:0728bde67bdb | 218 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 219 | { |
Vkadaba | 5:0728bde67bdb | 220 | ADMW_LOG_ERROR("Failed to deregister DATAREADY callback"); |
ADIJake | 0:85855ecd3257 | 221 | return res; |
ADIJake | 0:85855ecd3257 | 222 | } |
ADIJake | 0:85855ecd3257 | 223 | |
Vkadaba | 5:0728bde67bdb | 224 | res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, |
ADIJake | 0:85855ecd3257 | 225 | NULL, NULL); |
Vkadaba | 5:0728bde67bdb | 226 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 227 | { |
Vkadaba | 5:0728bde67bdb | 228 | ADMW_LOG_ERROR("Failed to deregister ERROR callback"); |
ADIJake | 0:85855ecd3257 | 229 | return res; |
ADIJake | 0:85855ecd3257 | 230 | } |
ADIJake | 0:85855ecd3257 | 231 | |
Vkadaba | 5:0728bde67bdb | 232 | res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, |
ADIJake | 0:85855ecd3257 | 233 | NULL, NULL); |
Vkadaba | 5:0728bde67bdb | 234 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 235 | { |
Vkadaba | 5:0728bde67bdb | 236 | ADMW_LOG_INFO("Failed to deregister ALERT callback"); |
ADIJake | 0:85855ecd3257 | 237 | return res; |
ADIJake | 0:85855ecd3257 | 238 | } |
ADIJake | 0:85855ecd3257 | 239 | |
Vkadaba | 5:0728bde67bdb | 240 | return ADMW_SUCCESS; |
ADIJake | 0:85855ecd3257 | 241 | } |
ADIJake | 0:85855ecd3257 | 242 | |
Vkadaba | 5:0728bde67bdb | 243 | ADMW_RESULT utils_runMeasurement( |
Vkadaba | 5:0728bde67bdb | 244 | ADMW_DEVICE_HANDLE hDevice, |
Vkadaba | 5:0728bde67bdb | 245 | ADMW_MEASUREMENT_MODE eMeasurementMode) |
ADIJake | 0:85855ecd3257 | 246 | { |
Vkadaba | 5:0728bde67bdb | 247 | ADMW_RESULT res; |
ADIJake | 0:85855ecd3257 | 248 | |
ADIJake | 0:85855ecd3257 | 249 | volatile bool bDataReady = false; |
ADIJake | 0:85855ecd3257 | 250 | volatile bool bError = false; |
ADIJake | 0:85855ecd3257 | 251 | volatile bool bAlert = false; |
ADIJake | 0:85855ecd3257 | 252 | res = utils_registerCallbacks(hDevice, &bDataReady, &bError, &bAlert); |
Vkadaba | 5:0728bde67bdb | 253 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 254 | return res; |
ADIJake | 0:85855ecd3257 | 255 | |
ADIJake | 0:85855ecd3257 | 256 | /* |
ADIJake | 0:85855ecd3257 | 257 | * Retrieve the number of samples per cycle, per DATAREADY pulse, etc. for |
ADIJake | 0:85855ecd3257 | 258 | * this configuration. |
ADIJake | 0:85855ecd3257 | 259 | */ |
Vkadaba | 5:0728bde67bdb | 260 | ADMW1001_OPERATING_MODE eOperatingMode; |
Vkadaba | 5:0728bde67bdb | 261 | ADMW1001_DATAREADY_MODE eDataReadyMode; |
ADIJake | 0:85855ecd3257 | 262 | uint32_t nSamplesPerDataready; |
ADIJake | 0:85855ecd3257 | 263 | uint32_t nSamplesPerCycle; |
ADIJake | 0:85855ecd3257 | 264 | uint8_t nBytesPerSample; |
Vkadaba | 5:0728bde67bdb | 265 | res = admw1001_GetDataReadyModeInfo(hDevice, |
ADIJake | 0:85855ecd3257 | 266 | eMeasurementMode, |
ADIJake | 0:85855ecd3257 | 267 | &eOperatingMode, |
ADIJake | 0:85855ecd3257 | 268 | &eDataReadyMode, |
ADIJake | 0:85855ecd3257 | 269 | &nSamplesPerDataready, |
ADIJake | 0:85855ecd3257 | 270 | &nSamplesPerCycle, |
ADIJake | 0:85855ecd3257 | 271 | &nBytesPerSample); |
Vkadaba | 5:0728bde67bdb | 272 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 273 | return res; |
ADIJake | 0:85855ecd3257 | 274 | |
ADIJake | 0:85855ecd3257 | 275 | /* |
ADIJake | 0:85855ecd3257 | 276 | * Allocate a buffer to store the samples retrieved on each DATAREADY pulse |
ADIJake | 0:85855ecd3257 | 277 | */ |
Vkadaba | 5:0728bde67bdb | 278 | ADMW_DATA_SAMPLE *pSampleBuffer; |
Vkadaba | 5:0728bde67bdb | 279 | pSampleBuffer = malloc(sizeof(ADMW_DATA_SAMPLE) * |
ADIJake | 0:85855ecd3257 | 280 | nSamplesPerDataready); |
ADIJake | 0:85855ecd3257 | 281 | if (pSampleBuffer == NULL) |
ADIJake | 0:85855ecd3257 | 282 | { |
Vkadaba | 5:0728bde67bdb | 283 | ADMW_LOG_ERROR("Failed to allocate sample buffer"); |
Vkadaba | 5:0728bde67bdb | 284 | return ADMW_NO_MEM; |
ADIJake | 0:85855ecd3257 | 285 | } |
ADIJake | 0:85855ecd3257 | 286 | |
ADIJake | 0:85855ecd3257 | 287 | /* |
ADIJake | 0:85855ecd3257 | 288 | * Kick off the measurement cycle(s) here |
ADIJake | 0:85855ecd3257 | 289 | */ |
Vkadaba | 5:0728bde67bdb | 290 | ADMW_LOG_INFO("Starting measurement"); |
Vkadaba | 5:0728bde67bdb | 291 | res = admw_StartMeasurement(hDevice, eMeasurementMode); |
Vkadaba | 5:0728bde67bdb | 292 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 293 | { |
Vkadaba | 5:0728bde67bdb | 294 | ADMW_LOG_ERROR("Failed to start measurement"); |
ADIJake | 0:85855ecd3257 | 295 | return res; |
ADIJake | 0:85855ecd3257 | 296 | } |
ADIJake | 0:85855ecd3257 | 297 | |
ADIJake | 0:85855ecd3257 | 298 | /* |
ADIJake | 0:85855ecd3257 | 299 | * Loop continuously unless operating mode is single-cycle |
ADIJake | 0:85855ecd3257 | 300 | */ |
ADIJake | 0:85855ecd3257 | 301 | uint32_t nSampleCount = 0; |
ADIJake | 0:85855ecd3257 | 302 | uint32_t nReturned; |
ADIJake | 0:85855ecd3257 | 303 | while (true) |
ADIJake | 0:85855ecd3257 | 304 | { |
Vkadaba | 5:0728bde67bdb | 305 | ADMW_STATUS status; |
ADIJake | 0:85855ecd3257 | 306 | |
ADIJake | 0:85855ecd3257 | 307 | /* |
ADIJake | 0:85855ecd3257 | 308 | * Wait until the next batch of 1 or more samples is ready, continuously |
ADIJake | 0:85855ecd3257 | 309 | * checking DATAREADY until it is asserted |
ADIJake | 0:85855ecd3257 | 310 | */ |
ADIJake | 0:85855ecd3257 | 311 | while (! (bDataReady || bError)) |
ADIJake | 0:85855ecd3257 | 312 | ; |
ADIJake | 0:85855ecd3257 | 313 | |
ADIJake | 0:85855ecd3257 | 314 | if (!bError) |
ADIJake | 0:85855ecd3257 | 315 | { |
ADIJake | 0:85855ecd3257 | 316 | /* |
ADIJake | 0:85855ecd3257 | 317 | * Get data samples from the measurement cycle, if no error has occurred |
ADIJake | 0:85855ecd3257 | 318 | */ |
ADIJake | 0:85855ecd3257 | 319 | bDataReady = false; |
Vkadaba | 5:0728bde67bdb | 320 | res = admw_GetData(hDevice, eMeasurementMode, pSampleBuffer, |
ADIJake | 0:85855ecd3257 | 321 | nBytesPerSample, nSamplesPerDataready, |
ADIJake | 0:85855ecd3257 | 322 | &nReturned); |
Vkadaba | 5:0728bde67bdb | 323 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 324 | { |
Vkadaba | 5:0728bde67bdb | 325 | if (res == ADMW_INCOMPLETE) |
ADIJake | 0:85855ecd3257 | 326 | { |
ADIJake | 0:85855ecd3257 | 327 | /* |
ADIJake | 0:85855ecd3257 | 328 | * This is expected in cases where cycleSkipCount may |
ADIJake | 0:85855ecd3257 | 329 | * be non-zero for some channels, resulting in |
ADIJake | 0:85855ecd3257 | 330 | * variable-length sequences |
ADIJake | 0:85855ecd3257 | 331 | */ |
Vkadaba | 5:0728bde67bdb | 332 | ADMW_LOG_DEBUG("Retrieved %u of %u requested data samples", |
ADIJake | 0:85855ecd3257 | 333 | nReturned, nSamplesPerDataready); |
ADIJake | 0:85855ecd3257 | 334 | } |
ADIJake | 0:85855ecd3257 | 335 | else |
ADIJake | 0:85855ecd3257 | 336 | { |
Vkadaba | 5:0728bde67bdb | 337 | ADMW_LOG_WARN("Failed to get data samples from device"); |
ADIJake | 0:85855ecd3257 | 338 | return res; |
ADIJake | 0:85855ecd3257 | 339 | } |
ADIJake | 0:85855ecd3257 | 340 | } |
ADIJake | 0:85855ecd3257 | 341 | |
ADIJake | 0:85855ecd3257 | 342 | /* |
ADIJake | 0:85855ecd3257 | 343 | * Display the data samples. |
ADIJake | 0:85855ecd3257 | 344 | * |
ADIJake | 0:85855ecd3257 | 345 | * NOTE: this requires a sufficient idle time between subsequent |
ADIJake | 0:85855ecd3257 | 346 | * DATAREADY pulses to allow printing to occur. Otherwise, |
ADIJake | 0:85855ecd3257 | 347 | * subsequent samples may be missed if not retrieved promptly when |
ADIJake | 0:85855ecd3257 | 348 | * the next DATAREADY assertion occurs. |
ADIJake | 0:85855ecd3257 | 349 | */ |
ADIJake | 0:85855ecd3257 | 350 | utils_printSamples(pSampleBuffer, nReturned, eMeasurementMode); |
ADIJake | 0:85855ecd3257 | 351 | nSampleCount += nReturned; |
ADIJake | 0:85855ecd3257 | 352 | } |
ADIJake | 0:85855ecd3257 | 353 | |
ADIJake | 0:85855ecd3257 | 354 | /* |
ADIJake | 0:85855ecd3257 | 355 | * Check and print device status if errors/alerts have been triggered |
ADIJake | 0:85855ecd3257 | 356 | */ |
ADIJake | 0:85855ecd3257 | 357 | if (bError || bAlert) |
ADIJake | 0:85855ecd3257 | 358 | { |
Vkadaba | 5:0728bde67bdb | 359 | res = admw_GetStatus(hDevice, &status); |
Vkadaba | 5:0728bde67bdb | 360 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 361 | { |
Vkadaba | 5:0728bde67bdb | 362 | ADMW_LOG_ERROR("Failed to retrieve device status"); |
ADIJake | 0:85855ecd3257 | 363 | return res; |
ADIJake | 0:85855ecd3257 | 364 | } |
ADIJake | 0:85855ecd3257 | 365 | |
ADIJake | 0:85855ecd3257 | 366 | if (status.deviceStatus & |
Vkadaba | 5:0728bde67bdb | 367 | (ADMW_DEVICE_STATUS_ERROR | ADMW_DEVICE_STATUS_ALERT)) |
ADIJake | 0:85855ecd3257 | 368 | { |
ADIJake | 0:85855ecd3257 | 369 | utils_printStatus(&status); |
ADIJake | 0:85855ecd3257 | 370 | |
ADIJake | 0:85855ecd3257 | 371 | /* Break out of the loop if any errors are raised */ |
ADIJake | 0:85855ecd3257 | 372 | if (bError) |
ADIJake | 0:85855ecd3257 | 373 | break; |
ADIJake | 0:85855ecd3257 | 374 | } |
ADIJake | 0:85855ecd3257 | 375 | } |
ADIJake | 0:85855ecd3257 | 376 | |
Vkadaba | 5:0728bde67bdb | 377 | if (eOperatingMode == ADMW1001_OPERATING_MODE_SINGLECYCLE) |
ADIJake | 0:85855ecd3257 | 378 | { |
ADIJake | 0:85855ecd3257 | 379 | /* |
ADIJake | 0:85855ecd3257 | 380 | * In this mode, break out of the loop when the measurement command |
ADIJake | 0:85855ecd3257 | 381 | * has completed. |
ADIJake | 0:85855ecd3257 | 382 | * |
ADIJake | 0:85855ecd3257 | 383 | * One option is to check for the expected number of samples to be |
ADIJake | 0:85855ecd3257 | 384 | * returned for the cycle. However, cycles may have variable-length |
ADIJake | 0:85855ecd3257 | 385 | * sequences if the cycleSkipCount option is non-zero for any of the |
ADIJake | 0:85855ecd3257 | 386 | * channels. |
ADIJake | 0:85855ecd3257 | 387 | * |
ADIJake | 0:85855ecd3257 | 388 | * So, instead, we check for the command-running status, which |
ADIJake | 0:85855ecd3257 | 389 | * will de-assert in this mode when the measurement command has |
ADIJake | 0:85855ecd3257 | 390 | * completed a single cycle. |
ADIJake | 0:85855ecd3257 | 391 | */ |
ADIJake | 0:85855ecd3257 | 392 | bool bCommandRunning; |
Vkadaba | 5:0728bde67bdb | 393 | res = admw_GetCommandRunningState(hDevice, &bCommandRunning); |
Vkadaba | 5:0728bde67bdb | 394 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 395 | { |
Vkadaba | 5:0728bde67bdb | 396 | ADMW_LOG_ERROR("Failed to get command-running status"); |
ADIJake | 0:85855ecd3257 | 397 | return res; |
ADIJake | 0:85855ecd3257 | 398 | } |
ADIJake | 0:85855ecd3257 | 399 | |
ADIJake | 0:85855ecd3257 | 400 | if (!bCommandRunning && !bDataReady) |
ADIJake | 0:85855ecd3257 | 401 | break; |
ADIJake | 0:85855ecd3257 | 402 | } |
ADIJake | 0:85855ecd3257 | 403 | } |
ADIJake | 0:85855ecd3257 | 404 | |
Vkadaba | 5:0728bde67bdb | 405 | ADMW_LOG_INFO("Stopping measurement"); |
Vkadaba | 5:0728bde67bdb | 406 | res = admw_StopMeasurement(hDevice); |
Vkadaba | 5:0728bde67bdb | 407 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 408 | { |
Vkadaba | 5:0728bde67bdb | 409 | ADMW_LOG_ERROR("Failed to send stop measurement"); |
ADIJake | 0:85855ecd3257 | 410 | return res; |
ADIJake | 0:85855ecd3257 | 411 | } |
ADIJake | 0:85855ecd3257 | 412 | |
ADIJake | 0:85855ecd3257 | 413 | free(pSampleBuffer); |
ADIJake | 0:85855ecd3257 | 414 | |
ADIJake | 0:85855ecd3257 | 415 | res = utils_deregisterCallbacks(hDevice); |
Vkadaba | 5:0728bde67bdb | 416 | if (res != ADMW_SUCCESS) |
ADIJake | 0:85855ecd3257 | 417 | return res; |
ADIJake | 0:85855ecd3257 | 418 | |
Vkadaba | 5:0728bde67bdb | 419 | return ADMW_SUCCESS; |
ADIJake | 0:85855ecd3257 | 420 | } |
ADIJake | 0:85855ecd3257 | 421 |