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