Host API Example for the ADMW1001

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers admw_gpio.cpp Source File

admw_gpio.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 Copyright 2017 (c) Analog Devices, Inc.
00003 
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are met:
00008   - Redistributions of source code must retain the above copyright
00009     notice, this list of conditions and the following disclaimer.
00010   - Redistributions in binary form must reproduce the above copyright
00011     notice, this list of conditions and the following disclaimer in
00012     the documentation and/or other materials provided with the
00013     distribution.
00014   - Neither the name of Analog Devices, Inc. nor the names of its
00015     contributors may be used to endorse or promote products derived
00016     from this software without specific prior written permission.
00017   - The use of this software may or may not infringe the patent rights
00018     of one or more patent holders. This license does not release you
00019     from the requirement that you obtain separate licenses from these
00020     patent holders to use this software.
00021   - Use of the software either in source or binary form, must be run
00022     on or directly connected to an Analog Devices Inc. component.
00023 
00024 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00025 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00026 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00027 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00028 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00030 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *****************************************************************************/
00036 
00037 /*!
00038  ******************************************************************************
00039  * @file:
00040  * @brief:  ADMW OS-dependent wrapper layer for GPIO interface
00041  *-----------------------------------------------------------------------------
00042  */
00043 
00044 #include <mbed.h>
00045 
00046 #include "inc/admw_gpio.h"
00047 #include "inc/admw_log.h"
00048 
00049 class GpioContext
00050 {
00051 public:
00052     GpioContext(
00053         PinName resetPin,
00054         PinName alertErrorPin,
00055         PinName datareadyPin)
00056         : _reset(resetPin),
00057           _alertError(alertErrorPin),
00058           _dataready(datareadyPin),
00059           _alertErrorIrq(alertErrorPin),
00060           _datareadyIrq(datareadyPin) {}
00061 
00062     ADMW_RESULT  get(
00063         ADMW_GPIO_PIN  ePinId,
00064         bool *pState);
00065 
00066     ADMW_RESULT  set(
00067         ADMW_GPIO_PIN  ePinId,
00068         bool state);
00069 
00070     ADMW_RESULT  enableIrq(
00071         ADMW_GPIO_PIN  ePinId,
00072         ADMW_GPIO_CALLBACK  callbackFn,
00073         void *pArg);
00074 
00075     ADMW_RESULT  disableIrq(
00076         ADMW_GPIO_PIN  ePinId);
00077 
00078 private:
00079     DigitalOut  _reset;
00080 
00081     DigitalIn   _alertError;
00082     DigitalIn   _dataready;
00083 
00084     InterruptIn _alertErrorIrq;
00085     InterruptIn _datareadyIrq;
00086 
00087     ADMW_GPIO_CALLBACK  _alertErrorIrqCallback;
00088     ADMW_GPIO_CALLBACK  _datareadyIrqCallback;
00089 
00090     void *_alertErrorIrqArg;
00091     void *_datareadyIrqArg;
00092 
00093     void _alertErrorIrqHandler()
00094     {
00095         _alertErrorIrqCallback(ADMW_GPIO_PIN_ALERT_ERROR , _alertErrorIrqArg);
00096     }
00097     void _datareadyIrqHandler()
00098     {
00099         _datareadyIrqCallback(ADMW_GPIO_PIN_DATAREADY , _datareadyIrqArg);
00100     }
00101 };
00102 
00103 ADMW_RESULT  GpioContext::get(
00104     ADMW_GPIO_PIN  ePinId,
00105     bool *pState)
00106 {
00107     switch(ePinId) {
00108         case ADMW_GPIO_PIN_ALERT_ERROR :
00109             *pState = _alertError;
00110             return ADMW_SUCCESS ;
00111         case ADMW_GPIO_PIN_DATAREADY :
00112             *pState = _dataready;
00113             return ADMW_SUCCESS ;
00114         case ADMW_GPIO_PIN_RESET :
00115             *pState = _reset;
00116             return ADMW_SUCCESS ;
00117         default:
00118             return ADMW_INVALID_DEVICE_NUM ;
00119     }
00120 }
00121 
00122 ADMW_RESULT  GpioContext::set(
00123     ADMW_GPIO_PIN  ePinId,
00124     bool state)
00125 {
00126     switch(ePinId) {
00127         case ADMW_GPIO_PIN_RESET :
00128             _reset = state;
00129             break;
00130         default:
00131             return ADMW_INVALID_DEVICE_NUM ;
00132     }
00133 
00134     return ADMW_SUCCESS ;
00135 }
00136 
00137 ADMW_RESULT  GpioContext::enableIrq(
00138     ADMW_GPIO_PIN  ePinId,
00139     ADMW_GPIO_CALLBACK  callbackFn,
00140     void *pArg)
00141 {
00142     switch(ePinId) {
00143         case ADMW_GPIO_PIN_ALERT_ERROR :
00144             _alertErrorIrqCallback = callbackFn;
00145             _alertErrorIrqArg = pArg;
00146             _alertErrorIrq.rise(callback(this, &GpioContext::_alertErrorIrqHandler));
00147             return ADMW_SUCCESS ;
00148         case ADMW_GPIO_PIN_DATAREADY :
00149             _datareadyIrqCallback = callbackFn;
00150             _datareadyIrqArg = pArg;
00151             _datareadyIrq.rise(callback(this, &GpioContext::_datareadyIrqHandler));
00152             return ADMW_SUCCESS ;
00153         default:
00154             return ADMW_INVALID_DEVICE_NUM ;
00155     }
00156 }
00157 
00158 ADMW_RESULT  GpioContext::disableIrq(
00159     ADMW_GPIO_PIN  ePinId)
00160 {
00161     switch(ePinId) {
00162         case ADMW_GPIO_PIN_ALERT_ERROR :
00163             _alertErrorIrq.rise(NULL);
00164             return ADMW_SUCCESS ;
00165         case ADMW_GPIO_PIN_DATAREADY :
00166             _datareadyIrq.rise(NULL);
00167             return ADMW_SUCCESS ;
00168         default:
00169             return ADMW_INVALID_DEVICE_NUM ;
00170     }
00171 }
00172 
00173 #ifdef __cplusplus
00174 extern "C" {
00175 #endif
00176 
00177 /*
00178  * Open the GPIO interface and allocate resources
00179  */
00180 ADMW_RESULT  admw_GpioOpen(
00181     ADMW_PLATFORM_GPIO_CONFIG *pConfig,
00182     ADMW_GPIO_HANDLE  *phDevice)
00183 {
00184     GpioContext *pCtx = new GpioContext((PinName)pConfig->resetPin,
00185                                         (PinName)pConfig->alertErrorPin,
00186                                         (PinName)pConfig->datareadyPin);
00187     if (!pCtx) {
00188         ADMW_LOG_ERROR("Failed to allocate memory for GPIO context");
00189         return ADMW_NO_MEM ;
00190     }
00191 
00192     *phDevice = reinterpret_cast<ADMW_GPIO_HANDLE >(pCtx);
00193     return ADMW_SUCCESS ;
00194 }
00195 
00196 /*
00197  * Get the state of the specified GPIO pin
00198  */
00199 ADMW_RESULT  admw_GpioGet(
00200     ADMW_GPIO_HANDLE  hDevice,
00201     ADMW_GPIO_PIN  ePinId,
00202     bool *pbState)
00203 {
00204     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00205 
00206     return pCtx->get(ePinId, pbState);
00207 }
00208 
00209 /*
00210  * Set the state of the specified GPIO pin
00211  */
00212 ADMW_RESULT  admw_GpioSet(
00213     ADMW_GPIO_HANDLE  hDevice,
00214     ADMW_GPIO_PIN  ePinId,
00215     bool bState)
00216 {
00217     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00218 
00219     return pCtx->set(ePinId, bState);
00220 }
00221 
00222 /*
00223  * Enable interrupt notifications on the specified GPIO pin
00224  */
00225 ADMW_RESULT  admw_GpioIrqEnable(
00226     ADMW_GPIO_HANDLE  hDevice,
00227     ADMW_GPIO_PIN  ePinId,
00228     ADMW_GPIO_CALLBACK  callback,
00229     void *arg)
00230 {
00231     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00232 
00233     return pCtx->enableIrq(ePinId, callback, arg);
00234 }
00235 
00236 /*
00237  * Disable interrupt notifications on the specified GPIO pin
00238  */
00239 ADMW_RESULT  admw_GpioIrqDisable(
00240     ADMW_GPIO_HANDLE  hDevice,
00241     ADMW_GPIO_PIN  ePinId)
00242 {
00243     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00244 
00245     return pCtx->disableIrq(ePinId);
00246 }
00247 
00248 /*
00249  * Close the GPIO interface and free resources
00250  */
00251 void admw_GpioClose(
00252     ADMW_GPIO_HANDLE  hDevice)
00253 {
00254     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00255 
00256     delete pCtx;
00257 }
00258 
00259 #ifdef __cplusplus
00260 }
00261 #endif