![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ADISense1000 Version 2.1 code base
Fork of AdiSense1000_V21 by
src/adi_sense_1000.c@7:4dbae381f693, 2017-10-20 (annotated)
- Committer:
- kevin1990
- Date:
- Fri Oct 20 15:58:01 2017 +0000
- Revision:
- 7:4dbae381f693
v0.3 release (New Host api)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevin1990 | 7:4dbae381f693 | 1 | /*! |
kevin1990 | 7:4dbae381f693 | 2 | ****************************************************************************** |
kevin1990 | 7:4dbae381f693 | 3 | * @file: adi_sense_1000.c |
kevin1990 | 7:4dbae381f693 | 4 | * @brief: ADI Sense API implementation for ADI Sense 1000 |
kevin1990 | 7:4dbae381f693 | 5 | *----------------------------------------------------------------------------- |
kevin1990 | 7:4dbae381f693 | 6 | */ |
kevin1990 | 7:4dbae381f693 | 7 | |
kevin1990 | 7:4dbae381f693 | 8 | /****************************************************************************** |
kevin1990 | 7:4dbae381f693 | 9 | Copyright (c) 2017 Emutex Ltd. / Analog Devices, Inc. |
kevin1990 | 7:4dbae381f693 | 10 | |
kevin1990 | 7:4dbae381f693 | 11 | All rights reserved. |
kevin1990 | 7:4dbae381f693 | 12 | |
kevin1990 | 7:4dbae381f693 | 13 | Redistribution and use in source and binary forms, with or without modification, |
kevin1990 | 7:4dbae381f693 | 14 | are permitted provided that the following conditions are met: |
kevin1990 | 7:4dbae381f693 | 15 | - Redistributions of source code must retain the above copyright notice, |
kevin1990 | 7:4dbae381f693 | 16 | this list of conditions and the following disclaimer. |
kevin1990 | 7:4dbae381f693 | 17 | - Redistributions in binary form must reproduce the above copyright notice, |
kevin1990 | 7:4dbae381f693 | 18 | this list of conditions and the following disclaimer in the documentation |
kevin1990 | 7:4dbae381f693 | 19 | and/or other materials provided with the distribution. |
kevin1990 | 7:4dbae381f693 | 20 | - Modified versions of the software must be conspicuously marked as such. |
kevin1990 | 7:4dbae381f693 | 21 | - This software is licensed solely and exclusively for use with processors |
kevin1990 | 7:4dbae381f693 | 22 | manufactured by or for Analog Devices, Inc. |
kevin1990 | 7:4dbae381f693 | 23 | - This software may not be combined or merged with other code in any manner |
kevin1990 | 7:4dbae381f693 | 24 | that would cause the software to become subject to terms and conditions |
kevin1990 | 7:4dbae381f693 | 25 | which differ from those listed here. |
kevin1990 | 7:4dbae381f693 | 26 | - Neither the name of Analog Devices, Inc. nor the names of its |
kevin1990 | 7:4dbae381f693 | 27 | contributors may be used to endorse or promote products derived |
kevin1990 | 7:4dbae381f693 | 28 | from this software without specific prior written permission. |
kevin1990 | 7:4dbae381f693 | 29 | - The use of this software may or may not infringe the patent rights of one |
kevin1990 | 7:4dbae381f693 | 30 | or more patent holders. This license does not release you from the |
kevin1990 | 7:4dbae381f693 | 31 | requirement that you obtain separate licenses from these patent holders |
kevin1990 | 7:4dbae381f693 | 32 | to use this software. |
kevin1990 | 7:4dbae381f693 | 33 | |
kevin1990 | 7:4dbae381f693 | 34 | THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY |
kevin1990 | 7:4dbae381f693 | 35 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, |
kevin1990 | 7:4dbae381f693 | 36 | TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN |
kevin1990 | 7:4dbae381f693 | 37 | NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
kevin1990 | 7:4dbae381f693 | 38 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES |
kevin1990 | 7:4dbae381f693 | 39 | (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL |
kevin1990 | 7:4dbae381f693 | 40 | PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
kevin1990 | 7:4dbae381f693 | 41 | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
kevin1990 | 7:4dbae381f693 | 42 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
kevin1990 | 7:4dbae381f693 | 43 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
kevin1990 | 7:4dbae381f693 | 44 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
kevin1990 | 7:4dbae381f693 | 45 | * |
kevin1990 | 7:4dbae381f693 | 46 | *****************************************************************************/ |
kevin1990 | 7:4dbae381f693 | 47 | #include <float.h> |
kevin1990 | 7:4dbae381f693 | 48 | #include <math.h> |
kevin1990 | 7:4dbae381f693 | 49 | |
kevin1990 | 7:4dbae381f693 | 50 | #include "inc/adi_sense_api.h" |
kevin1990 | 7:4dbae381f693 | 51 | #include "inc/adi_sense_platform.h" |
kevin1990 | 7:4dbae381f693 | 52 | |
kevin1990 | 7:4dbae381f693 | 53 | #include "adi_sense_1000/ADISENSE1000_REGISTERS_typedefs.h" |
kevin1990 | 7:4dbae381f693 | 54 | #include "adi_sense_1000/ADISENSE1000_REGISTERS.h" |
kevin1990 | 7:4dbae381f693 | 55 | |
kevin1990 | 7:4dbae381f693 | 56 | #include "crc16.h" |
kevin1990 | 7:4dbae381f693 | 57 | |
kevin1990 | 7:4dbae381f693 | 58 | #define REG_READ_DELAY_USEC (20) |
kevin1990 | 7:4dbae381f693 | 59 | |
kevin1990 | 7:4dbae381f693 | 60 | #define ADI_SENSE_CHANNEL_IS_ADC(c) \ |
kevin1990 | 7:4dbae381f693 | 61 | ((c) >= ADI_SENSE_CHANNEL_ID_CJC_0 && (c) <= ADI_SENSE_CHANNEL_ID_CURRENT_0) |
kevin1990 | 7:4dbae381f693 | 62 | |
kevin1990 | 7:4dbae381f693 | 63 | #define ADI_SENSE_CHANNEL_IS_ADC_CJC(c) \ |
kevin1990 | 7:4dbae381f693 | 64 | ((c) >= ADI_SENSE_CHANNEL_ID_CJC_0 && (c) <= ADI_SENSE_CHANNEL_ID_CJC_1) |
kevin1990 | 7:4dbae381f693 | 65 | |
kevin1990 | 7:4dbae381f693 | 66 | #define ADI_SENSE_CHANNEL_IS_ADC_SENSOR(c) \ |
kevin1990 | 7:4dbae381f693 | 67 | ((c) >= ADI_SENSE_CHANNEL_ID_SENSOR_0 && (c) <= ADI_SENSE_CHANNEL_ID_SENSOR_3) |
kevin1990 | 7:4dbae381f693 | 68 | |
kevin1990 | 7:4dbae381f693 | 69 | #define ADI_SENSE_CHANNEL_IS_ADC_VOLTAGE(c) \ |
kevin1990 | 7:4dbae381f693 | 70 | ((c) == ADI_SENSE_CHANNEL_ID_VOLTAGE_0) |
kevin1990 | 7:4dbae381f693 | 71 | |
kevin1990 | 7:4dbae381f693 | 72 | #define ADI_SENSE_CHANNEL_IS_ADC_CURRENT(c) \ |
kevin1990 | 7:4dbae381f693 | 73 | ((c) == ADI_SENSE_CHANNEL_ID_CURRENT_0) |
kevin1990 | 7:4dbae381f693 | 74 | |
kevin1990 | 7:4dbae381f693 | 75 | typedef struct |
kevin1990 | 7:4dbae381f693 | 76 | { |
kevin1990 | 7:4dbae381f693 | 77 | unsigned nDeviceIndex; |
kevin1990 | 7:4dbae381f693 | 78 | ADI_SENSE_SPI_HANDLE hSpi; |
kevin1990 | 7:4dbae381f693 | 79 | ADI_SENSE_GPIO_HANDLE hGpio; |
kevin1990 | 7:4dbae381f693 | 80 | } ADI_SENSE_DEVICE_CONTEXT; |
kevin1990 | 7:4dbae381f693 | 81 | |
kevin1990 | 7:4dbae381f693 | 82 | static ADI_SENSE_DEVICE_CONTEXT gDeviceCtx[ADI_SENSE_MAX_DEVICES]; |
kevin1990 | 7:4dbae381f693 | 83 | |
kevin1990 | 7:4dbae381f693 | 84 | /* |
kevin1990 | 7:4dbae381f693 | 85 | * Open an ADI Sense device instance. |
kevin1990 | 7:4dbae381f693 | 86 | */ |
kevin1990 | 7:4dbae381f693 | 87 | ADI_SENSE_RESULT adi_sense_Open( |
kevin1990 | 7:4dbae381f693 | 88 | unsigned const nDeviceIndex, |
kevin1990 | 7:4dbae381f693 | 89 | ADI_SENSE_CONNECTION * const pConnectionInfo, |
kevin1990 | 7:4dbae381f693 | 90 | ADI_SENSE_DEVICE_HANDLE * const phDevice) |
kevin1990 | 7:4dbae381f693 | 91 | { |
kevin1990 | 7:4dbae381f693 | 92 | ADI_SENSE_DEVICE_CONTEXT *pCtx; |
kevin1990 | 7:4dbae381f693 | 93 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 94 | |
kevin1990 | 7:4dbae381f693 | 95 | if (nDeviceIndex >= ADI_SENSE_MAX_DEVICES) |
kevin1990 | 7:4dbae381f693 | 96 | return ADI_SENSE_INVALID_DEVICE_NUM; |
kevin1990 | 7:4dbae381f693 | 97 | |
kevin1990 | 7:4dbae381f693 | 98 | pCtx = &gDeviceCtx[nDeviceIndex]; |
kevin1990 | 7:4dbae381f693 | 99 | pCtx->nDeviceIndex = nDeviceIndex; |
kevin1990 | 7:4dbae381f693 | 100 | |
kevin1990 | 7:4dbae381f693 | 101 | eRet = adi_sense_LogOpen(); |
kevin1990 | 7:4dbae381f693 | 102 | if (eRet != ADI_SENSE_SUCCESS) |
kevin1990 | 7:4dbae381f693 | 103 | return eRet; |
kevin1990 | 7:4dbae381f693 | 104 | |
kevin1990 | 7:4dbae381f693 | 105 | eRet = adi_sense_GpioOpen(&pConnectionInfo->gpio, &pCtx->hGpio); |
kevin1990 | 7:4dbae381f693 | 106 | if (eRet != ADI_SENSE_SUCCESS) |
kevin1990 | 7:4dbae381f693 | 107 | return eRet; |
kevin1990 | 7:4dbae381f693 | 108 | |
kevin1990 | 7:4dbae381f693 | 109 | eRet = adi_sense_SpiOpen(&pConnectionInfo->spi, &pCtx->hSpi); |
kevin1990 | 7:4dbae381f693 | 110 | if (eRet != ADI_SENSE_SUCCESS) |
kevin1990 | 7:4dbae381f693 | 111 | return eRet; |
kevin1990 | 7:4dbae381f693 | 112 | |
kevin1990 | 7:4dbae381f693 | 113 | *phDevice = pCtx; |
kevin1990 | 7:4dbae381f693 | 114 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 115 | } |
kevin1990 | 7:4dbae381f693 | 116 | |
kevin1990 | 7:4dbae381f693 | 117 | /* |
kevin1990 | 7:4dbae381f693 | 118 | * Get the current state of the specified GPIO input signal. |
kevin1990 | 7:4dbae381f693 | 119 | */ |
kevin1990 | 7:4dbae381f693 | 120 | ADI_SENSE_RESULT adi_sense_GetGpioState( |
kevin1990 | 7:4dbae381f693 | 121 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 122 | ADI_SENSE_GPIO_PIN const ePinId, |
kevin1990 | 7:4dbae381f693 | 123 | bool_t * const pbAsserted) |
kevin1990 | 7:4dbae381f693 | 124 | { |
kevin1990 | 7:4dbae381f693 | 125 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 126 | |
kevin1990 | 7:4dbae381f693 | 127 | return adi_sense_GpioGet(pCtx->hGpio, ePinId, pbAsserted); |
kevin1990 | 7:4dbae381f693 | 128 | } |
kevin1990 | 7:4dbae381f693 | 129 | |
kevin1990 | 7:4dbae381f693 | 130 | /* |
kevin1990 | 7:4dbae381f693 | 131 | * Register an application-defined callback function for GPIO interrupts. |
kevin1990 | 7:4dbae381f693 | 132 | */ |
kevin1990 | 7:4dbae381f693 | 133 | ADI_SENSE_RESULT adi_sense_RegisterGpioCallback( |
kevin1990 | 7:4dbae381f693 | 134 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 135 | ADI_SENSE_GPIO_PIN const ePinId, |
kevin1990 | 7:4dbae381f693 | 136 | ADI_SENSE_GPIO_CALLBACK const callbackFunction, |
kevin1990 | 7:4dbae381f693 | 137 | void * const pCallbackParam) |
kevin1990 | 7:4dbae381f693 | 138 | { |
kevin1990 | 7:4dbae381f693 | 139 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 140 | |
kevin1990 | 7:4dbae381f693 | 141 | if (callbackFunction) |
kevin1990 | 7:4dbae381f693 | 142 | { |
kevin1990 | 7:4dbae381f693 | 143 | return adi_sense_GpioIrqEnable(pCtx->hGpio, ePinId, callbackFunction, |
kevin1990 | 7:4dbae381f693 | 144 | pCallbackParam); |
kevin1990 | 7:4dbae381f693 | 145 | } |
kevin1990 | 7:4dbae381f693 | 146 | else |
kevin1990 | 7:4dbae381f693 | 147 | { |
kevin1990 | 7:4dbae381f693 | 148 | return adi_sense_GpioIrqDisable(pCtx->hGpio, ePinId); |
kevin1990 | 7:4dbae381f693 | 149 | } |
kevin1990 | 7:4dbae381f693 | 150 | } |
kevin1990 | 7:4dbae381f693 | 151 | |
kevin1990 | 7:4dbae381f693 | 152 | /* |
kevin1990 | 7:4dbae381f693 | 153 | * Reset the specified ADI Sense device. |
kevin1990 | 7:4dbae381f693 | 154 | */ |
kevin1990 | 7:4dbae381f693 | 155 | ADI_SENSE_RESULT adi_sense_Reset( |
kevin1990 | 7:4dbae381f693 | 156 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 157 | { |
kevin1990 | 7:4dbae381f693 | 158 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 159 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 160 | |
kevin1990 | 7:4dbae381f693 | 161 | /* Pulse the Reset GPIO pin low for a minimum of 4 microseconds */ |
kevin1990 | 7:4dbae381f693 | 162 | eRet = adi_sense_GpioSet(pCtx->hGpio, ADI_SENSE_GPIO_PIN_RESET, false); |
kevin1990 | 7:4dbae381f693 | 163 | if (eRet != ADI_SENSE_SUCCESS) |
kevin1990 | 7:4dbae381f693 | 164 | return eRet; |
kevin1990 | 7:4dbae381f693 | 165 | |
kevin1990 | 7:4dbae381f693 | 166 | adi_sense_TimeDelayUsec(4); |
kevin1990 | 7:4dbae381f693 | 167 | |
kevin1990 | 7:4dbae381f693 | 168 | eRet = adi_sense_GpioSet(pCtx->hGpio, ADI_SENSE_GPIO_PIN_RESET, true); |
kevin1990 | 7:4dbae381f693 | 169 | if (eRet != ADI_SENSE_SUCCESS) |
kevin1990 | 7:4dbae381f693 | 170 | return eRet; |
kevin1990 | 7:4dbae381f693 | 171 | |
kevin1990 | 7:4dbae381f693 | 172 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 173 | } |
kevin1990 | 7:4dbae381f693 | 174 | |
kevin1990 | 7:4dbae381f693 | 175 | |
kevin1990 | 7:4dbae381f693 | 176 | /*! |
kevin1990 | 7:4dbae381f693 | 177 | * @brief Get general status of ADISense module. |
kevin1990 | 7:4dbae381f693 | 178 | * |
kevin1990 | 7:4dbae381f693 | 179 | * @param[in] |
kevin1990 | 7:4dbae381f693 | 180 | * @param[out] pStatus : Pointer to CORE Status struct. |
kevin1990 | 7:4dbae381f693 | 181 | * |
kevin1990 | 7:4dbae381f693 | 182 | * @return Status |
kevin1990 | 7:4dbae381f693 | 183 | * - #ADI_SENSE_SUCCESS Call completed successfully. |
kevin1990 | 7:4dbae381f693 | 184 | * - #ADI_SENSE_FAILURE If status register read fails. |
kevin1990 | 7:4dbae381f693 | 185 | * |
kevin1990 | 7:4dbae381f693 | 186 | * @details Read the general status register for the ADISense |
kevin1990 | 7:4dbae381f693 | 187 | * module. Indicates Error, Alert conditions, data ready |
kevin1990 | 7:4dbae381f693 | 188 | * and command running. |
kevin1990 | 7:4dbae381f693 | 189 | * |
kevin1990 | 7:4dbae381f693 | 190 | */ |
kevin1990 | 7:4dbae381f693 | 191 | ADI_SENSE_RESULT adi_sense_GetStatus( |
kevin1990 | 7:4dbae381f693 | 192 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 193 | ADI_SENSE_STATUS * const pStatus) |
kevin1990 | 7:4dbae381f693 | 194 | { |
kevin1990 | 7:4dbae381f693 | 195 | ADI_ADISENSE_CORE_Status_t statusReg; |
kevin1990 | 7:4dbae381f693 | 196 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 197 | |
kevin1990 | 7:4dbae381f693 | 198 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_STATUS, |
kevin1990 | 7:4dbae381f693 | 199 | &statusReg, sizeof(statusReg)); |
kevin1990 | 7:4dbae381f693 | 200 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 201 | { |
kevin1990 | 7:4dbae381f693 | 202 | return eRet; |
kevin1990 | 7:4dbae381f693 | 203 | } |
kevin1990 | 7:4dbae381f693 | 204 | |
kevin1990 | 7:4dbae381f693 | 205 | pStatus->deviceStatus = 0; |
kevin1990 | 7:4dbae381f693 | 206 | |
kevin1990 | 7:4dbae381f693 | 207 | if (statusReg.Cmd_Running) |
kevin1990 | 7:4dbae381f693 | 208 | pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_BUSY; |
kevin1990 | 7:4dbae381f693 | 209 | |
kevin1990 | 7:4dbae381f693 | 210 | if (statusReg.Drdy) |
kevin1990 | 7:4dbae381f693 | 211 | pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_DATAREADY; |
kevin1990 | 7:4dbae381f693 | 212 | |
kevin1990 | 7:4dbae381f693 | 213 | if (statusReg.Error) |
kevin1990 | 7:4dbae381f693 | 214 | pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_ERROR; |
kevin1990 | 7:4dbae381f693 | 215 | |
kevin1990 | 7:4dbae381f693 | 216 | if (statusReg.Alert_Limit) |
kevin1990 | 7:4dbae381f693 | 217 | { |
kevin1990 | 7:4dbae381f693 | 218 | pStatus->deviceStatus |= ADI_SENSE_DEVICE_STATUS_ALERT; |
kevin1990 | 7:4dbae381f693 | 219 | |
kevin1990 | 7:4dbae381f693 | 220 | ADI_ADISENSE_CORE_Channel_Alert_Status_t channelAlertStatusReg; |
kevin1990 | 7:4dbae381f693 | 221 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_CHANNEL_ALERT_STATUS, |
kevin1990 | 7:4dbae381f693 | 222 | &channelAlertStatusReg, |
kevin1990 | 7:4dbae381f693 | 223 | sizeof(channelAlertStatusReg)); |
kevin1990 | 7:4dbae381f693 | 224 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 225 | { |
kevin1990 | 7:4dbae381f693 | 226 | return eRet; |
kevin1990 | 7:4dbae381f693 | 227 | } |
kevin1990 | 7:4dbae381f693 | 228 | |
kevin1990 | 7:4dbae381f693 | 229 | for (unsigned i = 0; i < ADI_SENSE_MAX_CHANNELS; i++) |
kevin1990 | 7:4dbae381f693 | 230 | { |
kevin1990 | 7:4dbae381f693 | 231 | pStatus->channelAlerts[i] = 0; |
kevin1990 | 7:4dbae381f693 | 232 | |
kevin1990 | 7:4dbae381f693 | 233 | if (channelAlertStatusReg.VALUE16 & (1 << i)) |
kevin1990 | 7:4dbae381f693 | 234 | { |
kevin1990 | 7:4dbae381f693 | 235 | ADI_ADISENSE_CORE_Alert_Detail_Ch_t alertDetailReg; |
kevin1990 | 7:4dbae381f693 | 236 | eRet = adi_sense_ReadRegister(hDevice, |
kevin1990 | 7:4dbae381f693 | 237 | REG_ADISENSE_CORE_ALERT_DETAIL_CHn(i), |
kevin1990 | 7:4dbae381f693 | 238 | &alertDetailReg, |
kevin1990 | 7:4dbae381f693 | 239 | sizeof(alertDetailReg)); |
kevin1990 | 7:4dbae381f693 | 240 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 241 | { |
kevin1990 | 7:4dbae381f693 | 242 | return eRet; |
kevin1990 | 7:4dbae381f693 | 243 | } |
kevin1990 | 7:4dbae381f693 | 244 | |
kevin1990 | 7:4dbae381f693 | 245 | if (alertDetailReg.Time_Out) |
kevin1990 | 7:4dbae381f693 | 246 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_TIMEOUT; |
kevin1990 | 7:4dbae381f693 | 247 | if (alertDetailReg.Under_Range) |
kevin1990 | 7:4dbae381f693 | 248 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_UNDER_RANGE; |
kevin1990 | 7:4dbae381f693 | 249 | if (alertDetailReg.Over_Range) |
kevin1990 | 7:4dbae381f693 | 250 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_OVER_RANGE; |
kevin1990 | 7:4dbae381f693 | 251 | if (alertDetailReg.Low_Limit) |
kevin1990 | 7:4dbae381f693 | 252 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_LOW_LIMIT; |
kevin1990 | 7:4dbae381f693 | 253 | if (alertDetailReg.High_Limit) |
kevin1990 | 7:4dbae381f693 | 254 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_HIGH_LIMIT; |
kevin1990 | 7:4dbae381f693 | 255 | if (alertDetailReg.Sensor_Open) |
kevin1990 | 7:4dbae381f693 | 256 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_SENSOR_OPEN; |
kevin1990 | 7:4dbae381f693 | 257 | if (alertDetailReg.Ref_Detect) |
kevin1990 | 7:4dbae381f693 | 258 | pStatus->channelAlerts[i] |= ADI_SENSE_CHANNEL_ALERT_REF_DETECT; |
kevin1990 | 7:4dbae381f693 | 259 | } |
kevin1990 | 7:4dbae381f693 | 260 | } |
kevin1990 | 7:4dbae381f693 | 261 | } |
kevin1990 | 7:4dbae381f693 | 262 | |
kevin1990 | 7:4dbae381f693 | 263 | /* TODO - fill diagnosticsStatus field */ |
kevin1990 | 7:4dbae381f693 | 264 | |
kevin1990 | 7:4dbae381f693 | 265 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 266 | } |
kevin1990 | 7:4dbae381f693 | 267 | |
kevin1990 | 7:4dbae381f693 | 268 | ADI_SENSE_RESULT adi_sense_GetCommandRunningState( |
kevin1990 | 7:4dbae381f693 | 269 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 270 | bool_t *pbCommandRunning) |
kevin1990 | 7:4dbae381f693 | 271 | { |
kevin1990 | 7:4dbae381f693 | 272 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 273 | ADI_ADISENSE_CORE_Status_t statusReg; |
kevin1990 | 7:4dbae381f693 | 274 | |
kevin1990 | 7:4dbae381f693 | 275 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_STATUS, |
kevin1990 | 7:4dbae381f693 | 276 | &statusReg, sizeof(statusReg)); |
kevin1990 | 7:4dbae381f693 | 277 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 278 | return eRet; |
kevin1990 | 7:4dbae381f693 | 279 | |
kevin1990 | 7:4dbae381f693 | 280 | *pbCommandRunning = statusReg.Cmd_Running; |
kevin1990 | 7:4dbae381f693 | 281 | |
kevin1990 | 7:4dbae381f693 | 282 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 283 | } |
kevin1990 | 7:4dbae381f693 | 284 | |
kevin1990 | 7:4dbae381f693 | 285 | |
kevin1990 | 7:4dbae381f693 | 286 | static ADI_SENSE_RESULT executeCommand( |
kevin1990 | 7:4dbae381f693 | 287 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 288 | ADI_ADISENSE_CORE_Command_Special_Command const command, |
kevin1990 | 7:4dbae381f693 | 289 | bool_t const bWaitForCompletion) |
kevin1990 | 7:4dbae381f693 | 290 | { |
kevin1990 | 7:4dbae381f693 | 291 | ADI_ADISENSE_CORE_Command_t commandReg = { |
kevin1990 | 7:4dbae381f693 | 292 | .Special_Command = command |
kevin1990 | 7:4dbae381f693 | 293 | }; |
kevin1990 | 7:4dbae381f693 | 294 | bool_t bCommandRunning; |
kevin1990 | 7:4dbae381f693 | 295 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 296 | |
kevin1990 | 7:4dbae381f693 | 297 | /* |
kevin1990 | 7:4dbae381f693 | 298 | * Don't allow another command to be issued if one is already running, but |
kevin1990 | 7:4dbae381f693 | 299 | * make an exception for ADISENSE_CORE_COMMAND_NOP which can be used to |
kevin1990 | 7:4dbae381f693 | 300 | * request a running command to be stopped (e.g. continuous measurement) |
kevin1990 | 7:4dbae381f693 | 301 | */ |
kevin1990 | 7:4dbae381f693 | 302 | if (command != ADISENSE_CORE_COMMAND_NOP) |
kevin1990 | 7:4dbae381f693 | 303 | { |
kevin1990 | 7:4dbae381f693 | 304 | eRet = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning); |
kevin1990 | 7:4dbae381f693 | 305 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 306 | return eRet; |
kevin1990 | 7:4dbae381f693 | 307 | |
kevin1990 | 7:4dbae381f693 | 308 | if (bCommandRunning) |
kevin1990 | 7:4dbae381f693 | 309 | return ADI_SENSE_IN_USE; |
kevin1990 | 7:4dbae381f693 | 310 | } |
kevin1990 | 7:4dbae381f693 | 311 | |
kevin1990 | 7:4dbae381f693 | 312 | eRet = adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_COMMAND, |
kevin1990 | 7:4dbae381f693 | 313 | &commandReg, sizeof(commandReg)); |
kevin1990 | 7:4dbae381f693 | 314 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 315 | return eRet; |
kevin1990 | 7:4dbae381f693 | 316 | |
kevin1990 | 7:4dbae381f693 | 317 | if (bWaitForCompletion) |
kevin1990 | 7:4dbae381f693 | 318 | { |
kevin1990 | 7:4dbae381f693 | 319 | do { |
kevin1990 | 7:4dbae381f693 | 320 | eRet = adi_sense_GetCommandRunningState(hDevice, &bCommandRunning); |
kevin1990 | 7:4dbae381f693 | 321 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 322 | return eRet; |
kevin1990 | 7:4dbae381f693 | 323 | } while (bCommandRunning); |
kevin1990 | 7:4dbae381f693 | 324 | } |
kevin1990 | 7:4dbae381f693 | 325 | |
kevin1990 | 7:4dbae381f693 | 326 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 327 | } |
kevin1990 | 7:4dbae381f693 | 328 | |
kevin1990 | 7:4dbae381f693 | 329 | ADI_SENSE_RESULT adi_sense_ApplyConfigUpdates( |
kevin1990 | 7:4dbae381f693 | 330 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 331 | { |
kevin1990 | 7:4dbae381f693 | 332 | return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LATCH_CONFIG, true); |
kevin1990 | 7:4dbae381f693 | 333 | } |
kevin1990 | 7:4dbae381f693 | 334 | |
kevin1990 | 7:4dbae381f693 | 335 | /*! |
kevin1990 | 7:4dbae381f693 | 336 | * @brief Start a measurement cycle. |
kevin1990 | 7:4dbae381f693 | 337 | * |
kevin1990 | 7:4dbae381f693 | 338 | * @param[out] |
kevin1990 | 7:4dbae381f693 | 339 | * |
kevin1990 | 7:4dbae381f693 | 340 | * @return Status |
kevin1990 | 7:4dbae381f693 | 341 | * - #ADI_SENSE_SUCCESS Call completed successfully. |
kevin1990 | 7:4dbae381f693 | 342 | * - #ADI_SENSE_FAILURE |
kevin1990 | 7:4dbae381f693 | 343 | * |
kevin1990 | 7:4dbae381f693 | 344 | * @details Sends the latch config command. Configuration for channels in |
kevin1990 | 7:4dbae381f693 | 345 | * conversion cycle should be completed before this function. |
kevin1990 | 7:4dbae381f693 | 346 | * Channel enabled bit should be set before this function. |
kevin1990 | 7:4dbae381f693 | 347 | * Starts a conversion and configures the format of the sample. |
kevin1990 | 7:4dbae381f693 | 348 | * |
kevin1990 | 7:4dbae381f693 | 349 | */ |
kevin1990 | 7:4dbae381f693 | 350 | ADI_SENSE_RESULT adi_sense_StartMeasurement( |
kevin1990 | 7:4dbae381f693 | 351 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 352 | { |
kevin1990 | 7:4dbae381f693 | 353 | return executeCommand(hDevice, ADISENSE_CORE_COMMAND_CONVERT_WITH_RAW, false); |
kevin1990 | 7:4dbae381f693 | 354 | } |
kevin1990 | 7:4dbae381f693 | 355 | |
kevin1990 | 7:4dbae381f693 | 356 | /* |
kevin1990 | 7:4dbae381f693 | 357 | * Store the configuration settings to persistent memory on the device. |
kevin1990 | 7:4dbae381f693 | 358 | * No other command must be running when this is called. |
kevin1990 | 7:4dbae381f693 | 359 | * Do not power down the device while this command is running. |
kevin1990 | 7:4dbae381f693 | 360 | */ |
kevin1990 | 7:4dbae381f693 | 361 | ADI_SENSE_RESULT adi_sense_SaveConfig( |
kevin1990 | 7:4dbae381f693 | 362 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 363 | { |
kevin1990 | 7:4dbae381f693 | 364 | return executeCommand(hDevice, ADISENSE_CORE_COMMAND_SAVE_CONFIG, true); |
kevin1990 | 7:4dbae381f693 | 365 | } |
kevin1990 | 7:4dbae381f693 | 366 | |
kevin1990 | 7:4dbae381f693 | 367 | /* |
kevin1990 | 7:4dbae381f693 | 368 | * Restore the configuration settings from persistent memory on the device. |
kevin1990 | 7:4dbae381f693 | 369 | * No other command must be running when this is called. |
kevin1990 | 7:4dbae381f693 | 370 | */ |
kevin1990 | 7:4dbae381f693 | 371 | ADI_SENSE_RESULT adi_sense_RestoreConfig( |
kevin1990 | 7:4dbae381f693 | 372 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 373 | { |
kevin1990 | 7:4dbae381f693 | 374 | return executeCommand(hDevice, ADISENSE_CORE_COMMAND_LOAD_CONFIG, true); |
kevin1990 | 7:4dbae381f693 | 375 | } |
kevin1990 | 7:4dbae381f693 | 376 | |
kevin1990 | 7:4dbae381f693 | 377 | /* |
kevin1990 | 7:4dbae381f693 | 378 | * Stop the measurement cycles on the device. |
kevin1990 | 7:4dbae381f693 | 379 | * To be used only if a measurement command is currently running. |
kevin1990 | 7:4dbae381f693 | 380 | */ |
kevin1990 | 7:4dbae381f693 | 381 | ADI_SENSE_RESULT adi_sense_StopMeasurement( |
kevin1990 | 7:4dbae381f693 | 382 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 383 | { |
kevin1990 | 7:4dbae381f693 | 384 | return executeCommand(hDevice, ADISENSE_CORE_COMMAND_NOP, true); |
kevin1990 | 7:4dbae381f693 | 385 | } |
kevin1990 | 7:4dbae381f693 | 386 | |
kevin1990 | 7:4dbae381f693 | 387 | /* |
kevin1990 | 7:4dbae381f693 | 388 | * Run built-in diagnostic checks on the device. |
kevin1990 | 7:4dbae381f693 | 389 | * Diagnostics are executed according to the current applied settings. |
kevin1990 | 7:4dbae381f693 | 390 | * No other command must be running when this is called. |
kevin1990 | 7:4dbae381f693 | 391 | */ |
kevin1990 | 7:4dbae381f693 | 392 | ADI_SENSE_RESULT adi_sense_RunDiagnostics( |
kevin1990 | 7:4dbae381f693 | 393 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 394 | { |
kevin1990 | 7:4dbae381f693 | 395 | return executeCommand(hDevice, ADISENSE_CORE_COMMAND_RUN_DIAGNOSTICS, true); |
kevin1990 | 7:4dbae381f693 | 396 | } |
kevin1990 | 7:4dbae381f693 | 397 | |
kevin1990 | 7:4dbae381f693 | 398 | /* |
kevin1990 | 7:4dbae381f693 | 399 | * Read a set of data samples from the device. |
kevin1990 | 7:4dbae381f693 | 400 | * This may be called at any time. |
kevin1990 | 7:4dbae381f693 | 401 | */ |
kevin1990 | 7:4dbae381f693 | 402 | ADI_SENSE_RESULT adi_sense_GetData( |
kevin1990 | 7:4dbae381f693 | 403 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 404 | ADI_SENSE_DATA_SAMPLE * const pSamples, |
kevin1990 | 7:4dbae381f693 | 405 | uint32_t const nRequested, |
kevin1990 | 7:4dbae381f693 | 406 | uint32_t * const pnReturned) |
kevin1990 | 7:4dbae381f693 | 407 | { |
kevin1990 | 7:4dbae381f693 | 408 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 409 | uint16_t command = REG_ADISENSE_CORE_DATA_FIFO; |
kevin1990 | 7:4dbae381f693 | 410 | uint8_t commandData[sizeof(command)] = { |
kevin1990 | 7:4dbae381f693 | 411 | command >> 8, |
kevin1990 | 7:4dbae381f693 | 412 | command & 0xFF |
kevin1990 | 7:4dbae381f693 | 413 | }; |
kevin1990 | 7:4dbae381f693 | 414 | unsigned nValidSamples = 0; |
kevin1990 | 7:4dbae381f693 | 415 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 416 | |
kevin1990 | 7:4dbae381f693 | 417 | eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, NULL, |
kevin1990 | 7:4dbae381f693 | 418 | sizeof(command), false); |
kevin1990 | 7:4dbae381f693 | 419 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 420 | return eRet; |
kevin1990 | 7:4dbae381f693 | 421 | |
kevin1990 | 7:4dbae381f693 | 422 | adi_sense_TimeDelayUsec(REG_READ_DELAY_USEC); |
kevin1990 | 7:4dbae381f693 | 423 | |
kevin1990 | 7:4dbae381f693 | 424 | for (unsigned i = 0; i < nRequested; i++) |
kevin1990 | 7:4dbae381f693 | 425 | { |
kevin1990 | 7:4dbae381f693 | 426 | ADI_ADISENSE_CORE_Data_FIFO_t dataFifoReg; |
kevin1990 | 7:4dbae381f693 | 427 | bool_t bHoldCs = true; |
kevin1990 | 7:4dbae381f693 | 428 | |
kevin1990 | 7:4dbae381f693 | 429 | /* Keep the CS signal asserted for all but the last sample */ |
kevin1990 | 7:4dbae381f693 | 430 | if ((i + 1) == nRequested) |
kevin1990 | 7:4dbae381f693 | 431 | bHoldCs = false; |
kevin1990 | 7:4dbae381f693 | 432 | |
kevin1990 | 7:4dbae381f693 | 433 | eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, &dataFifoReg, |
kevin1990 | 7:4dbae381f693 | 434 | sizeof(dataFifoReg), bHoldCs); |
kevin1990 | 7:4dbae381f693 | 435 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 436 | return eRet; |
kevin1990 | 7:4dbae381f693 | 437 | |
kevin1990 | 7:4dbae381f693 | 438 | if (! dataFifoReg.Ch_Valid) |
kevin1990 | 7:4dbae381f693 | 439 | { |
kevin1990 | 7:4dbae381f693 | 440 | ADI_SENSE_LOG_WARN("Read invalid data sample"); |
kevin1990 | 7:4dbae381f693 | 441 | continue; |
kevin1990 | 7:4dbae381f693 | 442 | } |
kevin1990 | 7:4dbae381f693 | 443 | |
kevin1990 | 7:4dbae381f693 | 444 | ADI_SENSE_DATA_SAMPLE *pSample = &pSamples[nValidSamples]; |
kevin1990 | 7:4dbae381f693 | 445 | |
kevin1990 | 7:4dbae381f693 | 446 | pSample->status = 0; |
kevin1990 | 7:4dbae381f693 | 447 | if (dataFifoReg.Ch_Error) |
kevin1990 | 7:4dbae381f693 | 448 | pSample->status |= ADI_SENSE_DEVICE_STATUS_ERROR; |
kevin1990 | 7:4dbae381f693 | 449 | if (dataFifoReg.Ch_Alert) |
kevin1990 | 7:4dbae381f693 | 450 | pSample->status |= ADI_SENSE_DEVICE_STATUS_ALERT; |
kevin1990 | 7:4dbae381f693 | 451 | |
kevin1990 | 7:4dbae381f693 | 452 | if (dataFifoReg.Ch_Raw) |
kevin1990 | 7:4dbae381f693 | 453 | pSample->rawValue = dataFifoReg.Raw_Sample; |
kevin1990 | 7:4dbae381f693 | 454 | else |
kevin1990 | 7:4dbae381f693 | 455 | pSample->rawValue = 0; |
kevin1990 | 7:4dbae381f693 | 456 | |
kevin1990 | 7:4dbae381f693 | 457 | pSample->channelId = dataFifoReg.Channel_ID; |
kevin1990 | 7:4dbae381f693 | 458 | pSample->processedValue = dataFifoReg.Sensor_Result; |
kevin1990 | 7:4dbae381f693 | 459 | |
kevin1990 | 7:4dbae381f693 | 460 | nValidSamples++; |
kevin1990 | 7:4dbae381f693 | 461 | } |
kevin1990 | 7:4dbae381f693 | 462 | *pnReturned = nValidSamples; |
kevin1990 | 7:4dbae381f693 | 463 | |
kevin1990 | 7:4dbae381f693 | 464 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 465 | } |
kevin1990 | 7:4dbae381f693 | 466 | |
kevin1990 | 7:4dbae381f693 | 467 | /* |
kevin1990 | 7:4dbae381f693 | 468 | * Close the given ADI Sense device. |
kevin1990 | 7:4dbae381f693 | 469 | */ |
kevin1990 | 7:4dbae381f693 | 470 | ADI_SENSE_RESULT adi_sense_Close( |
kevin1990 | 7:4dbae381f693 | 471 | ADI_SENSE_DEVICE_HANDLE const hDevice) |
kevin1990 | 7:4dbae381f693 | 472 | { |
kevin1990 | 7:4dbae381f693 | 473 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 474 | |
kevin1990 | 7:4dbae381f693 | 475 | adi_sense_GpioClose(pCtx->hGpio); |
kevin1990 | 7:4dbae381f693 | 476 | adi_sense_SpiClose(pCtx->hSpi); |
kevin1990 | 7:4dbae381f693 | 477 | |
kevin1990 | 7:4dbae381f693 | 478 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 479 | } |
kevin1990 | 7:4dbae381f693 | 480 | |
kevin1990 | 7:4dbae381f693 | 481 | ADI_SENSE_RESULT adi_sense_WriteRegister( |
kevin1990 | 7:4dbae381f693 | 482 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 483 | uint16_t nAddress, |
kevin1990 | 7:4dbae381f693 | 484 | void *pData, |
kevin1990 | 7:4dbae381f693 | 485 | unsigned nLength) |
kevin1990 | 7:4dbae381f693 | 486 | { |
kevin1990 | 7:4dbae381f693 | 487 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 488 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 489 | uint16_t command = 0x8000 | (nAddress & 0x7FFF); |
kevin1990 | 7:4dbae381f693 | 490 | uint8_t commandData[sizeof(command)] = { |
kevin1990 | 7:4dbae381f693 | 491 | command >> 8, |
kevin1990 | 7:4dbae381f693 | 492 | command & 0xFF |
kevin1990 | 7:4dbae381f693 | 493 | }; |
kevin1990 | 7:4dbae381f693 | 494 | |
kevin1990 | 7:4dbae381f693 | 495 | eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, NULL, |
kevin1990 | 7:4dbae381f693 | 496 | sizeof(commandData), false); |
kevin1990 | 7:4dbae381f693 | 497 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 498 | { |
kevin1990 | 7:4dbae381f693 | 499 | return eRet; |
kevin1990 | 7:4dbae381f693 | 500 | } |
kevin1990 | 7:4dbae381f693 | 501 | |
kevin1990 | 7:4dbae381f693 | 502 | return adi_sense_SpiTransfer(pCtx->hSpi, pData, NULL, nLength, false); |
kevin1990 | 7:4dbae381f693 | 503 | } |
kevin1990 | 7:4dbae381f693 | 504 | |
kevin1990 | 7:4dbae381f693 | 505 | ADI_SENSE_RESULT adi_sense_ReadRegister( |
kevin1990 | 7:4dbae381f693 | 506 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 507 | uint16_t nAddress, |
kevin1990 | 7:4dbae381f693 | 508 | void *pData, |
kevin1990 | 7:4dbae381f693 | 509 | unsigned nLength) |
kevin1990 | 7:4dbae381f693 | 510 | { |
kevin1990 | 7:4dbae381f693 | 511 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 512 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 513 | uint16_t command = nAddress & 0x7FFF; |
kevin1990 | 7:4dbae381f693 | 514 | uint8_t commandData[sizeof(command)] = { |
kevin1990 | 7:4dbae381f693 | 515 | command >> 8, |
kevin1990 | 7:4dbae381f693 | 516 | command & 0xFF |
kevin1990 | 7:4dbae381f693 | 517 | }; |
kevin1990 | 7:4dbae381f693 | 518 | |
kevin1990 | 7:4dbae381f693 | 519 | eRet = adi_sense_SpiTransfer(pCtx->hSpi, commandData, NULL, |
kevin1990 | 7:4dbae381f693 | 520 | sizeof(command), false); |
kevin1990 | 7:4dbae381f693 | 521 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 522 | { |
kevin1990 | 7:4dbae381f693 | 523 | return eRet; |
kevin1990 | 7:4dbae381f693 | 524 | } |
kevin1990 | 7:4dbae381f693 | 525 | |
kevin1990 | 7:4dbae381f693 | 526 | adi_sense_TimeDelayUsec(REG_READ_DELAY_USEC); |
kevin1990 | 7:4dbae381f693 | 527 | |
kevin1990 | 7:4dbae381f693 | 528 | eRet = adi_sense_SpiTransfer(pCtx->hSpi, NULL, pData, nLength, false); |
kevin1990 | 7:4dbae381f693 | 529 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 530 | { |
kevin1990 | 7:4dbae381f693 | 531 | return eRet; |
kevin1990 | 7:4dbae381f693 | 532 | } |
kevin1990 | 7:4dbae381f693 | 533 | |
kevin1990 | 7:4dbae381f693 | 534 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 535 | } |
kevin1990 | 7:4dbae381f693 | 536 | |
kevin1990 | 7:4dbae381f693 | 537 | ADI_SENSE_RESULT adi_sense_GetDeviceReadyState( |
kevin1990 | 7:4dbae381f693 | 538 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 539 | bool_t * const bReady) |
kevin1990 | 7:4dbae381f693 | 540 | { |
kevin1990 | 7:4dbae381f693 | 541 | ADI_ADISENSE_SPI_Chip_Type_t chipTypeReg; |
kevin1990 | 7:4dbae381f693 | 542 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 543 | |
kevin1990 | 7:4dbae381f693 | 544 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_SPI_CHIP_TYPE, &chipTypeReg, |
kevin1990 | 7:4dbae381f693 | 545 | sizeof(chipTypeReg)); |
kevin1990 | 7:4dbae381f693 | 546 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 547 | { |
kevin1990 | 7:4dbae381f693 | 548 | ADI_SENSE_LOG_ERROR("Failed to read chip-type register"); |
kevin1990 | 7:4dbae381f693 | 549 | return eRet; |
kevin1990 | 7:4dbae381f693 | 550 | } |
kevin1990 | 7:4dbae381f693 | 551 | |
kevin1990 | 7:4dbae381f693 | 552 | /* If we read this register successfully, assume the device is ready */ |
kevin1990 | 7:4dbae381f693 | 553 | *bReady = (chipTypeReg.VALUE8 == REG_ADISENSE_SPI_CHIP_TYPE_RESET); |
kevin1990 | 7:4dbae381f693 | 554 | |
kevin1990 | 7:4dbae381f693 | 555 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 556 | } |
kevin1990 | 7:4dbae381f693 | 557 | |
kevin1990 | 7:4dbae381f693 | 558 | ADI_SENSE_RESULT adi_sense_GetDataPublishingInfo( |
kevin1990 | 7:4dbae381f693 | 559 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 560 | ADI_SENSE_OPERATING_MODE * const peOperatingMode, |
kevin1990 | 7:4dbae381f693 | 561 | ADI_SENSE_DATA_PUBLISH_MODE * const peDataPublishMode, |
kevin1990 | 7:4dbae381f693 | 562 | uint32_t * const pnSamplesPerDataready, |
kevin1990 | 7:4dbae381f693 | 563 | uint32_t * const pnSamplesPerCycle) |
kevin1990 | 7:4dbae381f693 | 564 | { |
kevin1990 | 7:4dbae381f693 | 565 | ADI_ADISENSE_CORE_Channel_Count_t channelCountReg; |
kevin1990 | 7:4dbae381f693 | 566 | ADI_ADISENSE_CORE_Mode_t modeReg; |
kevin1990 | 7:4dbae381f693 | 567 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 568 | |
kevin1990 | 7:4dbae381f693 | 569 | unsigned nChannelsEnabled = 0; |
kevin1990 | 7:4dbae381f693 | 570 | unsigned nSamplesPerCycle = 0; |
kevin1990 | 7:4dbae381f693 | 571 | for (ADI_SENSE_CHANNEL_ID chId = 0; chId < ADI_SENSE_MAX_CHANNELS; chId++) |
kevin1990 | 7:4dbae381f693 | 572 | { |
kevin1990 | 7:4dbae381f693 | 573 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_CHANNEL_COUNTn(chId), |
kevin1990 | 7:4dbae381f693 | 574 | &channelCountReg, |
kevin1990 | 7:4dbae381f693 | 575 | sizeof(channelCountReg)); |
kevin1990 | 7:4dbae381f693 | 576 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 577 | return eRet; |
kevin1990 | 7:4dbae381f693 | 578 | |
kevin1990 | 7:4dbae381f693 | 579 | if (channelCountReg.Channel_Enable) |
kevin1990 | 7:4dbae381f693 | 580 | { |
kevin1990 | 7:4dbae381f693 | 581 | nChannelsEnabled++; |
kevin1990 | 7:4dbae381f693 | 582 | nSamplesPerCycle += (channelCountReg.Channel_Count + 1); |
kevin1990 | 7:4dbae381f693 | 583 | } |
kevin1990 | 7:4dbae381f693 | 584 | } |
kevin1990 | 7:4dbae381f693 | 585 | |
kevin1990 | 7:4dbae381f693 | 586 | if (nChannelsEnabled == 0) |
kevin1990 | 7:4dbae381f693 | 587 | { |
kevin1990 | 7:4dbae381f693 | 588 | *pnSamplesPerDataready = 0; |
kevin1990 | 7:4dbae381f693 | 589 | *pnSamplesPerCycle = 0; |
kevin1990 | 7:4dbae381f693 | 590 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 591 | } |
kevin1990 | 7:4dbae381f693 | 592 | |
kevin1990 | 7:4dbae381f693 | 593 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_MODE, |
kevin1990 | 7:4dbae381f693 | 594 | &modeReg, sizeof(modeReg)); |
kevin1990 | 7:4dbae381f693 | 595 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 596 | return eRet; |
kevin1990 | 7:4dbae381f693 | 597 | |
kevin1990 | 7:4dbae381f693 | 598 | *pnSamplesPerCycle = nSamplesPerCycle; |
kevin1990 | 7:4dbae381f693 | 599 | if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CONVERSION) |
kevin1990 | 7:4dbae381f693 | 600 | { |
kevin1990 | 7:4dbae381f693 | 601 | *pnSamplesPerDataready = 1; |
kevin1990 | 7:4dbae381f693 | 602 | } |
kevin1990 | 7:4dbae381f693 | 603 | else if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CYCLE) |
kevin1990 | 7:4dbae381f693 | 604 | { |
kevin1990 | 7:4dbae381f693 | 605 | *pnSamplesPerDataready = nSamplesPerCycle; |
kevin1990 | 7:4dbae381f693 | 606 | } |
kevin1990 | 7:4dbae381f693 | 607 | else |
kevin1990 | 7:4dbae381f693 | 608 | { |
kevin1990 | 7:4dbae381f693 | 609 | ADI_ADISENSE_CORE_Fifo_Num_Cycles_t fifoNumCyclesReg; |
kevin1990 | 7:4dbae381f693 | 610 | |
kevin1990 | 7:4dbae381f693 | 611 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_FIFO_NUM_CYCLES, |
kevin1990 | 7:4dbae381f693 | 612 | &fifoNumCyclesReg, |
kevin1990 | 7:4dbae381f693 | 613 | sizeof(fifoNumCyclesReg)); |
kevin1990 | 7:4dbae381f693 | 614 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 615 | return eRet; |
kevin1990 | 7:4dbae381f693 | 616 | |
kevin1990 | 7:4dbae381f693 | 617 | *pnSamplesPerDataready = |
kevin1990 | 7:4dbae381f693 | 618 | nSamplesPerCycle * fifoNumCyclesReg.Fifo_Num_Cycles; |
kevin1990 | 7:4dbae381f693 | 619 | } |
kevin1990 | 7:4dbae381f693 | 620 | |
kevin1990 | 7:4dbae381f693 | 621 | if (modeReg.Conversion_Mode == ADISENSE_CORE_MODE_SINGLECYCLE) |
kevin1990 | 7:4dbae381f693 | 622 | *peOperatingMode = ADI_SENSE_OPERATING_MODE_SINGLECYCLE; |
kevin1990 | 7:4dbae381f693 | 623 | else if (modeReg.Conversion_Mode == ADISENSE_CORE_MODE_MULTICYCLE) |
kevin1990 | 7:4dbae381f693 | 624 | *peOperatingMode = ADI_SENSE_OPERATING_MODE_MULTICYCLE; |
kevin1990 | 7:4dbae381f693 | 625 | else |
kevin1990 | 7:4dbae381f693 | 626 | *peOperatingMode = ADI_SENSE_OPERATING_MODE_CONTINUOUS; |
kevin1990 | 7:4dbae381f693 | 627 | |
kevin1990 | 7:4dbae381f693 | 628 | if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CONVERSION) |
kevin1990 | 7:4dbae381f693 | 629 | *peDataPublishMode = ADI_SENSE_DATA_PUBLISH_PER_CONVERSION; |
kevin1990 | 7:4dbae381f693 | 630 | else if (modeReg.Drdy_Mode == ADISENSE_CORE_MODE_DRDY_PER_CYCLE) |
kevin1990 | 7:4dbae381f693 | 631 | *peDataPublishMode = ADI_SENSE_DATA_PUBLISH_PER_CYCLE; |
kevin1990 | 7:4dbae381f693 | 632 | else |
kevin1990 | 7:4dbae381f693 | 633 | *peDataPublishMode = ADI_SENSE_DATA_PUBLISH_PER_MULTICYCLE_BURST; |
kevin1990 | 7:4dbae381f693 | 634 | |
kevin1990 | 7:4dbae381f693 | 635 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 636 | } |
kevin1990 | 7:4dbae381f693 | 637 | |
kevin1990 | 7:4dbae381f693 | 638 | ADI_SENSE_RESULT adi_sense_GetProductID( |
kevin1990 | 7:4dbae381f693 | 639 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 640 | ADI_SENSE_PRODUCT_ID *pProductId) |
kevin1990 | 7:4dbae381f693 | 641 | { |
kevin1990 | 7:4dbae381f693 | 642 | ADI_ADISENSE_SPI_Product_ID_L_t productIdLoReg; |
kevin1990 | 7:4dbae381f693 | 643 | ADI_ADISENSE_SPI_Product_ID_H_t productIdHiReg; |
kevin1990 | 7:4dbae381f693 | 644 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 645 | |
kevin1990 | 7:4dbae381f693 | 646 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_SPI_PRODUCT_ID_L, |
kevin1990 | 7:4dbae381f693 | 647 | &productIdLoReg, sizeof(productIdLoReg)); |
kevin1990 | 7:4dbae381f693 | 648 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 649 | return eRet; |
kevin1990 | 7:4dbae381f693 | 650 | |
kevin1990 | 7:4dbae381f693 | 651 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_SPI_PRODUCT_ID_H, |
kevin1990 | 7:4dbae381f693 | 652 | &productIdHiReg, sizeof(productIdHiReg)); |
kevin1990 | 7:4dbae381f693 | 653 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 654 | return eRet; |
kevin1990 | 7:4dbae381f693 | 655 | |
kevin1990 | 7:4dbae381f693 | 656 | *pProductId = (productIdHiReg.VALUE8 << 8) | productIdLoReg.VALUE8; |
kevin1990 | 7:4dbae381f693 | 657 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 658 | } |
kevin1990 | 7:4dbae381f693 | 659 | |
kevin1990 | 7:4dbae381f693 | 660 | static ADI_SENSE_RESULT adi_sense_SetPowerMode( |
kevin1990 | 7:4dbae381f693 | 661 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 662 | ADI_SENSE_POWER_MODE powerMode) |
kevin1990 | 7:4dbae381f693 | 663 | { |
kevin1990 | 7:4dbae381f693 | 664 | ADI_ADISENSE_CORE_Power_Config_t powerConfigReg; |
kevin1990 | 7:4dbae381f693 | 665 | |
kevin1990 | 7:4dbae381f693 | 666 | if (powerMode == ADI_SENSE_POWER_MODE_LOW) |
kevin1990 | 7:4dbae381f693 | 667 | { |
kevin1990 | 7:4dbae381f693 | 668 | powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_LOW_POWER; |
kevin1990 | 7:4dbae381f693 | 669 | /* TODO - we need an enum in the register map for the MCU power modes */ |
kevin1990 | 7:4dbae381f693 | 670 | powerConfigReg.Power_Mode_MCU = 0x0; |
kevin1990 | 7:4dbae381f693 | 671 | } |
kevin1990 | 7:4dbae381f693 | 672 | else if (powerMode == ADI_SENSE_POWER_MODE_MID) |
kevin1990 | 7:4dbae381f693 | 673 | { |
kevin1990 | 7:4dbae381f693 | 674 | powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_MID_POWER; |
kevin1990 | 7:4dbae381f693 | 675 | powerConfigReg.Power_Mode_MCU = 0x1; |
kevin1990 | 7:4dbae381f693 | 676 | } |
kevin1990 | 7:4dbae381f693 | 677 | else if (powerMode == ADI_SENSE_POWER_MODE_FULL) |
kevin1990 | 7:4dbae381f693 | 678 | { |
kevin1990 | 7:4dbae381f693 | 679 | powerConfigReg.Power_Mode_ADC = ADISENSE_CORE_POWER_CONFIG_ADC_FULL_POWER; |
kevin1990 | 7:4dbae381f693 | 680 | powerConfigReg.Power_Mode_MCU = 0x2; |
kevin1990 | 7:4dbae381f693 | 681 | } |
kevin1990 | 7:4dbae381f693 | 682 | else |
kevin1990 | 7:4dbae381f693 | 683 | { |
kevin1990 | 7:4dbae381f693 | 684 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 685 | } |
kevin1990 | 7:4dbae381f693 | 686 | |
kevin1990 | 7:4dbae381f693 | 687 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_POWER_CONFIG, |
kevin1990 | 7:4dbae381f693 | 688 | &powerConfigReg, sizeof(powerConfigReg)); |
kevin1990 | 7:4dbae381f693 | 689 | } |
kevin1990 | 7:4dbae381f693 | 690 | |
kevin1990 | 7:4dbae381f693 | 691 | static ADI_SENSE_RESULT adi_sense_SetVddVoltage( |
kevin1990 | 7:4dbae381f693 | 692 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 693 | float32_t vddVoltage) |
kevin1990 | 7:4dbae381f693 | 694 | { |
kevin1990 | 7:4dbae381f693 | 695 | ADI_ADISENSE_CORE_AVDD_Voltage_t avddVoltageReg = { |
kevin1990 | 7:4dbae381f693 | 696 | .Avdd_Voltage = vddVoltage |
kevin1990 | 7:4dbae381f693 | 697 | }; |
kevin1990 | 7:4dbae381f693 | 698 | |
kevin1990 | 7:4dbae381f693 | 699 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_AVDD_VOLTAGE, |
kevin1990 | 7:4dbae381f693 | 700 | &avddVoltageReg, sizeof(avddVoltageReg)); |
kevin1990 | 7:4dbae381f693 | 701 | } |
kevin1990 | 7:4dbae381f693 | 702 | |
kevin1990 | 7:4dbae381f693 | 703 | ADI_SENSE_RESULT adi_sense_SetPowerConfig( |
kevin1990 | 7:4dbae381f693 | 704 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 705 | ADI_SENSE_POWER_CONFIG *pPowerConfig) |
kevin1990 | 7:4dbae381f693 | 706 | { |
kevin1990 | 7:4dbae381f693 | 707 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 708 | |
kevin1990 | 7:4dbae381f693 | 709 | eRet = adi_sense_SetPowerMode(hDevice, pPowerConfig->powerMode); |
kevin1990 | 7:4dbae381f693 | 710 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 711 | { |
kevin1990 | 7:4dbae381f693 | 712 | return eRet; |
kevin1990 | 7:4dbae381f693 | 713 | } |
kevin1990 | 7:4dbae381f693 | 714 | |
kevin1990 | 7:4dbae381f693 | 715 | return adi_sense_SetVddVoltage(hDevice, pPowerConfig->supplyVoltage); |
kevin1990 | 7:4dbae381f693 | 716 | } |
kevin1990 | 7:4dbae381f693 | 717 | |
kevin1990 | 7:4dbae381f693 | 718 | static ADI_SENSE_RESULT adi_sense_SetMode( |
kevin1990 | 7:4dbae381f693 | 719 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 720 | ADI_SENSE_OPERATING_MODE eOperatingMode, |
kevin1990 | 7:4dbae381f693 | 721 | ADI_SENSE_DATA_PUBLISH_MODE ePublishMode) |
kevin1990 | 7:4dbae381f693 | 722 | { |
kevin1990 | 7:4dbae381f693 | 723 | ADI_ADISENSE_CORE_Mode_t modeReg = { .VALUE8 = 0 }; |
kevin1990 | 7:4dbae381f693 | 724 | |
kevin1990 | 7:4dbae381f693 | 725 | if (eOperatingMode == ADI_SENSE_OPERATING_MODE_SINGLECYCLE) |
kevin1990 | 7:4dbae381f693 | 726 | { |
kevin1990 | 7:4dbae381f693 | 727 | modeReg.Conversion_Mode = ADISENSE_CORE_MODE_SINGLECYCLE; |
kevin1990 | 7:4dbae381f693 | 728 | } |
kevin1990 | 7:4dbae381f693 | 729 | else if (eOperatingMode == ADI_SENSE_OPERATING_MODE_CONTINUOUS) |
kevin1990 | 7:4dbae381f693 | 730 | { |
kevin1990 | 7:4dbae381f693 | 731 | modeReg.Conversion_Mode = ADISENSE_CORE_MODE_CONTINUOUS; |
kevin1990 | 7:4dbae381f693 | 732 | } |
kevin1990 | 7:4dbae381f693 | 733 | else if (eOperatingMode == ADI_SENSE_OPERATING_MODE_MULTICYCLE) |
kevin1990 | 7:4dbae381f693 | 734 | { |
kevin1990 | 7:4dbae381f693 | 735 | modeReg.Conversion_Mode = ADISENSE_CORE_MODE_MULTICYCLE; |
kevin1990 | 7:4dbae381f693 | 736 | } |
kevin1990 | 7:4dbae381f693 | 737 | else |
kevin1990 | 7:4dbae381f693 | 738 | { |
kevin1990 | 7:4dbae381f693 | 739 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 740 | } |
kevin1990 | 7:4dbae381f693 | 741 | |
kevin1990 | 7:4dbae381f693 | 742 | if (ePublishMode == ADI_SENSE_DATA_PUBLISH_PER_CONVERSION) |
kevin1990 | 7:4dbae381f693 | 743 | { |
kevin1990 | 7:4dbae381f693 | 744 | modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_CONVERSION; |
kevin1990 | 7:4dbae381f693 | 745 | } |
kevin1990 | 7:4dbae381f693 | 746 | else if (ePublishMode == ADI_SENSE_DATA_PUBLISH_PER_CYCLE) |
kevin1990 | 7:4dbae381f693 | 747 | { |
kevin1990 | 7:4dbae381f693 | 748 | modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_CYCLE; |
kevin1990 | 7:4dbae381f693 | 749 | } |
kevin1990 | 7:4dbae381f693 | 750 | else if (ePublishMode == ADI_SENSE_DATA_PUBLISH_PER_MULTICYCLE_BURST) |
kevin1990 | 7:4dbae381f693 | 751 | { |
kevin1990 | 7:4dbae381f693 | 752 | if (eOperatingMode != ADI_SENSE_OPERATING_MODE_MULTICYCLE) |
kevin1990 | 7:4dbae381f693 | 753 | { |
kevin1990 | 7:4dbae381f693 | 754 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 755 | } |
kevin1990 | 7:4dbae381f693 | 756 | else |
kevin1990 | 7:4dbae381f693 | 757 | { |
kevin1990 | 7:4dbae381f693 | 758 | modeReg.Drdy_Mode = ADISENSE_CORE_MODE_DRDY_PER_FIFO_FILL; |
kevin1990 | 7:4dbae381f693 | 759 | } |
kevin1990 | 7:4dbae381f693 | 760 | } |
kevin1990 | 7:4dbae381f693 | 761 | else |
kevin1990 | 7:4dbae381f693 | 762 | { |
kevin1990 | 7:4dbae381f693 | 763 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 764 | } |
kevin1990 | 7:4dbae381f693 | 765 | |
kevin1990 | 7:4dbae381f693 | 766 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_MODE, |
kevin1990 | 7:4dbae381f693 | 767 | &modeReg, sizeof(modeReg)); |
kevin1990 | 7:4dbae381f693 | 768 | } |
kevin1990 | 7:4dbae381f693 | 769 | |
kevin1990 | 7:4dbae381f693 | 770 | ADI_SENSE_RESULT adi_sense_SetCycleInterval( |
kevin1990 | 7:4dbae381f693 | 771 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 772 | uint32_t nCycleInterval) |
kevin1990 | 7:4dbae381f693 | 773 | { |
kevin1990 | 7:4dbae381f693 | 774 | ADI_ADISENSE_CORE_Cycle_Control_t cycleControlReg; |
kevin1990 | 7:4dbae381f693 | 775 | |
kevin1990 | 7:4dbae381f693 | 776 | if (nCycleInterval < (1 << 12)) |
kevin1990 | 7:4dbae381f693 | 777 | { |
kevin1990 | 7:4dbae381f693 | 778 | cycleControlReg.Cycle_Time = nCycleInterval; |
kevin1990 | 7:4dbae381f693 | 779 | cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_MICROSECONDS; |
kevin1990 | 7:4dbae381f693 | 780 | } |
kevin1990 | 7:4dbae381f693 | 781 | else if (nCycleInterval < (1000 * (1 << 12))) |
kevin1990 | 7:4dbae381f693 | 782 | { |
kevin1990 | 7:4dbae381f693 | 783 | cycleControlReg.Cycle_Time = nCycleInterval / 1000; |
kevin1990 | 7:4dbae381f693 | 784 | cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_MILLISECONDS; |
kevin1990 | 7:4dbae381f693 | 785 | } |
kevin1990 | 7:4dbae381f693 | 786 | else |
kevin1990 | 7:4dbae381f693 | 787 | { |
kevin1990 | 7:4dbae381f693 | 788 | cycleControlReg.Cycle_Time = nCycleInterval / 1000000; |
kevin1990 | 7:4dbae381f693 | 789 | cycleControlReg.Cycle_Time_Units = ADISENSE_CORE_CYCLE_CONTROL_SECONDS; |
kevin1990 | 7:4dbae381f693 | 790 | } |
kevin1990 | 7:4dbae381f693 | 791 | |
kevin1990 | 7:4dbae381f693 | 792 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_CYCLE_CONTROL, |
kevin1990 | 7:4dbae381f693 | 793 | &cycleControlReg, sizeof(cycleControlReg)); |
kevin1990 | 7:4dbae381f693 | 794 | } |
kevin1990 | 7:4dbae381f693 | 795 | |
kevin1990 | 7:4dbae381f693 | 796 | static ADI_SENSE_RESULT adi_sense_SetMultiCycleConfig( |
kevin1990 | 7:4dbae381f693 | 797 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 798 | ADI_SENSE_MULTICYCLE_CONFIG *pMultiCycleConfig) |
kevin1990 | 7:4dbae381f693 | 799 | { |
kevin1990 | 7:4dbae381f693 | 800 | ADI_ADISENSE_CORE_Fifo_Num_Cycles_t fifoNumCyclesReg = { |
kevin1990 | 7:4dbae381f693 | 801 | .VALUE8 = REG_ADISENSE_CORE_FIFO_NUM_CYCLES_RESET |
kevin1990 | 7:4dbae381f693 | 802 | }; |
kevin1990 | 7:4dbae381f693 | 803 | ADI_ADISENSE_CORE_Multi_Cycle_Repeat_Interval_t multiCycleIntervalReg = { |
kevin1990 | 7:4dbae381f693 | 804 | .VALUE32 = REG_ADISENSE_CORE_MULTI_CYCLE_REPEAT_INTERVAL_RESET |
kevin1990 | 7:4dbae381f693 | 805 | }; |
kevin1990 | 7:4dbae381f693 | 806 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 807 | |
kevin1990 | 7:4dbae381f693 | 808 | fifoNumCyclesReg.Fifo_Num_Cycles = pMultiCycleConfig->cyclesPerBurst; |
kevin1990 | 7:4dbae381f693 | 809 | eRet = adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_FIFO_NUM_CYCLES, |
kevin1990 | 7:4dbae381f693 | 810 | &fifoNumCyclesReg, |
kevin1990 | 7:4dbae381f693 | 811 | sizeof(fifoNumCyclesReg)); |
kevin1990 | 7:4dbae381f693 | 812 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 813 | { |
kevin1990 | 7:4dbae381f693 | 814 | return eRet; |
kevin1990 | 7:4dbae381f693 | 815 | } |
kevin1990 | 7:4dbae381f693 | 816 | |
kevin1990 | 7:4dbae381f693 | 817 | multiCycleIntervalReg.Multi_Cycle_Repeat_Interval = pMultiCycleConfig->burstInterval; |
kevin1990 | 7:4dbae381f693 | 818 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_MULTI_CYCLE_REPEAT_INTERVAL, |
kevin1990 | 7:4dbae381f693 | 819 | &multiCycleIntervalReg, |
kevin1990 | 7:4dbae381f693 | 820 | sizeof(multiCycleIntervalReg)); |
kevin1990 | 7:4dbae381f693 | 821 | } |
kevin1990 | 7:4dbae381f693 | 822 | |
kevin1990 | 7:4dbae381f693 | 823 | static ADI_SENSE_RESULT adi_sense_SetExternalReferenceValues( |
kevin1990 | 7:4dbae381f693 | 824 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 825 | float32_t externalRef1Value, |
kevin1990 | 7:4dbae381f693 | 826 | float32_t externalRef2Value) |
kevin1990 | 7:4dbae381f693 | 827 | { |
kevin1990 | 7:4dbae381f693 | 828 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 829 | ADI_ADISENSE_CORE_External_Reference1_t externalReference1Reg = { |
kevin1990 | 7:4dbae381f693 | 830 | .Ext_Refin1_Value = externalRef1Value |
kevin1990 | 7:4dbae381f693 | 831 | }; |
kevin1990 | 7:4dbae381f693 | 832 | ADI_ADISENSE_CORE_External_Reference2_t externalReference2Reg = { |
kevin1990 | 7:4dbae381f693 | 833 | .Ext_Refin2_Value = externalRef2Value |
kevin1990 | 7:4dbae381f693 | 834 | }; |
kevin1990 | 7:4dbae381f693 | 835 | |
kevin1990 | 7:4dbae381f693 | 836 | eRet = adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_EXTERNAL_REFERENCE1, |
kevin1990 | 7:4dbae381f693 | 837 | &externalReference1Reg, |
kevin1990 | 7:4dbae381f693 | 838 | sizeof(externalReference1Reg)); |
kevin1990 | 7:4dbae381f693 | 839 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 840 | { |
kevin1990 | 7:4dbae381f693 | 841 | return eRet; |
kevin1990 | 7:4dbae381f693 | 842 | } |
kevin1990 | 7:4dbae381f693 | 843 | |
kevin1990 | 7:4dbae381f693 | 844 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_EXTERNAL_REFERENCE2, |
kevin1990 | 7:4dbae381f693 | 845 | &externalReference2Reg, |
kevin1990 | 7:4dbae381f693 | 846 | sizeof(externalReference2Reg)); |
kevin1990 | 7:4dbae381f693 | 847 | } |
kevin1990 | 7:4dbae381f693 | 848 | |
kevin1990 | 7:4dbae381f693 | 849 | ADI_SENSE_RESULT adi_sense_SetMeasurementConfig( |
kevin1990 | 7:4dbae381f693 | 850 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 851 | ADI_SENSE_MEASUREMENT_CONFIG *pMeasConfig) |
kevin1990 | 7:4dbae381f693 | 852 | { |
kevin1990 | 7:4dbae381f693 | 853 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 854 | |
kevin1990 | 7:4dbae381f693 | 855 | eRet = adi_sense_SetMode(hDevice, |
kevin1990 | 7:4dbae381f693 | 856 | pMeasConfig->operatingMode, |
kevin1990 | 7:4dbae381f693 | 857 | pMeasConfig->dataPublishMode); |
kevin1990 | 7:4dbae381f693 | 858 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 859 | { |
kevin1990 | 7:4dbae381f693 | 860 | return eRet; |
kevin1990 | 7:4dbae381f693 | 861 | } |
kevin1990 | 7:4dbae381f693 | 862 | |
kevin1990 | 7:4dbae381f693 | 863 | if (pMeasConfig->operatingMode != ADI_SENSE_OPERATING_MODE_SINGLECYCLE) |
kevin1990 | 7:4dbae381f693 | 864 | { |
kevin1990 | 7:4dbae381f693 | 865 | eRet = adi_sense_SetCycleInterval(hDevice, pMeasConfig->cycleInterval); |
kevin1990 | 7:4dbae381f693 | 866 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 867 | { |
kevin1990 | 7:4dbae381f693 | 868 | return eRet; |
kevin1990 | 7:4dbae381f693 | 869 | } |
kevin1990 | 7:4dbae381f693 | 870 | } |
kevin1990 | 7:4dbae381f693 | 871 | |
kevin1990 | 7:4dbae381f693 | 872 | if (pMeasConfig->operatingMode == ADI_SENSE_OPERATING_MODE_MULTICYCLE) |
kevin1990 | 7:4dbae381f693 | 873 | { |
kevin1990 | 7:4dbae381f693 | 874 | eRet = adi_sense_SetMultiCycleConfig(hDevice, |
kevin1990 | 7:4dbae381f693 | 875 | &pMeasConfig->multiCycleConfig); |
kevin1990 | 7:4dbae381f693 | 876 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 877 | { |
kevin1990 | 7:4dbae381f693 | 878 | return eRet; |
kevin1990 | 7:4dbae381f693 | 879 | } |
kevin1990 | 7:4dbae381f693 | 880 | } |
kevin1990 | 7:4dbae381f693 | 881 | |
kevin1990 | 7:4dbae381f693 | 882 | return adi_sense_SetExternalReferenceValues(hDevice, |
kevin1990 | 7:4dbae381f693 | 883 | pMeasConfig->externalRef1Value, |
kevin1990 | 7:4dbae381f693 | 884 | pMeasConfig->externalRef2Value); |
kevin1990 | 7:4dbae381f693 | 885 | } |
kevin1990 | 7:4dbae381f693 | 886 | |
kevin1990 | 7:4dbae381f693 | 887 | ADI_SENSE_RESULT adi_sense_SetDiagnosticsConfig( |
kevin1990 | 7:4dbae381f693 | 888 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 889 | ADI_SENSE_DIAGNOSTICS_CONFIG *pDiagnosticsConfig) |
kevin1990 | 7:4dbae381f693 | 890 | { |
kevin1990 | 7:4dbae381f693 | 891 | ADI_ADISENSE_CORE_Diagnostics_Control_t diagnosticsControlReg = { |
kevin1990 | 7:4dbae381f693 | 892 | .VALUE16 = REG_ADISENSE_CORE_DIAGNOSTICS_CONTROL_RESET |
kevin1990 | 7:4dbae381f693 | 893 | }; |
kevin1990 | 7:4dbae381f693 | 894 | |
kevin1990 | 7:4dbae381f693 | 895 | if (pDiagnosticsConfig->enableGlobalDiag) |
kevin1990 | 7:4dbae381f693 | 896 | diagnosticsControlReg.Diag_Global_En = 1; |
kevin1990 | 7:4dbae381f693 | 897 | |
kevin1990 | 7:4dbae381f693 | 898 | if (pDiagnosticsConfig->enableMeasurementDiag) |
kevin1990 | 7:4dbae381f693 | 899 | { |
kevin1990 | 7:4dbae381f693 | 900 | diagnosticsControlReg.Diag_Meas_En = 1; |
kevin1990 | 7:4dbae381f693 | 901 | |
kevin1990 | 7:4dbae381f693 | 902 | switch (pDiagnosticsConfig->openCircuitDetectionFreq) |
kevin1990 | 7:4dbae381f693 | 903 | { |
kevin1990 | 7:4dbae381f693 | 904 | case ADI_SENSE_OCD_DISABLED: |
kevin1990 | 7:4dbae381f693 | 905 | diagnosticsControlReg.Diag_OCD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_OFF; |
kevin1990 | 7:4dbae381f693 | 906 | break; |
kevin1990 | 7:4dbae381f693 | 907 | case ADI_SENSE_OCD_PER_CYCLE: |
kevin1990 | 7:4dbae381f693 | 908 | diagnosticsControlReg.Diag_OCD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_1_CYCLE; |
kevin1990 | 7:4dbae381f693 | 909 | break; |
kevin1990 | 7:4dbae381f693 | 910 | case ADI_SENSE_OCD_PER_100_CYCLES: |
kevin1990 | 7:4dbae381f693 | 911 | diagnosticsControlReg.Diag_OCD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_100_CYCLES; |
kevin1990 | 7:4dbae381f693 | 912 | break; |
kevin1990 | 7:4dbae381f693 | 913 | case ADI_SENSE_OCD_PER_1000_CYCLES: |
kevin1990 | 7:4dbae381f693 | 914 | diagnosticsControlReg.Diag_OCD_Freq = ADISENSE_CORE_DIAGNOSTICS_CONTROL_OCD_PER_1000_CYCLES; |
kevin1990 | 7:4dbae381f693 | 915 | break; |
kevin1990 | 7:4dbae381f693 | 916 | default: |
kevin1990 | 7:4dbae381f693 | 917 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 918 | } |
kevin1990 | 7:4dbae381f693 | 919 | } |
kevin1990 | 7:4dbae381f693 | 920 | |
kevin1990 | 7:4dbae381f693 | 921 | /* TODO - what should be set in the REG_ADISENSE_CORE_DIAGNOSTICS_EXTRA register? */ |
kevin1990 | 7:4dbae381f693 | 922 | |
kevin1990 | 7:4dbae381f693 | 923 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_DIAGNOSTICS_CONTROL, |
kevin1990 | 7:4dbae381f693 | 924 | &diagnosticsControlReg, |
kevin1990 | 7:4dbae381f693 | 925 | sizeof(diagnosticsControlReg)); |
kevin1990 | 7:4dbae381f693 | 926 | } |
kevin1990 | 7:4dbae381f693 | 927 | |
kevin1990 | 7:4dbae381f693 | 928 | ADI_SENSE_RESULT adi_sense_SetChannelCount( |
kevin1990 | 7:4dbae381f693 | 929 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 930 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 931 | uint32_t nMeasurementsPerCycle) |
kevin1990 | 7:4dbae381f693 | 932 | { |
kevin1990 | 7:4dbae381f693 | 933 | ADI_ADISENSE_CORE_Channel_Count_t channelCountReg = { |
kevin1990 | 7:4dbae381f693 | 934 | .VALUE8 = REG_ADISENSE_CORE_CHANNEL_COUNTn_RESET |
kevin1990 | 7:4dbae381f693 | 935 | }; |
kevin1990 | 7:4dbae381f693 | 936 | |
kevin1990 | 7:4dbae381f693 | 937 | if (nMeasurementsPerCycle > 0) |
kevin1990 | 7:4dbae381f693 | 938 | { |
kevin1990 | 7:4dbae381f693 | 939 | channelCountReg.Channel_Enable = 1; |
kevin1990 | 7:4dbae381f693 | 940 | channelCountReg.Channel_Count = nMeasurementsPerCycle - 1; |
kevin1990 | 7:4dbae381f693 | 941 | } |
kevin1990 | 7:4dbae381f693 | 942 | else |
kevin1990 | 7:4dbae381f693 | 943 | { |
kevin1990 | 7:4dbae381f693 | 944 | channelCountReg.Channel_Enable = 0; |
kevin1990 | 7:4dbae381f693 | 945 | } |
kevin1990 | 7:4dbae381f693 | 946 | |
kevin1990 | 7:4dbae381f693 | 947 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_CHANNEL_COUNTn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 948 | &channelCountReg, sizeof(channelCountReg)); |
kevin1990 | 7:4dbae381f693 | 949 | } |
kevin1990 | 7:4dbae381f693 | 950 | |
kevin1990 | 7:4dbae381f693 | 951 | static ADI_SENSE_RESULT adi_sense_SetChannelAdcSensorType( |
kevin1990 | 7:4dbae381f693 | 952 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 953 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 954 | ADI_SENSE_ADC_SENSOR_TYPE sensorType) |
kevin1990 | 7:4dbae381f693 | 955 | { |
kevin1990 | 7:4dbae381f693 | 956 | ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg = { |
kevin1990 | 7:4dbae381f693 | 957 | .VALUE16 = REG_ADISENSE_CORE_SENSOR_TYPEn_RESET |
kevin1990 | 7:4dbae381f693 | 958 | }; |
kevin1990 | 7:4dbae381f693 | 959 | |
kevin1990 | 7:4dbae381f693 | 960 | /* Ensure that the sensor type is valid for this channel */ |
kevin1990 | 7:4dbae381f693 | 961 | switch(sensorType) |
kevin1990 | 7:4dbae381f693 | 962 | { |
kevin1990 | 7:4dbae381f693 | 963 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_J: |
kevin1990 | 7:4dbae381f693 | 964 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_K: |
kevin1990 | 7:4dbae381f693 | 965 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_T: |
kevin1990 | 7:4dbae381f693 | 966 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 967 | case ADI_SENSE_ADC_SENSOR_RTD_3WIRE_PT100: |
kevin1990 | 7:4dbae381f693 | 968 | case ADI_SENSE_ADC_SENSOR_RTD_3WIRE_PT1000: |
kevin1990 | 7:4dbae381f693 | 969 | case ADI_SENSE_ADC_SENSOR_RTD_3WIRE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 970 | case ADI_SENSE_ADC_SENSOR_RTD_4WIRE_PT100: |
kevin1990 | 7:4dbae381f693 | 971 | case ADI_SENSE_ADC_SENSOR_RTD_4WIRE_PT1000: |
kevin1990 | 7:4dbae381f693 | 972 | case ADI_SENSE_ADC_SENSOR_RTD_4WIRE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 973 | case ADI_SENSE_ADC_SENSOR_BRIDGE_4WIRE_TRANSDUCER: |
kevin1990 | 7:4dbae381f693 | 974 | case ADI_SENSE_ADC_SENSOR_BRIDGE_4WIRE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 975 | case ADI_SENSE_ADC_SENSOR_BRIDGE_6WIRE_TRANSDUCER: |
kevin1990 | 7:4dbae381f693 | 976 | case ADI_SENSE_ADC_SENSOR_BRIDGE_6WIRE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 977 | case ADI_SENSE_ADC_SENSOR_THERMISTOR_10K_NTC: |
kevin1990 | 7:4dbae381f693 | 978 | case ADI_SENSE_ADC_SENSOR_THERMISTOR_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 979 | if (! ADI_SENSE_CHANNEL_IS_ADC_SENSOR(eChannelId)) |
kevin1990 | 7:4dbae381f693 | 980 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 981 | break; |
kevin1990 | 7:4dbae381f693 | 982 | case ADI_SENSE_ADC_SENSOR_RTD_2WIRE_PT100: |
kevin1990 | 7:4dbae381f693 | 983 | case ADI_SENSE_ADC_SENSOR_RTD_2WIRE_PT1000: |
kevin1990 | 7:4dbae381f693 | 984 | case ADI_SENSE_ADC_SENSOR_RTD_2WIRE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 985 | if (! ADI_SENSE_CHANNEL_IS_ADC_SENSOR(eChannelId) && |
kevin1990 | 7:4dbae381f693 | 986 | ! ADI_SENSE_CHANNEL_IS_ADC_CJC(eChannelId)) |
kevin1990 | 7:4dbae381f693 | 987 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 988 | break; |
kevin1990 | 7:4dbae381f693 | 989 | case ADI_SENSE_ADC_SENSOR_VOLTAGE_PRESSURE_1: |
kevin1990 | 7:4dbae381f693 | 990 | case ADI_SENSE_ADC_SENSOR_VOLTAGE_PRESSURE_2: |
kevin1990 | 7:4dbae381f693 | 991 | case ADI_SENSE_ADC_SENSOR_VOLTAGE_PRESSURE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 992 | if (! ADI_SENSE_CHANNEL_IS_ADC_VOLTAGE(eChannelId)) |
kevin1990 | 7:4dbae381f693 | 993 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 994 | break; |
kevin1990 | 7:4dbae381f693 | 995 | case ADI_SENSE_ADC_SENSOR_CURRENT_PRESSURE_1: |
kevin1990 | 7:4dbae381f693 | 996 | case ADI_SENSE_ADC_SENSOR_CURRENT_PRESSURE_CUSTOM: |
kevin1990 | 7:4dbae381f693 | 997 | if (! ADI_SENSE_CHANNEL_IS_ADC_CURRENT(eChannelId)) |
kevin1990 | 7:4dbae381f693 | 998 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 999 | break; |
kevin1990 | 7:4dbae381f693 | 1000 | default: |
kevin1990 | 7:4dbae381f693 | 1001 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1002 | } |
kevin1990 | 7:4dbae381f693 | 1003 | |
kevin1990 | 7:4dbae381f693 | 1004 | switch(sensorType) |
kevin1990 | 7:4dbae381f693 | 1005 | { |
kevin1990 | 7:4dbae381f693 | 1006 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_T: |
kevin1990 | 7:4dbae381f693 | 1007 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_T_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1008 | break; |
kevin1990 | 7:4dbae381f693 | 1009 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_J: |
kevin1990 | 7:4dbae381f693 | 1010 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_J_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1011 | break; |
kevin1990 | 7:4dbae381f693 | 1012 | case ADI_SENSE_ADC_SENSOR_THERMOCOUPLE_K: |
kevin1990 | 7:4dbae381f693 | 1013 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_THERMOCOUPLE_K_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1014 | break; |
kevin1990 | 7:4dbae381f693 | 1015 | case ADI_SENSE_ADC_SENSOR_RTD_2WIRE_PT100: |
kevin1990 | 7:4dbae381f693 | 1016 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_RTD_2W_PT100_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1017 | break; |
kevin1990 | 7:4dbae381f693 | 1018 | case ADI_SENSE_ADC_SENSOR_RTD_2WIRE_PT1000: |
kevin1990 | 7:4dbae381f693 | 1019 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_RTD_2W_PT1000_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1020 | break; |
kevin1990 | 7:4dbae381f693 | 1021 | case ADI_SENSE_ADC_SENSOR_RTD_3WIRE_PT100: |
kevin1990 | 7:4dbae381f693 | 1022 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_RTD_3W_PT100_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1023 | break; |
kevin1990 | 7:4dbae381f693 | 1024 | case ADI_SENSE_ADC_SENSOR_RTD_3WIRE_PT1000: |
kevin1990 | 7:4dbae381f693 | 1025 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_RTD_3W_PT1000_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1026 | break; |
kevin1990 | 7:4dbae381f693 | 1027 | case ADI_SENSE_ADC_SENSOR_RTD_4WIRE_PT100: |
kevin1990 | 7:4dbae381f693 | 1028 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_RTD_4W_PT100_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1029 | break; |
kevin1990 | 7:4dbae381f693 | 1030 | case ADI_SENSE_ADC_SENSOR_RTD_4WIRE_PT1000: |
kevin1990 | 7:4dbae381f693 | 1031 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_RTD_4W_PT1000_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1032 | break; |
kevin1990 | 7:4dbae381f693 | 1033 | case ADI_SENSE_ADC_SENSOR_BRIDGE_4WIRE_TRANSDUCER: |
kevin1990 | 7:4dbae381f693 | 1034 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_BRIDGE_4W_1_DEF_L2; |
kevin1990 | 7:4dbae381f693 | 1035 | break; |
kevin1990 | 7:4dbae381f693 | 1036 | case ADI_SENSE_ADC_SENSOR_BRIDGE_6WIRE_TRANSDUCER: |
kevin1990 | 7:4dbae381f693 | 1037 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_BRIDGE_6W_1_DEF_L2; |
kevin1990 | 7:4dbae381f693 | 1038 | break; |
kevin1990 | 7:4dbae381f693 | 1039 | case ADI_SENSE_ADC_SENSOR_THERMISTOR_10K_NTC: |
kevin1990 | 7:4dbae381f693 | 1040 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_THERMISTOR_A_10K_DEF_L1; |
kevin1990 | 7:4dbae381f693 | 1041 | break; |
kevin1990 | 7:4dbae381f693 | 1042 | case ADI_SENSE_ADC_SENSOR_VOLTAGE_PRESSURE_1: |
kevin1990 | 7:4dbae381f693 | 1043 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_VOLTAGE_PRESSURE_HONEYWELL_TRUSTABILITY; |
kevin1990 | 7:4dbae381f693 | 1044 | break; |
kevin1990 | 7:4dbae381f693 | 1045 | case ADI_SENSE_ADC_SENSOR_VOLTAGE_PRESSURE_2: |
kevin1990 | 7:4dbae381f693 | 1046 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_VOLTAGE_PRESSURE_AMPHENOL_NPA300X; |
kevin1990 | 7:4dbae381f693 | 1047 | break; |
kevin1990 | 7:4dbae381f693 | 1048 | case ADI_SENSE_ADC_SENSOR_CURRENT_PRESSURE_1: |
kevin1990 | 7:4dbae381f693 | 1049 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_CURRENT_PRESSURE_HONEYWELL_PX2; |
kevin1990 | 7:4dbae381f693 | 1050 | break; |
kevin1990 | 7:4dbae381f693 | 1051 | default: |
kevin1990 | 7:4dbae381f693 | 1052 | /* TODO - add support for custom sensor types? */ |
kevin1990 | 7:4dbae381f693 | 1053 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1054 | } |
kevin1990 | 7:4dbae381f693 | 1055 | |
kevin1990 | 7:4dbae381f693 | 1056 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_SENSOR_TYPEn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1057 | &sensorTypeReg, sizeof(sensorTypeReg)); |
kevin1990 | 7:4dbae381f693 | 1058 | } |
kevin1990 | 7:4dbae381f693 | 1059 | |
kevin1990 | 7:4dbae381f693 | 1060 | static ADI_SENSE_RESULT adi_sense_SetChannelAdcSensorDetails( |
kevin1990 | 7:4dbae381f693 | 1061 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1062 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1063 | ADI_SENSE_CHANNEL_CONFIG *pChannelConfig) |
kevin1990 | 7:4dbae381f693 | 1064 | /* |
kevin1990 | 7:4dbae381f693 | 1065 | * TODO - it would be nice if the general- vs. ADC-specific sensor details could be split into separate registers |
kevin1990 | 7:4dbae381f693 | 1066 | * General details: |
kevin1990 | 7:4dbae381f693 | 1067 | * - Measurement_Units |
kevin1990 | 7:4dbae381f693 | 1068 | * - Compensation_Channel |
kevin1990 | 7:4dbae381f693 | 1069 | * - CJC_Publish (if "CJC" was removed from the name) |
kevin1990 | 7:4dbae381f693 | 1070 | * ADC-specific details: |
kevin1990 | 7:4dbae381f693 | 1071 | * - PGA_Gain |
kevin1990 | 7:4dbae381f693 | 1072 | * - Reference_Select |
kevin1990 | 7:4dbae381f693 | 1073 | * - Reference_Buffer_Disable |
kevin1990 | 7:4dbae381f693 | 1074 | * - Vbias |
kevin1990 | 7:4dbae381f693 | 1075 | */ |
kevin1990 | 7:4dbae381f693 | 1076 | { |
kevin1990 | 7:4dbae381f693 | 1077 | ADI_SENSE_ADC_CHANNEL_CONFIG *pAdcChannelConfig = &pChannelConfig->adcChannelConfig; |
kevin1990 | 7:4dbae381f693 | 1078 | ADI_SENSE_ADC_REFERENCE_CONFIG *pRefConfig = &pAdcChannelConfig->reference; |
kevin1990 | 7:4dbae381f693 | 1079 | ADI_ADISENSE_CORE_Sensor_Details_t sensorDetailsReg = { |
kevin1990 | 7:4dbae381f693 | 1080 | .VALUE32 = REG_ADISENSE_CORE_SENSOR_DETAILSn_RESET |
kevin1990 | 7:4dbae381f693 | 1081 | }; |
kevin1990 | 7:4dbae381f693 | 1082 | |
kevin1990 | 7:4dbae381f693 | 1083 | /* TODO - measurementType will most likely be replaced with a unit-conversion option */ |
kevin1990 | 7:4dbae381f693 | 1084 | #if 0 |
kevin1990 | 7:4dbae381f693 | 1085 | switch(pChannelConfig->eMeasurementType) |
kevin1990 | 7:4dbae381f693 | 1086 | { |
kevin1990 | 7:4dbae381f693 | 1087 | case ADI_SENSE_MEASUREMENT_TEMPERATURE_CELCIUS: |
kevin1990 | 7:4dbae381f693 | 1088 | sensorDetailsReg.Measurement_Units = CORE_SENSOR_DETAILS_UNITS_DEGC; |
kevin1990 | 7:4dbae381f693 | 1089 | break; |
kevin1990 | 7:4dbae381f693 | 1090 | case ADI_SENSE_MEASUREMENT_TEMPERATURE_FAHRENHEIT: |
kevin1990 | 7:4dbae381f693 | 1091 | sensorDetailsReg.Measurement_Units = CORE_SENSOR_DETAILS_UNITS_DEGF; |
kevin1990 | 7:4dbae381f693 | 1092 | break; |
kevin1990 | 7:4dbae381f693 | 1093 | /* TODO - register map needs to define measurement units for the following options: */ |
kevin1990 | 7:4dbae381f693 | 1094 | case ADI_SENSE_MEASUREMENT_HUMIDITY_RH: |
kevin1990 | 7:4dbae381f693 | 1095 | case ADI_SENSE_MEASUREMENT_PRESSURE_BAR: |
kevin1990 | 7:4dbae381f693 | 1096 | case ADI_SENSE_MEASUREMENT_PRESSURE_PSI: |
kevin1990 | 7:4dbae381f693 | 1097 | case ADI_SENSE_MEASUREMENT_ACCELERATION_G: |
kevin1990 | 7:4dbae381f693 | 1098 | default: |
kevin1990 | 7:4dbae381f693 | 1099 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1100 | } |
kevin1990 | 7:4dbae381f693 | 1101 | #endif |
kevin1990 | 7:4dbae381f693 | 1102 | |
kevin1990 | 7:4dbae381f693 | 1103 | sensorDetailsReg.Compensation_Channel = pChannelConfig->compensationChannel; |
kevin1990 | 7:4dbae381f693 | 1104 | |
kevin1990 | 7:4dbae381f693 | 1105 | switch(pRefConfig->type) |
kevin1990 | 7:4dbae381f693 | 1106 | { |
kevin1990 | 7:4dbae381f693 | 1107 | case ADI_SENSE_ADC_REFERENCE_RESISTOR_INTERNAL_1: |
kevin1990 | 7:4dbae381f693 | 1108 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_RINT1; |
kevin1990 | 7:4dbae381f693 | 1109 | break; |
kevin1990 | 7:4dbae381f693 | 1110 | case ADI_SENSE_ADC_REFERENCE_RESISTOR_INTERNAL_2: |
kevin1990 | 7:4dbae381f693 | 1111 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_RINT2; |
kevin1990 | 7:4dbae381f693 | 1112 | break; |
kevin1990 | 7:4dbae381f693 | 1113 | case ADI_SENSE_ADC_REFERENCE_VOLTAGE_INTERNAL: |
kevin1990 | 7:4dbae381f693 | 1114 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_INT; |
kevin1990 | 7:4dbae381f693 | 1115 | break; |
kevin1990 | 7:4dbae381f693 | 1116 | case ADI_SENSE_ADC_REFERENCE_VOLTAGE_AVDD: |
kevin1990 | 7:4dbae381f693 | 1117 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_AVDD; |
kevin1990 | 7:4dbae381f693 | 1118 | break; |
kevin1990 | 7:4dbae381f693 | 1119 | case ADI_SENSE_ADC_REFERENCE_RESISTOR_EXTERNAL_1: |
kevin1990 | 7:4dbae381f693 | 1120 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_REXT1; |
kevin1990 | 7:4dbae381f693 | 1121 | break; |
kevin1990 | 7:4dbae381f693 | 1122 | case ADI_SENSE_ADC_REFERENCE_RESISTOR_EXTERNAL_2: |
kevin1990 | 7:4dbae381f693 | 1123 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_REXT2; |
kevin1990 | 7:4dbae381f693 | 1124 | break; |
kevin1990 | 7:4dbae381f693 | 1125 | case ADI_SENSE_ADC_REFERENCE_VOLTAGE_EXTERNAL_1: |
kevin1990 | 7:4dbae381f693 | 1126 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_VEXT1; |
kevin1990 | 7:4dbae381f693 | 1127 | break; |
kevin1990 | 7:4dbae381f693 | 1128 | case ADI_SENSE_ADC_REFERENCE_VOLTAGE_EXTERNAL_2: |
kevin1990 | 7:4dbae381f693 | 1129 | sensorDetailsReg.Reference_Select = ADISENSE_CORE_SENSOR_DETAILS_REF_VEXT2; |
kevin1990 | 7:4dbae381f693 | 1130 | break; |
kevin1990 | 7:4dbae381f693 | 1131 | default: |
kevin1990 | 7:4dbae381f693 | 1132 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1133 | } |
kevin1990 | 7:4dbae381f693 | 1134 | |
kevin1990 | 7:4dbae381f693 | 1135 | switch(pAdcChannelConfig->gain) |
kevin1990 | 7:4dbae381f693 | 1136 | { |
kevin1990 | 7:4dbae381f693 | 1137 | case ADI_SENSE_ADC_GAIN_1X: |
kevin1990 | 7:4dbae381f693 | 1138 | /* TODO - add enum for gain options in register map headers */ |
kevin1990 | 7:4dbae381f693 | 1139 | sensorDetailsReg.PGA_Gain = 0; |
kevin1990 | 7:4dbae381f693 | 1140 | break; |
kevin1990 | 7:4dbae381f693 | 1141 | case ADI_SENSE_ADC_GAIN_2X: |
kevin1990 | 7:4dbae381f693 | 1142 | sensorDetailsReg.PGA_Gain = 1; |
kevin1990 | 7:4dbae381f693 | 1143 | break; |
kevin1990 | 7:4dbae381f693 | 1144 | case ADI_SENSE_ADC_GAIN_4X: |
kevin1990 | 7:4dbae381f693 | 1145 | sensorDetailsReg.PGA_Gain = 2; |
kevin1990 | 7:4dbae381f693 | 1146 | break; |
kevin1990 | 7:4dbae381f693 | 1147 | case ADI_SENSE_ADC_GAIN_8X: |
kevin1990 | 7:4dbae381f693 | 1148 | sensorDetailsReg.PGA_Gain = 3; |
kevin1990 | 7:4dbae381f693 | 1149 | break; |
kevin1990 | 7:4dbae381f693 | 1150 | case ADI_SENSE_ADC_GAIN_16X: |
kevin1990 | 7:4dbae381f693 | 1151 | sensorDetailsReg.PGA_Gain = 4; |
kevin1990 | 7:4dbae381f693 | 1152 | break; |
kevin1990 | 7:4dbae381f693 | 1153 | case ADI_SENSE_ADC_GAIN_32X: |
kevin1990 | 7:4dbae381f693 | 1154 | sensorDetailsReg.PGA_Gain = 5; |
kevin1990 | 7:4dbae381f693 | 1155 | break; |
kevin1990 | 7:4dbae381f693 | 1156 | case ADI_SENSE_ADC_GAIN_64X: |
kevin1990 | 7:4dbae381f693 | 1157 | sensorDetailsReg.PGA_Gain = 6; |
kevin1990 | 7:4dbae381f693 | 1158 | break; |
kevin1990 | 7:4dbae381f693 | 1159 | case ADI_SENSE_ADC_GAIN_128X: |
kevin1990 | 7:4dbae381f693 | 1160 | sensorDetailsReg.PGA_Gain = 7; |
kevin1990 | 7:4dbae381f693 | 1161 | break; |
kevin1990 | 7:4dbae381f693 | 1162 | default: |
kevin1990 | 7:4dbae381f693 | 1163 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1164 | } |
kevin1990 | 7:4dbae381f693 | 1165 | |
kevin1990 | 7:4dbae381f693 | 1166 | if (pAdcChannelConfig->enableVbias) |
kevin1990 | 7:4dbae381f693 | 1167 | sensorDetailsReg.Vbias = 1; |
kevin1990 | 7:4dbae381f693 | 1168 | else |
kevin1990 | 7:4dbae381f693 | 1169 | sensorDetailsReg.Vbias = 0; |
kevin1990 | 7:4dbae381f693 | 1170 | |
kevin1990 | 7:4dbae381f693 | 1171 | if (pAdcChannelConfig->reference.disableBuffer) |
kevin1990 | 7:4dbae381f693 | 1172 | sensorDetailsReg.Reference_Buffer_Disable = 1; |
kevin1990 | 7:4dbae381f693 | 1173 | else |
kevin1990 | 7:4dbae381f693 | 1174 | sensorDetailsReg.Reference_Buffer_Disable = 0; |
kevin1990 | 7:4dbae381f693 | 1175 | |
kevin1990 | 7:4dbae381f693 | 1176 | if (pChannelConfig->disablePublishing) |
kevin1990 | 7:4dbae381f693 | 1177 | sensorDetailsReg.Do_Not_Publish = 1; |
kevin1990 | 7:4dbae381f693 | 1178 | else |
kevin1990 | 7:4dbae381f693 | 1179 | sensorDetailsReg.Do_Not_Publish = 0; |
kevin1990 | 7:4dbae381f693 | 1180 | |
kevin1990 | 7:4dbae381f693 | 1181 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_SENSOR_DETAILSn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1182 | &sensorDetailsReg, sizeof(sensorDetailsReg)); |
kevin1990 | 7:4dbae381f693 | 1183 | } |
kevin1990 | 7:4dbae381f693 | 1184 | |
kevin1990 | 7:4dbae381f693 | 1185 | static ADI_SENSE_RESULT adi_sense_SetChannelAdcFilter( |
kevin1990 | 7:4dbae381f693 | 1186 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1187 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1188 | ADI_SENSE_ADC_FILTER_CONFIG *pFilterConfig) |
kevin1990 | 7:4dbae381f693 | 1189 | { |
kevin1990 | 7:4dbae381f693 | 1190 | ADI_ADISENSE_CORE_Filter_Select_t filterSelectReg = { |
kevin1990 | 7:4dbae381f693 | 1191 | .VALUE32 = REG_ADISENSE_CORE_FILTER_SELECTn_RESET |
kevin1990 | 7:4dbae381f693 | 1192 | }; |
kevin1990 | 7:4dbae381f693 | 1193 | |
kevin1990 | 7:4dbae381f693 | 1194 | if (pFilterConfig->type == ADI_SENSE_ADC_FILTER_SINC4) |
kevin1990 | 7:4dbae381f693 | 1195 | { |
kevin1990 | 7:4dbae381f693 | 1196 | filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_SINC4; |
kevin1990 | 7:4dbae381f693 | 1197 | filterSelectReg.ADC_FS = pFilterConfig->fs; |
kevin1990 | 7:4dbae381f693 | 1198 | } |
kevin1990 | 7:4dbae381f693 | 1199 | else if (pFilterConfig->type == ADI_SENSE_ADC_FILTER_FIR_20SPS) |
kevin1990 | 7:4dbae381f693 | 1200 | { |
kevin1990 | 7:4dbae381f693 | 1201 | filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_FIR_20SPS; |
kevin1990 | 7:4dbae381f693 | 1202 | } |
kevin1990 | 7:4dbae381f693 | 1203 | else if (pFilterConfig->type == ADI_SENSE_ADC_FILTER_FIR_25SPS) |
kevin1990 | 7:4dbae381f693 | 1204 | { |
kevin1990 | 7:4dbae381f693 | 1205 | filterSelectReg.ADC_Filter_Type = ADISENSE_CORE_FILTER_SELECT_FILTER_FIR_25SPS; |
kevin1990 | 7:4dbae381f693 | 1206 | } |
kevin1990 | 7:4dbae381f693 | 1207 | else |
kevin1990 | 7:4dbae381f693 | 1208 | { |
kevin1990 | 7:4dbae381f693 | 1209 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1210 | } |
kevin1990 | 7:4dbae381f693 | 1211 | |
kevin1990 | 7:4dbae381f693 | 1212 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_FILTER_SELECTn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1213 | &filterSelectReg, sizeof(filterSelectReg)); |
kevin1990 | 7:4dbae381f693 | 1214 | } |
kevin1990 | 7:4dbae381f693 | 1215 | |
kevin1990 | 7:4dbae381f693 | 1216 | static ADI_SENSE_RESULT adi_sense_SetChannelAdcCurrentConfig( |
kevin1990 | 7:4dbae381f693 | 1217 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1218 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1219 | ADI_SENSE_ADC_CURRENT_CONFIG *pCurrentConfig) |
kevin1990 | 7:4dbae381f693 | 1220 | { |
kevin1990 | 7:4dbae381f693 | 1221 | ADI_ADISENSE_CORE_Channel_Excitation_t channelExcitationReg = { |
kevin1990 | 7:4dbae381f693 | 1222 | .VALUE8 = REG_ADISENSE_CORE_CHANNEL_EXCITATIONn_RESET |
kevin1990 | 7:4dbae381f693 | 1223 | }; |
kevin1990 | 7:4dbae381f693 | 1224 | |
kevin1990 | 7:4dbae381f693 | 1225 | if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_NONE) |
kevin1990 | 7:4dbae381f693 | 1226 | { |
kevin1990 | 7:4dbae381f693 | 1227 | /* TODO - how should the IOUT0/1 Disable options be represented on the API? */ |
kevin1990 | 7:4dbae381f693 | 1228 | channelExcitationReg.IOUT0_Disable = 1; |
kevin1990 | 7:4dbae381f693 | 1229 | channelExcitationReg.IOUT1_Disable = 1; |
kevin1990 | 7:4dbae381f693 | 1230 | |
kevin1990 | 7:4dbae381f693 | 1231 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_OFF; |
kevin1990 | 7:4dbae381f693 | 1232 | } |
kevin1990 | 7:4dbae381f693 | 1233 | else |
kevin1990 | 7:4dbae381f693 | 1234 | { |
kevin1990 | 7:4dbae381f693 | 1235 | channelExcitationReg.IOUT0_Disable = 0; |
kevin1990 | 7:4dbae381f693 | 1236 | channelExcitationReg.IOUT1_Disable = 0; |
kevin1990 | 7:4dbae381f693 | 1237 | |
kevin1990 | 7:4dbae381f693 | 1238 | if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_50uA) |
kevin1990 | 7:4dbae381f693 | 1239 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_50UA; |
kevin1990 | 7:4dbae381f693 | 1240 | else if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_100uA) |
kevin1990 | 7:4dbae381f693 | 1241 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_100UA; |
kevin1990 | 7:4dbae381f693 | 1242 | else if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_250uA) |
kevin1990 | 7:4dbae381f693 | 1243 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_250UA; |
kevin1990 | 7:4dbae381f693 | 1244 | else if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_500uA) |
kevin1990 | 7:4dbae381f693 | 1245 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_500UA; |
kevin1990 | 7:4dbae381f693 | 1246 | else if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_750uA) |
kevin1990 | 7:4dbae381f693 | 1247 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_750UA; |
kevin1990 | 7:4dbae381f693 | 1248 | else if (pCurrentConfig->outputLevel == ADI_SENSE_ADC_CURRENT_LEVEL_1000uA) |
kevin1990 | 7:4dbae381f693 | 1249 | channelExcitationReg.IOUT_Excitation_Current = ADISENSE_CORE_CHANNEL_EXCITATION_IEXC_1000UA; |
kevin1990 | 7:4dbae381f693 | 1250 | else |
kevin1990 | 7:4dbae381f693 | 1251 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1252 | |
kevin1990 | 7:4dbae381f693 | 1253 | if (pCurrentConfig->sourceOption == ADI_SENSE_ADC_CURRENT_SOURCE_DEFAULT) |
kevin1990 | 7:4dbae381f693 | 1254 | { |
kevin1990 | 7:4dbae381f693 | 1255 | channelExcitationReg.IOUT_Dont_Swap_3Wire = 1; |
kevin1990 | 7:4dbae381f693 | 1256 | channelExcitationReg.IOUT_Static_Swap_3Wire = 0; |
kevin1990 | 7:4dbae381f693 | 1257 | } |
kevin1990 | 7:4dbae381f693 | 1258 | else if (pCurrentConfig->sourceOption == ADI_SENSE_ADC_CURRENT_SOURCE_SWAP_STATIC) |
kevin1990 | 7:4dbae381f693 | 1259 | { |
kevin1990 | 7:4dbae381f693 | 1260 | channelExcitationReg.IOUT_Dont_Swap_3Wire = 1; |
kevin1990 | 7:4dbae381f693 | 1261 | channelExcitationReg.IOUT_Static_Swap_3Wire = 1; |
kevin1990 | 7:4dbae381f693 | 1262 | } |
kevin1990 | 7:4dbae381f693 | 1263 | else if (pCurrentConfig->sourceOption == ADI_SENSE_ADC_CURRENT_SOURCE_SWAP_DYNAMIC) |
kevin1990 | 7:4dbae381f693 | 1264 | { |
kevin1990 | 7:4dbae381f693 | 1265 | channelExcitationReg.IOUT_Dont_Swap_3Wire = 0; |
kevin1990 | 7:4dbae381f693 | 1266 | channelExcitationReg.IOUT_Static_Swap_3Wire = 0; |
kevin1990 | 7:4dbae381f693 | 1267 | } |
kevin1990 | 7:4dbae381f693 | 1268 | else |
kevin1990 | 7:4dbae381f693 | 1269 | { |
kevin1990 | 7:4dbae381f693 | 1270 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1271 | } |
kevin1990 | 7:4dbae381f693 | 1272 | } |
kevin1990 | 7:4dbae381f693 | 1273 | |
kevin1990 | 7:4dbae381f693 | 1274 | return adi_sense_WriteRegister(hDevice, |
kevin1990 | 7:4dbae381f693 | 1275 | REG_ADISENSE_CORE_CHANNEL_EXCITATIONn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1276 | &channelExcitationReg, |
kevin1990 | 7:4dbae381f693 | 1277 | sizeof(channelExcitationReg)); |
kevin1990 | 7:4dbae381f693 | 1278 | } |
kevin1990 | 7:4dbae381f693 | 1279 | |
kevin1990 | 7:4dbae381f693 | 1280 | ADI_SENSE_RESULT adi_sense_SetAdcChannelConfig( |
kevin1990 | 7:4dbae381f693 | 1281 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1282 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1283 | ADI_SENSE_CHANNEL_CONFIG *pChannelConfig) |
kevin1990 | 7:4dbae381f693 | 1284 | { |
kevin1990 | 7:4dbae381f693 | 1285 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1286 | ADI_SENSE_ADC_CHANNEL_CONFIG *pAdcChannelConfig = |
kevin1990 | 7:4dbae381f693 | 1287 | &pChannelConfig->adcChannelConfig; |
kevin1990 | 7:4dbae381f693 | 1288 | |
kevin1990 | 7:4dbae381f693 | 1289 | eRet = adi_sense_SetChannelAdcSensorType(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1290 | pAdcChannelConfig->sensor); |
kevin1990 | 7:4dbae381f693 | 1291 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1292 | { |
kevin1990 | 7:4dbae381f693 | 1293 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1294 | } |
kevin1990 | 7:4dbae381f693 | 1295 | |
kevin1990 | 7:4dbae381f693 | 1296 | eRet = adi_sense_SetChannelAdcSensorDetails(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1297 | pChannelConfig); |
kevin1990 | 7:4dbae381f693 | 1298 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1299 | { |
kevin1990 | 7:4dbae381f693 | 1300 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1301 | } |
kevin1990 | 7:4dbae381f693 | 1302 | |
kevin1990 | 7:4dbae381f693 | 1303 | eRet = adi_sense_SetChannelAdcFilter(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1304 | &pAdcChannelConfig->filter); |
kevin1990 | 7:4dbae381f693 | 1305 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1306 | { |
kevin1990 | 7:4dbae381f693 | 1307 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1308 | } |
kevin1990 | 7:4dbae381f693 | 1309 | |
kevin1990 | 7:4dbae381f693 | 1310 | eRet = adi_sense_SetChannelAdcCurrentConfig(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1311 | &pAdcChannelConfig->current); |
kevin1990 | 7:4dbae381f693 | 1312 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1313 | { |
kevin1990 | 7:4dbae381f693 | 1314 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1315 | } |
kevin1990 | 7:4dbae381f693 | 1316 | |
kevin1990 | 7:4dbae381f693 | 1317 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1318 | } |
kevin1990 | 7:4dbae381f693 | 1319 | |
kevin1990 | 7:4dbae381f693 | 1320 | static ADI_SENSE_RESULT adi_sense_SetChannelI2cSensorType( |
kevin1990 | 7:4dbae381f693 | 1321 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1322 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1323 | ADI_SENSE_I2C_SENSOR_TYPE sensorType) |
kevin1990 | 7:4dbae381f693 | 1324 | { |
kevin1990 | 7:4dbae381f693 | 1325 | ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg = { |
kevin1990 | 7:4dbae381f693 | 1326 | .VALUE16 = REG_ADISENSE_CORE_SENSOR_TYPEn_RESET |
kevin1990 | 7:4dbae381f693 | 1327 | }; |
kevin1990 | 7:4dbae381f693 | 1328 | |
kevin1990 | 7:4dbae381f693 | 1329 | /* Ensure that the sensor type is valid for this channel */ |
kevin1990 | 7:4dbae381f693 | 1330 | switch(sensorType) |
kevin1990 | 7:4dbae381f693 | 1331 | { |
kevin1990 | 7:4dbae381f693 | 1332 | case ADI_SENSE_I2C_SENSOR_HUMIDITY_HONEYWELL_HIH: |
kevin1990 | 7:4dbae381f693 | 1333 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_I2C_HUMIDITY_HONEYWELL_HUMIDICON; |
kevin1990 | 7:4dbae381f693 | 1334 | break; |
kevin1990 | 7:4dbae381f693 | 1335 | case ADI_SENSE_I2C_SENSOR_HUMIDITY_SENSIRION_SHT35: |
kevin1990 | 7:4dbae381f693 | 1336 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_I2C_HUMIDITY_SENSIRION_SHT3X; |
kevin1990 | 7:4dbae381f693 | 1337 | break; |
kevin1990 | 7:4dbae381f693 | 1338 | default: |
kevin1990 | 7:4dbae381f693 | 1339 | /* TODO - add support for custom I2C sensors */ |
kevin1990 | 7:4dbae381f693 | 1340 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1341 | } |
kevin1990 | 7:4dbae381f693 | 1342 | |
kevin1990 | 7:4dbae381f693 | 1343 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_SENSOR_TYPEn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1344 | &sensorTypeReg, sizeof(sensorTypeReg)); |
kevin1990 | 7:4dbae381f693 | 1345 | } |
kevin1990 | 7:4dbae381f693 | 1346 | |
kevin1990 | 7:4dbae381f693 | 1347 | static ADI_SENSE_RESULT adi_sense_SetChannelI2cSensorAddress( |
kevin1990 | 7:4dbae381f693 | 1348 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1349 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1350 | uint32_t deviceAddress) |
kevin1990 | 7:4dbae381f693 | 1351 | { |
kevin1990 | 7:4dbae381f693 | 1352 | ADI_ADISENSE_CORE_Digital_Sensor_Address_t sensorAddressReg = { |
kevin1990 | 7:4dbae381f693 | 1353 | .VALUE8 = REG_ADISENSE_CORE_DIGITAL_SENSOR_ADDRESSn_RESET |
kevin1990 | 7:4dbae381f693 | 1354 | }; |
kevin1990 | 7:4dbae381f693 | 1355 | |
kevin1990 | 7:4dbae381f693 | 1356 | sensorAddressReg.Digital_Sensor_Address = deviceAddress; |
kevin1990 | 7:4dbae381f693 | 1357 | |
kevin1990 | 7:4dbae381f693 | 1358 | return adi_sense_WriteRegister(hDevice, |
kevin1990 | 7:4dbae381f693 | 1359 | REG_ADISENSE_CORE_DIGITAL_SENSOR_ADDRESSn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1360 | &sensorAddressReg, sizeof(sensorAddressReg)); |
kevin1990 | 7:4dbae381f693 | 1361 | } |
kevin1990 | 7:4dbae381f693 | 1362 | |
kevin1990 | 7:4dbae381f693 | 1363 | ADI_SENSE_RESULT adi_sense_SetI2cChannelConfig( |
kevin1990 | 7:4dbae381f693 | 1364 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1365 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1366 | ADI_SENSE_I2C_CHANNEL_CONFIG *pI2cChannelConfig) |
kevin1990 | 7:4dbae381f693 | 1367 | { |
kevin1990 | 7:4dbae381f693 | 1368 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1369 | |
kevin1990 | 7:4dbae381f693 | 1370 | eRet = adi_sense_SetChannelI2cSensorType(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1371 | pI2cChannelConfig->sensor); |
kevin1990 | 7:4dbae381f693 | 1372 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1373 | { |
kevin1990 | 7:4dbae381f693 | 1374 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1375 | } |
kevin1990 | 7:4dbae381f693 | 1376 | |
kevin1990 | 7:4dbae381f693 | 1377 | eRet = adi_sense_SetChannelI2cSensorAddress(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1378 | pI2cChannelConfig->deviceAddress); |
kevin1990 | 7:4dbae381f693 | 1379 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1380 | { |
kevin1990 | 7:4dbae381f693 | 1381 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1382 | } |
kevin1990 | 7:4dbae381f693 | 1383 | |
kevin1990 | 7:4dbae381f693 | 1384 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1385 | } |
kevin1990 | 7:4dbae381f693 | 1386 | |
kevin1990 | 7:4dbae381f693 | 1387 | static ADI_SENSE_RESULT adi_sense_SetChannelSpiSensorType( |
kevin1990 | 7:4dbae381f693 | 1388 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1389 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1390 | ADI_SENSE_SPI_SENSOR_TYPE sensorType) |
kevin1990 | 7:4dbae381f693 | 1391 | { |
kevin1990 | 7:4dbae381f693 | 1392 | ADI_ADISENSE_CORE_Sensor_Type_t sensorTypeReg = { |
kevin1990 | 7:4dbae381f693 | 1393 | .VALUE16 = REG_ADISENSE_CORE_SENSOR_TYPEn_RESET |
kevin1990 | 7:4dbae381f693 | 1394 | }; |
kevin1990 | 7:4dbae381f693 | 1395 | |
kevin1990 | 7:4dbae381f693 | 1396 | /* Ensure that the sensor type is valid for this channel */ |
kevin1990 | 7:4dbae381f693 | 1397 | switch(sensorType) |
kevin1990 | 7:4dbae381f693 | 1398 | { |
kevin1990 | 7:4dbae381f693 | 1399 | case ADI_SENSE_SPI_SENSOR_PRESSURE_HONEYWELL_HSCDRN1: |
kevin1990 | 7:4dbae381f693 | 1400 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_PRESSURE_HONEYWELL_TRUSTABILITY; |
kevin1990 | 7:4dbae381f693 | 1401 | break; |
kevin1990 | 7:4dbae381f693 | 1402 | case ADI_SENSE_SPI_SENSOR_PRESSURE_ADI_ADXL362: |
kevin1990 | 7:4dbae381f693 | 1403 | sensorTypeReg.Sensor_Type = ADISENSE_CORE_SENSOR_TYPE_SENSOR_SPI_ACCELEROMETER_1; |
kevin1990 | 7:4dbae381f693 | 1404 | break; |
kevin1990 | 7:4dbae381f693 | 1405 | default: |
kevin1990 | 7:4dbae381f693 | 1406 | /* TODO - add support for custom SPI sensors */ |
kevin1990 | 7:4dbae381f693 | 1407 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1408 | } |
kevin1990 | 7:4dbae381f693 | 1409 | |
kevin1990 | 7:4dbae381f693 | 1410 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_SENSOR_TYPEn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1411 | &sensorTypeReg, sizeof(sensorTypeReg)); |
kevin1990 | 7:4dbae381f693 | 1412 | } |
kevin1990 | 7:4dbae381f693 | 1413 | |
kevin1990 | 7:4dbae381f693 | 1414 | ADI_SENSE_RESULT adi_sense_SetSpiChannelConfig( |
kevin1990 | 7:4dbae381f693 | 1415 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1416 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1417 | ADI_SENSE_SPI_CHANNEL_CONFIG *pSpiChannelConfig) |
kevin1990 | 7:4dbae381f693 | 1418 | { |
kevin1990 | 7:4dbae381f693 | 1419 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1420 | |
kevin1990 | 7:4dbae381f693 | 1421 | eRet = adi_sense_SetChannelSpiSensorType(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1422 | pSpiChannelConfig->sensor); |
kevin1990 | 7:4dbae381f693 | 1423 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1424 | { |
kevin1990 | 7:4dbae381f693 | 1425 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1426 | } |
kevin1990 | 7:4dbae381f693 | 1427 | |
kevin1990 | 7:4dbae381f693 | 1428 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1429 | } |
kevin1990 | 7:4dbae381f693 | 1430 | |
kevin1990 | 7:4dbae381f693 | 1431 | ADI_SENSE_RESULT adi_sense_SetChannelThresholdLimits( |
kevin1990 | 7:4dbae381f693 | 1432 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1433 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1434 | float32_t highThresholdLimit, |
kevin1990 | 7:4dbae381f693 | 1435 | float32_t lowThresholdLimit) |
kevin1990 | 7:4dbae381f693 | 1436 | { |
kevin1990 | 7:4dbae381f693 | 1437 | ADI_ADISENSE_CORE_High_Threshold_Limit_t highLimitReg = { |
kevin1990 | 7:4dbae381f693 | 1438 | .High_Threshold = highThresholdLimit |
kevin1990 | 7:4dbae381f693 | 1439 | }; |
kevin1990 | 7:4dbae381f693 | 1440 | ADI_ADISENSE_CORE_Low_Threshold_Limit_t lowLimitReg = { |
kevin1990 | 7:4dbae381f693 | 1441 | .Low_Threshold = lowThresholdLimit |
kevin1990 | 7:4dbae381f693 | 1442 | }; |
kevin1990 | 7:4dbae381f693 | 1443 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1444 | |
kevin1990 | 7:4dbae381f693 | 1445 | eRet = adi_sense_WriteRegister(hDevice, |
kevin1990 | 7:4dbae381f693 | 1446 | REG_ADISENSE_CORE_HIGH_THRESHOLD_LIMITn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1447 | &highLimitReg, sizeof(highLimitReg)); |
kevin1990 | 7:4dbae381f693 | 1448 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1449 | { |
kevin1990 | 7:4dbae381f693 | 1450 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1451 | } |
kevin1990 | 7:4dbae381f693 | 1452 | |
kevin1990 | 7:4dbae381f693 | 1453 | return adi_sense_WriteRegister(hDevice, |
kevin1990 | 7:4dbae381f693 | 1454 | REG_ADISENSE_CORE_LOW_THRESHOLD_LIMITn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1455 | &lowLimitReg, sizeof(lowLimitReg));; |
kevin1990 | 7:4dbae381f693 | 1456 | } |
kevin1990 | 7:4dbae381f693 | 1457 | |
kevin1990 | 7:4dbae381f693 | 1458 | ADI_SENSE_RESULT adi_sense_SetChannelSettlingTime( |
kevin1990 | 7:4dbae381f693 | 1459 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1460 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1461 | uint32_t settlingTime) |
kevin1990 | 7:4dbae381f693 | 1462 | { |
kevin1990 | 7:4dbae381f693 | 1463 | ADI_ADISENSE_CORE_Settling_Time_t settlingTimeReg = { |
kevin1990 | 7:4dbae381f693 | 1464 | .VALUE16 = REG_ADISENSE_CORE_SETTLING_TIMEn_RESET |
kevin1990 | 7:4dbae381f693 | 1465 | }; |
kevin1990 | 7:4dbae381f693 | 1466 | |
kevin1990 | 7:4dbae381f693 | 1467 | settlingTimeReg.Settling_Time = settlingTime; |
kevin1990 | 7:4dbae381f693 | 1468 | |
kevin1990 | 7:4dbae381f693 | 1469 | return adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_SETTLING_TIMEn(eChannelId), |
kevin1990 | 7:4dbae381f693 | 1470 | &settlingTimeReg, sizeof(settlingTimeReg)); |
kevin1990 | 7:4dbae381f693 | 1471 | } |
kevin1990 | 7:4dbae381f693 | 1472 | |
kevin1990 | 7:4dbae381f693 | 1473 | ADI_SENSE_RESULT adi_sense_SetChannelConfig( |
kevin1990 | 7:4dbae381f693 | 1474 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1475 | ADI_SENSE_CHANNEL_ID eChannelId, |
kevin1990 | 7:4dbae381f693 | 1476 | ADI_SENSE_CHANNEL_CONFIG *pChannelConfig) |
kevin1990 | 7:4dbae381f693 | 1477 | { |
kevin1990 | 7:4dbae381f693 | 1478 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1479 | |
kevin1990 | 7:4dbae381f693 | 1480 | /* If the channel is not enabled, disable it and return */ |
kevin1990 | 7:4dbae381f693 | 1481 | if (! pChannelConfig->enableChannel) |
kevin1990 | 7:4dbae381f693 | 1482 | return adi_sense_SetChannelCount(hDevice, eChannelId, 0); |
kevin1990 | 7:4dbae381f693 | 1483 | |
kevin1990 | 7:4dbae381f693 | 1484 | eRet = adi_sense_SetChannelCount(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1485 | pChannelConfig->measurementsPerCycle); |
kevin1990 | 7:4dbae381f693 | 1486 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1487 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1488 | |
kevin1990 | 7:4dbae381f693 | 1489 | switch (eChannelId) |
kevin1990 | 7:4dbae381f693 | 1490 | { |
kevin1990 | 7:4dbae381f693 | 1491 | case ADI_SENSE_CHANNEL_ID_CJC_0: |
kevin1990 | 7:4dbae381f693 | 1492 | case ADI_SENSE_CHANNEL_ID_CJC_1: |
kevin1990 | 7:4dbae381f693 | 1493 | case ADI_SENSE_CHANNEL_ID_SENSOR_0: |
kevin1990 | 7:4dbae381f693 | 1494 | case ADI_SENSE_CHANNEL_ID_SENSOR_1: |
kevin1990 | 7:4dbae381f693 | 1495 | case ADI_SENSE_CHANNEL_ID_SENSOR_2: |
kevin1990 | 7:4dbae381f693 | 1496 | case ADI_SENSE_CHANNEL_ID_SENSOR_3: |
kevin1990 | 7:4dbae381f693 | 1497 | case ADI_SENSE_CHANNEL_ID_VOLTAGE_0: |
kevin1990 | 7:4dbae381f693 | 1498 | case ADI_SENSE_CHANNEL_ID_CURRENT_0: |
kevin1990 | 7:4dbae381f693 | 1499 | eRet = adi_sense_SetAdcChannelConfig(hDevice, eChannelId, pChannelConfig); |
kevin1990 | 7:4dbae381f693 | 1500 | break; |
kevin1990 | 7:4dbae381f693 | 1501 | case ADI_SENSE_CHANNEL_ID_I2C_0: |
kevin1990 | 7:4dbae381f693 | 1502 | case ADI_SENSE_CHANNEL_ID_I2C_1: |
kevin1990 | 7:4dbae381f693 | 1503 | eRet = adi_sense_SetI2cChannelConfig(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1504 | &pChannelConfig->i2cChannelConfig); |
kevin1990 | 7:4dbae381f693 | 1505 | break; |
kevin1990 | 7:4dbae381f693 | 1506 | case ADI_SENSE_CHANNEL_ID_SPI_0: |
kevin1990 | 7:4dbae381f693 | 1507 | eRet = adi_sense_SetSpiChannelConfig(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1508 | &pChannelConfig->spiChannelConfig); |
kevin1990 | 7:4dbae381f693 | 1509 | break; |
kevin1990 | 7:4dbae381f693 | 1510 | default: |
kevin1990 | 7:4dbae381f693 | 1511 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1512 | } |
kevin1990 | 7:4dbae381f693 | 1513 | |
kevin1990 | 7:4dbae381f693 | 1514 | eRet = adi_sense_SetChannelThresholdLimits(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1515 | pChannelConfig->measurementMaxValue, |
kevin1990 | 7:4dbae381f693 | 1516 | pChannelConfig->measurementMinValue); |
kevin1990 | 7:4dbae381f693 | 1517 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1518 | { |
kevin1990 | 7:4dbae381f693 | 1519 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1520 | } |
kevin1990 | 7:4dbae381f693 | 1521 | |
kevin1990 | 7:4dbae381f693 | 1522 | eRet = adi_sense_SetChannelSettlingTime(hDevice, eChannelId, |
kevin1990 | 7:4dbae381f693 | 1523 | pChannelConfig->extraSettlingTime); |
kevin1990 | 7:4dbae381f693 | 1524 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1525 | { |
kevin1990 | 7:4dbae381f693 | 1526 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1527 | } |
kevin1990 | 7:4dbae381f693 | 1528 | |
kevin1990 | 7:4dbae381f693 | 1529 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1530 | } |
kevin1990 | 7:4dbae381f693 | 1531 | |
kevin1990 | 7:4dbae381f693 | 1532 | ADI_SENSE_RESULT adi_sense_SetDeviceConfig( |
kevin1990 | 7:4dbae381f693 | 1533 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 1534 | ADI_SENSE_CONFIG * const pConfig) |
kevin1990 | 7:4dbae381f693 | 1535 | { |
kevin1990 | 7:4dbae381f693 | 1536 | ADI_SENSE_DEVICE_CONTEXT *pCtx = hDevice; |
kevin1990 | 7:4dbae381f693 | 1537 | ADI_SENSE_DEVICE_CONFIG *pDeviceConfig; |
kevin1990 | 7:4dbae381f693 | 1538 | ADI_SENSE_PRODUCT_ID productId; |
kevin1990 | 7:4dbae381f693 | 1539 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1540 | |
kevin1990 | 7:4dbae381f693 | 1541 | if (pCtx->nDeviceIndex >= pConfig->numDevices) |
kevin1990 | 7:4dbae381f693 | 1542 | { |
kevin1990 | 7:4dbae381f693 | 1543 | return ADI_SENSE_INVALID_DEVICE_NUM; |
kevin1990 | 7:4dbae381f693 | 1544 | } |
kevin1990 | 7:4dbae381f693 | 1545 | pDeviceConfig = &pConfig->devices[pCtx->nDeviceIndex]; |
kevin1990 | 7:4dbae381f693 | 1546 | |
kevin1990 | 7:4dbae381f693 | 1547 | /* Check that the Product ID is a match? */ |
kevin1990 | 7:4dbae381f693 | 1548 | eRet = adi_sense_GetProductID(hDevice, &productId); |
kevin1990 | 7:4dbae381f693 | 1549 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1550 | { |
kevin1990 | 7:4dbae381f693 | 1551 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1552 | } |
kevin1990 | 7:4dbae381f693 | 1553 | if (pDeviceConfig->productId != productId) |
kevin1990 | 7:4dbae381f693 | 1554 | { |
kevin1990 | 7:4dbae381f693 | 1555 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1556 | } |
kevin1990 | 7:4dbae381f693 | 1557 | |
kevin1990 | 7:4dbae381f693 | 1558 | eRet = adi_sense_SetPowerConfig(hDevice, &pDeviceConfig->power); |
kevin1990 | 7:4dbae381f693 | 1559 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1560 | { |
kevin1990 | 7:4dbae381f693 | 1561 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1562 | } |
kevin1990 | 7:4dbae381f693 | 1563 | |
kevin1990 | 7:4dbae381f693 | 1564 | eRet = adi_sense_SetMeasurementConfig(hDevice, &pDeviceConfig->measurement); |
kevin1990 | 7:4dbae381f693 | 1565 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1566 | { |
kevin1990 | 7:4dbae381f693 | 1567 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1568 | } |
kevin1990 | 7:4dbae381f693 | 1569 | |
kevin1990 | 7:4dbae381f693 | 1570 | eRet = adi_sense_SetDiagnosticsConfig(hDevice, &pDeviceConfig->diagnostics); |
kevin1990 | 7:4dbae381f693 | 1571 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1572 | { |
kevin1990 | 7:4dbae381f693 | 1573 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1574 | } |
kevin1990 | 7:4dbae381f693 | 1575 | |
kevin1990 | 7:4dbae381f693 | 1576 | for (ADI_SENSE_CHANNEL_ID id = 0; id < ADI_SENSE_MAX_CHANNELS; id++) |
kevin1990 | 7:4dbae381f693 | 1577 | { |
kevin1990 | 7:4dbae381f693 | 1578 | eRet = adi_sense_SetChannelConfig(hDevice, id, |
kevin1990 | 7:4dbae381f693 | 1579 | &pDeviceConfig->channels[id]); |
kevin1990 | 7:4dbae381f693 | 1580 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1581 | { |
kevin1990 | 7:4dbae381f693 | 1582 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1583 | } |
kevin1990 | 7:4dbae381f693 | 1584 | } |
kevin1990 | 7:4dbae381f693 | 1585 | |
kevin1990 | 7:4dbae381f693 | 1586 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1587 | } |
kevin1990 | 7:4dbae381f693 | 1588 | |
kevin1990 | 7:4dbae381f693 | 1589 | ADI_SENSE_RESULT adi_sense_SetDspData( |
kevin1990 | 7:4dbae381f693 | 1590 | ADI_SENSE_DEVICE_HANDLE const hDevice, |
kevin1990 | 7:4dbae381f693 | 1591 | ADI_SENSE_DSP_LUT_RAW * const pDspData) |
kevin1990 | 7:4dbae381f693 | 1592 | { |
kevin1990 | 7:4dbae381f693 | 1593 | ADI_SENSE_DSP_LUT *pLut = (ADI_SENSE_DSP_LUT *)pDspData; |
kevin1990 | 7:4dbae381f693 | 1594 | ADI_SENSE_DSP_LUT_TABLE *pLutTable = pLut->tables; |
kevin1990 | 7:4dbae381f693 | 1595 | unsigned actualLength = 0; |
kevin1990 | 7:4dbae381f693 | 1596 | |
kevin1990 | 7:4dbae381f693 | 1597 | if (pLut->header.signature != ADI_SENSE_DSP_LUT_SIGNATURE) |
kevin1990 | 7:4dbae381f693 | 1598 | { |
kevin1990 | 7:4dbae381f693 | 1599 | ADI_SENSE_LOG_ERROR("DSP LUT signature incorrect (expected 0x%X, actual 0x%X)", |
kevin1990 | 7:4dbae381f693 | 1600 | ADI_SENSE_DSP_LUT_SIGNATURE, pLut->header.signature); |
kevin1990 | 7:4dbae381f693 | 1601 | return ADI_SENSE_INVALID_SIGNATURE; |
kevin1990 | 7:4dbae381f693 | 1602 | } |
kevin1990 | 7:4dbae381f693 | 1603 | |
kevin1990 | 7:4dbae381f693 | 1604 | for (unsigned i = 0; i < pLut->header.numTables; i++) |
kevin1990 | 7:4dbae381f693 | 1605 | { |
kevin1990 | 7:4dbae381f693 | 1606 | ADI_SENSE_DSP_LUT_DESCRIPTOR *pDesc = &pLutTable->descriptor; |
kevin1990 | 7:4dbae381f693 | 1607 | unsigned short calculatedCrc; |
kevin1990 | 7:4dbae381f693 | 1608 | |
kevin1990 | 7:4dbae381f693 | 1609 | switch (pDesc->geometry) |
kevin1990 | 7:4dbae381f693 | 1610 | { |
kevin1990 | 7:4dbae381f693 | 1611 | case ADI_SENSE_DSP_LUT_GEOMETRY_COEFFICIENT_LIST: |
kevin1990 | 7:4dbae381f693 | 1612 | case ADI_SENSE_DSP_LUT_GEOMETRY_EQUALLY_SPACED_1D: |
kevin1990 | 7:4dbae381f693 | 1613 | case ADI_SENSE_DSP_LUT_GEOMETRY_EQUALLY_SPACED_2D: |
kevin1990 | 7:4dbae381f693 | 1614 | break; |
kevin1990 | 7:4dbae381f693 | 1615 | default: |
kevin1990 | 7:4dbae381f693 | 1616 | ADI_SENSE_LOG_ERROR("Invalid geometry %u specified for DSP LUT table %u", |
kevin1990 | 7:4dbae381f693 | 1617 | pDesc->geometry, i); |
kevin1990 | 7:4dbae381f693 | 1618 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1619 | } |
kevin1990 | 7:4dbae381f693 | 1620 | |
kevin1990 | 7:4dbae381f693 | 1621 | switch (pDesc->equation) |
kevin1990 | 7:4dbae381f693 | 1622 | { |
kevin1990 | 7:4dbae381f693 | 1623 | case ADI_SENSE_DSP_LUT_EQUATION_NONE: |
kevin1990 | 7:4dbae381f693 | 1624 | case ADI_SENSE_DSP_LUT_EQUATION_POLYNOMIAL: |
kevin1990 | 7:4dbae381f693 | 1625 | case ADI_SENSE_DSP_LUT_EQUATION_POLYNOMIAL_EXPONENTIAL: |
kevin1990 | 7:4dbae381f693 | 1626 | case ADI_SENSE_DSP_LUT_EQUATION_QUADRATIC: |
kevin1990 | 7:4dbae381f693 | 1627 | case ADI_SENSE_DSP_LUT_EQUATION_EXPONENTIAL: |
kevin1990 | 7:4dbae381f693 | 1628 | case ADI_SENSE_DSP_LUT_EQUATION_LOGARITHMIC: |
kevin1990 | 7:4dbae381f693 | 1629 | break; |
kevin1990 | 7:4dbae381f693 | 1630 | default: |
kevin1990 | 7:4dbae381f693 | 1631 | ADI_SENSE_LOG_ERROR("Invalid equation %u specified for DSP LUT table %u", |
kevin1990 | 7:4dbae381f693 | 1632 | pDesc->equation, i); |
kevin1990 | 7:4dbae381f693 | 1633 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1634 | } |
kevin1990 | 7:4dbae381f693 | 1635 | |
kevin1990 | 7:4dbae381f693 | 1636 | switch (pDesc->function) |
kevin1990 | 7:4dbae381f693 | 1637 | { |
kevin1990 | 7:4dbae381f693 | 1638 | case ADI_SENSE_DSP_LUT_FUNCTION_TCJ_MV2C: |
kevin1990 | 7:4dbae381f693 | 1639 | case ADI_SENSE_DSP_LUT_FUNCTION_TCJ_C2MV: |
kevin1990 | 7:4dbae381f693 | 1640 | case ADI_SENSE_DSP_LUT_FUNCTION_TCK_MV2C: |
kevin1990 | 7:4dbae381f693 | 1641 | case ADI_SENSE_DSP_LUT_FUNCTION_TCK_C2MV: |
kevin1990 | 7:4dbae381f693 | 1642 | case ADI_SENSE_DSP_LUT_FUNCTION_TCT_MV2C: |
kevin1990 | 7:4dbae381f693 | 1643 | case ADI_SENSE_DSP_LUT_FUNCTION_TCT_C2MV: |
kevin1990 | 7:4dbae381f693 | 1644 | case ADI_SENSE_DSP_LUT_FUNCTION_RTD_OHM2C: |
kevin1990 | 7:4dbae381f693 | 1645 | case ADI_SENSE_DSP_LUT_FUNCTION_VOUT_V2BAR: |
kevin1990 | 7:4dbae381f693 | 1646 | case ADI_SENSE_DSP_LUT_FUNCTION_VOUT_V2PSI: |
kevin1990 | 7:4dbae381f693 | 1647 | case ADI_SENSE_DSP_LUT_FUNCTION_IOUT_MA2PSI: |
kevin1990 | 7:4dbae381f693 | 1648 | break; |
kevin1990 | 7:4dbae381f693 | 1649 | default: |
kevin1990 | 7:4dbae381f693 | 1650 | ADI_SENSE_LOG_ERROR("Invalid function %u specified for DSP LUT table %u", |
kevin1990 | 7:4dbae381f693 | 1651 | pDesc->function, i); |
kevin1990 | 7:4dbae381f693 | 1652 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1653 | } |
kevin1990 | 7:4dbae381f693 | 1654 | |
kevin1990 | 7:4dbae381f693 | 1655 | switch (pDesc->vectorFormat) |
kevin1990 | 7:4dbae381f693 | 1656 | { |
kevin1990 | 7:4dbae381f693 | 1657 | case ADI_SENSE_DSP_LUT_VECTOR_FORMAT_FLOAT32: |
kevin1990 | 7:4dbae381f693 | 1658 | case ADI_SENSE_DSP_LUT_VECTOR_FORMAT_FLOAT64: |
kevin1990 | 7:4dbae381f693 | 1659 | break; |
kevin1990 | 7:4dbae381f693 | 1660 | default: |
kevin1990 | 7:4dbae381f693 | 1661 | ADI_SENSE_LOG_ERROR("Invalid vector format %u specified for DSP LUT table %u", |
kevin1990 | 7:4dbae381f693 | 1662 | pDesc->vectorFormat, i); |
kevin1990 | 7:4dbae381f693 | 1663 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1664 | } |
kevin1990 | 7:4dbae381f693 | 1665 | |
kevin1990 | 7:4dbae381f693 | 1666 | if (((pDesc->rangeMin != NAN) && (pDesc->rangeMax != NAN)) && |
kevin1990 | 7:4dbae381f693 | 1667 | (pDesc->rangeMax <= pDesc->rangeMin)) |
kevin1990 | 7:4dbae381f693 | 1668 | { |
kevin1990 | 7:4dbae381f693 | 1669 | ADI_SENSE_LOG_ERROR("Invalid range specified for DSP LUT table %u", i); |
kevin1990 | 7:4dbae381f693 | 1670 | return ADI_SENSE_INVALID_PARAM; |
kevin1990 | 7:4dbae381f693 | 1671 | } |
kevin1990 | 7:4dbae381f693 | 1672 | |
kevin1990 | 7:4dbae381f693 | 1673 | calculatedCrc = crc16_ccitt(pLutTable->data, pDesc->dataLength); |
kevin1990 | 7:4dbae381f693 | 1674 | if (calculatedCrc != pDesc->crc16) |
kevin1990 | 7:4dbae381f693 | 1675 | { |
kevin1990 | 7:4dbae381f693 | 1676 | ADI_SENSE_LOG_ERROR("CRC validation failed on DSP LUT table %u (expected 0x%04X, actual 0x%04X)", |
kevin1990 | 7:4dbae381f693 | 1677 | i, pDesc->crc16, calculatedCrc); |
kevin1990 | 7:4dbae381f693 | 1678 | return ADI_SENSE_CRC_ERROR; |
kevin1990 | 7:4dbae381f693 | 1679 | } |
kevin1990 | 7:4dbae381f693 | 1680 | |
kevin1990 | 7:4dbae381f693 | 1681 | actualLength += sizeof(*pDesc) + pDesc->dataLength; |
kevin1990 | 7:4dbae381f693 | 1682 | |
kevin1990 | 7:4dbae381f693 | 1683 | /* Move to the next look-up table */ |
kevin1990 | 7:4dbae381f693 | 1684 | pLutTable = (ADI_SENSE_DSP_LUT_TABLE *)(pLutTable->data + pDesc->dataLength); |
kevin1990 | 7:4dbae381f693 | 1685 | } |
kevin1990 | 7:4dbae381f693 | 1686 | |
kevin1990 | 7:4dbae381f693 | 1687 | if (actualLength != pLut->header.totalLength) |
kevin1990 | 7:4dbae381f693 | 1688 | { |
kevin1990 | 7:4dbae381f693 | 1689 | ADI_SENSE_LOG_ERROR("DSP LUT table length mismatch (expected %u, actual %u)", |
kevin1990 | 7:4dbae381f693 | 1690 | pLut->header.totalLength, actualLength); |
kevin1990 | 7:4dbae381f693 | 1691 | return ADI_SENSE_WRONG_SIZE; |
kevin1990 | 7:4dbae381f693 | 1692 | } |
kevin1990 | 7:4dbae381f693 | 1693 | |
kevin1990 | 7:4dbae381f693 | 1694 | /* TODO - add a check to ensure that the total length doesn't exceed the maximum internal LUT storage size (TBD) */ |
kevin1990 | 7:4dbae381f693 | 1695 | |
kevin1990 | 7:4dbae381f693 | 1696 | /* TODO - write the LUT data to the device */ |
kevin1990 | 7:4dbae381f693 | 1697 | |
kevin1990 | 7:4dbae381f693 | 1698 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1699 | } |
kevin1990 | 7:4dbae381f693 | 1700 | |
kevin1990 | 7:4dbae381f693 | 1701 | #define CAL_TABLE_ROWS 56 |
kevin1990 | 7:4dbae381f693 | 1702 | #define CAL_TABLE_COLS 3 |
kevin1990 | 7:4dbae381f693 | 1703 | #define CAL_TABLE_SIZE (sizeof(float) * CAL_TABLE_ROWS * CAL_TABLE_COLS) |
kevin1990 | 7:4dbae381f693 | 1704 | |
kevin1990 | 7:4dbae381f693 | 1705 | /*! |
kevin1990 | 7:4dbae381f693 | 1706 | * @brief Read the contents of the ADISense internal calibration table |
kevin1990 | 7:4dbae381f693 | 1707 | * |
kevin1990 | 7:4dbae381f693 | 1708 | * Calibration coefficients/gains/offsets are stored internally in a table |
kevin1990 | 7:4dbae381f693 | 1709 | * of 56x3 32-bit floating point values |
kevin1990 | 7:4dbae381f693 | 1710 | * |
kevin1990 | 7:4dbae381f693 | 1711 | * @param[in] uint8_t* : Pointer to destination buffer for the calibration data |
kevin1990 | 7:4dbae381f693 | 1712 | * @param[in] maxLen : The buffer capacity in bytes (minimum 672 bytes) |
kevin1990 | 7:4dbae381f693 | 1713 | * @param[out] dataLen : The number of bytes written to the buffer |
kevin1990 | 7:4dbae381f693 | 1714 | * @param[out] nRows : The number of rows in the table (56) |
kevin1990 | 7:4dbae381f693 | 1715 | * @param[out] nRows : The number of columns in the table (3) |
kevin1990 | 7:4dbae381f693 | 1716 | * |
kevin1990 | 7:4dbae381f693 | 1717 | * @return Status |
kevin1990 | 7:4dbae381f693 | 1718 | * - #ADI_SENSE_SUCCESS Call completed successfully. |
kevin1990 | 7:4dbae381f693 | 1719 | * - #ADI_SENSE_FAILURE |
kevin1990 | 7:4dbae381f693 | 1720 | * - #ADI_SENSE_INVALID_OPERATION Invalid register identifier. |
kevin1990 | 7:4dbae381f693 | 1721 | */ |
kevin1990 | 7:4dbae381f693 | 1722 | ADI_SENSE_RESULT adi_sense_ReadCalTable( |
kevin1990 | 7:4dbae381f693 | 1723 | ADI_SENSE_DEVICE_HANDLE hDevice, |
kevin1990 | 7:4dbae381f693 | 1724 | float *buffer, |
kevin1990 | 7:4dbae381f693 | 1725 | unsigned maxLen, |
kevin1990 | 7:4dbae381f693 | 1726 | unsigned *dataLen, |
kevin1990 | 7:4dbae381f693 | 1727 | unsigned *nRows, |
kevin1990 | 7:4dbae381f693 | 1728 | unsigned *nColumns) |
kevin1990 | 7:4dbae381f693 | 1729 | { |
kevin1990 | 7:4dbae381f693 | 1730 | ADI_SENSE_RESULT eRet; |
kevin1990 | 7:4dbae381f693 | 1731 | ADI_ADISENSE_CORE_CAL_Offset_t calOffsetReg; |
kevin1990 | 7:4dbae381f693 | 1732 | |
kevin1990 | 7:4dbae381f693 | 1733 | *dataLen = 0; |
kevin1990 | 7:4dbae381f693 | 1734 | *nRows = CAL_TABLE_ROWS; |
kevin1990 | 7:4dbae381f693 | 1735 | *nColumns = CAL_TABLE_COLS; |
kevin1990 | 7:4dbae381f693 | 1736 | /* Read back in 256-byte chunks due to limitation in underlying layer */ |
kevin1990 | 7:4dbae381f693 | 1737 | for (unsigned offset = 0; offset < CAL_TABLE_SIZE; offset += 256) |
kevin1990 | 7:4dbae381f693 | 1738 | { |
kevin1990 | 7:4dbae381f693 | 1739 | unsigned readSize = 256; |
kevin1990 | 7:4dbae381f693 | 1740 | |
kevin1990 | 7:4dbae381f693 | 1741 | if (readSize > maxLen) |
kevin1990 | 7:4dbae381f693 | 1742 | readSize = maxLen; |
kevin1990 | 7:4dbae381f693 | 1743 | |
kevin1990 | 7:4dbae381f693 | 1744 | if (readSize > (CAL_TABLE_SIZE - offset)) |
kevin1990 | 7:4dbae381f693 | 1745 | readSize = CAL_TABLE_SIZE - offset; |
kevin1990 | 7:4dbae381f693 | 1746 | |
kevin1990 | 7:4dbae381f693 | 1747 | calOffsetReg.CAL_Offset = offset; |
kevin1990 | 7:4dbae381f693 | 1748 | eRet = adi_sense_WriteRegister(hDevice, REG_ADISENSE_CORE_CAL_OFFSET, |
kevin1990 | 7:4dbae381f693 | 1749 | &calOffsetReg, sizeof(calOffsetReg)); |
kevin1990 | 7:4dbae381f693 | 1750 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1751 | { |
kevin1990 | 7:4dbae381f693 | 1752 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1753 | } |
kevin1990 | 7:4dbae381f693 | 1754 | |
kevin1990 | 7:4dbae381f693 | 1755 | eRet = adi_sense_ReadRegister(hDevice, REG_ADISENSE_CORE_CAL_DATA, |
kevin1990 | 7:4dbae381f693 | 1756 | (uint8_t *)buffer + offset, readSize); |
kevin1990 | 7:4dbae381f693 | 1757 | if (eRet) |
kevin1990 | 7:4dbae381f693 | 1758 | { |
kevin1990 | 7:4dbae381f693 | 1759 | return eRet; |
kevin1990 | 7:4dbae381f693 | 1760 | } |
kevin1990 | 7:4dbae381f693 | 1761 | *dataLen += readSize; |
kevin1990 | 7:4dbae381f693 | 1762 | } |
kevin1990 | 7:4dbae381f693 | 1763 | |
kevin1990 | 7:4dbae381f693 | 1764 | return ADI_SENSE_SUCCESS; |
kevin1990 | 7:4dbae381f693 | 1765 | } |
kevin1990 | 7:4dbae381f693 | 1766 |