ADMW1001_Mbed_FW / Mbed OS EV-PRO-MW1001_Development
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 2019 (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 csPin,
00055         PinName alertErrorPin,
00056         PinName datareadyPin)
00057         : _reset(resetPin),
00058           _cs(csPin),
00059           _alertError(alertErrorPin),
00060           _dataready(datareadyPin),
00061           _alertErrorIrq(alertErrorPin),
00062           _datareadyIrq(datareadyPin) {}
00063 
00064     ADMW_RESULT  get(
00065         ADMW_GPIO_PIN  ePinId,
00066         bool *pState);
00067 
00068     ADMW_RESULT  set(
00069         ADMW_GPIO_PIN  ePinId,
00070         bool state);
00071 
00072     ADMW_RESULT  enableIrq(
00073         ADMW_GPIO_PIN  ePinId,
00074         ADMW_GPIO_CALLBACK  callbackFn,
00075         void *pArg);
00076 
00077     ADMW_RESULT  disableIrq(
00078         ADMW_GPIO_PIN  ePinId);
00079 
00080 private:
00081     DigitalOut  _reset;
00082     DigitalOut  _cs;
00083     
00084     DigitalIn   _alertError;
00085     DigitalIn   _dataready;
00086 
00087     InterruptIn _alertErrorIrq;
00088     InterruptIn _datareadyIrq;
00089 
00090     ADMW_GPIO_CALLBACK  _alertErrorIrqCallback;
00091     ADMW_GPIO_CALLBACK  _datareadyIrqCallback;
00092 
00093     void *_alertErrorIrqArg;
00094     void *_datareadyIrqArg;
00095 
00096     void _alertErrorIrqHandler()
00097     {
00098         _alertErrorIrqCallback(ADMW_GPIO_PIN_ALERT_ERROR , _alertErrorIrqArg);
00099     }
00100     void _datareadyIrqHandler()
00101     {
00102         _datareadyIrqCallback(ADMW_GPIO_PIN_DATAREADY , _datareadyIrqArg);
00103     }
00104 };
00105 
00106 ADMW_RESULT  GpioContext::get(
00107     ADMW_GPIO_PIN  ePinId,
00108     bool *pState)
00109 {
00110     switch(ePinId)
00111     {
00112     case ADMW_GPIO_PIN_ALERT_ERROR :
00113         *pState = _alertError;
00114         return ADMW_SUCCESS ;
00115     case ADMW_GPIO_PIN_DATAREADY :
00116         *pState = _dataready;
00117         return ADMW_SUCCESS ;
00118     case ADMW_GPIO_PIN_RESET :
00119         *pState = _reset;
00120         return ADMW_SUCCESS ;
00121     case ADMW_GPIO_PIN_CS :
00122         *pState = _cs;
00123         return ADMW_SUCCESS ;
00124     default:
00125         return ADMW_INVALID_DEVICE_NUM ;
00126     }
00127 }
00128 
00129 ADMW_RESULT  GpioContext::set(
00130     ADMW_GPIO_PIN  ePinId,
00131     bool state)
00132 {
00133     switch(ePinId)
00134     {
00135     case ADMW_GPIO_PIN_RESET :
00136         _reset = state;
00137         break;
00138     case ADMW_GPIO_PIN_CS :
00139         _cs = state;
00140         break;
00141     default:
00142         return ADMW_INVALID_DEVICE_NUM ;
00143     }
00144 
00145     return ADMW_SUCCESS ;
00146 }
00147 
00148 ADMW_RESULT  GpioContext::enableIrq(
00149     ADMW_GPIO_PIN  ePinId,
00150     ADMW_GPIO_CALLBACK  callbackFn,
00151     void *pArg)
00152 {
00153     switch(ePinId)
00154     {
00155     case ADMW_GPIO_PIN_ALERT_ERROR :
00156         _alertErrorIrqCallback = callbackFn;
00157         _alertErrorIrqArg = pArg;
00158         _alertErrorIrq.rise(callback(this, &GpioContext::_alertErrorIrqHandler));
00159         return ADMW_SUCCESS ;
00160     case ADMW_GPIO_PIN_DATAREADY :
00161         _datareadyIrqCallback = callbackFn;
00162         _datareadyIrqArg = pArg;
00163         _datareadyIrq.rise(callback(this, &GpioContext::_datareadyIrqHandler));
00164         return ADMW_SUCCESS ;
00165     default:
00166         return ADMW_INVALID_DEVICE_NUM ;
00167     }
00168 }
00169 
00170 ADMW_RESULT  GpioContext::disableIrq(
00171     ADMW_GPIO_PIN  ePinId)
00172 {
00173     switch(ePinId) {
00174         case ADMW_GPIO_PIN_ALERT_ERROR :
00175             _alertErrorIrq.rise(NULL);
00176             return ADMW_SUCCESS ;
00177         case ADMW_GPIO_PIN_DATAREADY :
00178             _datareadyIrq.rise(NULL);
00179             return ADMW_SUCCESS ;
00180         default:
00181             return ADMW_INVALID_DEVICE_NUM ;
00182     }
00183 }
00184 
00185 #ifdef __cplusplus
00186 extern "C" {
00187 #endif
00188 
00189 /*
00190  * Open the GPIO interface and allocate resources
00191  */
00192 ADMW_RESULT  admw_GpioOpen(
00193     ADMW_PLATFORM_GPIO_CONFIG *pConfig,
00194     ADMW_GPIO_HANDLE  *phDevice)
00195 {
00196     GpioContext *pCtx = new GpioContext((PinName)pConfig->resetPin,
00197                                         (PinName)pConfig->csPin,
00198                                         (PinName)pConfig->alertErrorPin,
00199                                         (PinName)pConfig->datareadyPin);
00200     if (!pCtx) {
00201         ADMW_LOG_ERROR("Failed to allocate memory for GPIO context");
00202         return ADMW_NO_MEM ;
00203     }
00204 
00205     *phDevice = reinterpret_cast<ADMW_GPIO_HANDLE >(pCtx);
00206     return ADMW_SUCCESS ;
00207 }
00208 
00209 /*
00210  * Get the state of the specified GPIO pin
00211  */
00212 ADMW_RESULT  admw_GpioGet(
00213     ADMW_GPIO_HANDLE  hDevice,
00214     ADMW_GPIO_PIN  ePinId,
00215     bool *pbState)
00216 {
00217     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00218 
00219     return pCtx->get(ePinId, pbState);
00220 }
00221 
00222 /*
00223  * Set the state of the specified GPIO pin
00224  */
00225 ADMW_RESULT  admw_GpioSet(
00226     ADMW_GPIO_HANDLE  hDevice,
00227     ADMW_GPIO_PIN  ePinId,
00228     bool bState)
00229 {
00230     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00231 
00232     return pCtx->set(ePinId, bState);
00233 }
00234 
00235 /*
00236  * Enable interrupt notifications on the specified GPIO pin
00237  */
00238 ADMW_RESULT  admw_GpioIrqEnable(
00239     ADMW_GPIO_HANDLE  hDevice,
00240     ADMW_GPIO_PIN  ePinId,
00241     ADMW_GPIO_CALLBACK  callback,
00242     void *arg)
00243 {
00244     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00245 
00246     return pCtx->enableIrq(ePinId, callback, arg);
00247 }
00248 
00249 /*
00250  * Disable interrupt notifications on the specified GPIO pin
00251  */
00252 ADMW_RESULT  admw_GpioIrqDisable(
00253     ADMW_GPIO_HANDLE  hDevice,
00254     ADMW_GPIO_PIN  ePinId)
00255 {
00256     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00257 
00258     return pCtx->disableIrq(ePinId);
00259 }
00260 
00261 /*
00262  * Close the GPIO interface and free resources
00263  */
00264 void admw_GpioClose(
00265     ADMW_GPIO_HANDLE  hDevice)
00266 {
00267     GpioContext *pCtx = reinterpret_cast<GpioContext *>(hDevice);
00268 
00269     delete pCtx;
00270 }
00271 
00272 #ifdef __cplusplus
00273 }
00274 #endif