sensor result structure modified

Committer:
ADIJake
Date:
Mon Apr 01 11:09:52 2019 +0000
Revision:
0:85855ecd3257
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ADIJake 0:85855ecd3257 1 /******************************************************************************
ADIJake 0:85855ecd3257 2 Copyright 2017 (c) Analog Devices, Inc.
ADIJake 0:85855ecd3257 3
ADIJake 0:85855ecd3257 4 All rights reserved.
ADIJake 0:85855ecd3257 5
ADIJake 0:85855ecd3257 6 Redistribution and use in source and binary forms, with or without
ADIJake 0:85855ecd3257 7 modification, are permitted provided that the following conditions are met:
ADIJake 0:85855ecd3257 8 - Redistributions of source code must retain the above copyright
ADIJake 0:85855ecd3257 9 notice, this list of conditions and the following disclaimer.
ADIJake 0:85855ecd3257 10 - Redistributions in binary form must reproduce the above copyright
ADIJake 0:85855ecd3257 11 notice, this list of conditions and the following disclaimer in
ADIJake 0:85855ecd3257 12 the documentation and/or other materials provided with the
ADIJake 0:85855ecd3257 13 distribution.
ADIJake 0:85855ecd3257 14 - Neither the name of Analog Devices, Inc. nor the names of its
ADIJake 0:85855ecd3257 15 contributors may be used to endorse or promote products derived
ADIJake 0:85855ecd3257 16 from this software without specific prior written permission.
ADIJake 0:85855ecd3257 17 - The use of this software may or may not infringe the patent rights
ADIJake 0:85855ecd3257 18 of one or more patent holders. This license does not release you
ADIJake 0:85855ecd3257 19 from the requirement that you obtain separate licenses from these
ADIJake 0:85855ecd3257 20 patent holders to use this software.
ADIJake 0:85855ecd3257 21 - Use of the software either in source or binary form, must be run
ADIJake 0:85855ecd3257 22 on or directly connected to an Analog Devices Inc. component.
ADIJake 0:85855ecd3257 23
ADIJake 0:85855ecd3257 24 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
ADIJake 0:85855ecd3257 25 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
ADIJake 0:85855ecd3257 26 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ADIJake 0:85855ecd3257 27 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
ADIJake 0:85855ecd3257 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
ADIJake 0:85855ecd3257 29 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
ADIJake 0:85855ecd3257 30 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ADIJake 0:85855ecd3257 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ADIJake 0:85855ecd3257 32 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ADIJake 0:85855ecd3257 33 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ADIJake 0:85855ecd3257 34 *
ADIJake 0:85855ecd3257 35 *****************************************************************************/
ADIJake 0:85855ecd3257 36
ADIJake 0:85855ecd3257 37 /*!
ADIJake 0:85855ecd3257 38 ******************************************************************************
ADIJake 0:85855ecd3257 39 * @file:
ADIJake 0:85855ecd3257 40 * @brief: ADISENSE OS-dependent wrapper layer for GPIO interface
ADIJake 0:85855ecd3257 41 *-----------------------------------------------------------------------------
ADIJake 0:85855ecd3257 42 */
ADIJake 0:85855ecd3257 43
ADIJake 0:85855ecd3257 44 #include <mbed.h>
ADIJake 0:85855ecd3257 45
ADIJake 0:85855ecd3257 46 #include "inc/adi_sense_gpio.h"
ADIJake 0:85855ecd3257 47 #include "inc/adi_sense_log.h"
ADIJake 0:85855ecd3257 48
ADIJake 0:85855ecd3257 49 class GpioContext
ADIJake 0:85855ecd3257 50 {
ADIJake 0:85855ecd3257 51 public:
ADIJake 0:85855ecd3257 52 GpioContext(
ADIJake 0:85855ecd3257 53 PinName resetPin,
ADIJake 0:85855ecd3257 54 PinName errorPin,
ADIJake 0:85855ecd3257 55 PinName alertPin,
ADIJake 0:85855ecd3257 56 PinName datareadyPin)
ADIJake 0:85855ecd3257 57 : _reset(resetPin),
ADIJake 0:85855ecd3257 58 _error(errorPin),
ADIJake 0:85855ecd3257 59 _alert(alertPin),
ADIJake 0:85855ecd3257 60 _dataready(datareadyPin),
ADIJake 0:85855ecd3257 61 _errorIrq(errorPin),
ADIJake 0:85855ecd3257 62 _alertIrq(alertPin),
ADIJake 0:85855ecd3257 63 _datareadyIrq(datareadyPin) {}
ADIJake 0:85855ecd3257 64
ADIJake 0:85855ecd3257 65 ADI_SENSE_RESULT get(
ADIJake 0:85855ecd3257 66 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 67 bool *pState);
ADIJake 0:85855ecd3257 68
ADIJake 0:85855ecd3257 69 ADI_SENSE_RESULT set(
ADIJake 0:85855ecd3257 70 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 71 bool state);
ADIJake 0:85855ecd3257 72
ADIJake 0:85855ecd3257 73 ADI_SENSE_RESULT enableIrq(
ADIJake 0:85855ecd3257 74 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 75 ADI_SENSE_GPIO_CALLBACK callbackFn,
ADIJake 0:85855ecd3257 76 void *pArg);
ADIJake 0:85855ecd3257 77
ADIJake 0:85855ecd3257 78 ADI_SENSE_RESULT disableIrq(
ADIJake 0:85855ecd3257 79 ADI_SENSE_GPIO_PIN ePinId);
ADIJake 0:85855ecd3257 80
ADIJake 0:85855ecd3257 81 private:
ADIJake 0:85855ecd3257 82 DigitalOut _reset;
ADIJake 0:85855ecd3257 83
ADIJake 0:85855ecd3257 84 DigitalIn _error;
ADIJake 0:85855ecd3257 85 DigitalIn _alert;
ADIJake 0:85855ecd3257 86 DigitalIn _dataready;
ADIJake 0:85855ecd3257 87
ADIJake 0:85855ecd3257 88 InterruptIn _errorIrq;
ADIJake 0:85855ecd3257 89 InterruptIn _alertIrq;
ADIJake 0:85855ecd3257 90 InterruptIn _datareadyIrq;
ADIJake 0:85855ecd3257 91
ADIJake 0:85855ecd3257 92 ADI_SENSE_GPIO_CALLBACK _errorIrqCallback;
ADIJake 0:85855ecd3257 93 ADI_SENSE_GPIO_CALLBACK _alertIrqCallback;
ADIJake 0:85855ecd3257 94 ADI_SENSE_GPIO_CALLBACK _datareadyIrqCallback;
ADIJake 0:85855ecd3257 95
ADIJake 0:85855ecd3257 96 void *_errorIrqArg;
ADIJake 0:85855ecd3257 97 void *_alertIrqArg;
ADIJake 0:85855ecd3257 98 void *_datareadyIrqArg;
ADIJake 0:85855ecd3257 99
ADIJake 0:85855ecd3257 100 void _errorIrqHandler()
ADIJake 0:85855ecd3257 101 {
ADIJake 0:85855ecd3257 102 _errorIrqCallback(ADI_SENSE_GPIO_PIN_ERROR, _errorIrqArg);
ADIJake 0:85855ecd3257 103 }
ADIJake 0:85855ecd3257 104 void _alertIrqHandler()
ADIJake 0:85855ecd3257 105 {
ADIJake 0:85855ecd3257 106 _alertIrqCallback(ADI_SENSE_GPIO_PIN_ALERT, _alertIrqArg);
ADIJake 0:85855ecd3257 107 }
ADIJake 0:85855ecd3257 108 void _datareadyIrqHandler()
ADIJake 0:85855ecd3257 109 {
ADIJake 0:85855ecd3257 110 _datareadyIrqCallback(ADI_SENSE_GPIO_PIN_DATAREADY, _datareadyIrqArg);
ADIJake 0:85855ecd3257 111 }
ADIJake 0:85855ecd3257 112 };
ADIJake 0:85855ecd3257 113
ADIJake 0:85855ecd3257 114 ADI_SENSE_RESULT GpioContext::get(
ADIJake 0:85855ecd3257 115 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 116 bool *pState)
ADIJake 0:85855ecd3257 117 {
ADIJake 0:85855ecd3257 118 switch(ePinId)
ADIJake 0:85855ecd3257 119 {
ADIJake 0:85855ecd3257 120 case ADI_SENSE_GPIO_PIN_ERROR:
ADIJake 0:85855ecd3257 121 *pState = _error;
ADIJake 0:85855ecd3257 122 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 123 case ADI_SENSE_GPIO_PIN_ALERT:
ADIJake 0:85855ecd3257 124 *pState = _alert;
ADIJake 0:85855ecd3257 125 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 126 case ADI_SENSE_GPIO_PIN_DATAREADY:
ADIJake 0:85855ecd3257 127 *pState = _dataready;
ADIJake 0:85855ecd3257 128 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 129 case ADI_SENSE_GPIO_PIN_RESET:
ADIJake 0:85855ecd3257 130 *pState = _reset;
ADIJake 0:85855ecd3257 131 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 132 default:
ADIJake 0:85855ecd3257 133 return ADI_SENSE_INVALID_DEVICE_NUM;
ADIJake 0:85855ecd3257 134 }
ADIJake 0:85855ecd3257 135 }
ADIJake 0:85855ecd3257 136
ADIJake 0:85855ecd3257 137 ADI_SENSE_RESULT GpioContext::set(
ADIJake 0:85855ecd3257 138 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 139 bool state)
ADIJake 0:85855ecd3257 140 {
ADIJake 0:85855ecd3257 141 switch(ePinId)
ADIJake 0:85855ecd3257 142 {
ADIJake 0:85855ecd3257 143 case ADI_SENSE_GPIO_PIN_RESET:
ADIJake 0:85855ecd3257 144 _reset = state;
ADIJake 0:85855ecd3257 145 break;
ADIJake 0:85855ecd3257 146 default:
ADIJake 0:85855ecd3257 147 return ADI_SENSE_INVALID_DEVICE_NUM;
ADIJake 0:85855ecd3257 148 }
ADIJake 0:85855ecd3257 149
ADIJake 0:85855ecd3257 150 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 151 }
ADIJake 0:85855ecd3257 152
ADIJake 0:85855ecd3257 153 ADI_SENSE_RESULT GpioContext::enableIrq(
ADIJake 0:85855ecd3257 154 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 155 ADI_SENSE_GPIO_CALLBACK callbackFn,
ADIJake 0:85855ecd3257 156 void *pArg)
ADIJake 0:85855ecd3257 157 {
ADIJake 0:85855ecd3257 158 switch(ePinId)
ADIJake 0:85855ecd3257 159 {
ADIJake 0:85855ecd3257 160 case ADI_SENSE_GPIO_PIN_ERROR:
ADIJake 0:85855ecd3257 161 _errorIrqCallback = callbackFn;
ADIJake 0:85855ecd3257 162 _errorIrqArg = pArg;
ADIJake 0:85855ecd3257 163 _errorIrq.rise(callback(this, &GpioContext::_errorIrqHandler));
ADIJake 0:85855ecd3257 164 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 165 case ADI_SENSE_GPIO_PIN_ALERT:
ADIJake 0:85855ecd3257 166 _alertIrqCallback = callbackFn;
ADIJake 0:85855ecd3257 167 _alertIrqArg = pArg;
ADIJake 0:85855ecd3257 168 _alertIrq.rise(callback(this, &GpioContext::_alertIrqHandler));
ADIJake 0:85855ecd3257 169 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 170 case ADI_SENSE_GPIO_PIN_DATAREADY:
ADIJake 0:85855ecd3257 171 _datareadyIrqCallback = callbackFn;
ADIJake 0:85855ecd3257 172 _datareadyIrqArg = pArg;
ADIJake 0:85855ecd3257 173 _datareadyIrq.rise(callback(this, &GpioContext::_datareadyIrqHandler));
ADIJake 0:85855ecd3257 174 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 175 default:
ADIJake 0:85855ecd3257 176 return ADI_SENSE_INVALID_DEVICE_NUM;
ADIJake 0:85855ecd3257 177 }
ADIJake 0:85855ecd3257 178 }
ADIJake 0:85855ecd3257 179
ADIJake 0:85855ecd3257 180 ADI_SENSE_RESULT GpioContext::disableIrq(
ADIJake 0:85855ecd3257 181 ADI_SENSE_GPIO_PIN ePinId)
ADIJake 0:85855ecd3257 182 {
ADIJake 0:85855ecd3257 183 switch(ePinId)
ADIJake 0:85855ecd3257 184 {
ADIJake 0:85855ecd3257 185 case ADI_SENSE_GPIO_PIN_ERROR:
ADIJake 0:85855ecd3257 186 _errorIrq.rise(NULL);
ADIJake 0:85855ecd3257 187 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 188 case ADI_SENSE_GPIO_PIN_ALERT:
ADIJake 0:85855ecd3257 189 _alertIrq.rise(NULL);
ADIJake 0:85855ecd3257 190 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 191 case ADI_SENSE_GPIO_PIN_DATAREADY:
ADIJake 0:85855ecd3257 192 _datareadyIrq.rise(NULL);
ADIJake 0:85855ecd3257 193 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 194 default:
ADIJake 0:85855ecd3257 195 return ADI_SENSE_INVALID_DEVICE_NUM;
ADIJake 0:85855ecd3257 196 }
ADIJake 0:85855ecd3257 197 }
ADIJake 0:85855ecd3257 198
ADIJake 0:85855ecd3257 199 #ifdef __cplusplus
ADIJake 0:85855ecd3257 200 extern "C" {
ADIJake 0:85855ecd3257 201 #endif
ADIJake 0:85855ecd3257 202
ADIJake 0:85855ecd3257 203 /*
ADIJake 0:85855ecd3257 204 * Open the GPIO interface and allocate resources
ADIJake 0:85855ecd3257 205 */
ADIJake 0:85855ecd3257 206 ADI_SENSE_RESULT adi_sense_GpioOpen(
ADIJake 0:85855ecd3257 207 ADI_SENSE_PLATFORM_GPIO_CONFIG *pConfig,
ADIJake 0:85855ecd3257 208 ADI_SENSE_GPIO_HANDLE *phDevice)
ADIJake 0:85855ecd3257 209 {
ADIJake 0:85855ecd3257 210 GpioContext *pCtx = new GpioContext((PinName)pConfig->resetPin,
ADIJake 0:85855ecd3257 211 (PinName)pConfig->errorPin,
ADIJake 0:85855ecd3257 212 (PinName)pConfig->alertPin,
ADIJake 0:85855ecd3257 213 (PinName)pConfig->datareadyPin);
ADIJake 0:85855ecd3257 214 if (!pCtx)
ADIJake 0:85855ecd3257 215 {
ADIJake 0:85855ecd3257 216 ADI_SENSE_LOG_ERROR("Failed to allocate memory for GPIO context");
ADIJake 0:85855ecd3257 217 return ADI_SENSE_NO_MEM;
ADIJake 0:85855ecd3257 218 }
ADIJake 0:85855ecd3257 219
ADIJake 0:85855ecd3257 220 *phDevice = reinterpret_cast<ADI_SENSE_GPIO_HANDLE>(pCtx);
ADIJake 0:85855ecd3257 221 return ADI_SENSE_SUCCESS;
ADIJake 0:85855ecd3257 222 }
ADIJake 0:85855ecd3257 223
ADIJake 0:85855ecd3257 224 /*
ADIJake 0:85855ecd3257 225 * Get the state of the specified GPIO pin
ADIJake 0:85855ecd3257 226 */
ADIJake 0:85855ecd3257 227 ADI_SENSE_RESULT adi_sense_GpioGet(
ADIJake 0:85855ecd3257 228 ADI_SENSE_GPIO_HANDLE hDevice,
ADIJake 0:85855ecd3257 229 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 230 bool *pbState)
ADIJake 0:85855ecd3257 231 {
ADIJake 0:85855ecd3257 232 GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
ADIJake 0:85855ecd3257 233
ADIJake 0:85855ecd3257 234 return pCtx->get(ePinId, pbState);
ADIJake 0:85855ecd3257 235 }
ADIJake 0:85855ecd3257 236
ADIJake 0:85855ecd3257 237 /*
ADIJake 0:85855ecd3257 238 * Set the state of the specified GPIO pin
ADIJake 0:85855ecd3257 239 */
ADIJake 0:85855ecd3257 240 ADI_SENSE_RESULT adi_sense_GpioSet(
ADIJake 0:85855ecd3257 241 ADI_SENSE_GPIO_HANDLE hDevice,
ADIJake 0:85855ecd3257 242 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 243 bool bState)
ADIJake 0:85855ecd3257 244 {
ADIJake 0:85855ecd3257 245 GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
ADIJake 0:85855ecd3257 246
ADIJake 0:85855ecd3257 247 return pCtx->set(ePinId, bState);
ADIJake 0:85855ecd3257 248 }
ADIJake 0:85855ecd3257 249
ADIJake 0:85855ecd3257 250 /*
ADIJake 0:85855ecd3257 251 * Enable interrupt notifications on the specified GPIO pin
ADIJake 0:85855ecd3257 252 */
ADIJake 0:85855ecd3257 253 ADI_SENSE_RESULT adi_sense_GpioIrqEnable(
ADIJake 0:85855ecd3257 254 ADI_SENSE_GPIO_HANDLE hDevice,
ADIJake 0:85855ecd3257 255 ADI_SENSE_GPIO_PIN ePinId,
ADIJake 0:85855ecd3257 256 ADI_SENSE_GPIO_CALLBACK callback,
ADIJake 0:85855ecd3257 257 void *arg)
ADIJake 0:85855ecd3257 258 {
ADIJake 0:85855ecd3257 259 GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
ADIJake 0:85855ecd3257 260
ADIJake 0:85855ecd3257 261 return pCtx->enableIrq(ePinId, callback, arg);
ADIJake 0:85855ecd3257 262 }
ADIJake 0:85855ecd3257 263
ADIJake 0:85855ecd3257 264 /*
ADIJake 0:85855ecd3257 265 * Disable interrupt notifications on the specified GPIO pin
ADIJake 0:85855ecd3257 266 */
ADIJake 0:85855ecd3257 267 ADI_SENSE_RESULT adi_sense_GpioIrqDisable(
ADIJake 0:85855ecd3257 268 ADI_SENSE_GPIO_HANDLE hDevice,
ADIJake 0:85855ecd3257 269 ADI_SENSE_GPIO_PIN ePinId)
ADIJake 0:85855ecd3257 270 {
ADIJake 0:85855ecd3257 271 GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
ADIJake 0:85855ecd3257 272
ADIJake 0:85855ecd3257 273 return pCtx->disableIrq(ePinId);
ADIJake 0:85855ecd3257 274 }
ADIJake 0:85855ecd3257 275
ADIJake 0:85855ecd3257 276 /*
ADIJake 0:85855ecd3257 277 * Close the GPIO interface and free resources
ADIJake 0:85855ecd3257 278 */
ADIJake 0:85855ecd3257 279 void adi_sense_GpioClose(
ADIJake 0:85855ecd3257 280 ADI_SENSE_GPIO_HANDLE hDevice)
ADIJake 0:85855ecd3257 281 {
ADIJake 0:85855ecd3257 282 GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
ADIJake 0:85855ecd3257 283
ADIJake 0:85855ecd3257 284 delete pCtx;
ADIJake 0:85855ecd3257 285 }
ADIJake 0:85855ecd3257 286
ADIJake 0:85855ecd3257 287 #ifdef __cplusplus
ADIJake 0:85855ecd3257 288 }
ADIJake 0:85855ecd3257 289 #endif
ADIJake 0:85855ecd3257 290