ADISense1000 Version 2.1 code base

Fork of AdiSense1000_V21 by Sean Wilson

Committer:
Dan O'Donovan
Date:
Mon Mar 26 20:28:05 2018 +0100
Branch:
v2.0
Revision:
27:567abf893938
Parent:
26:12d0204be712
Child:
30:119ff4f3aef6
Adding host library and example code for v1.1 release

Who changed what in which revision?

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