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