Analog Devices / Mbed OS EVAL-ADMX2001

Dependencies:   ADMX2001

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 <mbed.h>
00019 
00020 #include "platform_drivers.h"
00021 #include "gpio_extra.h"
00022 
00023 /******************************************************************************/
00024 /************************ Functions Definitions *******************************/
00025 /******************************************************************************/
00026 
00027 /**
00028  * @brief Obtain the GPIO decriptor.
00029  * @param desc - The GPIO descriptor.
00030  * @param gpio_number - The number of the GPIO.
00031  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00032  */
00033 int32_t gpio_get(struct gpio_desc **desc, const gpio_init_param *param)
00034 {
00035     if (desc) {
00036         // Create the gpio description object for the device
00037         gpio_desc *new_gpio = (gpio_desc *)malloc(sizeof(gpio_desc));
00038         if (new_gpio == NULL) {
00039             return ADI_FAILURE;
00040         }
00041 
00042         new_gpio->number = param->number;
00043         *desc = new_gpio;
00044 
00045         return ADI_SUCCESS;
00046     }
00047 
00048     return ADI_FAILURE;
00049 }
00050 
00051 
00052 /**
00053  * @brief Free the resources allocated by gpio_get().
00054  * @param desc - The GPIO descriptor.
00055  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00056  */
00057 int32_t gpio_remove(struct gpio_desc *desc)
00058 {
00059     if (desc) {
00060         // Free the gpio object
00061         if (((mbed_gpio_desc *)(desc->extra))->gpio_pin) {
00062             free(((mbed_gpio_desc *)(desc->extra))->gpio_pin);
00063         }
00064 
00065         // Free the gpio extra descriptor object
00066         if ((mbed_gpio_desc *)(desc->extra)) {
00067             free((mbed_gpio_desc *)(desc->extra));
00068         }
00069 
00070         // Free the gpio descriptor object
00071         free(desc);
00072 
00073         return ADI_SUCCESS;
00074     }
00075 
00076     return ADI_FAILURE;
00077 }
00078 
00079 
00080 /**
00081  * @brief Enable the input direction of the specified GPIO.
00082  * @param desc - The GPIO descriptor.
00083  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00084  */
00085 int32_t gpio_direction_input(struct gpio_desc *desc)
00086 {
00087     DigitalIn *gpio_input;      // pointer to gpio input object
00088     mbed_gpio_desc *gpio_desc_extra;  // pointer to gpio desc extra parameters
00089 
00090     if (desc) {
00091         // Configure and instantiate GPIO pin as input
00092         gpio_input = new DigitalIn((PinName)desc->number);
00093         if (gpio_input == NULL) {
00094             return ADI_FAILURE;
00095         }
00096 
00097         // Create the gpio extra descriptor object to store new gpio instance
00098         gpio_desc_extra = (mbed_gpio_desc *)malloc(sizeof(mbed_gpio_desc));
00099         if (gpio_desc_extra == NULL) {
00100             return ADI_FAILURE;
00101         }
00102 
00103         gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input;
00104         desc->extra = (mbed_gpio_desc *)gpio_desc_extra;
00105 
00106         // Set the gpio pin mode
00107         gpio_input->mode((PinMode)((mbed_gpio_init_param *)desc->extra)->pin_mode);
00108 
00109         return ADI_SUCCESS;
00110     }
00111 
00112     return ADI_FAILURE;
00113 }
00114 
00115 
00116 /**
00117  * @brief Enable the output direction of the specified GPIO.
00118  * @param desc - The GPIO descriptor.
00119  * @param value - The value.
00120  *                Example: GPIO_HIGH
00121  *                         GPIO_LOW
00122  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00123  */
00124 int32_t gpio_direction_output(struct gpio_desc *desc, uint8_t value)
00125 {
00126     DigitalOut *gpio_output;    // pointer to gpio output object
00127     mbed_gpio_desc *gpio_desc_extra;  // pointer to gpio desc extra parameters
00128 
00129     if(desc) {
00130         // Configure and instantiate GPIO pin as output
00131         gpio_output = new DigitalOut((PinName)desc->number);
00132         if (gpio_output == NULL) {
00133             return ADI_FAILURE;
00134         }
00135 
00136         // Create the gpio extra descriptor object to store new gpio instance
00137         gpio_desc_extra = (mbed_gpio_desc *)malloc(sizeof(mbed_gpio_desc));
00138         if (gpio_desc_extra == NULL) {
00139             return ADI_FAILURE;
00140         }
00141 
00142         gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output;
00143         desc->extra = (mbed_gpio_desc *)gpio_desc_extra;
00144 
00145         return ADI_SUCCESS;
00146     }
00147 
00148     if (value) {
00149         // Unused variable - fix compiler warning
00150     }
00151 
00152     return ADI_FAILURE;
00153 }
00154 
00155 
00156 /**
00157  * @brief Get the direction of the specified GPIO.
00158  * @param desc - The GPIO descriptor.
00159  * @param direction - The direction.
00160  *                    Example: GPIO_OUT
00161  *                             GPIO_IN
00162  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00163  */
00164 int32_t gpio_get_direction(struct gpio_desc *desc, uint8_t *direction)
00165 {
00166     if (desc) {
00167         // Unused variable - fix compiler warning
00168     }
00169 
00170     if (direction) {
00171         // Unused variable - fix compiler warning
00172     }
00173 
00174     return ADI_SUCCESS;
00175 }
00176 
00177 
00178 /**
00179  * @brief Set the value of the specified GPIO.
00180  * @param desc - The GPIO descriptor.
00181  * @param value - The value.
00182  *                Example: GPIO_HIGH
00183  *                         GPIO_LOW
00184  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00185  */
00186 int32_t gpio_set_value(struct gpio_desc *desc, uint8_t value)
00187 {
00188     DigitalOut *gpio_output;        // pointer to gpio output object
00189 
00190     if (desc) {
00191         gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
00192         gpio_output->write(value);
00193 
00194         return ADI_SUCCESS;
00195     }
00196 
00197     if (value) {
00198         // Unused variable - fix compiler warning
00199     }
00200 
00201     return ADI_FAILURE;
00202 }
00203 
00204 
00205 /**
00206  * @brief Get the value of the specified GPIO.
00207  * @param desc - The GPIO descriptor.
00208  * @param value - The value.
00209  *                Example: GPIO_HIGH
00210  *                         GPIO_LOW
00211  * @return ADI_SUCCESS in case of success, ADI_FAILURE otherwise.
00212  */
00213 int32_t gpio_get_value(struct gpio_desc *desc, uint8_t *value)
00214 {
00215     DigitalIn *gpio_input;      // pointer to gpio input object
00216     uint8_t returnVal = ADI_FAILURE;
00217 
00218     if (desc) {
00219         gpio_input = (DigitalIn *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
00220         *value = (uint8_t)gpio_input->read();
00221         returnVal = gpio_input->is_connected() ? ADI_SUCCESS : ADI_FAILURE;
00222 
00223         return returnVal;
00224     }
00225 
00226     return ADI_FAILURE;
00227 }