ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
kevin1990
Date:
Mon Jan 08 16:39:30 2018 +0000
Revision:
20:7031cd147ed7
Child:
21:354f33c60595
v1.0_Rc4 Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kevin1990 20:7031cd147ed7 1 /*!
kevin1990 20:7031cd147ed7 2 ******************************************************************************
kevin1990 20:7031cd147ed7 3 * @file: adi_sense_1000.c
kevin1990 20:7031cd147ed7 4 * @brief: ADI Sense API implementation for ADI Sense 1000
kevin1990 20:7031cd147ed7 5 *-----------------------------------------------------------------------------
kevin1990 20:7031cd147ed7 6 */
kevin1990 20:7031cd147ed7 7
kevin1990 20:7031cd147ed7 8 /******************************************************************************
kevin1990 20:7031cd147ed7 9 Copyright (c) 2017 Emutex Ltd. / Analog Devices, Inc.
kevin1990 20:7031cd147ed7 10
kevin1990 20:7031cd147ed7 11 All rights reserved.
kevin1990 20:7031cd147ed7 12
kevin1990 20:7031cd147ed7 13 Redistribution and use in source and binary forms, with or without modification,
kevin1990 20:7031cd147ed7 14 are permitted provided that the following conditions are met:
kevin1990 20:7031cd147ed7 15 - Redistributions of source code must retain the above copyright notice,
kevin1990 20:7031cd147ed7 16 this list of conditions and the following disclaimer.
kevin1990 20:7031cd147ed7 17 - Redistributions in binary form must reproduce the above copyright notice,
kevin1990 20:7031cd147ed7 18 this list of conditions and the following disclaimer in the documentation
kevin1990 20:7031cd147ed7 19 and/or other materials provided with the distribution.
kevin1990 20:7031cd147ed7 20 - Modified versions of the software must be conspicuously marked as such.
kevin1990 20:7031cd147ed7 21 - This software is licensed solely and exclusively for use with processors
kevin1990 20:7031cd147ed7 22 manufactured by or for Analog Devices, Inc.
kevin1990 20:7031cd147ed7 23 - This software may not be combined or merged with other code in any manner
kevin1990 20:7031cd147ed7 24 that would cause the software to become subject to terms and conditions
kevin1990 20:7031cd147ed7 25 which differ from those listed here.
kevin1990 20:7031cd147ed7 26 - Neither the name of Analog Devices, Inc. nor the names of its
kevin1990 20:7031cd147ed7 27 contributors may be used to endorse or promote products derived
kevin1990 20:7031cd147ed7 28 from this software without specific prior written permission.
kevin1990 20:7031cd147ed7 29 - The use of this software may or may not infringe the patent rights of one
kevin1990 20:7031cd147ed7 30 or more patent holders. This license does not release you from the
kevin1990 20:7031cd147ed7 31 requirement that you obtain separate licenses from these patent holders
kevin1990 20:7031cd147ed7 32 to use this software.
kevin1990 20:7031cd147ed7 33
kevin1990 20:7031cd147ed7 34 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY
kevin1990 20:7031cd147ed7 35 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
kevin1990 20:7031cd147ed7 36 TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
kevin1990 20:7031cd147ed7 37 NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
kevin1990 20:7031cd147ed7 38 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES
kevin1990 20:7031cd147ed7 39 (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL
kevin1990 20:7031cd147ed7 40 PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
kevin1990 20:7031cd147ed7 41 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
kevin1990 20:7031cd147ed7 42 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
kevin1990 20:7031cd147ed7 43 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
kevin1990 20:7031cd147ed7 44 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kevin1990 20:7031cd147ed7 45 *
kevin1990 20:7031cd147ed7 46 *****************************************************************************/
kevin1990 20:7031cd147ed7 47 #include <float.h>
kevin1990 20:7031cd147ed7 48 #include <math.h>
kevin1990 20:7031cd147ed7 49 #include <string.h>
kevin1990 20:7031cd147ed7 50
kevin1990 20:7031cd147ed7 51 #include "inc/adi_sense_platform.h"
kevin1990 20:7031cd147ed7 52 #include "inc/adi_sense_api.h"
kevin1990 20:7031cd147ed7 53 #include "inc/adi_sense_1000/adi_sense_1000_api.h"
kevin1990 20:7031cd147ed7 54
kevin1990 20:7031cd147ed7 55 #include "adi_sense_1000/ADISENSE1000_REGISTERS_typedefs.h"
kevin1990 20:7031cd147ed7 56 #include "adi_sense_1000/ADISENSE1000_REGISTERS.h"
kevin1990 20:7031cd147ed7 57 #include "adi_sense_1000/adi_sense_1000_lut_data.h"
kevin1990 20:7031cd147ed7 58 #include "adi_sense_1000/adi_sense_1000_calibration.h"
kevin1990 20:7031cd147ed7 59
kevin1990 20:7031cd147ed7 60 #include "crc16.h"
kevin1990 20:7031cd147ed7 61
kevin1990 20:7031cd147ed7 62 /*
kevin1990 20:7031cd147ed7 63 * The host is expected to transfer a 16-bit command, followed by data bytes, in 2
kevin1990 20:7031cd147ed7 64 * separate transfers delineated by the CS signal and a short delay in between.
kevin1990 20:7031cd147ed7 65 *
kevin1990 20:7031cd147ed7 66 * The 16-bit command contains a right-justified 11-bit register address (offset),
kevin1990 20:7031cd147ed7 67 * and the remaining upper 5 bits are reserved as command bits assigned as follows:
kevin1990 20:7031cd147ed7 68 * [15:11] 10000b = write command, 01000b = read command, anything else is invalid
kevin1990 20:7031cd147ed7 69 * [10:0] register address (0-2047)
kevin1990 20:7031cd147ed7 70 */
kevin1990 20:7031cd147ed7 71
kevin1990 20:7031cd147ed7 72 /* Register address space is limited to 2048 bytes (11 bit address) */
kevin1990 20:7031cd147ed7 73 #define REG_COMMAND_MASK 0xF800
kevin1990 20:7031cd147ed7 74 #define REG_ADDRESS_MASK 0x07FF
kevin1990 20:7031cd147ed7 75
kevin1990 20:7031cd147ed7 76 /*
kevin1990 20:7031cd147ed7 77 * The following commands are currently supported, anything else is treated
kevin1990 20:7031cd147ed7 78 * as an error
kevin1990 20:7031cd147ed7 79 */
kevin1990 20:7031cd147ed7 80 #define REG_WRITE_COMMAND 0x8000
kevin1990 20:7031cd147ed7 81 #define REG_READ_COMMAND 0x4000
kevin1990 20:7031cd147ed7 82
kevin1990 20:7031cd147ed7 83 /*
kevin1990 20:7031cd147ed7 84 * The following bytes are sent back to the host when a command is recieved,
kevin1990 20:7031cd147ed7 85 * to be used by the host to verify that we were ready to receive the command.
kevin1990 20:7031cd147ed7 86 */
kevin1990 20:7031cd147ed7 87 #define REG_COMMAND_RESP_0 0xF0
kevin1990 20:7031cd147ed7 88 #define REG_COMMAND_RESP_1 0xE1
kevin1990 20:7031cd147ed7 89
kevin1990 20:7031cd147ed7 90 /*
kevin1990 20:7031cd147ed7 91 * The following minimum delay must be inserted after each SPI transfer to allow
kevin1990 20:7031cd147ed7 92 * time for it to be processed by the device
kevin1990 20:7031cd147ed7 93 */
kevin1990 20:7031cd147ed7 94 #define POST_SPI_TRANSFER_DELAY_USEC (20)
kevin1990 20:7031cd147ed7 95
kevin1990 20:7031cd147ed7 96 /*
kevin1990 20:7031cd147ed7 97 * The following macros are used to encapsulate the register access code
kevin1990 20:7031cd147ed7 98 * to improve readability in the functions further below in this file
kevin1990 20:7031cd147ed7 99 */
kevin1990 20:7031cd147ed7 100 #define STRINGIFY(name) #name
kevin1990 20:7031cd147ed7 101
kevin1990 20:7031cd147ed7 102 /* Expand the full name of the reset value macro for the specified register */
kevin1990 20:7031cd147ed7 103 #define REG_RESET_VAL(_name) REG_ADISENSE_##_name##_RESET
kevin1990 20:7031cd147ed7 104
kevin1990 20:7031cd147ed7 105 /* Checks if a value is outside the bounds of the specified register field */
kevin1990 20:7031cd147ed7 106 #define CHECK_REG_FIELD_VAL(_field, _val) \
kevin1990 20:7031cd147ed7 107 do { \
kevin1990 20:7031cd147ed7 108 uint32_t _mask = BITM_ADISENSE_##_field; \
kevin1990 20:7031cd147ed7 109 uint32_t _shift = BITP_ADISENSE_##_field; \
kevin1990 20:7031cd147ed7 110 if ((((_val) << _shift) & ~(_mask)) != 0) { \
kevin1990 20:7031cd147ed7 111 ADI_SENSE_LOG_ERROR("Value 0x%08X invalid for register field %s", \
kevin1990 20:7031cd147ed7 112 (uint32_t)(_val), \
kevin1990 20:7031cd147ed7 113 STRINGIFY(ADISENSE_##_field)); \
kevin1990 20:7031cd147ed7 114 return ADI_SENSE_INVALID_PARAM; \
kevin1990 20:7031cd147ed7 115 } \
kevin1990 20:7031cd147ed7 116 } while(false)
kevin1990 20:7031cd147ed7 117
kevin1990 20:7031cd147ed7 118 /*
kevin1990 20:7031cd147ed7 119 * Encapsulates the write to a specified register
kevin1990 20:7031cd147ed7 120 * NOTE - this will cause the calling function to return on error
kevin1990 20:7031cd147ed7 121 */
kevin1990 20:7031cd147ed7 122 #define WRITE_REG(_hdev, _val, _name, _type) \
kevin1990 20:7031cd147ed7 123 do { \
kevin1990 20:7031cd147ed7 124 ADI_SENSE_RESULT _res; \
kevin1990 20:7031cd147ed7 125 _type _regval = _val; \
kevin1990 20:7031cd147ed7 126 _res = adi_sense_1000_WriteRegister((_hdev), \
kevin1990 20:7031cd147ed7 127 REG_ADISENSE_##_name, \
kevin1990 20:7031cd147ed7 128 &_regval, sizeof(_regval)); \
kevin1990 20:7031cd147ed7 129 if (_res != ADI_SENSE_SUCCESS) \
kevin1990 20:7031cd147ed7 130 return _res; \
kevin1990 20:7031cd147ed7 131 } while(false)
kevin1990 20:7031cd147ed7 132
kevin1990 20:7031cd147ed7 133 /* Wrapper macro to write a value to a uint32_t register */
kevin1990 20:7031cd147ed7 134 #define WRITE_REG_U32(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 135 WRITE_REG(_hdev, _val, _name, uint32_t)
kevin1990 20:7031cd147ed7 136 /* Wrapper macro to write a value to a uint16_t register */
kevin1990 20:7031cd147ed7 137 #define WRITE_REG_U16(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 138 WRITE_REG(_hdev, _val, _name, uint16_t)
kevin1990 20:7031cd147ed7 139 /* Wrapper macro to write a value to a uint8_t register */
kevin1990 20:7031cd147ed7 140 #define WRITE_REG_U8(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 141 WRITE_REG(_hdev, _val, _name, uint8_t)
kevin1990 20:7031cd147ed7 142 /* Wrapper macro to write a value to a float32_t register */
kevin1990 20:7031cd147ed7 143 #define WRITE_REG_FLOAT(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 144 WRITE_REG(_hdev, _val, _name, float32_t)
kevin1990 20:7031cd147ed7 145
kevin1990 20:7031cd147ed7 146 /*
kevin1990 20:7031cd147ed7 147 * Encapsulates the read from a specified register
kevin1990 20:7031cd147ed7 148 * NOTE - this will cause the calling function to return on error
kevin1990 20:7031cd147ed7 149 */
kevin1990 20:7031cd147ed7 150 #define READ_REG(_hdev, _val, _name, _type) \
kevin1990 20:7031cd147ed7 151 do { \
kevin1990 20:7031cd147ed7 152 ADI_SENSE_RESULT _res; \
kevin1990 20:7031cd147ed7 153 _type _regval; \
kevin1990 20:7031cd147ed7 154 _res = adi_sense_1000_ReadRegister((_hdev), \
kevin1990 20:7031cd147ed7 155 REG_ADISENSE_##_name, \
kevin1990 20:7031cd147ed7 156 &_regval, sizeof(_regval)); \
kevin1990 20:7031cd147ed7 157 if (_res != ADI_SENSE_SUCCESS) \
kevin1990 20:7031cd147ed7 158 return _res; \
kevin1990 20:7031cd147ed7 159 _val = _regval; \
kevin1990 20:7031cd147ed7 160 } while(false)
kevin1990 20:7031cd147ed7 161
kevin1990 20:7031cd147ed7 162 /* Wrapper macro to read a value from a uint32_t register */
kevin1990 20:7031cd147ed7 163 #define READ_REG_U32(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 164 READ_REG(_hdev, _val, _name, uint32_t)
kevin1990 20:7031cd147ed7 165 /* Wrapper macro to read a value from a uint16_t register */
kevin1990 20:7031cd147ed7 166 #define READ_REG_U16(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 167 READ_REG(_hdev, _val, _name, uint16_t)
kevin1990 20:7031cd147ed7 168 /* Wrapper macro to read a value from a uint8_t register */
kevin1990 20:7031cd147ed7 169 #define READ_REG_U8(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 170 READ_REG(_hdev, _val, _name, uint8_t)
kevin1990 20:7031cd147ed7 171 /* Wrapper macro to read a value from a float32_t register */
kevin1990 20:7031cd147ed7 172 #define READ_REG_FLOAT(_hdev, _val, _name) \
kevin1990 20:7031cd147ed7 173 READ_REG(_hdev, _val, _name, float32_t)
kevin1990 20:7031cd147ed7 174
kevin1990 20:7031cd147ed7 175 /*
kevin1990 20:7031cd147ed7 176 * Wrapper macro to write an array of values to a uint8_t register
kevin1990 20:7031cd147ed7 177 * NOTE - this is intended only for writing to a keyhole data register
kevin1990 20:7031cd147ed7 178 */
kevin1990 20:7031cd147ed7 179 #define WRITE_REG_U8_ARRAY(_hdev, _arr, _len, _name) \
kevin1990 20:7031cd147ed7 180 do { \
kevin1990 20:7031cd147ed7 181 ADI_SENSE_RESULT _res; \
kevin1990 20:7031cd147ed7 182 _res = adi_sense_1000_WriteRegister(_hdev, \
kevin1990 20:7031cd147ed7 183 REG_ADISENSE_##_name, \
kevin1990 20:7031cd147ed7 184 _arr, _len); \
kevin1990 20:7031cd147ed7 185 if (_res != ADI_SENSE_SUCCESS) \
kevin1990 20:7031cd147ed7 186 return _res; \
kevin1990 20:7031cd147ed7 187 } while(false)
kevin1990 20:7031cd147ed7 188
kevin1990 20:7031cd147ed7 189 /*
kevin1990 20:7031cd147ed7 190 * Wrapper macro to read an array of values from a uint8_t register
kevin1990 20:7031cd147ed7 191 * NOTE - this is intended only for reading from a keyhole data register
kevin1990 20:7031cd147ed7 192 */
kevin1990 20:7031cd147ed7 193 #define READ_REG_U8_ARRAY(_hdev, _arr, _len, _name) \
kevin1990 20:7031cd147ed7 194 do { \
kevin1990 20:7031cd147ed7 195 ADI_SENSE_RESULT _res; \
kevin1990 20:7031cd147ed7 196 _res = adi_sense_1000_ReadRegister((_hdev), \
kevin1990 20:7031cd147ed7 197 REG_ADISENSE_##_name, \
kevin1990 20:7031cd147ed7 198 _arr, _len); \
kevin1990 20:7031cd147ed7 199 if (_res != ADI_SENSE_SUCCESS) \
kevin1990 20:7031cd147ed7 200 return _res; \
kevin1990 20:7031cd147ed7 201 } while(false)
kevin1990 20:7031cd147ed7 202
kevin1990 20:7031cd147ed7 203 #define ADI_SENSE_1000_CHANNEL_IS_ADC(c) \
kevin1990 20:7031cd147ed7 204 ((c) >= ADI_SENSE_1000_CHANNEL_ID_CJC_0 && (c) <= ADI_SENSE_1000_CHANNEL_ID_CURRENT_0)
kevin1990 20:7031cd147ed7 205
kevin1990 20:7031cd147ed7 206 #define ADI_SENSE_1000_CHANNEL_IS_ADC_CJC(c) \
kevin1990 20:7031cd147ed7 207 ((c) >= ADI_SENSE_1000_CHANNEL_ID_CJC_0 && (c) <= ADI_SENSE_1000_CHANNEL_ID_CJC_1)
kevin1990 20:7031cd147ed7 208
kevin1990 20:7031cd147ed7 209 #define ADI_SENSE_1000_CHANNEL_IS_ADC_SENSOR(c) \
kevin1990 20:7031cd147ed7 210 ((c) >= ADI_SENSE_1000_CHANNEL_ID_SENSOR_0 && (c) <= ADI_SENSE_1000_CHANNEL_ID_SENSOR_3)
kevin1990 20:7031cd147ed7 211
kevin1990 20:7031cd147ed7 212 #define ADI_SENSE_1000_CHANNEL_IS_ADC_VOLTAGE(c) \
kevin1990 20:7031cd147ed7 213 ((c) == ADI_SENSE_1000_CHANNEL_ID_VOLTAGE_0)
kevin1990 20:7031cd147ed7 214
kevin1990 20:7031cd147ed7 215 #define ADI_SENSE_1000_CHANNEL_IS_ADC_CURRENT(c) \
kevin1990 20:7031cd147ed7 216 ((c) == ADI_SENSE_1000_CHANNEL_ID_CURRENT_0)
kevin1990 20:7031cd147ed7 217
kevin1990 20:7031cd147ed7 218 #define ADI_SENSE_1000_CHANNEL_IS_VIRTUAL(c) \
kevin1990 20:7031cd147ed7 219 ((c) == ADI_SENSE_1000_CHANNEL_ID_SPI_1 || (c) == ADI_SENSE_1000_CHANNEL_ID_SPI_2)
kevin1990 20:7031cd147ed7 220
kevin1990 20:7031cd147ed7 221 typedef struct
kevin1990 20:7031cd147ed7 222 {
kevin1990 20:7031cd147ed7 223 unsigned nDeviceIndex;
kevin1990 20:7031cd147ed7 224 ADI_SENSE_SPI_HANDLE hSpi;
kevin1990 20:7031cd147ed7 225 ADI_SENSE_GPIO_HANDLE hGpio;
kevin1990 20:7031cd147ed7 226 } ADI_SENSE_DEVICE_CONTEXT;
kevin1990 20:7031cd147ed7 227
kevin1990 20:7031cd147ed7 228 static ADI_SENSE_DEVICE_CONTEXT gDeviceCtx[ADI_SENSE_PLATFORM_MAX_DEVICES];
kevin1990 20:7031cd147ed7 229
kevin1990 20:7031cd147ed7 230 /*
kevin1990 20:7031cd147ed7 231 * Open an ADI Sense device instance.
kevin1990 20:7031cd147ed7 232 */
kevin1990 20:7031cd147ed7 233 ADI_SENSE_RESULT adi_sense_Open(
kevin1990 20:7031cd147ed7 234 unsigned const nDeviceIndex,
kevin1990 20:7031cd147ed7 235 ADI_SENSE_CONNECTION * const pConnectionInfo,
kevin1990 20:7031cd147ed7 236 ADI_SENSE_DEVICE_HANDLE * const phDevice)
kevin1990 20:7031cd147ed7 237 {
kevin1990 20:7031cd147ed7 238 ADI_SENSE_DEVICE_CONTEXT *pCtx;
kevin1990 20:7031cd147ed7 239 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 240
kevin1990 20:7031cd147ed7 241 if (nDeviceIndex >= ADI_SENSE_PLATFORM_MAX_DEVICES)
kevin1990 20:7031cd147ed7 242 return ADI_SENSE_INVALID_DEVICE_NUM;
kevin1990 20:7031cd147ed7 243
kevin1990 20:7031cd147ed7 244 pCtx = &gDeviceCtx[nDeviceIndex];
kevin1990 20:7031cd147ed7 245 pCtx->nDeviceIndex = nDeviceIndex;
kevin1990 20:7031cd147ed7 246
kevin1990 20:7031cd147ed7 247 eRet = adi_sense_LogOpen();
kevin1990 20:7031cd147ed7 248 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 249 return eRet;
kevin1990 20:7031cd147ed7 250
kevin1990 20:7031cd147ed7 251 eRet = adi_sense_GpioOpen(&pConnectionInfo->gpio, &pCtx->hGpio);
kevin1990 20:7031cd147ed7 252 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 253 return eRet;
kevin1990 20:7031cd147ed7 254
kevin1990 20:7031cd147ed7 255 eRet = adi_sense_SpiOpen(&pConnectionInfo->spi, &pCtx->hSpi);
kevin1990 20:7031cd147ed7 256 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 257 return eRet;
kevin1990 20:7031cd147ed7 258
kevin1990 20:7031cd147ed7 259 *phDevice = pCtx;
kevin1990 20:7031cd147ed7 260 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 261 }
kevin1990 20:7031cd147ed7 262
kevin1990 20:7031cd147ed7 263 /*
kevin1990 20:7031cd147ed7 264 * Get the current state of the specified GPIO input signal.
kevin1990 20:7031cd147ed7 265 */
kevin1990 20:7031cd147ed7 266 ADI_SENSE_RESULT adi_sense_GetGpioState(
kevin1990 20:7031cd147ed7 267 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 268 ADI_SENSE_GPIO_PIN const ePinId,
kevin1990 20:7031cd147ed7 269 bool_t * const pbAsserted)
kevin1990 20:7031cd147ed7 270 {
kevin1990 20:7031cd147ed7 271 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 272
kevin1990 20:7031cd147ed7 273 return adi_sense_GpioGet(pCtx->hGpio, ePinId, pbAsserted);
kevin1990 20:7031cd147ed7 274 }
kevin1990 20:7031cd147ed7 275
kevin1990 20:7031cd147ed7 276 /*
kevin1990 20:7031cd147ed7 277 * Register an application-defined callback function for GPIO interrupts.
kevin1990 20:7031cd147ed7 278 */
kevin1990 20:7031cd147ed7 279 ADI_SENSE_RESULT adi_sense_RegisterGpioCallback(
kevin1990 20:7031cd147ed7 280 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 281 ADI_SENSE_GPIO_PIN const ePinId,
kevin1990 20:7031cd147ed7 282 ADI_SENSE_GPIO_CALLBACK const callbackFunction,
kevin1990 20:7031cd147ed7 283 void * const pCallbackParam)
kevin1990 20:7031cd147ed7 284 {
kevin1990 20:7031cd147ed7 285 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 286
kevin1990 20:7031cd147ed7 287 if (callbackFunction)
kevin1990 20:7031cd147ed7 288 {
kevin1990 20:7031cd147ed7 289 return adi_sense_GpioIrqEnable(pCtx->hGpio, ePinId, callbackFunction,
kevin1990 20:7031cd147ed7 290 pCallbackParam);
kevin1990 20:7031cd147ed7 291 }
kevin1990 20:7031cd147ed7 292 else
kevin1990 20:7031cd147ed7 293 {
kevin1990 20:7031cd147ed7 294 return adi_sense_GpioIrqDisable(pCtx->hGpio, ePinId);
kevin1990 20:7031cd147ed7 295 }
kevin1990 20:7031cd147ed7 296 }
kevin1990 20:7031cd147ed7 297
kevin1990 20:7031cd147ed7 298 /*
kevin1990 20:7031cd147ed7 299 * Reset the specified ADI Sense device.
kevin1990 20:7031cd147ed7 300 */
kevin1990 20:7031cd147ed7 301 ADI_SENSE_RESULT adi_sense_Reset(
kevin1990 20:7031cd147ed7 302 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 303 {
kevin1990 20:7031cd147ed7 304 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 305 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 306
kevin1990 20:7031cd147ed7 307 /* Pulse the Reset GPIO pin low for a minimum of 4 microseconds */
kevin1990 20:7031cd147ed7 308 eRet = adi_sense_GpioSet(pCtx->hGpio, ADI_SENSE_GPIO_PIN_RESET, false);
kevin1990 20:7031cd147ed7 309 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 310 return eRet;
kevin1990 20:7031cd147ed7 311
kevin1990 20:7031cd147ed7 312 adi_sense_TimeDelayUsec(4);
kevin1990 20:7031cd147ed7 313
kevin1990 20:7031cd147ed7 314 eRet = adi_sense_GpioSet(pCtx->hGpio, ADI_SENSE_GPIO_PIN_RESET, true);
kevin1990 20:7031cd147ed7 315 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 316 return eRet;
kevin1990 20:7031cd147ed7 317
kevin1990 20:7031cd147ed7 318 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 319 }
kevin1990 20:7031cd147ed7 320
kevin1990 20:7031cd147ed7 321
kevin1990 20:7031cd147ed7 322 /*!
kevin1990 20:7031cd147ed7 323 * @brief Get general status of ADISense module.
kevin1990 20:7031cd147ed7 324 *
kevin1990 20:7031cd147ed7 325 * @param[in]
kevin1990 20:7031cd147ed7 326 * @param[out] pStatus : Pointer to CORE Status struct.
kevin1990 20:7031cd147ed7 327 *
kevin1990 20:7031cd147ed7 328 * @return Status
kevin1990 20:7031cd147ed7 329 * - #ADI_SENSE_SUCCESS Call completed successfully.
kevin1990 20:7031cd147ed7 330 * - #ADI_SENSE_FAILURE If status register read fails.
kevin1990 20:7031cd147ed7 331 *
kevin1990 20:7031cd147ed7 332 * @details Read the general status register for the ADISense
kevin1990 20:7031cd147ed7 333 * module. Indicates Error, Alert conditions, data ready
kevin1990 20:7031cd147ed7 334 * and command running.
kevin1990 20:7031cd147ed7 335 *
kevin1990 20:7031cd147ed7 336 */
kevin1990 20:7031cd147ed7 337 ADI_SENSE_RESULT adi_sense_GetStatus(
kevin1990 20:7031cd147ed7 338 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 339 ADI_SENSE_STATUS * const pStatus)
kevin1990 20:7031cd147ed7 340 {
kevin1990 20:7031cd147ed7 341 ADI_ADISENSE_CORE_Status_t statusReg;
kevin1990 20:7031cd147ed7 342 READ_REG_U8(hDevice, statusReg.VALUE8, CORE_STATUS);
kevin1990 20:7031cd147ed7 343
kevin1990 20:7031cd147ed7 344 memset(pStatus, 0, sizeof(*pStatus));
kevin1990 20:7031cd147ed7 345
kevin1990 20:7031cd147ed7 346 if (!statusReg.Cmd_Running) /* Active-low, so invert it */
kevin1990 20:7031cd147ed7 347 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_BUSY;
kevin1990 20:7031cd147ed7 348 if (statusReg.Drdy)
kevin1990 20:7031cd147ed7 349 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_DATAREADY;
kevin1990 20:7031cd147ed7 350 if (statusReg.FIFO_Error)
kevin1990 20:7031cd147ed7 351 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_FIFO_ERROR;
kevin1990 20:7031cd147ed7 352 if (statusReg.Alert_Active)
kevin1990 20:7031cd147ed7 353 {
kevin1990 20:7031cd147ed7 354 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_ALERT;
kevin1990 20:7031cd147ed7 355
kevin1990 20:7031cd147ed7 356 ADI_ADISENSE_CORE_Alert_Code_t alertCodeReg;
kevin1990 20:7031cd147ed7 357 READ_REG_U16(hDevice, alertCodeReg.VALUE16, CORE_ALERT_CODE);
kevin1990 20:7031cd147ed7 358 pStatus->alertCode = alertCodeReg.Alert_Code;
kevin1990 20:7031cd147ed7 359
kevin1990 20:7031cd147ed7 360 ADI_ADISENSE_CORE_Channel_Alert_Status_t channelAlertStatusReg;
kevin1990 20:7031cd147ed7 361 READ_REG_U16(hDevice, channelAlertStatusReg.VALUE16,
kevin1990 20:7031cd147ed7 362 CORE_CHANNEL_ALERT_STATUS);
kevin1990 20:7031cd147ed7 363
kevin1990 20:7031cd147ed7 364 for (unsigned i = 0; i < ADI_SENSE_1000_MAX_CHANNELS; i++)
kevin1990 20:7031cd147ed7 365 {
kevin1990 20:7031cd147ed7 366 if (channelAlertStatusReg.VALUE16 & (1 << i))
kevin1990 20:7031cd147ed7 367 {
kevin1990 20:7031cd147ed7 368 ADI_ADISENSE_CORE_Alert_Code_Ch_t channelAlertCodeReg;
kevin1990 20:7031cd147ed7 369 READ_REG_U16(hDevice, channelAlertCodeReg.VALUE16, CORE_ALERT_CODE_CHn(i));
kevin1990 20:7031cd147ed7 370 pStatus->channelAlertCodes[i] = channelAlertCodeReg.Alert_Code_Ch;
kevin1990 20:7031cd147ed7 371
kevin1990 20:7031cd147ed7 372 ADI_ADISENSE_CORE_Alert_Detail_Ch_t alertDetailReg;
kevin1990 20:7031cd147ed7 373 READ_REG_U16(hDevice, alertDetailReg.VALUE16,
kevin1990 20:7031cd147ed7 374 CORE_ALERT_DETAIL_CHn(i));
kevin1990 20:7031cd147ed7 375
kevin1990 20:7031cd147ed7 376 if (alertDetailReg.Time_Out)
kevin1990 20:7031cd147ed7 377 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_TIMEOUT;
kevin1990 20:7031cd147ed7 378 if (alertDetailReg.Under_Range)
kevin1990 20:7031cd147ed7 379 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_UNDER_RANGE;
kevin1990 20:7031cd147ed7 380 if (alertDetailReg.Over_Range)
kevin1990 20:7031cd147ed7 381 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_OVER_RANGE;
kevin1990 20:7031cd147ed7 382 if (alertDetailReg.Low_Limit)
kevin1990 20:7031cd147ed7 383 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LOW_LIMIT;
kevin1990 20:7031cd147ed7 384 if (alertDetailReg.High_Limit)
kevin1990 20:7031cd147ed7 385 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_HIGH_LIMIT;
kevin1990 20:7031cd147ed7 386 if (alertDetailReg.Sensor_Open)
kevin1990 20:7031cd147ed7 387 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_SENSOR_OPEN;
kevin1990 20:7031cd147ed7 388 if (alertDetailReg.Ref_Detect)
kevin1990 20:7031cd147ed7 389 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_REF_DETECT;
kevin1990 20:7031cd147ed7 390 if (alertDetailReg.Config_Err)
kevin1990 20:7031cd147ed7 391 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_CONFIG_ERR;
kevin1990 20:7031cd147ed7 392 if (alertDetailReg.LUT_Error_Ch)
kevin1990 20:7031cd147ed7 393 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LUT_ERR;
kevin1990 20:7031cd147ed7 394 if (alertDetailReg.Sensor_Not_Ready)
kevin1990 20:7031cd147ed7 395 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_SENSOR_NOT_READY;
kevin1990 20:7031cd147ed7 396 if (alertDetailReg.Comp_Not_Ready)
kevin1990 20:7031cd147ed7 397 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_COMP_NOT_READY;
kevin1990 20:7031cd147ed7 398 if (alertDetailReg.Under_Voltage)
kevin1990 20:7031cd147ed7 399 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_UNDER_VOLTAGE;
kevin1990 20:7031cd147ed7 400 if (alertDetailReg.Over_Voltage)
kevin1990 20:7031cd147ed7 401 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_OVER_VOLTAGE;
kevin1990 20:7031cd147ed7 402 if (alertDetailReg.Correction_UnderRange)
kevin1990 20:7031cd147ed7 403 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LUT_UNDER_RANGE;
kevin1990 20:7031cd147ed7 404 if (alertDetailReg.Correction_OverRange)
kevin1990 20:7031cd147ed7 405 pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LUT_OVER_RANGE;
kevin1990 20:7031cd147ed7 406 }
kevin1990 20:7031cd147ed7 407 }
kevin1990 20:7031cd147ed7 408
kevin1990 20:7031cd147ed7 409 ADI_ADISENSE_CORE_Alert_Status_2_t alert2Reg;
kevin1990 20:7031cd147ed7 410 READ_REG_U16(hDevice, alert2Reg.VALUE16, CORE_ALERT_STATUS_2);
kevin1990 20:7031cd147ed7 411 if (alert2Reg.Configuration_Error)
kevin1990 20:7031cd147ed7 412 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_CONFIG_ERROR;
kevin1990 20:7031cd147ed7 413 if (alert2Reg.LUT_Error)
kevin1990 20:7031cd147ed7 414 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_LUT_ERROR;
kevin1990 20:7031cd147ed7 415 }
kevin1990 20:7031cd147ed7 416
kevin1990 20:7031cd147ed7 417 if (statusReg.Error)
kevin1990 20:7031cd147ed7 418 {
kevin1990 20:7031cd147ed7 419 pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_ERROR;
kevin1990 20:7031cd147ed7 420
kevin1990 20:7031cd147ed7 421 ADI_ADISENSE_CORE_Error_Code_t errorCodeReg;
kevin1990 20:7031cd147ed7 422 READ_REG_U16(hDevice, errorCodeReg.VALUE16, CORE_ERROR_CODE);
kevin1990 20:7031cd147ed7 423 pStatus->errorCode = errorCodeReg.Error_Code;
kevin1990 20:7031cd147ed7 424
kevin1990 20:7031cd147ed7 425 ADI_ADISENSE_CORE_Diagnostics_Status_t diagStatusReg;
kevin1990 20:7031cd147ed7 426 READ_REG_U16(hDevice, diagStatusReg.VALUE16, CORE_DIAGNOSTICS_STATUS);
kevin1990 20:7031cd147ed7 427
kevin1990 20:7031cd147ed7 428 if (diagStatusReg.Diag_Checksum_Error)
kevin1990 20:7031cd147ed7 429 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_CHECKSUM_ERROR;
kevin1990 20:7031cd147ed7 430 if (diagStatusReg.Diag_Comms_Error)
kevin1990 20:7031cd147ed7 431 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_COMMS_ERROR;
kevin1990 20:7031cd147ed7 432 if (diagStatusReg.Diag_Supply_Monitor_Error)
kevin1990 20:7031cd147ed7 433 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_SUPPLY_MONITOR_ERROR;
kevin1990 20:7031cd147ed7 434 if (diagStatusReg.Diag_Supply_Cap_Error)
kevin1990 20:7031cd147ed7 435 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_SUPPLY_CAP_ERROR;
kevin1990 20:7031cd147ed7 436 if (diagStatusReg.Diag_Ainm_UV_Error)
kevin1990 20:7031cd147ed7 437 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_AINM_UV_ERROR;
kevin1990 20:7031cd147ed7 438 if (diagStatusReg.Diag_Ainm_OV_Error)
kevin1990 20:7031cd147ed7 439 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_AINM_OV_ERROR;
kevin1990 20:7031cd147ed7 440 if (diagStatusReg.Diag_Ainp_UV_Error)
kevin1990 20:7031cd147ed7 441 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_AINP_UV_ERROR;
kevin1990 20:7031cd147ed7 442 if (diagStatusReg.Diag_Ainp_OV_Error)
kevin1990 20:7031cd147ed7 443 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_AINP_OV_ERROR;
kevin1990 20:7031cd147ed7 444 if (diagStatusReg.Diag_Conversion_Error)
kevin1990 20:7031cd147ed7 445 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_CONVERSION_ERROR;
kevin1990 20:7031cd147ed7 446 if (diagStatusReg.Diag_Calibration_Error)
kevin1990 20:7031cd147ed7 447 pStatus->diagnosticsStatus |= ADI_SENSE_DIAGNOSTICS_STATUS_CALIBRATION_ERROR;
kevin1990 20:7031cd147ed7 448 }
kevin1990 20:7031cd147ed7 449
kevin1990 20:7031cd147ed7 450 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 451 }
kevin1990 20:7031cd147ed7 452
kevin1990 20:7031cd147ed7 453 ADI_SENSE_RESULT adi_sense_GetCommandRunningState(
kevin1990 20:7031cd147ed7 454 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 455 bool_t *pbCommandRunning)
kevin1990 20:7031cd147ed7 456 {
kevin1990 20:7031cd147ed7 457 ADI_ADISENSE_CORE_Status_t statusReg;
kevin1990 20:7031cd147ed7 458
kevin1990 20:7031cd147ed7 459 READ_REG_U8(hDevice, statusReg.VALUE8, CORE_STATUS);
kevin1990 20:7031cd147ed7 460
kevin1990 20:7031cd147ed7 461 /* We should never normally see 0xFF here if the module is operational */
kevin1990 20:7031cd147ed7 462 if (statusReg.VALUE8 == 0xFF)
kevin1990 20:7031cd147ed7 463 return ADI_SENSE_ERR_NOT_INITIALIZED;
kevin1990 20:7031cd147ed7 464
kevin1990 20:7031cd147ed7 465 *pbCommandRunning = !statusReg.Cmd_Running; /* Active-low, so invert it */
kevin1990 20:7031cd147ed7 466
kevin1990 20:7031cd147ed7 467 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 468 }
kevin1990 20:7031cd147ed7 469
kevin1990 20:7031cd147ed7 470 static ADI_SENSE_RESULT executeCommand(
kevin1990 20:7031cd147ed7 471 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 472 ADI_ADISENSE_CORE_Command_Special_Command const command,
kevin1990 20:7031cd147ed7 473 bool_t const bWaitForCompletion)
kevin1990 20:7031cd147ed7 474 {
kevin1990 20:7031cd147ed7 475 ADI_ADISENSE_CORE_Command_t commandReg;
kevin1990 20:7031cd147ed7 476 bool_t bCommandRunning;
kevin1990 20:7031cd147ed7 477 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 478
kevin1990 20:7031cd147ed7 479 /*
kevin1990 20:7031cd147ed7 480 * Don't allow another command to be issued if one is already running, but
kevin1990 20:7031cd147ed7 481 * make an exception for ADISENSE_CORE_COMMAND_NOP which can be used to
kevin1990 20:7031cd147ed7 482 * request a running command to be stopped (e.g. continuous measurement)
kevin1990 20:7031cd147ed7 483 */
kevin1990 20:7031cd147ed7 484 if (command != ADISENSE_CORE_COMMAND_NOP)
kevin1990 20:7031cd147ed7 485 {
kevin1990 20:7031cd147ed7 486 eRet = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning);
kevin1990 20:7031cd147ed7 487 if (eRet)
kevin1990 20:7031cd147ed7 488 return eRet;
kevin1990 20:7031cd147ed7 489
kevin1990 20:7031cd147ed7 490 if (bCommandRunning)
kevin1990 20:7031cd147ed7 491 return ADI_SENSE_IN_USE;
kevin1990 20:7031cd147ed7 492 }
kevin1990 20:7031cd147ed7 493
kevin1990 20:7031cd147ed7 494 commandReg.Special_Command = command;
kevin1990 20:7031cd147ed7 495 WRITE_REG_U8(hDevice, commandReg.VALUE8, CORE_COMMAND);
kevin1990 20:7031cd147ed7 496
kevin1990 20:7031cd147ed7 497 if (bWaitForCompletion)
kevin1990 20:7031cd147ed7 498 {
kevin1990 20:7031cd147ed7 499 do {
kevin1990 20:7031cd147ed7 500 /* Allow a minimum 50usec delay for status update before checking */
kevin1990 20:7031cd147ed7 501 adi_sense_TimeDelayUsec(50);
kevin1990 20:7031cd147ed7 502
kevin1990 20:7031cd147ed7 503 eRet = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning);
kevin1990 20:7031cd147ed7 504 if (eRet)
kevin1990 20:7031cd147ed7 505 return eRet;
kevin1990 20:7031cd147ed7 506 } while (bCommandRunning);
kevin1990 20:7031cd147ed7 507 }
kevin1990 20:7031cd147ed7 508
kevin1990 20:7031cd147ed7 509 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 510 }
kevin1990 20:7031cd147ed7 511
kevin1990 20:7031cd147ed7 512 ADI_SENSE_RESULT adi_sense_ApplyConfigUpdates(
kevin1990 20:7031cd147ed7 513 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 514 {
kevin1990 20:7031cd147ed7 515 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LATCH_CONFIG, true);
kevin1990 20:7031cd147ed7 516 }
kevin1990 20:7031cd147ed7 517
kevin1990 20:7031cd147ed7 518 /*!
kevin1990 20:7031cd147ed7 519 * @brief Start a measurement cycle.
kevin1990 20:7031cd147ed7 520 *
kevin1990 20:7031cd147ed7 521 * @param[out]
kevin1990 20:7031cd147ed7 522 *
kevin1990 20:7031cd147ed7 523 * @return Status
kevin1990 20:7031cd147ed7 524 * - #ADI_SENSE_SUCCESS Call completed successfully.
kevin1990 20:7031cd147ed7 525 * - #ADI_SENSE_FAILURE
kevin1990 20:7031cd147ed7 526 *
kevin1990 20:7031cd147ed7 527 * @details Sends the latch config command. Configuration for channels in
kevin1990 20:7031cd147ed7 528 * conversion cycle should be completed before this function.
kevin1990 20:7031cd147ed7 529 * Channel enabled bit should be set before this function.
kevin1990 20:7031cd147ed7 530 * Starts a conversion and configures the format of the sample.
kevin1990 20:7031cd147ed7 531 *
kevin1990 20:7031cd147ed7 532 */
kevin1990 20:7031cd147ed7 533 ADI_SENSE_RESULT adi_sense_StartMeasurement(
kevin1990 20:7031cd147ed7 534 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 535 ADI_SENSE_MEASUREMENT_MODE const eMeasurementMode)
kevin1990 20:7031cd147ed7 536 {
kevin1990 20:7031cd147ed7 537 switch (eMeasurementMode)
kevin1990 20:7031cd147ed7 538 {
kevin1990 20:7031cd147ed7 539 case ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK:
kevin1990 20:7031cd147ed7 540 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SYSTEM_CHECK, false);
kevin1990 20:7031cd147ed7 541 case ADI_SENSE_MEASUREMENT_MODE_NORMAL:
kevin1990 20:7031cd147ed7 542 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CONVERT_WITH_RAW, false);
kevin1990 20:7031cd147ed7 543 case ADI_SENSE_MEASUREMENT_MODE_OMIT_RAW:
kevin1990 20:7031cd147ed7 544 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CONVERT, false);
kevin1990 20:7031cd147ed7 545 default:
kevin1990 20:7031cd147ed7 546 ADI_SENSE_LOG_ERROR("Invalid measurement mode %d specified",
kevin1990 20:7031cd147ed7 547 eMeasurementMode);
kevin1990 20:7031cd147ed7 548 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 549 }
kevin1990 20:7031cd147ed7 550 }
kevin1990 20:7031cd147ed7 551
kevin1990 20:7031cd147ed7 552 /*
kevin1990 20:7031cd147ed7 553 * Store the configuration settings to persistent memory on the device.
kevin1990 20:7031cd147ed7 554 * No other command must be running when this is called.
kevin1990 20:7031cd147ed7 555 * Do not power down the device while this command is running.
kevin1990 20:7031cd147ed7 556 */
kevin1990 20:7031cd147ed7 557 ADI_SENSE_RESULT adi_sense_SaveConfig(
kevin1990 20:7031cd147ed7 558 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 559 {
kevin1990 20:7031cd147ed7 560 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_CONFIG, true);
kevin1990 20:7031cd147ed7 561 }
kevin1990 20:7031cd147ed7 562
kevin1990 20:7031cd147ed7 563 /*
kevin1990 20:7031cd147ed7 564 * Restore the configuration settings from persistent memory on the device.
kevin1990 20:7031cd147ed7 565 * No other command must be running when this is called.
kevin1990 20:7031cd147ed7 566 */
kevin1990 20:7031cd147ed7 567 ADI_SENSE_RESULT adi_sense_RestoreConfig(
kevin1990 20:7031cd147ed7 568 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 569 {
kevin1990 20:7031cd147ed7 570 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_CONFIG, true);
kevin1990 20:7031cd147ed7 571 }
kevin1990 20:7031cd147ed7 572
kevin1990 20:7031cd147ed7 573 /*
kevin1990 20:7031cd147ed7 574 * Store the LUT data to persistent memory on the device.
kevin1990 20:7031cd147ed7 575 * No other command must be running when this is called.
kevin1990 20:7031cd147ed7 576 * Do not power down the device while this command is running.
kevin1990 20:7031cd147ed7 577 */
kevin1990 20:7031cd147ed7 578 ADI_SENSE_RESULT adi_sense_SaveLutData(
kevin1990 20:7031cd147ed7 579 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 580 {
kevin1990 20:7031cd147ed7 581 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_LUT2, true);
kevin1990 20:7031cd147ed7 582 }
kevin1990 20:7031cd147ed7 583
kevin1990 20:7031cd147ed7 584 /*
kevin1990 20:7031cd147ed7 585 * Restore the LUT data from persistent memory on the device.
kevin1990 20:7031cd147ed7 586 * No other command must be running when this is called.
kevin1990 20:7031cd147ed7 587 */
kevin1990 20:7031cd147ed7 588 ADI_SENSE_RESULT adi_sense_RestoreLutData(
kevin1990 20:7031cd147ed7 589 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 590 {
kevin1990 20:7031cd147ed7 591 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_LUT, true);
kevin1990 20:7031cd147ed7 592 }
kevin1990 20:7031cd147ed7 593
kevin1990 20:7031cd147ed7 594 /*
kevin1990 20:7031cd147ed7 595 * Stop the measurement cycles on the device.
kevin1990 20:7031cd147ed7 596 * To be used only if a measurement command is currently running.
kevin1990 20:7031cd147ed7 597 */
kevin1990 20:7031cd147ed7 598 ADI_SENSE_RESULT adi_sense_StopMeasurement(
kevin1990 20:7031cd147ed7 599 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 600 {
kevin1990 20:7031cd147ed7 601 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_NOP, true);
kevin1990 20:7031cd147ed7 602 }
kevin1990 20:7031cd147ed7 603
kevin1990 20:7031cd147ed7 604 /*
kevin1990 20:7031cd147ed7 605 * Run built-in diagnostic checks on the device.
kevin1990 20:7031cd147ed7 606 * Diagnostics are executed according to the current applied settings.
kevin1990 20:7031cd147ed7 607 * No other command must be running when this is called.
kevin1990 20:7031cd147ed7 608 */
kevin1990 20:7031cd147ed7 609 ADI_SENSE_RESULT adi_sense_RunDiagnostics(
kevin1990 20:7031cd147ed7 610 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 611 {
kevin1990 20:7031cd147ed7 612 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_RUN_DIAGNOSTICS, true);
kevin1990 20:7031cd147ed7 613 }
kevin1990 20:7031cd147ed7 614
kevin1990 20:7031cd147ed7 615 /*
kevin1990 20:7031cd147ed7 616 * Run self-calibration routines on the device.
kevin1990 20:7031cd147ed7 617 * Calibration is executed according to the current applied settings.
kevin1990 20:7031cd147ed7 618 * No other command must be running when this is called.
kevin1990 20:7031cd147ed7 619 */
kevin1990 20:7031cd147ed7 620 ADI_SENSE_RESULT adi_sense_RunCalibration(
kevin1990 20:7031cd147ed7 621 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 622 {
kevin1990 20:7031cd147ed7 623 return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SELF_CALIBRATION, true);
kevin1990 20:7031cd147ed7 624 }
kevin1990 20:7031cd147ed7 625
kevin1990 20:7031cd147ed7 626 /*
kevin1990 20:7031cd147ed7 627 * Read a set of data samples from the device.
kevin1990 20:7031cd147ed7 628 * This may be called at any time.
kevin1990 20:7031cd147ed7 629 */
kevin1990 20:7031cd147ed7 630 ADI_SENSE_RESULT adi_sense_GetData(
kevin1990 20:7031cd147ed7 631 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 632 ADI_SENSE_MEASUREMENT_MODE const eMeasurementMode,
kevin1990 20:7031cd147ed7 633 ADI_SENSE_DATA_SAMPLE * const pSamples,
kevin1990 20:7031cd147ed7 634 uint32_t const nRequested,
kevin1990 20:7031cd147ed7 635 uint32_t * const pnReturned)
kevin1990 20:7031cd147ed7 636 {
kevin1990 20:7031cd147ed7 637 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 638 uint16_t command = REG_READ_COMMAND |
kevin1990 20:7031cd147ed7 639 (REG_ADISENSE_CORE_DATA_FIFO & REG_ADDRESS_MASK);
kevin1990 20:7031cd147ed7 640 uint8_t commandData[2] = {
kevin1990 20:7031cd147ed7 641 command >> 8,
kevin1990 20:7031cd147ed7 642 command & 0xFF
kevin1990 20:7031cd147ed7 643 };
kevin1990 20:7031cd147ed7 644 uint8_t commandResponse[2];
kevin1990 20:7031cd147ed7 645 unsigned nValidSamples = 0;
kevin1990 20:7031cd147ed7 646 ADI_SENSE_RESULT eRet = ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 647
kevin1990 20:7031cd147ed7 648 do {
kevin1990 20:7031cd147ed7 649 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
kevin1990 20:7031cd147ed7 650 sizeof(command), false);
kevin1990 20:7031cd147ed7 651 if (eRet)
kevin1990 20:7031cd147ed7 652 {
kevin1990 20:7031cd147ed7 653 ADI_SENSE_LOG_ERROR("Failed to send read command for FIFO register");
kevin1990 20:7031cd147ed7 654 return eRet;
kevin1990 20:7031cd147ed7 655 }
kevin1990 20:7031cd147ed7 656
kevin1990 20:7031cd147ed7 657 adi_sense_TimeDelayUsec(POST_SPI_TRANSFER_DELAY_USEC);
kevin1990 20:7031cd147ed7 658 } while ((commandResponse[0] != REG_COMMAND_RESP_0) ||
kevin1990 20:7031cd147ed7 659 (commandResponse[1] != REG_COMMAND_RESP_1));
kevin1990 20:7031cd147ed7 660
kevin1990 20:7031cd147ed7 661 for (unsigned i = 0; i < nRequested; i++)
kevin1990 20:7031cd147ed7 662 {
kevin1990 20:7031cd147ed7 663 ADI_ADISENSE_CORE_Data_FIFO_t dataFifoReg;
kevin1990 20:7031cd147ed7 664 bool_t bHoldCs = true;
kevin1990 20:7031cd147ed7 665 unsigned readSampleSize = sizeof(dataFifoReg);
kevin1990 20:7031cd147ed7 666
kevin1990 20:7031cd147ed7 667 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_OMIT_RAW)
kevin1990 20:7031cd147ed7 668 readSampleSize -= 3; /* 3B raw value omitted in this case */
kevin1990 20:7031cd147ed7 669
kevin1990 20:7031cd147ed7 670 /* Keep the CS signal asserted for all but the last sample */
kevin1990 20:7031cd147ed7 671 if ((i + 1) == nRequested)
kevin1990 20:7031cd147ed7 672 bHoldCs = false;
kevin1990 20:7031cd147ed7 673
kevin1990 20:7031cd147ed7 674 eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, &dataFifoReg,
kevin1990 20:7031cd147ed7 675 readSampleSize, bHoldCs);
kevin1990 20:7031cd147ed7 676 if (eRet)
kevin1990 20:7031cd147ed7 677 {
kevin1990 20:7031cd147ed7 678 ADI_SENSE_LOG_ERROR("Failed to read data from FIFO register");
kevin1990 20:7031cd147ed7 679 return eRet;
kevin1990 20:7031cd147ed7 680 }
kevin1990 20:7031cd147ed7 681
kevin1990 20:7031cd147ed7 682 if (! dataFifoReg.Ch_Valid)
kevin1990 20:7031cd147ed7 683 {
kevin1990 20:7031cd147ed7 684 /*
kevin1990 20:7031cd147ed7 685 * Reading an invalid sample indicates that there are no
kevin1990 20:7031cd147ed7 686 * more samples available or we've lost sync with the device.
kevin1990 20:7031cd147ed7 687 * In the latter case, it might be recoverable, but return here
kevin1990 20:7031cd147ed7 688 * to let the application check the device status and decide itself.
kevin1990 20:7031cd147ed7 689 */
kevin1990 20:7031cd147ed7 690 eRet = ADI_SENSE_INCOMPLETE;
kevin1990 20:7031cd147ed7 691 break;
kevin1990 20:7031cd147ed7 692 }
kevin1990 20:7031cd147ed7 693
kevin1990 20:7031cd147ed7 694 ADI_SENSE_DATA_SAMPLE *pSample = &pSamples[nValidSamples];
kevin1990 20:7031cd147ed7 695
kevin1990 20:7031cd147ed7 696 pSample->status = 0;
kevin1990 20:7031cd147ed7 697 if (dataFifoReg.Ch_Error)
kevin1990 20:7031cd147ed7 698 pSample->status |= ADI_SENSE_DEVICE_STATUS_ERROR;
kevin1990 20:7031cd147ed7 699 if (dataFifoReg.Ch_Alert)
kevin1990 20:7031cd147ed7 700 pSample->status |= ADI_SENSE_DEVICE_STATUS_ALERT;
kevin1990 20:7031cd147ed7 701
kevin1990 20:7031cd147ed7 702 if (dataFifoReg.Ch_Raw)
kevin1990 20:7031cd147ed7 703 pSample->rawValue = dataFifoReg.Raw_Sample;
kevin1990 20:7031cd147ed7 704 else
kevin1990 20:7031cd147ed7 705 pSample->rawValue = 0;
kevin1990 20:7031cd147ed7 706
kevin1990 20:7031cd147ed7 707 pSample->channelId = dataFifoReg.Channel_ID;
kevin1990 20:7031cd147ed7 708 pSample->processedValue = dataFifoReg.Sensor_Result;
kevin1990 20:7031cd147ed7 709
kevin1990 20:7031cd147ed7 710 nValidSamples++;
kevin1990 20:7031cd147ed7 711 }
kevin1990 20:7031cd147ed7 712 *pnReturned = nValidSamples;
kevin1990 20:7031cd147ed7 713
kevin1990 20:7031cd147ed7 714 adi_sense_TimeDelayUsec(POST_SPI_TRANSFER_DELAY_USEC);
kevin1990 20:7031cd147ed7 715
kevin1990 20:7031cd147ed7 716 return eRet;
kevin1990 20:7031cd147ed7 717 }
kevin1990 20:7031cd147ed7 718
kevin1990 20:7031cd147ed7 719 /*
kevin1990 20:7031cd147ed7 720 * Close the given ADI Sense device.
kevin1990 20:7031cd147ed7 721 */
kevin1990 20:7031cd147ed7 722 ADI_SENSE_RESULT adi_sense_Close(
kevin1990 20:7031cd147ed7 723 ADI_SENSE_DEVICE_HANDLE const hDevice)
kevin1990 20:7031cd147ed7 724 {
kevin1990 20:7031cd147ed7 725 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 726
kevin1990 20:7031cd147ed7 727 adi_sense_GpioClose(pCtx->hGpio);
kevin1990 20:7031cd147ed7 728 adi_sense_SpiClose(pCtx->hSpi);
kevin1990 20:7031cd147ed7 729 adi_sense_LogClose();
kevin1990 20:7031cd147ed7 730
kevin1990 20:7031cd147ed7 731 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 732 }
kevin1990 20:7031cd147ed7 733
kevin1990 20:7031cd147ed7 734 ADI_SENSE_RESULT adi_sense_1000_WriteRegister(
kevin1990 20:7031cd147ed7 735 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 736 uint16_t nAddress,
kevin1990 20:7031cd147ed7 737 void *pData,
kevin1990 20:7031cd147ed7 738 unsigned nLength)
kevin1990 20:7031cd147ed7 739 {
kevin1990 20:7031cd147ed7 740 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 741 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 742 uint16_t command = REG_WRITE_COMMAND | (nAddress & REG_ADDRESS_MASK);
kevin1990 20:7031cd147ed7 743 uint8_t commandData[2] = {
kevin1990 20:7031cd147ed7 744 command >> 8,
kevin1990 20:7031cd147ed7 745 command & 0xFF
kevin1990 20:7031cd147ed7 746 };
kevin1990 20:7031cd147ed7 747 uint8_t commandResponse[2];
kevin1990 20:7031cd147ed7 748
kevin1990 20:7031cd147ed7 749 do {
kevin1990 20:7031cd147ed7 750 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
kevin1990 20:7031cd147ed7 751 sizeof(command), false);
kevin1990 20:7031cd147ed7 752 if (eRet)
kevin1990 20:7031cd147ed7 753 {
kevin1990 20:7031cd147ed7 754 ADI_SENSE_LOG_ERROR("Failed to send write command for register %u",
kevin1990 20:7031cd147ed7 755 nAddress);
kevin1990 20:7031cd147ed7 756 return eRet;
kevin1990 20:7031cd147ed7 757 }
kevin1990 20:7031cd147ed7 758
kevin1990 20:7031cd147ed7 759 adi_sense_TimeDelayUsec(POST_SPI_TRANSFER_DELAY_USEC);
kevin1990 20:7031cd147ed7 760 } while ((commandResponse[0] != REG_COMMAND_RESP_0) ||
kevin1990 20:7031cd147ed7 761 (commandResponse[1] != REG_COMMAND_RESP_1));
kevin1990 20:7031cd147ed7 762
kevin1990 20:7031cd147ed7 763 eRet = adi_sense_SpiTransfer(pCtx->hSpi, pData, NULL, nLength, false);
kevin1990 20:7031cd147ed7 764 if (eRet)
kevin1990 20:7031cd147ed7 765 {
kevin1990 20:7031cd147ed7 766 ADI_SENSE_LOG_ERROR("Failed to write data (%dB) to register %u",
kevin1990 20:7031cd147ed7 767 nLength, nAddress);
kevin1990 20:7031cd147ed7 768 return eRet;
kevin1990 20:7031cd147ed7 769 }
kevin1990 20:7031cd147ed7 770
kevin1990 20:7031cd147ed7 771 adi_sense_TimeDelayUsec(POST_SPI_TRANSFER_DELAY_USEC);
kevin1990 20:7031cd147ed7 772
kevin1990 20:7031cd147ed7 773 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 774 }
kevin1990 20:7031cd147ed7 775
kevin1990 20:7031cd147ed7 776 ADI_SENSE_RESULT adi_sense_1000_ReadRegister(
kevin1990 20:7031cd147ed7 777 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 778 uint16_t nAddress,
kevin1990 20:7031cd147ed7 779 void *pData,
kevin1990 20:7031cd147ed7 780 unsigned nLength)
kevin1990 20:7031cd147ed7 781 {
kevin1990 20:7031cd147ed7 782 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 783 ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice;
kevin1990 20:7031cd147ed7 784 uint16_t command = REG_READ_COMMAND | (nAddress & REG_ADDRESS_MASK);
kevin1990 20:7031cd147ed7 785 uint8_t commandData[2] = {
kevin1990 20:7031cd147ed7 786 command >> 8,
kevin1990 20:7031cd147ed7 787 command & 0xFF
kevin1990 20:7031cd147ed7 788 };
kevin1990 20:7031cd147ed7 789 uint8_t commandResponse[2];
kevin1990 20:7031cd147ed7 790
kevin1990 20:7031cd147ed7 791 do {
kevin1990 20:7031cd147ed7 792 eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, commandResponse,
kevin1990 20:7031cd147ed7 793 sizeof(command), false);
kevin1990 20:7031cd147ed7 794 if (eRet)
kevin1990 20:7031cd147ed7 795 {
kevin1990 20:7031cd147ed7 796 ADI_SENSE_LOG_ERROR("Failed to send read command for register %u",
kevin1990 20:7031cd147ed7 797 nAddress);
kevin1990 20:7031cd147ed7 798 return eRet;
kevin1990 20:7031cd147ed7 799 }
kevin1990 20:7031cd147ed7 800
kevin1990 20:7031cd147ed7 801 adi_sense_TimeDelayUsec(POST_SPI_TRANSFER_DELAY_USEC);
kevin1990 20:7031cd147ed7 802 } while ((commandResponse[0] != REG_COMMAND_RESP_0) ||
kevin1990 20:7031cd147ed7 803 (commandResponse[1] != REG_COMMAND_RESP_1));
kevin1990 20:7031cd147ed7 804
kevin1990 20:7031cd147ed7 805 eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, pData, nLength, false);
kevin1990 20:7031cd147ed7 806 if (eRet)
kevin1990 20:7031cd147ed7 807 {
kevin1990 20:7031cd147ed7 808 ADI_SENSE_LOG_ERROR("Failed to read data (%uB) from register %u",
kevin1990 20:7031cd147ed7 809 nLength, nAddress);
kevin1990 20:7031cd147ed7 810 return eRet;
kevin1990 20:7031cd147ed7 811 }
kevin1990 20:7031cd147ed7 812
kevin1990 20:7031cd147ed7 813 adi_sense_TimeDelayUsec(POST_SPI_TRANSFER_DELAY_USEC);
kevin1990 20:7031cd147ed7 814
kevin1990 20:7031cd147ed7 815 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 816 }
kevin1990 20:7031cd147ed7 817
kevin1990 20:7031cd147ed7 818 ADI_SENSE_RESULT adi_sense_GetDeviceReadyState(
kevin1990 20:7031cd147ed7 819 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 820 bool_t * const bReady)
kevin1990 20:7031cd147ed7 821 {
kevin1990 20:7031cd147ed7 822 ADI_ADISENSE_SPI_Chip_Type_t chipTypeReg;
kevin1990 20:7031cd147ed7 823
kevin1990 20:7031cd147ed7 824 READ_REG_U8(hDevice, chipTypeReg.VALUE8, SPI_CHIP_TYPE);
kevin1990 20:7031cd147ed7 825 /* If we read this register successfully, assume the device is ready */
kevin1990 20:7031cd147ed7 826 *bReady = (chipTypeReg.VALUE8 == REG_ADISENSE_SPI_CHIP_TYPE_RESET);
kevin1990 20:7031cd147ed7 827
kevin1990 20:7031cd147ed7 828 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 829 }
kevin1990 20:7031cd147ed7 830
kevin1990 20:7031cd147ed7 831 ADI_SENSE_RESULT adi_sense_1000_GetDataReadyModeInfo(
kevin1990 20:7031cd147ed7 832 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 833 ADI_SENSE_MEASUREMENT_MODE const eMeasurementMode,
kevin1990 20:7031cd147ed7 834 ADI_SENSE_1000_OPERATING_MODE * const peOperatingMode,
kevin1990 20:7031cd147ed7 835 ADI_SENSE_1000_DATAREADY_MODE * const peDataReadyMode,
kevin1990 20:7031cd147ed7 836 uint32_t * const pnSamplesPerDataready,
kevin1990 20:7031cd147ed7 837 uint32_t * const pnSamplesPerCycle)
kevin1990 20:7031cd147ed7 838 {
kevin1990 20:7031cd147ed7 839 unsigned nChannelsEnabled = 0;
kevin1990 20:7031cd147ed7 840 unsigned nSamplesPerCycle = 0;
kevin1990 20:7031cd147ed7 841
kevin1990 20:7031cd147ed7 842 for (ADI_SENSE_1000_CHANNEL_ID chId = 0; chId < ADI_SENSE_1000_MAX_CHANNELS; chId++)
kevin1990 20:7031cd147ed7 843 {
kevin1990 20:7031cd147ed7 844 ADI_ADISENSE_CORE_Sensor_Details_t sensorDetailsReg;
kevin1990 20:7031cd147ed7 845 ADI_ADISENSE_CORE_Channel_Count_t channelCountReg;
kevin1990 20:7031cd147ed7 846
kevin1990 20:7031cd147ed7 847 if (ADI_SENSE_1000_CHANNEL_IS_VIRTUAL(chId))
kevin1990 20:7031cd147ed7 848 continue;
kevin1990 20:7031cd147ed7 849
kevin1990 20:7031cd147ed7 850 READ_REG_U8(hDevice, channelCountReg.VALUE8, CORE_CHANNEL_COUNTn(chId));
kevin1990 20:7031cd147ed7 851 READ_REG_U32(hDevice, sensorDetailsReg.VALUE32, CORE_SENSOR_DETAILSn(chId));
kevin1990 20:7031cd147ed7 852
kevin1990 20:7031cd147ed7 853 if (channelCountReg.Channel_Enable && !sensorDetailsReg.Do_Not_Publish)
kevin1990 20:7031cd147ed7 854 {
kevin1990 20:7031cd147ed7 855 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
kevin1990 20:7031cd147ed7 856 unsigned nActualChannels = 1;
kevin1990 20:7031cd147ed7 857
kevin1990 20:7031cd147ed7 858 READ_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(chId));
kevin1990 20:7031cd147ed7 859
kevin1990 20:7031cd147ed7 860 if (chId == ADI_SENSE_1000_CHANNEL_ID_SPI_0)
kevin1990 20:7031cd147ed7 861 {
kevin1990 20:7031cd147ed7 862 /* Some sensors automatically generate samples on additional "virtual" channels
kevin1990 20:7031cd147ed7 863 * so these channels must be counted as active when those sensors are selected
kevin1990 20:7031cd147ed7 864 * and we use the count from the corresponding "physical" channel */
kevin1990 20:7031cd147ed7 865 if (sensorTypeReg.Sensor_Type ==
kevin1990 20:7031cd147ed7 866 ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_ACCELEROMETER_1)
kevin1990 20:7031cd147ed7 867 nActualChannels += 2;
kevin1990 20:7031cd147ed7 868 }
kevin1990 20:7031cd147ed7 869
kevin1990 20:7031cd147ed7 870 nChannelsEnabled += nActualChannels;
kevin1990 20:7031cd147ed7 871 if (eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK)
kevin1990 20:7031cd147ed7 872 /* Assume a single sample per channel in test mode */
kevin1990 20:7031cd147ed7 873 nSamplesPerCycle += nActualChannels;
kevin1990 20:7031cd147ed7 874 else
kevin1990 20:7031cd147ed7 875 nSamplesPerCycle += nActualChannels *
kevin1990 20:7031cd147ed7 876 (channelCountReg.Channel_Count + 1);
kevin1990 20:7031cd147ed7 877 }
kevin1990 20:7031cd147ed7 878 }
kevin1990 20:7031cd147ed7 879
kevin1990 20:7031cd147ed7 880 if (nChannelsEnabled == 0)
kevin1990 20:7031cd147ed7 881 {
kevin1990 20:7031cd147ed7 882 *pnSamplesPerDataready = 0;
kevin1990 20:7031cd147ed7 883 *pnSamplesPerCycle = 0;
kevin1990 20:7031cd147ed7 884 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 885 }
kevin1990 20:7031cd147ed7 886
kevin1990 20:7031cd147ed7 887 ADI_ADISENSE_CORE_Mode_t modeReg;
kevin1990 20:7031cd147ed7 888 READ_REG_U8(hDevice, modeReg.VALUE8, CORE_MODE);
kevin1990 20:7031cd147ed7 889
kevin1990 20:7031cd147ed7 890 *pnSamplesPerCycle = nSamplesPerCycle;
kevin1990 20:7031cd147ed7 891
kevin1990 20:7031cd147ed7 892 /* Assume DRDY_PER_CONVERSION behaviour in test mode */
kevin1990 20:7031cd147ed7 893 if ((eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK) ||
kevin1990 20:7031cd147ed7 894 (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CONVERSION))
kevin1990 20:7031cd147ed7 895 {
kevin1990 20:7031cd147ed7 896 *pnSamplesPerDataready = 1;
kevin1990 20:7031cd147ed7 897 }
kevin1990 20:7031cd147ed7 898 else if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CYCLE)
kevin1990 20:7031cd147ed7 899 {
kevin1990 20:7031cd147ed7 900 *pnSamplesPerDataready = nSamplesPerCycle;
kevin1990 20:7031cd147ed7 901 }
kevin1990 20:7031cd147ed7 902 else
kevin1990 20:7031cd147ed7 903 {
kevin1990 20:7031cd147ed7 904 ADI_ADISENSE_CORE_Fifo_Num_Cycles_t fifoNumCyclesReg;
kevin1990 20:7031cd147ed7 905 READ_REG_U8(hDevice, fifoNumCyclesReg.VALUE8, CORE_FIFO_NUM_CYCLES);
kevin1990 20:7031cd147ed7 906
kevin1990 20:7031cd147ed7 907 *pnSamplesPerDataready =
kevin1990 20:7031cd147ed7 908 nSamplesPerCycle * fifoNumCyclesReg.Fifo_Num_Cycles;
kevin1990 20:7031cd147ed7 909 }
kevin1990 20:7031cd147ed7 910
kevin1990 20:7031cd147ed7 911 /* Assume SINGLECYCLE in test mode */
kevin1990 20:7031cd147ed7 912 if ((eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK) ||
kevin1990 20:7031cd147ed7 913 (modeReg.Conversion_Mode == ADISENSE_CORE_MODE_SINGLECYCLE))
kevin1990 20:7031cd147ed7 914 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE;
kevin1990 20:7031cd147ed7 915 else if (modeReg.Conversion_Mode == ADISENSE_CORE_MODE_MULTICYCLE)
kevin1990 20:7031cd147ed7 916 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE;
kevin1990 20:7031cd147ed7 917 else
kevin1990 20:7031cd147ed7 918 *peOperatingMode = ADI_SENSE_1000_OPERATING_MODE_CONTINUOUS;
kevin1990 20:7031cd147ed7 919
kevin1990 20:7031cd147ed7 920 /* Assume DRDY_PER_CONVERSION behaviour in test mode */
kevin1990 20:7031cd147ed7 921 if ((eMeasurementMode == ADI_SENSE_MEASUREMENT_MODE_HEALTHCHECK) ||
kevin1990 20:7031cd147ed7 922 (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CONVERSION))
kevin1990 20:7031cd147ed7 923 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_CONVERSION;
kevin1990 20:7031cd147ed7 924 else if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CYCLE)
kevin1990 20:7031cd147ed7 925 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_CYCLE;
kevin1990 20:7031cd147ed7 926 else
kevin1990 20:7031cd147ed7 927 *peDataReadyMode = ADI_SENSE_1000_DATAREADY_PER_MULTICYCLE_BURST;
kevin1990 20:7031cd147ed7 928
kevin1990 20:7031cd147ed7 929 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 930 }
kevin1990 20:7031cd147ed7 931
kevin1990 20:7031cd147ed7 932 ADI_SENSE_RESULT adi_sense_GetProductID(
kevin1990 20:7031cd147ed7 933 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 934 ADI_SENSE_PRODUCT_ID *pProductId)
kevin1990 20:7031cd147ed7 935 {
kevin1990 20:7031cd147ed7 936 ADI_ADISENSE_SPI_Product_ID_L_t productIdLoReg;
kevin1990 20:7031cd147ed7 937 ADI_ADISENSE_SPI_Product_ID_H_t productIdHiReg;
kevin1990 20:7031cd147ed7 938
kevin1990 20:7031cd147ed7 939 READ_REG_U8(hDevice, productIdLoReg.VALUE8, SPI_PRODUCT_ID_L);
kevin1990 20:7031cd147ed7 940 READ_REG_U8(hDevice, productIdHiReg.VALUE8, SPI_PRODUCT_ID_H);
kevin1990 20:7031cd147ed7 941
kevin1990 20:7031cd147ed7 942 *pProductId = (productIdHiReg.VALUE8 << 8) | productIdLoReg.VALUE8;
kevin1990 20:7031cd147ed7 943 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 944 }
kevin1990 20:7031cd147ed7 945
kevin1990 20:7031cd147ed7 946 static ADI_SENSE_RESULT adi_sense_SetPowerMode(
kevin1990 20:7031cd147ed7 947 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 948 ADI_SENSE_1000_POWER_MODE powerMode)
kevin1990 20:7031cd147ed7 949 {
kevin1990 20:7031cd147ed7 950 ADI_ADISENSE_CORE_Power_Config_t powerConfigReg;
kevin1990 20:7031cd147ed7 951
kevin1990 20:7031cd147ed7 952 if (powerMode == ADI_SENSE_1000_POWER_MODE_LOW)
kevin1990 20:7031cd147ed7 953 {
kevin1990 20:7031cd147ed7 954 powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_LOW_POWER;
kevin1990 20:7031cd147ed7 955 /* TODO - we need an enum in the register map for the MCU power modes */
kevin1990 20:7031cd147ed7 956 powerConfigReg.Power_Mode_MCU = 0x0;
kevin1990 20:7031cd147ed7 957 }
kevin1990 20:7031cd147ed7 958 else if (powerMode == ADI_SENSE_1000_POWER_MODE_MID)
kevin1990 20:7031cd147ed7 959 {
kevin1990 20:7031cd147ed7 960 powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_MID_POWER;
kevin1990 20:7031cd147ed7 961 powerConfigReg.Power_Mode_MCU = 0x1;
kevin1990 20:7031cd147ed7 962 }
kevin1990 20:7031cd147ed7 963 else if (powerMode == ADI_SENSE_1000_POWER_MODE_FULL)
kevin1990 20:7031cd147ed7 964 {
kevin1990 20:7031cd147ed7 965 powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_FULL_POWER;
kevin1990 20:7031cd147ed7 966 powerConfigReg.Power_Mode_MCU = 0x2;
kevin1990 20:7031cd147ed7 967 }
kevin1990 20:7031cd147ed7 968 else
kevin1990 20:7031cd147ed7 969 {
kevin1990 20:7031cd147ed7 970 ADI_SENSE_LOG_ERROR("Invalid power mode %d specified", powerMode);
kevin1990 20:7031cd147ed7 971 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 972 }
kevin1990 20:7031cd147ed7 973
kevin1990 20:7031cd147ed7 974 WRITE_REG_U8(hDevice, powerConfigReg.VALUE8, CORE_POWER_CONFIG);
kevin1990 20:7031cd147ed7 975
kevin1990 20:7031cd147ed7 976 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 977 }
kevin1990 20:7031cd147ed7 978
kevin1990 20:7031cd147ed7 979 static ADI_SENSE_RESULT adi_sense_SetVddVoltage(
kevin1990 20:7031cd147ed7 980 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 981 float32_t vddVoltage)
kevin1990 20:7031cd147ed7 982 {
kevin1990 20:7031cd147ed7 983 WRITE_REG_FLOAT(hDevice, vddVoltage, CORE_AVDD_VOLTAGE);
kevin1990 20:7031cd147ed7 984
kevin1990 20:7031cd147ed7 985 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 986 }
kevin1990 20:7031cd147ed7 987
kevin1990 20:7031cd147ed7 988 ADI_SENSE_RESULT adi_sense_1000_SetPowerConfig(
kevin1990 20:7031cd147ed7 989 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 990 ADI_SENSE_1000_POWER_CONFIG *pPowerConfig)
kevin1990 20:7031cd147ed7 991 {
kevin1990 20:7031cd147ed7 992 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 993
kevin1990 20:7031cd147ed7 994 eRet = adi_sense_SetPowerMode(hDevice, pPowerConfig->powerMode);
kevin1990 20:7031cd147ed7 995 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 996 {
kevin1990 20:7031cd147ed7 997 ADI_SENSE_LOG_ERROR("Failed to set power mode");
kevin1990 20:7031cd147ed7 998 return eRet;
kevin1990 20:7031cd147ed7 999 }
kevin1990 20:7031cd147ed7 1000
kevin1990 20:7031cd147ed7 1001 eRet = adi_sense_SetVddVoltage(hDevice, pPowerConfig->supplyVoltage);
kevin1990 20:7031cd147ed7 1002 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1003 {
kevin1990 20:7031cd147ed7 1004 ADI_SENSE_LOG_ERROR("Failed to set AVdd voltage");
kevin1990 20:7031cd147ed7 1005 return eRet;
kevin1990 20:7031cd147ed7 1006 }
kevin1990 20:7031cd147ed7 1007
kevin1990 20:7031cd147ed7 1008 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1009 }
kevin1990 20:7031cd147ed7 1010
kevin1990 20:7031cd147ed7 1011 static ADI_SENSE_RESULT adi_sense_SetMode(
kevin1990 20:7031cd147ed7 1012 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1013 ADI_SENSE_1000_OPERATING_MODE eOperatingMode,
kevin1990 20:7031cd147ed7 1014 ADI_SENSE_1000_DATAREADY_MODE eDataReadyMode)
kevin1990 20:7031cd147ed7 1015 {
kevin1990 20:7031cd147ed7 1016 ADI_ADISENSE_CORE_Mode_t modeReg;
kevin1990 20:7031cd147ed7 1017
kevin1990 20:7031cd147ed7 1018 modeReg.VALUE8 = REG_RESET_VAL(CORE_MODE);
kevin1990 20:7031cd147ed7 1019
kevin1990 20:7031cd147ed7 1020 if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE)
kevin1990 20:7031cd147ed7 1021 {
kevin1990 20:7031cd147ed7 1022 modeReg.Conversion_Mode = ADISENSE_CORE_MODE_SINGLECYCLE;
kevin1990 20:7031cd147ed7 1023 }
kevin1990 20:7031cd147ed7 1024 else if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_CONTINUOUS)
kevin1990 20:7031cd147ed7 1025 {
kevin1990 20:7031cd147ed7 1026 modeReg.Conversion_Mode = ADISENSE_CORE_MODE_CONTINUOUS;
kevin1990 20:7031cd147ed7 1027 }
kevin1990 20:7031cd147ed7 1028 else if (eOperatingMode == ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE)
kevin1990 20:7031cd147ed7 1029 {
kevin1990 20:7031cd147ed7 1030 modeReg.Conversion_Mode = ADISENSE_CORE_MODE_MULTICYCLE;
kevin1990 20:7031cd147ed7 1031 }
kevin1990 20:7031cd147ed7 1032 else
kevin1990 20:7031cd147ed7 1033 {
kevin1990 20:7031cd147ed7 1034 ADI_SENSE_LOG_ERROR("Invalid operating mode %d specified",
kevin1990 20:7031cd147ed7 1035 eOperatingMode);
kevin1990 20:7031cd147ed7 1036 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1037 }
kevin1990 20:7031cd147ed7 1038
kevin1990 20:7031cd147ed7 1039 if (eDataReadyMode == ADI_SENSE_1000_DATAREADY_PER_CONVERSION)
kevin1990 20:7031cd147ed7 1040 {
kevin1990 20:7031cd147ed7 1041 modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_CONVERSION;
kevin1990 20:7031cd147ed7 1042 }
kevin1990 20:7031cd147ed7 1043 else if (eDataReadyMode == ADI_SENSE_1000_DATAREADY_PER_CYCLE)
kevin1990 20:7031cd147ed7 1044 {
kevin1990 20:7031cd147ed7 1045 modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_CYCLE;
kevin1990 20:7031cd147ed7 1046 }
kevin1990 20:7031cd147ed7 1047 else if (eDataReadyMode == ADI_SENSE_1000_DATAREADY_PER_MULTICYCLE_BURST)
kevin1990 20:7031cd147ed7 1048 {
kevin1990 20:7031cd147ed7 1049 if (eOperatingMode != ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE)
kevin1990 20:7031cd147ed7 1050 {
kevin1990 20:7031cd147ed7 1051 ADI_SENSE_LOG_ERROR(
kevin1990 20:7031cd147ed7 1052 "Data-ready mode %d cannot be used with operating mode %d",
kevin1990 20:7031cd147ed7 1053 eDataReadyMode, eOperatingMode);
kevin1990 20:7031cd147ed7 1054 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1055 }
kevin1990 20:7031cd147ed7 1056 else
kevin1990 20:7031cd147ed7 1057 {
kevin1990 20:7031cd147ed7 1058 modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_FIFO_FILL;
kevin1990 20:7031cd147ed7 1059 }
kevin1990 20:7031cd147ed7 1060 }
kevin1990 20:7031cd147ed7 1061 else
kevin1990 20:7031cd147ed7 1062 {
kevin1990 20:7031cd147ed7 1063 ADI_SENSE_LOG_ERROR("Invalid data-ready mode %d specified", eDataReadyMode);
kevin1990 20:7031cd147ed7 1064 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1065 }
kevin1990 20:7031cd147ed7 1066
kevin1990 20:7031cd147ed7 1067 WRITE_REG_U8(hDevice, modeReg.VALUE8, CORE_MODE);
kevin1990 20:7031cd147ed7 1068
kevin1990 20:7031cd147ed7 1069 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1070 }
kevin1990 20:7031cd147ed7 1071
kevin1990 20:7031cd147ed7 1072 ADI_SENSE_RESULT adi_sense_SetCycleInterval(
kevin1990 20:7031cd147ed7 1073 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1074 uint32_t nCycleInterval)
kevin1990 20:7031cd147ed7 1075 {
kevin1990 20:7031cd147ed7 1076 ADI_ADISENSE_CORE_Cycle_Control_t cycleControlReg;
kevin1990 20:7031cd147ed7 1077
kevin1990 20:7031cd147ed7 1078 cycleControlReg.VALUE16 = REG_RESET_VAL(CORE_CYCLE_CONTROL);
kevin1990 20:7031cd147ed7 1079
kevin1990 20:7031cd147ed7 1080 if (nCycleInterval < (1 << 12))
kevin1990 20:7031cd147ed7 1081 {
kevin1990 20:7031cd147ed7 1082 cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_MICROSECONDS;
kevin1990 20:7031cd147ed7 1083 }
kevin1990 20:7031cd147ed7 1084 else if (nCycleInterval < (1000 * (1 << 12)))
kevin1990 20:7031cd147ed7 1085 {
kevin1990 20:7031cd147ed7 1086 cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_MILLISECONDS;
kevin1990 20:7031cd147ed7 1087 nCycleInterval /= 1000;
kevin1990 20:7031cd147ed7 1088 }
kevin1990 20:7031cd147ed7 1089 else
kevin1990 20:7031cd147ed7 1090 {
kevin1990 20:7031cd147ed7 1091 cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_SECONDS;
kevin1990 20:7031cd147ed7 1092 nCycleInterval /= 1000000;
kevin1990 20:7031cd147ed7 1093 }
kevin1990 20:7031cd147ed7 1094
kevin1990 20:7031cd147ed7 1095 CHECK_REG_FIELD_VAL(CORE_CYCLE_CONTROL_CYCLE_TIME, nCycleInterval);
kevin1990 20:7031cd147ed7 1096 cycleControlReg.Cycle_Time = nCycleInterval;
kevin1990 20:7031cd147ed7 1097
kevin1990 20:7031cd147ed7 1098 WRITE_REG_U16(hDevice, cycleControlReg.VALUE16, CORE_CYCLE_CONTROL);
kevin1990 20:7031cd147ed7 1099
kevin1990 20:7031cd147ed7 1100 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1101 }
kevin1990 20:7031cd147ed7 1102
kevin1990 20:7031cd147ed7 1103 static ADI_SENSE_RESULT adi_sense_SetMultiCycleConfig(
kevin1990 20:7031cd147ed7 1104 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1105 ADI_SENSE_1000_MULTICYCLE_CONFIG *pMultiCycleConfig)
kevin1990 20:7031cd147ed7 1106 {
kevin1990 20:7031cd147ed7 1107 CHECK_REG_FIELD_VAL(CORE_FIFO_NUM_CYCLES_FIFO_NUM_CYCLES,
kevin1990 20:7031cd147ed7 1108 pMultiCycleConfig->cyclesPerBurst);
kevin1990 20:7031cd147ed7 1109
kevin1990 20:7031cd147ed7 1110 WRITE_REG_U8(hDevice, pMultiCycleConfig->cyclesPerBurst,
kevin1990 20:7031cd147ed7 1111 CORE_FIFO_NUM_CYCLES);
kevin1990 20:7031cd147ed7 1112
kevin1990 20:7031cd147ed7 1113 WRITE_REG_U32(hDevice, pMultiCycleConfig->burstInterval,
kevin1990 20:7031cd147ed7 1114 CORE_MULTI_CYCLE_REPEAT_INTERVAL);
kevin1990 20:7031cd147ed7 1115
kevin1990 20:7031cd147ed7 1116 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1117 }
kevin1990 20:7031cd147ed7 1118
kevin1990 20:7031cd147ed7 1119 static ADI_SENSE_RESULT adi_sense_SetExternalReferenceValues(
kevin1990 20:7031cd147ed7 1120 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1121 float32_t externalRef1Value,
kevin1990 20:7031cd147ed7 1122 float32_t externalRef2Value)
kevin1990 20:7031cd147ed7 1123 {
kevin1990 20:7031cd147ed7 1124 WRITE_REG_FLOAT(hDevice, externalRef1Value, CORE_EXTERNAL_REFERENCE1);
kevin1990 20:7031cd147ed7 1125 WRITE_REG_FLOAT(hDevice, externalRef2Value, CORE_EXTERNAL_REFERENCE2);
kevin1990 20:7031cd147ed7 1126
kevin1990 20:7031cd147ed7 1127 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1128 }
kevin1990 20:7031cd147ed7 1129
kevin1990 20:7031cd147ed7 1130 ADI_SENSE_RESULT adi_sense_1000_SetMeasurementConfig(
kevin1990 20:7031cd147ed7 1131 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1132 ADI_SENSE_1000_MEASUREMENT_CONFIG *pMeasConfig)
kevin1990 20:7031cd147ed7 1133 {
kevin1990 20:7031cd147ed7 1134 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 1135
kevin1990 20:7031cd147ed7 1136 eRet = adi_sense_SetMode(hDevice,
kevin1990 20:7031cd147ed7 1137 pMeasConfig->operatingMode,
kevin1990 20:7031cd147ed7 1138 pMeasConfig->dataReadyMode);
kevin1990 20:7031cd147ed7 1139 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1140 {
kevin1990 20:7031cd147ed7 1141 ADI_SENSE_LOG_ERROR("Failed to set operating mode");
kevin1990 20:7031cd147ed7 1142 return eRet;
kevin1990 20:7031cd147ed7 1143 }
kevin1990 20:7031cd147ed7 1144
kevin1990 20:7031cd147ed7 1145 if (pMeasConfig->operatingMode != ADI_SENSE_1000_OPERATING_MODE_SINGLECYCLE)
kevin1990 20:7031cd147ed7 1146 {
kevin1990 20:7031cd147ed7 1147 eRet = adi_sense_SetCycleInterval(hDevice, pMeasConfig->cycleInterval);
kevin1990 20:7031cd147ed7 1148 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1149 {
kevin1990 20:7031cd147ed7 1150 ADI_SENSE_LOG_ERROR("Failed to set cycle interval");
kevin1990 20:7031cd147ed7 1151 return eRet;
kevin1990 20:7031cd147ed7 1152 }
kevin1990 20:7031cd147ed7 1153 }
kevin1990 20:7031cd147ed7 1154
kevin1990 20:7031cd147ed7 1155 if (pMeasConfig->operatingMode == ADI_SENSE_1000_OPERATING_MODE_MULTICYCLE)
kevin1990 20:7031cd147ed7 1156 {
kevin1990 20:7031cd147ed7 1157 eRet = adi_sense_SetMultiCycleConfig(hDevice,
kevin1990 20:7031cd147ed7 1158 &pMeasConfig->multiCycleConfig);
kevin1990 20:7031cd147ed7 1159 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1160 {
kevin1990 20:7031cd147ed7 1161 ADI_SENSE_LOG_ERROR("Failed to set multi-cycle configuration");
kevin1990 20:7031cd147ed7 1162 return eRet;
kevin1990 20:7031cd147ed7 1163 }
kevin1990 20:7031cd147ed7 1164 }
kevin1990 20:7031cd147ed7 1165
kevin1990 20:7031cd147ed7 1166 eRet = adi_sense_SetExternalReferenceValues(hDevice,
kevin1990 20:7031cd147ed7 1167 pMeasConfig->externalRef1Value,
kevin1990 20:7031cd147ed7 1168 pMeasConfig->externalRef2Value);
kevin1990 20:7031cd147ed7 1169 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1170 {
kevin1990 20:7031cd147ed7 1171 ADI_SENSE_LOG_ERROR("Failed to set external reference values");
kevin1990 20:7031cd147ed7 1172 return eRet;
kevin1990 20:7031cd147ed7 1173 }
kevin1990 20:7031cd147ed7 1174
kevin1990 20:7031cd147ed7 1175 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1176 }
kevin1990 20:7031cd147ed7 1177
kevin1990 20:7031cd147ed7 1178 ADI_SENSE_RESULT adi_sense_1000_SetDiagnosticsConfig(
kevin1990 20:7031cd147ed7 1179 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1180 ADI_SENSE_1000_DIAGNOSTICS_CONFIG *pDiagnosticsConfig)
kevin1990 20:7031cd147ed7 1181 {
kevin1990 20:7031cd147ed7 1182 ADI_ADISENSE_CORE_Diagnostics_Control_t diagnosticsControlReg;
kevin1990 20:7031cd147ed7 1183
kevin1990 20:7031cd147ed7 1184 diagnosticsControlReg.VALUE16 = REG_RESET_VAL(CORE_DIAGNOSTICS_CONTROL);
kevin1990 20:7031cd147ed7 1185
kevin1990 20:7031cd147ed7 1186 if (pDiagnosticsConfig->disableGlobalDiag)
kevin1990 20:7031cd147ed7 1187 diagnosticsControlReg.Diag_Global_En = 0;
kevin1990 20:7031cd147ed7 1188 else
kevin1990 20:7031cd147ed7 1189 diagnosticsControlReg.Diag_Global_En = 1;
kevin1990 20:7031cd147ed7 1190
kevin1990 20:7031cd147ed7 1191 if (pDiagnosticsConfig->disableMeasurementDiag)
kevin1990 20:7031cd147ed7 1192 diagnosticsControlReg.Diag_Meas_En = 0;
kevin1990 20:7031cd147ed7 1193 else
kevin1990 20:7031cd147ed7 1194 diagnosticsControlReg.Diag_Meas_En = 1;
kevin1990 20:7031cd147ed7 1195
kevin1990 20:7031cd147ed7 1196 switch (pDiagnosticsConfig->osdFrequency)
kevin1990 20:7031cd147ed7 1197 {
kevin1990 20:7031cd147ed7 1198 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_DISABLED:
kevin1990 20:7031cd147ed7 1199 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_OFF;
kevin1990 20:7031cd147ed7 1200 break;
kevin1990 20:7031cd147ed7 1201 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_PER_CYCLE:
kevin1990 20:7031cd147ed7 1202 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_1_CYCLE;
kevin1990 20:7031cd147ed7 1203 break;
kevin1990 20:7031cd147ed7 1204 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_PER_100_CYCLES:
kevin1990 20:7031cd147ed7 1205 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_100_CYCLES;
kevin1990 20:7031cd147ed7 1206 break;
kevin1990 20:7031cd147ed7 1207 case ADI_SENSE_1000_OPEN_SENSOR_DIAGNOSTICS_PER_1000_CYCLES:
kevin1990 20:7031cd147ed7 1208 diagnosticsControlReg.Diag_OSD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_1000_CYCLES;
kevin1990 20:7031cd147ed7 1209 break;
kevin1990 20:7031cd147ed7 1210 default:
kevin1990 20:7031cd147ed7 1211 ADI_SENSE_LOG_ERROR("Invalid open-sensor diagnostic frequency %d specified",
kevin1990 20:7031cd147ed7 1212 pDiagnosticsConfig->osdFrequency);
kevin1990 20:7031cd147ed7 1213 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1214 }
kevin1990 20:7031cd147ed7 1215
kevin1990 20:7031cd147ed7 1216 WRITE_REG_U16(hDevice, diagnosticsControlReg.VALUE16, CORE_DIAGNOSTICS_CONTROL);
kevin1990 20:7031cd147ed7 1217
kevin1990 20:7031cd147ed7 1218 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1219 }
kevin1990 20:7031cd147ed7 1220
kevin1990 20:7031cd147ed7 1221 ADI_SENSE_RESULT adi_sense_1000_SetChannelCount(
kevin1990 20:7031cd147ed7 1222 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1223 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1224 uint32_t nMeasurementsPerCycle)
kevin1990 20:7031cd147ed7 1225 {
kevin1990 20:7031cd147ed7 1226 ADI_ADISENSE_CORE_Channel_Count_t channelCountReg;
kevin1990 20:7031cd147ed7 1227
kevin1990 20:7031cd147ed7 1228 channelCountReg.VALUE8 = REG_RESET_VAL(CORE_CHANNEL_COUNTn);
kevin1990 20:7031cd147ed7 1229
kevin1990 20:7031cd147ed7 1230 if (nMeasurementsPerCycle > 0)
kevin1990 20:7031cd147ed7 1231 {
kevin1990 20:7031cd147ed7 1232 nMeasurementsPerCycle -= 1;
kevin1990 20:7031cd147ed7 1233
kevin1990 20:7031cd147ed7 1234 CHECK_REG_FIELD_VAL(CORE_CHANNEL_COUNT_CHANNEL_COUNT,
kevin1990 20:7031cd147ed7 1235 nMeasurementsPerCycle);
kevin1990 20:7031cd147ed7 1236
kevin1990 20:7031cd147ed7 1237 channelCountReg.Channel_Enable = 1;
kevin1990 20:7031cd147ed7 1238 channelCountReg.Channel_Count = nMeasurementsPerCycle;
kevin1990 20:7031cd147ed7 1239 }
kevin1990 20:7031cd147ed7 1240 else
kevin1990 20:7031cd147ed7 1241 {
kevin1990 20:7031cd147ed7 1242 channelCountReg.Channel_Enable = 0;
kevin1990 20:7031cd147ed7 1243 }
kevin1990 20:7031cd147ed7 1244
kevin1990 20:7031cd147ed7 1245 WRITE_REG_U8(hDevice, channelCountReg.VALUE8, CORE_CHANNEL_COUNTn(eChannelId));
kevin1990 20:7031cd147ed7 1246
kevin1990 20:7031cd147ed7 1247 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1248 }
kevin1990 20:7031cd147ed7 1249
kevin1990 20:7031cd147ed7 1250 static ADI_SENSE_RESULT adi_sense_SetChannelAdcSensorType(
kevin1990 20:7031cd147ed7 1251 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1252 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1253 ADI_SENSE_1000_ADC_SENSOR_TYPE sensorType)
kevin1990 20:7031cd147ed7 1254 {
kevin1990 20:7031cd147ed7 1255 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
kevin1990 20:7031cd147ed7 1256
kevin1990 20:7031cd147ed7 1257 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
kevin1990 20:7031cd147ed7 1258
kevin1990 20:7031cd147ed7 1259 /* Ensure that the sensor type is valid for this channel */
kevin1990 20:7031cd147ed7 1260 switch(sensorType)
kevin1990 20:7031cd147ed7 1261 {
kevin1990 20:7031cd147ed7 1262 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_J_DEF_L1:
kevin1990 20:7031cd147ed7 1263 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_K_DEF_L1:
kevin1990 20:7031cd147ed7 1264 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_T_DEF_L1:
kevin1990 20:7031cd147ed7 1265 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_1_DEF_L2:
kevin1990 20:7031cd147ed7 1266 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_2_DEF_L2:
kevin1990 20:7031cd147ed7 1267 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_3_DEF_L2:
kevin1990 20:7031cd147ed7 1268 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_4_DEF_L2:
kevin1990 20:7031cd147ed7 1269 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_J_ADV_L1:
kevin1990 20:7031cd147ed7 1270 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_K_ADV_L1:
kevin1990 20:7031cd147ed7 1271 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_T_ADV_L1:
kevin1990 20:7031cd147ed7 1272 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_1_ADV_L2:
kevin1990 20:7031cd147ed7 1273 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_2_ADV_L2:
kevin1990 20:7031cd147ed7 1274 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_3_ADV_L2:
kevin1990 20:7031cd147ed7 1275 case ADI_SENSE_1000_ADC_SENSOR_THERMOCOUPLE_4_ADV_L2:
kevin1990 20:7031cd147ed7 1276 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT100_DEF_L1:
kevin1990 20:7031cd147ed7 1277 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT1000_DEF_L1:
kevin1990 20:7031cd147ed7 1278 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_1_DEF_L2:
kevin1990 20:7031cd147ed7 1279 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_2_DEF_L2:
kevin1990 20:7031cd147ed7 1280 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_3_DEF_L2:
kevin1990 20:7031cd147ed7 1281 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_4_DEF_L2:
kevin1990 20:7031cd147ed7 1282 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT100_ADV_L1:
kevin1990 20:7031cd147ed7 1283 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_PT1000_ADV_L1:
kevin1990 20:7031cd147ed7 1284 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_1_ADV_L2:
kevin1990 20:7031cd147ed7 1285 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_2_ADV_L2:
kevin1990 20:7031cd147ed7 1286 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_3_ADV_L2:
kevin1990 20:7031cd147ed7 1287 case ADI_SENSE_1000_ADC_SENSOR_RTD_3WIRE_4_ADV_L2:
kevin1990 20:7031cd147ed7 1288 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT100_DEF_L1:
kevin1990 20:7031cd147ed7 1289 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT1000_DEF_L1:
kevin1990 20:7031cd147ed7 1290 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_1_DEF_L2:
kevin1990 20:7031cd147ed7 1291 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_2_DEF_L2:
kevin1990 20:7031cd147ed7 1292 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_3_DEF_L2:
kevin1990 20:7031cd147ed7 1293 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_4_DEF_L2:
kevin1990 20:7031cd147ed7 1294 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT100_ADV_L1:
kevin1990 20:7031cd147ed7 1295 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_PT1000_ADV_L1:
kevin1990 20:7031cd147ed7 1296 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_1_ADV_L2:
kevin1990 20:7031cd147ed7 1297 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_2_ADV_L2:
kevin1990 20:7031cd147ed7 1298 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_3_ADV_L2:
kevin1990 20:7031cd147ed7 1299 case ADI_SENSE_1000_ADC_SENSOR_RTD_4WIRE_4_ADV_L2:
kevin1990 20:7031cd147ed7 1300 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_1_DEF_L2:
kevin1990 20:7031cd147ed7 1301 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_2_DEF_L2:
kevin1990 20:7031cd147ed7 1302 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_3_DEF_L2:
kevin1990 20:7031cd147ed7 1303 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_4_DEF_L2:
kevin1990 20:7031cd147ed7 1304 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_1_ADV_L2:
kevin1990 20:7031cd147ed7 1305 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_2_ADV_L2:
kevin1990 20:7031cd147ed7 1306 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_3_ADV_L2:
kevin1990 20:7031cd147ed7 1307 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_4WIRE_4_ADV_L2:
kevin1990 20:7031cd147ed7 1308 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_1_DEF_L2:
kevin1990 20:7031cd147ed7 1309 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_2_DEF_L2:
kevin1990 20:7031cd147ed7 1310 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_3_DEF_L2:
kevin1990 20:7031cd147ed7 1311 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_4_DEF_L2:
kevin1990 20:7031cd147ed7 1312 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_1_ADV_L2:
kevin1990 20:7031cd147ed7 1313 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_2_ADV_L2:
kevin1990 20:7031cd147ed7 1314 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_3_ADV_L2:
kevin1990 20:7031cd147ed7 1315 case ADI_SENSE_1000_ADC_SENSOR_BRIDGE_6WIRE_4_ADV_L2:
kevin1990 20:7031cd147ed7 1316 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_A_10K_DEF_L1:
kevin1990 20:7031cd147ed7 1317 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_B_10K_DEF_L1:
kevin1990 20:7031cd147ed7 1318 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_1_DEF_L2:
kevin1990 20:7031cd147ed7 1319 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_2_DEF_L2:
kevin1990 20:7031cd147ed7 1320 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_3_DEF_L2:
kevin1990 20:7031cd147ed7 1321 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_4_DEF_L2:
kevin1990 20:7031cd147ed7 1322 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_A_10K_ADV_L1:
kevin1990 20:7031cd147ed7 1323 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_B_10K_ADV_L1:
kevin1990 20:7031cd147ed7 1324 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_1_ADV_L2:
kevin1990 20:7031cd147ed7 1325 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_2_ADV_L2:
kevin1990 20:7031cd147ed7 1326 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_3_ADV_L2:
kevin1990 20:7031cd147ed7 1327 case ADI_SENSE_1000_ADC_SENSOR_THERMISTOR_4_ADV_L2:
kevin1990 20:7031cd147ed7 1328 if (! ADI_SENSE_1000_CHANNEL_IS_ADC_SENSOR(eChannelId))
kevin1990 20:7031cd147ed7 1329 {
kevin1990 20:7031cd147ed7 1330 ADI_SENSE_LOG_ERROR(
kevin1990 20:7031cd147ed7 1331 "Invalid ADC sensor type %d specified for channel %d",
kevin1990 20:7031cd147ed7 1332 sensorType, eChannelId);
kevin1990 20:7031cd147ed7 1333 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1334 }
kevin1990 20:7031cd147ed7 1335 break;
kevin1990 20:7031cd147ed7 1336 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT100_DEF_L1:
kevin1990 20:7031cd147ed7 1337 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT1000_DEF_L1:
kevin1990 20:7031cd147ed7 1338 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_1_DEF_L2:
kevin1990 20:7031cd147ed7 1339 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_2_DEF_L2:
kevin1990 20:7031cd147ed7 1340 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_3_DEF_L2:
kevin1990 20:7031cd147ed7 1341 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_4_DEF_L2:
kevin1990 20:7031cd147ed7 1342 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT100_ADV_L1:
kevin1990 20:7031cd147ed7 1343 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_PT1000_ADV_L1:
kevin1990 20:7031cd147ed7 1344 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_1_ADV_L2:
kevin1990 20:7031cd147ed7 1345 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_2_ADV_L2:
kevin1990 20:7031cd147ed7 1346 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_3_ADV_L2:
kevin1990 20:7031cd147ed7 1347 case ADI_SENSE_1000_ADC_SENSOR_RTD_2WIRE_4_ADV_L2:
kevin1990 20:7031cd147ed7 1348 if (! (ADI_SENSE_1000_CHANNEL_IS_ADC_SENSOR(eChannelId) ||
kevin1990 20:7031cd147ed7 1349 ADI_SENSE_1000_CHANNEL_IS_ADC_CJC(eChannelId)))
kevin1990 20:7031cd147ed7 1350 {
kevin1990 20:7031cd147ed7 1351 ADI_SENSE_LOG_ERROR(
kevin1990 20:7031cd147ed7 1352 "Invalid ADC sensor type %d specified for channel %d",
kevin1990 20:7031cd147ed7 1353 sensorType, eChannelId);
kevin1990 20:7031cd147ed7 1354 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1355 }
kevin1990 20:7031cd147ed7 1356 break;
kevin1990 20:7031cd147ed7 1357 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE:
kevin1990 20:7031cd147ed7 1358 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_HONEYWELL_TRUSTABILITY:
kevin1990 20:7031cd147ed7 1359 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_AMPHENOL_NPA300X:
kevin1990 20:7031cd147ed7 1360 case ADI_SENSE_1000_ADC_SENSOR_VOLTAGE_PRESSURE_3_DEF:
kevin1990 20:7031cd147ed7 1361 if (! ADI_SENSE_1000_CHANNEL_IS_ADC_VOLTAGE(eChannelId))
kevin1990 20:7031cd147ed7 1362 {
kevin1990 20:7031cd147ed7 1363 ADI_SENSE_LOG_ERROR(
kevin1990 20:7031cd147ed7 1364 "Invalid ADC sensor type %d specified for channel %d",
kevin1990 20:7031cd147ed7 1365 sensorType, eChannelId);
kevin1990 20:7031cd147ed7 1366 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1367 }
kevin1990 20:7031cd147ed7 1368 break;
kevin1990 20:7031cd147ed7 1369 case ADI_SENSE_1000_ADC_SENSOR_CURRENT:
kevin1990 20:7031cd147ed7 1370 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_HONEYWELL_PX2:
kevin1990 20:7031cd147ed7 1371 case ADI_SENSE_1000_ADC_SENSOR_CURRENT_PRESSURE_2_DEF:
kevin1990 20:7031cd147ed7 1372 if (! ADI_SENSE_1000_CHANNEL_IS_ADC_CURRENT(eChannelId))
kevin1990 20:7031cd147ed7 1373 {
kevin1990 20:7031cd147ed7 1374 ADI_SENSE_LOG_ERROR(
kevin1990 20:7031cd147ed7 1375 "Invalid ADC sensor type %d specified for channel %d",
kevin1990 20:7031cd147ed7 1376 sensorType, eChannelId);
kevin1990 20:7031cd147ed7 1377 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1378 }
kevin1990 20:7031cd147ed7 1379 break;
kevin1990 20:7031cd147ed7 1380 default:
kevin1990 20:7031cd147ed7 1381 ADI_SENSE_LOG_ERROR("Invalid/unsupported ADC sensor type %d specified",
kevin1990 20:7031cd147ed7 1382 sensorType);
kevin1990 20:7031cd147ed7 1383 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1384 }
kevin1990 20:7031cd147ed7 1385
kevin1990 20:7031cd147ed7 1386 sensorTypeReg.Sensor_Type = sensorType;
kevin1990 20:7031cd147ed7 1387
kevin1990 20:7031cd147ed7 1388 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
kevin1990 20:7031cd147ed7 1389
kevin1990 20:7031cd147ed7 1390 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1391 }
kevin1990 20:7031cd147ed7 1392
kevin1990 20:7031cd147ed7 1393 static ADI_SENSE_RESULT adi_sense_SetChannelAdcSensorDetails(
kevin1990 20:7031cd147ed7 1394 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1395 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1396 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
kevin1990 20:7031cd147ed7 1397 /*
kevin1990 20:7031cd147ed7 1398 * TODO - it would be nice if the general- vs. ADC-specific sensor details could be split into separate registers
kevin1990 20:7031cd147ed7 1399 * General details:
kevin1990 20:7031cd147ed7 1400 * - Measurement_Units
kevin1990 20:7031cd147ed7 1401 * - Compensation_Channel
kevin1990 20:7031cd147ed7 1402 * - CJC_Publish (if "CJC" was removed from the name)
kevin1990 20:7031cd147ed7 1403 * ADC-specific details:
kevin1990 20:7031cd147ed7 1404 * - PGA_Gain
kevin1990 20:7031cd147ed7 1405 * - Reference_Select
kevin1990 20:7031cd147ed7 1406 * - Reference_Buffer_Disable
kevin1990 20:7031cd147ed7 1407 * - Vbias
kevin1990 20:7031cd147ed7 1408 */
kevin1990 20:7031cd147ed7 1409 {
kevin1990 20:7031cd147ed7 1410 ADI_SENSE_1000_ADC_CHANNEL_CONFIG *pAdcChannelConfig = &pChannelConfig->adcChannelConfig;
kevin1990 20:7031cd147ed7 1411 ADI_SENSE_1000_ADC_REFERENCE_CONFIG *pRefConfig = &pAdcChannelConfig->reference;
kevin1990 20:7031cd147ed7 1412 ADI_ADISENSE_CORE_Sensor_Details_t sensorDetailsReg;
kevin1990 20:7031cd147ed7 1413
kevin1990 20:7031cd147ed7 1414 sensorDetailsReg.VALUE32 = REG_RESET_VAL(CORE_SENSOR_DETAILSn);
kevin1990 20:7031cd147ed7 1415
kevin1990 20:7031cd147ed7 1416 switch(pChannelConfig->measurementUnit)
kevin1990 20:7031cd147ed7 1417 {
kevin1990 20:7031cd147ed7 1418 case ADI_SENSE_1000_MEASUREMENT_UNIT_FAHRENHEIT:
kevin1990 20:7031cd147ed7 1419 sensorDetailsReg.Measurement_Units = ADISENSE_CORE_SENSOR_DETAILS_UNITS_DEGF;
kevin1990 20:7031cd147ed7 1420 break;
kevin1990 20:7031cd147ed7 1421 case ADI_SENSE_1000_MEASUREMENT_UNIT_CELSIUS:
kevin1990 20:7031cd147ed7 1422 case ADI_SENSE_1000_MEASUREMENT_UNIT_DEFAULT:
kevin1990 20:7031cd147ed7 1423 sensorDetailsReg.Measurement_Units = ADISENSE_CORE_SENSOR_DETAILS_UNITS_DEGC;
kevin1990 20:7031cd147ed7 1424 break;
kevin1990 20:7031cd147ed7 1425 default:
kevin1990 20:7031cd147ed7 1426 ADI_SENSE_LOG_ERROR("Invalid measurement unit %d specified",
kevin1990 20:7031cd147ed7 1427 pChannelConfig->measurementUnit);
kevin1990 20:7031cd147ed7 1428 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1429 }
kevin1990 20:7031cd147ed7 1430
kevin1990 20:7031cd147ed7 1431 sensorDetailsReg.Compensation_Channel = pChannelConfig->compensationChannel;
kevin1990 20:7031cd147ed7 1432
kevin1990 20:7031cd147ed7 1433 switch(pRefConfig->type)
kevin1990 20:7031cd147ed7 1434 {
kevin1990 20:7031cd147ed7 1435 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_INTERNAL_1:
kevin1990 20:7031cd147ed7 1436 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_RINT1;
kevin1990 20:7031cd147ed7 1437 break;
kevin1990 20:7031cd147ed7 1438 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_INTERNAL_2:
kevin1990 20:7031cd147ed7 1439 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_RINT2;
kevin1990 20:7031cd147ed7 1440 break;
kevin1990 20:7031cd147ed7 1441 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_INTERNAL:
kevin1990 20:7031cd147ed7 1442 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_INT;
kevin1990 20:7031cd147ed7 1443 break;
kevin1990 20:7031cd147ed7 1444 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_AVDD:
kevin1990 20:7031cd147ed7 1445 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_AVDD;
kevin1990 20:7031cd147ed7 1446 break;
kevin1990 20:7031cd147ed7 1447 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_EXTERNAL_1:
kevin1990 20:7031cd147ed7 1448 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_REXT1;
kevin1990 20:7031cd147ed7 1449 break;
kevin1990 20:7031cd147ed7 1450 case ADI_SENSE_1000_ADC_REFERENCE_RESISTOR_EXTERNAL_2:
kevin1990 20:7031cd147ed7 1451 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_REXT2;
kevin1990 20:7031cd147ed7 1452 break;
kevin1990 20:7031cd147ed7 1453 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_EXTERNAL_1:
kevin1990 20:7031cd147ed7 1454 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_VEXT1;
kevin1990 20:7031cd147ed7 1455 break;
kevin1990 20:7031cd147ed7 1456 case ADI_SENSE_1000_ADC_REFERENCE_VOLTAGE_EXTERNAL_2:
kevin1990 20:7031cd147ed7 1457 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_VEXT2;
kevin1990 20:7031cd147ed7 1458 break;
kevin1990 20:7031cd147ed7 1459 case ADI_SENSE_1000_ADC_REFERENCE_BRIDGE_EXCITATION:
kevin1990 20:7031cd147ed7 1460 sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_EXC;
kevin1990 20:7031cd147ed7 1461 break;
kevin1990 20:7031cd147ed7 1462 default:
kevin1990 20:7031cd147ed7 1463 ADI_SENSE_LOG_ERROR("Invalid ADC reference type %d specified",
kevin1990 20:7031cd147ed7 1464 pRefConfig->type);
kevin1990 20:7031cd147ed7 1465 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1466 }
kevin1990 20:7031cd147ed7 1467
kevin1990 20:7031cd147ed7 1468 switch(pAdcChannelConfig->gain)
kevin1990 20:7031cd147ed7 1469 {
kevin1990 20:7031cd147ed7 1470 case ADI_SENSE_1000_ADC_GAIN_1X:
kevin1990 20:7031cd147ed7 1471 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_1;
kevin1990 20:7031cd147ed7 1472 break;
kevin1990 20:7031cd147ed7 1473 case ADI_SENSE_1000_ADC_GAIN_2X:
kevin1990 20:7031cd147ed7 1474 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_2;
kevin1990 20:7031cd147ed7 1475 break;
kevin1990 20:7031cd147ed7 1476 case ADI_SENSE_1000_ADC_GAIN_4X:
kevin1990 20:7031cd147ed7 1477 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_4;
kevin1990 20:7031cd147ed7 1478 break;
kevin1990 20:7031cd147ed7 1479 case ADI_SENSE_1000_ADC_GAIN_8X:
kevin1990 20:7031cd147ed7 1480 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_8;
kevin1990 20:7031cd147ed7 1481 break;
kevin1990 20:7031cd147ed7 1482 case ADI_SENSE_1000_ADC_GAIN_16X:
kevin1990 20:7031cd147ed7 1483 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_16;
kevin1990 20:7031cd147ed7 1484 break;
kevin1990 20:7031cd147ed7 1485 case ADI_SENSE_1000_ADC_GAIN_32X:
kevin1990 20:7031cd147ed7 1486 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_32;
kevin1990 20:7031cd147ed7 1487 break;
kevin1990 20:7031cd147ed7 1488 case ADI_SENSE_1000_ADC_GAIN_64X:
kevin1990 20:7031cd147ed7 1489 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_64;
kevin1990 20:7031cd147ed7 1490 break;
kevin1990 20:7031cd147ed7 1491 case ADI_SENSE_1000_ADC_GAIN_128X:
kevin1990 20:7031cd147ed7 1492 sensorDetailsReg.PGA_Gain = ADISENSE_CORE_SENSOR_DETAILS_PGA_GAIN_128;
kevin1990 20:7031cd147ed7 1493 break;
kevin1990 20:7031cd147ed7 1494 default:
kevin1990 20:7031cd147ed7 1495 ADI_SENSE_LOG_ERROR("Invalid ADC gain %d specified",
kevin1990 20:7031cd147ed7 1496 pAdcChannelConfig->gain);
kevin1990 20:7031cd147ed7 1497 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1498 }
kevin1990 20:7031cd147ed7 1499
kevin1990 20:7031cd147ed7 1500 if (pAdcChannelConfig->enableVbias)
kevin1990 20:7031cd147ed7 1501 sensorDetailsReg.Vbias = 1;
kevin1990 20:7031cd147ed7 1502 else
kevin1990 20:7031cd147ed7 1503 sensorDetailsReg.Vbias = 0;
kevin1990 20:7031cd147ed7 1504
kevin1990 20:7031cd147ed7 1505 if (pAdcChannelConfig->reference.disableBuffer)
kevin1990 20:7031cd147ed7 1506 sensorDetailsReg.Reference_Buffer_Disable = 1;
kevin1990 20:7031cd147ed7 1507 else
kevin1990 20:7031cd147ed7 1508 sensorDetailsReg.Reference_Buffer_Disable = 0;
kevin1990 20:7031cd147ed7 1509
kevin1990 20:7031cd147ed7 1510 if (pChannelConfig->disablePublishing)
kevin1990 20:7031cd147ed7 1511 sensorDetailsReg.Do_Not_Publish = 1;
kevin1990 20:7031cd147ed7 1512 else
kevin1990 20:7031cd147ed7 1513 sensorDetailsReg.Do_Not_Publish = 0;
kevin1990 20:7031cd147ed7 1514
kevin1990 20:7031cd147ed7 1515 WRITE_REG_U32(hDevice, sensorDetailsReg.VALUE32, CORE_SENSOR_DETAILSn(eChannelId));
kevin1990 20:7031cd147ed7 1516
kevin1990 20:7031cd147ed7 1517 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1518 }
kevin1990 20:7031cd147ed7 1519
kevin1990 20:7031cd147ed7 1520 static ADI_SENSE_RESULT adi_sense_SetChannelAdcFilter(
kevin1990 20:7031cd147ed7 1521 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1522 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1523 ADI_SENSE_1000_ADC_FILTER_CONFIG *pFilterConfig)
kevin1990 20:7031cd147ed7 1524 {
kevin1990 20:7031cd147ed7 1525 ADI_ADISENSE_CORE_Filter_Select_t filterSelectReg;
kevin1990 20:7031cd147ed7 1526
kevin1990 20:7031cd147ed7 1527 filterSelectReg.VALUE32 = REG_RESET_VAL(CORE_FILTER_SELECTn);
kevin1990 20:7031cd147ed7 1528
kevin1990 20:7031cd147ed7 1529 if (pFilterConfig->type == ADI_SENSE_1000_ADC_FILTER_SINC4)
kevin1990 20:7031cd147ed7 1530 {
kevin1990 20:7031cd147ed7 1531 filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_SINC4;
kevin1990 20:7031cd147ed7 1532 filterSelectReg.ADC_FS = pFilterConfig->fs;
kevin1990 20:7031cd147ed7 1533 }
kevin1990 20:7031cd147ed7 1534 else if (pFilterConfig->type == ADI_SENSE_1000_ADC_FILTER_FIR_20SPS)
kevin1990 20:7031cd147ed7 1535 {
kevin1990 20:7031cd147ed7 1536 filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_FIR_20SPS;
kevin1990 20:7031cd147ed7 1537 }
kevin1990 20:7031cd147ed7 1538 else if (pFilterConfig->type == ADI_SENSE_1000_ADC_FILTER_FIR_25SPS)
kevin1990 20:7031cd147ed7 1539 {
kevin1990 20:7031cd147ed7 1540 filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_FIR_25SPS;
kevin1990 20:7031cd147ed7 1541 }
kevin1990 20:7031cd147ed7 1542 else
kevin1990 20:7031cd147ed7 1543 {
kevin1990 20:7031cd147ed7 1544 ADI_SENSE_LOG_ERROR("Invalid ADC filter type %d specified",
kevin1990 20:7031cd147ed7 1545 pFilterConfig->type);
kevin1990 20:7031cd147ed7 1546 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1547 }
kevin1990 20:7031cd147ed7 1548
kevin1990 20:7031cd147ed7 1549 WRITE_REG_U32(hDevice, filterSelectReg.VALUE32, CORE_FILTER_SELECTn(eChannelId));
kevin1990 20:7031cd147ed7 1550
kevin1990 20:7031cd147ed7 1551 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1552 }
kevin1990 20:7031cd147ed7 1553
kevin1990 20:7031cd147ed7 1554 static ADI_SENSE_RESULT adi_sense_SetChannelAdcCurrentConfig(
kevin1990 20:7031cd147ed7 1555 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1556 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1557 ADI_SENSE_1000_ADC_EXC_CURRENT_CONFIG *pCurrentConfig)
kevin1990 20:7031cd147ed7 1558 {
kevin1990 20:7031cd147ed7 1559 ADI_ADISENSE_CORE_Channel_Excitation_t channelExcitationReg;
kevin1990 20:7031cd147ed7 1560
kevin1990 20:7031cd147ed7 1561 channelExcitationReg.VALUE8 = REG_RESET_VAL(CORE_CHANNEL_EXCITATIONn);
kevin1990 20:7031cd147ed7 1562
kevin1990 20:7031cd147ed7 1563 if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_NONE)
kevin1990 20:7031cd147ed7 1564 {
kevin1990 20:7031cd147ed7 1565 channelExcitationReg.IOUT0_Disable = 1;
kevin1990 20:7031cd147ed7 1566 channelExcitationReg.IOUT1_Disable = 1;
kevin1990 20:7031cd147ed7 1567
kevin1990 20:7031cd147ed7 1568 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_OFF;
kevin1990 20:7031cd147ed7 1569 }
kevin1990 20:7031cd147ed7 1570 else
kevin1990 20:7031cd147ed7 1571 {
kevin1990 20:7031cd147ed7 1572 channelExcitationReg.IOUT0_Disable = 0;
kevin1990 20:7031cd147ed7 1573 channelExcitationReg.IOUT1_Disable = 0;
kevin1990 20:7031cd147ed7 1574
kevin1990 20:7031cd147ed7 1575 if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_50uA)
kevin1990 20:7031cd147ed7 1576 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_50UA;
kevin1990 20:7031cd147ed7 1577 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_100uA)
kevin1990 20:7031cd147ed7 1578 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_100UA;
kevin1990 20:7031cd147ed7 1579 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_250uA)
kevin1990 20:7031cd147ed7 1580 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_250UA;
kevin1990 20:7031cd147ed7 1581 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_500uA)
kevin1990 20:7031cd147ed7 1582 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_500UA;
kevin1990 20:7031cd147ed7 1583 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_750uA)
kevin1990 20:7031cd147ed7 1584 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_750UA;
kevin1990 20:7031cd147ed7 1585 else if (pCurrentConfig->outputLevel == ADI_SENSE_1000_ADC_EXC_CURRENT_1000uA)
kevin1990 20:7031cd147ed7 1586 channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_1000UA;
kevin1990 20:7031cd147ed7 1587 else
kevin1990 20:7031cd147ed7 1588 {
kevin1990 20:7031cd147ed7 1589 ADI_SENSE_LOG_ERROR("Invalid ADC excitation current %d specified",
kevin1990 20:7031cd147ed7 1590 pCurrentConfig->outputLevel);
kevin1990 20:7031cd147ed7 1591 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1592 }
kevin1990 20:7031cd147ed7 1593
kevin1990 20:7031cd147ed7 1594 if (pCurrentConfig->swapOption == ADI_SENSE_1000_ADC_EXC_CURRENT_SWAP_DYNAMIC)
kevin1990 20:7031cd147ed7 1595 {
kevin1990 20:7031cd147ed7 1596 channelExcitationReg.IOUT_Dont_Swap_3Wire = 0;
kevin1990 20:7031cd147ed7 1597 channelExcitationReg.IOUT_Static_Swap_3Wire = 0;
kevin1990 20:7031cd147ed7 1598 }
kevin1990 20:7031cd147ed7 1599 else if (pCurrentConfig->swapOption == ADI_SENSE_1000_ADC_EXC_CURRENT_SWAP_STATIC)
kevin1990 20:7031cd147ed7 1600 {
kevin1990 20:7031cd147ed7 1601 channelExcitationReg.IOUT_Dont_Swap_3Wire = 1;
kevin1990 20:7031cd147ed7 1602 channelExcitationReg.IOUT_Static_Swap_3Wire = 1;
kevin1990 20:7031cd147ed7 1603 }
kevin1990 20:7031cd147ed7 1604 else if (pCurrentConfig->swapOption == ADI_SENSE_1000_ADC_EXC_CURRENT_SWAP_NONE)
kevin1990 20:7031cd147ed7 1605 {
kevin1990 20:7031cd147ed7 1606 channelExcitationReg.IOUT_Dont_Swap_3Wire = 1;
kevin1990 20:7031cd147ed7 1607 channelExcitationReg.IOUT_Static_Swap_3Wire = 0;
kevin1990 20:7031cd147ed7 1608 }
kevin1990 20:7031cd147ed7 1609 else
kevin1990 20:7031cd147ed7 1610 {
kevin1990 20:7031cd147ed7 1611 ADI_SENSE_LOG_ERROR(
kevin1990 20:7031cd147ed7 1612 "Invalid ADC excitation current swap option %d specified",
kevin1990 20:7031cd147ed7 1613 pCurrentConfig->swapOption);
kevin1990 20:7031cd147ed7 1614 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1615 }
kevin1990 20:7031cd147ed7 1616 }
kevin1990 20:7031cd147ed7 1617
kevin1990 20:7031cd147ed7 1618 WRITE_REG_U8(hDevice, channelExcitationReg.VALUE8, CORE_CHANNEL_EXCITATIONn(eChannelId));
kevin1990 20:7031cd147ed7 1619
kevin1990 20:7031cd147ed7 1620 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1621 }
kevin1990 20:7031cd147ed7 1622
kevin1990 20:7031cd147ed7 1623 ADI_SENSE_RESULT adi_sense_SetAdcChannelConfig(
kevin1990 20:7031cd147ed7 1624 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1625 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1626 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
kevin1990 20:7031cd147ed7 1627 {
kevin1990 20:7031cd147ed7 1628 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 1629 ADI_SENSE_1000_ADC_CHANNEL_CONFIG *pAdcChannelConfig =
kevin1990 20:7031cd147ed7 1630 &pChannelConfig->adcChannelConfig;
kevin1990 20:7031cd147ed7 1631
kevin1990 20:7031cd147ed7 1632 eRet = adi_sense_SetChannelAdcSensorType(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1633 pAdcChannelConfig->sensor);
kevin1990 20:7031cd147ed7 1634 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1635 {
kevin1990 20:7031cd147ed7 1636 ADI_SENSE_LOG_ERROR("Failed to set ADC sensor type for channel %d",
kevin1990 20:7031cd147ed7 1637 eChannelId);
kevin1990 20:7031cd147ed7 1638 return eRet;
kevin1990 20:7031cd147ed7 1639 }
kevin1990 20:7031cd147ed7 1640
kevin1990 20:7031cd147ed7 1641 eRet = adi_sense_SetChannelAdcSensorDetails(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1642 pChannelConfig);
kevin1990 20:7031cd147ed7 1643 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1644 {
kevin1990 20:7031cd147ed7 1645 ADI_SENSE_LOG_ERROR("Failed to set ADC sensor details for channel %d",
kevin1990 20:7031cd147ed7 1646 eChannelId);
kevin1990 20:7031cd147ed7 1647 return eRet;
kevin1990 20:7031cd147ed7 1648 }
kevin1990 20:7031cd147ed7 1649
kevin1990 20:7031cd147ed7 1650 eRet = adi_sense_SetChannelAdcFilter(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1651 &pAdcChannelConfig->filter);
kevin1990 20:7031cd147ed7 1652 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1653 {
kevin1990 20:7031cd147ed7 1654 ADI_SENSE_LOG_ERROR("Failed to set ADC filter for channel %d",
kevin1990 20:7031cd147ed7 1655 eChannelId);
kevin1990 20:7031cd147ed7 1656 return eRet;
kevin1990 20:7031cd147ed7 1657 }
kevin1990 20:7031cd147ed7 1658
kevin1990 20:7031cd147ed7 1659 eRet = adi_sense_SetChannelAdcCurrentConfig(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1660 &pAdcChannelConfig->current);
kevin1990 20:7031cd147ed7 1661 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1662 {
kevin1990 20:7031cd147ed7 1663 ADI_SENSE_LOG_ERROR("Failed to set ADC current for channel %d",
kevin1990 20:7031cd147ed7 1664 eChannelId);
kevin1990 20:7031cd147ed7 1665 return eRet;
kevin1990 20:7031cd147ed7 1666 }
kevin1990 20:7031cd147ed7 1667
kevin1990 20:7031cd147ed7 1668 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1669 }
kevin1990 20:7031cd147ed7 1670
kevin1990 20:7031cd147ed7 1671
kevin1990 20:7031cd147ed7 1672 static ADI_SENSE_RESULT adi_sense_SetDigitalSensorCommands(
kevin1990 20:7031cd147ed7 1673 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1674 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1675 ADI_SENSE_1000_DIGITAL_SENSOR_COMMAND *pConfigCommand,
kevin1990 20:7031cd147ed7 1676 ADI_SENSE_1000_DIGITAL_SENSOR_COMMAND *pDataRequestCommand)
kevin1990 20:7031cd147ed7 1677 {
kevin1990 20:7031cd147ed7 1678 ADI_ADISENSE_CORE_Digital_Sensor_Num_Cmds_t numCmdsReg;
kevin1990 20:7031cd147ed7 1679
kevin1990 20:7031cd147ed7 1680 numCmdsReg.VALUE8 = REG_RESET_VAL(CORE_DIGITAL_SENSOR_NUM_CMDSn);
kevin1990 20:7031cd147ed7 1681
kevin1990 20:7031cd147ed7 1682 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_NUM_CMDS_DIGITAL_SENSOR_NUM_CFG_CMDS,
kevin1990 20:7031cd147ed7 1683 pConfigCommand->commandLength);
kevin1990 20:7031cd147ed7 1684 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_NUM_CMDS_DIGITAL_SENSOR_NUM_READ_CMDS,
kevin1990 20:7031cd147ed7 1685 pDataRequestCommand->commandLength);
kevin1990 20:7031cd147ed7 1686
kevin1990 20:7031cd147ed7 1687 numCmdsReg.Digital_Sensor_Num_Cfg_Cmds = pConfigCommand->commandLength;
kevin1990 20:7031cd147ed7 1688 numCmdsReg.Digital_Sensor_Num_Read_Cmds = pDataRequestCommand->commandLength;
kevin1990 20:7031cd147ed7 1689
kevin1990 20:7031cd147ed7 1690 WRITE_REG_U8(hDevice, numCmdsReg.VALUE8,
kevin1990 20:7031cd147ed7 1691 CORE_DIGITAL_SENSOR_NUM_CMDSn(eChannelId));
kevin1990 20:7031cd147ed7 1692
kevin1990 20:7031cd147ed7 1693 switch (pConfigCommand->commandLength)
kevin1990 20:7031cd147ed7 1694 {
kevin1990 20:7031cd147ed7 1695 /* NOTE - intentional fall-through cases below */
kevin1990 20:7031cd147ed7 1696 case 7:
kevin1990 20:7031cd147ed7 1697 WRITE_REG_U8(hDevice, pConfigCommand->command[6],
kevin1990 20:7031cd147ed7 1698 CORE_DIGITAL_SENSOR_COMMAND7n(eChannelId));
kevin1990 20:7031cd147ed7 1699 case 6:
kevin1990 20:7031cd147ed7 1700 WRITE_REG_U8(hDevice, pConfigCommand->command[5],
kevin1990 20:7031cd147ed7 1701 CORE_DIGITAL_SENSOR_COMMAND6n(eChannelId));
kevin1990 20:7031cd147ed7 1702 case 5:
kevin1990 20:7031cd147ed7 1703 WRITE_REG_U8(hDevice, pConfigCommand->command[4],
kevin1990 20:7031cd147ed7 1704 CORE_DIGITAL_SENSOR_COMMAND5n(eChannelId));
kevin1990 20:7031cd147ed7 1705 case 4:
kevin1990 20:7031cd147ed7 1706 WRITE_REG_U8(hDevice, pConfigCommand->command[3],
kevin1990 20:7031cd147ed7 1707 CORE_DIGITAL_SENSOR_COMMAND4n(eChannelId));
kevin1990 20:7031cd147ed7 1708 case 3:
kevin1990 20:7031cd147ed7 1709 WRITE_REG_U8(hDevice, pConfigCommand->command[2],
kevin1990 20:7031cd147ed7 1710 CORE_DIGITAL_SENSOR_COMMAND3n(eChannelId));
kevin1990 20:7031cd147ed7 1711 case 2:
kevin1990 20:7031cd147ed7 1712 WRITE_REG_U8(hDevice, pConfigCommand->command[1],
kevin1990 20:7031cd147ed7 1713 CORE_DIGITAL_SENSOR_COMMAND2n(eChannelId));
kevin1990 20:7031cd147ed7 1714 case 1:
kevin1990 20:7031cd147ed7 1715 WRITE_REG_U8(hDevice, pConfigCommand->command[0],
kevin1990 20:7031cd147ed7 1716 CORE_DIGITAL_SENSOR_COMMAND1n(eChannelId));
kevin1990 20:7031cd147ed7 1717 case 0:
kevin1990 20:7031cd147ed7 1718 default:
kevin1990 20:7031cd147ed7 1719 break;
kevin1990 20:7031cd147ed7 1720 };
kevin1990 20:7031cd147ed7 1721
kevin1990 20:7031cd147ed7 1722 switch (pDataRequestCommand->commandLength)
kevin1990 20:7031cd147ed7 1723 {
kevin1990 20:7031cd147ed7 1724 /* NOTE - intentional fall-through cases below */
kevin1990 20:7031cd147ed7 1725 case 7:
kevin1990 20:7031cd147ed7 1726 WRITE_REG_U8(hDevice, pDataRequestCommand->command[6],
kevin1990 20:7031cd147ed7 1727 CORE_DIGITAL_SENSOR_READ_CMD7n(eChannelId));
kevin1990 20:7031cd147ed7 1728 case 6:
kevin1990 20:7031cd147ed7 1729 WRITE_REG_U8(hDevice, pDataRequestCommand->command[5],
kevin1990 20:7031cd147ed7 1730 CORE_DIGITAL_SENSOR_READ_CMD6n(eChannelId));
kevin1990 20:7031cd147ed7 1731 case 5:
kevin1990 20:7031cd147ed7 1732 WRITE_REG_U8(hDevice, pDataRequestCommand->command[4],
kevin1990 20:7031cd147ed7 1733 CORE_DIGITAL_SENSOR_READ_CMD5n(eChannelId));
kevin1990 20:7031cd147ed7 1734 case 4:
kevin1990 20:7031cd147ed7 1735 WRITE_REG_U8(hDevice, pDataRequestCommand->command[3],
kevin1990 20:7031cd147ed7 1736 CORE_DIGITAL_SENSOR_READ_CMD4n(eChannelId));
kevin1990 20:7031cd147ed7 1737 case 3:
kevin1990 20:7031cd147ed7 1738 WRITE_REG_U8(hDevice, pDataRequestCommand->command[2],
kevin1990 20:7031cd147ed7 1739 CORE_DIGITAL_SENSOR_READ_CMD3n(eChannelId));
kevin1990 20:7031cd147ed7 1740 case 2:
kevin1990 20:7031cd147ed7 1741 WRITE_REG_U8(hDevice, pDataRequestCommand->command[1],
kevin1990 20:7031cd147ed7 1742 CORE_DIGITAL_SENSOR_READ_CMD2n(eChannelId));
kevin1990 20:7031cd147ed7 1743 case 1:
kevin1990 20:7031cd147ed7 1744 WRITE_REG_U8(hDevice, pDataRequestCommand->command[0],
kevin1990 20:7031cd147ed7 1745 CORE_DIGITAL_SENSOR_READ_CMD1n(eChannelId));
kevin1990 20:7031cd147ed7 1746 case 0:
kevin1990 20:7031cd147ed7 1747 default:
kevin1990 20:7031cd147ed7 1748 break;
kevin1990 20:7031cd147ed7 1749 };
kevin1990 20:7031cd147ed7 1750
kevin1990 20:7031cd147ed7 1751 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1752 }
kevin1990 20:7031cd147ed7 1753
kevin1990 20:7031cd147ed7 1754 static ADI_SENSE_RESULT adi_sense_SetDigitalSensorFormat(
kevin1990 20:7031cd147ed7 1755 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1756 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1757 ADI_SENSE_1000_DIGITAL_SENSOR_DATA_FORMAT *pDataFormat)
kevin1990 20:7031cd147ed7 1758 {
kevin1990 20:7031cd147ed7 1759 ADI_ADISENSE_CORE_Digital_Sensor_Config_t sensorConfigReg;
kevin1990 20:7031cd147ed7 1760
kevin1990 20:7031cd147ed7 1761 sensorConfigReg.VALUE16 = REG_RESET_VAL(CORE_DIGITAL_SENSOR_CONFIGn);
kevin1990 20:7031cd147ed7 1762
kevin1990 20:7031cd147ed7 1763 if (pDataFormat->coding != ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_NONE)
kevin1990 20:7031cd147ed7 1764 {
kevin1990 20:7031cd147ed7 1765 if (pDataFormat->frameLength == 0)
kevin1990 20:7031cd147ed7 1766 {
kevin1990 20:7031cd147ed7 1767 ADI_SENSE_LOG_ERROR("Invalid frame length specified for digital sensor data format");
kevin1990 20:7031cd147ed7 1768 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1769 }
kevin1990 20:7031cd147ed7 1770 if (pDataFormat->numDataBits == 0)
kevin1990 20:7031cd147ed7 1771 {
kevin1990 20:7031cd147ed7 1772 ADI_SENSE_LOG_ERROR("Invalid frame length specified for digital sensor data format");
kevin1990 20:7031cd147ed7 1773 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1774 }
kevin1990 20:7031cd147ed7 1775
kevin1990 20:7031cd147ed7 1776 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_CONFIG_DIGITAL_SENSOR_READ_BYTES,
kevin1990 20:7031cd147ed7 1777 pDataFormat->frameLength - 1);
kevin1990 20:7031cd147ed7 1778 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_CONFIG_DIGITAL_SENSOR_DATA_BITS,
kevin1990 20:7031cd147ed7 1779 pDataFormat->numDataBits - 1);
kevin1990 20:7031cd147ed7 1780 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_CONFIG_DIGITAL_SENSOR_BIT_OFFSET,
kevin1990 20:7031cd147ed7 1781 pDataFormat->bitOffset);
kevin1990 20:7031cd147ed7 1782
kevin1990 20:7031cd147ed7 1783 sensorConfigReg.Digital_Sensor_Read_Bytes = pDataFormat->frameLength - 1;
kevin1990 20:7031cd147ed7 1784 sensorConfigReg.Digital_Sensor_Data_Bits = pDataFormat->numDataBits - 1;
kevin1990 20:7031cd147ed7 1785 sensorConfigReg.Digital_Sensor_Bit_Offset = pDataFormat->bitOffset;
kevin1990 20:7031cd147ed7 1786 sensorConfigReg.Digital_Sensor_LeftAligned = pDataFormat->leftJustified ? 1 : 0;
kevin1990 20:7031cd147ed7 1787 sensorConfigReg.Digital_Sensor_LittleEndian = pDataFormat->littleEndian ? 1 : 0;
kevin1990 20:7031cd147ed7 1788
kevin1990 20:7031cd147ed7 1789 switch (pDataFormat->coding)
kevin1990 20:7031cd147ed7 1790 {
kevin1990 20:7031cd147ed7 1791 case ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_UNIPOLAR:
kevin1990 20:7031cd147ed7 1792 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_UNIPOLAR;
kevin1990 20:7031cd147ed7 1793 break;
kevin1990 20:7031cd147ed7 1794 case ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_TWOS_COMPLEMENT:
kevin1990 20:7031cd147ed7 1795 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_TWOS_COMPL;
kevin1990 20:7031cd147ed7 1796 break;
kevin1990 20:7031cd147ed7 1797 case ADI_SENSE_1000_DIGITAL_SENSOR_DATA_CODING_OFFSET_BINARY:
kevin1990 20:7031cd147ed7 1798 sensorConfigReg.Digital_Sensor_Coding = ADISENSE_CORE_DIGITAL_SENSOR_CONFIG_CODING_OFFSET_BINARY;
kevin1990 20:7031cd147ed7 1799 break;
kevin1990 20:7031cd147ed7 1800 default:
kevin1990 20:7031cd147ed7 1801 ADI_SENSE_LOG_ERROR("Invalid coding specified for digital sensor data format");
kevin1990 20:7031cd147ed7 1802 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1803 }
kevin1990 20:7031cd147ed7 1804 }
kevin1990 20:7031cd147ed7 1805
kevin1990 20:7031cd147ed7 1806 WRITE_REG_U16(hDevice, sensorConfigReg.VALUE16,
kevin1990 20:7031cd147ed7 1807 CORE_DIGITAL_SENSOR_CONFIGn(eChannelId));
kevin1990 20:7031cd147ed7 1808
kevin1990 20:7031cd147ed7 1809
kevin1990 20:7031cd147ed7 1810 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1811 }
kevin1990 20:7031cd147ed7 1812
kevin1990 20:7031cd147ed7 1813 static ADI_SENSE_RESULT adi_sense_SetChannelI2cSensorType(
kevin1990 20:7031cd147ed7 1814 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1815 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1816 ADI_SENSE_1000_I2C_SENSOR_TYPE sensorType)
kevin1990 20:7031cd147ed7 1817 {
kevin1990 20:7031cd147ed7 1818 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
kevin1990 20:7031cd147ed7 1819
kevin1990 20:7031cd147ed7 1820 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
kevin1990 20:7031cd147ed7 1821
kevin1990 20:7031cd147ed7 1822 /* Ensure that the sensor type is valid for this channel */
kevin1990 20:7031cd147ed7 1823 switch(sensorType)
kevin1990 20:7031cd147ed7 1824 {
kevin1990 20:7031cd147ed7 1825 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_HONEYWELL_HUMIDICON:
kevin1990 20:7031cd147ed7 1826 sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_I2C_HUMIDITY_HONEYWELL_HUMIDICON;
kevin1990 20:7031cd147ed7 1827 break;
kevin1990 20:7031cd147ed7 1828 case ADI_SENSE_1000_I2C_SENSOR_HUMIDITY_SENSIRION_SHT3X:
kevin1990 20:7031cd147ed7 1829 sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_I2C_HUMIDITY_SENSIRION_SHT3X;
kevin1990 20:7031cd147ed7 1830 break;
kevin1990 20:7031cd147ed7 1831 default:
kevin1990 20:7031cd147ed7 1832 ADI_SENSE_LOG_ERROR("Unsupported I2C sensor type %d specified", sensorType);
kevin1990 20:7031cd147ed7 1833 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1834 }
kevin1990 20:7031cd147ed7 1835
kevin1990 20:7031cd147ed7 1836 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
kevin1990 20:7031cd147ed7 1837
kevin1990 20:7031cd147ed7 1838 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1839 }
kevin1990 20:7031cd147ed7 1840
kevin1990 20:7031cd147ed7 1841 static ADI_SENSE_RESULT adi_sense_SetChannelI2cSensorAddress(
kevin1990 20:7031cd147ed7 1842 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1843 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1844 uint32_t deviceAddress)
kevin1990 20:7031cd147ed7 1845 {
kevin1990 20:7031cd147ed7 1846 CHECK_REG_FIELD_VAL(CORE_DIGITAL_SENSOR_ADDRESS_DIGITAL_SENSOR_ADDRESS, deviceAddress);
kevin1990 20:7031cd147ed7 1847 WRITE_REG_U8(hDevice, deviceAddress, CORE_DIGITAL_SENSOR_ADDRESSn(eChannelId));
kevin1990 20:7031cd147ed7 1848
kevin1990 20:7031cd147ed7 1849 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1850 }
kevin1990 20:7031cd147ed7 1851
kevin1990 20:7031cd147ed7 1852 ADI_SENSE_RESULT adi_sense_SetI2cChannelConfig(
kevin1990 20:7031cd147ed7 1853 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1854 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1855 ADI_SENSE_1000_I2C_CHANNEL_CONFIG *pI2cChannelConfig)
kevin1990 20:7031cd147ed7 1856 {
kevin1990 20:7031cd147ed7 1857 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 1858
kevin1990 20:7031cd147ed7 1859 eRet = adi_sense_SetChannelI2cSensorType(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1860 pI2cChannelConfig->sensor);
kevin1990 20:7031cd147ed7 1861 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1862 {
kevin1990 20:7031cd147ed7 1863 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor type for channel %d",
kevin1990 20:7031cd147ed7 1864 eChannelId);
kevin1990 20:7031cd147ed7 1865 return eRet;
kevin1990 20:7031cd147ed7 1866 }
kevin1990 20:7031cd147ed7 1867
kevin1990 20:7031cd147ed7 1868 eRet = adi_sense_SetChannelI2cSensorAddress(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1869 pI2cChannelConfig->deviceAddress);
kevin1990 20:7031cd147ed7 1870 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1871 {
kevin1990 20:7031cd147ed7 1872 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor address for channel %d",
kevin1990 20:7031cd147ed7 1873 eChannelId);
kevin1990 20:7031cd147ed7 1874 return eRet;
kevin1990 20:7031cd147ed7 1875 }
kevin1990 20:7031cd147ed7 1876
kevin1990 20:7031cd147ed7 1877 eRet = adi_sense_SetDigitalSensorCommands(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1878 &pI2cChannelConfig->configurationCommand,
kevin1990 20:7031cd147ed7 1879 &pI2cChannelConfig->dataRequestCommand);
kevin1990 20:7031cd147ed7 1880 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1881 {
kevin1990 20:7031cd147ed7 1882 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor commands for channel %d",
kevin1990 20:7031cd147ed7 1883 eChannelId);
kevin1990 20:7031cd147ed7 1884 return eRet;
kevin1990 20:7031cd147ed7 1885 }
kevin1990 20:7031cd147ed7 1886
kevin1990 20:7031cd147ed7 1887 eRet = adi_sense_SetDigitalSensorFormat(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1888 &pI2cChannelConfig->dataFormat);
kevin1990 20:7031cd147ed7 1889 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1890 {
kevin1990 20:7031cd147ed7 1891 ADI_SENSE_LOG_ERROR("Failed to set I2C sensor data format for channel %d",
kevin1990 20:7031cd147ed7 1892 eChannelId);
kevin1990 20:7031cd147ed7 1893 return eRet;
kevin1990 20:7031cd147ed7 1894 }
kevin1990 20:7031cd147ed7 1895
kevin1990 20:7031cd147ed7 1896 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1897 }
kevin1990 20:7031cd147ed7 1898
kevin1990 20:7031cd147ed7 1899 static ADI_SENSE_RESULT adi_sense_SetChannelSpiSensorType(
kevin1990 20:7031cd147ed7 1900 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1901 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1902 ADI_SENSE_1000_SPI_SENSOR_TYPE sensorType)
kevin1990 20:7031cd147ed7 1903 {
kevin1990 20:7031cd147ed7 1904 ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg;
kevin1990 20:7031cd147ed7 1905
kevin1990 20:7031cd147ed7 1906 sensorTypeReg.VALUE16 = REG_RESET_VAL(CORE_SENSOR_TYPEn);
kevin1990 20:7031cd147ed7 1907
kevin1990 20:7031cd147ed7 1908 /* Ensure that the sensor type is valid for this channel */
kevin1990 20:7031cd147ed7 1909 switch(sensorType)
kevin1990 20:7031cd147ed7 1910 {
kevin1990 20:7031cd147ed7 1911 case ADI_SENSE_1000_SPI_SENSOR_PRESSURE_HONEYWELL_TRUSTABILITY:
kevin1990 20:7031cd147ed7 1912 sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_PRESSURE_HONEYWELL_TRUSTABILITY;
kevin1990 20:7031cd147ed7 1913 break;
kevin1990 20:7031cd147ed7 1914 case ADI_SENSE_1000_SPI_SENSOR_ACCELEROMETER_ADI_ADXL362:
kevin1990 20:7031cd147ed7 1915 sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_ACCELEROMETER_1;
kevin1990 20:7031cd147ed7 1916 break;
kevin1990 20:7031cd147ed7 1917 default:
kevin1990 20:7031cd147ed7 1918 ADI_SENSE_LOG_ERROR("Unsupported SPI sensor type %d specified", sensorType);
kevin1990 20:7031cd147ed7 1919 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 1920 }
kevin1990 20:7031cd147ed7 1921
kevin1990 20:7031cd147ed7 1922 WRITE_REG_U16(hDevice, sensorTypeReg.VALUE16, CORE_SENSOR_TYPEn(eChannelId));
kevin1990 20:7031cd147ed7 1923
kevin1990 20:7031cd147ed7 1924 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1925 }
kevin1990 20:7031cd147ed7 1926
kevin1990 20:7031cd147ed7 1927 ADI_SENSE_RESULT adi_sense_SetSpiChannelConfig(
kevin1990 20:7031cd147ed7 1928 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1929 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1930 ADI_SENSE_1000_SPI_CHANNEL_CONFIG *pSpiChannelConfig)
kevin1990 20:7031cd147ed7 1931 {
kevin1990 20:7031cd147ed7 1932 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 1933
kevin1990 20:7031cd147ed7 1934 eRet = adi_sense_SetChannelSpiSensorType(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1935 pSpiChannelConfig->sensor);
kevin1990 20:7031cd147ed7 1936 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1937 {
kevin1990 20:7031cd147ed7 1938 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor type for channel %d",
kevin1990 20:7031cd147ed7 1939 eChannelId);
kevin1990 20:7031cd147ed7 1940 return eRet;
kevin1990 20:7031cd147ed7 1941 }
kevin1990 20:7031cd147ed7 1942
kevin1990 20:7031cd147ed7 1943 eRet = adi_sense_SetDigitalSensorCommands(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1944 &pSpiChannelConfig->configurationCommand,
kevin1990 20:7031cd147ed7 1945 &pSpiChannelConfig->dataRequestCommand);
kevin1990 20:7031cd147ed7 1946 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1947 {
kevin1990 20:7031cd147ed7 1948 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor commands for channel %d",
kevin1990 20:7031cd147ed7 1949 eChannelId);
kevin1990 20:7031cd147ed7 1950 return eRet;
kevin1990 20:7031cd147ed7 1951 }
kevin1990 20:7031cd147ed7 1952
kevin1990 20:7031cd147ed7 1953 eRet = adi_sense_SetDigitalSensorFormat(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 1954 &pSpiChannelConfig->dataFormat);
kevin1990 20:7031cd147ed7 1955 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 1956 {
kevin1990 20:7031cd147ed7 1957 ADI_SENSE_LOG_ERROR("Failed to set SPI sensor data format for channel %d",
kevin1990 20:7031cd147ed7 1958 eChannelId);
kevin1990 20:7031cd147ed7 1959 return eRet;
kevin1990 20:7031cd147ed7 1960 }
kevin1990 20:7031cd147ed7 1961
kevin1990 20:7031cd147ed7 1962 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1963 }
kevin1990 20:7031cd147ed7 1964
kevin1990 20:7031cd147ed7 1965 ADI_SENSE_RESULT adi_sense_1000_SetChannelThresholdLimits(
kevin1990 20:7031cd147ed7 1966 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1967 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1968 float32_t fHighThresholdLimit,
kevin1990 20:7031cd147ed7 1969 float32_t fLowThresholdLimit)
kevin1990 20:7031cd147ed7 1970 {
kevin1990 20:7031cd147ed7 1971 /*
kevin1990 20:7031cd147ed7 1972 * If the low/high limits are *both* set to 0 in memory, or NaNs, assume
kevin1990 20:7031cd147ed7 1973 * that they are unset, or not required, and use infinity defaults instead
kevin1990 20:7031cd147ed7 1974 */
kevin1990 20:7031cd147ed7 1975 if (fHighThresholdLimit == 0.0 && fLowThresholdLimit == 0.0)
kevin1990 20:7031cd147ed7 1976 {
kevin1990 20:7031cd147ed7 1977 fHighThresholdLimit = INFINITY;
kevin1990 20:7031cd147ed7 1978 fLowThresholdLimit = -INFINITY;
kevin1990 20:7031cd147ed7 1979 }
kevin1990 20:7031cd147ed7 1980 else
kevin1990 20:7031cd147ed7 1981 {
kevin1990 20:7031cd147ed7 1982 if (isnanf(fHighThresholdLimit))
kevin1990 20:7031cd147ed7 1983 fHighThresholdLimit = INFINITY;
kevin1990 20:7031cd147ed7 1984 if (isnanf(fLowThresholdLimit))
kevin1990 20:7031cd147ed7 1985 fLowThresholdLimit = -INFINITY;
kevin1990 20:7031cd147ed7 1986 }
kevin1990 20:7031cd147ed7 1987
kevin1990 20:7031cd147ed7 1988 WRITE_REG_FLOAT(hDevice, fHighThresholdLimit,
kevin1990 20:7031cd147ed7 1989 CORE_HIGH_THRESHOLD_LIMITn(eChannelId));
kevin1990 20:7031cd147ed7 1990 WRITE_REG_FLOAT(hDevice, fLowThresholdLimit,
kevin1990 20:7031cd147ed7 1991 CORE_LOW_THRESHOLD_LIMITn(eChannelId));
kevin1990 20:7031cd147ed7 1992
kevin1990 20:7031cd147ed7 1993 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 1994 }
kevin1990 20:7031cd147ed7 1995
kevin1990 20:7031cd147ed7 1996 ADI_SENSE_RESULT adi_sense_1000_SetOffsetGain(
kevin1990 20:7031cd147ed7 1997 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 1998 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 1999 float32_t fOffsetAdjustment,
kevin1990 20:7031cd147ed7 2000 float32_t fGainAdjustment)
kevin1990 20:7031cd147ed7 2001 {
kevin1990 20:7031cd147ed7 2002 /* Replace with default values if NaNs are specified (or 0.0 for gain) */
kevin1990 20:7031cd147ed7 2003 if (isnanf(fGainAdjustment) || (fGainAdjustment == 0.0))
kevin1990 20:7031cd147ed7 2004 fGainAdjustment = 1.0;
kevin1990 20:7031cd147ed7 2005 if (isnanf(fOffsetAdjustment))
kevin1990 20:7031cd147ed7 2006 fOffsetAdjustment = 0.0;
kevin1990 20:7031cd147ed7 2007
kevin1990 20:7031cd147ed7 2008 WRITE_REG_FLOAT(hDevice, fGainAdjustment, CORE_SENSOR_GAINn(eChannelId));
kevin1990 20:7031cd147ed7 2009 WRITE_REG_FLOAT(hDevice, fOffsetAdjustment, CORE_SENSOR_OFFSETn(eChannelId));
kevin1990 20:7031cd147ed7 2010
kevin1990 20:7031cd147ed7 2011 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2012 }
kevin1990 20:7031cd147ed7 2013
kevin1990 20:7031cd147ed7 2014 ADI_SENSE_RESULT adi_sense_1000_SetChannelSettlingTime(
kevin1990 20:7031cd147ed7 2015 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 2016 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 2017 uint32_t nSettlingTime)
kevin1990 20:7031cd147ed7 2018 {
kevin1990 20:7031cd147ed7 2019 CHECK_REG_FIELD_VAL(CORE_SETTLING_TIME_SETTLING_TIME, nSettlingTime);
kevin1990 20:7031cd147ed7 2020
kevin1990 20:7031cd147ed7 2021 WRITE_REG_U16(hDevice, nSettlingTime, CORE_SETTLING_TIMEn(eChannelId));
kevin1990 20:7031cd147ed7 2022
kevin1990 20:7031cd147ed7 2023 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2024 }
kevin1990 20:7031cd147ed7 2025
kevin1990 20:7031cd147ed7 2026 ADI_SENSE_RESULT adi_sense_1000_SetChannelConfig(
kevin1990 20:7031cd147ed7 2027 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 2028 ADI_SENSE_1000_CHANNEL_ID eChannelId,
kevin1990 20:7031cd147ed7 2029 ADI_SENSE_1000_CHANNEL_CONFIG *pChannelConfig)
kevin1990 20:7031cd147ed7 2030 {
kevin1990 20:7031cd147ed7 2031 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 2032
kevin1990 20:7031cd147ed7 2033 if (! ADI_SENSE_1000_CHANNEL_IS_VIRTUAL(eChannelId))
kevin1990 20:7031cd147ed7 2034 {
kevin1990 20:7031cd147ed7 2035 /* If the channel is not enabled, disable it and return */
kevin1990 20:7031cd147ed7 2036 if (! pChannelConfig->enableChannel)
kevin1990 20:7031cd147ed7 2037 return adi_sense_1000_SetChannelCount(hDevice, eChannelId, 0);
kevin1990 20:7031cd147ed7 2038
kevin1990 20:7031cd147ed7 2039 eRet = adi_sense_1000_SetChannelCount(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 2040 pChannelConfig->measurementsPerCycle);
kevin1990 20:7031cd147ed7 2041 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 2042 {
kevin1990 20:7031cd147ed7 2043 ADI_SENSE_LOG_ERROR("Failed to set measurement count for channel %d",
kevin1990 20:7031cd147ed7 2044 eChannelId);
kevin1990 20:7031cd147ed7 2045 return eRet;
kevin1990 20:7031cd147ed7 2046 }
kevin1990 20:7031cd147ed7 2047
kevin1990 20:7031cd147ed7 2048 switch (eChannelId)
kevin1990 20:7031cd147ed7 2049 {
kevin1990 20:7031cd147ed7 2050 case ADI_SENSE_1000_CHANNEL_ID_CJC_0:
kevin1990 20:7031cd147ed7 2051 case ADI_SENSE_1000_CHANNEL_ID_CJC_1:
kevin1990 20:7031cd147ed7 2052 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_0:
kevin1990 20:7031cd147ed7 2053 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_1:
kevin1990 20:7031cd147ed7 2054 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_2:
kevin1990 20:7031cd147ed7 2055 case ADI_SENSE_1000_CHANNEL_ID_SENSOR_3:
kevin1990 20:7031cd147ed7 2056 case ADI_SENSE_1000_CHANNEL_ID_VOLTAGE_0:
kevin1990 20:7031cd147ed7 2057 case ADI_SENSE_1000_CHANNEL_ID_CURRENT_0:
kevin1990 20:7031cd147ed7 2058 eRet = adi_sense_SetAdcChannelConfig(hDevice, eChannelId, pChannelConfig);
kevin1990 20:7031cd147ed7 2059 break;
kevin1990 20:7031cd147ed7 2060 case ADI_SENSE_1000_CHANNEL_ID_I2C_0:
kevin1990 20:7031cd147ed7 2061 case ADI_SENSE_1000_CHANNEL_ID_I2C_1:
kevin1990 20:7031cd147ed7 2062 eRet = adi_sense_SetI2cChannelConfig(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 2063 &pChannelConfig->i2cChannelConfig);
kevin1990 20:7031cd147ed7 2064 break;
kevin1990 20:7031cd147ed7 2065 case ADI_SENSE_1000_CHANNEL_ID_SPI_0:
kevin1990 20:7031cd147ed7 2066 eRet = adi_sense_SetSpiChannelConfig(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 2067 &pChannelConfig->spiChannelConfig);
kevin1990 20:7031cd147ed7 2068 break;
kevin1990 20:7031cd147ed7 2069 default:
kevin1990 20:7031cd147ed7 2070 ADI_SENSE_LOG_ERROR("Invalid channel ID %d specified", eChannelId);
kevin1990 20:7031cd147ed7 2071 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2072 }
kevin1990 20:7031cd147ed7 2073
kevin1990 20:7031cd147ed7 2074 eRet = adi_sense_1000_SetChannelSettlingTime(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 2075 pChannelConfig->extraSettlingTime);
kevin1990 20:7031cd147ed7 2076 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 2077 {
kevin1990 20:7031cd147ed7 2078 ADI_SENSE_LOG_ERROR("Failed to set settling time for channel %d",
kevin1990 20:7031cd147ed7 2079 eChannelId);
kevin1990 20:7031cd147ed7 2080 return eRet;
kevin1990 20:7031cd147ed7 2081 }
kevin1990 20:7031cd147ed7 2082 }
kevin1990 20:7031cd147ed7 2083
kevin1990 20:7031cd147ed7 2084 if (pChannelConfig->enableChannel)
kevin1990 20:7031cd147ed7 2085 {
kevin1990 20:7031cd147ed7 2086 /* Threshold limits can be configured individually for virtual channels */
kevin1990 20:7031cd147ed7 2087 eRet = adi_sense_1000_SetChannelThresholdLimits(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 2088 pChannelConfig->highThreshold,
kevin1990 20:7031cd147ed7 2089 pChannelConfig->lowThreshold);
kevin1990 20:7031cd147ed7 2090 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 2091 {
kevin1990 20:7031cd147ed7 2092 ADI_SENSE_LOG_ERROR("Failed to set threshold limits for channel %d",
kevin1990 20:7031cd147ed7 2093 eChannelId);
kevin1990 20:7031cd147ed7 2094 return eRet;
kevin1990 20:7031cd147ed7 2095 }
kevin1990 20:7031cd147ed7 2096
kevin1990 20:7031cd147ed7 2097 /* Offset and gain can be configured individually for virtual channels */
kevin1990 20:7031cd147ed7 2098 eRet = adi_sense_1000_SetOffsetGain(hDevice, eChannelId,
kevin1990 20:7031cd147ed7 2099 pChannelConfig->offsetAdjustment,
kevin1990 20:7031cd147ed7 2100 pChannelConfig->gainAdjustment);
kevin1990 20:7031cd147ed7 2101 if (eRet != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 2102 {
kevin1990 20:7031cd147ed7 2103 ADI_SENSE_LOG_ERROR("Failed to set offset/gain for channel %d",
kevin1990 20:7031cd147ed7 2104 eChannelId);
kevin1990 20:7031cd147ed7 2105 return eRet;
kevin1990 20:7031cd147ed7 2106 }
kevin1990 20:7031cd147ed7 2107 }
kevin1990 20:7031cd147ed7 2108
kevin1990 20:7031cd147ed7 2109 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2110 }
kevin1990 20:7031cd147ed7 2111
kevin1990 20:7031cd147ed7 2112 ADI_SENSE_RESULT adi_sense_SetConfig(
kevin1990 20:7031cd147ed7 2113 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 2114 ADI_SENSE_CONFIG * const pConfig)
kevin1990 20:7031cd147ed7 2115 {
kevin1990 20:7031cd147ed7 2116 ADI_SENSE_1000_CONFIG *pDeviceConfig;
kevin1990 20:7031cd147ed7 2117 ADI_SENSE_PRODUCT_ID productId;
kevin1990 20:7031cd147ed7 2118 ADI_SENSE_RESULT eRet;
kevin1990 20:7031cd147ed7 2119
kevin1990 20:7031cd147ed7 2120 if (pConfig->productId != ADI_SENSE_PRODUCT_ID_1000)
kevin1990 20:7031cd147ed7 2121 {
kevin1990 20:7031cd147ed7 2122 ADI_SENSE_LOG_ERROR("Configuration Product ID (0x%X) is not supported (0x%0X)",
kevin1990 20:7031cd147ed7 2123 pConfig->productId, ADI_SENSE_PRODUCT_ID_1000);
kevin1990 20:7031cd147ed7 2124 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2125 }
kevin1990 20:7031cd147ed7 2126
kevin1990 20:7031cd147ed7 2127 /* Check that the actual Product ID is a match? */
kevin1990 20:7031cd147ed7 2128 eRet = adi_sense_GetProductID(hDevice, &productId);
kevin1990 20:7031cd147ed7 2129 if (eRet)
kevin1990 20:7031cd147ed7 2130 {
kevin1990 20:7031cd147ed7 2131 ADI_SENSE_LOG_ERROR("Failed to read device Product ID register");
kevin1990 20:7031cd147ed7 2132 return eRet;
kevin1990 20:7031cd147ed7 2133 }
kevin1990 20:7031cd147ed7 2134 if (pConfig->productId != productId)
kevin1990 20:7031cd147ed7 2135 {
kevin1990 20:7031cd147ed7 2136 ADI_SENSE_LOG_ERROR("Configuration Product ID (0x%X) does not match device (0x%0X)",
kevin1990 20:7031cd147ed7 2137 pConfig->productId, productId);
kevin1990 20:7031cd147ed7 2138 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2139 }
kevin1990 20:7031cd147ed7 2140
kevin1990 20:7031cd147ed7 2141 pDeviceConfig = &pConfig->adisense1000;
kevin1990 20:7031cd147ed7 2142
kevin1990 20:7031cd147ed7 2143 eRet = adi_sense_1000_SetPowerConfig(hDevice, &pDeviceConfig->power);
kevin1990 20:7031cd147ed7 2144 if (eRet)
kevin1990 20:7031cd147ed7 2145 {
kevin1990 20:7031cd147ed7 2146 ADI_SENSE_LOG_ERROR("Failed to set power configuration");
kevin1990 20:7031cd147ed7 2147 return eRet;
kevin1990 20:7031cd147ed7 2148 }
kevin1990 20:7031cd147ed7 2149
kevin1990 20:7031cd147ed7 2150 eRet = adi_sense_1000_SetMeasurementConfig(hDevice, &pDeviceConfig->measurement);
kevin1990 20:7031cd147ed7 2151 if (eRet)
kevin1990 20:7031cd147ed7 2152 {
kevin1990 20:7031cd147ed7 2153 ADI_SENSE_LOG_ERROR("Failed to set measurement configuration");
kevin1990 20:7031cd147ed7 2154 return eRet;
kevin1990 20:7031cd147ed7 2155 }
kevin1990 20:7031cd147ed7 2156
kevin1990 20:7031cd147ed7 2157 eRet = adi_sense_1000_SetDiagnosticsConfig(hDevice, &pDeviceConfig->diagnostics);
kevin1990 20:7031cd147ed7 2158 if (eRet)
kevin1990 20:7031cd147ed7 2159 {
kevin1990 20:7031cd147ed7 2160 ADI_SENSE_LOG_ERROR("Failed to set diagnostics configuration");
kevin1990 20:7031cd147ed7 2161 return eRet;
kevin1990 20:7031cd147ed7 2162 }
kevin1990 20:7031cd147ed7 2163
kevin1990 20:7031cd147ed7 2164 for (ADI_SENSE_1000_CHANNEL_ID id = 0; id < ADI_SENSE_1000_MAX_CHANNELS; id++)
kevin1990 20:7031cd147ed7 2165 {
kevin1990 20:7031cd147ed7 2166 eRet = adi_sense_1000_SetChannelConfig(hDevice, id,
kevin1990 20:7031cd147ed7 2167 &pDeviceConfig->channels[id]);
kevin1990 20:7031cd147ed7 2168 if (eRet)
kevin1990 20:7031cd147ed7 2169 {
kevin1990 20:7031cd147ed7 2170 ADI_SENSE_LOG_ERROR("Failed to set channel %d configuration", id);
kevin1990 20:7031cd147ed7 2171 return eRet;
kevin1990 20:7031cd147ed7 2172 }
kevin1990 20:7031cd147ed7 2173 }
kevin1990 20:7031cd147ed7 2174
kevin1990 20:7031cd147ed7 2175 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2176 }
kevin1990 20:7031cd147ed7 2177
kevin1990 20:7031cd147ed7 2178 ADI_SENSE_RESULT adi_sense_1000_SetLutData(
kevin1990 20:7031cd147ed7 2179 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 2180 ADI_SENSE_1000_LUT * const pLutData)
kevin1990 20:7031cd147ed7 2181 {
kevin1990 20:7031cd147ed7 2182 ADI_SENSE_1000_LUT_HEADER *pLutHeader = &pLutData->header;
kevin1990 20:7031cd147ed7 2183 ADI_SENSE_1000_LUT_TABLE *pLutTable = pLutData->tables;
kevin1990 20:7031cd147ed7 2184 unsigned actualLength = 0;
kevin1990 20:7031cd147ed7 2185
kevin1990 20:7031cd147ed7 2186 if (pLutData->header.signature != ADI_SENSE_LUT_SIGNATURE)
kevin1990 20:7031cd147ed7 2187 {
kevin1990 20:7031cd147ed7 2188 ADI_SENSE_LOG_ERROR("LUT signature incorrect (expected 0x%X, actual 0x%X)",
kevin1990 20:7031cd147ed7 2189 ADI_SENSE_LUT_SIGNATURE, pLutHeader->signature);
kevin1990 20:7031cd147ed7 2190 return ADI_SENSE_INVALID_SIGNATURE;
kevin1990 20:7031cd147ed7 2191 }
kevin1990 20:7031cd147ed7 2192
kevin1990 20:7031cd147ed7 2193 for (unsigned i = 0; i < pLutHeader->numTables; i++)
kevin1990 20:7031cd147ed7 2194 {
kevin1990 20:7031cd147ed7 2195 ADI_SENSE_1000_LUT_DESCRIPTOR *pDesc = &pLutTable->descriptor;
kevin1990 20:7031cd147ed7 2196 ADI_SENSE_1000_LUT_TABLE_DATA *pData = &pLutTable->data;
kevin1990 20:7031cd147ed7 2197 unsigned short calculatedCrc;
kevin1990 20:7031cd147ed7 2198
kevin1990 20:7031cd147ed7 2199 switch (pDesc->geometry)
kevin1990 20:7031cd147ed7 2200 {
kevin1990 20:7031cd147ed7 2201 case ADI_SENSE_1000_LUT_GEOMETRY_COEFFS:
kevin1990 20:7031cd147ed7 2202 switch (pDesc->equation)
kevin1990 20:7031cd147ed7 2203 {
kevin1990 20:7031cd147ed7 2204 case ADI_SENSE_1000_LUT_EQUATION_POLYN:
kevin1990 20:7031cd147ed7 2205 case ADI_SENSE_1000_LUT_EQUATION_POLYNEXP:
kevin1990 20:7031cd147ed7 2206 case ADI_SENSE_1000_LUT_EQUATION_QUADRATIC:
kevin1990 20:7031cd147ed7 2207 case ADI_SENSE_1000_LUT_EQUATION_STEINHART:
kevin1990 20:7031cd147ed7 2208 case ADI_SENSE_1000_LUT_EQUATION_LOGARITHMIC:
kevin1990 20:7031cd147ed7 2209 case ADI_SENSE_1000_LUT_EQUATION_EXPONENTIAL:
kevin1990 20:7031cd147ed7 2210 case ADI_SENSE_1000_LUT_EQUATION_BIVARIATE_POLYN:
kevin1990 20:7031cd147ed7 2211 break;
kevin1990 20:7031cd147ed7 2212 default:
kevin1990 20:7031cd147ed7 2213 ADI_SENSE_LOG_ERROR("Invalid equation %u specified for LUT table %u",
kevin1990 20:7031cd147ed7 2214 pDesc->equation, i);
kevin1990 20:7031cd147ed7 2215 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2216 }
kevin1990 20:7031cd147ed7 2217 break;
kevin1990 20:7031cd147ed7 2218 case ADI_SENSE_1000_LUT_GEOMETRY_NES_1D:
kevin1990 20:7031cd147ed7 2219 case ADI_SENSE_1000_LUT_GEOMETRY_NES_2D:
kevin1990 20:7031cd147ed7 2220 case ADI_SENSE_1000_LUT_GEOMETRY_ES_1D:
kevin1990 20:7031cd147ed7 2221 case ADI_SENSE_1000_LUT_GEOMETRY_ES_2D:
kevin1990 20:7031cd147ed7 2222 if (pDesc->equation != ADI_SENSE_1000_LUT_EQUATION_LUT) {
kevin1990 20:7031cd147ed7 2223 ADI_SENSE_LOG_ERROR("Invalid equation %u specified for LUT table %u",
kevin1990 20:7031cd147ed7 2224 pDesc->equation, i);
kevin1990 20:7031cd147ed7 2225 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2226 }
kevin1990 20:7031cd147ed7 2227 break;
kevin1990 20:7031cd147ed7 2228 default:
kevin1990 20:7031cd147ed7 2229 ADI_SENSE_LOG_ERROR("Invalid geometry %u specified for LUT table %u",
kevin1990 20:7031cd147ed7 2230 pDesc->geometry, i);
kevin1990 20:7031cd147ed7 2231 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2232 }
kevin1990 20:7031cd147ed7 2233
kevin1990 20:7031cd147ed7 2234 switch (pDesc->dataType)
kevin1990 20:7031cd147ed7 2235 {
kevin1990 20:7031cd147ed7 2236 case ADI_SENSE_1000_LUT_DATA_TYPE_FLOAT32:
kevin1990 20:7031cd147ed7 2237 case ADI_SENSE_1000_LUT_DATA_TYPE_FLOAT64:
kevin1990 20:7031cd147ed7 2238 break;
kevin1990 20:7031cd147ed7 2239 default:
kevin1990 20:7031cd147ed7 2240 ADI_SENSE_LOG_ERROR("Invalid vector format %u specified for LUT table %u",
kevin1990 20:7031cd147ed7 2241 pDesc->dataType, i);
kevin1990 20:7031cd147ed7 2242 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2243 }
kevin1990 20:7031cd147ed7 2244
kevin1990 20:7031cd147ed7 2245 calculatedCrc = crc16_ccitt(pData, pDesc->length);
kevin1990 20:7031cd147ed7 2246 if (calculatedCrc != pDesc->crc16)
kevin1990 20:7031cd147ed7 2247 {
kevin1990 20:7031cd147ed7 2248 ADI_SENSE_LOG_ERROR("CRC validation failed on LUT table %u (expected 0x%04X, actual 0x%04X)",
kevin1990 20:7031cd147ed7 2249 i, pDesc->crc16, calculatedCrc);
kevin1990 20:7031cd147ed7 2250 return ADI_SENSE_CRC_ERROR;
kevin1990 20:7031cd147ed7 2251 }
kevin1990 20:7031cd147ed7 2252
kevin1990 20:7031cd147ed7 2253 actualLength += sizeof(*pDesc) + pDesc->length;
kevin1990 20:7031cd147ed7 2254
kevin1990 20:7031cd147ed7 2255 /* Move to the next look-up table */
kevin1990 20:7031cd147ed7 2256 pLutTable = (ADI_SENSE_1000_LUT_TABLE *)((uint8_t *)pLutTable + sizeof(*pDesc) + pDesc->length);
kevin1990 20:7031cd147ed7 2257 }
kevin1990 20:7031cd147ed7 2258
kevin1990 20:7031cd147ed7 2259 if (actualLength != pLutHeader->totalLength)
kevin1990 20:7031cd147ed7 2260 {
kevin1990 20:7031cd147ed7 2261 ADI_SENSE_LOG_ERROR("LUT table length mismatch (expected %u, actual %u)",
kevin1990 20:7031cd147ed7 2262 pLutHeader->totalLength, actualLength);
kevin1990 20:7031cd147ed7 2263 return ADI_SENSE_WRONG_SIZE;
kevin1990 20:7031cd147ed7 2264 }
kevin1990 20:7031cd147ed7 2265
kevin1990 20:7031cd147ed7 2266 if (sizeof(*pLutHeader) + pLutHeader->totalLength > ADI_SENSE_LUT_MAX_SIZE)
kevin1990 20:7031cd147ed7 2267 {
kevin1990 20:7031cd147ed7 2268 ADI_SENSE_LOG_ERROR("Maximum LUT table length (%u bytes) exceeded",
kevin1990 20:7031cd147ed7 2269 ADI_SENSE_LUT_MAX_SIZE);
kevin1990 20:7031cd147ed7 2270 return ADI_SENSE_WRONG_SIZE;
kevin1990 20:7031cd147ed7 2271 }
kevin1990 20:7031cd147ed7 2272
kevin1990 20:7031cd147ed7 2273 /* Write the LUT data to the device */
kevin1990 20:7031cd147ed7 2274 unsigned lutSize = sizeof(*pLutHeader) + pLutHeader->totalLength;
kevin1990 20:7031cd147ed7 2275 WRITE_REG_U16(hDevice, 0, CORE_LUT_OFFSET);
kevin1990 20:7031cd147ed7 2276 WRITE_REG_U8_ARRAY(hDevice, (uint8_t *)pLutData, lutSize, CORE_LUT_DATA);
kevin1990 20:7031cd147ed7 2277
kevin1990 20:7031cd147ed7 2278 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2279 }
kevin1990 20:7031cd147ed7 2280
kevin1990 20:7031cd147ed7 2281 ADI_SENSE_RESULT adi_sense_1000_SetLutDataRaw(
kevin1990 20:7031cd147ed7 2282 ADI_SENSE_DEVICE_HANDLE const hDevice,
kevin1990 20:7031cd147ed7 2283 ADI_SENSE_1000_LUT_RAW * const pLutData)
kevin1990 20:7031cd147ed7 2284 {
kevin1990 20:7031cd147ed7 2285 return adi_sense_1000_SetLutData(hDevice,
kevin1990 20:7031cd147ed7 2286 (ADI_SENSE_1000_LUT * const)pLutData);
kevin1990 20:7031cd147ed7 2287 }
kevin1990 20:7031cd147ed7 2288
kevin1990 20:7031cd147ed7 2289 static ADI_SENSE_RESULT getLutTableSize(
kevin1990 20:7031cd147ed7 2290 ADI_SENSE_1000_LUT_DESCRIPTOR * const pDesc,
kevin1990 20:7031cd147ed7 2291 ADI_SENSE_1000_LUT_TABLE_DATA * const pData,
kevin1990 20:7031cd147ed7 2292 unsigned *pLength)
kevin1990 20:7031cd147ed7 2293 {
kevin1990 20:7031cd147ed7 2294 switch (pDesc->geometry)
kevin1990 20:7031cd147ed7 2295 {
kevin1990 20:7031cd147ed7 2296 case ADI_SENSE_1000_LUT_GEOMETRY_COEFFS:
kevin1990 20:7031cd147ed7 2297 if (pDesc->equation == ADI_SENSE_1000_LUT_EQUATION_BIVARIATE_POLYN)
kevin1990 20:7031cd147ed7 2298 *pLength = ADI_SENSE_1000_LUT_2D_POLYN_COEFF_LIST_SIZE(pData->coeffList2d);
kevin1990 20:7031cd147ed7 2299 else
kevin1990 20:7031cd147ed7 2300 *pLength = ADI_SENSE_1000_LUT_COEFF_LIST_SIZE(pData->coeffList);
kevin1990 20:7031cd147ed7 2301 break;
kevin1990 20:7031cd147ed7 2302 case ADI_SENSE_1000_LUT_GEOMETRY_NES_1D:
kevin1990 20:7031cd147ed7 2303 *pLength = ADI_SENSE_1000_LUT_1D_NES_SIZE(pData->lut1dNes);
kevin1990 20:7031cd147ed7 2304 break;
kevin1990 20:7031cd147ed7 2305 case ADI_SENSE_1000_LUT_GEOMETRY_NES_2D:
kevin1990 20:7031cd147ed7 2306 *pLength = ADI_SENSE_1000_LUT_2D_NES_SIZE(pData->lut2dNes);
kevin1990 20:7031cd147ed7 2307 break;
kevin1990 20:7031cd147ed7 2308 case ADI_SENSE_1000_LUT_GEOMETRY_ES_1D:
kevin1990 20:7031cd147ed7 2309 *pLength = ADI_SENSE_1000_LUT_1D_ES_SIZE(pData->lut1dEs);
kevin1990 20:7031cd147ed7 2310 break;
kevin1990 20:7031cd147ed7 2311 case ADI_SENSE_1000_LUT_GEOMETRY_ES_2D:
kevin1990 20:7031cd147ed7 2312 *pLength = ADI_SENSE_1000_LUT_2D_ES_SIZE(pData->lut2dEs);
kevin1990 20:7031cd147ed7 2313 break;
kevin1990 20:7031cd147ed7 2314 default:
kevin1990 20:7031cd147ed7 2315 ADI_SENSE_LOG_ERROR("Invalid LUT table geometry %d specified\r\n",
kevin1990 20:7031cd147ed7 2316 pDesc->geometry);
kevin1990 20:7031cd147ed7 2317 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2318 }
kevin1990 20:7031cd147ed7 2319
kevin1990 20:7031cd147ed7 2320 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2321 }
kevin1990 20:7031cd147ed7 2322
kevin1990 20:7031cd147ed7 2323 ADI_SENSE_RESULT adi_sense_1000_AssembleLutData(
kevin1990 20:7031cd147ed7 2324 ADI_SENSE_1000_LUT * pLutBuffer,
kevin1990 20:7031cd147ed7 2325 unsigned nLutBufferSize,
kevin1990 20:7031cd147ed7 2326 unsigned const nNumTables,
kevin1990 20:7031cd147ed7 2327 ADI_SENSE_1000_LUT_DESCRIPTOR * const ppDesc[],
kevin1990 20:7031cd147ed7 2328 ADI_SENSE_1000_LUT_TABLE_DATA * const ppData[])
kevin1990 20:7031cd147ed7 2329 {
kevin1990 20:7031cd147ed7 2330 ADI_SENSE_1000_LUT_HEADER *pHdr = &pLutBuffer->header;
kevin1990 20:7031cd147ed7 2331 uint8_t *pLutTableData = (uint8_t *)pLutBuffer + sizeof(*pHdr);
kevin1990 20:7031cd147ed7 2332
kevin1990 20:7031cd147ed7 2333 if (sizeof(*pHdr) > nLutBufferSize)
kevin1990 20:7031cd147ed7 2334 {
kevin1990 20:7031cd147ed7 2335 ADI_SENSE_LOG_ERROR("Insufficient LUT buffer size provided");
kevin1990 20:7031cd147ed7 2336 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2337 }
kevin1990 20:7031cd147ed7 2338
kevin1990 20:7031cd147ed7 2339 /* First initialise the top-level header */
kevin1990 20:7031cd147ed7 2340 pHdr->signature = ADI_SENSE_LUT_SIGNATURE;
kevin1990 20:7031cd147ed7 2341 pHdr->version.major = 1;
kevin1990 20:7031cd147ed7 2342 pHdr->version.minor = 0;
kevin1990 20:7031cd147ed7 2343 pHdr->numTables = 0;
kevin1990 20:7031cd147ed7 2344 pHdr->totalLength = 0;
kevin1990 20:7031cd147ed7 2345
kevin1990 20:7031cd147ed7 2346 /*
kevin1990 20:7031cd147ed7 2347 * Walk through the list of table pointers provided, appending the table
kevin1990 20:7031cd147ed7 2348 * descriptor+data from each one to the provided LUT buffer
kevin1990 20:7031cd147ed7 2349 */
kevin1990 20:7031cd147ed7 2350 for (unsigned i = 0; i < nNumTables; i++)
kevin1990 20:7031cd147ed7 2351 {
kevin1990 20:7031cd147ed7 2352 ADI_SENSE_1000_LUT_DESCRIPTOR * const pDesc = ppDesc[i];
kevin1990 20:7031cd147ed7 2353 ADI_SENSE_1000_LUT_TABLE_DATA * const pData = ppData[i];
kevin1990 20:7031cd147ed7 2354 ADI_SENSE_RESULT res;
kevin1990 20:7031cd147ed7 2355 unsigned dataLength = 0;
kevin1990 20:7031cd147ed7 2356
kevin1990 20:7031cd147ed7 2357 /* Calculate the length of the table data */
kevin1990 20:7031cd147ed7 2358 res = getLutTableSize(pDesc, pData, &dataLength);
kevin1990 20:7031cd147ed7 2359 if (res != ADI_SENSE_SUCCESS)
kevin1990 20:7031cd147ed7 2360 return res;
kevin1990 20:7031cd147ed7 2361
kevin1990 20:7031cd147ed7 2362 /* Fill in the table descriptor length and CRC fields */
kevin1990 20:7031cd147ed7 2363 pDesc->length = dataLength;
kevin1990 20:7031cd147ed7 2364 pDesc->crc16 = crc16_ccitt(pData, dataLength);
kevin1990 20:7031cd147ed7 2365
kevin1990 20:7031cd147ed7 2366 if ((sizeof(*pHdr) + pHdr->totalLength + sizeof(*pDesc) + dataLength) > nLutBufferSize)
kevin1990 20:7031cd147ed7 2367 {
kevin1990 20:7031cd147ed7 2368 ADI_SENSE_LOG_ERROR("Insufficient LUT buffer size provided");
kevin1990 20:7031cd147ed7 2369 return ADI_SENSE_INVALID_PARAM;
kevin1990 20:7031cd147ed7 2370 }
kevin1990 20:7031cd147ed7 2371
kevin1990 20:7031cd147ed7 2372 /* Append the table to the LUT buffer (desc + data) */
kevin1990 20:7031cd147ed7 2373 memcpy(pLutTableData + pHdr->totalLength, pDesc, sizeof(*pDesc));
kevin1990 20:7031cd147ed7 2374 pHdr->totalLength += sizeof(*pDesc);
kevin1990 20:7031cd147ed7 2375 memcpy(pLutTableData + pHdr->totalLength, pData, dataLength);
kevin1990 20:7031cd147ed7 2376 pHdr->totalLength += dataLength;
kevin1990 20:7031cd147ed7 2377
kevin1990 20:7031cd147ed7 2378 pHdr->numTables++;
kevin1990 20:7031cd147ed7 2379 }
kevin1990 20:7031cd147ed7 2380
kevin1990 20:7031cd147ed7 2381 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2382 }
kevin1990 20:7031cd147ed7 2383
kevin1990 20:7031cd147ed7 2384 #define CAL_TABLE_ROWS ADI_SENSE_1000_CAL_NUM_TABLES
kevin1990 20:7031cd147ed7 2385 #define CAL_TABLE_COLS ADI_SENSE_1000_CAL_NUM_TEMPS
kevin1990 20:7031cd147ed7 2386 #define CAL_TABLE_SIZE (sizeof(float) * CAL_TABLE_ROWS * CAL_TABLE_COLS)
kevin1990 20:7031cd147ed7 2387
kevin1990 20:7031cd147ed7 2388 ADI_SENSE_RESULT adi_sense_1000_ReadCalTable(
kevin1990 20:7031cd147ed7 2389 ADI_SENSE_DEVICE_HANDLE hDevice,
kevin1990 20:7031cd147ed7 2390 float *pfBuffer,
kevin1990 20:7031cd147ed7 2391 unsigned nMaxLen,
kevin1990 20:7031cd147ed7 2392 unsigned *pnDataLen,
kevin1990 20:7031cd147ed7 2393 unsigned *pnRows,
kevin1990 20:7031cd147ed7 2394 unsigned *pnColumns)
kevin1990 20:7031cd147ed7 2395 {
kevin1990 20:7031cd147ed7 2396 *pnDataLen = sizeof(float) * CAL_TABLE_ROWS * CAL_TABLE_COLS;
kevin1990 20:7031cd147ed7 2397 *pnRows = CAL_TABLE_ROWS;
kevin1990 20:7031cd147ed7 2398 *pnColumns = CAL_TABLE_COLS;
kevin1990 20:7031cd147ed7 2399
kevin1990 20:7031cd147ed7 2400 if (nMaxLen > *pnDataLen)
kevin1990 20:7031cd147ed7 2401 nMaxLen = *pnDataLen;
kevin1990 20:7031cd147ed7 2402
kevin1990 20:7031cd147ed7 2403 WRITE_REG_U16(hDevice, 0, CORE_CAL_OFFSET);
kevin1990 20:7031cd147ed7 2404 READ_REG_U8_ARRAY(hDevice, (uint8_t *)pfBuffer, nMaxLen, CORE_CAL_DATA);
kevin1990 20:7031cd147ed7 2405
kevin1990 20:7031cd147ed7 2406 return ADI_SENSE_SUCCESS;
kevin1990 20:7031cd147ed7 2407 }
kevin1990 20:7031cd147ed7 2408