PoC_Team / Mbed OS ADMW1001v0510002
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     {
00109     case ADMW_GPIO_PIN_ALERT_ERROR :
00110         *pState = _alertError;
00111         return ADMW_SUCCESS ;
00112     case ADMW_GPIO_PIN_DATAREADY :
00113         *pState = _dataready;
00114         return ADMW_SUCCESS ;
00115     case ADMW_GPIO_PIN_RESET :
00116         *pState = _reset;
00117         return ADMW_SUCCESS ;
00118     default:
00119         return ADMW_INVALID_DEVICE_NUM ;
00120     }
00121 }
00122 
00123 ADMW_RESULT  GpioContext::set(
00124     ADMW_GPIO_PIN  ePinId,
00125     bool state)
00126 {
00127     switch(ePinId)
00128     {
00129     case ADMW_GPIO_PIN_RESET :
00130         _reset = state;
00131         break;
00132     default:
00133         return ADMW_INVALID_DEVICE_NUM ;
00134     }
00135 
00136     return ADMW_SUCCESS ;
00137 }
00138 
00139 ADMW_RESULT  GpioContext::enableIrq(
00140     ADMW_GPIO_PIN  ePinId,
00141     ADMW_GPIO_CALLBACK  callbackFn,
00142     void *pArg)
00143 {
00144     switch(ePinId)
00145     {
00146     case ADMW_GPIO_PIN_ALERT_ERROR :
00147         _alertErrorIrqCallback = callbackFn;
00148         _alertErrorIrqArg = pArg;
00149         _alertErrorIrq.rise(callback(this, &GpioContext::_alertErrorIrqHandler));
00150         return ADMW_SUCCESS ;
00151     case ADMW_GPIO_PIN_DATAREADY :
00152         _datareadyIrqCallback = callbackFn;
00153         _datareadyIrqArg = pArg;
00154         _datareadyIrq.rise(callback(this, &GpioContext::_datareadyIrqHandler));
00155         return ADMW_SUCCESS ;
00156     default:
00157         return ADMW_INVALID_DEVICE_NUM ;
00158     }
00159 }
00160 
00161 ADMW_RESULT  GpioContext::disableIrq(
00162     ADMW_GPIO_PIN  ePinId)
00163 {
00164     switch(ePinId)
00165     {
00166     case ADMW_GPIO_PIN_ALERT_ERROR :
00167         _alertErrorIrq.rise(NULL);
00168         return ADMW_SUCCESS ;
00169     case ADMW_GPIO_PIN_DATAREADY :
00170         _datareadyIrq.rise(NULL);
00171         return ADMW_SUCCESS ;
00172     default:
00173         return ADMW_INVALID_DEVICE_NUM ;
00174     }
00175 }
00176 
00177 #ifdef __cplusplus
00178 extern "C" {
00179 #endif
00180 
00181 /*
00182  * Open the GPIO interface and allocate resources
00183  */
00184 ADMW_RESULT  admw_GpioOpen(
00185     ADMW_PLATFORM_GPIO_CONFIG *pConfig,
00186     ADMW_GPIO_HANDLE  *phDevice)
00187 {
00188     GpioContext *pCtx = new GpioContext((PinName)pConfig->resetPin,
00189                                         (PinName)pConfig->alertErrorPin,
00190                                         (PinName)pConfig->datareadyPin);
00191     if (!pCtx)
00192     {
00193         ADMW_LOG_ERROR("Failed to allocate memory for GPIO context");
00194         return ADMW_NO_MEM ;
00195     }
00196 
00197     *phDevice = reinterpret_cast<ADMW_GPIO_HANDLE >(pCtx);
00198     return ADMW_SUCCESS ;
00199 }
00200 
00201 /*
00202  * Get the state of the specified GPIO pin
00203  */
00204 ADMW_RESULT  admw_GpioGet(
00205     ADMW_GPIO_HANDLE  hDevice,
00206     ADMW_GPIO_PIN  ePinId,
00207     bool *pbState)
00208 {
00209     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00210 
00211     return pCtx->get(ePinId, pbState);
00212 }
00213 
00214 /*
00215  * Set the state of the specified GPIO pin
00216  */
00217 ADMW_RESULT  admw_GpioSet(
00218     ADMW_GPIO_HANDLE  hDevice,
00219     ADMW_GPIO_PIN  ePinId,
00220     bool bState)
00221 {
00222     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00223 
00224     return pCtx->set(ePinId, bState);
00225 }
00226 
00227 /*
00228  * Enable interrupt notifications on the specified GPIO pin
00229  */
00230 ADMW_RESULT  admw_GpioIrqEnable(
00231     ADMW_GPIO_HANDLE  hDevice,
00232     ADMW_GPIO_PIN  ePinId,
00233     ADMW_GPIO_CALLBACK  callback,
00234     void *arg)
00235 {
00236     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00237 
00238     return pCtx->enableIrq(ePinId, callback, arg);
00239 }
00240 
00241 /*
00242  * Disable interrupt notifications on the specified GPIO pin
00243  */
00244 ADMW_RESULT  admw_GpioIrqDisable(
00245     ADMW_GPIO_HANDLE  hDevice,
00246     ADMW_GPIO_PIN  ePinId)
00247 {
00248     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00249 
00250     return pCtx->disableIrq(ePinId);
00251 }
00252 
00253 /*
00254  * Close the GPIO interface and free resources
00255  */
00256 void admw_GpioClose(
00257     ADMW_GPIO_HANDLE  hDevice)
00258 {
00259     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00260 
00261     delete pCtx;
00262 }
00263 
00264 #ifdef __cplusplus
00265 }
00266 #endif