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