Enda Kilgarriff / platform_drivers
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gpio.cpp Source File

gpio.cpp

Go to the documentation of this file.
00001 /***************************************************************************//**
00002  *   @file   gpio.cpp
00003  *   @brief  Implementation of GPIO No-OS platform driver interfaces
00004 ********************************************************************************
00005  * Copyright (c) 2019, 2020 Analog Devices, Inc.
00006  *
00007  * All rights reserved.
00008  *
00009  * This software is proprietary to Analog Devices, Inc. and its licensors.
00010  * By using this software you agree to the terms of the associated
00011  * Analog Devices Software License Agreement.
00012 *******************************************************************************/
00013 
00014 /******************************************************************************/
00015 /************************ Includes Files *******************************/
00016 /******************************************************************************/
00017 #include <stdio.h>
00018 #include <stdlib.h>
00019 #include <mbed.h>
00020 
00021 #include "platform_drivers.h"
00022 #include "gpio_extra.h"
00023 
00024 /******************************************************************************/
00025 /************************ Functions Definitions *******************************/
00026 /******************************************************************************/
00027 
00028 /**
00029  * @brief Obtain the GPIO decriptor.
00030  * @param desc - The GPIO descriptor.
00031  * @param gpio_number - The number of the GPIO.
00032  * @return SUCCESS in case of success, FAILURE otherwise.
00033  */
00034 int32_t gpio_get(struct gpio_desc **desc, const struct gpio_init_param *param)
00035 {
00036     if (desc && param) {
00037         // Create the gpio description object for the device
00038         gpio_desc *new_gpio = (gpio_desc *)calloc(1, sizeof(gpio_desc)) ;
00039         if (new_gpio == NULL) {
00040             return FAILURE;
00041         }
00042 
00043         // Create the gpio extra descriptor object to store extra mbed gpio info
00044         mbed_gpio_desc *gpio_desc_extra = (mbed_gpio_desc *)calloc(1,
00045                           sizeof(mbed_gpio_desc)) ;
00046         if (gpio_desc_extra == NULL) {
00047             return FAILURE;
00048         }
00049 
00050         new_gpio->number = param->number;
00051         new_gpio->extra = gpio_desc_extra;
00052 
00053         gpio_desc_extra->direction = GPIO_IN;
00054         gpio_desc_extra->gpio_pin = NULL;
00055         gpio_desc_extra->pin_mode = ((mbed_gpio_init_param *)(*desc)->extra)->pin_mode;
00056 
00057         *desc = new_gpio;
00058 
00059         return SUCCESS;
00060     }
00061 
00062     return FAILURE;
00063 }
00064 
00065 
00066 /**
00067  * @brief Get the value of an optional GPIO.
00068  * @param desc - The GPIO descriptor.
00069  * @param param - GPIO Initialization parameters.
00070  * @return SUCCESS in case of success, FAILURE otherwise.
00071  */
00072 int32_t gpio_get_optional(struct gpio_desc **desc,
00073               const struct gpio_init_param *param)
00074 {
00075     if (param) {
00076         return gpio_get(desc, param);
00077     } else {
00078         *desc = NULL;
00079         return SUCCESS;
00080     }
00081 }
00082 
00083 
00084 /**
00085  * @brief Free the resources allocated by gpio_get().
00086  * @param desc - The GPIO descriptor.
00087  * @return SUCCESS in case of success, FAILURE otherwise.
00088  */
00089 int32_t gpio_remove(struct gpio_desc *desc)
00090 {
00091     if (desc) {
00092         // Free the gpio object
00093         if(((mbed_gpio_desc *)(desc->extra))->gpio_pin) {
00094             free(((mbed_gpio_desc *)(desc->extra))->gpio_pin);
00095         }
00096 
00097         // Free the gpio extra descriptor object
00098         if((mbed_gpio_desc *)(desc->extra)) {
00099             free((mbed_gpio_desc *)(desc->extra));
00100         }
00101 
00102         // Free the gpio descriptor object
00103         free(desc);
00104 
00105         return SUCCESS;
00106     }
00107 
00108     return FAILURE;
00109 }
00110 
00111 
00112 /**
00113  * @brief Enable the input direction of the specified GPIO.
00114  * @param desc - The GPIO descriptor.
00115  * @return SUCCESS in case of success, FAILURE otherwise.
00116  * @note does not support reconfiguration of already set pin direction
00117  */
00118 int32_t gpio_direction_input(struct gpio_desc *desc)
00119 {
00120     DigitalIn *gpio_input;      // pointer to gpio input object
00121     mbed_gpio_desc *gpio_desc_extra;   // pointer to gpio desc extra parameters
00122 
00123     if (desc && desc->extra) {
00124         gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
00125         if (gpio_desc_extra->gpio_pin == NULL) {
00126             // Configure and instantiate GPIO pin as input
00127             gpio_input = new DigitalIn((PinName)desc->number);
00128             if (gpio_input == NULL) {
00129                 return FAILURE;
00130             }
00131 
00132             gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input;
00133             gpio_desc_extra->direction = GPIO_IN;
00134 
00135             // Set the gpio pin mode
00136             gpio_input->mode((PinMode)((mbed_gpio_desc *)desc->extra)->pin_mode);
00137 
00138             return SUCCESS;
00139         }
00140     }
00141 
00142     return FAILURE;
00143 }
00144 
00145 
00146 /**
00147  * @brief Enable the output direction of the specified GPIO.
00148  * @param desc - The GPIO descriptor.
00149  * @param value - The value.
00150  *                Example: GPIO_HIGH
00151  *                         GPIO_LOW
00152  * @return SUCCESS in case of success, FAILURE otherwise.
00153  * @note does not support reconfiguration of already set pin direction
00154  */
00155 int32_t gpio_direction_output(struct gpio_desc *desc, uint8_t value)
00156 {
00157     DigitalOut *gpio_output;        // pointer to gpio output object
00158     mbed_gpio_desc *gpio_desc_extra;   // pointer to gpio desc extra parameters
00159 
00160     if(desc && desc->extra) {
00161         gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
00162         if (gpio_desc_extra->gpio_pin == NULL) {
00163 
00164             // Configure and instantiate GPIO pin as output
00165             gpio_output = new DigitalOut((PinName)desc->number);
00166 
00167             if (gpio_output == NULL) {
00168                 return FAILURE;
00169             }
00170 
00171             gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output;
00172             gpio_desc_extra->direction = GPIO_OUT;
00173 
00174             // Set the GPIO value
00175             if(gpio_set_value(desc, value) == SUCCESS) {
00176                 return SUCCESS;
00177             }
00178         }
00179     }
00180 
00181     return FAILURE;
00182 }
00183 
00184 
00185 /**
00186  * @brief Get the direction of the specified GPIO.
00187  * @param desc - The GPIO descriptor.
00188  * @param direction - The direction.
00189  *                    Example: GPIO_OUT
00190  *                             GPIO_IN
00191  * @return SUCCESS in case of success, FAILURE otherwise.
00192  */
00193 int32_t gpio_get_direction(struct gpio_desc *desc, uint8_t *direction)
00194 {
00195     mbed_gpio_desc *gpio_desc_extra;     // pointer to gpio desc extra parameters
00196 
00197     if(desc && desc->extra) {
00198         gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
00199 
00200         if (gpio_desc_extra->gpio_pin) {
00201             *direction = gpio_desc_extra->direction;
00202         }
00203 
00204         return SUCCESS;
00205     }
00206 
00207     return FAILURE;
00208 }
00209 
00210 
00211 /**
00212  * @brief Set the value of the specified GPIO.
00213  * @param desc - The GPIO descriptor.
00214  * @param value - The value.
00215  *                Example: GPIO_HIGH
00216  *                         GPIO_LOW
00217  * @return SUCCESS in case of success, FAILURE otherwise.
00218  */
00219 int32_t gpio_set_value(struct gpio_desc *desc, uint8_t value)
00220 {
00221     DigitalOut *gpio_output;        // pointer to gpio output object
00222     mbed_gpio_desc *gpio_desc_extra;    // pointer to gpio desc extra parameters
00223 
00224     if(desc && desc->extra) {
00225         gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
00226 
00227         if (gpio_desc_extra->gpio_pin) {
00228             gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
00229             gpio_output->write(value);
00230         }
00231 
00232         return SUCCESS;
00233     }
00234 
00235     return FAILURE;
00236 }
00237 
00238 
00239 /**
00240  * @brief Get the value of the specified GPIO.
00241  * @param desc - The GPIO descriptor.
00242  * @param value - The value.
00243  *                Example: GPIO_HIGH
00244  *                         GPIO_LOW
00245  * @return SUCCESS in case of success, FAILURE otherwise.
00246  */
00247 int32_t gpio_get_value(struct gpio_desc *desc, uint8_t *value)
00248 {
00249     DigitalIn *gpio_input;      // pointer to gpio input object
00250     uint8_t returnVal = FAILURE;
00251 
00252     if (desc && desc->extra) {
00253         gpio_input = (DigitalIn *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
00254         *value = (uint8_t)gpio_input->read();
00255         returnVal = gpio_input->is_connected() ? SUCCESS : FAILURE;
00256 
00257         return returnVal;
00258     }
00259 
00260     return FAILURE;
00261 }