Update platform drivers
Diff: src/gpio.cpp
- Revision:
- 9:9e247b9c9abf
- Parent:
- 8:70fc373a5f46
diff -r 70fc373a5f46 -r 9e247b9c9abf src/gpio.cpp --- a/src/gpio.cpp Wed Feb 26 06:09:13 2020 +0000 +++ b/src/gpio.cpp Mon Jun 15 13:03:55 2020 +0000 @@ -15,6 +15,7 @@ /************************ Includes Files *******************************/ /******************************************************************************/ #include <stdio.h> +#include <stdlib.h> #include <mbed.h> #include "platform_drivers.h" @@ -30,16 +31,29 @@ * @param gpio_number - The number of the GPIO. * @return SUCCESS in case of success, FAILURE otherwise. */ -int32_t gpio_get(struct gpio_desc **desc, const gpio_init_param *param) +int32_t gpio_get(struct gpio_desc **desc, const struct gpio_init_param *param) { - if (desc) { + if (desc && param) { // Create the gpio description object for the device - gpio_desc *new_gpio = (gpio_desc *)malloc(sizeof(gpio_desc)); + gpio_desc *new_gpio = (gpio_desc *)calloc(1, sizeof(gpio_desc)) ; if (new_gpio == NULL) { return FAILURE; } + // Create the gpio extra descriptor object to store extra mbed gpio info + mbed_gpio_desc *gpio_desc_extra = (mbed_gpio_desc *)calloc(1, + sizeof(mbed_gpio_desc)) ; + if (gpio_desc_extra == NULL) { + return FAILURE; + } + new_gpio->number = param->number; + new_gpio->extra = gpio_desc_extra; + + gpio_desc_extra->direction = GPIO_IN; + gpio_desc_extra->gpio_pin = NULL; + gpio_desc_extra->pin_mode = ((mbed_gpio_init_param *)(*desc)->extra)->pin_mode; + *desc = new_gpio; return SUCCESS; @@ -50,6 +64,24 @@ /** + * @brief Get the value of an optional GPIO. + * @param desc - The GPIO descriptor. + * @param param - GPIO Initialization parameters. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t gpio_get_optional(struct gpio_desc **desc, + const struct gpio_init_param *param) +{ + if (param) { + return gpio_get(desc, param); + } else { + *desc = NULL; + return SUCCESS; + } +} + + +/** * @brief Free the resources allocated by gpio_get(). * @param desc - The GPIO descriptor. * @return SUCCESS in case of success, FAILURE otherwise. @@ -58,12 +90,12 @@ { if (desc) { // Free the gpio object - if (((mbed_gpio_desc *)(desc->extra))->gpio_pin) { + if(((mbed_gpio_desc *)(desc->extra))->gpio_pin) { free(((mbed_gpio_desc *)(desc->extra))->gpio_pin); } // Free the gpio extra descriptor object - if ((mbed_gpio_desc *)(desc->extra)) { + if((mbed_gpio_desc *)(desc->extra)) { free((mbed_gpio_desc *)(desc->extra)); } @@ -81,32 +113,30 @@ * @brief Enable the input direction of the specified GPIO. * @param desc - The GPIO descriptor. * @return SUCCESS in case of success, FAILURE otherwise. + * @note does not support reconfiguration of already set pin direction */ int32_t gpio_direction_input(struct gpio_desc *desc) { - DigitalIn *gpio_input; // pointer to gpio input object - mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters - - if (desc) { - // Configure and instantiate GPIO pin as input - gpio_input = new DigitalIn((PinName)desc->number); - if (gpio_input == NULL) { - return FAILURE; - } + DigitalIn *gpio_input; // pointer to gpio input object + mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters - // Create the gpio extra descriptor object to store new gpio instance - gpio_desc_extra = (mbed_gpio_desc *)malloc(sizeof(mbed_gpio_desc)); - if (gpio_desc_extra == NULL) { - return FAILURE; - } + if (desc && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + if (gpio_desc_extra->gpio_pin == NULL) { + // Configure and instantiate GPIO pin as input + gpio_input = new DigitalIn((PinName)desc->number); + if (gpio_input == NULL) { + return FAILURE; + } - gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input; - desc->extra = (mbed_gpio_desc *)gpio_desc_extra; + gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input; + gpio_desc_extra->direction = GPIO_IN; - // Set the gpio pin mode - gpio_input->mode((PinMode)((mbed_gpio_init_param *)desc->extra)->pin_mode); + // Set the gpio pin mode + gpio_input->mode((PinMode)((mbed_gpio_desc *)desc->extra)->pin_mode); - return SUCCESS; + return SUCCESS; + } } return FAILURE; @@ -120,33 +150,32 @@ * Example: GPIO_HIGH * GPIO_LOW * @return SUCCESS in case of success, FAILURE otherwise. + * @note does not support reconfiguration of already set pin direction */ int32_t gpio_direction_output(struct gpio_desc *desc, uint8_t value) { - DigitalOut *gpio_output; // pointer to gpio output object - mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters + DigitalOut *gpio_output; // pointer to gpio output object + mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters - if(desc) { - // Configure and instantiate GPIO pin as output - gpio_output = new DigitalOut((PinName)desc->number); - if (gpio_output == NULL) { - return FAILURE; - } + if(desc && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + if (gpio_desc_extra->gpio_pin == NULL) { + + // Configure and instantiate GPIO pin as output + gpio_output = new DigitalOut((PinName)desc->number); - // Create the gpio extra descriptor object to store new gpio instance - gpio_desc_extra = (mbed_gpio_desc *)malloc(sizeof(mbed_gpio_desc)); - if (gpio_desc_extra == NULL) { - return FAILURE; - } + if (gpio_output == NULL) { + return FAILURE; + } - gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output; - desc->extra = (mbed_gpio_desc *)gpio_desc_extra; + gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output; + gpio_desc_extra->direction = GPIO_OUT; - return SUCCESS; - } - - if (value) { - // Unused variable - fix compiler warning + // Set the GPIO value + if(gpio_set_value(desc, value) == SUCCESS) { + return SUCCESS; + } + } } return FAILURE; @@ -163,15 +192,19 @@ */ int32_t gpio_get_direction(struct gpio_desc *desc, uint8_t *direction) { - if (desc) { - // Unused variable - fix compiler warning + mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters + + if(desc && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + + if (gpio_desc_extra->gpio_pin) { + *direction = gpio_desc_extra->direction; + } + + return SUCCESS; } - if (direction) { - // Unused variable - fix compiler warning - } - - return SUCCESS; + return FAILURE; } @@ -185,19 +218,20 @@ */ int32_t gpio_set_value(struct gpio_desc *desc, uint8_t value) { - DigitalOut *gpio_output; // pointer to gpio output object + DigitalOut *gpio_output; // pointer to gpio output object + mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters - if (desc) { - gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin; - gpio_output->write(value); + if(desc && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + + if (gpio_desc_extra->gpio_pin) { + gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin; + gpio_output->write(value); + } return SUCCESS; } - if (value) { - // Unused variable - fix compiler warning - } - return FAILURE; } @@ -212,10 +246,10 @@ */ int32_t gpio_get_value(struct gpio_desc *desc, uint8_t *value) { - DigitalIn *gpio_input; // pointer to gpio input object + DigitalIn *gpio_input; // pointer to gpio input object uint8_t returnVal = FAILURE; - if (desc) { + if (desc && desc->extra) { gpio_input = (DigitalIn *)((mbed_gpio_desc *)desc->extra)->gpio_pin; *value = (uint8_t)gpio_input->read(); returnVal = gpio_input->is_connected() ? SUCCESS : FAILURE; @@ -224,4 +258,4 @@ } return FAILURE; -} +} \ No newline at end of file