Example Code to get you up and running with ADISense1000

Fork of ADISense_ExampleFirmware by Sean Wilson

Committer:
seanwilson10
Date:
Thu Jan 25 16:00:23 2018 +0000
Revision:
0:76fed7dd9235
initial;

Who changed what in which revision?

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