ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
kevin1990
Date:
Mon Jan 08 16:57:35 2018 +0000
Revision:
21:354f33c60595
Parent:
20:7031cd147ed7
v1.0RC4 Release

Who changed what in which revision?

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