initial commit

Committer:
ADIJake
Date:
Mon Apr 01 11:09:52 2019 +0000
Revision:
0:85855ecd3257
Child:
4:2ca06eee5735
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ADIJake 0:85855ecd3257 1 /*
ADIJake 0:85855ecd3257 2 Copyright 2018 (c) Analog Devices, Inc.
ADIJake 0:85855ecd3257 3
ADIJake 0:85855ecd3257 4 All rights reserved.
ADIJake 0:85855ecd3257 5
ADIJake 0:85855ecd3257 6 Redistribution and use in source and binary forms, with or without
ADIJake 0:85855ecd3257 7 modification, are permitted provided that the following conditions are met:
ADIJake 0:85855ecd3257 8 - Redistributions of source code must retain the above copyright
ADIJake 0:85855ecd3257 9 notice, this list of conditions and the following disclaimer.
ADIJake 0:85855ecd3257 10 - Redistributions in binary form must reproduce the above copyright
ADIJake 0:85855ecd3257 11 notice, this list of conditions and the following disclaimer in
ADIJake 0:85855ecd3257 12 the documentation and/or other materials provided with the
ADIJake 0:85855ecd3257 13 distribution.
ADIJake 0:85855ecd3257 14 - Neither the name of Analog Devices, Inc. nor the names of its
ADIJake 0:85855ecd3257 15 contributors may be used to endorse or promote products derived
ADIJake 0:85855ecd3257 16 from this software without specific prior written permission.
ADIJake 0:85855ecd3257 17 - The use of this software may or may not infringe the patent rights
ADIJake 0:85855ecd3257 18 of one or more patent holders. This license does not release you
ADIJake 0:85855ecd3257 19 from the requirement that you obtain separate licenses from these
ADIJake 0:85855ecd3257 20 patent holders to use this software.
ADIJake 0:85855ecd3257 21 - Use of the software either in source or binary form, must be run
ADIJake 0:85855ecd3257 22 on or directly connected to an Analog Devices Inc. component.
ADIJake 0:85855ecd3257 23
ADIJake 0:85855ecd3257 24 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
ADIJake 0:85855ecd3257 25 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
ADIJake 0:85855ecd3257 26 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ADIJake 0:85855ecd3257 27 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
ADIJake 0:85855ecd3257 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ADIJake 0:85855ecd3257 29 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
ADIJake 0:85855ecd3257 30 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ADIJake 0:85855ecd3257 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ADIJake 0:85855ecd3257 32 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ADIJake 0:85855ecd3257 33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ADIJake 0:85855ecd3257 34 */
ADIJake 0:85855ecd3257 35
ADIJake 0:85855ecd3257 36 /******************************************************************************
ADIJake 0:85855ecd3257 37 Copyright 2017 (c) Analog Devices, Inc.
ADIJake 0:85855ecd3257 38
ADIJake 0:85855ecd3257 39 All rights reserved.
ADIJake 0:85855ecd3257 40
ADIJake 0:85855ecd3257 41 Redistribution and use in source and binary forms, with or without
ADIJake 0:85855ecd3257 42 modification, are permitted provided that the following conditions are met:
ADIJake 0:85855ecd3257 43 - Redistributions of source code must retain the above copyright
ADIJake 0:85855ecd3257 44 notice, this list of conditions and the following disclaimer.
ADIJake 0:85855ecd3257 45 - Redistributions in binary form must reproduce the above copyright
ADIJake 0:85855ecd3257 46 notice, this list of conditions and the following disclaimer in
ADIJake 0:85855ecd3257 47 the documentation and/or other materials provided with the
ADIJake 0:85855ecd3257 48 distribution.
ADIJake 0:85855ecd3257 49 - Neither the name of Analog Devices, Inc. nor the names of its
ADIJake 0:85855ecd3257 50 contributors may be used to endorse or promote products derived
ADIJake 0:85855ecd3257 51 from this software without specific prior written permission.
ADIJake 0:85855ecd3257 52 - The use of this software may or may not infringe the patent rights
ADIJake 0:85855ecd3257 53 of one or more patent holders. This license does not release you
ADIJake 0:85855ecd3257 54 from the requirement that you obtain separate licenses from these
ADIJake 0:85855ecd3257 55 patent holders to use this software.
ADIJake 0:85855ecd3257 56 - Use of the software either in source or binary form, must be run
ADIJake 0:85855ecd3257 57 on or directly connected to an Analog Devices Inc. component.
ADIJake 0:85855ecd3257 58
ADIJake 0:85855ecd3257 59 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
ADIJake 0:85855ecd3257 60 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
ADIJake 0:85855ecd3257 61 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ADIJake 0:85855ecd3257 62 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
ADIJake 0:85855ecd3257 63 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ADIJake 0:85855ecd3257 64 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
ADIJake 0:85855ecd3257 65 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ADIJake 0:85855ecd3257 66 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ADIJake 0:85855ecd3257 67 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ADIJake 0:85855ecd3257 68 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ADIJake 0:85855ecd3257 69 *
ADIJake 0:85855ecd3257 70 *****************************************************************************/
ADIJake 0:85855ecd3257 71
ADIJake 0:85855ecd3257 72 /*!
ADIJake 0:85855ecd3257 73 ******************************************************************************
ADIJake 0:85855ecd3257 74 * @file:
ADIJake 0:85855ecd3257 75 * @brief: ADISENSE API implementation for ADSNS1000
ADIJake 0:85855ecd3257 76 *-----------------------------------------------------------------------------
ADIJake 0:85855ecd3257 77 */
ADIJake 0:85855ecd3257 78
ADIJake 0:85855ecd3257 79 #include <float.h>
ADIJake 0:85855ecd3257 80 #include <math.h>
ADIJake 0:85855ecd3257 81 #include <string.h>
ADIJake 0:85855ecd3257 82
ADIJake 0:85855ecd3257 83 #include "adi_sense_platform.h"
ADIJake 0:85855ecd3257 84 #include "adi_sense_api.h"
ADIJake 0:85855ecd3257 85 #include "adi_sense_1000/adi_sense_1000_api.h"
ADIJake 0:85855ecd3257 86
ADIJake 0:85855ecd3257 87 #include "adi_sense_1000/ADISENSE1000_REGISTERS_typedefs.h"
ADIJake 0:85855ecd3257 88 #include "adi_sense_1000/ADISENSE1000_REGISTERS.h"
ADIJake 0:85855ecd3257 89 #include "adi_sense_1000/adi_sense_1000_lut_data.h"
ADIJake 0:85855ecd3257 90 #include "adi_sense_1000/adi_sense_1000_host_comms.h"
ADIJake 0:85855ecd3257 91
ADIJake 0:85855ecd3257 92 #include "crc16.h"
ADIJake 0:85855ecd3257 93
ADIJake 0:85855ecd3257 94
ADIJake 0:85855ecd3257 95 uint32_t getDataCnt = 0;
ADIJake 0:85855ecd3257 96
ADIJake 0:85855ecd3257 97 /*
ADIJake 0:85855ecd3257 98 * The following macros are used to encapsulate the register access code
ADIJake 0:85855ecd3257 99 * to improve readability in the functions further below in this file
ADIJake 0:85855ecd3257 100 */
ADIJake 0:85855ecd3257 101 #define STRINGIFY(name) #name
ADIJake 0:85855ecd3257 102
ADIJake 0:85855ecd3257 103 /* Expand the full name of the reset value macro for the specified register */
ADIJake 0:85855ecd3257 104 #define REG_RESET_VAL(_name) REG_ADISENSE_##_name##_RESET
ADIJake 0:85855ecd3257 105
ADIJake 0:85855ecd3257 106 /* Checks if a value is outside the bounds of the specified register field */
ADIJake 0:85855ecd3257 107 #define CHECK_REG_FIELD_VAL(_field, _val) \
ADIJake 0:85855ecd3257 108 do { \
ADIJake 0:85855ecd3257 109 uint32_t _mask = BITM_ADISENSE_##_field; \
ADIJake 0:85855ecd3257 110 uint32_t _shift = BITP_ADISENSE_##_field; \
ADIJake 0:85855ecd3257 111 if ((((_val) << _shift) & ~(_mask)) != 0) { \
ADIJake 0:85855ecd3257 112 ADI_SENSE_LOG_ERROR("Value 0x%08X invalid for register field %s", \
ADIJake 0:85855ecd3257 113 (uint32_t)(_val), \
ADIJake 0:85855ecd3257 114 STRINGIFY(ADISENSE_##_field)); \
ADIJake 0:85855ecd3257 115 return ADI_SENSE_INVALID_PARAM; \
ADIJake 0:85855ecd3257 116 } \
ADIJake 0:85855ecd3257 117 } while(false)
ADIJake 0:85855ecd3257 118
ADIJake 0:85855ecd3257 119 /*
ADIJake 0:85855ecd3257 120 * Encapsulates the write to a specified register
ADIJake 0:85855ecd3257 121 * NOTE - this will cause the calling function to return on error
ADIJake 0:85855ecd3257 122 */
ADIJake 0:85855ecd3257 123 #define WRITE_REG(_hdev, _val, _name, _type) \
ADIJake 0:85855ecd3257 124 do { \
ADIJake 0:85855ecd3257 125 ADI_SENSE_RESULT _res; \
ADIJake 0:85855ecd3257 126 _type _regval = _val; \
ADIJake 0:85855ecd3257 127 _res = adi_sense_1000_WriteRegister((_hdev), \
ADIJake 0:85855ecd3257 128 REG_ADISENSE_##_name, \
ADIJake 0:85855ecd3257 129 &_regval, sizeof(_regval)); \
ADIJake 0:85855ecd3257 130 if (_res != ADI_SENSE_SUCCESS) \
ADIJake 0:85855ecd3257 131 return _res; \
ADIJake 0:85855ecd3257 132 } while(false)
ADIJake 0:85855ecd3257 133
ADIJake 0:85855ecd3257 134 /* Wrapper macro to write a value to a uint32_t register */
ADIJake 0:85855ecd3257 135 #define WRITE_REG_U32(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 136 WRITE_REG(_hdev, _val, _name, uint32_t)
ADIJake 0:85855ecd3257 137 /* Wrapper macro to write a value to a uint16_t register */
ADIJake 0:85855ecd3257 138 #define WRITE_REG_U16(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 139 WRITE_REG(_hdev, _val, _name, uint16_t)
ADIJake 0:85855ecd3257 140 /* Wrapper macro to write a value to a uint8_t register */
ADIJake 0:85855ecd3257 141 #define WRITE_REG_U8(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 142 WRITE_REG(_hdev, _val, _name, uint8_t)
ADIJake 0:85855ecd3257 143 /* Wrapper macro to write a value to a float32_t register */
ADIJake 0:85855ecd3257 144 #define WRITE_REG_FLOAT(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 145 WRITE_REG(_hdev, _val, _name, float32_t)
ADIJake 0:85855ecd3257 146
ADIJake 0:85855ecd3257 147 /*
ADIJake 0:85855ecd3257 148 * Encapsulates the read from a specified register
ADIJake 0:85855ecd3257 149 * NOTE - this will cause the calling function to return on error
ADIJake 0:85855ecd3257 150 */
ADIJake 0:85855ecd3257 151 #define READ_REG(_hdev, _val, _name, _type) \
ADIJake 0:85855ecd3257 152 do { \
ADIJake 0:85855ecd3257 153 ADI_SENSE_RESULT _res; \
ADIJake 0:85855ecd3257 154 _type _regval; \
ADIJake 0:85855ecd3257 155 _res = adi_sense_1000_ReadRegister((_hdev), \
ADIJake 0:85855ecd3257 156 REG_ADISENSE_##_name, \
ADIJake 0:85855ecd3257 157 &_regval, sizeof(_regval)); \
ADIJake 0:85855ecd3257 158 if (_res != ADI_SENSE_SUCCESS) \
ADIJake 0:85855ecd3257 159 return _res; \
ADIJake 0:85855ecd3257 160 _val = _regval; \
ADIJake 0:85855ecd3257 161 } while(false)
ADIJake 0:85855ecd3257 162
ADIJake 0:85855ecd3257 163 /* Wrapper macro to read a value from a uint32_t register */
ADIJake 0:85855ecd3257 164 #define READ_REG_U32(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 165 READ_REG(_hdev, _val, _name, uint32_t)
ADIJake 0:85855ecd3257 166 /* Wrapper macro to read a value from a uint16_t register */
ADIJake 0:85855ecd3257 167 #define READ_REG_U16(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 168 READ_REG(_hdev, _val, _name, uint16_t)
ADIJake 0:85855ecd3257 169 /* Wrapper macro to read a value from a uint8_t register */
ADIJake 0:85855ecd3257 170 #define READ_REG_U8(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 171 READ_REG(_hdev, _val, _name, uint8_t)
ADIJake 0:85855ecd3257 172 /* Wrapper macro to read a value from a float32_t register */
ADIJake 0:85855ecd3257 173 #define READ_REG_FLOAT(_hdev, _val, _name) \
ADIJake 0:85855ecd3257 174 READ_REG(_hdev, _val, _name, float32_t)
ADIJake 0:85855ecd3257 175
ADIJake 0:85855ecd3257 176 /*
ADIJake 0:85855ecd3257 177 * Wrapper macro to write an array of values to a uint8_t register
ADIJake 0:85855ecd3257 178 * NOTE - this is intended only for writing to a keyhole data register
ADIJake 0:85855ecd3257 179 */
ADIJake 0:85855ecd3257 180 #define WRITE_REG_U8_ARRAY(_hdev, _arr, _len, _name) \
ADIJake 0:85855ecd3257 181 do { \
ADIJake 0:85855ecd3257 182 ADI_SENSE_RESULT _res; \
ADIJake 0:85855ecd3257 183 _res = adi_sense_1000_WriteRegister(_hdev, \
ADIJake 0:85855ecd3257 184 REG_ADISENSE_##_name, \
ADIJake 0:85855ecd3257 185 _arr, _len); \
ADIJake 0:85855ecd3257 186 if (_res != ADI_SENSE_SUCCESS) \
ADIJake 0:85855ecd3257 187 return _res; \
ADIJake 0:85855ecd3257 188 } while(false)
ADIJake 0:85855ecd3257 189
ADIJake 0:85855ecd3257 190 /*
ADIJake 0:85855ecd3257 191 * Wrapper macro to read an array of values from a uint8_t register
ADIJake 0:85855ecd3257 192 * NOTE - this is intended only for reading from a keyhole data register
ADIJake 0:85855ecd3257 193 */
ADIJake 0:85855ecd3257 194 #define READ_REG_U8_ARRAY(_hdev, _arr, _len, _name) \
ADIJake 0:85855ecd3257 195 do { \
ADIJake 0:85855ecd3257 196 ADI_SENSE_RESULT _res; \
ADIJake 0:85855ecd3257 197 _res = adi_sense_1000_ReadRegister((_hdev), \
ADIJake 0:85855ecd3257 198 REG_ADISENSE_##_name, \
ADIJake 0:85855ecd3257 199 _arr, _len); \
ADIJake 0:85855ecd3257 200 if (_res != ADI_SENSE_SUCCESS) \
ADIJake 0:85855ecd3257 201 return _res; \
ADIJake 0:85855ecd3257 202 } while(false)
ADIJake 0:85855ecd3257 203
ADIJake 0:85855ecd3257 204 #define ADI_SENSE_1000_CHANNEL_IS_ADC(c) \
ADIJake 0:85855ecd3257 205 ((c) >= ADI_SENSE_1000_CHANNEL_ID_CJC_0 && (c) <= ADI_SENSE_1000_CHANNEL_ID_CURRENT_0)
ADIJake 0:85855ecd3257 206
ADIJake 0:85855ecd3257 207 #define ADI_SENSE_1000_CHANNEL_IS_ADC_CJC(c) \
ADIJake 0:85855ecd3257 208 ((c) >= ADI_SENSE_1000_CHANNEL_ID_CJC_0 && (c) <= ADI_SENSE_1000_CHANNEL_ID_CJC_1)
ADIJake 0:85855ecd3257 209
ADIJake 0:85855ecd3257 210 #define ADI_SENSE_1000_CHANNEL_IS_ADC_SENSOR(c) \
ADIJake 0:85855ecd3257 211 ((c) >= ADI_SENSE_1000_CHANNEL_ID_SENSOR_0 && (c) <= ADI_SENSE_1000_CHANNEL_ID_SENSOR_3)
ADIJake 0:85855ecd3257 212
ADIJake 0:85855ecd3257 213 #define ADI_SENSE_1000_CHANNEL_IS_ADC_VOLTAGE(c) \
ADIJake 0:85855ecd3257 214 ((c) == ADI_SENSE_1000_CHANNEL_ID_VOLTAGE_0)
ADIJake 0:85855ecd3257 215
ADIJake 0:85855ecd3257 216 #define ADI_SENSE_1000_CHANNEL_IS_ADC_CURRENT(c) \
ADIJake 0:85855ecd3257 217 ((c) == ADI_SENSE_1000_CHANNEL_ID_CURRENT_0)
ADIJake 0:85855ecd3257 218
ADIJake 0:85855ecd3257 219 #define ADI_SENSE_1000_CHANNEL_IS_VIRTUAL(c) \
ADIJake 0:85855ecd3257 220 ((c) == ADI_SENSE_1000_CHANNEL_ID_SPI_1 || (c) == ADI_SENSE_1000_CHANNEL_ID_SPI_2)
ADIJake 0:85855ecd3257 221
ADIJake 0:85855ecd3257 222 typedef struct
ADIJake 0:85855ecd3257 223 {
ADIJake 0:85855ecd3257 224 unsigned nDeviceIndex;
ADIJake 0:85855ecd3257 225 ADI_SENSE_SPI_HANDLE hSpi;
ADIJake 0:85855ecd3257 226 ADI_SENSE_GPIO_HANDLE hGpio;
ADIJake 0:85855ecd3257 227 } ADI_SENSE_DEVICE_CONTEXT;
ADIJake 0:85855ecd3257 228
ADIJake 0:85855ecd3257 229 static ADI_SENSE_DEVICE_CONTEXT gDeviceCtx[ADI_SENSE_PLATFORM_MAX_DEVICES];
ADIJake 0:85855ecd3257 230
ADIJake 0:85855ecd3257 231 /*
ADIJake 0:85855ecd3257 232 * Open an ADISENSE device instance.
ADIJake 0:85855ecd3257 233 */
ADIJake 0:85855ecd3257 234 ADI_SENSE_RESULT adi_sense_Open(
ADIJake 0:85855ecd3257 235 unsigned const nDeviceIndex,
ADIJake 0:85855ecd3257 236 ADI_SENSE_CONNECTION * const pConnectionInfo,
ADIJake 0:85855ecd3257 237 ADI_SENSE_DEVICE_HANDLE * const phDevice)
ADIJake 0:85855ecd3257 238 {
ADIJake 0:85855ecd3257 239 ADI_SENSE_DEVICE_CONTEXT *pCtx;
ADIJake 0:85855ecd3257 240 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 241
ADIJake 0:85855ecd3257 242 if (nDeviceIndex >= ADI_SENSE_PLATFORM_MAX_DEVICES)
ADIJake 0:85855ecd3257 243 return ADI_SENSE_INVALID_DEVICE_NUM;
ADIJake 0:85855ecd3257 244
ADIJake 0:85855ecd3257 245 pCtx = &gDeviceCtx[nDeviceIndex];
ADIJake 0:85855ecd3257 246 pCtx->nDeviceIndex = nDeviceIndex;
ADIJake 0:85855ecd3257 247
ADIJake 0:85855ecd3257 248 eRet = adi_sense_LogOpen(&pConnectionInfo->log);
ADIJake 0:85855ecd3257 249 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 250 return eRet;
ADIJake 0:85855ecd3257 251
ADIJake 0:85855ecd3257 252 eRet = adi_sense_GpioOpen(&pConnectionInfo->gpio, &pCtx->hGpio);
ADIJake 0:85855ecd3257 253 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 254 return eRet;
ADIJake 0:85855ecd3257 255
ADIJake 0:85855ecd3257 256 eRet = adi_sense_SpiOpen(&pConnectionInfo->spi, &pCtx->hSpi);
ADIJake 0:85855ecd3257 257 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 258 return eRet;
ADIJake 0:85855ecd3257 259
ADIJake 0:85855ecd3257 260 *phDevice = pCtx;
ADIJake 0:85855ecd3257 261 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 262 }
ADIJake 0:85855ecd3257 263
ADIJake 0:85855ecd3257 264 /*
ADIJake 0:85855ecd3257 265 * Get the current state of the specified GPIO input signal.
ADIJake 0:85855ecd3257 266 */
ADIJake 0:85855ecd3257 267 ADI_SENSE_RESULT adi_sense_GetGpioState(
ADIJake 0:85855ecd3257 268 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 269 ADI_SENSE_GPIO_PIN const ePinId,
ADIJake 0:85855ecd3257 270 bool * const pbAsserted)
ADIJake 0:85855ecd3257 271 {
ADIJake 0:85855ecd3257 272 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 273
ADIJake 0:85855ecd3257 274 return adi_sense_GpioGet(pCtx->hGpio, ePinId, pbAsserted);
ADIJake 0:85855ecd3257 275 }
ADIJake 0:85855ecd3257 276
ADIJake 0:85855ecd3257 277 /*
ADIJake 0:85855ecd3257 278 * Register an application-defined callback function for GPIO interrupts.
ADIJake 0:85855ecd3257 279 */
ADIJake 0:85855ecd3257 280 ADI_SENSE_RESULT adi_sense_RegisterGpioCallback(
ADIJake 0:85855ecd3257 281 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 282 ADI_SENSE_GPIO_PIN const ePinId,
ADIJake 0:85855ecd3257 283 ADI_SENSE_GPIO_CALLBACK const callbackFunction,
ADIJake 0:85855ecd3257 284 void * const pCallbackParam)
ADIJake 0:85855ecd3257 285 {
ADIJake 0:85855ecd3257 286 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 287
ADIJake 0:85855ecd3257 288 if (callbackFunction)
ADIJake 0:85855ecd3257 289 {
ADIJake 0:85855ecd3257 290 return adi_sense_GpioIrqEnable(pCtx->hGpio, ePinId, callbackFunction,
ADIJake 0:85855ecd3257 291 pCallbackParam);
ADIJake 0:85855ecd3257 292 }
ADIJake 0:85855ecd3257 293 else
ADIJake 0:85855ecd3257 294 {
ADIJake 0:85855ecd3257 295 return adi_sense_GpioIrqDisable(pCtx->hGpio, ePinId);
ADIJake 0:85855ecd3257 296 }
ADIJake 0:85855ecd3257 297 }
ADIJake 0:85855ecd3257 298
ADIJake 0:85855ecd3257 299 /*
ADIJake 0:85855ecd3257 300 * Reset the specified ADISENSE device.
ADIJake 0:85855ecd3257 301 */
ADIJake 0:85855ecd3257 302 ADI_SENSE_RESULT adi_sense_Reset(
ADIJake 0:85855ecd3257 303 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 304 {
ADIJake 0:85855ecd3257 305 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 306 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 307
ADIJake 0:85855ecd3257 308 /* Pulse the Reset GPIO pin low for a minimum of 4 microseconds */
ADIJake 0:85855ecd3257 309 eRet = adi_sense_GpioSet(pCtx->hGpio, ADI_SENSE_GPIO_PIN_RESET, false);
ADIJake 0:85855ecd3257 310 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 311 return eRet;
ADIJake 0:85855ecd3257 312
ADIJake 0:85855ecd3257 313 adi_sense_TimeDelayUsec(4);
ADIJake 0:85855ecd3257 314
ADIJake 0:85855ecd3257 315 eRet = adi_sense_GpioSet(pCtx->hGpio, ADI_SENSE_GPIO_PIN_RESET, true);
ADIJake 0:85855ecd3257 316 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 317 return eRet;
ADIJake 0:85855ecd3257 318
ADIJake 0:85855ecd3257 319 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 320 }
ADIJake 0:85855ecd3257 321
ADIJake 0:85855ecd3257 322
ADIJake 0:85855ecd3257 323 /*!
ADIJake 0:85855ecd3257 324 * @brief Get general status of ADISense module.
ADIJake 0:85855ecd3257 325 *
ADIJake 0:85855ecd3257 326 * @param[in]
ADIJake 0:85855ecd3257 327 * @param[out] pStatus : Pointer to CORE Status struct.
ADIJake 0:85855ecd3257 328 *
ADIJake 0:85855ecd3257 329 * @return Status
ADIJake 0:85855ecd3257 330 * - #ADI_SENSE_SUCCESS Call completed successfully.
ADIJake 0:85855ecd3257 331 * - #ADI_SENSE_FAILURE If status register read fails.
ADIJake 0:85855ecd3257 332 *
ADIJake 0:85855ecd3257 333 * @details Read the general status register for the ADISense
ADIJake 0:85855ecd3257 334 * module. Indicates Error, Alert conditions, data ready
ADIJake 0:85855ecd3257 335 * and command running.
ADIJake 0:85855ecd3257 336 *
ADIJake 0:85855ecd3257 337 */
ADIJake 0:85855ecd3257 338 ADI_SENSE_RESULT adi_sense_GetStatus(
ADIJake 0:85855ecd3257 339 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 340 ADI_SENSE_STATUS * const pStatus)
ADIJake 0:85855ecd3257 341 {
ADIJake 0:85855ecd3257 342 ADI_ADISENSE_CORE_Status_t statusReg;
ADIJake 0:85855ecd3257 343 READ_REG_U8(hDevice, statusReg.VALUE8, CORE_STATUS);
ADIJake 0:85855ecd3257 344
ADIJake 0:85855ecd3257 345 ADI_ADISENSE_CORE_Alert_Status_2_t alert2Reg;
ADIJake 0:85855ecd3257 346 READ_REG_U16(hDevice, alert2Reg.VALUE16, CORE_ALERT_STATUS_2);
ADIJake 0:85855ecd3257 347
ADIJake 0:85855ecd3257 348 memset(pStatus, 0, sizeof(*pStatus));
ADIJake 0:85855ecd3257 349
ADIJake 0:85855ecd3257 350 if (!statusReg.Cmd_Running) /* Active-low, so invert it */
ADIJake 0:85855ecd3257 351 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_BUSY;
ADIJake 0:85855ecd3257 352 if (statusReg.Drdy)
ADIJake 0:85855ecd3257 353 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_DATAREADY;
ADIJake 0:85855ecd3257 354 if (statusReg.FIFO_Error)
ADIJake 0:85855ecd3257 355 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_FIFO_ERROR;
ADIJake 0:85855ecd3257 356 if (alert2Reg.Ext_Flash_Error)
ADIJake 0:85855ecd3257 357 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_EXT_FLASH_ERROR;
ADIJake 0:85855ecd3257 358 if (statusReg.Alert_Active)
ADIJake 0:85855ecd3257 359 {
ADIJake 0:85855ecd3257 360 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_ALERT;
ADIJake 0:85855ecd3257 361
ADIJake 0:85855ecd3257 362 ADI_ADISENSE_CORE_Alert_Code_t alertCodeReg;
ADIJake 0:85855ecd3257 363 READ_REG_U16(hDevice, alertCodeReg.VALUE16, CORE_ALERT_CODE);
ADIJake 0:85855ecd3257 364 pStatus->alertCode = alertCodeReg.Alert_Code;
ADIJake 0:85855ecd3257 365
ADIJake 0:85855ecd3257 366 ADI_ADISENSE_CORE_Channel_Alert_Status_t channelAlertStatusReg;
ADIJake 0:85855ecd3257 367 READ_REG_U16(hDevice, channelAlertStatusReg.VALUE16,
ADIJake 0:85855ecd3257 368 CORE_CHANNEL_ALERT_STATUS);
ADIJake 0:85855ecd3257 369
ADIJake 0:85855ecd3257 370 for (unsigned i = 0; i < ADI_SENSE_1000_MAX_CHANNELS; i++)
ADIJake 0:85855ecd3257 371 {
ADIJake 0:85855ecd3257 372 if (channelAlertStatusReg.VALUE16 & (1 << i))
ADIJake 0:85855ecd3257 373 {
ADIJake 0:85855ecd3257 374 ADI_ADISENSE_CORE_Alert_Code_Ch_t channelAlertCodeReg;
ADIJake 0:85855ecd3257 375 READ_REG_U16(hDevice, channelAlertCodeReg.VALUE16, CORE_ALERT_CODE_CHn(i));
ADIJake 0:85855ecd3257 376 pStatus->channelAlertCodes[i] = channelAlertCodeReg.Alert_Code_Ch;
ADIJake 0:85855ecd3257 377
ADIJake 0:85855ecd3257 378 ADI_ADISENSE_CORE_Alert_Detail_Ch_t alertDetailReg;
ADIJake 0:85855ecd3257 379 READ_REG_U16(hDevice, alertDetailReg.VALUE16,
ADIJake 0:85855ecd3257 380 CORE_ALERT_DETAIL_CHn(i));
ADIJake 0:85855ecd3257 381
ADIJake 0:85855ecd3257 382 if (alertDetailReg.Time_Out)
ADIJake 0:85855ecd3257 383 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_TIMEOUT;
ADIJake 0:85855ecd3257 384 if (alertDetailReg.Under_Range)
ADIJake 0:85855ecd3257 385 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_UNDER_RANGE;
ADIJake 0:85855ecd3257 386 if (alertDetailReg.Over_Range)
ADIJake 0:85855ecd3257 387 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_OVER_RANGE;
ADIJake 0:85855ecd3257 388 if (alertDetailReg.Low_Limit)
ADIJake 0:85855ecd3257 389 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LOW_LIMIT;
ADIJake 0:85855ecd3257 390 if (alertDetailReg.High_Limit)
ADIJake 0:85855ecd3257 391 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_HIGH_LIMIT;
ADIJake 0:85855ecd3257 392 if (alertDetailReg.Sensor_Open)
ADIJake 0:85855ecd3257 393 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_SENSOR_OPEN;
ADIJake 0:85855ecd3257 394 if (alertDetailReg.Ref_Detect)
ADIJake 0:85855ecd3257 395 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_REF_DETECT;
ADIJake 0:85855ecd3257 396 if (alertDetailReg.Config_Err)
ADIJake 0:85855ecd3257 397 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_CONFIG_ERR;
ADIJake 0:85855ecd3257 398 if (alertDetailReg.LUT_Error_Ch)
ADIJake 0:85855ecd3257 399 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LUT_ERR;
ADIJake 0:85855ecd3257 400 if (alertDetailReg.Sensor_Not_Ready)
ADIJake 0:85855ecd3257 401 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_SENSOR_NOT_READY;
ADIJake 0:85855ecd3257 402 if (alertDetailReg.Comp_Not_Ready)
ADIJake 0:85855ecd3257 403 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_COMP_NOT_READY;
ADIJake 0:85855ecd3257 404 if (alertDetailReg.Correction_UnderRange)
ADIJake 0:85855ecd3257 405 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LUT_UNDER_RANGE;
ADIJake 0:85855ecd3257 406 if (alertDetailReg.Correction_OverRange)
ADIJake 0:85855ecd3257 407 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LUT_OVER_RANGE;
ADIJake 0:85855ecd3257 408 }
ADIJake 0:85855ecd3257 409 }
ADIJake 0:85855ecd3257 410
ADIJake 0:85855ecd3257 411 if (alert2Reg.Configuration_Error)
ADIJake 0:85855ecd3257 412 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_CONFIG_ERROR;
ADIJake 0:85855ecd3257 413 if (alert2Reg.LUT_Error)
ADIJake 0:85855ecd3257 414 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_LUT_ERROR;
ADIJake 0:85855ecd3257 415 }
ADIJake 0:85855ecd3257 416
ADIJake 0:85855ecd3257 417 if (statusReg.Error)
ADIJake 0:85855ecd3257 418 {
ADIJake 0:85855ecd3257 419 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_ERROR;
ADIJake 0:85855ecd3257 420
ADIJake 0:85855ecd3257 421 ADI_ADISENSE_CORE_Error_Code_t errorCodeReg;
ADIJake 0:85855ecd3257 422 READ_REG_U16(hDevice, errorCodeReg.VALUE16, CORE_ERROR_CODE);
ADIJake 0:85855ecd3257 423 pStatus->errorCode = errorCodeReg.Error_Code;
ADIJake 0:85855ecd3257 424
ADIJake 0:85855ecd3257 425 ADI_ADISENSE_CORE_Diagnostics_Status_t diagStatusReg;
ADIJake 0:85855ecd3257 426 READ_REG_U16(hDevice, diagStatusReg.VALUE16, CORE_DIAGNOSTICS_STATUS);
ADIJake 0:85855ecd3257 427
ADIJake 0:85855ecd3257 428 if (diagStatusReg.Diag_Checksum_Error)
ADIJake 0:85855ecd3257 429 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_CHECKSUM_ERROR;
ADIJake 0:85855ecd3257 430 if (diagStatusReg.Diag_Comms_Error)
ADIJake 0:85855ecd3257 431 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_COMMS_ERROR;
ADIJake 0:85855ecd3257 432 if (diagStatusReg.Diag_Supply_Monitor_Error)
ADIJake 0:85855ecd3257 433 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_SUPPLY_MONITOR_ERROR;
ADIJake 0:85855ecd3257 434 if (diagStatusReg.Diag_Supply_Cap_Error)
ADIJake 0:85855ecd3257 435 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_SUPPLY_CAP_ERROR;
ADIJake 0:85855ecd3257 436 if (diagStatusReg.Diag_Conversion_Error)
ADIJake 0:85855ecd3257 437 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_CONVERSION_ERROR;
ADIJake 0:85855ecd3257 438 if (diagStatusReg.Diag_Calibration_Error)
ADIJake 0:85855ecd3257 439 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_CALIBRATION_ERROR;
ADIJake 0:85855ecd3257 440 }
ADIJake 0:85855ecd3257 441
ADIJake 0:85855ecd3257 442 if (statusReg.Alert_Active || statusReg.Error)
ADIJake 0:85855ecd3257 443 {
ADIJake 0:85855ecd3257 444 ADI_ADISENSE_CORE_Debug_Code_t debugCodeReg;
ADIJake 0:85855ecd3257 445 READ_REG_U32(hDevice, debugCodeReg.VALUE32, CORE_DEBUG_CODE);
ADIJake 0:85855ecd3257 446 pStatus->debugCode = debugCodeReg.Debug_Code;
ADIJake 0:85855ecd3257 447 }
ADIJake 0:85855ecd3257 448
ADIJake 0:85855ecd3257 449 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 450 }
ADIJake 0:85855ecd3257 451
ADIJake 0:85855ecd3257 452 ADI_SENSE_RESULT adi_sense_GetCommandRunningState(
ADIJake 0:85855ecd3257 453 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 454 bool *pbCommandRunning)
ADIJake 0:85855ecd3257 455 {
ADIJake 0:85855ecd3257 456 ADI_ADISENSE_CORE_Status_t statusReg;
ADIJake 0:85855ecd3257 457
ADIJake 0:85855ecd3257 458 READ_REG_U8(hDevice, statusReg.VALUE8, CORE_STATUS);
ADIJake 0:85855ecd3257 459
ADIJake 0:85855ecd3257 460 /* We should never normally see 0xFF here if the module is operational */
ADIJake 0:85855ecd3257 461 if (statusReg.VALUE8 == 0xFF)
ADIJake 0:85855ecd3257 462 return ADI_SENSE_ERR_NOT_INITIALIZED;
ADIJake 0:85855ecd3257 463
ADIJake 0:85855ecd3257 464 *pbCommandRunning = !statusReg.Cmd_Running; /* Active-low, so invert it */
ADIJake 0:85855ecd3257 465
ADIJake 0:85855ecd3257 466 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 467 }
ADIJake 0:85855ecd3257 468
ADIJake 0:85855ecd3257 469 static ADI_SENSE_RESULT executeCommand(
ADIJake 0:85855ecd3257 470 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 471 ADI_ADISENSE_CORE_Command_Special_Command const command,
ADIJake 0:85855ecd3257 472 bool const bWaitForCompletion)
ADIJake 0:85855ecd3257 473 {
ADIJake 0:85855ecd3257 474 ADI_ADISENSE_CORE_Command_t commandReg;
ADIJake 0:85855ecd3257 475 bool bCommandRunning;
ADIJake 0:85855ecd3257 476 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 477
ADIJake 0:85855ecd3257 478 /*
ADIJake 0:85855ecd3257 479 * Don't allow another command to be issued if one is already running, but
ADIJake 0:85855ecd3257 480 * make an exception for ADISENSE_CORE_COMMAND_NOP which can be used to
ADIJake 0:85855ecd3257 481 * request a running command to be stopped (e.g. continuous measurement)
ADIJake 0:85855ecd3257 482 */
ADIJake 0:85855ecd3257 483 if (command != ADISENSE_CORE_COMMAND_NOP)
ADIJake 0:85855ecd3257 484 {
ADIJake 0:85855ecd3257 485 eRet = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning);
ADIJake 0:85855ecd3257 486 if (eRet)
ADIJake 0:85855ecd3257 487 return eRet;
ADIJake 0:85855ecd3257 488
ADIJake 0:85855ecd3257 489 if (bCommandRunning)
ADIJake 0:85855ecd3257 490 return ADI_SENSE_IN_USE;
ADIJake 0:85855ecd3257 491 }
ADIJake 0:85855ecd3257 492
ADIJake 0:85855ecd3257 493 commandReg.Special_Command = command;
ADIJake 0:85855ecd3257 494 WRITE_REG_U8(hDevice, commandReg.VALUE8, CORE_COMMAND);
ADIJake 0:85855ecd3257 495
ADIJake 0:85855ecd3257 496 if (bWaitForCompletion)
ADIJake 0:85855ecd3257 497 {
ADIJake 0:85855ecd3257 498 do {
ADIJake 0:85855ecd3257 499 /* Allow a minimum 50usec delay for status update before checking */
ADIJake 0:85855ecd3257 500 adi_sense_TimeDelayUsec(50);
ADIJake 0:85855ecd3257 501
ADIJake 0:85855ecd3257 502 eRet = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning);
ADIJake 0:85855ecd3257 503 if (eRet)
ADIJake 0:85855ecd3257 504 return eRet;
ADIJake 0:85855ecd3257 505 } while (bCommandRunning);
ADIJake 0:85855ecd3257 506 }
ADIJake 0:85855ecd3257 507
ADIJake 0:85855ecd3257 508 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 509 }
ADIJake 0:85855ecd3257 510
ADIJake 0:85855ecd3257 511 ADI_SENSE_RESULT adi_sense_Shutdown(
ADIJake 0:85855ecd3257 512 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 513 {
ADIJake 0:85855ecd3257 514 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_POWER_DOWN, false);
ADIJake 0:85855ecd3257 515 }
ADIJake 0:85855ecd3257 516
ADIJake 0:85855ecd3257 517
ADIJake 0:85855ecd3257 518 ADI_SENSE_RESULT adi_sense_ApplyConfigUpdates(
ADIJake 0:85855ecd3257 519 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 520 {
ADIJake 0:85855ecd3257 521 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LATCH_CONFIG, true);
ADIJake 0:85855ecd3257 522 }
ADIJake 0:85855ecd3257 523
ADIJake 0:85855ecd3257 524 /*!
ADIJake 0:85855ecd3257 525 * @brief Start a measurement cycle.
ADIJake 0:85855ecd3257 526 *
ADIJake 0:85855ecd3257 527 * @param[out]
ADIJake 0:85855ecd3257 528 *
ADIJake 0:85855ecd3257 529 * @return Status
ADIJake 0:85855ecd3257 530 * - #ADI_SENSE_SUCCESS Call completed successfully.
ADIJake 0:85855ecd3257 531 * - #ADI_SENSE_FAILURE
ADIJake 0:85855ecd3257 532 *
ADIJake 0:85855ecd3257 533 * @details Sends the latch config command. Configuration for channels in
ADIJake 0:85855ecd3257 534 * conversion cycle should be completed before this function.
ADIJake 0:85855ecd3257 535 * Channel enabled bit should be set before this function.
ADIJake 0:85855ecd3257 536 * Starts a conversion and configures the format of the sample.
ADIJake 0:85855ecd3257 537 *
ADIJake 0:85855ecd3257 538 */
ADIJake 0:85855ecd3257 539 ADI_SENSE_RESULT adi_sense_StartMeasurement(
ADIJake 0:85855ecd3257 540 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 541 ADI_SENSE_MEASUREMENT_MODE const eMeasurementMode)
ADIJake 0:85855ecd3257 542 {
ADIJake 0:85855ecd3257 543 switch (eMeasurementMode)
ADIJake 0:85855ecd3257 544 {
ADIJake 0:85855ecd3257 545 case ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK:
ADIJake 0:85855ecd3257 546 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SYSTEM_CHECK, false);
ADIJake 0:85855ecd3257 547 case ADI_SENSE_MEASUREMENT_MODE_NORMAL:
ADIJake 0:85855ecd3257 548 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CONVERT_WITH_RAW, false);
ADIJake 0:85855ecd3257 549 case ADI_SENSE_MEASUREMENT_MODE_OMIT_RAW:
ADIJake 0:85855ecd3257 550 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CONVERT, false);
ADIJake 0:85855ecd3257 551 case ADI_SENSE_MEASUREMENT_MODE_FFT:
ADIJake 0:85855ecd3257 552 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CONVERT_FFT, false);
ADIJake 0:85855ecd3257 553 default:
ADIJake 0:85855ecd3257 554 ADI_SENSE_LOG_ERROR("Invalid measurement mode %d specified",
ADIJake 0:85855ecd3257 555 eMeasurementMode);
ADIJake 0:85855ecd3257 556 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 557 }
ADIJake 0:85855ecd3257 558 }
ADIJake 0:85855ecd3257 559
ADIJake 0:85855ecd3257 560 /*
ADIJake 0:85855ecd3257 561 * Store the configuration settings to persistent memory on the device.
ADIJake 0:85855ecd3257 562 * The settings can be saved to 4 different flash memory areas (slots).
ADIJake 0:85855ecd3257 563 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 564 * Do not power down the device while this command is running.
ADIJake 0:85855ecd3257 565 */
ADIJake 0:85855ecd3257 566 ADI_SENSE_RESULT adi_sense_SaveConfig(
ADIJake 0:85855ecd3257 567 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 568 ADI_SENSE_USER_CONFIG_SLOT const eSlotId)
ADIJake 0:85855ecd3257 569 {
ADIJake 0:85855ecd3257 570 switch (eSlotId)
ADIJake 0:85855ecd3257 571 {
ADIJake 0:85855ecd3257 572 case ADI_SENSE_FLASH_CONFIG_1:
ADIJake 0:85855ecd3257 573 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_CONFIG_1, true);
ADIJake 0:85855ecd3257 574 case ADI_SENSE_FLASH_CONFIG_2:
ADIJake 0:85855ecd3257 575 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_CONFIG_2, true);
ADIJake 0:85855ecd3257 576 case ADI_SENSE_FLASH_CONFIG_3:
ADIJake 0:85855ecd3257 577 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_CONFIG_3, true);
ADIJake 0:85855ecd3257 578 case ADI_SENSE_FLASH_CONFIG_4:
ADIJake 0:85855ecd3257 579 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_CONFIG_4, true);
ADIJake 0:85855ecd3257 580 default:
ADIJake 0:85855ecd3257 581 ADI_SENSE_LOG_ERROR("Invalid user config target slot %d specified",
ADIJake 0:85855ecd3257 582 eSlotId);
ADIJake 0:85855ecd3257 583 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 584 }
ADIJake 0:85855ecd3257 585 }
ADIJake 0:85855ecd3257 586
ADIJake 0:85855ecd3257 587 /*
ADIJake 0:85855ecd3257 588 * Restore the configuration settings from persistent memory on the device.
ADIJake 0:85855ecd3257 589 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 590 */
ADIJake 0:85855ecd3257 591 ADI_SENSE_RESULT adi_sense_RestoreConfig(
ADIJake 0:85855ecd3257 592 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 593 ADI_SENSE_USER_CONFIG_SLOT const eSlotId)
ADIJake 0:85855ecd3257 594 {
ADIJake 0:85855ecd3257 595 switch (eSlotId)
ADIJake 0:85855ecd3257 596 {
ADIJake 0:85855ecd3257 597 case ADI_SENSE_FLASH_CONFIG_1:
ADIJake 0:85855ecd3257 598 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_CONFIG_1, true);
ADIJake 0:85855ecd3257 599 case ADI_SENSE_FLASH_CONFIG_2:
ADIJake 0:85855ecd3257 600 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_CONFIG_2, true);
ADIJake 0:85855ecd3257 601 case ADI_SENSE_FLASH_CONFIG_3:
ADIJake 0:85855ecd3257 602 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_CONFIG_3, true);
ADIJake 0:85855ecd3257 603 case ADI_SENSE_FLASH_CONFIG_4:
ADIJake 0:85855ecd3257 604 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_CONFIG_4, true);
ADIJake 0:85855ecd3257 605 default:
ADIJake 0:85855ecd3257 606 ADI_SENSE_LOG_ERROR("Invalid user config source slot %d specified",
ADIJake 0:85855ecd3257 607 eSlotId);
ADIJake 0:85855ecd3257 608 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 609 }
ADIJake 0:85855ecd3257 610 }
ADIJake 0:85855ecd3257 611
ADIJake 0:85855ecd3257 612 /*
ADIJake 0:85855ecd3257 613 * Erase the entire external flash memory.
ADIJake 0:85855ecd3257 614 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 615 */
ADIJake 0:85855ecd3257 616 ADI_SENSE_RESULT adi_sense_EraseExternalFlash(
ADIJake 0:85855ecd3257 617 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 618 {
ADIJake 0:85855ecd3257 619 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_ERASE_EXTERNAL_FLASH, true);
ADIJake 0:85855ecd3257 620 }
ADIJake 0:85855ecd3257 621
ADIJake 0:85855ecd3257 622 /*
ADIJake 0:85855ecd3257 623 * Read the number of samples stored in external flash memory.
ADIJake 0:85855ecd3257 624 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 625 */
ADIJake 0:85855ecd3257 626 ADI_SENSE_RESULT adi_sense_GetExternalFlashSampleCount(
ADIJake 0:85855ecd3257 627 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 628 uint32_t * nSampleCount)
ADIJake 0:85855ecd3257 629 {
ADIJake 0:85855ecd3257 630 ADI_ADISENSE_CORE_Ext_Flash_Sample_Count_t nCount;
ADIJake 0:85855ecd3257 631
ADIJake 0:85855ecd3257 632 READ_REG_U32(hDevice, nCount.VALUE32, CORE_EXT_FLASH_SAMPLE_COUNT);
ADIJake 0:85855ecd3257 633
ADIJake 0:85855ecd3257 634 *nSampleCount = nCount.VALUE32;
ADIJake 0:85855ecd3257 635
ADIJake 0:85855ecd3257 636 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 637 }
ADIJake 0:85855ecd3257 638
ADIJake 0:85855ecd3257 639 // DEBUG - TO BE DELETED
ADIJake 0:85855ecd3257 640 ADI_SENSE_RESULT adi_sense_SetExternalFlashIndex(
ADIJake 0:85855ecd3257 641 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 642 uint32_t nStartIndex)
ADIJake 0:85855ecd3257 643 {
ADIJake 0:85855ecd3257 644 WRITE_REG_U32(hDevice, nStartIndex, CORE_EXT_FLASH_INDEX);
ADIJake 0:85855ecd3257 645
ADIJake 0:85855ecd3257 646 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 647 }
ADIJake 0:85855ecd3257 648
ADIJake 0:85855ecd3257 649 /*
ADIJake 0:85855ecd3257 650 * Read a set of data samples stored in the device external flash memory.
ADIJake 0:85855ecd3257 651 * This may be called at any time.
ADIJake 0:85855ecd3257 652 */
ADIJake 0:85855ecd3257 653 ADI_SENSE_RESULT adi_sense_GetExternalFlashData(
ADIJake 0:85855ecd3257 654 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 655 ADI_SENSE_DATA_SAMPLE * const pSamples,
ADIJake 0:85855ecd3257 656 uint32_t const nStartIndex,
ADIJake 0:85855ecd3257 657 uint32_t const nRequested,
ADIJake 0:85855ecd3257 658 uint32_t * const pnReturned)
ADIJake 0:85855ecd3257 659 {
ADIJake 0:85855ecd3257 660 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 661 uint16_t command = ADI_SENSE_1000_HOST_COMMS_READ_CMD |
ADIJake 0:85855ecd3257 662 (REG_ADISENSE_CORE_EXT_FLASH_DATA & ADI_SENSE_1000_HOST_COMMS_ADR_MASK);
ADIJake 0:85855ecd3257 663 uint8_t commandData[2] = {
ADIJake 0:85855ecd3257 664 command >> 8,
ADIJake 0:85855ecd3257 665 command & 0xFF
ADIJake 0:85855ecd3257 666 };
ADIJake 0:85855ecd3257 667 uint8_t commandResponse[2];
ADIJake 0:85855ecd3257 668 unsigned nValidSamples = 0;
ADIJake 0:85855ecd3257 669 ADI_SENSE_RESULT eRet = ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 670
ADIJake 0:85855ecd3257 671 /* Setup initial sample */
ADIJake 0:85855ecd3257 672 WRITE_REG_U32(hDevice, nStartIndex, CORE_EXT_FLASH_INDEX);
ADIJake 0:85855ecd3257 673
ADIJake 0:85855ecd3257 674 /* Send flash read command */
ADIJake 0:85855ecd3257 675 do {
ADIJake 0:85855ecd3257 676 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
ADIJake 0:85855ecd3257 677 sizeof(command), false);
ADIJake 0:85855ecd3257 678 if (eRet)
ADIJake 0:85855ecd3257 679 {
ADIJake 0:85855ecd3257 680 ADI_SENSE_LOG_ERROR("Failed to send read command for external flash");
ADIJake 0:85855ecd3257 681 return eRet;
ADIJake 0:85855ecd3257 682 }
ADIJake 0:85855ecd3257 683
ADIJake 0:85855ecd3257 684 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 685 } while ((commandResponse[0] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_0) ||
ADIJake 0:85855ecd3257 686 (commandResponse[1] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_1));
ADIJake 0:85855ecd3257 687
ADIJake 0:85855ecd3257 688 /* Read samples from external flash memory */
ADIJake 0:85855ecd3257 689 for (unsigned i = 0; i < nRequested; i++)
ADIJake 0:85855ecd3257 690 {
ADIJake 0:85855ecd3257 691 ADI_SENSE_1000_Sensor_Result_t sensorResult;
ADIJake 0:85855ecd3257 692 bool bHoldCs = true;
ADIJake 0:85855ecd3257 693
ADIJake 0:85855ecd3257 694 /* Keep the CS signal asserted for all but the last sample */
ADIJake 0:85855ecd3257 695 if ((i + 1) == nRequested)
ADIJake 0:85855ecd3257 696 bHoldCs = false;
ADIJake 0:85855ecd3257 697
ADIJake 0:85855ecd3257 698 eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, (uint8_t *) (&sensorResult),
ADIJake 0:85855ecd3257 699 8, bHoldCs);
ADIJake 0:85855ecd3257 700 if (eRet)
ADIJake 0:85855ecd3257 701 {
ADIJake 0:85855ecd3257 702 ADI_SENSE_LOG_ERROR("Failed to read data from external flash");
ADIJake 0:85855ecd3257 703 return eRet;
ADIJake 0:85855ecd3257 704 }
ADIJake 0:85855ecd3257 705
ADIJake 0:85855ecd3257 706 ADI_SENSE_DATA_SAMPLE *pSample = &pSamples[nValidSamples];
ADIJake 0:85855ecd3257 707
ADIJake 0:85855ecd3257 708 pSample->status = (ADI_SENSE_DEVICE_STATUS_FLAGS)0;
ADIJake 0:85855ecd3257 709 if (sensorResult.Ch_Error)
ADIJake 0:85855ecd3257 710 pSample->status |= ADI_SENSE_DEVICE_STATUS_ERROR;
ADIJake 0:85855ecd3257 711 if (sensorResult.Ch_Alert)
ADIJake 0:85855ecd3257 712 pSample->status |= ADI_SENSE_DEVICE_STATUS_ALERT;
ADIJake 0:85855ecd3257 713
ADIJake 0:85855ecd3257 714 if (sensorResult.Ch_Raw)
ADIJake 0:85855ecd3257 715 pSample->rawValue = sensorResult.Raw_Sample;
ADIJake 0:85855ecd3257 716 else
ADIJake 0:85855ecd3257 717 pSample->rawValue = 0;
ADIJake 0:85855ecd3257 718
ADIJake 0:85855ecd3257 719 pSample->channelId = sensorResult.Channel_ID;
ADIJake 0:85855ecd3257 720 pSample->processedValue = sensorResult.Sensor_Result;
ADIJake 0:85855ecd3257 721
ADIJake 0:85855ecd3257 722 nValidSamples++;
ADIJake 0:85855ecd3257 723 }
ADIJake 0:85855ecd3257 724 *pnReturned = nValidSamples;
ADIJake 0:85855ecd3257 725
ADIJake 0:85855ecd3257 726 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 727
ADIJake 0:85855ecd3257 728 return eRet;
ADIJake 0:85855ecd3257 729 }
ADIJake 0:85855ecd3257 730
ADIJake 0:85855ecd3257 731
ADIJake 0:85855ecd3257 732 /*
ADIJake 0:85855ecd3257 733 * Store the LUT data to persistent memory on the device.
ADIJake 0:85855ecd3257 734 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 735 * Do not power down the device while this command is running.
ADIJake 0:85855ecd3257 736 */
ADIJake 0:85855ecd3257 737 ADI_SENSE_RESULT adi_sense_SaveLutData(
ADIJake 0:85855ecd3257 738 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 739 {
ADIJake 0:85855ecd3257 740 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_LUT, true);
ADIJake 0:85855ecd3257 741 }
ADIJake 0:85855ecd3257 742
ADIJake 0:85855ecd3257 743 /*
ADIJake 0:85855ecd3257 744 * Restore the LUT data from persistent memory on the device.
ADIJake 0:85855ecd3257 745 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 746 */
ADIJake 0:85855ecd3257 747 ADI_SENSE_RESULT adi_sense_RestoreLutData(
ADIJake 0:85855ecd3257 748 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 749 {
ADIJake 0:85855ecd3257 750 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_LUT, true);
ADIJake 0:85855ecd3257 751 }
ADIJake 0:85855ecd3257 752
ADIJake 0:85855ecd3257 753 /*
ADIJake 0:85855ecd3257 754 * Stop the measurement cycles on the device.
ADIJake 0:85855ecd3257 755 * To be used only if a measurement command is currently running.
ADIJake 0:85855ecd3257 756 */
ADIJake 0:85855ecd3257 757 ADI_SENSE_RESULT adi_sense_StopMeasurement(
ADIJake 0:85855ecd3257 758 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 759 {
ADIJake 0:85855ecd3257 760 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_NOP, true);
ADIJake 0:85855ecd3257 761 }
ADIJake 0:85855ecd3257 762
ADIJake 0:85855ecd3257 763 /*
ADIJake 0:85855ecd3257 764 * Run built-in diagnostic checks on the device.
ADIJake 0:85855ecd3257 765 * Diagnostics are executed according to the current applied settings.
ADIJake 0:85855ecd3257 766 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 767 */
ADIJake 0:85855ecd3257 768 ADI_SENSE_RESULT adi_sense_RunDiagnostics(
ADIJake 0:85855ecd3257 769 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 770 {
ADIJake 0:85855ecd3257 771 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_RUN_DIAGNOSTICS, true);
ADIJake 0:85855ecd3257 772 }
ADIJake 0:85855ecd3257 773
ADIJake 0:85855ecd3257 774 /*
ADIJake 0:85855ecd3257 775 * Run self-calibration routines on the device.
ADIJake 0:85855ecd3257 776 * Calibration is executed according to the current applied settings.
ADIJake 0:85855ecd3257 777 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 778 */
ADIJake 0:85855ecd3257 779 ADI_SENSE_RESULT adi_sense_RunCalibration(
ADIJake 0:85855ecd3257 780 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 781 {
ADIJake 0:85855ecd3257 782 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SELF_CALIBRATION, true);
ADIJake 0:85855ecd3257 783 }
ADIJake 0:85855ecd3257 784
ADIJake 0:85855ecd3257 785 /*
ADIJake 0:85855ecd3257 786 * Run digital calibration routines on the device.
ADIJake 0:85855ecd3257 787 * Calibration is executed according to the current applied settings.
ADIJake 0:85855ecd3257 788 * No other command must be running when this is called.
ADIJake 0:85855ecd3257 789 */
ADIJake 0:85855ecd3257 790 ADI_SENSE_RESULT adi_sense_RunDigitalCalibration(
ADIJake 0:85855ecd3257 791 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 792 {
ADIJake 0:85855ecd3257 793 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CALIBRATE_DIGITAL, true);
ADIJake 0:85855ecd3257 794 }
ADIJake 0:85855ecd3257 795
ADIJake 0:85855ecd3257 796 /*
ADIJake 0:85855ecd3257 797 * Read a set of data samples from the device.
ADIJake 0:85855ecd3257 798 * This may be called at any time.
ADIJake 0:85855ecd3257 799 */
ADIJake 0:85855ecd3257 800 ADI_SENSE_RESULT adi_sense_GetData(
ADIJake 0:85855ecd3257 801 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 802 ADI_SENSE_MEASUREMENT_MODE const eMeasurementMode,
ADIJake 0:85855ecd3257 803 ADI_SENSE_DATA_SAMPLE * const pSamples,
ADIJake 0:85855ecd3257 804 uint8_t const nBytesPerSample,
ADIJake 0:85855ecd3257 805 uint32_t const nRequested,
ADIJake 0:85855ecd3257 806 uint32_t * const pnReturned)
ADIJake 0:85855ecd3257 807 {
ADIJake 0:85855ecd3257 808 ADI_SENSE_1000_Sensor_Result_t sensorResult;
ADIJake 0:85855ecd3257 809 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 810 uint16_t command = ADI_SENSE_1000_HOST_COMMS_READ_CMD |
ADIJake 0:85855ecd3257 811 (REG_ADISENSE_CORE_DATA_FIFO & ADI_SENSE_1000_HOST_COMMS_ADR_MASK);
ADIJake 0:85855ecd3257 812 uint8_t commandData[2] = {
ADIJake 0:85855ecd3257 813 command >> 8,
ADIJake 0:85855ecd3257 814 command & 0xFF
ADIJake 0:85855ecd3257 815 };
ADIJake 0:85855ecd3257 816 uint8_t commandResponse[2];
ADIJake 0:85855ecd3257 817 unsigned nValidSamples = 0;
ADIJake 0:85855ecd3257 818 ADI_SENSE_RESULT eRet = ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 819
ADIJake 0:85855ecd3257 820 do {
ADIJake 0:85855ecd3257 821 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
ADIJake 0:85855ecd3257 822 sizeof(command), false);
ADIJake 0:85855ecd3257 823 if (eRet)
ADIJake 0:85855ecd3257 824 {
ADIJake 0:85855ecd3257 825 ADI_SENSE_LOG_ERROR("Failed to send read command for FIFO register");
ADIJake 0:85855ecd3257 826 return eRet;
ADIJake 0:85855ecd3257 827 }
ADIJake 0:85855ecd3257 828 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 829 } while ((commandResponse[0] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_0) ||
ADIJake 0:85855ecd3257 830 (commandResponse[1] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_1));
ADIJake 0:85855ecd3257 831
ADIJake 0:85855ecd3257 832 for (unsigned i = 0; i < nRequested; i++)
ADIJake 0:85855ecd3257 833 {
ADIJake 0:85855ecd3257 834 bool bHoldCs = true;
ADIJake 0:85855ecd3257 835
ADIJake 0:85855ecd3257 836 /* Keep the CS signal asserted for all but the last sample */
ADIJake 0:85855ecd3257 837 if ((i + 1) == nRequested)
ADIJake 0:85855ecd3257 838 bHoldCs = false;
ADIJake 0:85855ecd3257 839
ADIJake 0:85855ecd3257 840 getDataCnt++;
ADIJake 0:85855ecd3257 841
ADIJake 0:85855ecd3257 842 eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, &sensorResult,
ADIJake 0:85855ecd3257 843 nBytesPerSample, bHoldCs);
ADIJake 0:85855ecd3257 844 if (eRet)
ADIJake 0:85855ecd3257 845 {
ADIJake 0:85855ecd3257 846 ADI_SENSE_LOG_ERROR("Failed to read data from FIFO register");
ADIJake 0:85855ecd3257 847 return eRet;
ADIJake 0:85855ecd3257 848 }
ADIJake 0:85855ecd3257 849
ADIJake 0:85855ecd3257 850 if (! sensorResult.Ch_Valid)
ADIJake 0:85855ecd3257 851 {
ADIJake 0:85855ecd3257 852 /*
ADIJake 0:85855ecd3257 853 * Reading an invalid sample indicates that there are no
ADIJake 0:85855ecd3257 854 * more samples available or we've lost sync with the device.
ADIJake 0:85855ecd3257 855 * In the latter case, it might be recoverable, but return here
ADIJake 0:85855ecd3257 856 * to let the application check the device status and decide itself.
ADIJake 0:85855ecd3257 857 */
ADIJake 0:85855ecd3257 858 eRet = ADI_SENSE_INCOMPLETE;
ADIJake 0:85855ecd3257 859 break;
ADIJake 0:85855ecd3257 860 }
ADIJake 0:85855ecd3257 861
ADIJake 0:85855ecd3257 862 ADI_SENSE_DATA_SAMPLE *pSample = &pSamples[nValidSamples];
ADIJake 0:85855ecd3257 863
ADIJake 0:85855ecd3257 864 pSample->status = (ADI_SENSE_DEVICE_STATUS_FLAGS)0;
ADIJake 0:85855ecd3257 865 if (sensorResult.Ch_Error)
ADIJake 0:85855ecd3257 866 pSample->status |= ADI_SENSE_DEVICE_STATUS_ERROR;
ADIJake 0:85855ecd3257 867 if (sensorResult.Ch_Alert)
ADIJake 0:85855ecd3257 868 pSample->status |= ADI_SENSE_DEVICE_STATUS_ALERT;
ADIJake 0:85855ecd3257 869
ADIJake 0:85855ecd3257 870 if (sensorResult.Ch_Raw)
ADIJake 0:85855ecd3257 871 pSample->rawValue = sensorResult.Raw_Sample;
ADIJake 0:85855ecd3257 872 else
ADIJake 0:85855ecd3257 873 pSample->rawValue = 0;
ADIJake 0:85855ecd3257 874
ADIJake 0:85855ecd3257 875 pSample->channelId = sensorResult.Channel_ID;
ADIJake 0:85855ecd3257 876 pSample->processedValue = sensorResult.Sensor_Result;
ADIJake 0:85855ecd3257 877
ADIJake 0:85855ecd3257 878 nValidSamples++;
ADIJake 0:85855ecd3257 879
ADIJake 0:85855ecd3257 880 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 881 }
ADIJake 0:85855ecd3257 882 *pnReturned = nValidSamples;
ADIJake 0:85855ecd3257 883
ADIJake 0:85855ecd3257 884 return eRet;
ADIJake 0:85855ecd3257 885 }
ADIJake 0:85855ecd3257 886
ADIJake 0:85855ecd3257 887 /*
ADIJake 0:85855ecd3257 888 * Close the given ADISENSE device.
ADIJake 0:85855ecd3257 889 */
ADIJake 0:85855ecd3257 890 ADI_SENSE_RESULT adi_sense_Close(
ADIJake 0:85855ecd3257 891 ADI_SENSE_DEVICE_HANDLE const hDevice)
ADIJake 0:85855ecd3257 892 {
ADIJake 0:85855ecd3257 893 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 894
ADIJake 0:85855ecd3257 895 adi_sense_GpioClose(pCtx->hGpio);
ADIJake 0:85855ecd3257 896 adi_sense_SpiClose(pCtx->hSpi);
ADIJake 0:85855ecd3257 897 adi_sense_LogClose();
ADIJake 0:85855ecd3257 898
ADIJake 0:85855ecd3257 899 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 900 }
ADIJake 0:85855ecd3257 901
ADIJake 0:85855ecd3257 902 ADI_SENSE_RESULT adi_sense_1000_WriteRegister(
ADIJake 0:85855ecd3257 903 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 904 uint16_t nAddress,
ADIJake 0:85855ecd3257 905 void *pData,
ADIJake 0:85855ecd3257 906 unsigned nLength)
ADIJake 0:85855ecd3257 907 {
ADIJake 0:85855ecd3257 908 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 909 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 910 uint16_t command = ADI_SENSE_1000_HOST_COMMS_WRITE_CMD |
ADIJake 0:85855ecd3257 911 (nAddress & ADI_SENSE_1000_HOST_COMMS_ADR_MASK);
ADIJake 0:85855ecd3257 912 uint8_t commandData[2] = {
ADIJake 0:85855ecd3257 913 command >> 8,
ADIJake 0:85855ecd3257 914 command & 0xFF
ADIJake 0:85855ecd3257 915 };
ADIJake 0:85855ecd3257 916 uint8_t commandResponse[2];
ADIJake 0:85855ecd3257 917
ADIJake 0:85855ecd3257 918 do {
ADIJake 0:85855ecd3257 919 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
ADIJake 0:85855ecd3257 920 sizeof(command), false);
ADIJake 0:85855ecd3257 921 if (eRet)
ADIJake 0:85855ecd3257 922 {
ADIJake 0:85855ecd3257 923 ADI_SENSE_LOG_ERROR("Failed to send write command for register %u",
ADIJake 0:85855ecd3257 924 nAddress);
ADIJake 0:85855ecd3257 925 return eRet;
ADIJake 0:85855ecd3257 926 }
ADIJake 0:85855ecd3257 927
ADIJake 0:85855ecd3257 928 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 929 } while ((commandResponse[0] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_0) ||
ADIJake 0:85855ecd3257 930 (commandResponse[1] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_1));
ADIJake 0:85855ecd3257 931
ADIJake 0:85855ecd3257 932 eRet = adi_sense_SpiTransfer(pCtx->hSpi, pData, NULL, nLength, false);
ADIJake 0:85855ecd3257 933 if (eRet)
ADIJake 0:85855ecd3257 934 {
ADIJake 0:85855ecd3257 935 ADI_SENSE_LOG_ERROR("Failed to write data (%dB) to register %u",
ADIJake 0:85855ecd3257 936 nLength, nAddress);
ADIJake 0:85855ecd3257 937 return eRet;
ADIJake 0:85855ecd3257 938 }
ADIJake 0:85855ecd3257 939
ADIJake 0:85855ecd3257 940 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 941
ADIJake 0:85855ecd3257 942 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 943 }
ADIJake 0:85855ecd3257 944
ADIJake 0:85855ecd3257 945 ADI_SENSE_RESULT adi_sense_1000_ReadRegister(
ADIJake 0:85855ecd3257 946 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 947 uint16_t nAddress,
ADIJake 0:85855ecd3257 948 void *pData,
ADIJake 0:85855ecd3257 949 unsigned nLength)
ADIJake 0:85855ecd3257 950 {
ADIJake 0:85855ecd3257 951 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 952 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
ADIJake 0:85855ecd3257 953 uint16_t command = ADI_SENSE_1000_HOST_COMMS_READ_CMD |
ADIJake 0:85855ecd3257 954 (nAddress & ADI_SENSE_1000_HOST_COMMS_ADR_MASK);
ADIJake 0:85855ecd3257 955 uint8_t commandData[2] = {
ADIJake 0:85855ecd3257 956 command >> 8,
ADIJake 0:85855ecd3257 957 command & 0xFF
ADIJake 0:85855ecd3257 958 };
ADIJake 0:85855ecd3257 959 uint8_t commandResponse[2];
ADIJake 0:85855ecd3257 960
ADIJake 0:85855ecd3257 961 do {
ADIJake 0:85855ecd3257 962 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
ADIJake 0:85855ecd3257 963 sizeof(command), false);
ADIJake 0:85855ecd3257 964 if (eRet)
ADIJake 0:85855ecd3257 965 {
ADIJake 0:85855ecd3257 966 ADI_SENSE_LOG_ERROR("Failed to send read command for register %u",
ADIJake 0:85855ecd3257 967 nAddress);
ADIJake 0:85855ecd3257 968 return eRet;
ADIJake 0:85855ecd3257 969 }
ADIJake 0:85855ecd3257 970
ADIJake 0:85855ecd3257 971 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 972 } while ((commandResponse[0] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_0) ||
ADIJake 0:85855ecd3257 973 (commandResponse[1] != ADI_SENSE_1000_HOST_COMMS_CMD_RESP_1));
ADIJake 0:85855ecd3257 974
ADIJake 0:85855ecd3257 975 eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, pData, nLength, false);
ADIJake 0:85855ecd3257 976 if (eRet)
ADIJake 0:85855ecd3257 977 {
ADIJake 0:85855ecd3257 978 ADI_SENSE_LOG_ERROR("Failed to read data (%uB) from register %u",
ADIJake 0:85855ecd3257 979 nLength, nAddress);
ADIJake 0:85855ecd3257 980 return eRet;
ADIJake 0:85855ecd3257 981 }
ADIJake 0:85855ecd3257 982
ADIJake 0:85855ecd3257 983 adi_sense_TimeDelayUsec(ADI_SENSE_1000_HOST_COMMS_XFER_DELAY);
ADIJake 0:85855ecd3257 984
ADIJake 0:85855ecd3257 985 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 986 }
ADIJake 0:85855ecd3257 987
ADIJake 0:85855ecd3257 988 ADI_SENSE_RESULT adi_sense_GetDeviceReadyState(
ADIJake 0:85855ecd3257 989 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 990 bool * const bReady)
ADIJake 0:85855ecd3257 991 {
ADIJake 0:85855ecd3257 992 ADI_ADISENSE_SPI_Chip_Type_t chipTypeReg;
ADIJake 0:85855ecd3257 993
ADIJake 0:85855ecd3257 994 READ_REG_U8(hDevice, chipTypeReg.VALUE8, SPI_CHIP_TYPE);
ADIJake 0:85855ecd3257 995 /* If we read this register successfully, assume the device is ready */
ADIJake 0:85855ecd3257 996 *bReady = (chipTypeReg.VALUE8 == REG_ADISENSE_SPI_CHIP_TYPE_RESET);
ADIJake 0:85855ecd3257 997
ADIJake 0:85855ecd3257 998 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 999 }
ADIJake 0:85855ecd3257 1000
ADIJake 0:85855ecd3257 1001 ADI_SENSE_RESULT adi_sense_1000_GetDataReadyModeInfo(
ADIJake 0:85855ecd3257 1002 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 1003 ADI_SENSE_MEASUREMENT_MODE const eMeasurementMode,
ADIJake 0:85855ecd3257 1004 ADI_SENSE_1000_OPERATING_MODE * const peOperatingMode,
ADIJake 0:85855ecd3257 1005 ADI_SENSE_1000_DATAREADY_MODE * const peDataReadyMode,
ADIJake 0:85855ecd3257 1006 uint32_t * const pnSamplesPerDataready,
ADIJake 0:85855ecd3257 1007 uint32_t * const pnSamplesPerCycle,
ADIJake 0:85855ecd3257 1008 uint8_t * const pnBytesPerSample)
ADIJake 0:85855ecd3257 1009 {
ADIJake 0:85855ecd3257 1010 unsigned nChannelsEnabled = 0;
ADIJake 0:85855ecd3257 1011 unsigned nSamplesPerCycle = 0;
ADIJake 0:85855ecd3257 1012
ADIJake 0:85855ecd3257 1013 ADI_ADISENSE_CORE_Mode_t modeReg;
ADIJake 0:85855ecd3257 1014 READ_REG_U8(hDevice, modeReg.VALUE8, CORE_MODE);
ADIJake 0:85855ecd3257 1015
ADIJake 0:85855ecd3257 1016 if ((eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK) ||
ADIJake 0:85855ecd3257 1017 (modeReg.Conversion_Mode == ADISENSE_CORE_MODE_SINGLECYCLE))
ADIJake 0:85855ecd3257 1018 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE;
ADIJake 0:85855ecd3257 1019 else if (modeReg.Conversion_Mode == ADISENSE_CORE_MODE_MULTICYCLE)
ADIJake 0:85855ecd3257 1020 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE;
ADIJake 0:85855ecd3257 1021 else
ADIJake 0:85855ecd3257 1022 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_CONTINUOUS;
ADIJake 0:85855ecd3257 1023
ADIJake 0:85855ecd3257 1024
ADIJake 0:85855ecd3257 1025 /* FFT mode is quite different to the other modes:
ADIJake 0:85855ecd3257 1026 * - Each FFT result produces a batch of samples
ADIJake 0:85855ecd3257 1027 * - The size of the batch depends on selected FFT size and output config options
ADIJake 0:85855ecd3257 1028 * - DATAREADY will fire for each FFT result (once per channel)
ADIJake 0:85855ecd3257 1029 * - The size of the cycle depends on the number of channels enabled for FFT
ADIJake 0:85855ecd3257 1030 */
ADIJake 0:85855ecd3257 1031 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_FFT)
ADIJake 0:85855ecd3257 1032 {
ADIJake 0:85855ecd3257 1033 ADI_ADISENSE_CORE_FFT_Config_t fftConfigReg;
ADIJake 0:85855ecd3257 1034
ADIJake 0:85855ecd3257 1035 unsigned nFftChannels;
ADIJake 0:85855ecd3257 1036 unsigned nSamplesPerChannel;
ADIJake 0:85855ecd3257 1037
ADIJake 0:85855ecd3257 1038 READ_REG_U32(hDevice, fftConfigReg.VALUE32, CORE_FFT_CONFIG);
ADIJake 0:85855ecd3257 1039
ADIJake 0:85855ecd3257 1040 nFftChannels = fftConfigReg.FFT_Num_Channels + 1;
ADIJake 0:85855ecd3257 1041
ADIJake 0:85855ecd3257 1042 if (fftConfigReg.FFT_Output == ADISENSE_CORE_FFT_CONFIG_FFT_OUTPUT_MAX16)
ADIJake 0:85855ecd3257 1043 {
ADIJake 0:85855ecd3257 1044 nSamplesPerChannel = 16;
ADIJake 0:85855ecd3257 1045 *pnBytesPerSample = 8;
ADIJake 0:85855ecd3257 1046 }
ADIJake 0:85855ecd3257 1047 else if (fftConfigReg.FFT_Output == ADISENSE_CORE_FFT_CONFIG_FFT_OUTPUT_FULL)
ADIJake 0:85855ecd3257 1048 {
ADIJake 0:85855ecd3257 1049 nSamplesPerChannel = (256 << fftConfigReg.FFT_Num_Bins) >> 1;
ADIJake 0:85855ecd3257 1050 *pnBytesPerSample = 5;
ADIJake 0:85855ecd3257 1051 }
ADIJake 0:85855ecd3257 1052 else if (fftConfigReg.FFT_Output == ADISENSE_CORE_FFT_CONFIG_FFT_OUTPUT_FULL_WITH_RAW)
ADIJake 0:85855ecd3257 1053 {
ADIJake 0:85855ecd3257 1054 nSamplesPerChannel = (256 << fftConfigReg.FFT_Num_Bins);
ADIJake 0:85855ecd3257 1055 *pnBytesPerSample = 8;
ADIJake 0:85855ecd3257 1056 }
ADIJake 0:85855ecd3257 1057 else
ADIJake 0:85855ecd3257 1058 {
ADIJake 0:85855ecd3257 1059 ADI_SENSE_LOG_ERROR("Invalid FFT output format option %d configured",
ADIJake 0:85855ecd3257 1060 fftConfigReg.FFT_Output);
ADIJake 0:85855ecd3257 1061 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1062 }
ADIJake 0:85855ecd3257 1063
ADIJake 0:85855ecd3257 1064 *pnSamplesPerDataready = nSamplesPerChannel;
ADIJake 0:85855ecd3257 1065 *pnSamplesPerCycle = nSamplesPerChannel * nFftChannels;
ADIJake 0:85855ecd3257 1066
ADIJake 0:85855ecd3257 1067 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_CYCLE;
ADIJake 0:85855ecd3257 1068
ADIJake 0:85855ecd3257 1069 if (modeReg.FFT_Mode == ADISENSE_CORE_MODE_FFT_MODE_CONTINUOUS)
ADIJake 0:85855ecd3257 1070 {
ADIJake 0:85855ecd3257 1071 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_CONTINUOUS;
ADIJake 0:85855ecd3257 1072 }
ADIJake 0:85855ecd3257 1073 else
ADIJake 0:85855ecd3257 1074 {
ADIJake 0:85855ecd3257 1075 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE;
ADIJake 0:85855ecd3257 1076 }
ADIJake 0:85855ecd3257 1077 }
ADIJake 0:85855ecd3257 1078 else
ADIJake 0:85855ecd3257 1079 {
ADIJake 0:85855ecd3257 1080 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_OMIT_RAW)
ADIJake 0:85855ecd3257 1081 {
ADIJake 0:85855ecd3257 1082 *pnBytesPerSample = 5;
ADIJake 0:85855ecd3257 1083 }
ADIJake 0:85855ecd3257 1084 else
ADIJake 0:85855ecd3257 1085 {
ADIJake 0:85855ecd3257 1086 *pnBytesPerSample = 8;
ADIJake 0:85855ecd3257 1087 }
ADIJake 0:85855ecd3257 1088
ADIJake 0:85855ecd3257 1089 for (ADI_SENSE_1000_CHANNEL_ID chId = ADI_SENSE_1000_CHANNEL_ID_CJC_0;
ADIJake 0:85855ecd3257 1090 chId < ADI_SENSE_1000_MAX_CHANNELS;
ADIJake 0:85855ecd3257 1091 chId++)
ADIJake 0:85855ecd3257 1092 {
ADIJake 0:85855ecd3257 1093 ADI_ADISENSE_CORE_Sensor_Details_t sensorDetailsReg;
ADIJake 0:85855ecd3257 1094 ADI_ADISENSE_CORE_Channel_Count_t channelCountReg;
ADIJake 0:85855ecd3257 1095
ADIJake 0:85855ecd3257 1096 if (ADI_SENSE_1000_CHANNEL_IS_VIRTUAL(chId))
ADIJake 0:85855ecd3257 1097 continue;
ADIJake 0:85855ecd3257 1098
ADIJake 0:85855ecd3257 1099 READ_REG_U8(hDevice, channelCountReg.VALUE8, CORE_CHANNEL_COUNTn(chId));
ADIJake 0:85855ecd3257 1100 READ_REG_U32(hDevice, sensorDetailsReg.VALUE32, CORE_SENSOR_DETAILSn(chId));
ADIJake 0:85855ecd3257 1101
ADIJake 0:85855ecd3257 1102 if (channelCountReg.Channel_Enable && !sensorDetailsReg.Do_Not_Publish)
ADIJake 0:85855ecd3257 1103 {
ADIJake 0:85855ecd3257 1104 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
ADIJake 0:85855ecd3257 1105 unsigned nActualChannels = 1;
ADIJake 0:85855ecd3257 1106
ADIJake 0:85855ecd3257 1107 READ_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(chId));
ADIJake 0:85855ecd3257 1108
ADIJake 0:85855ecd3257 1109 if (chId == ADI_SENSE_1000_CHANNEL_ID_SPI_0)
ADIJake 0:85855ecd3257 1110 {
ADIJake 0:85855ecd3257 1111 /* Some sensors automatically generate samples on additional "virtual" channels
ADIJake 0:85855ecd3257 1112 * so these channels must be counted as active when those sensors are selected
ADIJake 0:85855ecd3257 1113 * and we use the count from the corresponding "physical" channel */
ADIJake 0:85855ecd3257 1114 if ((sensorTypeReg.Sensor_Type >=
ADIJake 0:85855ecd3257 1115 ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_ACCELEROMETER_A_DEF_L1) &&
ADIJake 0:85855ecd3257 1116 (sensorTypeReg.Sensor_Type <=
ADIJake 0:85855ecd3257 1117 ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_ACCELEROMETER_B_ADV_L2))
ADIJake 0:85855ecd3257 1118 nActualChannels += 2;
ADIJake 0:85855ecd3257 1119 }
ADIJake 0:85855ecd3257 1120
ADIJake 0:85855ecd3257 1121 nChannelsEnabled += nActualChannels;
ADIJake 0:85855ecd3257 1122 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK)
ADIJake 0:85855ecd3257 1123 /* Assume a single sample per channel in test mode */
ADIJake 0:85855ecd3257 1124 nSamplesPerCycle += nActualChannels;
ADIJake 0:85855ecd3257 1125 else
ADIJake 0:85855ecd3257 1126 nSamplesPerCycle += nActualChannels *
ADIJake 0:85855ecd3257 1127 (channelCountReg.Channel_Count + 1);
ADIJake 0:85855ecd3257 1128 }
ADIJake 0:85855ecd3257 1129 }
ADIJake 0:85855ecd3257 1130
ADIJake 0:85855ecd3257 1131 if (nChannelsEnabled == 0)
ADIJake 0:85855ecd3257 1132 {
ADIJake 0:85855ecd3257 1133 *pnSamplesPerDataready = 0;
ADIJake 0:85855ecd3257 1134 *pnSamplesPerCycle = 0;
ADIJake 0:85855ecd3257 1135 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1136 }
ADIJake 0:85855ecd3257 1137
ADIJake 0:85855ecd3257 1138 *pnSamplesPerCycle = nSamplesPerCycle;
ADIJake 0:85855ecd3257 1139
ADIJake 0:85855ecd3257 1140 if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CONVERSION)
ADIJake 0:85855ecd3257 1141 {
ADIJake 0:85855ecd3257 1142 *pnSamplesPerDataready = 1;
ADIJake 0:85855ecd3257 1143 }
ADIJake 0:85855ecd3257 1144 else if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CYCLE)
ADIJake 0:85855ecd3257 1145 {
ADIJake 0:85855ecd3257 1146 *pnSamplesPerDataready = nSamplesPerCycle;
ADIJake 0:85855ecd3257 1147 }
ADIJake 0:85855ecd3257 1148 else
ADIJake 0:85855ecd3257 1149 {
ADIJake 0:85855ecd3257 1150 /* Assume DRDY will be asserted after max. 1 cycle in test mode */
ADIJake 0:85855ecd3257 1151 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK)
ADIJake 0:85855ecd3257 1152 {
ADIJake 0:85855ecd3257 1153 *pnSamplesPerDataready = nSamplesPerCycle;
ADIJake 0:85855ecd3257 1154 }
ADIJake 0:85855ecd3257 1155 else
ADIJake 0:85855ecd3257 1156 {
ADIJake 0:85855ecd3257 1157 ADI_ADISENSE_CORE_Fifo_Num_Cycles_t fifoNumCyclesReg;
ADIJake 0:85855ecd3257 1158 READ_REG_U8(hDevice, fifoNumCyclesReg.VALUE8, CORE_FIFO_NUM_CYCLES);
ADIJake 0:85855ecd3257 1159
ADIJake 0:85855ecd3257 1160 *pnSamplesPerDataready =
ADIJake 0:85855ecd3257 1161 nSamplesPerCycle * fifoNumCyclesReg.Fifo_Num_Cycles;
ADIJake 0:85855ecd3257 1162 }
ADIJake 0:85855ecd3257 1163 }
ADIJake 0:85855ecd3257 1164
ADIJake 0:85855ecd3257 1165 if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CONVERSION)
ADIJake 0:85855ecd3257 1166 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_CONVERSION;
ADIJake 0:85855ecd3257 1167 else if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CYCLE)
ADIJake 0:85855ecd3257 1168 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_CYCLE;
ADIJake 0:85855ecd3257 1169 else
ADIJake 0:85855ecd3257 1170 {
ADIJake 0:85855ecd3257 1171 /* Assume DRDY will be asserted after max. 1 cycle in test mode */
ADIJake 0:85855ecd3257 1172 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK)
ADIJake 0:85855ecd3257 1173 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_CYCLE;
ADIJake 0:85855ecd3257 1174 else
ADIJake 0:85855ecd3257 1175 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_MULTICYCLE_BURST;
ADIJake 0:85855ecd3257 1176 }
ADIJake 0:85855ecd3257 1177 }
ADIJake 0:85855ecd3257 1178
ADIJake 0:85855ecd3257 1179 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1180 }
ADIJake 0:85855ecd3257 1181
ADIJake 0:85855ecd3257 1182 ADI_SENSE_RESULT adi_sense_GetProductID(
ADIJake 0:85855ecd3257 1183 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1184 ADI_SENSE_PRODUCT_ID *pProductId)
ADIJake 0:85855ecd3257 1185 {
ADIJake 0:85855ecd3257 1186 ADI_ADISENSE_SPI_Product_ID_L_t productIdLoReg;
ADIJake 0:85855ecd3257 1187 ADI_ADISENSE_SPI_Product_ID_H_t productIdHiReg;
ADIJake 0:85855ecd3257 1188
ADIJake 0:85855ecd3257 1189 READ_REG_U8(hDevice, productIdLoReg.VALUE8, SPI_PRODUCT_ID_L);
ADIJake 0:85855ecd3257 1190 READ_REG_U8(hDevice, productIdHiReg.VALUE8, SPI_PRODUCT_ID_H);
ADIJake 0:85855ecd3257 1191
ADIJake 0:85855ecd3257 1192 *pProductId = (ADI_SENSE_PRODUCT_ID)((productIdHiReg.VALUE8 << 8)
ADIJake 0:85855ecd3257 1193 | productIdLoReg.VALUE8);
ADIJake 0:85855ecd3257 1194 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1195 }
ADIJake 0:85855ecd3257 1196
ADIJake 0:85855ecd3257 1197 static ADI_SENSE_RESULT adi_sense_SetPowerMode(
ADIJake 0:85855ecd3257 1198 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1199 ADI_SENSE_1000_POWER_MODE powerMode)
ADIJake 0:85855ecd3257 1200 {
ADIJake 0:85855ecd3257 1201 ADI_ADISENSE_CORE_Power_Config_t powerConfigReg;
ADIJake 0:85855ecd3257 1202
ADIJake 0:85855ecd3257 1203 if (powerMode == ADI_SENSE_1000_POWER_MODE_LOW)
ADIJake 0:85855ecd3257 1204 {
ADIJake 0:85855ecd3257 1205 powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_LOW_POWER;
ADIJake 0:85855ecd3257 1206 }
ADIJake 0:85855ecd3257 1207 else if (powerMode == ADI_SENSE_1000_POWER_MODE_MID)
ADIJake 0:85855ecd3257 1208 {
ADIJake 0:85855ecd3257 1209 powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_MID_POWER;
ADIJake 0:85855ecd3257 1210 }
ADIJake 0:85855ecd3257 1211 else if (powerMode == ADI_SENSE_1000_POWER_MODE_FULL)
ADIJake 0:85855ecd3257 1212 {
ADIJake 0:85855ecd3257 1213 powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_FULL_POWER;
ADIJake 0:85855ecd3257 1214 }
ADIJake 0:85855ecd3257 1215 else
ADIJake 0:85855ecd3257 1216 {
ADIJake 0:85855ecd3257 1217 ADI_SENSE_LOG_ERROR("Invalid power mode %d specified", powerMode);
ADIJake 0:85855ecd3257 1218 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1219 }
ADIJake 0:85855ecd3257 1220
ADIJake 0:85855ecd3257 1221 WRITE_REG_U8(hDevice, powerConfigReg.VALUE8, CORE_POWER_CONFIG);
ADIJake 0:85855ecd3257 1222
ADIJake 0:85855ecd3257 1223 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1224 }
ADIJake 0:85855ecd3257 1225
ADIJake 0:85855ecd3257 1226 ADI_SENSE_RESULT adi_sense_1000_SetPowerConfig(
ADIJake 0:85855ecd3257 1227 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1228 ADI_SENSE_1000_POWER_CONFIG *pPowerConfig)
ADIJake 0:85855ecd3257 1229 {
ADIJake 0:85855ecd3257 1230 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 1231
ADIJake 0:85855ecd3257 1232 eRet = adi_sense_SetPowerMode(hDevice, pPowerConfig->powerMode);
ADIJake 0:85855ecd3257 1233 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 1234 {
ADIJake 0:85855ecd3257 1235 ADI_SENSE_LOG_ERROR("Failed to set power mode");
ADIJake 0:85855ecd3257 1236 return eRet;
ADIJake 0:85855ecd3257 1237 }
ADIJake 0:85855ecd3257 1238
ADIJake 0:85855ecd3257 1239 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1240 }
ADIJake 0:85855ecd3257 1241
ADIJake 0:85855ecd3257 1242 static ADI_SENSE_RESULT adi_sense_SetMode(
ADIJake 0:85855ecd3257 1243 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1244 ADI_SENSE_1000_OPERATING_MODE eOperatingMode,
ADIJake 0:85855ecd3257 1245 ADI_SENSE_1000_DATAREADY_MODE eDataReadyMode,
ADIJake 0:85855ecd3257 1246 ADI_SENSE_1000_CALIBRATION_MODE eCalibrationMode,
ADIJake 0:85855ecd3257 1247 bool bEnableExtFlash)
ADIJake 0:85855ecd3257 1248 {
ADIJake 0:85855ecd3257 1249 ADI_ADISENSE_CORE_Mode_t modeReg;
ADIJake 0:85855ecd3257 1250
ADIJake 0:85855ecd3257 1251 modeReg.VALUE8 = REG_RESET_VAL(CORE_MODE);
ADIJake 0:85855ecd3257 1252
ADIJake 0:85855ecd3257 1253 if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE)
ADIJake 0:85855ecd3257 1254 {
ADIJake 0:85855ecd3257 1255 modeReg.Conversion_Mode = ADISENSE_CORE_MODE_SINGLECYCLE;
ADIJake 0:85855ecd3257 1256 }
ADIJake 0:85855ecd3257 1257 else if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_CONTINUOUS)
ADIJake 0:85855ecd3257 1258 {
ADIJake 0:85855ecd3257 1259 modeReg.Conversion_Mode = ADISENSE_CORE_MODE_CONTINUOUS;
ADIJake 0:85855ecd3257 1260 }
ADIJake 0:85855ecd3257 1261 else if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE)
ADIJake 0:85855ecd3257 1262 {
ADIJake 0:85855ecd3257 1263 modeReg.Conversion_Mode = ADISENSE_CORE_MODE_MULTICYCLE;
ADIJake 0:85855ecd3257 1264 }
ADIJake 0:85855ecd3257 1265 else
ADIJake 0:85855ecd3257 1266 {
ADIJake 0:85855ecd3257 1267 ADI_SENSE_LOG_ERROR("Invalid operating mode %d specified",
ADIJake 0:85855ecd3257 1268 eOperatingMode);
ADIJake 0:85855ecd3257 1269 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1270 }
ADIJake 0:85855ecd3257 1271
ADIJake 0:85855ecd3257 1272 if (eDataReadyMode == ADI_SENSE_1000_DATAREADY_PER_CONVERSION)
ADIJake 0:85855ecd3257 1273 {
ADIJake 0:85855ecd3257 1274 modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_CONVERSION;
ADIJake 0:85855ecd3257 1275 }
ADIJake 0:85855ecd3257 1276 else if (eDataReadyMode == ADI_SENSE_1000_DATAREADY_PER_CYCLE)
ADIJake 0:85855ecd3257 1277 {
ADIJake 0:85855ecd3257 1278 modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_CYCLE;
ADIJake 0:85855ecd3257 1279 }
ADIJake 0:85855ecd3257 1280 else if (eDataReadyMode == ADI_SENSE_1000_DATAREADY_PER_MULTICYCLE_BURST)
ADIJake 0:85855ecd3257 1281 {
ADIJake 0:85855ecd3257 1282 if (eOperatingMode != ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE)
ADIJake 0:85855ecd3257 1283 {
ADIJake 0:85855ecd3257 1284 ADI_SENSE_LOG_ERROR(
ADIJake 0:85855ecd3257 1285 "Data-ready mode %d cannot be used with operating mode %d",
ADIJake 0:85855ecd3257 1286 eDataReadyMode, eOperatingMode);
ADIJake 0:85855ecd3257 1287 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1288 }
ADIJake 0:85855ecd3257 1289 else
ADIJake 0:85855ecd3257 1290 {
ADIJake 0:85855ecd3257 1291 modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_FIFO_FILL;
ADIJake 0:85855ecd3257 1292 }
ADIJake 0:85855ecd3257 1293 }
ADIJake 0:85855ecd3257 1294 else
ADIJake 0:85855ecd3257 1295 {
ADIJake 0:85855ecd3257 1296 ADI_SENSE_LOG_ERROR("Invalid data-ready mode %d specified", eDataReadyMode);
ADIJake 0:85855ecd3257 1297 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1298 }
ADIJake 0:85855ecd3257 1299
ADIJake 0:85855ecd3257 1300 if (eCalibrationMode == ADI_SENSE_1000_NO_CALIBRATION)
ADIJake 0:85855ecd3257 1301 {
ADIJake 0:85855ecd3257 1302 modeReg.Calibration_Method = ADISENSE_CORE_MODE_NO_CAL;
ADIJake 0:85855ecd3257 1303 }
ADIJake 0:85855ecd3257 1304 else if (eCalibrationMode == ADI_SENSE_1000_DO_CALIBRATION)
ADIJake 0:85855ecd3257 1305 {
ADIJake 0:85855ecd3257 1306 modeReg.Calibration_Method = ADISENSE_CORE_MODE_DO_CAL;
ADIJake 0:85855ecd3257 1307 }
ADIJake 0:85855ecd3257 1308 else
ADIJake 0:85855ecd3257 1309 {
ADIJake 0:85855ecd3257 1310 ADI_SENSE_LOG_ERROR("Invalid calibration mode %d specified",
ADIJake 0:85855ecd3257 1311 eCalibrationMode);
ADIJake 0:85855ecd3257 1312 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1313 }
ADIJake 0:85855ecd3257 1314
ADIJake 0:85855ecd3257 1315 modeReg.Ext_Flash_Store = (bEnableExtFlash ?
ADIJake 0:85855ecd3257 1316 ADISENSE_CORE_MODE_EXT_FLASH_USED :
ADIJake 0:85855ecd3257 1317 ADISENSE_CORE_MODE_EXT_FLASH_NOT_USED);
ADIJake 0:85855ecd3257 1318
ADIJake 0:85855ecd3257 1319 WRITE_REG_U8(hDevice, modeReg.VALUE8, CORE_MODE);
ADIJake 0:85855ecd3257 1320
ADIJake 0:85855ecd3257 1321 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1322 }
ADIJake 0:85855ecd3257 1323
ADIJake 0:85855ecd3257 1324 ADI_SENSE_RESULT adi_sense_SetCycleControl(
ADIJake 0:85855ecd3257 1325 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1326 uint32_t nCycleInterval,
ADIJake 0:85855ecd3257 1327
ADIJake 0:85855ecd3257 1328 #ifdef __V2_3_CFG_FMT__
ADIJake 0:85855ecd3257 1329 ADI_SENSE_1000_CYCLE_TYPE eCycleType,
ADIJake 0:85855ecd3257 1330 ADI_SENSE_1000_FILTER_SETTLING eFilterSettling)
ADIJake 0:85855ecd3257 1331 #else
ADIJake 0:85855ecd3257 1332 ADI_SENSE_1000_CYCLE_TYPE eCycleType)
ADIJake 0:85855ecd3257 1333 #endif
ADIJake 0:85855ecd3257 1334 {
ADIJake 0:85855ecd3257 1335 ADI_ADISENSE_CORE_Cycle_Control_t cycleControlReg;
ADIJake 0:85855ecd3257 1336
ADIJake 0:85855ecd3257 1337 cycleControlReg.VALUE16 = REG_RESET_VAL(CORE_CYCLE_CONTROL);
ADIJake 0:85855ecd3257 1338
ADIJake 0:85855ecd3257 1339 if (nCycleInterval < (1 << 12))
ADIJake 0:85855ecd3257 1340 {
ADIJake 0:85855ecd3257 1341 cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_MICROSECONDS;
ADIJake 0:85855ecd3257 1342 }
ADIJake 0:85855ecd3257 1343 else if (nCycleInterval < (1000 * (1 << 12)))
ADIJake 0:85855ecd3257 1344 {
ADIJake 0:85855ecd3257 1345 cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_MILLISECONDS;
ADIJake 0:85855ecd3257 1346 nCycleInterval /= 1000;
ADIJake 0:85855ecd3257 1347 }
ADIJake 0:85855ecd3257 1348 else
ADIJake 0:85855ecd3257 1349 {
ADIJake 0:85855ecd3257 1350 cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_SECONDS;
ADIJake 0:85855ecd3257 1351 nCycleInterval /= 1000000;
ADIJake 0:85855ecd3257 1352 }
ADIJake 0:85855ecd3257 1353
ADIJake 0:85855ecd3257 1354 CHECK_REG_FIELD_VAL(CORE_CYCLE_CONTROL_CYCLE_TIME, nCycleInterval);
ADIJake 0:85855ecd3257 1355 cycleControlReg.Cycle_Time = nCycleInterval;
ADIJake 0:85855ecd3257 1356
ADIJake 0:85855ecd3257 1357 if (eCycleType == ADI_SENSE_1000_CYCLE_TYPE_SWITCH)
ADIJake 0:85855ecd3257 1358 {
ADIJake 0:85855ecd3257 1359 cycleControlReg.Cycle_Type = ADISENSE_CORE_CYCLE_CONTROL_CYCLE_TYPE_SWITCH;
ADIJake 0:85855ecd3257 1360 }
ADIJake 0:85855ecd3257 1361 else if (eCycleType == ADI_SENSE_1000_CYCLE_TYPE_FULL)
ADIJake 0:85855ecd3257 1362 {
ADIJake 0:85855ecd3257 1363 cycleControlReg.Cycle_Type = ADISENSE_CORE_CYCLE_CONTROL_CYCLE_TYPE_FULL;
ADIJake 0:85855ecd3257 1364 }
ADIJake 0:85855ecd3257 1365 else
ADIJake 0:85855ecd3257 1366 {
ADIJake 0:85855ecd3257 1367 ADI_SENSE_LOG_ERROR("Invalid cycle type %d specified", eCycleType);
ADIJake 0:85855ecd3257 1368 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1369 }
ADIJake 0:85855ecd3257 1370
ADIJake 0:85855ecd3257 1371 #ifdef __V2_3_CFG_FMT__
ADIJake 0:85855ecd3257 1372 if (eFilterSettling == ADI_SENSE_1000_FILTER_SETTLING_ALWAYS)
ADIJake 0:85855ecd3257 1373 {
ADIJake 0:85855ecd3257 1374 cycleControlReg.Filter_Settling = ADISENSE_CORE_CYCLE_CONTROL_FILTER_SETTLING_SETTLED;
ADIJake 0:85855ecd3257 1375 }
ADIJake 0:85855ecd3257 1376 else if (eFilterSettling == ADI_SENSE_1000_FILTER_SETTLING_FAST)
ADIJake 0:85855ecd3257 1377 {
ADIJake 0:85855ecd3257 1378 cycleControlReg.Filter_Settling = ADISENSE_CORE_CYCLE_CONTROL_FILTER_SETTLING_FAST;
ADIJake 0:85855ecd3257 1379 }
ADIJake 0:85855ecd3257 1380 else
ADIJake 0:85855ecd3257 1381 {
ADIJake 0:85855ecd3257 1382 ADI_SENSE_LOG_ERROR("Invalid filter settling option %d specified", eFilterSettling);
ADIJake 0:85855ecd3257 1383 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1384 }
ADIJake 0:85855ecd3257 1385 #endif
ADIJake 0:85855ecd3257 1386
ADIJake 0:85855ecd3257 1387 WRITE_REG_U16(hDevice, cycleControlReg.VALUE16, CORE_CYCLE_CONTROL);
ADIJake 0:85855ecd3257 1388
ADIJake 0:85855ecd3257 1389 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1390 }
ADIJake 0:85855ecd3257 1391
ADIJake 0:85855ecd3257 1392 static ADI_SENSE_RESULT adi_sense_SetMultiCycleConfig(
ADIJake 0:85855ecd3257 1393 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1394 ADI_SENSE_1000_MULTICYCLE_CONFIG *pMultiCycleConfig)
ADIJake 0:85855ecd3257 1395 {
ADIJake 0:85855ecd3257 1396 CHECK_REG_FIELD_VAL(CORE_FIFO_NUM_CYCLES_FIFO_NUM_CYCLES,
ADIJake 0:85855ecd3257 1397 pMultiCycleConfig->cyclesPerBurst);
ADIJake 0:85855ecd3257 1398
ADIJake 0:85855ecd3257 1399 WRITE_REG_U8(hDevice, pMultiCycleConfig->cyclesPerBurst,
ADIJake 0:85855ecd3257 1400 CORE_FIFO_NUM_CYCLES);
ADIJake 0:85855ecd3257 1401
ADIJake 0:85855ecd3257 1402 WRITE_REG_U32(hDevice, pMultiCycleConfig->burstInterval,
ADIJake 0:85855ecd3257 1403 CORE_MULTI_CYCLE_REPEAT_INTERVAL);
ADIJake 0:85855ecd3257 1404
ADIJake 0:85855ecd3257 1405 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1406 }
ADIJake 0:85855ecd3257 1407
ADIJake 0:85855ecd3257 1408 static ADI_SENSE_RESULT adi_sense_SetExternalReferenceValues(
ADIJake 0:85855ecd3257 1409 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1410 float32_t externalRef1Value,
ADIJake 0:85855ecd3257 1411 float32_t externalRef2Value)
ADIJake 0:85855ecd3257 1412 {
ADIJake 0:85855ecd3257 1413 WRITE_REG_FLOAT(hDevice, externalRef1Value, CORE_EXTERNAL_REFERENCE1);
ADIJake 0:85855ecd3257 1414 WRITE_REG_FLOAT(hDevice, externalRef2Value, CORE_EXTERNAL_REFERENCE2);
ADIJake 0:85855ecd3257 1415
ADIJake 0:85855ecd3257 1416 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1417 }
ADIJake 0:85855ecd3257 1418
ADIJake 0:85855ecd3257 1419 ADI_SENSE_RESULT adi_sense_1000_SetMeasurementConfig(
ADIJake 0:85855ecd3257 1420 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1421 ADI_SENSE_1000_MEASUREMENT_CONFIG *pMeasConfig)
ADIJake 0:85855ecd3257 1422 {
ADIJake 0:85855ecd3257 1423 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 1424
ADIJake 0:85855ecd3257 1425 eRet = adi_sense_SetMode(hDevice,
ADIJake 0:85855ecd3257 1426 pMeasConfig->operatingMode,
ADIJake 0:85855ecd3257 1427 pMeasConfig->dataReadyMode,
ADIJake 0:85855ecd3257 1428 pMeasConfig->calibrationMode,
ADIJake 0:85855ecd3257 1429 pMeasConfig->enableExternalFlash);
ADIJake 0:85855ecd3257 1430 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 1431 {
ADIJake 0:85855ecd3257 1432 ADI_SENSE_LOG_ERROR("Failed to set operating mode");
ADIJake 0:85855ecd3257 1433 return eRet;
ADIJake 0:85855ecd3257 1434 }
ADIJake 0:85855ecd3257 1435
ADIJake 0:85855ecd3257 1436 eRet = adi_sense_SetCycleControl(hDevice,
ADIJake 0:85855ecd3257 1437 pMeasConfig->cycleInterval,
ADIJake 0:85855ecd3257 1438 pMeasConfig->cycleType);
ADIJake 0:85855ecd3257 1439 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 1440 {
ADIJake 0:85855ecd3257 1441 ADI_SENSE_LOG_ERROR("Failed to set cycle control");
ADIJake 0:85855ecd3257 1442 return eRet;
ADIJake 0:85855ecd3257 1443 }
ADIJake 0:85855ecd3257 1444
ADIJake 0:85855ecd3257 1445 if (pMeasConfig->operatingMode == ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE)
ADIJake 0:85855ecd3257 1446 {
ADIJake 0:85855ecd3257 1447 eRet = adi_sense_SetMultiCycleConfig(hDevice,
ADIJake 0:85855ecd3257 1448 &pMeasConfig->multiCycleConfig);
ADIJake 0:85855ecd3257 1449 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 1450 {
ADIJake 0:85855ecd3257 1451 ADI_SENSE_LOG_ERROR("Failed to set multi-cycle configuration");
ADIJake 0:85855ecd3257 1452 return eRet;
ADIJake 0:85855ecd3257 1453 }
ADIJake 0:85855ecd3257 1454 }
ADIJake 0:85855ecd3257 1455
ADIJake 0:85855ecd3257 1456 eRet = adi_sense_SetExternalReferenceValues(hDevice,
ADIJake 0:85855ecd3257 1457 pMeasConfig->externalRef1Value,
ADIJake 0:85855ecd3257 1458 pMeasConfig->externalRef2Value);
ADIJake 0:85855ecd3257 1459 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 1460 {
ADIJake 0:85855ecd3257 1461 ADI_SENSE_LOG_ERROR("Failed to set external reference values");
ADIJake 0:85855ecd3257 1462 return eRet;
ADIJake 0:85855ecd3257 1463 }
ADIJake 0:85855ecd3257 1464
ADIJake 0:85855ecd3257 1465 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1466 }
ADIJake 0:85855ecd3257 1467
ADIJake 0:85855ecd3257 1468 ADI_SENSE_RESULT adi_sense_1000_SetDiagnosticsConfig(
ADIJake 0:85855ecd3257 1469 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1470 ADI_SENSE_1000_DIAGNOSTICS_CONFIG *pDiagnosticsConfig)
ADIJake 0:85855ecd3257 1471 {
ADIJake 0:85855ecd3257 1472 ADI_ADISENSE_CORE_Diagnostics_Control_t diagnosticsControlReg;
ADIJake 0:85855ecd3257 1473
ADIJake 0:85855ecd3257 1474 diagnosticsControlReg.VALUE16 = REG_RESET_VAL(CORE_DIAGNOSTICS_CONTROL);
ADIJake 0:85855ecd3257 1475
ADIJake 0:85855ecd3257 1476 if (pDiagnosticsConfig->disableGlobalDiag)
ADIJake 0:85855ecd3257 1477 diagnosticsControlReg.Diag_Global_En = 0;
ADIJake 0:85855ecd3257 1478 else
ADIJake 0:85855ecd3257 1479 diagnosticsControlReg.Diag_Global_En = 1;
ADIJake 0:85855ecd3257 1480
ADIJake 0:85855ecd3257 1481 if (pDiagnosticsConfig->disableMeasurementDiag)
ADIJake 0:85855ecd3257 1482 diagnosticsControlReg.Diag_Meas_En = 0;
ADIJake 0:85855ecd3257 1483 else
ADIJake 0:85855ecd3257 1484 diagnosticsControlReg.Diag_Meas_En = 1;
ADIJake 0:85855ecd3257 1485
ADIJake 0:85855ecd3257 1486 switch (pDiagnosticsConfig->osdFrequency)
ADIJake 0:85855ecd3257 1487 {
ADIJake 0:85855ecd3257 1488 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_DISABLED:
ADIJake 0:85855ecd3257 1489 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_OFF;
ADIJake 0:85855ecd3257 1490 break;
ADIJake 0:85855ecd3257 1491 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_PER_CYCLE:
ADIJake 0:85855ecd3257 1492 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_1_CYCLE;
ADIJake 0:85855ecd3257 1493 break;
ADIJake 0:85855ecd3257 1494 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_PER_100_CYCLES:
ADIJake 0:85855ecd3257 1495 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_100_CYCLES;
ADIJake 0:85855ecd3257 1496 break;
ADIJake 0:85855ecd3257 1497 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_PER_1000_CYCLES:
ADIJake 0:85855ecd3257 1498 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_1000_CYCLES;
ADIJake 0:85855ecd3257 1499 break;
ADIJake 0:85855ecd3257 1500 default:
ADIJake 0:85855ecd3257 1501 ADI_SENSE_LOG_ERROR("Invalid open-sensor diagnostic frequency %d specified",
ADIJake 0:85855ecd3257 1502 pDiagnosticsConfig->osdFrequency);
ADIJake 0:85855ecd3257 1503 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1504 }
ADIJake 0:85855ecd3257 1505
ADIJake 0:85855ecd3257 1506 WRITE_REG_U16(hDevice, diagnosticsControlReg.VALUE16, CORE_DIAGNOSTICS_CONTROL);
ADIJake 0:85855ecd3257 1507
ADIJake 0:85855ecd3257 1508 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1509 }
ADIJake 0:85855ecd3257 1510
ADIJake 0:85855ecd3257 1511 ADI_SENSE_RESULT adi_sense_1000_SetFftConfig(
ADIJake 0:85855ecd3257 1512 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1513 ADI_SENSE_1000_FFT_CONFIG *pFftConfig,
ADIJake 0:85855ecd3257 1514 ADI_SENSE_1000_CHANNEL_CONFIG *pChannels)
ADIJake 0:85855ecd3257 1515 {
ADIJake 0:85855ecd3257 1516 ADI_ADISENSE_CORE_FFT_Config_t fftConfigReg;
ADIJake 0:85855ecd3257 1517 ADI_ADISENSE_CORE_Mode_t modeReg;
ADIJake 0:85855ecd3257 1518 uint32_t numFftChannels = 0;
ADIJake 0:85855ecd3257 1519
ADIJake 0:85855ecd3257 1520 fftConfigReg.VALUE32 = REG_RESET_VAL(CORE_FFT_CONFIG);
ADIJake 0:85855ecd3257 1521
ADIJake 0:85855ecd3257 1522 for (ADI_SENSE_1000_CHANNEL_ID id = ADI_SENSE_1000_CHANNEL_ID_CJC_0;
ADIJake 0:85855ecd3257 1523 id < ADI_SENSE_1000_MAX_CHANNELS;
ADIJake 0:85855ecd3257 1524 id++)
ADIJake 0:85855ecd3257 1525 {
ADIJake 0:85855ecd3257 1526 if (pChannels[id].enableFFT)
ADIJake 0:85855ecd3257 1527 {
ADIJake 0:85855ecd3257 1528 if (numFftChannels >= 4) /* TODO - temporary limit */
ADIJake 0:85855ecd3257 1529 {
ADIJake 0:85855ecd3257 1530 ADI_SENSE_LOG_ERROR("Maximum limit of 4 FFT channels exceeded");
ADIJake 0:85855ecd3257 1531 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1532 }
ADIJake 0:85855ecd3257 1533
ADIJake 0:85855ecd3257 1534 numFftChannels++;
ADIJake 0:85855ecd3257 1535 }
ADIJake 0:85855ecd3257 1536 }
ADIJake 0:85855ecd3257 1537
ADIJake 0:85855ecd3257 1538 if (numFftChannels > 0)
ADIJake 0:85855ecd3257 1539 {
ADIJake 0:85855ecd3257 1540 fftConfigReg.FFT_Num_Channels = numFftChannels - 1;
ADIJake 0:85855ecd3257 1541
ADIJake 0:85855ecd3257 1542 switch (pFftConfig->size)
ADIJake 0:85855ecd3257 1543 {
ADIJake 0:85855ecd3257 1544 case ADI_SENSE_1000_FFT_SIZE_256:
ADIJake 0:85855ecd3257 1545 fftConfigReg.FFT_Num_Bins = ADISENSE_CORE_FFT_CONFIG_FFT_BINS_256;
ADIJake 0:85855ecd3257 1546 break;
ADIJake 0:85855ecd3257 1547 case ADI_SENSE_1000_FFT_SIZE_512:
ADIJake 0:85855ecd3257 1548 fftConfigReg.FFT_Num_Bins = ADISENSE_CORE_FFT_CONFIG_FFT_BINS_512;
ADIJake 0:85855ecd3257 1549 break;
ADIJake 0:85855ecd3257 1550 case ADI_SENSE_1000_FFT_SIZE_1024:
ADIJake 0:85855ecd3257 1551 fftConfigReg.FFT_Num_Bins = ADISENSE_CORE_FFT_CONFIG_FFT_BINS_1024;
ADIJake 0:85855ecd3257 1552 break;
ADIJake 0:85855ecd3257 1553 case ADI_SENSE_1000_FFT_SIZE_2048:
ADIJake 0:85855ecd3257 1554 fftConfigReg.FFT_Num_Bins = ADISENSE_CORE_FFT_CONFIG_FFT_BINS_2048;
ADIJake 0:85855ecd3257 1555 break;
ADIJake 0:85855ecd3257 1556 default:
ADIJake 0:85855ecd3257 1557 ADI_SENSE_LOG_ERROR("Invalid FFT size option %d specified",
ADIJake 0:85855ecd3257 1558 pFftConfig->size);
ADIJake 0:85855ecd3257 1559 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1560 }
ADIJake 0:85855ecd3257 1561
ADIJake 0:85855ecd3257 1562 switch (pFftConfig->window)
ADIJake 0:85855ecd3257 1563 {
ADIJake 0:85855ecd3257 1564 case ADI_SENSE_1000_FFT_WINDOW_NONE:
ADIJake 0:85855ecd3257 1565 fftConfigReg.FFT_Window = ADISENSE_CORE_FFT_CONFIG_FFT_WINDOW_NONE;
ADIJake 0:85855ecd3257 1566 break;
ADIJake 0:85855ecd3257 1567 case ADI_SENSE_1000_FFT_WINDOW_HANN:
ADIJake 0:85855ecd3257 1568 fftConfigReg.FFT_Window = ADISENSE_CORE_FFT_CONFIG_FFT_WINDOW_HANN;
ADIJake 0:85855ecd3257 1569 break;
ADIJake 0:85855ecd3257 1570 case ADI_SENSE_1000_FFT_WINDOW_BLACKMAN_HARRIS:
ADIJake 0:85855ecd3257 1571 fftConfigReg.FFT_Window = ADISENSE_CORE_FFT_CONFIG_FFT_WINDOW_BLACKMANN_HARRIS;
ADIJake 0:85855ecd3257 1572 break;
ADIJake 0:85855ecd3257 1573 default:
ADIJake 0:85855ecd3257 1574 ADI_SENSE_LOG_ERROR("Invalid FFT window option %d specified",
ADIJake 0:85855ecd3257 1575 pFftConfig->window);
ADIJake 0:85855ecd3257 1576 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1577 }
ADIJake 0:85855ecd3257 1578
ADIJake 0:85855ecd3257 1579 switch (pFftConfig->output)
ADIJake 0:85855ecd3257 1580 {
ADIJake 0:85855ecd3257 1581 case ADI_SENSE_1000_FFT_OUTPUT_FULL:
ADIJake 0:85855ecd3257 1582 fftConfigReg.FFT_Output = ADISENSE_CORE_FFT_CONFIG_FFT_OUTPUT_FULL;
ADIJake 0:85855ecd3257 1583 break;
ADIJake 0:85855ecd3257 1584 case ADI_SENSE_1000_FFT_OUTPUT_MAX16:
ADIJake 0:85855ecd3257 1585 fftConfigReg.FFT_Output = ADISENSE_CORE_FFT_CONFIG_FFT_OUTPUT_MAX16;
ADIJake 0:85855ecd3257 1586 break;
ADIJake 0:85855ecd3257 1587 case ADI_SENSE_1000_FFT_OUTPUT_FULL_WITH_RAW:
ADIJake 0:85855ecd3257 1588 fftConfigReg.FFT_Output = ADISENSE_CORE_FFT_CONFIG_FFT_OUTPUT_FULL_WITH_RAW;
ADIJake 0:85855ecd3257 1589 break;
ADIJake 0:85855ecd3257 1590 default:
ADIJake 0:85855ecd3257 1591 ADI_SENSE_LOG_ERROR("Invalid FFT output format option %d specified",
ADIJake 0:85855ecd3257 1592 pFftConfig->output);
ADIJake 0:85855ecd3257 1593 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1594 }
ADIJake 0:85855ecd3257 1595 }
ADIJake 0:85855ecd3257 1596 WRITE_REG_U32(hDevice, fftConfigReg.VALUE32, CORE_FFT_CONFIG);
ADIJake 0:85855ecd3257 1597
ADIJake 0:85855ecd3257 1598 if (numFftChannels > 0)
ADIJake 0:85855ecd3257 1599 {
ADIJake 0:85855ecd3257 1600 READ_REG_U8(hDevice, modeReg.VALUE8, CORE_MODE);
ADIJake 0:85855ecd3257 1601
ADIJake 0:85855ecd3257 1602 if (pFftConfig->mode == ADI_SENSE_1000_FFT_MODE_SINGLE)
ADIJake 0:85855ecd3257 1603 {
ADIJake 0:85855ecd3257 1604 modeReg.FFT_Mode = ADISENSE_CORE_MODE_FFT_MODE_SINGLE;
ADIJake 0:85855ecd3257 1605 }
ADIJake 0:85855ecd3257 1606 else if (pFftConfig->mode == ADI_SENSE_1000_FFT_MODE_CONTINUOUS)
ADIJake 0:85855ecd3257 1607 {
ADIJake 0:85855ecd3257 1608 modeReg.FFT_Mode = ADISENSE_CORE_MODE_FFT_MODE_CONTINUOUS;
ADIJake 0:85855ecd3257 1609 }
ADIJake 0:85855ecd3257 1610 else
ADIJake 0:85855ecd3257 1611 {
ADIJake 0:85855ecd3257 1612 ADI_SENSE_LOG_ERROR("Invalid FFT mode %d specified",
ADIJake 0:85855ecd3257 1613 pFftConfig->mode);
ADIJake 0:85855ecd3257 1614 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1615 }
ADIJake 0:85855ecd3257 1616
ADIJake 0:85855ecd3257 1617 WRITE_REG_U8(hDevice, modeReg.VALUE8, CORE_MODE);
ADIJake 0:85855ecd3257 1618 }
ADIJake 0:85855ecd3257 1619
ADIJake 0:85855ecd3257 1620 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1621 }
ADIJake 0:85855ecd3257 1622
ADIJake 0:85855ecd3257 1623 ADI_SENSE_RESULT adi_sense_1000_SetChannelCount(
ADIJake 0:85855ecd3257 1624 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1625 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 1626 uint32_t nMeasurementsPerCycle)
ADIJake 0:85855ecd3257 1627 {
ADIJake 0:85855ecd3257 1628 ADI_ADISENSE_CORE_Channel_Count_t channelCountReg;
ADIJake 0:85855ecd3257 1629
ADIJake 0:85855ecd3257 1630 channelCountReg.VALUE8 = REG_RESET_VAL(CORE_CHANNEL_COUNTn);
ADIJake 0:85855ecd3257 1631
ADIJake 0:85855ecd3257 1632 if (nMeasurementsPerCycle > 0)
ADIJake 0:85855ecd3257 1633 {
ADIJake 0:85855ecd3257 1634 nMeasurementsPerCycle -= 1;
ADIJake 0:85855ecd3257 1635
ADIJake 0:85855ecd3257 1636 CHECK_REG_FIELD_VAL(CORE_CHANNEL_COUNT_CHANNEL_COUNT,
ADIJake 0:85855ecd3257 1637 nMeasurementsPerCycle);
ADIJake 0:85855ecd3257 1638
ADIJake 0:85855ecd3257 1639 channelCountReg.Channel_Enable = 1;
ADIJake 0:85855ecd3257 1640 channelCountReg.Channel_Count = nMeasurementsPerCycle;
ADIJake 0:85855ecd3257 1641 }
ADIJake 0:85855ecd3257 1642 else
ADIJake 0:85855ecd3257 1643 {
ADIJake 0:85855ecd3257 1644 channelCountReg.Channel_Enable = 0;
ADIJake 0:85855ecd3257 1645 }
ADIJake 0:85855ecd3257 1646
ADIJake 0:85855ecd3257 1647 WRITE_REG_U8(hDevice, channelCountReg.VALUE8, CORE_CHANNEL_COUNTn(eChannelId));
ADIJake 0:85855ecd3257 1648
ADIJake 0:85855ecd3257 1649 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1650 }
ADIJake 0:85855ecd3257 1651
ADIJake 0:85855ecd3257 1652 ADI_SENSE_RESULT adi_sense_1000_SetChannelOptions(
ADIJake 0:85855ecd3257 1653 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1654 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 1655 ADI_SENSE_1000_CHANNEL_PRIORITY ePriority,
ADIJake 0:85855ecd3257 1656 bool bEnableFft)
ADIJake 0:85855ecd3257 1657 {
ADIJake 0:85855ecd3257 1658 ADI_ADISENSE_CORE_Channel_Options_t channelOptionsReg;
ADIJake 0:85855ecd3257 1659
ADIJake 0:85855ecd3257 1660 channelOptionsReg.VALUE8 = REG_RESET_VAL(CORE_CHANNEL_OPTIONSn);
ADIJake 0:85855ecd3257 1661
ADIJake 0:85855ecd3257 1662 CHECK_REG_FIELD_VAL(CORE_CHANNEL_OPTIONS_CHANNEL_PRIORITY, ePriority);
ADIJake 0:85855ecd3257 1663 channelOptionsReg.Channel_Priority = ePriority;
ADIJake 0:85855ecd3257 1664 channelOptionsReg.FFT_Enable_Ch = bEnableFft ? 1 : 0;
ADIJake 0:85855ecd3257 1665
ADIJake 0:85855ecd3257 1666 WRITE_REG_U8(hDevice, channelOptionsReg.VALUE8, CORE_CHANNEL_OPTIONSn(eChannelId));
ADIJake 0:85855ecd3257 1667
ADIJake 0:85855ecd3257 1668 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1669 }
ADIJake 0:85855ecd3257 1670
ADIJake 0:85855ecd3257 1671 ADI_SENSE_RESULT adi_sense_1000_SetChannelSkipCount(
ADIJake 0:85855ecd3257 1672 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1673 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 1674 uint32_t nCycleSkipCount)
ADIJake 0:85855ecd3257 1675 {
ADIJake 0:85855ecd3257 1676 ADI_ADISENSE_CORE_Channel_Skip_t channelSkipReg;
ADIJake 0:85855ecd3257 1677
ADIJake 0:85855ecd3257 1678 channelSkipReg.VALUE16 = REG_RESET_VAL(CORE_CHANNEL_SKIPn);
ADIJake 0:85855ecd3257 1679
ADIJake 0:85855ecd3257 1680 CHECK_REG_FIELD_VAL(CORE_CHANNEL_SKIP_CHANNEL_SKIP, nCycleSkipCount);
ADIJake 0:85855ecd3257 1681
ADIJake 0:85855ecd3257 1682 channelSkipReg.Channel_Skip = nCycleSkipCount;
ADIJake 0:85855ecd3257 1683
ADIJake 0:85855ecd3257 1684 WRITE_REG_U16(hDevice, channelSkipReg.VALUE16, CORE_CHANNEL_SKIPn(eChannelId));
ADIJake 0:85855ecd3257 1685
ADIJake 0:85855ecd3257 1686 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1687 }
ADIJake 0:85855ecd3257 1688
ADIJake 0:85855ecd3257 1689 static ADI_SENSE_RESULT adi_sense_SetChannelAdcSensorType(
ADIJake 0:85855ecd3257 1690 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1691 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 1692 ADI_SENSE_1000_ADC_SENSOR_TYPE sensorType)
ADIJake 0:85855ecd3257 1693 {
ADIJake 0:85855ecd3257 1694 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
ADIJake 0:85855ecd3257 1695
ADIJake 0:85855ecd3257 1696 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
ADIJake 0:85855ecd3257 1697
ADIJake 0:85855ecd3257 1698 /* Ensure that the sensor type is valid for this channel */
ADIJake 0:85855ecd3257 1699 switch(sensorType)
ADIJake 0:85855ecd3257 1700 {
ADIJake 0:85855ecd3257 1701 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_J_DEF_L1:
ADIJake 0:85855ecd3257 1702 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_K_DEF_L1:
ADIJake 0:85855ecd3257 1703 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_T_DEF_L1:
ADIJake 0:85855ecd3257 1704 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_1_DEF_L2:
ADIJake 0:85855ecd3257 1705 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_2_DEF_L2:
ADIJake 0:85855ecd3257 1706 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_3_DEF_L2:
ADIJake 0:85855ecd3257 1707 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_4_DEF_L2:
ADIJake 0:85855ecd3257 1708 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_J_ADV_L1:
ADIJake 0:85855ecd3257 1709 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_K_ADV_L1:
ADIJake 0:85855ecd3257 1710 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_T_ADV_L1:
ADIJake 0:85855ecd3257 1711 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_1_ADV_L2:
ADIJake 0:85855ecd3257 1712 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_2_ADV_L2:
ADIJake 0:85855ecd3257 1713 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_3_ADV_L2:
ADIJake 0:85855ecd3257 1714 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_4_ADV_L2:
ADIJake 0:85855ecd3257 1715 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT100_DEF_L1:
ADIJake 0:85855ecd3257 1716 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT1000_DEF_L1:
ADIJake 0:85855ecd3257 1717 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_1_DEF_L2:
ADIJake 0:85855ecd3257 1718 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_2_DEF_L2:
ADIJake 0:85855ecd3257 1719 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_3_DEF_L2:
ADIJake 0:85855ecd3257 1720 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_4_DEF_L2:
ADIJake 0:85855ecd3257 1721 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT100_ADV_L1:
ADIJake 0:85855ecd3257 1722 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT1000_ADV_L1:
ADIJake 0:85855ecd3257 1723 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_1_ADV_L2:
ADIJake 0:85855ecd3257 1724 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_2_ADV_L2:
ADIJake 0:85855ecd3257 1725 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_3_ADV_L2:
ADIJake 0:85855ecd3257 1726 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_4_ADV_L2:
ADIJake 0:85855ecd3257 1727 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT100_DEF_L1:
ADIJake 0:85855ecd3257 1728 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT1000_DEF_L1:
ADIJake 0:85855ecd3257 1729 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_1_DEF_L2:
ADIJake 0:85855ecd3257 1730 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_2_DEF_L2:
ADIJake 0:85855ecd3257 1731 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_3_DEF_L2:
ADIJake 0:85855ecd3257 1732 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_4_DEF_L2:
ADIJake 0:85855ecd3257 1733 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT100_ADV_L1:
ADIJake 0:85855ecd3257 1734 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT1000_ADV_L1:
ADIJake 0:85855ecd3257 1735 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_1_ADV_L2:
ADIJake 0:85855ecd3257 1736 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_2_ADV_L2:
ADIJake 0:85855ecd3257 1737 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_3_ADV_L2:
ADIJake 0:85855ecd3257 1738 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_4_ADV_L2:
ADIJake 0:85855ecd3257 1739 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_1_DEF_L2:
ADIJake 0:85855ecd3257 1740 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_2_DEF_L2:
ADIJake 0:85855ecd3257 1741 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_3_DEF_L2:
ADIJake 0:85855ecd3257 1742 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_4_DEF_L2:
ADIJake 0:85855ecd3257 1743 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_1_ADV_L2:
ADIJake 0:85855ecd3257 1744 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_2_ADV_L2:
ADIJake 0:85855ecd3257 1745 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_3_ADV_L2:
ADIJake 0:85855ecd3257 1746 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_4_ADV_L2:
ADIJake 0:85855ecd3257 1747 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_1_DEF_L2:
ADIJake 0:85855ecd3257 1748 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_2_DEF_L2:
ADIJake 0:85855ecd3257 1749 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_3_DEF_L2:
ADIJake 0:85855ecd3257 1750 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_4_DEF_L2:
ADIJake 0:85855ecd3257 1751 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_1_ADV_L2:
ADIJake 0:85855ecd3257 1752 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_2_ADV_L2:
ADIJake 0:85855ecd3257 1753 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_3_ADV_L2:
ADIJake 0:85855ecd3257 1754 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_4_ADV_L2:
ADIJake 0:85855ecd3257 1755 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_A_DEF_L1:
ADIJake 0:85855ecd3257 1756 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_B_DEF_L1:
ADIJake 0:85855ecd3257 1757 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_1_DEF_L2:
ADIJake 0:85855ecd3257 1758 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_2_DEF_L2:
ADIJake 0:85855ecd3257 1759 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_A_ADV_L1:
ADIJake 0:85855ecd3257 1760 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_B_ADV_L1:
ADIJake 0:85855ecd3257 1761 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_1_ADV_L2:
ADIJake 0:85855ecd3257 1762 case ADI_SENSE_1000_ADC_SENSOR_MICROPHONE_2_ADV_L2:
ADIJake 0:85855ecd3257 1763 if (! ADI_SENSE_1000_CHANNEL_IS_ADC_SENSOR(eChannelId))
ADIJake 0:85855ecd3257 1764 {
ADIJake 0:85855ecd3257 1765 ADI_SENSE_LOG_ERROR(
ADIJake 0:85855ecd3257 1766 "Invalid ADC sensor type %d specified for channel %d",
ADIJake 0:85855ecd3257 1767 sensorType, eChannelId);
ADIJake 0:85855ecd3257 1768 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1769 }
ADIJake 0:85855ecd3257 1770 break;
ADIJake 0:85855ecd3257 1771 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT100_DEF_L1:
ADIJake 0:85855ecd3257 1772 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT1000_DEF_L1:
ADIJake 0:85855ecd3257 1773 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_1_DEF_L2:
ADIJake 0:85855ecd3257 1774 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_2_DEF_L2:
ADIJake 0:85855ecd3257 1775 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_3_DEF_L2:
ADIJake 0:85855ecd3257 1776 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_4_DEF_L2:
ADIJake 0:85855ecd3257 1777 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT100_ADV_L1:
ADIJake 0:85855ecd3257 1778 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT1000_ADV_L1:
ADIJake 0:85855ecd3257 1779 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_1_ADV_L2:
ADIJake 0:85855ecd3257 1780 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_2_ADV_L2:
ADIJake 0:85855ecd3257 1781 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_3_ADV_L2:
ADIJake 0:85855ecd3257 1782 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_4_ADV_L2:
ADIJake 0:85855ecd3257 1783 case ADI_SENSE_1000_ADC_SENSOR_DIODE_2C_TYPEA_DEF_L1:
ADIJake 0:85855ecd3257 1784 case ADI_SENSE_1000_ADC_SENSOR_DIODE_3C_TYPEA_DEF_L1:
ADIJake 0:85855ecd3257 1785 case ADI_SENSE_1000_ADC_SENSOR_DIODE_2C_1_DEF_L2:
ADIJake 0:85855ecd3257 1786 case ADI_SENSE_1000_ADC_SENSOR_DIODE_3C_1_DEF_L2:
ADIJake 0:85855ecd3257 1787 case ADI_SENSE_1000_ADC_SENSOR_DIODE_2C_TYPEA_ADV_L1:
ADIJake 0:85855ecd3257 1788 case ADI_SENSE_1000_ADC_SENSOR_DIODE_3C_TYPEA_ADV_L1:
ADIJake 0:85855ecd3257 1789 case ADI_SENSE_1000_ADC_SENSOR_DIODE_2C_1_ADV_L2:
ADIJake 0:85855ecd3257 1790 case ADI_SENSE_1000_ADC_SENSOR_DIODE_3C_1_ADV_L2:
ADIJake 0:85855ecd3257 1791 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_A_10K_DEF_L1:
ADIJake 0:85855ecd3257 1792 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_B_10K_DEF_L1:
ADIJake 0:85855ecd3257 1793 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_1_DEF_L2:
ADIJake 0:85855ecd3257 1794 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_2_DEF_L2:
ADIJake 0:85855ecd3257 1795 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_3_DEF_L2:
ADIJake 0:85855ecd3257 1796 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_4_DEF_L2:
ADIJake 0:85855ecd3257 1797 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_A_10K_ADV_L1:
ADIJake 0:85855ecd3257 1798 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_B_10K_ADV_L1:
ADIJake 0:85855ecd3257 1799 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_1_ADV_L2:
ADIJake 0:85855ecd3257 1800 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_2_ADV_L2:
ADIJake 0:85855ecd3257 1801 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_3_ADV_L2:
ADIJake 0:85855ecd3257 1802 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_4_ADV_L2:
ADIJake 0:85855ecd3257 1803 if (! (ADI_SENSE_1000_CHANNEL_IS_ADC_SENSOR(eChannelId) ||
ADIJake 0:85855ecd3257 1804 ADI_SENSE_1000_CHANNEL_IS_ADC_CJC(eChannelId)))
ADIJake 0:85855ecd3257 1805 {
ADIJake 0:85855ecd3257 1806 ADI_SENSE_LOG_ERROR(
ADIJake 0:85855ecd3257 1807 "Invalid ADC sensor type %d specified for channel %d",
ADIJake 0:85855ecd3257 1808 sensorType, eChannelId);
ADIJake 0:85855ecd3257 1809 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1810 }
ADIJake 0:85855ecd3257 1811 break;
ADIJake 0:85855ecd3257 1812 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE:
ADIJake 0:85855ecd3257 1813 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_A_DEF_L1:
ADIJake 0:85855ecd3257 1814 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_B_DEF_L1:
ADIJake 0:85855ecd3257 1815 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_1_DEF_L2:
ADIJake 0:85855ecd3257 1816 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_2_DEF_L2:
ADIJake 0:85855ecd3257 1817 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_A_ADV_L1:
ADIJake 0:85855ecd3257 1818 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_B_ADV_L1:
ADIJake 0:85855ecd3257 1819 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_1_ADV_L2:
ADIJake 0:85855ecd3257 1820 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_2_ADV_L2:
ADIJake 0:85855ecd3257 1821 if (! ADI_SENSE_1000_CHANNEL_IS_ADC_VOLTAGE(eChannelId))
ADIJake 0:85855ecd3257 1822 {
ADIJake 0:85855ecd3257 1823 ADI_SENSE_LOG_ERROR(
ADIJake 0:85855ecd3257 1824 "Invalid ADC sensor type %d specified for channel %d",
ADIJake 0:85855ecd3257 1825 sensorType, eChannelId);
ADIJake 0:85855ecd3257 1826 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1827 }
ADIJake 0:85855ecd3257 1828 break;
ADIJake 0:85855ecd3257 1829 case ADI_SENSE_1000_ADC_SENSOR_CURRENT:
ADIJake 0:85855ecd3257 1830 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_A_DEF_L1:
ADIJake 0:85855ecd3257 1831 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_1_DEF_L2:
ADIJake 0:85855ecd3257 1832 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_2_DEF_L2:
ADIJake 0:85855ecd3257 1833 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_A_ADV_L1:
ADIJake 0:85855ecd3257 1834 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_1_ADV_L2:
ADIJake 0:85855ecd3257 1835 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_2_ADV_L2:
ADIJake 0:85855ecd3257 1836 if (! ADI_SENSE_1000_CHANNEL_IS_ADC_CURRENT(eChannelId))
ADIJake 0:85855ecd3257 1837 {
ADIJake 0:85855ecd3257 1838 ADI_SENSE_LOG_ERROR(
ADIJake 0:85855ecd3257 1839 "Invalid ADC sensor type %d specified for channel %d",
ADIJake 0:85855ecd3257 1840 sensorType, eChannelId);
ADIJake 0:85855ecd3257 1841 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1842 }
ADIJake 0:85855ecd3257 1843 break;
ADIJake 0:85855ecd3257 1844 default:
ADIJake 0:85855ecd3257 1845 ADI_SENSE_LOG_ERROR("Invalid/unsupported ADC sensor type %d specified",
ADIJake 0:85855ecd3257 1846 sensorType);
ADIJake 0:85855ecd3257 1847 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1848 }
ADIJake 0:85855ecd3257 1849
ADIJake 0:85855ecd3257 1850 sensorTypeReg.Sensor_Type = sensorType;
ADIJake 0:85855ecd3257 1851
ADIJake 0:85855ecd3257 1852 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
ADIJake 0:85855ecd3257 1853
ADIJake 0:85855ecd3257 1854 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1855 }
ADIJake 0:85855ecd3257 1856
ADIJake 0:85855ecd3257 1857 static ADI_SENSE_RESULT adi_sense_SetChannelAdcSensorDetails(
ADIJake 0:85855ecd3257 1858 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 1859 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 1860 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 1861 /*
ADIJake 0:85855ecd3257 1862 * TODO - it would be nice if the general- vs. ADC-specific sensor details could be split into separate registers
ADIJake 0:85855ecd3257 1863 * General details:
ADIJake 0:85855ecd3257 1864 * - Measurement_Units
ADIJake 0:85855ecd3257 1865 * - Compensation_Channel
ADIJake 0:85855ecd3257 1866 * - CJC_Publish (if "CJC" was removed from the name)
ADIJake 0:85855ecd3257 1867 * ADC-specific details:
ADIJake 0:85855ecd3257 1868 * - PGA_Gain
ADIJake 0:85855ecd3257 1869 * - Reference_Select
ADIJake 0:85855ecd3257 1870 * - Reference_Buffer_Disable
ADIJake 0:85855ecd3257 1871 * - Vbias
ADIJake 0:85855ecd3257 1872 */
ADIJake 0:85855ecd3257 1873 {
ADIJake 0:85855ecd3257 1874 ADI_SENSE_1000_ADC_CHANNEL_CONFIG *pAdcChannelConfig = &pChannelConfig->adcChannelConfig;
ADIJake 0:85855ecd3257 1875 ADI_SENSE_1000_ADC_REFERENCE_CONFIG *pRefConfig = &pAdcChannelConfig->reference;
ADIJake 0:85855ecd3257 1876 ADI_ADISENSE_CORE_Sensor_Details_t sensorDetailsReg;
ADIJake 0:85855ecd3257 1877
ADIJake 0:85855ecd3257 1878 sensorDetailsReg.VALUE32 = REG_RESET_VAL(CORE_SENSOR_DETAILSn);
ADIJake 0:85855ecd3257 1879
ADIJake 0:85855ecd3257 1880 switch(pChannelConfig->measurementUnit)
ADIJake 0:85855ecd3257 1881 {
ADIJake 0:85855ecd3257 1882 case ADI_SENSE_1000_MEASUREMENT_UNIT_FAHRENHEIT:
ADIJake 0:85855ecd3257 1883 sensorDetailsReg.Measurement_Units = ADISENSE_CORE_SENSOR_DETAILS_UNITS_DEGF;
ADIJake 0:85855ecd3257 1884 break;
ADIJake 0:85855ecd3257 1885 case ADI_SENSE_1000_MEASUREMENT_UNIT_CELSIUS:
ADIJake 0:85855ecd3257 1886 sensorDetailsReg.Measurement_Units = ADISENSE_CORE_SENSOR_DETAILS_UNITS_DEGC;
ADIJake 0:85855ecd3257 1887 break;
ADIJake 0:85855ecd3257 1888 case ADI_SENSE_1000_MEASUREMENT_UNIT_UNSPECIFIED:
ADIJake 0:85855ecd3257 1889 sensorDetailsReg.Measurement_Units = ADISENSE_CORE_SENSOR_DETAILS_UNITS_UNSPECIFIED;
ADIJake 0:85855ecd3257 1890 break;
ADIJake 0:85855ecd3257 1891 default:
ADIJake 0:85855ecd3257 1892 ADI_SENSE_LOG_ERROR("Invalid measurement unit %d specified",
ADIJake 0:85855ecd3257 1893 pChannelConfig->measurementUnit);
ADIJake 0:85855ecd3257 1894 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1895 }
ADIJake 0:85855ecd3257 1896
ADIJake 0:85855ecd3257 1897 if (pChannelConfig->compensationChannel == ADI_SENSE_1000_CHANNEL_ID_NONE)
ADIJake 0:85855ecd3257 1898 {
ADIJake 0:85855ecd3257 1899 sensorDetailsReg.Compensation_Disable = 1;
ADIJake 0:85855ecd3257 1900 sensorDetailsReg.Compensation_Channel = 0;
ADIJake 0:85855ecd3257 1901 }
ADIJake 0:85855ecd3257 1902 else
ADIJake 0:85855ecd3257 1903 {
ADIJake 0:85855ecd3257 1904 sensorDetailsReg.Compensation_Disable = 0;
ADIJake 0:85855ecd3257 1905 sensorDetailsReg.Compensation_Channel = pChannelConfig->compensationChannel;
ADIJake 0:85855ecd3257 1906 }
ADIJake 0:85855ecd3257 1907
ADIJake 0:85855ecd3257 1908 switch(pRefConfig->type)
ADIJake 0:85855ecd3257 1909 {
ADIJake 0:85855ecd3257 1910 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_INTERNAL_1:
ADIJake 0:85855ecd3257 1911 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_RINT1;
ADIJake 0:85855ecd3257 1912 break;
ADIJake 0:85855ecd3257 1913 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_INTERNAL_2:
ADIJake 0:85855ecd3257 1914 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_RINT2;
ADIJake 0:85855ecd3257 1915 break;
ADIJake 0:85855ecd3257 1916 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_INTERNAL:
ADIJake 0:85855ecd3257 1917 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_INT;
ADIJake 0:85855ecd3257 1918 break;
ADIJake 0:85855ecd3257 1919 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_AVDD:
ADIJake 0:85855ecd3257 1920 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_AVDD;
ADIJake 0:85855ecd3257 1921 break;
ADIJake 0:85855ecd3257 1922 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_EXTERNAL_1:
ADIJake 0:85855ecd3257 1923 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_REXT1;
ADIJake 0:85855ecd3257 1924 break;
ADIJake 0:85855ecd3257 1925 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_EXTERNAL_2:
ADIJake 0:85855ecd3257 1926 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_REXT2;
ADIJake 0:85855ecd3257 1927 break;
ADIJake 0:85855ecd3257 1928 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_EXTERNAL_1:
ADIJake 0:85855ecd3257 1929 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_VEXT1;
ADIJake 0:85855ecd3257 1930 break;
ADIJake 0:85855ecd3257 1931 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_EXTERNAL_2:
ADIJake 0:85855ecd3257 1932 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_VEXT2;
ADIJake 0:85855ecd3257 1933 break;
ADIJake 0:85855ecd3257 1934 case ADI_SENSE_1000_ADC_REFERENCE_BRIDGE_EXCITATION:
ADIJake 0:85855ecd3257 1935 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_EXC;
ADIJake 0:85855ecd3257 1936 break;
ADIJake 0:85855ecd3257 1937 default:
ADIJake 0:85855ecd3257 1938 ADI_SENSE_LOG_ERROR("Invalid ADC reference type %d specified",
ADIJake 0:85855ecd3257 1939 pRefConfig->type);
ADIJake 0:85855ecd3257 1940 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1941 }
ADIJake 0:85855ecd3257 1942
ADIJake 0:85855ecd3257 1943 switch(pAdcChannelConfig->gain)
ADIJake 0:85855ecd3257 1944 {
ADIJake 0:85855ecd3257 1945 case ADI_SENSE_1000_ADC_GAIN_1X:
ADIJake 0:85855ecd3257 1946 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_1;
ADIJake 0:85855ecd3257 1947 break;
ADIJake 0:85855ecd3257 1948 case ADI_SENSE_1000_ADC_GAIN_2X:
ADIJake 0:85855ecd3257 1949 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_2;
ADIJake 0:85855ecd3257 1950 break;
ADIJake 0:85855ecd3257 1951 case ADI_SENSE_1000_ADC_GAIN_4X:
ADIJake 0:85855ecd3257 1952 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_4;
ADIJake 0:85855ecd3257 1953 break;
ADIJake 0:85855ecd3257 1954 case ADI_SENSE_1000_ADC_GAIN_8X:
ADIJake 0:85855ecd3257 1955 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_8;
ADIJake 0:85855ecd3257 1956 break;
ADIJake 0:85855ecd3257 1957 case ADI_SENSE_1000_ADC_GAIN_16X:
ADIJake 0:85855ecd3257 1958 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_16;
ADIJake 0:85855ecd3257 1959 break;
ADIJake 0:85855ecd3257 1960 case ADI_SENSE_1000_ADC_GAIN_32X:
ADIJake 0:85855ecd3257 1961 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_32;
ADIJake 0:85855ecd3257 1962 break;
ADIJake 0:85855ecd3257 1963 case ADI_SENSE_1000_ADC_GAIN_64X:
ADIJake 0:85855ecd3257 1964 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_64;
ADIJake 0:85855ecd3257 1965 break;
ADIJake 0:85855ecd3257 1966 case ADI_SENSE_1000_ADC_GAIN_128X:
ADIJake 0:85855ecd3257 1967 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_128;
ADIJake 0:85855ecd3257 1968 break;
ADIJake 0:85855ecd3257 1969 default:
ADIJake 0:85855ecd3257 1970 ADI_SENSE_LOG_ERROR("Invalid ADC gain %d specified",
ADIJake 0:85855ecd3257 1971 pAdcChannelConfig->gain);
ADIJake 0:85855ecd3257 1972 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 1973 }
ADIJake 0:85855ecd3257 1974
ADIJake 0:85855ecd3257 1975 if (pAdcChannelConfig->enableVbias)
ADIJake 0:85855ecd3257 1976 sensorDetailsReg.Vbias = 1;
ADIJake 0:85855ecd3257 1977 else
ADIJake 0:85855ecd3257 1978 sensorDetailsReg.Vbias = 0;
ADIJake 0:85855ecd3257 1979
ADIJake 0:85855ecd3257 1980 if (pAdcChannelConfig->reference.disableBuffer)
ADIJake 0:85855ecd3257 1981 sensorDetailsReg.Reference_Buffer_Disable = 1;
ADIJake 0:85855ecd3257 1982 else
ADIJake 0:85855ecd3257 1983 sensorDetailsReg.Reference_Buffer_Disable = 0;
ADIJake 0:85855ecd3257 1984
ADIJake 0:85855ecd3257 1985 if (pChannelConfig->disablePublishing)
ADIJake 0:85855ecd3257 1986 sensorDetailsReg.Do_Not_Publish = 1;
ADIJake 0:85855ecd3257 1987 else
ADIJake 0:85855ecd3257 1988 sensorDetailsReg.Do_Not_Publish = 0;
ADIJake 0:85855ecd3257 1989
ADIJake 0:85855ecd3257 1990 if (pChannelConfig->enableUnityLut)
ADIJake 0:85855ecd3257 1991 sensorDetailsReg.Unity_LUT_Select = 1;
ADIJake 0:85855ecd3257 1992 else
ADIJake 0:85855ecd3257 1993 sensorDetailsReg.Unity_LUT_Select = 0;
ADIJake 0:85855ecd3257 1994
ADIJake 0:85855ecd3257 1995 WRITE_REG_U32(hDevice, sensorDetailsReg.VALUE32, CORE_SENSOR_DETAILSn(eChannelId));
ADIJake 0:85855ecd3257 1996
ADIJake 0:85855ecd3257 1997 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 1998 }
ADIJake 0:85855ecd3257 1999
ADIJake 0:85855ecd3257 2000 static ADI_SENSE_RESULT adi_sense_SetChannelAdcFilter(
ADIJake 0:85855ecd3257 2001 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2002 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2003 ADI_SENSE_1000_ADC_FILTER_CONFIG *pFilterConfig)
ADIJake 0:85855ecd3257 2004 {
ADIJake 0:85855ecd3257 2005 ADI_ADISENSE_CORE_Filter_Select_t filterSelectReg;
ADIJake 0:85855ecd3257 2006
ADIJake 0:85855ecd3257 2007 filterSelectReg.VALUE32 = REG_RESET_VAL(CORE_FILTER_SELECTn);
ADIJake 0:85855ecd3257 2008
ADIJake 0:85855ecd3257 2009 if (pFilterConfig->type == ADI_SENSE_1000_ADC_FILTER_SINC4)
ADIJake 0:85855ecd3257 2010 {
ADIJake 0:85855ecd3257 2011 filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_SINC4;
ADIJake 0:85855ecd3257 2012 filterSelectReg.ADC_FS = pFilterConfig->fs;
ADIJake 0:85855ecd3257 2013 }
ADIJake 0:85855ecd3257 2014 else if (pFilterConfig->type == ADI_SENSE_1000_ADC_FILTER_FIR_20SPS)
ADIJake 0:85855ecd3257 2015 {
ADIJake 0:85855ecd3257 2016 filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_FIR_20SPS;
ADIJake 0:85855ecd3257 2017 }
ADIJake 0:85855ecd3257 2018 else if (pFilterConfig->type == ADI_SENSE_1000_ADC_FILTER_FIR_25SPS)
ADIJake 0:85855ecd3257 2019 {
ADIJake 0:85855ecd3257 2020 filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_FIR_25SPS;
ADIJake 0:85855ecd3257 2021 }
ADIJake 0:85855ecd3257 2022 else
ADIJake 0:85855ecd3257 2023 {
ADIJake 0:85855ecd3257 2024 ADI_SENSE_LOG_ERROR("Invalid ADC filter type %d specified",
ADIJake 0:85855ecd3257 2025 pFilterConfig->type);
ADIJake 0:85855ecd3257 2026 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2027 }
ADIJake 0:85855ecd3257 2028
ADIJake 0:85855ecd3257 2029 WRITE_REG_U32(hDevice, filterSelectReg.VALUE32, CORE_FILTER_SELECTn(eChannelId));
ADIJake 0:85855ecd3257 2030
ADIJake 0:85855ecd3257 2031 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2032 }
ADIJake 0:85855ecd3257 2033
ADIJake 0:85855ecd3257 2034 static ADI_SENSE_RESULT adi_sense_SetChannelAdcCurrentConfig(
ADIJake 0:85855ecd3257 2035 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2036 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2037 ADI_SENSE_1000_ADC_EXC_CURRENT_CONFIG *pCurrentConfig)
ADIJake 0:85855ecd3257 2038 {
ADIJake 0:85855ecd3257 2039 ADI_ADISENSE_CORE_Channel_Excitation_t channelExcitationReg;
ADIJake 0:85855ecd3257 2040
ADIJake 0:85855ecd3257 2041 channelExcitationReg.VALUE8 = REG_RESET_VAL(CORE_CHANNEL_EXCITATIONn);
ADIJake 0:85855ecd3257 2042
ADIJake 0:85855ecd3257 2043 if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_NONE)
ADIJake 0:85855ecd3257 2044 {
ADIJake 0:85855ecd3257 2045 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_OFF;
ADIJake 0:85855ecd3257 2046 }
ADIJake 0:85855ecd3257 2047 else
ADIJake 0:85855ecd3257 2048 {
ADIJake 0:85855ecd3257 2049 if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_50uA)
ADIJake 0:85855ecd3257 2050 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_50UA;
ADIJake 0:85855ecd3257 2051 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_100uA)
ADIJake 0:85855ecd3257 2052 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_100UA;
ADIJake 0:85855ecd3257 2053 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_250uA)
ADIJake 0:85855ecd3257 2054 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_250UA;
ADIJake 0:85855ecd3257 2055 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_500uA)
ADIJake 0:85855ecd3257 2056 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_500UA;
ADIJake 0:85855ecd3257 2057 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_750uA)
ADIJake 0:85855ecd3257 2058 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_750UA;
ADIJake 0:85855ecd3257 2059 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_1000uA)
ADIJake 0:85855ecd3257 2060 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_1000UA;
ADIJake 0:85855ecd3257 2061 else
ADIJake 0:85855ecd3257 2062 {
ADIJake 0:85855ecd3257 2063 ADI_SENSE_LOG_ERROR("Invalid ADC excitation current %d specified",
ADIJake 0:85855ecd3257 2064 pCurrentConfig->outputLevel);
ADIJake 0:85855ecd3257 2065 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2066 }
ADIJake 0:85855ecd3257 2067 }
ADIJake 0:85855ecd3257 2068
ADIJake 0:85855ecd3257 2069 if (pCurrentConfig->diodeRatio == ADI_SENSE_1000_ADC_EXC_CURRENT_IOUT_DIODE_DEFAULT)
ADIJake 0:85855ecd3257 2070 {
ADIJake 0:85855ecd3257 2071 channelExcitationReg.IOUT_Diode_Ratio = 0;
ADIJake 0:85855ecd3257 2072 }
ADIJake 0:85855ecd3257 2073 else
ADIJake 0:85855ecd3257 2074 {
ADIJake 0:85855ecd3257 2075 channelExcitationReg.IOUT_Diode_Ratio = 1;
ADIJake 0:85855ecd3257 2076 }
ADIJake 0:85855ecd3257 2077
ADIJake 0:85855ecd3257 2078 WRITE_REG_U8(hDevice, channelExcitationReg.VALUE8, CORE_CHANNEL_EXCITATIONn(eChannelId));
ADIJake 0:85855ecd3257 2079
ADIJake 0:85855ecd3257 2080 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2081 }
ADIJake 0:85855ecd3257 2082
ADIJake 0:85855ecd3257 2083 ADI_SENSE_RESULT adi_sense_SetAdcChannelConfig(
ADIJake 0:85855ecd3257 2084 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2085 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2086 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 2087 {
ADIJake 0:85855ecd3257 2088 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 2089 ADI_SENSE_1000_ADC_CHANNEL_CONFIG *pAdcChannelConfig =
ADIJake 0:85855ecd3257 2090 &pChannelConfig->adcChannelConfig;
ADIJake 0:85855ecd3257 2091
ADIJake 0:85855ecd3257 2092 eRet = adi_sense_SetChannelAdcSensorType(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2093 pAdcChannelConfig->sensor);
ADIJake 0:85855ecd3257 2094 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2095 {
ADIJake 0:85855ecd3257 2096 ADI_SENSE_LOG_ERROR("Failed to set ADC sensor type for channel %d",
ADIJake 0:85855ecd3257 2097 eChannelId);
ADIJake 0:85855ecd3257 2098 return eRet;
ADIJake 0:85855ecd3257 2099 }
ADIJake 0:85855ecd3257 2100
ADIJake 0:85855ecd3257 2101 eRet = adi_sense_SetChannelAdcSensorDetails(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2102 pChannelConfig);
ADIJake 0:85855ecd3257 2103 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2104 {
ADIJake 0:85855ecd3257 2105 ADI_SENSE_LOG_ERROR("Failed to set ADC sensor details for channel %d",
ADIJake 0:85855ecd3257 2106 eChannelId);
ADIJake 0:85855ecd3257 2107 return eRet;
ADIJake 0:85855ecd3257 2108 }
ADIJake 0:85855ecd3257 2109
ADIJake 0:85855ecd3257 2110 eRet = adi_sense_SetChannelAdcFilter(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2111 &pAdcChannelConfig->filter);
ADIJake 0:85855ecd3257 2112 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2113 {
ADIJake 0:85855ecd3257 2114 ADI_SENSE_LOG_ERROR("Failed to set ADC filter for channel %d",
ADIJake 0:85855ecd3257 2115 eChannelId);
ADIJake 0:85855ecd3257 2116 return eRet;
ADIJake 0:85855ecd3257 2117 }
ADIJake 0:85855ecd3257 2118
ADIJake 0:85855ecd3257 2119 eRet = adi_sense_SetChannelAdcCurrentConfig(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2120 &pAdcChannelConfig->current);
ADIJake 0:85855ecd3257 2121 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2122 {
ADIJake 0:85855ecd3257 2123 ADI_SENSE_LOG_ERROR("Failed to set ADC current for channel %d",
ADIJake 0:85855ecd3257 2124 eChannelId);
ADIJake 0:85855ecd3257 2125 return eRet;
ADIJake 0:85855ecd3257 2126 }
ADIJake 0:85855ecd3257 2127
ADIJake 0:85855ecd3257 2128 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2129 }
ADIJake 0:85855ecd3257 2130
ADIJake 0:85855ecd3257 2131 static ADI_SENSE_RESULT adi_sense_SetChannelDigitalSensorDetails(
ADIJake 0:85855ecd3257 2132 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2133 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2134 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 2135 {
ADIJake 0:85855ecd3257 2136 ADI_ADISENSE_CORE_Sensor_Details_t sensorDetailsReg;
ADIJake 0:85855ecd3257 2137
ADIJake 0:85855ecd3257 2138 sensorDetailsReg.VALUE32 = REG_RESET_VAL(CORE_SENSOR_DETAILSn);
ADIJake 0:85855ecd3257 2139
ADIJake 0:85855ecd3257 2140 if (pChannelConfig->compensationChannel == ADI_SENSE_1000_CHANNEL_ID_NONE)
ADIJake 0:85855ecd3257 2141 {
ADIJake 0:85855ecd3257 2142 sensorDetailsReg.Compensation_Disable = 1;
ADIJake 0:85855ecd3257 2143 sensorDetailsReg.Compensation_Channel = 0;
ADIJake 0:85855ecd3257 2144 }
ADIJake 0:85855ecd3257 2145 else
ADIJake 0:85855ecd3257 2146 {
ADIJake 0:85855ecd3257 2147 ADI_SENSE_LOG_ERROR("Invalid compensation channel specified for digital sensor");
ADIJake 0:85855ecd3257 2148 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2149 }
ADIJake 0:85855ecd3257 2150
ADIJake 0:85855ecd3257 2151 if (pChannelConfig->measurementUnit == ADI_SENSE_1000_MEASUREMENT_UNIT_UNSPECIFIED)
ADIJake 0:85855ecd3257 2152 {
ADIJake 0:85855ecd3257 2153 sensorDetailsReg.Measurement_Units = ADISENSE_CORE_SENSOR_DETAILS_UNITS_UNSPECIFIED;
ADIJake 0:85855ecd3257 2154 }
ADIJake 0:85855ecd3257 2155 else
ADIJake 0:85855ecd3257 2156 {
ADIJake 0:85855ecd3257 2157 ADI_SENSE_LOG_ERROR("Invalid measurement unit specified for digital channel");
ADIJake 0:85855ecd3257 2158 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2159 }
ADIJake 0:85855ecd3257 2160
ADIJake 0:85855ecd3257 2161 if (pChannelConfig->disablePublishing)
ADIJake 0:85855ecd3257 2162 sensorDetailsReg.Do_Not_Publish = 1;
ADIJake 0:85855ecd3257 2163 else
ADIJake 0:85855ecd3257 2164 sensorDetailsReg.Do_Not_Publish = 0;
ADIJake 0:85855ecd3257 2165
ADIJake 0:85855ecd3257 2166 if (pChannelConfig->enableUnityLut)
ADIJake 0:85855ecd3257 2167 sensorDetailsReg.Unity_LUT_Select = 1;
ADIJake 0:85855ecd3257 2168 else
ADIJake 0:85855ecd3257 2169 sensorDetailsReg.Unity_LUT_Select = 0;
ADIJake 0:85855ecd3257 2170
ADIJake 0:85855ecd3257 2171 sensorDetailsReg.Vbias = 0;
ADIJake 0:85855ecd3257 2172 sensorDetailsReg.Reference_Buffer_Disable = 1;
ADIJake 0:85855ecd3257 2173
ADIJake 0:85855ecd3257 2174 WRITE_REG_U32(hDevice, sensorDetailsReg.VALUE32, CORE_SENSOR_DETAILSn(eChannelId));
ADIJake 0:85855ecd3257 2175
ADIJake 0:85855ecd3257 2176 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2177 }
ADIJake 0:85855ecd3257 2178
ADIJake 0:85855ecd3257 2179 static ADI_SENSE_RESULT adi_sense_SetDigitalSensorCommands(
ADIJake 0:85855ecd3257 2180 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2181 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2182 ADI_SENSE_1000_DIGITAL_SENSOR_COMMAND *pConfigCommand,
ADIJake 0:85855ecd3257 2183 ADI_SENSE_1000_DIGITAL_SENSOR_COMMAND *pDataRequestCommand)
ADIJake 0:85855ecd3257 2184 {
ADIJake 0:85855ecd3257 2185 ADI_ADISENSE_CORE_Digital_Sensor_Num_Cmds_t numCmdsReg;
ADIJake 0:85855ecd3257 2186
ADIJake 0:85855ecd3257 2187 numCmdsReg.VALUE8 = REG_RESET_VAL(CORE_DIGITAL_SENSOR_NUM_CMDSn);
ADIJake 0:85855ecd3257 2188
ADIJake 0:85855ecd3257 2189 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_NUM_CMDS_DIGITAL_SENSOR_NUM_CFG_CMDS,
ADIJake 0:85855ecd3257 2190 pConfigCommand->commandLength);
ADIJake 0:85855ecd3257 2191 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_NUM_CMDS_DIGITAL_SENSOR_NUM_READ_CMDS,
ADIJake 0:85855ecd3257 2192 pDataRequestCommand->commandLength);
ADIJake 0:85855ecd3257 2193
ADIJake 0:85855ecd3257 2194 numCmdsReg.Digital_Sensor_Num_Cfg_Cmds = pConfigCommand->commandLength;
ADIJake 0:85855ecd3257 2195 numCmdsReg.Digital_Sensor_Num_Read_Cmds = pDataRequestCommand->commandLength;
ADIJake 0:85855ecd3257 2196
ADIJake 0:85855ecd3257 2197 WRITE_REG_U8(hDevice, numCmdsReg.VALUE8,
ADIJake 0:85855ecd3257 2198 CORE_DIGITAL_SENSOR_NUM_CMDSn(eChannelId));
ADIJake 0:85855ecd3257 2199
ADIJake 0:85855ecd3257 2200 /*
ADIJake 0:85855ecd3257 2201 * NOTE - the fall-through cases in the switch statement below are
ADIJake 0:85855ecd3257 2202 * intentional, so temporarily disable related compiler warnings which may
ADIJake 0:85855ecd3257 2203 * be produced here by GCC
ADIJake 0:85855ecd3257 2204 */
ADIJake 0:85855ecd3257 2205 #ifndef __CC_ARM
ADIJake 0:85855ecd3257 2206 #pragma GCC diagnostic push
ADIJake 0:85855ecd3257 2207 #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
ADIJake 0:85855ecd3257 2208 #endif
ADIJake 0:85855ecd3257 2209
ADIJake 0:85855ecd3257 2210 switch (pConfigCommand->commandLength)
ADIJake 0:85855ecd3257 2211 {
ADIJake 0:85855ecd3257 2212 case 7:
ADIJake 0:85855ecd3257 2213 WRITE_REG_U8(hDevice, pConfigCommand->command[6],
ADIJake 0:85855ecd3257 2214 CORE_DIGITAL_SENSOR_COMMAND7n(eChannelId));
ADIJake 0:85855ecd3257 2215 case 6:
ADIJake 0:85855ecd3257 2216 WRITE_REG_U8(hDevice, pConfigCommand->command[5],
ADIJake 0:85855ecd3257 2217 CORE_DIGITAL_SENSOR_COMMAND6n(eChannelId));
ADIJake 0:85855ecd3257 2218 case 5:
ADIJake 0:85855ecd3257 2219 WRITE_REG_U8(hDevice, pConfigCommand->command[4],
ADIJake 0:85855ecd3257 2220 CORE_DIGITAL_SENSOR_COMMAND5n(eChannelId));
ADIJake 0:85855ecd3257 2221 case 4:
ADIJake 0:85855ecd3257 2222 WRITE_REG_U8(hDevice, pConfigCommand->command[3],
ADIJake 0:85855ecd3257 2223 CORE_DIGITAL_SENSOR_COMMAND4n(eChannelId));
ADIJake 0:85855ecd3257 2224 case 3:
ADIJake 0:85855ecd3257 2225 WRITE_REG_U8(hDevice, pConfigCommand->command[2],
ADIJake 0:85855ecd3257 2226 CORE_DIGITAL_SENSOR_COMMAND3n(eChannelId));
ADIJake 0:85855ecd3257 2227 case 2:
ADIJake 0:85855ecd3257 2228 WRITE_REG_U8(hDevice, pConfigCommand->command[1],
ADIJake 0:85855ecd3257 2229 CORE_DIGITAL_SENSOR_COMMAND2n(eChannelId));
ADIJake 0:85855ecd3257 2230 case 1:
ADIJake 0:85855ecd3257 2231 WRITE_REG_U8(hDevice, pConfigCommand->command[0],
ADIJake 0:85855ecd3257 2232 CORE_DIGITAL_SENSOR_COMMAND1n(eChannelId));
ADIJake 0:85855ecd3257 2233 case 0:
ADIJake 0:85855ecd3257 2234 default:
ADIJake 0:85855ecd3257 2235 break;
ADIJake 0:85855ecd3257 2236 };
ADIJake 0:85855ecd3257 2237
ADIJake 0:85855ecd3257 2238 switch (pDataRequestCommand->commandLength)
ADIJake 0:85855ecd3257 2239 {
ADIJake 0:85855ecd3257 2240 case 7:
ADIJake 0:85855ecd3257 2241 WRITE_REG_U8(hDevice, pDataRequestCommand->command[6],
ADIJake 0:85855ecd3257 2242 CORE_DIGITAL_SENSOR_READ_CMD7n(eChannelId));
ADIJake 0:85855ecd3257 2243 case 6:
ADIJake 0:85855ecd3257 2244 WRITE_REG_U8(hDevice, pDataRequestCommand->command[5],
ADIJake 0:85855ecd3257 2245 CORE_DIGITAL_SENSOR_READ_CMD6n(eChannelId));
ADIJake 0:85855ecd3257 2246 case 5:
ADIJake 0:85855ecd3257 2247 WRITE_REG_U8(hDevice, pDataRequestCommand->command[4],
ADIJake 0:85855ecd3257 2248 CORE_DIGITAL_SENSOR_READ_CMD5n(eChannelId));
ADIJake 0:85855ecd3257 2249 case 4:
ADIJake 0:85855ecd3257 2250 WRITE_REG_U8(hDevice, pDataRequestCommand->command[3],
ADIJake 0:85855ecd3257 2251 CORE_DIGITAL_SENSOR_READ_CMD4n(eChannelId));
ADIJake 0:85855ecd3257 2252 case 3:
ADIJake 0:85855ecd3257 2253 WRITE_REG_U8(hDevice, pDataRequestCommand->command[2],
ADIJake 0:85855ecd3257 2254 CORE_DIGITAL_SENSOR_READ_CMD3n(eChannelId));
ADIJake 0:85855ecd3257 2255 case 2:
ADIJake 0:85855ecd3257 2256 WRITE_REG_U8(hDevice, pDataRequestCommand->command[1],
ADIJake 0:85855ecd3257 2257 CORE_DIGITAL_SENSOR_READ_CMD2n(eChannelId));
ADIJake 0:85855ecd3257 2258 case 1:
ADIJake 0:85855ecd3257 2259 WRITE_REG_U8(hDevice, pDataRequestCommand->command[0],
ADIJake 0:85855ecd3257 2260 CORE_DIGITAL_SENSOR_READ_CMD1n(eChannelId));
ADIJake 0:85855ecd3257 2261 case 0:
ADIJake 0:85855ecd3257 2262 default:
ADIJake 0:85855ecd3257 2263 break;
ADIJake 0:85855ecd3257 2264 };
ADIJake 0:85855ecd3257 2265
ADIJake 0:85855ecd3257 2266 /* Re-enable the implicit-fallthrough warning */
ADIJake 0:85855ecd3257 2267 #ifndef __CC_ARM
ADIJake 0:85855ecd3257 2268 #pragma GCC diagnostic pop
ADIJake 0:85855ecd3257 2269 #endif
ADIJake 0:85855ecd3257 2270
ADIJake 0:85855ecd3257 2271 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2272 }
ADIJake 0:85855ecd3257 2273
ADIJake 0:85855ecd3257 2274 static ADI_SENSE_RESULT adi_sense_SetDigitalSensorFormat(
ADIJake 0:85855ecd3257 2275 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2276 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2277 ADI_SENSE_1000_DIGITAL_SENSOR_DATA_FORMAT *pDataFormat)
ADIJake 0:85855ecd3257 2278 {
ADIJake 0:85855ecd3257 2279 ADI_ADISENSE_CORE_Digital_Sensor_Config_t sensorConfigReg;
ADIJake 0:85855ecd3257 2280
ADIJake 0:85855ecd3257 2281 sensorConfigReg.VALUE16 = REG_RESET_VAL(CORE_DIGITAL_SENSOR_CONFIGn);
ADIJake 0:85855ecd3257 2282
ADIJake 0:85855ecd3257 2283 if (pDataFormat->coding != ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_NONE)
ADIJake 0:85855ecd3257 2284 {
ADIJake 0:85855ecd3257 2285 if (pDataFormat->frameLength == 0)
ADIJake 0:85855ecd3257 2286 {
ADIJake 0:85855ecd3257 2287 ADI_SENSE_LOG_ERROR("Invalid frame length specified for digital sensor data format");
ADIJake 0:85855ecd3257 2288 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2289 }
ADIJake 0:85855ecd3257 2290 if (pDataFormat->numDataBits == 0)
ADIJake 0:85855ecd3257 2291 {
ADIJake 0:85855ecd3257 2292 ADI_SENSE_LOG_ERROR("Invalid frame length specified for digital sensor data format");
ADIJake 0:85855ecd3257 2293 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2294 }
ADIJake 0:85855ecd3257 2295
ADIJake 0:85855ecd3257 2296 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_CONFIG_DIGITAL_SENSOR_READ_BYTES,
ADIJake 0:85855ecd3257 2297 pDataFormat->frameLength - 1);
ADIJake 0:85855ecd3257 2298 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_CONFIG_DIGITAL_SENSOR_DATA_BITS,
ADIJake 0:85855ecd3257 2299 pDataFormat->numDataBits - 1);
ADIJake 0:85855ecd3257 2300 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_CONFIG_DIGITAL_SENSOR_BIT_OFFSET,
ADIJake 0:85855ecd3257 2301 pDataFormat->bitOffset);
ADIJake 0:85855ecd3257 2302
ADIJake 0:85855ecd3257 2303 sensorConfigReg.Digital_Sensor_Read_Bytes = pDataFormat->frameLength - 1;
ADIJake 0:85855ecd3257 2304 sensorConfigReg.Digital_Sensor_Data_Bits = pDataFormat->numDataBits - 1;
ADIJake 0:85855ecd3257 2305 sensorConfigReg.Digital_Sensor_Bit_Offset = pDataFormat->bitOffset;
ADIJake 0:85855ecd3257 2306 sensorConfigReg.Digital_Sensor_Left_Aligned = pDataFormat->leftJustified ? 1 : 0;
ADIJake 0:85855ecd3257 2307 sensorConfigReg.Digital_Sensor_Little_Endian = pDataFormat->littleEndian ? 1 : 0;
ADIJake 0:85855ecd3257 2308
ADIJake 0:85855ecd3257 2309 switch (pDataFormat->coding)
ADIJake 0:85855ecd3257 2310 {
ADIJake 0:85855ecd3257 2311 case ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_UNIPOLAR:
ADIJake 0:85855ecd3257 2312 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_UNIPOLAR;
ADIJake 0:85855ecd3257 2313 break;
ADIJake 0:85855ecd3257 2314 case ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_TWOS_COMPLEMENT:
ADIJake 0:85855ecd3257 2315 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_TWOS_COMPL;
ADIJake 0:85855ecd3257 2316 break;
ADIJake 0:85855ecd3257 2317 case ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_OFFSET_BINARY:
ADIJake 0:85855ecd3257 2318 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_OFFSET_BINARY;
ADIJake 0:85855ecd3257 2319 break;
ADIJake 0:85855ecd3257 2320 default:
ADIJake 0:85855ecd3257 2321 ADI_SENSE_LOG_ERROR("Invalid coding specified for digital sensor data format");
ADIJake 0:85855ecd3257 2322 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2323 }
ADIJake 0:85855ecd3257 2324 }
ADIJake 0:85855ecd3257 2325 else
ADIJake 0:85855ecd3257 2326 {
ADIJake 0:85855ecd3257 2327 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_NONE;
ADIJake 0:85855ecd3257 2328 }
ADIJake 0:85855ecd3257 2329
ADIJake 0:85855ecd3257 2330 WRITE_REG_U16(hDevice, sensorConfigReg.VALUE16,
ADIJake 0:85855ecd3257 2331 CORE_DIGITAL_SENSOR_CONFIGn(eChannelId));
ADIJake 0:85855ecd3257 2332
ADIJake 0:85855ecd3257 2333
ADIJake 0:85855ecd3257 2334 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2335 }
ADIJake 0:85855ecd3257 2336
ADIJake 0:85855ecd3257 2337 static ADI_SENSE_RESULT adi_sense_SetDigitalCalibrationParam(
ADIJake 0:85855ecd3257 2338 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2339 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2340 ADI_SENSE_1000_DIGITAL_CALIBRATION_COMMAND *pCalibrationParam)
ADIJake 0:85855ecd3257 2341 {
ADIJake 0:85855ecd3257 2342 ADI_ADISENSE_CORE_Calibration_Parameter_t calibrationParamReg;
ADIJake 0:85855ecd3257 2343
ADIJake 0:85855ecd3257 2344 calibrationParamReg.VALUE32 = REG_RESET_VAL(CORE_CALIBRATION_PARAMETERn);
ADIJake 0:85855ecd3257 2345
ADIJake 0:85855ecd3257 2346 if (pCalibrationParam->enableCalibrationParam == false)
ADIJake 0:85855ecd3257 2347 calibrationParamReg.Calibration_Parameter_Enable = 0;
ADIJake 0:85855ecd3257 2348 else
ADIJake 0:85855ecd3257 2349 calibrationParamReg.Calibration_Parameter_Enable = 1;
ADIJake 0:85855ecd3257 2350
ADIJake 0:85855ecd3257 2351 CHECK_REG_FIELD_VAL(CORE_CALIBRATION_PARAMETER_CALIBRATION_PARAMETER,
ADIJake 0:85855ecd3257 2352 pCalibrationParam->calibrationParam);
ADIJake 0:85855ecd3257 2353
ADIJake 0:85855ecd3257 2354 calibrationParamReg.Calibration_Parameter = pCalibrationParam->calibrationParam;
ADIJake 0:85855ecd3257 2355
ADIJake 0:85855ecd3257 2356 WRITE_REG_U32(hDevice, calibrationParamReg.VALUE32,
ADIJake 0:85855ecd3257 2357 CORE_CALIBRATION_PARAMETERn(eChannelId));
ADIJake 0:85855ecd3257 2358
ADIJake 0:85855ecd3257 2359 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2360 }
ADIJake 0:85855ecd3257 2361
ADIJake 0:85855ecd3257 2362 static ADI_SENSE_RESULT adi_sense_SetChannelI2cSensorType(
ADIJake 0:85855ecd3257 2363 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2364 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2365 ADI_SENSE_1000_I2C_SENSOR_TYPE sensorType)
ADIJake 0:85855ecd3257 2366 {
ADIJake 0:85855ecd3257 2367 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
ADIJake 0:85855ecd3257 2368
ADIJake 0:85855ecd3257 2369 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
ADIJake 0:85855ecd3257 2370
ADIJake 0:85855ecd3257 2371 /* Ensure that the sensor type is valid for this channel */
ADIJake 0:85855ecd3257 2372 switch(sensorType)
ADIJake 0:85855ecd3257 2373 {
ADIJake 0:85855ecd3257 2374 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_A_DEF_L1:
ADIJake 0:85855ecd3257 2375 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_B_DEF_L1:
ADIJake 0:85855ecd3257 2376 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_A_DEF_L2:
ADIJake 0:85855ecd3257 2377 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_B_DEF_L2:
ADIJake 0:85855ecd3257 2378 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_A_ADV_L1:
ADIJake 0:85855ecd3257 2379 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_B_ADV_L1:
ADIJake 0:85855ecd3257 2380 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_A_ADV_L2:
ADIJake 0:85855ecd3257 2381 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_B_ADV_L2:
ADIJake 0:85855ecd3257 2382 case ADI_SENSE_1000_I2C_SENSOR_AMBIENTLIGHT_A_DEF_L1:
ADIJake 0:85855ecd3257 2383 case ADI_SENSE_1000_I2C_SENSOR_AMBIENTLIGHT_A_DEF_L2:
ADIJake 0:85855ecd3257 2384 case ADI_SENSE_1000_I2C_SENSOR_AMBIENTLIGHT_A_ADV_L1:
ADIJake 0:85855ecd3257 2385 case ADI_SENSE_1000_I2C_SENSOR_AMBIENTLIGHT_A_ADV_L2:
ADIJake 0:85855ecd3257 2386 sensorTypeReg.Sensor_Type = sensorType;
ADIJake 0:85855ecd3257 2387 break;
ADIJake 0:85855ecd3257 2388 default:
ADIJake 0:85855ecd3257 2389 ADI_SENSE_LOG_ERROR("Unsupported I2C sensor type %d specified", sensorType);
ADIJake 0:85855ecd3257 2390 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2391 }
ADIJake 0:85855ecd3257 2392
ADIJake 0:85855ecd3257 2393 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
ADIJake 0:85855ecd3257 2394
ADIJake 0:85855ecd3257 2395 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2396 }
ADIJake 0:85855ecd3257 2397
ADIJake 0:85855ecd3257 2398 static ADI_SENSE_RESULT adi_sense_SetChannelI2cSensorAddress(
ADIJake 0:85855ecd3257 2399 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2400 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2401 uint32_t deviceAddress)
ADIJake 0:85855ecd3257 2402 {
ADIJake 0:85855ecd3257 2403 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_ADDRESS_DIGITAL_SENSOR_ADDRESS, deviceAddress);
ADIJake 0:85855ecd3257 2404 WRITE_REG_U8(hDevice, deviceAddress, CORE_DIGITAL_SENSOR_ADDRESSn(eChannelId));
ADIJake 0:85855ecd3257 2405
ADIJake 0:85855ecd3257 2406 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2407 }
ADIJake 0:85855ecd3257 2408
ADIJake 0:85855ecd3257 2409 static ADI_SENSE_RESULT adi_sense_SetDigitalChannelComms(
ADIJake 0:85855ecd3257 2410 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2411 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2412 ADI_SENSE_1000_DIGITAL_SENSOR_COMMS *pDigitalComms)
ADIJake 0:85855ecd3257 2413 {
ADIJake 0:85855ecd3257 2414 ADI_ADISENSE_CORE_Digital_Sensor_Comms_t digitalSensorComms;
ADIJake 0:85855ecd3257 2415
ADIJake 0:85855ecd3257 2416 digitalSensorComms.VALUE16 = REG_RESET_VAL(CORE_DIGITAL_SENSOR_COMMSn);
ADIJake 0:85855ecd3257 2417
ADIJake 0:85855ecd3257 2418 if(pDigitalComms->useCustomCommsConfig)
ADIJake 0:85855ecd3257 2419 {
ADIJake 0:85855ecd3257 2420 digitalSensorComms.Digital_Sensor_Comms_En = 1;
ADIJake 0:85855ecd3257 2421
ADIJake 0:85855ecd3257 2422 if(pDigitalComms->i2cClockSpeed == ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_I2C_CLOCK_SPEED_100K)
ADIJake 0:85855ecd3257 2423 {
ADIJake 0:85855ecd3257 2424 digitalSensorComms.I2C_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_I2C_100K;
ADIJake 0:85855ecd3257 2425 }
ADIJake 0:85855ecd3257 2426 else if(pDigitalComms->i2cClockSpeed == ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_I2C_CLOCK_SPEED_400K)
ADIJake 0:85855ecd3257 2427 {
ADIJake 0:85855ecd3257 2428 digitalSensorComms.I2C_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_I2C_400K;
ADIJake 0:85855ecd3257 2429 }
ADIJake 0:85855ecd3257 2430 else
ADIJake 0:85855ecd3257 2431 {
ADIJake 0:85855ecd3257 2432 ADI_SENSE_LOG_ERROR("Invalid I2C clock speed %d specified",
ADIJake 0:85855ecd3257 2433 pDigitalComms->i2cClockSpeed);
ADIJake 0:85855ecd3257 2434 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2435 }
ADIJake 0:85855ecd3257 2436
ADIJake 0:85855ecd3257 2437 if(pDigitalComms->spiMode == ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_MODE_0)
ADIJake 0:85855ecd3257 2438 {
ADIJake 0:85855ecd3257 2439 digitalSensorComms.SPI_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_MODE_0;
ADIJake 0:85855ecd3257 2440 }
ADIJake 0:85855ecd3257 2441 else if(pDigitalComms->spiMode == ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_MODE_1)
ADIJake 0:85855ecd3257 2442 {
ADIJake 0:85855ecd3257 2443 digitalSensorComms.SPI_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_MODE_1;
ADIJake 0:85855ecd3257 2444 }
ADIJake 0:85855ecd3257 2445 else if(pDigitalComms->spiMode == ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_MODE_2)
ADIJake 0:85855ecd3257 2446 {
ADIJake 0:85855ecd3257 2447 digitalSensorComms.SPI_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_MODE_2;
ADIJake 0:85855ecd3257 2448 }
ADIJake 0:85855ecd3257 2449 else if(pDigitalComms->spiMode == ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_MODE_3)
ADIJake 0:85855ecd3257 2450 {
ADIJake 0:85855ecd3257 2451 digitalSensorComms.SPI_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_MODE_3;
ADIJake 0:85855ecd3257 2452 }
ADIJake 0:85855ecd3257 2453 else
ADIJake 0:85855ecd3257 2454 {
ADIJake 0:85855ecd3257 2455 ADI_SENSE_LOG_ERROR("Invalid SPI mode %d specified",
ADIJake 0:85855ecd3257 2456 pDigitalComms->spiMode);
ADIJake 0:85855ecd3257 2457 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2458 }
ADIJake 0:85855ecd3257 2459
ADIJake 0:85855ecd3257 2460 switch (pDigitalComms->spiClock)
ADIJake 0:85855ecd3257 2461 {
ADIJake 0:85855ecd3257 2462 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_13MHZ:
ADIJake 0:85855ecd3257 2463 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_13MHZ;
ADIJake 0:85855ecd3257 2464 break;
ADIJake 0:85855ecd3257 2465 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_6_5MHZ:
ADIJake 0:85855ecd3257 2466 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_6_5MHZ;
ADIJake 0:85855ecd3257 2467 break;
ADIJake 0:85855ecd3257 2468 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_3_25MHZ:
ADIJake 0:85855ecd3257 2469 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_3_25MHZ;
ADIJake 0:85855ecd3257 2470 break;
ADIJake 0:85855ecd3257 2471 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_1_625MHZ:
ADIJake 0:85855ecd3257 2472 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_1_625MHZ;
ADIJake 0:85855ecd3257 2473 break;
ADIJake 0:85855ecd3257 2474 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_812KHZ:
ADIJake 0:85855ecd3257 2475 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_812KHZ;
ADIJake 0:85855ecd3257 2476 break;
ADIJake 0:85855ecd3257 2477 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_406KHZ:
ADIJake 0:85855ecd3257 2478 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_406KHZ;
ADIJake 0:85855ecd3257 2479 break;
ADIJake 0:85855ecd3257 2480 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_203KHZ:
ADIJake 0:85855ecd3257 2481 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_203KHZ;
ADIJake 0:85855ecd3257 2482 break;
ADIJake 0:85855ecd3257 2483 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_101KHZ:
ADIJake 0:85855ecd3257 2484 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_101KHZ;
ADIJake 0:85855ecd3257 2485 break;
ADIJake 0:85855ecd3257 2486 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_50KHZ:
ADIJake 0:85855ecd3257 2487 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_50KHZ;
ADIJake 0:85855ecd3257 2488 break;
ADIJake 0:85855ecd3257 2489 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_25KHZ:
ADIJake 0:85855ecd3257 2490 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_25KHZ;
ADIJake 0:85855ecd3257 2491 break;
ADIJake 0:85855ecd3257 2492 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_12KHZ:
ADIJake 0:85855ecd3257 2493 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_12KHZ;
ADIJake 0:85855ecd3257 2494 break;
ADIJake 0:85855ecd3257 2495 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_6KHZ:
ADIJake 0:85855ecd3257 2496 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_6KHZ;
ADIJake 0:85855ecd3257 2497 break;
ADIJake 0:85855ecd3257 2498 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_3KHZ:
ADIJake 0:85855ecd3257 2499 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_3KHZ;
ADIJake 0:85855ecd3257 2500 break;
ADIJake 0:85855ecd3257 2501 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_1_5KHZ:
ADIJake 0:85855ecd3257 2502 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_1_5KHZ;
ADIJake 0:85855ecd3257 2503 break;
ADIJake 0:85855ecd3257 2504 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_793HZ:
ADIJake 0:85855ecd3257 2505 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_793HZ;
ADIJake 0:85855ecd3257 2506 break;
ADIJake 0:85855ecd3257 2507 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_SPI_CLOCK_396HZ:
ADIJake 0:85855ecd3257 2508 digitalSensorComms.SPI_Clock = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_SPI_396HZ;
ADIJake 0:85855ecd3257 2509 break;
ADIJake 0:85855ecd3257 2510 default:
ADIJake 0:85855ecd3257 2511 ADI_SENSE_LOG_ERROR("Invalid SPI clock %d specified",
ADIJake 0:85855ecd3257 2512 pDigitalComms->spiClock);
ADIJake 0:85855ecd3257 2513 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2514 }
ADIJake 0:85855ecd3257 2515
ADIJake 0:85855ecd3257 2516 switch (pDigitalComms->uartLineConfig)
ADIJake 0:85855ecd3257 2517 {
ADIJake 0:85855ecd3257 2518 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8N1:
ADIJake 0:85855ecd3257 2519 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8N1;
ADIJake 0:85855ecd3257 2520 break;
ADIJake 0:85855ecd3257 2521 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8N2:
ADIJake 0:85855ecd3257 2522 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8N2;
ADIJake 0:85855ecd3257 2523 break;
ADIJake 0:85855ecd3257 2524 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8N3:
ADIJake 0:85855ecd3257 2525 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8N3;
ADIJake 0:85855ecd3257 2526 break;
ADIJake 0:85855ecd3257 2527 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8E1:
ADIJake 0:85855ecd3257 2528 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8E1;
ADIJake 0:85855ecd3257 2529 break;
ADIJake 0:85855ecd3257 2530 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8E2:
ADIJake 0:85855ecd3257 2531 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8E2;
ADIJake 0:85855ecd3257 2532 break;
ADIJake 0:85855ecd3257 2533 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8E3:
ADIJake 0:85855ecd3257 2534 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8E3;
ADIJake 0:85855ecd3257 2535 break;
ADIJake 0:85855ecd3257 2536 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8O1:
ADIJake 0:85855ecd3257 2537 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8O1;
ADIJake 0:85855ecd3257 2538 break;
ADIJake 0:85855ecd3257 2539 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8O2:
ADIJake 0:85855ecd3257 2540 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8O2;
ADIJake 0:85855ecd3257 2541 break;
ADIJake 0:85855ecd3257 2542 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_LINE_CONFIG_8O3:
ADIJake 0:85855ecd3257 2543 digitalSensorComms.Uart_Mode = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_LINECONTROL_8O3;
ADIJake 0:85855ecd3257 2544 break;
ADIJake 0:85855ecd3257 2545 default:
ADIJake 0:85855ecd3257 2546 ADI_SENSE_LOG_ERROR("Invalid UART mode %d specified",
ADIJake 0:85855ecd3257 2547 pDigitalComms->uartLineConfig);
ADIJake 0:85855ecd3257 2548 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2549 }
ADIJake 0:85855ecd3257 2550
ADIJake 0:85855ecd3257 2551 switch (pDigitalComms->uartBaudRate)
ADIJake 0:85855ecd3257 2552 {
ADIJake 0:85855ecd3257 2553 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_115200:
ADIJake 0:85855ecd3257 2554 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_115200;
ADIJake 0:85855ecd3257 2555 break;
ADIJake 0:85855ecd3257 2556 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_57600:
ADIJake 0:85855ecd3257 2557 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_57600;
ADIJake 0:85855ecd3257 2558 break;
ADIJake 0:85855ecd3257 2559 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_38400:
ADIJake 0:85855ecd3257 2560 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_38400;
ADIJake 0:85855ecd3257 2561 break;
ADIJake 0:85855ecd3257 2562 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_19200:
ADIJake 0:85855ecd3257 2563 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_19200;
ADIJake 0:85855ecd3257 2564 break;
ADIJake 0:85855ecd3257 2565 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_9600:
ADIJake 0:85855ecd3257 2566 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_9600;
ADIJake 0:85855ecd3257 2567 break;
ADIJake 0:85855ecd3257 2568 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_4800:
ADIJake 0:85855ecd3257 2569 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_4800;
ADIJake 0:85855ecd3257 2570 break;
ADIJake 0:85855ecd3257 2571 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_2400:
ADIJake 0:85855ecd3257 2572 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_2400;
ADIJake 0:85855ecd3257 2573 break;
ADIJake 0:85855ecd3257 2574 case ADI_SENSE_1000_DIGITAL_SENSOR_COMMS_UART_BAUD_RATE_1200:
ADIJake 0:85855ecd3257 2575 digitalSensorComms.Uart_Baud = ADISENSE_CORE_DIGITAL_SENSOR_COMMS_UART_1200;
ADIJake 0:85855ecd3257 2576 break;
ADIJake 0:85855ecd3257 2577 default:
ADIJake 0:85855ecd3257 2578 ADI_SENSE_LOG_ERROR("Invalid UART baud rate %d specified",
ADIJake 0:85855ecd3257 2579 pDigitalComms->uartBaudRate);
ADIJake 0:85855ecd3257 2580 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2581 }
ADIJake 0:85855ecd3257 2582 }
ADIJake 0:85855ecd3257 2583 else
ADIJake 0:85855ecd3257 2584 {
ADIJake 0:85855ecd3257 2585 digitalSensorComms.Digital_Sensor_Comms_En = 0;
ADIJake 0:85855ecd3257 2586 }
ADIJake 0:85855ecd3257 2587
ADIJake 0:85855ecd3257 2588 WRITE_REG_U16(hDevice, digitalSensorComms.VALUE16, CORE_DIGITAL_SENSOR_COMMSn(eChannelId));
ADIJake 0:85855ecd3257 2589
ADIJake 0:85855ecd3257 2590 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2591 }
ADIJake 0:85855ecd3257 2592
ADIJake 0:85855ecd3257 2593 ADI_SENSE_RESULT adi_sense_SetI2cChannelConfig(
ADIJake 0:85855ecd3257 2594 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2595 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2596 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 2597 {
ADIJake 0:85855ecd3257 2598 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 2599 ADI_SENSE_1000_I2C_CHANNEL_CONFIG *pI2cChannelConfig =
ADIJake 0:85855ecd3257 2600 &pChannelConfig->i2cChannelConfig;
ADIJake 0:85855ecd3257 2601
ADIJake 0:85855ecd3257 2602 eRet = adi_sense_SetChannelI2cSensorType(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2603 pI2cChannelConfig->sensor);
ADIJake 0:85855ecd3257 2604 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2605 {
ADIJake 0:85855ecd3257 2606 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor type for channel %d",
ADIJake 0:85855ecd3257 2607 eChannelId);
ADIJake 0:85855ecd3257 2608 return eRet;
ADIJake 0:85855ecd3257 2609 }
ADIJake 0:85855ecd3257 2610
ADIJake 0:85855ecd3257 2611 eRet = adi_sense_SetChannelI2cSensorAddress(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2612 pI2cChannelConfig->deviceAddress);
ADIJake 0:85855ecd3257 2613 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2614 {
ADIJake 0:85855ecd3257 2615 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor address for channel %d",
ADIJake 0:85855ecd3257 2616 eChannelId);
ADIJake 0:85855ecd3257 2617 return eRet;
ADIJake 0:85855ecd3257 2618 }
ADIJake 0:85855ecd3257 2619
ADIJake 0:85855ecd3257 2620 eRet = adi_sense_SetChannelDigitalSensorDetails(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2621 pChannelConfig);
ADIJake 0:85855ecd3257 2622 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2623 {
ADIJake 0:85855ecd3257 2624 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor details for channel %d",
ADIJake 0:85855ecd3257 2625 eChannelId);
ADIJake 0:85855ecd3257 2626 return eRet;
ADIJake 0:85855ecd3257 2627 }
ADIJake 0:85855ecd3257 2628
ADIJake 0:85855ecd3257 2629 eRet = adi_sense_SetDigitalSensorCommands(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2630 &pI2cChannelConfig->configurationCommand,
ADIJake 0:85855ecd3257 2631 &pI2cChannelConfig->dataRequestCommand);
ADIJake 0:85855ecd3257 2632 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2633 {
ADIJake 0:85855ecd3257 2634 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor commands for channel %d",
ADIJake 0:85855ecd3257 2635 eChannelId);
ADIJake 0:85855ecd3257 2636 return eRet;
ADIJake 0:85855ecd3257 2637 }
ADIJake 0:85855ecd3257 2638
ADIJake 0:85855ecd3257 2639 eRet = adi_sense_SetDigitalSensorFormat(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2640 &pI2cChannelConfig->dataFormat);
ADIJake 0:85855ecd3257 2641 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2642 {
ADIJake 0:85855ecd3257 2643 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor data format for channel %d",
ADIJake 0:85855ecd3257 2644 eChannelId);
ADIJake 0:85855ecd3257 2645 return eRet;
ADIJake 0:85855ecd3257 2646 }
ADIJake 0:85855ecd3257 2647
ADIJake 0:85855ecd3257 2648 eRet = adi_sense_SetDigitalCalibrationParam(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2649 &pI2cChannelConfig->digitalCalibrationParam);
ADIJake 0:85855ecd3257 2650 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2651 {
ADIJake 0:85855ecd3257 2652 ADI_SENSE_LOG_ERROR("Failed to set I2C digital calibration param for channel %d",
ADIJake 0:85855ecd3257 2653 eChannelId);
ADIJake 0:85855ecd3257 2654 return eRet;
ADIJake 0:85855ecd3257 2655 }
ADIJake 0:85855ecd3257 2656
ADIJake 0:85855ecd3257 2657 eRet = adi_sense_SetDigitalChannelComms(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2658 &pI2cChannelConfig->configureComms);
ADIJake 0:85855ecd3257 2659 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2660 {
ADIJake 0:85855ecd3257 2661 ADI_SENSE_LOG_ERROR("Failed to set I2C comms for channel %d",
ADIJake 0:85855ecd3257 2662 eChannelId);
ADIJake 0:85855ecd3257 2663 return eRet;
ADIJake 0:85855ecd3257 2664 }
ADIJake 0:85855ecd3257 2665
ADIJake 0:85855ecd3257 2666 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2667 }
ADIJake 0:85855ecd3257 2668
ADIJake 0:85855ecd3257 2669 static ADI_SENSE_RESULT adi_sense_SetChannelSpiSensorType(
ADIJake 0:85855ecd3257 2670 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2671 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2672 ADI_SENSE_1000_SPI_SENSOR_TYPE sensorType)
ADIJake 0:85855ecd3257 2673 {
ADIJake 0:85855ecd3257 2674 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
ADIJake 0:85855ecd3257 2675
ADIJake 0:85855ecd3257 2676 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
ADIJake 0:85855ecd3257 2677
ADIJake 0:85855ecd3257 2678 /* Ensure that the sensor type is valid for this channel */
ADIJake 0:85855ecd3257 2679 switch(sensorType)
ADIJake 0:85855ecd3257 2680 {
ADIJake 0:85855ecd3257 2681 case ADI_SENSE_1000_SPI_SENSOR_PRESSURE_A_DEF_L1:
ADIJake 0:85855ecd3257 2682 case ADI_SENSE_1000_SPI_SENSOR_PRESSURE_A_DEF_L2:
ADIJake 0:85855ecd3257 2683 case ADI_SENSE_1000_SPI_SENSOR_PRESSURE_A_ADV_L1:
ADIJake 0:85855ecd3257 2684 case ADI_SENSE_1000_SPI_SENSOR_PRESSURE_A_ADV_L2:
ADIJake 0:85855ecd3257 2685 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_A_DEF_L1:
ADIJake 0:85855ecd3257 2686 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_B_DEF_L1:
ADIJake 0:85855ecd3257 2687 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_A_DEF_L2:
ADIJake 0:85855ecd3257 2688 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_B_DEF_L2:
ADIJake 0:85855ecd3257 2689 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_A_ADV_L1:
ADIJake 0:85855ecd3257 2690 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_B_ADV_L1:
ADIJake 0:85855ecd3257 2691 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_A_ADV_L2:
ADIJake 0:85855ecd3257 2692 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_B_ADV_L2:
ADIJake 0:85855ecd3257 2693 sensorTypeReg.Sensor_Type = sensorType;
ADIJake 0:85855ecd3257 2694 break;
ADIJake 0:85855ecd3257 2695 default:
ADIJake 0:85855ecd3257 2696 ADI_SENSE_LOG_ERROR("Unsupported SPI sensor type %d specified", sensorType);
ADIJake 0:85855ecd3257 2697 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2698 }
ADIJake 0:85855ecd3257 2699
ADIJake 0:85855ecd3257 2700 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
ADIJake 0:85855ecd3257 2701
ADIJake 0:85855ecd3257 2702 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2703 }
ADIJake 0:85855ecd3257 2704
ADIJake 0:85855ecd3257 2705 ADI_SENSE_RESULT adi_sense_SetSpiChannelConfig(
ADIJake 0:85855ecd3257 2706 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2707 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2708 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 2709 {
ADIJake 0:85855ecd3257 2710 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 2711 ADI_SENSE_1000_SPI_CHANNEL_CONFIG *pSpiChannelConfig =
ADIJake 0:85855ecd3257 2712 &pChannelConfig->spiChannelConfig;
ADIJake 0:85855ecd3257 2713
ADIJake 0:85855ecd3257 2714 eRet = adi_sense_SetChannelSpiSensorType(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2715 pSpiChannelConfig->sensor);
ADIJake 0:85855ecd3257 2716 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2717 {
ADIJake 0:85855ecd3257 2718 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor type for channel %d",
ADIJake 0:85855ecd3257 2719 eChannelId);
ADIJake 0:85855ecd3257 2720 return eRet;
ADIJake 0:85855ecd3257 2721 }
ADIJake 0:85855ecd3257 2722
ADIJake 0:85855ecd3257 2723 eRet = adi_sense_SetChannelDigitalSensorDetails(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2724 pChannelConfig);
ADIJake 0:85855ecd3257 2725 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2726 {
ADIJake 0:85855ecd3257 2727 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor details for channel %d",
ADIJake 0:85855ecd3257 2728 eChannelId);
ADIJake 0:85855ecd3257 2729 return eRet;
ADIJake 0:85855ecd3257 2730 }
ADIJake 0:85855ecd3257 2731
ADIJake 0:85855ecd3257 2732 eRet = adi_sense_SetDigitalSensorCommands(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2733 &pSpiChannelConfig->configurationCommand,
ADIJake 0:85855ecd3257 2734 &pSpiChannelConfig->dataRequestCommand);
ADIJake 0:85855ecd3257 2735 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2736 {
ADIJake 0:85855ecd3257 2737 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor commands for channel %d",
ADIJake 0:85855ecd3257 2738 eChannelId);
ADIJake 0:85855ecd3257 2739 return eRet;
ADIJake 0:85855ecd3257 2740 }
ADIJake 0:85855ecd3257 2741
ADIJake 0:85855ecd3257 2742 eRet = adi_sense_SetDigitalSensorFormat(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2743 &pSpiChannelConfig->dataFormat);
ADIJake 0:85855ecd3257 2744 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2745 {
ADIJake 0:85855ecd3257 2746 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor data format for channel %d",
ADIJake 0:85855ecd3257 2747 eChannelId);
ADIJake 0:85855ecd3257 2748 return eRet;
ADIJake 0:85855ecd3257 2749 }
ADIJake 0:85855ecd3257 2750
ADIJake 0:85855ecd3257 2751 eRet = adi_sense_SetDigitalCalibrationParam(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2752 &pSpiChannelConfig->digitalCalibrationParam);
ADIJake 0:85855ecd3257 2753 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2754 {
ADIJake 0:85855ecd3257 2755 ADI_SENSE_LOG_ERROR("Failed to set SPI digital calibration param for channel %d",
ADIJake 0:85855ecd3257 2756 eChannelId);
ADIJake 0:85855ecd3257 2757 return eRet;
ADIJake 0:85855ecd3257 2758 }
ADIJake 0:85855ecd3257 2759
ADIJake 0:85855ecd3257 2760 eRet = adi_sense_SetDigitalChannelComms(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2761 &pSpiChannelConfig->configureComms);
ADIJake 0:85855ecd3257 2762 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2763 {
ADIJake 0:85855ecd3257 2764 ADI_SENSE_LOG_ERROR("Failed to set SPI comms for channel %d",
ADIJake 0:85855ecd3257 2765 eChannelId);
ADIJake 0:85855ecd3257 2766 return eRet;
ADIJake 0:85855ecd3257 2767 }
ADIJake 0:85855ecd3257 2768
ADIJake 0:85855ecd3257 2769 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2770 }
ADIJake 0:85855ecd3257 2771
ADIJake 0:85855ecd3257 2772 static ADI_SENSE_RESULT adi_sense_SetChannelUartSensorType(
ADIJake 0:85855ecd3257 2773 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2774 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2775 ADI_SENSE_1000_UART_SENSOR_TYPE sensorType)
ADIJake 0:85855ecd3257 2776 {
ADIJake 0:85855ecd3257 2777 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
ADIJake 0:85855ecd3257 2778
ADIJake 0:85855ecd3257 2779 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
ADIJake 0:85855ecd3257 2780
ADIJake 0:85855ecd3257 2781 /* Ensure that the sensor type is valid for this channel */
ADIJake 0:85855ecd3257 2782 switch(sensorType)
ADIJake 0:85855ecd3257 2783 {
ADIJake 0:85855ecd3257 2784 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_A_DEF_L1:
ADIJake 0:85855ecd3257 2785 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_B_DEF_L1:
ADIJake 0:85855ecd3257 2786 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_A_DEF_L2:
ADIJake 0:85855ecd3257 2787 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_B_DEF_L2:
ADIJake 0:85855ecd3257 2788 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_A_ADV_L1:
ADIJake 0:85855ecd3257 2789 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_B_ADV_L1:
ADIJake 0:85855ecd3257 2790 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_A_ADV_L2:
ADIJake 0:85855ecd3257 2791 case ADI_SENSE_1000_UART_SENSOR_UART_CO2_B_ADV_L2:
ADIJake 0:85855ecd3257 2792 sensorTypeReg.Sensor_Type = sensorType;
ADIJake 0:85855ecd3257 2793 break;
ADIJake 0:85855ecd3257 2794 default:
ADIJake 0:85855ecd3257 2795 ADI_SENSE_LOG_ERROR("Unsupported UART sensor type %d specified", sensorType);
ADIJake 0:85855ecd3257 2796 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 2797 }
ADIJake 0:85855ecd3257 2798
ADIJake 0:85855ecd3257 2799 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
ADIJake 0:85855ecd3257 2800
ADIJake 0:85855ecd3257 2801 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2802 }
ADIJake 0:85855ecd3257 2803
ADIJake 0:85855ecd3257 2804 ADI_SENSE_RESULT adi_sense_SetUartChannelConfig(
ADIJake 0:85855ecd3257 2805 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2806 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2807 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 2808 {
ADIJake 0:85855ecd3257 2809 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 2810 ADI_SENSE_1000_UART_CHANNEL_CONFIG *pUartChannelConfig =
ADIJake 0:85855ecd3257 2811 &pChannelConfig->uartChannelConfig;
ADIJake 0:85855ecd3257 2812
ADIJake 0:85855ecd3257 2813 eRet = adi_sense_SetChannelUartSensorType(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2814 pUartChannelConfig->sensor);
ADIJake 0:85855ecd3257 2815 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2816 {
ADIJake 0:85855ecd3257 2817 ADI_SENSE_LOG_ERROR("Failed to set UART sensor type for channel %d",
ADIJake 0:85855ecd3257 2818 eChannelId);
ADIJake 0:85855ecd3257 2819 return eRet;
ADIJake 0:85855ecd3257 2820 }
ADIJake 0:85855ecd3257 2821
ADIJake 0:85855ecd3257 2822 eRet = adi_sense_SetChannelDigitalSensorDetails(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2823 pChannelConfig);
ADIJake 0:85855ecd3257 2824 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2825 {
ADIJake 0:85855ecd3257 2826 ADI_SENSE_LOG_ERROR("Failed to set UART sensor details for channel %d",
ADIJake 0:85855ecd3257 2827 eChannelId);
ADIJake 0:85855ecd3257 2828 return eRet;
ADIJake 0:85855ecd3257 2829 }
ADIJake 0:85855ecd3257 2830
ADIJake 0:85855ecd3257 2831 eRet = adi_sense_SetDigitalCalibrationParam(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2832 &pUartChannelConfig->digitalCalibrationParam);
ADIJake 0:85855ecd3257 2833 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2834 {
ADIJake 0:85855ecd3257 2835 ADI_SENSE_LOG_ERROR("Failed to set UART digital calibration param for channel %d",
ADIJake 0:85855ecd3257 2836 eChannelId);
ADIJake 0:85855ecd3257 2837 return eRet;
ADIJake 0:85855ecd3257 2838 }
ADIJake 0:85855ecd3257 2839
ADIJake 0:85855ecd3257 2840 eRet = adi_sense_SetDigitalChannelComms(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2841 &pUartChannelConfig->configureComms);
ADIJake 0:85855ecd3257 2842 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2843 {
ADIJake 0:85855ecd3257 2844 ADI_SENSE_LOG_ERROR("Failed to set UART comms for channel %d",
ADIJake 0:85855ecd3257 2845 eChannelId);
ADIJake 0:85855ecd3257 2846 return eRet;
ADIJake 0:85855ecd3257 2847 }
ADIJake 0:85855ecd3257 2848
ADIJake 0:85855ecd3257 2849
ADIJake 0:85855ecd3257 2850 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2851 }
ADIJake 0:85855ecd3257 2852
ADIJake 0:85855ecd3257 2853 ADI_SENSE_RESULT adi_sense_1000_SetChannelThresholdLimits(
ADIJake 0:85855ecd3257 2854 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2855 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2856 float32_t fHighThresholdLimit,
ADIJake 0:85855ecd3257 2857 float32_t fLowThresholdLimit)
ADIJake 0:85855ecd3257 2858 {
ADIJake 0:85855ecd3257 2859 /*
ADIJake 0:85855ecd3257 2860 * If the low/high limits are *both* set to 0 in memory, or NaNs, assume
ADIJake 0:85855ecd3257 2861 * that they are unset, or not required, and use infinity defaults instead
ADIJake 0:85855ecd3257 2862 */
ADIJake 0:85855ecd3257 2863 if (fHighThresholdLimit == 0.0f && fLowThresholdLimit == 0.0f)
ADIJake 0:85855ecd3257 2864 {
ADIJake 0:85855ecd3257 2865 fHighThresholdLimit = INFINITY;
ADIJake 0:85855ecd3257 2866 fLowThresholdLimit = -INFINITY;
ADIJake 0:85855ecd3257 2867 }
ADIJake 0:85855ecd3257 2868 else
ADIJake 0:85855ecd3257 2869 {
ADIJake 0:85855ecd3257 2870 if (isnan(fHighThresholdLimit))
ADIJake 0:85855ecd3257 2871 fHighThresholdLimit = INFINITY;
ADIJake 0:85855ecd3257 2872 if (isnan(fLowThresholdLimit))
ADIJake 0:85855ecd3257 2873 fLowThresholdLimit = -INFINITY;
ADIJake 0:85855ecd3257 2874 }
ADIJake 0:85855ecd3257 2875
ADIJake 0:85855ecd3257 2876 WRITE_REG_FLOAT(hDevice, fHighThresholdLimit,
ADIJake 0:85855ecd3257 2877 CORE_HIGH_THRESHOLD_LIMITn(eChannelId));
ADIJake 0:85855ecd3257 2878 WRITE_REG_FLOAT(hDevice, fLowThresholdLimit,
ADIJake 0:85855ecd3257 2879 CORE_LOW_THRESHOLD_LIMITn(eChannelId));
ADIJake 0:85855ecd3257 2880
ADIJake 0:85855ecd3257 2881 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2882 }
ADIJake 0:85855ecd3257 2883
ADIJake 0:85855ecd3257 2884 ADI_SENSE_RESULT adi_sense_1000_SetOffsetGain(
ADIJake 0:85855ecd3257 2885 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2886 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2887 float32_t fOffsetAdjustment,
ADIJake 0:85855ecd3257 2888 float32_t fGainAdjustment)
ADIJake 0:85855ecd3257 2889 {
ADIJake 0:85855ecd3257 2890 /* Replace with default values if NaNs are specified (or 0.0 for gain) */
ADIJake 0:85855ecd3257 2891 if (isnan(fGainAdjustment) || (fGainAdjustment == 0.0f))
ADIJake 0:85855ecd3257 2892 fGainAdjustment = 1.0f;
ADIJake 0:85855ecd3257 2893 if (isnan(fOffsetAdjustment))
ADIJake 0:85855ecd3257 2894 fOffsetAdjustment = 0.0f;
ADIJake 0:85855ecd3257 2895
ADIJake 0:85855ecd3257 2896 WRITE_REG_FLOAT(hDevice, fGainAdjustment, CORE_SENSOR_GAINn(eChannelId));
ADIJake 0:85855ecd3257 2897 WRITE_REG_FLOAT(hDevice, fOffsetAdjustment, CORE_SENSOR_OFFSETn(eChannelId));
ADIJake 0:85855ecd3257 2898
ADIJake 0:85855ecd3257 2899 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2900 }
ADIJake 0:85855ecd3257 2901
ADIJake 0:85855ecd3257 2902 ADI_SENSE_RESULT adi_sense_1000_SetSensorParameter(
ADIJake 0:85855ecd3257 2903 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2904 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2905 float32_t fSensorParam)
ADIJake 0:85855ecd3257 2906 {
ADIJake 0:85855ecd3257 2907 if (fSensorParam == 0.0f)
ADIJake 0:85855ecd3257 2908 fSensorParam = NAN;
ADIJake 0:85855ecd3257 2909
ADIJake 0:85855ecd3257 2910 WRITE_REG_FLOAT(hDevice, fSensorParam, CORE_SENSOR_PARAMETERn(eChannelId));
ADIJake 0:85855ecd3257 2911
ADIJake 0:85855ecd3257 2912 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2913 }
ADIJake 0:85855ecd3257 2914
ADIJake 0:85855ecd3257 2915 ADI_SENSE_RESULT adi_sense_1000_SetChannelSettlingTime(
ADIJake 0:85855ecd3257 2916 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2917 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2918 uint32_t nSettlingTime)
ADIJake 0:85855ecd3257 2919 {
ADIJake 0:85855ecd3257 2920 ADI_ADISENSE_CORE_Settling_Time_t settlingTimeReg;
ADIJake 0:85855ecd3257 2921
ADIJake 0:85855ecd3257 2922 if (nSettlingTime < (1 << 12))
ADIJake 0:85855ecd3257 2923 {
ADIJake 0:85855ecd3257 2924 settlingTimeReg.Settling_Time_Units = ADISENSE_CORE_SETTLING_TIME_MICROSECONDS;
ADIJake 0:85855ecd3257 2925 }
ADIJake 0:85855ecd3257 2926 else if (nSettlingTime < (1000 * (1 << 12)))
ADIJake 0:85855ecd3257 2927 {
ADIJake 0:85855ecd3257 2928 settlingTimeReg.Settling_Time_Units = ADISENSE_CORE_SETTLING_TIME_MILLISECONDS;
ADIJake 0:85855ecd3257 2929 nSettlingTime /= 1000;
ADIJake 0:85855ecd3257 2930 }
ADIJake 0:85855ecd3257 2931 else
ADIJake 0:85855ecd3257 2932 {
ADIJake 0:85855ecd3257 2933 settlingTimeReg.Settling_Time_Units = ADISENSE_CORE_SETTLING_TIME_SECONDS;
ADIJake 0:85855ecd3257 2934 nSettlingTime /= 1000000;
ADIJake 0:85855ecd3257 2935 }
ADIJake 0:85855ecd3257 2936
ADIJake 0:85855ecd3257 2937 CHECK_REG_FIELD_VAL(CORE_SETTLING_TIME_SETTLING_TIME, nSettlingTime);
ADIJake 0:85855ecd3257 2938 settlingTimeReg.Settling_Time = nSettlingTime;
ADIJake 0:85855ecd3257 2939
ADIJake 0:85855ecd3257 2940 WRITE_REG_U16(hDevice, settlingTimeReg.VALUE16, CORE_SETTLING_TIMEn(eChannelId));
ADIJake 0:85855ecd3257 2941
ADIJake 0:85855ecd3257 2942 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 2943 }
ADIJake 0:85855ecd3257 2944
ADIJake 0:85855ecd3257 2945 ADI_SENSE_RESULT adi_sense_1000_SetChannelConfig(
ADIJake 0:85855ecd3257 2946 ADI_SENSE_DEVICE_HANDLE hDevice,
ADIJake 0:85855ecd3257 2947 ADI_SENSE_1000_CHANNEL_ID eChannelId,
ADIJake 0:85855ecd3257 2948 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
ADIJake 0:85855ecd3257 2949 {
ADIJake 0:85855ecd3257 2950 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 2951
ADIJake 0:85855ecd3257 2952 if (! ADI_SENSE_1000_CHANNEL_IS_VIRTUAL(eChannelId))
ADIJake 0:85855ecd3257 2953 {
ADIJake 0:85855ecd3257 2954 eRet = adi_sense_1000_SetChannelCount(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2955 pChannelConfig->enableChannel ?
ADIJake 0:85855ecd3257 2956 pChannelConfig->measurementsPerCycle : 0);
ADIJake 0:85855ecd3257 2957 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2958 {
ADIJake 0:85855ecd3257 2959 ADI_SENSE_LOG_ERROR("Failed to set measurement count for channel %d",
ADIJake 0:85855ecd3257 2960 eChannelId);
ADIJake 0:85855ecd3257 2961 return eRet;
ADIJake 0:85855ecd3257 2962 }
ADIJake 0:85855ecd3257 2963
ADIJake 0:85855ecd3257 2964 eRet = adi_sense_1000_SetChannelOptions(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2965 pChannelConfig->priority,
ADIJake 0:85855ecd3257 2966 pChannelConfig->enableFFT);
ADIJake 0:85855ecd3257 2967 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2968 {
ADIJake 0:85855ecd3257 2969 ADI_SENSE_LOG_ERROR("Failed to set priority for channel %d",
ADIJake 0:85855ecd3257 2970 eChannelId);
ADIJake 0:85855ecd3257 2971 return eRet;
ADIJake 0:85855ecd3257 2972 }
ADIJake 0:85855ecd3257 2973
ADIJake 0:85855ecd3257 2974 /* If the channel is not enabled, we can skip the following steps */
ADIJake 0:85855ecd3257 2975 if (pChannelConfig->enableChannel || pChannelConfig->enableFFT)
ADIJake 0:85855ecd3257 2976 {
ADIJake 0:85855ecd3257 2977 eRet = adi_sense_1000_SetChannelSkipCount(hDevice, eChannelId,
ADIJake 0:85855ecd3257 2978 pChannelConfig->cycleSkipCount);
ADIJake 0:85855ecd3257 2979 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 2980 {
ADIJake 0:85855ecd3257 2981 ADI_SENSE_LOG_ERROR("Failed to set cycle skip count for channel %d",
ADIJake 0:85855ecd3257 2982 eChannelId);
ADIJake 0:85855ecd3257 2983 return eRet;
ADIJake 0:85855ecd3257 2984 }
ADIJake 0:85855ecd3257 2985
ADIJake 0:85855ecd3257 2986 switch (eChannelId)
ADIJake 0:85855ecd3257 2987 {
ADIJake 0:85855ecd3257 2988 case ADI_SENSE_1000_CHANNEL_ID_CJC_0:
ADIJake 0:85855ecd3257 2989 case ADI_SENSE_1000_CHANNEL_ID_CJC_1:
ADIJake 0:85855ecd3257 2990 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_0:
ADIJake 0:85855ecd3257 2991 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_1:
ADIJake 0:85855ecd3257 2992 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_2:
ADIJake 0:85855ecd3257 2993 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_3:
ADIJake 0:85855ecd3257 2994 case ADI_SENSE_1000_CHANNEL_ID_VOLTAGE_0:
ADIJake 0:85855ecd3257 2995 case ADI_SENSE_1000_CHANNEL_ID_CURRENT_0:
ADIJake 0:85855ecd3257 2996 eRet = adi_sense_SetAdcChannelConfig(hDevice, eChannelId, pChannelConfig);
ADIJake 0:85855ecd3257 2997 break;
ADIJake 0:85855ecd3257 2998 case ADI_SENSE_1000_CHANNEL_ID_I2C_0:
ADIJake 0:85855ecd3257 2999 case ADI_SENSE_1000_CHANNEL_ID_I2C_1:
ADIJake 0:85855ecd3257 3000 eRet = adi_sense_SetI2cChannelConfig(hDevice, eChannelId, pChannelConfig);
ADIJake 0:85855ecd3257 3001 break;
ADIJake 0:85855ecd3257 3002 case ADI_SENSE_1000_CHANNEL_ID_SPI_0:
ADIJake 0:85855ecd3257 3003 eRet = adi_sense_SetSpiChannelConfig(hDevice, eChannelId, pChannelConfig);
ADIJake 0:85855ecd3257 3004 break;
ADIJake 0:85855ecd3257 3005 case ADI_SENSE_1000_CHANNEL_ID_UART:
ADIJake 0:85855ecd3257 3006 eRet = adi_sense_SetUartChannelConfig(hDevice, eChannelId, pChannelConfig);
ADIJake 0:85855ecd3257 3007 break;
ADIJake 0:85855ecd3257 3008 default:
ADIJake 0:85855ecd3257 3009 ADI_SENSE_LOG_ERROR("Invalid channel ID %d specified", eChannelId);
ADIJake 0:85855ecd3257 3010 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3011 }
ADIJake 0:85855ecd3257 3012
ADIJake 0:85855ecd3257 3013 eRet = adi_sense_1000_SetChannelSettlingTime(hDevice, eChannelId,
ADIJake 0:85855ecd3257 3014 pChannelConfig->extraSettlingTime);
ADIJake 0:85855ecd3257 3015 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 3016 {
ADIJake 0:85855ecd3257 3017 ADI_SENSE_LOG_ERROR("Failed to set settling time for channel %d",
ADIJake 0:85855ecd3257 3018 eChannelId);
ADIJake 0:85855ecd3257 3019 return eRet;
ADIJake 0:85855ecd3257 3020 }
ADIJake 0:85855ecd3257 3021 }
ADIJake 0:85855ecd3257 3022 }
ADIJake 0:85855ecd3257 3023
ADIJake 0:85855ecd3257 3024 if (pChannelConfig->enableChannel || pChannelConfig->enableFFT)
ADIJake 0:85855ecd3257 3025 {
ADIJake 0:85855ecd3257 3026 /* Threshold limits can be configured individually for virtual channels */
ADIJake 0:85855ecd3257 3027 eRet = adi_sense_1000_SetChannelThresholdLimits(hDevice, eChannelId,
ADIJake 0:85855ecd3257 3028 pChannelConfig->highThreshold,
ADIJake 0:85855ecd3257 3029 pChannelConfig->lowThreshold);
ADIJake 0:85855ecd3257 3030 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 3031 {
ADIJake 0:85855ecd3257 3032 ADI_SENSE_LOG_ERROR("Failed to set threshold limits for channel %d",
ADIJake 0:85855ecd3257 3033 eChannelId);
ADIJake 0:85855ecd3257 3034 return eRet;
ADIJake 0:85855ecd3257 3035 }
ADIJake 0:85855ecd3257 3036
ADIJake 0:85855ecd3257 3037 /* Offset and gain can be configured individually for virtual channels */
ADIJake 0:85855ecd3257 3038 eRet = adi_sense_1000_SetOffsetGain(hDevice, eChannelId,
ADIJake 0:85855ecd3257 3039 pChannelConfig->offsetAdjustment,
ADIJake 0:85855ecd3257 3040 pChannelConfig->gainAdjustment);
ADIJake 0:85855ecd3257 3041 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 3042 {
ADIJake 0:85855ecd3257 3043 ADI_SENSE_LOG_ERROR("Failed to set offset/gain for channel %d",
ADIJake 0:85855ecd3257 3044 eChannelId);
ADIJake 0:85855ecd3257 3045 return eRet;
ADIJake 0:85855ecd3257 3046 }
ADIJake 0:85855ecd3257 3047
ADIJake 0:85855ecd3257 3048 /* Set sensor specific parameter */
ADIJake 0:85855ecd3257 3049 eRet = adi_sense_1000_SetSensorParameter(hDevice, eChannelId,
ADIJake 0:85855ecd3257 3050 pChannelConfig->sensorParameter);
ADIJake 0:85855ecd3257 3051 if (eRet != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 3052 {
ADIJake 0:85855ecd3257 3053 ADI_SENSE_LOG_ERROR("Failed to set sensor parameter for channel %d",
ADIJake 0:85855ecd3257 3054 eChannelId);
ADIJake 0:85855ecd3257 3055 return eRet;
ADIJake 0:85855ecd3257 3056 }
ADIJake 0:85855ecd3257 3057 }
ADIJake 0:85855ecd3257 3058
ADIJake 0:85855ecd3257 3059 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 3060 }
ADIJake 0:85855ecd3257 3061
ADIJake 0:85855ecd3257 3062 ADI_SENSE_RESULT adi_sense_SetConfig(
ADIJake 0:85855ecd3257 3063 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 3064 ADI_SENSE_CONFIG * const pConfig)
ADIJake 0:85855ecd3257 3065 {
ADIJake 0:85855ecd3257 3066 ADI_SENSE_1000_CONFIG *pDeviceConfig;
ADIJake 0:85855ecd3257 3067 ADI_SENSE_PRODUCT_ID productId;
ADIJake 0:85855ecd3257 3068 ADI_SENSE_RESULT eRet;
ADIJake 0:85855ecd3257 3069
ADIJake 0:85855ecd3257 3070 if (pConfig->productId != ADI_SENSE_PRODUCT_ID_ADSNS1000)
ADIJake 0:85855ecd3257 3071 {
ADIJake 0:85855ecd3257 3072 ADI_SENSE_LOG_ERROR("Configuration Product ID (0x%X) is not supported (0x%0X)",
ADIJake 0:85855ecd3257 3073 pConfig->productId, ADI_SENSE_PRODUCT_ID_ADSNS1000);
ADIJake 0:85855ecd3257 3074 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3075 }
ADIJake 0:85855ecd3257 3076
ADIJake 0:85855ecd3257 3077 /* Check that the actual Product ID is a match? */
ADIJake 0:85855ecd3257 3078 eRet = adi_sense_GetProductID(hDevice, &productId);
ADIJake 0:85855ecd3257 3079 if (eRet)
ADIJake 0:85855ecd3257 3080 {
ADIJake 0:85855ecd3257 3081 ADI_SENSE_LOG_ERROR("Failed to read device Product ID register");
ADIJake 0:85855ecd3257 3082 return eRet;
ADIJake 0:85855ecd3257 3083 }
ADIJake 0:85855ecd3257 3084 if (pConfig->productId != productId)
ADIJake 0:85855ecd3257 3085 {
ADIJake 0:85855ecd3257 3086 ADI_SENSE_LOG_ERROR("Configuration Product ID (0x%X) does not match device (0x%0X)",
ADIJake 0:85855ecd3257 3087 pConfig->productId, productId);
ADIJake 0:85855ecd3257 3088 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3089 }
ADIJake 0:85855ecd3257 3090
ADIJake 0:85855ecd3257 3091 pDeviceConfig = &pConfig->adisense1000;
ADIJake 0:85855ecd3257 3092
ADIJake 0:85855ecd3257 3093 eRet = adi_sense_1000_SetPowerConfig(hDevice, &pDeviceConfig->power);
ADIJake 0:85855ecd3257 3094 if (eRet)
ADIJake 0:85855ecd3257 3095 {
ADIJake 0:85855ecd3257 3096 ADI_SENSE_LOG_ERROR("Failed to set power configuration");
ADIJake 0:85855ecd3257 3097 return eRet;
ADIJake 0:85855ecd3257 3098 }
ADIJake 0:85855ecd3257 3099
ADIJake 0:85855ecd3257 3100 eRet = adi_sense_1000_SetMeasurementConfig(hDevice, &pDeviceConfig->measurement);
ADIJake 0:85855ecd3257 3101 if (eRet)
ADIJake 0:85855ecd3257 3102 {
ADIJake 0:85855ecd3257 3103 ADI_SENSE_LOG_ERROR("Failed to set measurement configuration");
ADIJake 0:85855ecd3257 3104 return eRet;
ADIJake 0:85855ecd3257 3105 }
ADIJake 0:85855ecd3257 3106
ADIJake 0:85855ecd3257 3107 eRet = adi_sense_1000_SetDiagnosticsConfig(hDevice, &pDeviceConfig->diagnostics);
ADIJake 0:85855ecd3257 3108 if (eRet)
ADIJake 0:85855ecd3257 3109 {
ADIJake 0:85855ecd3257 3110 ADI_SENSE_LOG_ERROR("Failed to set diagnostics configuration");
ADIJake 0:85855ecd3257 3111 return eRet;
ADIJake 0:85855ecd3257 3112 }
ADIJake 0:85855ecd3257 3113
ADIJake 0:85855ecd3257 3114 for (ADI_SENSE_1000_CHANNEL_ID id = ADI_SENSE_1000_CHANNEL_ID_CJC_0;
ADIJake 0:85855ecd3257 3115 id < ADI_SENSE_1000_MAX_CHANNELS;
ADIJake 0:85855ecd3257 3116 id++)
ADIJake 0:85855ecd3257 3117 {
ADIJake 0:85855ecd3257 3118 eRet = adi_sense_1000_SetChannelConfig(hDevice, id,
ADIJake 0:85855ecd3257 3119 &pDeviceConfig->channels[id]);
ADIJake 0:85855ecd3257 3120 if (eRet)
ADIJake 0:85855ecd3257 3121 {
ADIJake 0:85855ecd3257 3122 ADI_SENSE_LOG_ERROR("Failed to set channel %d configuration", id);
ADIJake 0:85855ecd3257 3123 return eRet;
ADIJake 0:85855ecd3257 3124 }
ADIJake 0:85855ecd3257 3125 }
ADIJake 0:85855ecd3257 3126
ADIJake 0:85855ecd3257 3127 eRet = adi_sense_1000_SetFftConfig(hDevice, &pDeviceConfig->fft,
ADIJake 0:85855ecd3257 3128 pDeviceConfig->channels);
ADIJake 0:85855ecd3257 3129 if (eRet)
ADIJake 0:85855ecd3257 3130 {
ADIJake 0:85855ecd3257 3131 ADI_SENSE_LOG_ERROR("Failed to set FFT configuration");
ADIJake 0:85855ecd3257 3132 return eRet;
ADIJake 0:85855ecd3257 3133 }
ADIJake 0:85855ecd3257 3134
ADIJake 0:85855ecd3257 3135 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 3136 }
ADIJake 0:85855ecd3257 3137
ADIJake 0:85855ecd3257 3138 ADI_SENSE_RESULT adi_sense_1000_SetLutData(
ADIJake 0:85855ecd3257 3139 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 3140 ADI_SENSE_1000_LUT * const pLutData)
ADIJake 0:85855ecd3257 3141 {
ADIJake 0:85855ecd3257 3142 ADI_SENSE_1000_LUT_HEADER *pLutHeader = &pLutData->header;
ADIJake 0:85855ecd3257 3143 ADI_SENSE_1000_LUT_TABLE *pLutTable = pLutData->tables;
ADIJake 0:85855ecd3257 3144 unsigned actualLength = 0;
ADIJake 0:85855ecd3257 3145
ADIJake 0:85855ecd3257 3146 if (pLutData->header.signature != ADI_SENSE_LUT_SIGNATURE)
ADIJake 0:85855ecd3257 3147 {
ADIJake 0:85855ecd3257 3148 ADI_SENSE_LOG_ERROR("LUT signature incorrect (expected 0x%X, actual 0x%X)",
ADIJake 0:85855ecd3257 3149 ADI_SENSE_LUT_SIGNATURE, pLutHeader->signature);
ADIJake 0:85855ecd3257 3150 return ADI_SENSE_INVALID_SIGNATURE;
ADIJake 0:85855ecd3257 3151 }
ADIJake 0:85855ecd3257 3152
ADIJake 0:85855ecd3257 3153 for (unsigned i = 0; i < pLutHeader->numTables; i++)
ADIJake 0:85855ecd3257 3154 {
ADIJake 0:85855ecd3257 3155 ADI_SENSE_1000_LUT_DESCRIPTOR *pDesc = &pLutTable->descriptor;
ADIJake 0:85855ecd3257 3156 ADI_SENSE_1000_LUT_TABLE_DATA *pData = &pLutTable->data;
ADIJake 0:85855ecd3257 3157 unsigned short calculatedCrc;
ADIJake 0:85855ecd3257 3158
ADIJake 0:85855ecd3257 3159 switch (pDesc->geometry)
ADIJake 0:85855ecd3257 3160 {
ADIJake 0:85855ecd3257 3161 case ADI_SENSE_1000_LUT_GEOMETRY_COEFFS:
ADIJake 0:85855ecd3257 3162 switch (pDesc->equation)
ADIJake 0:85855ecd3257 3163 {
ADIJake 0:85855ecd3257 3164 case ADI_SENSE_1000_LUT_EQUATION_POLYN:
ADIJake 0:85855ecd3257 3165 case ADI_SENSE_1000_LUT_EQUATION_POLYNEXP:
ADIJake 0:85855ecd3257 3166 case ADI_SENSE_1000_LUT_EQUATION_QUADRATIC:
ADIJake 0:85855ecd3257 3167 case ADI_SENSE_1000_LUT_EQUATION_STEINHART:
ADIJake 0:85855ecd3257 3168 case ADI_SENSE_1000_LUT_EQUATION_LOGARITHMIC:
ADIJake 0:85855ecd3257 3169 case ADI_SENSE_1000_LUT_EQUATION_BIVARIATE_POLYN:
ADIJake 0:85855ecd3257 3170 break;
ADIJake 0:85855ecd3257 3171 default:
ADIJake 0:85855ecd3257 3172 ADI_SENSE_LOG_ERROR("Invalid equation %u specified for LUT table %u",
ADIJake 0:85855ecd3257 3173 pDesc->equation, i);
ADIJake 0:85855ecd3257 3174 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3175 }
ADIJake 0:85855ecd3257 3176 break;
ADIJake 0:85855ecd3257 3177 case ADI_SENSE_1000_LUT_GEOMETRY_NES_1D:
ADIJake 0:85855ecd3257 3178 case ADI_SENSE_1000_LUT_GEOMETRY_NES_2D:
ADIJake 0:85855ecd3257 3179 case ADI_SENSE_1000_LUT_GEOMETRY_ES_1D:
ADIJake 0:85855ecd3257 3180 case ADI_SENSE_1000_LUT_GEOMETRY_ES_2D:
ADIJake 0:85855ecd3257 3181 if (pDesc->equation != ADI_SENSE_1000_LUT_EQUATION_LUT) {
ADIJake 0:85855ecd3257 3182 ADI_SENSE_LOG_ERROR("Invalid equation %u specified for LUT table %u",
ADIJake 0:85855ecd3257 3183 pDesc->equation, i);
ADIJake 0:85855ecd3257 3184 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3185 }
ADIJake 0:85855ecd3257 3186 break;
ADIJake 0:85855ecd3257 3187 default:
ADIJake 0:85855ecd3257 3188 ADI_SENSE_LOG_ERROR("Invalid geometry %u specified for LUT table %u",
ADIJake 0:85855ecd3257 3189 pDesc->geometry, i);
ADIJake 0:85855ecd3257 3190 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3191 }
ADIJake 0:85855ecd3257 3192
ADIJake 0:85855ecd3257 3193 switch (pDesc->dataType)
ADIJake 0:85855ecd3257 3194 {
ADIJake 0:85855ecd3257 3195 case ADI_SENSE_1000_LUT_DATA_TYPE_FLOAT32:
ADIJake 0:85855ecd3257 3196 case ADI_SENSE_1000_LUT_DATA_TYPE_FLOAT64:
ADIJake 0:85855ecd3257 3197 break;
ADIJake 0:85855ecd3257 3198 default:
ADIJake 0:85855ecd3257 3199 ADI_SENSE_LOG_ERROR("Invalid vector format %u specified for LUT table %u",
ADIJake 0:85855ecd3257 3200 pDesc->dataType, i);
ADIJake 0:85855ecd3257 3201 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3202 }
ADIJake 0:85855ecd3257 3203
ADIJake 0:85855ecd3257 3204 calculatedCrc = adi_sense_crc16_ccitt(pData, pDesc->length);
ADIJake 0:85855ecd3257 3205 if (calculatedCrc != pDesc->crc16)
ADIJake 0:85855ecd3257 3206 {
ADIJake 0:85855ecd3257 3207 ADI_SENSE_LOG_ERROR("CRC validation failed on LUT table %u (expected 0x%04X, actual 0x%04X)",
ADIJake 0:85855ecd3257 3208 i, pDesc->crc16, calculatedCrc);
ADIJake 0:85855ecd3257 3209 return ADI_SENSE_CRC_ERROR;
ADIJake 0:85855ecd3257 3210 }
ADIJake 0:85855ecd3257 3211
ADIJake 0:85855ecd3257 3212 actualLength += sizeof(*pDesc) + pDesc->length;
ADIJake 0:85855ecd3257 3213
ADIJake 0:85855ecd3257 3214 /* Move to the next look-up table */
ADIJake 0:85855ecd3257 3215 pLutTable = (ADI_SENSE_1000_LUT_TABLE *)((uint8_t *)pLutTable + sizeof(*pDesc) + pDesc->length);
ADIJake 0:85855ecd3257 3216 }
ADIJake 0:85855ecd3257 3217
ADIJake 0:85855ecd3257 3218 if (actualLength != pLutHeader->totalLength)
ADIJake 0:85855ecd3257 3219 {
ADIJake 0:85855ecd3257 3220 ADI_SENSE_LOG_ERROR("LUT table length mismatch (expected %u, actual %u)",
ADIJake 0:85855ecd3257 3221 pLutHeader->totalLength, actualLength);
ADIJake 0:85855ecd3257 3222 return ADI_SENSE_WRONG_SIZE;
ADIJake 0:85855ecd3257 3223 }
ADIJake 0:85855ecd3257 3224
ADIJake 0:85855ecd3257 3225 if (sizeof(*pLutHeader) + pLutHeader->totalLength > ADI_SENSE_LUT_MAX_SIZE)
ADIJake 0:85855ecd3257 3226 {
ADIJake 0:85855ecd3257 3227 ADI_SENSE_LOG_ERROR("Maximum LUT table length (%u bytes) exceeded",
ADIJake 0:85855ecd3257 3228 ADI_SENSE_LUT_MAX_SIZE);
ADIJake 0:85855ecd3257 3229 return ADI_SENSE_WRONG_SIZE;
ADIJake 0:85855ecd3257 3230 }
ADIJake 0:85855ecd3257 3231
ADIJake 0:85855ecd3257 3232 /* Write the LUT data to the device */
ADIJake 0:85855ecd3257 3233 unsigned lutSize = sizeof(*pLutHeader) + pLutHeader->totalLength;
ADIJake 0:85855ecd3257 3234 WRITE_REG_U16(hDevice, 0, CORE_LUT_OFFSET);
ADIJake 0:85855ecd3257 3235 WRITE_REG_U8_ARRAY(hDevice, (uint8_t *)pLutData, lutSize, CORE_LUT_DATA);
ADIJake 0:85855ecd3257 3236
ADIJake 0:85855ecd3257 3237 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 3238 }
ADIJake 0:85855ecd3257 3239
ADIJake 0:85855ecd3257 3240 ADI_SENSE_RESULT adi_sense_1000_SetLutDataRaw(
ADIJake 0:85855ecd3257 3241 ADI_SENSE_DEVICE_HANDLE const hDevice,
ADIJake 0:85855ecd3257 3242 ADI_SENSE_1000_LUT_RAW * const pLutData)
ADIJake 0:85855ecd3257 3243 {
ADIJake 0:85855ecd3257 3244 return adi_sense_1000_SetLutData(hDevice,
ADIJake 0:85855ecd3257 3245 (ADI_SENSE_1000_LUT *)pLutData);
ADIJake 0:85855ecd3257 3246 }
ADIJake 0:85855ecd3257 3247
ADIJake 0:85855ecd3257 3248 static ADI_SENSE_RESULT getLutTableSize(
ADIJake 0:85855ecd3257 3249 ADI_SENSE_1000_LUT_DESCRIPTOR * const pDesc,
ADIJake 0:85855ecd3257 3250 ADI_SENSE_1000_LUT_TABLE_DATA * const pData,
ADIJake 0:85855ecd3257 3251 unsigned *pLength)
ADIJake 0:85855ecd3257 3252 {
ADIJake 0:85855ecd3257 3253 switch (pDesc->geometry)
ADIJake 0:85855ecd3257 3254 {
ADIJake 0:85855ecd3257 3255 case ADI_SENSE_1000_LUT_GEOMETRY_COEFFS:
ADIJake 0:85855ecd3257 3256 if (pDesc->equation == ADI_SENSE_1000_LUT_EQUATION_BIVARIATE_POLYN)
ADIJake 0:85855ecd3257 3257 *pLength = ADI_SENSE_1000_LUT_2D_POLYN_COEFF_LIST_SIZE(pData->coeffList2d);
ADIJake 0:85855ecd3257 3258 else
ADIJake 0:85855ecd3257 3259 *pLength = ADI_SENSE_1000_LUT_COEFF_LIST_SIZE(pData->coeffList);
ADIJake 0:85855ecd3257 3260 break;
ADIJake 0:85855ecd3257 3261 case ADI_SENSE_1000_LUT_GEOMETRY_NES_1D:
ADIJake 0:85855ecd3257 3262 *pLength = ADI_SENSE_1000_LUT_1D_NES_SIZE(pData->lut1dNes);
ADIJake 0:85855ecd3257 3263 break;
ADIJake 0:85855ecd3257 3264 case ADI_SENSE_1000_LUT_GEOMETRY_NES_2D:
ADIJake 0:85855ecd3257 3265 *pLength = ADI_SENSE_1000_LUT_2D_NES_SIZE(pData->lut2dNes);
ADIJake 0:85855ecd3257 3266 break;
ADIJake 0:85855ecd3257 3267 case ADI_SENSE_1000_LUT_GEOMETRY_ES_1D:
ADIJake 0:85855ecd3257 3268 *pLength = ADI_SENSE_1000_LUT_1D_ES_SIZE(pData->lut1dEs);
ADIJake 0:85855ecd3257 3269 break;
ADIJake 0:85855ecd3257 3270 case ADI_SENSE_1000_LUT_GEOMETRY_ES_2D:
ADIJake 0:85855ecd3257 3271 *pLength = ADI_SENSE_1000_LUT_2D_ES_SIZE(pData->lut2dEs);
ADIJake 0:85855ecd3257 3272 break;
ADIJake 0:85855ecd3257 3273 default:
ADIJake 0:85855ecd3257 3274 ADI_SENSE_LOG_ERROR("Invalid LUT table geometry %d specified\r\n",
ADIJake 0:85855ecd3257 3275 pDesc->geometry);
ADIJake 0:85855ecd3257 3276 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3277 }
ADIJake 0:85855ecd3257 3278
ADIJake 0:85855ecd3257 3279 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 3280 }
ADIJake 0:85855ecd3257 3281
ADIJake 0:85855ecd3257 3282 ADI_SENSE_RESULT adi_sense_1000_AssembleLutData(
ADIJake 0:85855ecd3257 3283 ADI_SENSE_1000_LUT * pLutBuffer,
ADIJake 0:85855ecd3257 3284 unsigned nLutBufferSize,
ADIJake 0:85855ecd3257 3285 unsigned const nNumTables,
ADIJake 0:85855ecd3257 3286 ADI_SENSE_1000_LUT_DESCRIPTOR * const ppDesc[],
ADIJake 0:85855ecd3257 3287 ADI_SENSE_1000_LUT_TABLE_DATA * const ppData[])
ADIJake 0:85855ecd3257 3288 {
ADIJake 0:85855ecd3257 3289 ADI_SENSE_1000_LUT_HEADER *pHdr = &pLutBuffer->header;
ADIJake 0:85855ecd3257 3290 uint8_t *pLutTableData = (uint8_t *)pLutBuffer + sizeof(*pHdr);
ADIJake 0:85855ecd3257 3291
ADIJake 0:85855ecd3257 3292 if (sizeof(*pHdr) > nLutBufferSize)
ADIJake 0:85855ecd3257 3293 {
ADIJake 0:85855ecd3257 3294 ADI_SENSE_LOG_ERROR("Insufficient LUT buffer size provided");
ADIJake 0:85855ecd3257 3295 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3296 }
ADIJake 0:85855ecd3257 3297
ADIJake 0:85855ecd3257 3298 /* First initialise the top-level header */
ADIJake 0:85855ecd3257 3299 pHdr->signature = ADI_SENSE_LUT_SIGNATURE;
ADIJake 0:85855ecd3257 3300 pHdr->version.major = 1;
ADIJake 0:85855ecd3257 3301 pHdr->version.minor = 0;
ADIJake 0:85855ecd3257 3302 pHdr->numTables = 0;
ADIJake 0:85855ecd3257 3303 pHdr->totalLength = 0;
ADIJake 0:85855ecd3257 3304
ADIJake 0:85855ecd3257 3305 /*
ADIJake 0:85855ecd3257 3306 * Walk through the list of table pointers provided, appending the table
ADIJake 0:85855ecd3257 3307 * descriptor+data from each one to the provided LUT buffer
ADIJake 0:85855ecd3257 3308 */
ADIJake 0:85855ecd3257 3309 for (unsigned i = 0; i < nNumTables; i++)
ADIJake 0:85855ecd3257 3310 {
ADIJake 0:85855ecd3257 3311 ADI_SENSE_1000_LUT_DESCRIPTOR * const pDesc = ppDesc[i];
ADIJake 0:85855ecd3257 3312 ADI_SENSE_1000_LUT_TABLE_DATA * const pData = ppData[i];
ADIJake 0:85855ecd3257 3313 ADI_SENSE_RESULT res;
ADIJake 0:85855ecd3257 3314 unsigned dataLength = 0;
ADIJake 0:85855ecd3257 3315
ADIJake 0:85855ecd3257 3316 /* Calculate the length of the table data */
ADIJake 0:85855ecd3257 3317 res = getLutTableSize(pDesc, pData, &dataLength);
ADIJake 0:85855ecd3257 3318 if (res != ADI_SENSE_SUCCESS)
ADIJake 0:85855ecd3257 3319 return res;
ADIJake 0:85855ecd3257 3320
ADIJake 0:85855ecd3257 3321 /* Fill in the table descriptor length and CRC fields */
ADIJake 0:85855ecd3257 3322 pDesc->length = dataLength;
ADIJake 0:85855ecd3257 3323 pDesc->crc16 = adi_sense_crc16_ccitt(pData, dataLength);
ADIJake 0:85855ecd3257 3324
ADIJake 0:85855ecd3257 3325 if ((sizeof(*pHdr) + pHdr->totalLength + sizeof(*pDesc) + dataLength) > nLutBufferSize)
ADIJake 0:85855ecd3257 3326 {
ADIJake 0:85855ecd3257 3327 ADI_SENSE_LOG_ERROR("Insufficient LUT buffer size provided");
ADIJake 0:85855ecd3257 3328 return ADI_SENSE_INVALID_PARAM;
ADIJake 0:85855ecd3257 3329 }
ADIJake 0:85855ecd3257 3330
ADIJake 0:85855ecd3257 3331 /* Append the table to the LUT buffer (desc + data) */
ADIJake 0:85855ecd3257 3332 memcpy(pLutTableData + pHdr->totalLength, pDesc, sizeof(*pDesc));
ADIJake 0:85855ecd3257 3333 pHdr->totalLength += sizeof(*pDesc);
ADIJake 0:85855ecd3257 3334 memcpy(pLutTableData + pHdr->totalLength, pData, dataLength);
ADIJake 0:85855ecd3257 3335 pHdr->totalLength += dataLength;
ADIJake 0:85855ecd3257 3336
ADIJake 0:85855ecd3257 3337 pHdr->numTables++;
ADIJake 0:85855ecd3257 3338 }
ADIJake 0:85855ecd3257 3339
ADIJake 0:85855ecd3257 3340 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 3341 }
ADIJake 0:85855ecd3257 3342