initial commit

Committer:
Vkadaba
Date:
Fri Jul 26 08:42:53 2019 +0000
Revision:
15:b5d57daeb7c0
Parent:
10:14954555be2a
Child:
23:bb685f35b08b
Modified the copyright notice

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vkadaba 15:b5d57daeb7c0 1 /*
Vkadaba 15:b5d57daeb7c0 2 Copyright 2019 (c) Analog Devices, Inc.
Vkadaba 15:b5d57daeb7c0 3
Vkadaba 15:b5d57daeb7c0 4 All rights reserved.
Vkadaba 15:b5d57daeb7c0 5
Vkadaba 15:b5d57daeb7c0 6 Redistribution and use in source and binary forms, with or without
Vkadaba 15:b5d57daeb7c0 7 modification, are permitted provided that the following conditions are met:
Vkadaba 15:b5d57daeb7c0 8 - Redistributions of source code must retain the above copyright
Vkadaba 15:b5d57daeb7c0 9 notice, this list of conditions and the following disclaimer.
Vkadaba 15:b5d57daeb7c0 10 - Redistributions in binary form must reproduce the above copyright
Vkadaba 15:b5d57daeb7c0 11 notice, this list of conditions and the following disclaimer in
Vkadaba 15:b5d57daeb7c0 12 the documentation and/or other materials provided with the
Vkadaba 15:b5d57daeb7c0 13 distribution.
Vkadaba 15:b5d57daeb7c0 14 - Neither the name of Analog Devices, Inc. nor the names of its
Vkadaba 15:b5d57daeb7c0 15 contributors may be used to endorse or promote products derived
Vkadaba 15:b5d57daeb7c0 16 from this software without specific prior written permission.
Vkadaba 15:b5d57daeb7c0 17 - The use of this software may or may not infringe the patent rights
Vkadaba 15:b5d57daeb7c0 18 of one or more patent holders. This license does not release you
Vkadaba 15:b5d57daeb7c0 19 from the requirement that you obtain separate licenses from these
Vkadaba 15:b5d57daeb7c0 20 patent holders to use this software.
Vkadaba 15:b5d57daeb7c0 21 - Use of the software either in source or binary form, must be run
Vkadaba 15:b5d57daeb7c0 22 on or directly connected to an Analog Devices Inc. component.
Vkadaba 15:b5d57daeb7c0 23
Vkadaba 15:b5d57daeb7c0 24 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
Vkadaba 15:b5d57daeb7c0 25 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
Vkadaba 15:b5d57daeb7c0 26 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Vkadaba 15:b5d57daeb7c0 27 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
Vkadaba 15:b5d57daeb7c0 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Vkadaba 15:b5d57daeb7c0 29 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
Vkadaba 15:b5d57daeb7c0 30 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Vkadaba 15:b5d57daeb7c0 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Vkadaba 15:b5d57daeb7c0 32 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Vkadaba 15:b5d57daeb7c0 33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Vkadaba 15:b5d57daeb7c0 34 */
Vkadaba 15:b5d57daeb7c0 35
Vkadaba 15:b5d57daeb7c0 36 /*!
Vkadaba 15:b5d57daeb7c0 37 ******************************************************************************
Vkadaba 15:b5d57daeb7c0 38 * @file: utils.c
Vkadaba 15:b5d57daeb7c0 39 * @brief: Interface Utilities for ADMW1001
Vkadaba 15:b5d57daeb7c0 40 *-----------------------------------------------------------------------------
Vkadaba 15:b5d57daeb7c0 41 */
ADIJake 0:85855ecd3257 42 #include <stdlib.h>
ADIJake 0:85855ecd3257 43
ADIJake 0:85855ecd3257 44 #include "utils.h"
Vkadaba 5:0728bde67bdb 45 #include "admw_log.h"
ADIJake 0:85855ecd3257 46
ADIJake 0:85855ecd3257 47 void utils_printStatus(
Vkadaba 5:0728bde67bdb 48 ADMW_STATUS *pStatus)
ADIJake 0:85855ecd3257 49 {
Vkadaba 5:0728bde67bdb 50 ADMW_LOG_INFO("Status Summary:");
ADIJake 0:85855ecd3257 51
ADIJake 0:85855ecd3257 52 if (pStatus->deviceStatus == 0)
ADIJake 0:85855ecd3257 53 {
Vkadaba 5:0728bde67bdb 54 ADMW_LOG_INFO("\tNo errors detected");
ADIJake 0:85855ecd3257 55 }
ADIJake 0:85855ecd3257 56 else
ADIJake 0:85855ecd3257 57 {
Vkadaba 5:0728bde67bdb 58 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_BUSY)
Vkadaba 5:0728bde67bdb 59 ADMW_LOG_INFO("\tCommand running");
Vkadaba 5:0728bde67bdb 60 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_DATAREADY)
Vkadaba 5:0728bde67bdb 61 ADMW_LOG_INFO("\tData ready");
Vkadaba 5:0728bde67bdb 62 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR)
Vkadaba 5:0728bde67bdb 63 ADMW_LOG_INFO("\tActive Errors - RESET REQUIRED");
Vkadaba 5:0728bde67bdb 64 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_FIFO_ERROR)
Vkadaba 5:0728bde67bdb 65 ADMW_LOG_INFO("\tActive FIFO Errors - ATTENTION REQUIRED");
Vkadaba 5:0728bde67bdb 66 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_CONFIG_ERROR)
Vkadaba 5:0728bde67bdb 67 ADMW_LOG_INFO("\tActive Configuration Errors - ATTENTION REQUIRED");
Vkadaba 5:0728bde67bdb 68 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_LUT_ERROR)
Vkadaba 5:0728bde67bdb 69 ADMW_LOG_INFO("\tActive Look-Up Table Errors - ATTENTION REQUIRED");
Vkadaba 6:9d393a9677f4 70
ADIJake 0:85855ecd3257 71
Vkadaba 5:0728bde67bdb 72 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR)
ADIJake 0:85855ecd3257 73 {
Vkadaba 5:0728bde67bdb 74 ADMW_LOG_INFO("\tActive Errors - ATTENTION REQUIRED");
Vkadaba 5:0728bde67bdb 75 ADMW_LOG_INFO("\t\tLast Error Code: %u (0x%X)",
ADIJake 0:85855ecd3257 76 pStatus->errorCode, pStatus->errorCode);
ADIJake 0:85855ecd3257 77
ADIJake 0:85855ecd3257 78 if (pStatus->diagnosticsStatus == 0)
ADIJake 0:85855ecd3257 79 {
Vkadaba 5:0728bde67bdb 80 ADMW_LOG_INFO("\t\tNo diagnostics faults detected");
ADIJake 0:85855ecd3257 81 }
ADIJake 0:85855ecd3257 82 else
ADIJake 0:85855ecd3257 83 {
Vkadaba 5:0728bde67bdb 84 ADMW_LOG_INFO("\t\tActive diagnostics faults:");
ADIJake 0:85855ecd3257 85
Vkadaba 5:0728bde67bdb 86 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CHECKSUM_ERROR)
Vkadaba 5:0728bde67bdb 87 ADMW_LOG_INFO("\t\t\tInternal Checksum fault detected");
Vkadaba 6:9d393a9677f4 88
Vkadaba 6:9d393a9677f4 89
Vkadaba 6:9d393a9677f4 90
Vkadaba 5:0728bde67bdb 91 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CONVERSION_ERROR)
Vkadaba 5:0728bde67bdb 92 ADMW_LOG_INFO("\t\t\tInternal ADC Conversions fault detected");
Vkadaba 5:0728bde67bdb 93 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CALIBRATION_ERROR)
Vkadaba 5:0728bde67bdb 94 ADMW_LOG_INFO("\t\t\tInternal Device Calibrations fault detected");
ADIJake 0:85855ecd3257 95 }
ADIJake 0:85855ecd3257 96 }
ADIJake 0:85855ecd3257 97
Vkadaba 5:0728bde67bdb 98 if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT)
ADIJake 0:85855ecd3257 99 {
Vkadaba 5:0728bde67bdb 100 ADMW_LOG_INFO("\tActive Alerts - ATTENTION REQUIRED:");
Vkadaba 5:0728bde67bdb 101 ADMW_LOG_INFO("\t\tLast Alert Code: %u (0x%X)",
ADIJake 0:85855ecd3257 102 pStatus->alertCode, pStatus->alertCode);
ADIJake 0:85855ecd3257 103
Vkadaba 5:0728bde67bdb 104 for (unsigned i = 0; i < ADMW1001_MAX_CHANNELS; i++)
ADIJake 0:85855ecd3257 105 {
ADIJake 0:85855ecd3257 106 if (pStatus->channelAlerts[i] == 0)
ADIJake 0:85855ecd3257 107 continue;
ADIJake 0:85855ecd3257 108
Vkadaba 5:0728bde67bdb 109 ADMW_LOG_INFO("\t\tChannel #%u:", i);
Vkadaba 5:0728bde67bdb 110 ADMW_LOG_INFO("\t\t\tLast Alert Code: %u (0x%X)",
ADIJake 0:85855ecd3257 111 pStatus->channelAlertCodes[i],
ADIJake 0:85855ecd3257 112 pStatus->channelAlertCodes[i]);
Vkadaba 5:0728bde67bdb 113 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_TIMEOUT)
Vkadaba 5:0728bde67bdb 114 ADMW_LOG_INFO("\t\t\tTimeout alert detected");
Vkadaba 5:0728bde67bdb 115 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_UNDER_RANGE)
Vkadaba 5:0728bde67bdb 116 ADMW_LOG_INFO("\t\t\tUnder Range alert detected");
Vkadaba 5:0728bde67bdb 117 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_OVER_RANGE)
Vkadaba 5:0728bde67bdb 118 ADMW_LOG_INFO("\t\t\tOver Range alert detected");
Vkadaba 5:0728bde67bdb 119 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LOW_LIMIT)
Vkadaba 5:0728bde67bdb 120 ADMW_LOG_INFO("\t\t\tLow limit alert detected");
Vkadaba 5:0728bde67bdb 121 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_HIGH_LIMIT)
Vkadaba 5:0728bde67bdb 122 ADMW_LOG_INFO("\t\t\tHigh Limit alert detected");
Vkadaba 5:0728bde67bdb 123 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_SENSOR_OPEN)
Vkadaba 5:0728bde67bdb 124 ADMW_LOG_INFO("\t\t\tSensor Fault alert detected");
Vkadaba 5:0728bde67bdb 125 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_REF_DETECT)
Vkadaba 5:0728bde67bdb 126 ADMW_LOG_INFO("\t\t\tReference Detection alert detected");
Vkadaba 5:0728bde67bdb 127 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_CONFIG_ERR)
Vkadaba 5:0728bde67bdb 128 ADMW_LOG_INFO("\t\t\tConfiguration Error alert detected");
Vkadaba 5:0728bde67bdb 129 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LUT_ERR)
Vkadaba 5:0728bde67bdb 130 ADMW_LOG_INFO("\t\t\tLook-Up Table Error alert detected");
Vkadaba 5:0728bde67bdb 131 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_SENSOR_NOT_READY)
Vkadaba 5:0728bde67bdb 132 ADMW_LOG_INFO("\t\t\tSensor Not Ready alert detected");
Vkadaba 5:0728bde67bdb 133 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_COMP_NOT_READY)
Vkadaba 5:0728bde67bdb 134 ADMW_LOG_INFO("\t\t\tCompensation Channel Not Ready alert detected");
Vkadaba 5:0728bde67bdb 135 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LUT_UNDER_RANGE)
Vkadaba 5:0728bde67bdb 136 ADMW_LOG_INFO("\t\t\tUnder Look-Up Table Range alert detected");
Vkadaba 5:0728bde67bdb 137 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_LUT_OVER_RANGE)
Vkadaba 5:0728bde67bdb 138 ADMW_LOG_INFO("\t\t\tOver Look-Up Table Range alert detected");
ADIJake 0:85855ecd3257 139 }
ADIJake 0:85855ecd3257 140 }
ADIJake 0:85855ecd3257 141
Vkadaba 5:0728bde67bdb 142 if ((pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) ||
Vkadaba 5:0728bde67bdb 143 (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT))
ADIJake 0:85855ecd3257 144 {
Vkadaba 5:0728bde67bdb 145 ADMW_LOG_INFO("\t\tLast Debug Code: %u-%u",
ADIJake 0:85855ecd3257 146 (pStatus->debugCode >> 16) & 0xFFFF,
ADIJake 0:85855ecd3257 147 (pStatus->debugCode >> 0) & 0xFFFF);
ADIJake 0:85855ecd3257 148 }
ADIJake 0:85855ecd3257 149 }
ADIJake 0:85855ecd3257 150 }
ADIJake 0:85855ecd3257 151
ADIJake 0:85855ecd3257 152 void utils_printSamples(
Vkadaba 5:0728bde67bdb 153 ADMW_DATA_SAMPLE *pSampleBuffer,
ADIJake 0:85855ecd3257 154 uint32_t nNumSamples,
Vkadaba 5:0728bde67bdb 155 ADMW_MEASUREMENT_MODE eMeasurementMode)
ADIJake 0:85855ecd3257 156 {
Vkadaba 10:14954555be2a 157
Vkadaba 10:14954555be2a 158 for (uint32_t i = 0; i < nNumSamples; i++)
Vkadaba 10:14954555be2a 159 {
Vkadaba 10:14954555be2a 160 ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Raw %0X %f :: Processed %f :: flags:%s%s",
Vkadaba 10:14954555be2a 161 i+1,
Vkadaba 10:14954555be2a 162 pSampleBuffer[i].channelId,
Vkadaba 10:14954555be2a 163 pSampleBuffer[i].rawValue, (pSampleBuffer[i].rawValue/1024.0),
Vkadaba 10:14954555be2a 164 pSampleBuffer[i].processedValue,
Vkadaba 10:14954555be2a 165 pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR ? " ERROR" : "",
Vkadaba 10:14954555be2a 166 pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT ? " ALERT" : "");
Vkadaba 10:14954555be2a 167 }
ADIJake 0:85855ecd3257 168 }
ADIJake 0:85855ecd3257 169
Vkadaba 5:0728bde67bdb 170 static void gpioCallbackFn(ADMW_GPIO_PIN ePinId, void * pArg)
ADIJake 0:85855ecd3257 171 {
ADIJake 0:85855ecd3257 172 volatile bool *pbFlag = (volatile bool *)pArg;
ADIJake 0:85855ecd3257 173 *pbFlag = true;
ADIJake 0:85855ecd3257 174 }
ADIJake 0:85855ecd3257 175
Vkadaba 5:0728bde67bdb 176 ADMW_RESULT utils_registerCallbacks(
Vkadaba 5:0728bde67bdb 177 ADMW_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 178 volatile bool *pbDataReady,
ADIJake 0:85855ecd3257 179 volatile bool *pbError,
ADIJake 0:85855ecd3257 180 volatile bool *pbAlert)
ADIJake 0:85855ecd3257 181 {
Vkadaba 5:0728bde67bdb 182 ADMW_RESULT res;
ADIJake 0:85855ecd3257 183 bool state;
ADIJake 0:85855ecd3257 184
Vkadaba 5:0728bde67bdb 185 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
ADIJake 0:85855ecd3257 186 gpioCallbackFn, (void *)pbDataReady);
Vkadaba 5:0728bde67bdb 187 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 188 {
Vkadaba 5:0728bde67bdb 189 ADMW_LOG_ERROR("Failed to register DATAREADY callback");
ADIJake 0:85855ecd3257 190 return res;
ADIJake 0:85855ecd3257 191 }
ADIJake 0:85855ecd3257 192
Vkadaba 5:0728bde67bdb 193 res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, &state);
Vkadaba 5:0728bde67bdb 194 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 195 {
Vkadaba 5:0728bde67bdb 196 ADMW_LOG_ERROR("Failed to get current ERROR state");
ADIJake 0:85855ecd3257 197 return res;
ADIJake 0:85855ecd3257 198 }
ADIJake 0:85855ecd3257 199 if (state)
ADIJake 0:85855ecd3257 200 {
Vkadaba 5:0728bde67bdb 201 ADMW_LOG_ERROR("ERROR signal already asserted");
Vkadaba 5:0728bde67bdb 202 return ADMW_FAILURE;
ADIJake 0:85855ecd3257 203 }
Vkadaba 5:0728bde67bdb 204 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
ADIJake 0:85855ecd3257 205 gpioCallbackFn, (void *)pbError);
Vkadaba 5:0728bde67bdb 206 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 207 {
Vkadaba 5:0728bde67bdb 208 ADMW_LOG_ERROR("Failed to register ERROR callback");
ADIJake 0:85855ecd3257 209 return res;
ADIJake 0:85855ecd3257 210 }
ADIJake 0:85855ecd3257 211
Vkadaba 5:0728bde67bdb 212 /*res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT, &state);
Vkadaba 5:0728bde67bdb 213 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 214 {
Vkadaba 5:0728bde67bdb 215 ADMW_LOG_ERROR("Failed to get current ALERT state");
ADIJake 0:85855ecd3257 216 return res;
ADIJake 0:85855ecd3257 217 }
ADIJake 0:85855ecd3257 218 if (state)
ADIJake 0:85855ecd3257 219 {
Vkadaba 5:0728bde67bdb 220 ADMW_LOG_ERROR("ALERT signal already asserted");
Vkadaba 5:0728bde67bdb 221 return ADMW_FAILURE;
ADIJake 1:63dd03580de4 222 }*/
Vkadaba 5:0728bde67bdb 223 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
ADIJake 0:85855ecd3257 224 gpioCallbackFn, (void *)pbAlert);
Vkadaba 5:0728bde67bdb 225 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 226 {
Vkadaba 5:0728bde67bdb 227 ADMW_LOG_ERROR("Failed to register ALERT callback");
ADIJake 0:85855ecd3257 228 return res;
ADIJake 0:85855ecd3257 229 }
ADIJake 0:85855ecd3257 230
Vkadaba 5:0728bde67bdb 231 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 232 }
ADIJake 0:85855ecd3257 233
Vkadaba 5:0728bde67bdb 234 ADMW_RESULT utils_deregisterCallbacks(
Vkadaba 5:0728bde67bdb 235 ADMW_DEVICE_HANDLE hDevice)
ADIJake 0:85855ecd3257 236 {
Vkadaba 5:0728bde67bdb 237 ADMW_RESULT res;
ADIJake 0:85855ecd3257 238
Vkadaba 5:0728bde67bdb 239 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
ADIJake 0:85855ecd3257 240 NULL, NULL);
Vkadaba 5:0728bde67bdb 241 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 242 {
Vkadaba 5:0728bde67bdb 243 ADMW_LOG_ERROR("Failed to deregister DATAREADY callback");
ADIJake 0:85855ecd3257 244 return res;
ADIJake 0:85855ecd3257 245 }
ADIJake 0:85855ecd3257 246
Vkadaba 5:0728bde67bdb 247 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
ADIJake 0:85855ecd3257 248 NULL, NULL);
Vkadaba 5:0728bde67bdb 249 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 250 {
Vkadaba 5:0728bde67bdb 251 ADMW_LOG_ERROR("Failed to deregister ERROR callback");
ADIJake 0:85855ecd3257 252 return res;
ADIJake 0:85855ecd3257 253 }
ADIJake 0:85855ecd3257 254
Vkadaba 5:0728bde67bdb 255 res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
ADIJake 0:85855ecd3257 256 NULL, NULL);
Vkadaba 5:0728bde67bdb 257 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 258 {
Vkadaba 5:0728bde67bdb 259 ADMW_LOG_INFO("Failed to deregister ALERT callback");
ADIJake 0:85855ecd3257 260 return res;
ADIJake 0:85855ecd3257 261 }
ADIJake 0:85855ecd3257 262
Vkadaba 5:0728bde67bdb 263 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 264 }
ADIJake 0:85855ecd3257 265
Vkadaba 5:0728bde67bdb 266 ADMW_RESULT utils_runMeasurement(
Vkadaba 5:0728bde67bdb 267 ADMW_DEVICE_HANDLE hDevice,
Vkadaba 5:0728bde67bdb 268 ADMW_MEASUREMENT_MODE eMeasurementMode)
ADIJake 0:85855ecd3257 269 {
Vkadaba 5:0728bde67bdb 270 ADMW_RESULT res;
ADIJake 0:85855ecd3257 271
ADIJake 0:85855ecd3257 272 volatile bool bDataReady = false;
ADIJake 0:85855ecd3257 273 volatile bool bError = false;
ADIJake 0:85855ecd3257 274 volatile bool bAlert = false;
ADIJake 0:85855ecd3257 275 res = utils_registerCallbacks(hDevice, &bDataReady, &bError, &bAlert);
Vkadaba 5:0728bde67bdb 276 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 277 return res;
ADIJake 0:85855ecd3257 278
ADIJake 0:85855ecd3257 279 /*
ADIJake 0:85855ecd3257 280 * Retrieve the number of samples per cycle, per DATAREADY pulse, etc. for
ADIJake 0:85855ecd3257 281 * this configuration.
ADIJake 0:85855ecd3257 282 */
Vkadaba 5:0728bde67bdb 283 ADMW1001_OPERATING_MODE eOperatingMode;
Vkadaba 5:0728bde67bdb 284 ADMW1001_DATAREADY_MODE eDataReadyMode;
ADIJake 0:85855ecd3257 285 uint32_t nSamplesPerDataready;
ADIJake 0:85855ecd3257 286 uint32_t nSamplesPerCycle;
ADIJake 0:85855ecd3257 287 uint8_t nBytesPerSample;
Vkadaba 5:0728bde67bdb 288 res = admw1001_GetDataReadyModeInfo(hDevice,
ADIJake 0:85855ecd3257 289 eMeasurementMode,
ADIJake 0:85855ecd3257 290 &eOperatingMode,
ADIJake 0:85855ecd3257 291 &eDataReadyMode,
ADIJake 0:85855ecd3257 292 &nSamplesPerDataready,
ADIJake 0:85855ecd3257 293 &nSamplesPerCycle,
ADIJake 0:85855ecd3257 294 &nBytesPerSample);
Vkadaba 5:0728bde67bdb 295 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 296 return res;
ADIJake 0:85855ecd3257 297
ADIJake 0:85855ecd3257 298 /*
ADIJake 0:85855ecd3257 299 * Allocate a buffer to store the samples retrieved on each DATAREADY pulse
ADIJake 0:85855ecd3257 300 */
Vkadaba 5:0728bde67bdb 301 ADMW_DATA_SAMPLE *pSampleBuffer;
Vkadaba 5:0728bde67bdb 302 pSampleBuffer = malloc(sizeof(ADMW_DATA_SAMPLE) *
ADIJake 0:85855ecd3257 303 nSamplesPerDataready);
ADIJake 0:85855ecd3257 304 if (pSampleBuffer == NULL)
ADIJake 0:85855ecd3257 305 {
Vkadaba 5:0728bde67bdb 306 ADMW_LOG_ERROR("Failed to allocate sample buffer");
Vkadaba 5:0728bde67bdb 307 return ADMW_NO_MEM;
ADIJake 0:85855ecd3257 308 }
ADIJake 0:85855ecd3257 309
ADIJake 0:85855ecd3257 310 /*
ADIJake 0:85855ecd3257 311 * Kick off the measurement cycle(s) here
ADIJake 0:85855ecd3257 312 */
Vkadaba 5:0728bde67bdb 313 ADMW_LOG_INFO("Starting measurement");
Vkadaba 5:0728bde67bdb 314 res = admw_StartMeasurement(hDevice, eMeasurementMode);
Vkadaba 5:0728bde67bdb 315 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 316 {
Vkadaba 5:0728bde67bdb 317 ADMW_LOG_ERROR("Failed to start measurement");
ADIJake 0:85855ecd3257 318 return res;
ADIJake 0:85855ecd3257 319 }
ADIJake 0:85855ecd3257 320
ADIJake 0:85855ecd3257 321 /*
ADIJake 0:85855ecd3257 322 * Loop continuously unless operating mode is single-cycle
ADIJake 0:85855ecd3257 323 */
ADIJake 0:85855ecd3257 324 uint32_t nSampleCount = 0;
ADIJake 0:85855ecd3257 325 uint32_t nReturned;
ADIJake 0:85855ecd3257 326 while (true)
ADIJake 0:85855ecd3257 327 {
Vkadaba 5:0728bde67bdb 328 ADMW_STATUS status;
ADIJake 0:85855ecd3257 329
ADIJake 0:85855ecd3257 330 /*
ADIJake 0:85855ecd3257 331 * Wait until the next batch of 1 or more samples is ready, continuously
ADIJake 0:85855ecd3257 332 * checking DATAREADY until it is asserted
ADIJake 0:85855ecd3257 333 */
ADIJake 0:85855ecd3257 334 while (! (bDataReady || bError))
ADIJake 0:85855ecd3257 335 ;
ADIJake 0:85855ecd3257 336
ADIJake 0:85855ecd3257 337 if (!bError)
ADIJake 0:85855ecd3257 338 {
ADIJake 0:85855ecd3257 339 /*
ADIJake 0:85855ecd3257 340 * Get data samples from the measurement cycle, if no error has occurred
ADIJake 0:85855ecd3257 341 */
ADIJake 0:85855ecd3257 342 bDataReady = false;
Vkadaba 5:0728bde67bdb 343 res = admw_GetData(hDevice, eMeasurementMode, pSampleBuffer,
ADIJake 0:85855ecd3257 344 nBytesPerSample, nSamplesPerDataready,
ADIJake 0:85855ecd3257 345 &nReturned);
Vkadaba 5:0728bde67bdb 346 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 347 {
Vkadaba 5:0728bde67bdb 348 if (res == ADMW_INCOMPLETE)
ADIJake 0:85855ecd3257 349 {
ADIJake 0:85855ecd3257 350 /*
ADIJake 0:85855ecd3257 351 * This is expected in cases where cycleSkipCount may
ADIJake 0:85855ecd3257 352 * be non-zero for some channels, resulting in
ADIJake 0:85855ecd3257 353 * variable-length sequences
ADIJake 0:85855ecd3257 354 */
Vkadaba 5:0728bde67bdb 355 ADMW_LOG_DEBUG("Retrieved %u of %u requested data samples",
ADIJake 0:85855ecd3257 356 nReturned, nSamplesPerDataready);
ADIJake 0:85855ecd3257 357 }
ADIJake 0:85855ecd3257 358 else
ADIJake 0:85855ecd3257 359 {
Vkadaba 5:0728bde67bdb 360 ADMW_LOG_WARN("Failed to get data samples from device");
ADIJake 0:85855ecd3257 361 return res;
ADIJake 0:85855ecd3257 362 }
ADIJake 0:85855ecd3257 363 }
ADIJake 0:85855ecd3257 364
ADIJake 0:85855ecd3257 365 /*
ADIJake 0:85855ecd3257 366 * Display the data samples.
ADIJake 0:85855ecd3257 367 *
ADIJake 0:85855ecd3257 368 * NOTE: this requires a sufficient idle time between subsequent
ADIJake 0:85855ecd3257 369 * DATAREADY pulses to allow printing to occur. Otherwise,
ADIJake 0:85855ecd3257 370 * subsequent samples may be missed if not retrieved promptly when
ADIJake 0:85855ecd3257 371 * the next DATAREADY assertion occurs.
ADIJake 0:85855ecd3257 372 */
ADIJake 0:85855ecd3257 373 utils_printSamples(pSampleBuffer, nReturned, eMeasurementMode);
ADIJake 0:85855ecd3257 374 nSampleCount += nReturned;
ADIJake 0:85855ecd3257 375 }
ADIJake 0:85855ecd3257 376
ADIJake 0:85855ecd3257 377 /*
ADIJake 0:85855ecd3257 378 * Check and print device status if errors/alerts have been triggered
ADIJake 0:85855ecd3257 379 */
ADIJake 0:85855ecd3257 380 if (bError || bAlert)
ADIJake 0:85855ecd3257 381 {
Vkadaba 5:0728bde67bdb 382 res = admw_GetStatus(hDevice, &status);
Vkadaba 5:0728bde67bdb 383 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 384 {
Vkadaba 5:0728bde67bdb 385 ADMW_LOG_ERROR("Failed to retrieve device status");
ADIJake 0:85855ecd3257 386 return res;
ADIJake 0:85855ecd3257 387 }
ADIJake 0:85855ecd3257 388
ADIJake 0:85855ecd3257 389 if (status.deviceStatus &
Vkadaba 5:0728bde67bdb 390 (ADMW_DEVICE_STATUS_ERROR | ADMW_DEVICE_STATUS_ALERT))
ADIJake 0:85855ecd3257 391 {
ADIJake 0:85855ecd3257 392 utils_printStatus(&status);
ADIJake 0:85855ecd3257 393
ADIJake 0:85855ecd3257 394 /* Break out of the loop if any errors are raised */
ADIJake 0:85855ecd3257 395 if (bError)
ADIJake 0:85855ecd3257 396 break;
ADIJake 0:85855ecd3257 397 }
ADIJake 0:85855ecd3257 398 }
ADIJake 0:85855ecd3257 399
Vkadaba 5:0728bde67bdb 400 if (eOperatingMode == ADMW1001_OPERATING_MODE_SINGLECYCLE)
ADIJake 0:85855ecd3257 401 {
ADIJake 0:85855ecd3257 402 /*
ADIJake 0:85855ecd3257 403 * In this mode, break out of the loop when the measurement command
ADIJake 0:85855ecd3257 404 * has completed.
ADIJake 0:85855ecd3257 405 *
ADIJake 0:85855ecd3257 406 * One option is to check for the expected number of samples to be
ADIJake 0:85855ecd3257 407 * returned for the cycle. However, cycles may have variable-length
ADIJake 0:85855ecd3257 408 * sequences if the cycleSkipCount option is non-zero for any of the
ADIJake 0:85855ecd3257 409 * channels.
ADIJake 0:85855ecd3257 410 *
ADIJake 0:85855ecd3257 411 * So, instead, we check for the command-running status, which
ADIJake 0:85855ecd3257 412 * will de-assert in this mode when the measurement command has
ADIJake 0:85855ecd3257 413 * completed a single cycle.
ADIJake 0:85855ecd3257 414 */
ADIJake 0:85855ecd3257 415 bool bCommandRunning;
Vkadaba 5:0728bde67bdb 416 res = admw_GetCommandRunningState(hDevice, &bCommandRunning);
Vkadaba 5:0728bde67bdb 417 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 418 {
Vkadaba 5:0728bde67bdb 419 ADMW_LOG_ERROR("Failed to get command-running status");
ADIJake 0:85855ecd3257 420 return res;
ADIJake 0:85855ecd3257 421 }
ADIJake 0:85855ecd3257 422
ADIJake 0:85855ecd3257 423 if (!bCommandRunning && !bDataReady)
ADIJake 0:85855ecd3257 424 break;
ADIJake 0:85855ecd3257 425 }
ADIJake 0:85855ecd3257 426 }
ADIJake 0:85855ecd3257 427
Vkadaba 5:0728bde67bdb 428 ADMW_LOG_INFO("Stopping measurement");
Vkadaba 5:0728bde67bdb 429 res = admw_StopMeasurement(hDevice);
Vkadaba 5:0728bde67bdb 430 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 431 {
Vkadaba 5:0728bde67bdb 432 ADMW_LOG_ERROR("Failed to send stop measurement");
ADIJake 0:85855ecd3257 433 return res;
ADIJake 0:85855ecd3257 434 }
ADIJake 0:85855ecd3257 435
ADIJake 0:85855ecd3257 436 free(pSampleBuffer);
ADIJake 0:85855ecd3257 437
ADIJake 0:85855ecd3257 438 res = utils_deregisterCallbacks(hDevice);
Vkadaba 5:0728bde67bdb 439 if (res != ADMW_SUCCESS)
ADIJake 0:85855ecd3257 440 return res;
ADIJake 0:85855ecd3257 441
Vkadaba 5:0728bde67bdb 442 return ADMW_SUCCESS;
ADIJake 0:85855ecd3257 443 }
ADIJake 0:85855ecd3257 444