ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
danodonovan
Date:
Mon Jan 22 17:40:50 2018 +0000
Revision:
24:5a2272a25ff1
Updating example to adisense1000 v1.0.1

Who changed what in which revision?

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