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