Platform drivers for Mbed.

Dependents:   EVAL-CN0535-FMCZ EVAL-CN0535-FMCZ EVAL-AD568x-AD569x EVAL-AD7606 ... more

Committer:
mahphalke
Date:
Fri Mar 19 12:10:16 2021 +0530
Revision:
16:61ad39564f45
Parent:
9:9e247b9c9abf
Added uart changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mahphalke 8:70fc373a5f46 1 /***************************************************************************//**
mahphalke 8:70fc373a5f46 2 * @file gpio.cpp
mahphalke 8:70fc373a5f46 3 * @brief Implementation of GPIO No-OS platform driver interfaces
mahphalke 8:70fc373a5f46 4 ********************************************************************************
mahphalke 8:70fc373a5f46 5 * Copyright (c) 2019, 2020 Analog Devices, Inc.
mahphalke 8:70fc373a5f46 6 *
mahphalke 8:70fc373a5f46 7 * All rights reserved.
mahphalke 8:70fc373a5f46 8 *
mahphalke 8:70fc373a5f46 9 * This software is proprietary to Analog Devices, Inc. and its licensors.
mahphalke 8:70fc373a5f46 10 * By using this software you agree to the terms of the associated
mahphalke 8:70fc373a5f46 11 * Analog Devices Software License Agreement.
mahphalke 8:70fc373a5f46 12 *******************************************************************************/
mahphalke 8:70fc373a5f46 13
mahphalke 8:70fc373a5f46 14 /******************************************************************************/
mahphalke 8:70fc373a5f46 15 /************************ Includes Files *******************************/
mahphalke 8:70fc373a5f46 16 /******************************************************************************/
mahphalke 8:70fc373a5f46 17 #include <stdio.h>
EndaKilgarriff 9:9e247b9c9abf 18 #include <stdlib.h>
mahphalke 8:70fc373a5f46 19 #include <mbed.h>
mahphalke 8:70fc373a5f46 20
mahphalke 8:70fc373a5f46 21 #include "platform_drivers.h"
mahphalke 8:70fc373a5f46 22 #include "gpio_extra.h"
mahphalke 8:70fc373a5f46 23
mahphalke 8:70fc373a5f46 24 /******************************************************************************/
mahphalke 8:70fc373a5f46 25 /************************ Functions Definitions *******************************/
mahphalke 8:70fc373a5f46 26 /******************************************************************************/
mahphalke 8:70fc373a5f46 27
mahphalke 8:70fc373a5f46 28 /**
mahphalke 8:70fc373a5f46 29 * @brief Obtain the GPIO decriptor.
mahphalke 8:70fc373a5f46 30 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 31 * @param gpio_number - The number of the GPIO.
mahphalke 8:70fc373a5f46 32 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 33 */
EndaKilgarriff 9:9e247b9c9abf 34 int32_t gpio_get(struct gpio_desc **desc, const struct gpio_init_param *param)
mahphalke 8:70fc373a5f46 35 {
EndaKilgarriff 9:9e247b9c9abf 36 if (desc && param) {
mahphalke 8:70fc373a5f46 37 // Create the gpio description object for the device
EndaKilgarriff 9:9e247b9c9abf 38 gpio_desc *new_gpio = (gpio_desc *)calloc(1, sizeof(gpio_desc)) ;
mahphalke 8:70fc373a5f46 39 if (new_gpio == NULL) {
mahphalke 8:70fc373a5f46 40 return FAILURE;
mahphalke 8:70fc373a5f46 41 }
mahphalke 8:70fc373a5f46 42
EndaKilgarriff 9:9e247b9c9abf 43 // Create the gpio extra descriptor object to store extra mbed gpio info
EndaKilgarriff 9:9e247b9c9abf 44 mbed_gpio_desc *gpio_desc_extra = (mbed_gpio_desc *)calloc(1,
EndaKilgarriff 9:9e247b9c9abf 45 sizeof(mbed_gpio_desc)) ;
EndaKilgarriff 9:9e247b9c9abf 46 if (gpio_desc_extra == NULL) {
EndaKilgarriff 9:9e247b9c9abf 47 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 48 }
EndaKilgarriff 9:9e247b9c9abf 49
mahphalke 8:70fc373a5f46 50 new_gpio->number = param->number;
EndaKilgarriff 9:9e247b9c9abf 51 new_gpio->extra = gpio_desc_extra;
EndaKilgarriff 9:9e247b9c9abf 52
EndaKilgarriff 9:9e247b9c9abf 53 gpio_desc_extra->direction = GPIO_IN;
EndaKilgarriff 9:9e247b9c9abf 54 gpio_desc_extra->gpio_pin = NULL;
EndaKilgarriff 9:9e247b9c9abf 55 gpio_desc_extra->pin_mode = ((mbed_gpio_init_param *)(*desc)->extra)->pin_mode;
EndaKilgarriff 9:9e247b9c9abf 56
mahphalke 8:70fc373a5f46 57 *desc = new_gpio;
mahphalke 8:70fc373a5f46 58
mahphalke 8:70fc373a5f46 59 return SUCCESS;
mahphalke 8:70fc373a5f46 60 }
mahphalke 8:70fc373a5f46 61
mahphalke 8:70fc373a5f46 62 return FAILURE;
mahphalke 8:70fc373a5f46 63 }
mahphalke 8:70fc373a5f46 64
mahphalke 8:70fc373a5f46 65
mahphalke 8:70fc373a5f46 66 /**
EndaKilgarriff 9:9e247b9c9abf 67 * @brief Get the value of an optional GPIO.
EndaKilgarriff 9:9e247b9c9abf 68 * @param desc - The GPIO descriptor.
EndaKilgarriff 9:9e247b9c9abf 69 * @param param - GPIO Initialization parameters.
EndaKilgarriff 9:9e247b9c9abf 70 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 71 */
EndaKilgarriff 9:9e247b9c9abf 72 int32_t gpio_get_optional(struct gpio_desc **desc,
EndaKilgarriff 9:9e247b9c9abf 73 const struct gpio_init_param *param)
EndaKilgarriff 9:9e247b9c9abf 74 {
EndaKilgarriff 9:9e247b9c9abf 75 if (param) {
EndaKilgarriff 9:9e247b9c9abf 76 return gpio_get(desc, param);
EndaKilgarriff 9:9e247b9c9abf 77 } else {
EndaKilgarriff 9:9e247b9c9abf 78 *desc = NULL;
EndaKilgarriff 9:9e247b9c9abf 79 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 80 }
EndaKilgarriff 9:9e247b9c9abf 81 }
EndaKilgarriff 9:9e247b9c9abf 82
EndaKilgarriff 9:9e247b9c9abf 83
EndaKilgarriff 9:9e247b9c9abf 84 /**
mahphalke 8:70fc373a5f46 85 * @brief Free the resources allocated by gpio_get().
mahphalke 8:70fc373a5f46 86 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 87 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 88 */
mahphalke 8:70fc373a5f46 89 int32_t gpio_remove(struct gpio_desc *desc)
mahphalke 8:70fc373a5f46 90 {
mahphalke 8:70fc373a5f46 91 if (desc) {
mahphalke 8:70fc373a5f46 92 // Free the gpio object
EndaKilgarriff 9:9e247b9c9abf 93 if(((mbed_gpio_desc *)(desc->extra))->gpio_pin) {
mahphalke 8:70fc373a5f46 94 free(((mbed_gpio_desc *)(desc->extra))->gpio_pin);
mahphalke 8:70fc373a5f46 95 }
mahphalke 8:70fc373a5f46 96
mahphalke 8:70fc373a5f46 97 // Free the gpio extra descriptor object
EndaKilgarriff 9:9e247b9c9abf 98 if((mbed_gpio_desc *)(desc->extra)) {
mahphalke 8:70fc373a5f46 99 free((mbed_gpio_desc *)(desc->extra));
mahphalke 8:70fc373a5f46 100 }
mahphalke 8:70fc373a5f46 101
mahphalke 8:70fc373a5f46 102 // Free the gpio descriptor object
mahphalke 8:70fc373a5f46 103 free(desc);
mahphalke 8:70fc373a5f46 104
mahphalke 8:70fc373a5f46 105 return SUCCESS;
mahphalke 8:70fc373a5f46 106 }
mahphalke 8:70fc373a5f46 107
mahphalke 8:70fc373a5f46 108 return FAILURE;
mahphalke 8:70fc373a5f46 109 }
mahphalke 8:70fc373a5f46 110
mahphalke 8:70fc373a5f46 111
mahphalke 8:70fc373a5f46 112 /**
mahphalke 8:70fc373a5f46 113 * @brief Enable the input direction of the specified GPIO.
mahphalke 8:70fc373a5f46 114 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 115 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 116 * @note does not support reconfiguration of already set pin direction
mahphalke 8:70fc373a5f46 117 */
mahphalke 8:70fc373a5f46 118 int32_t gpio_direction_input(struct gpio_desc *desc)
mahphalke 8:70fc373a5f46 119 {
EndaKilgarriff 9:9e247b9c9abf 120 DigitalIn *gpio_input; // pointer to gpio input object
EndaKilgarriff 9:9e247b9c9abf 121 mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters
mahphalke 8:70fc373a5f46 122
EndaKilgarriff 9:9e247b9c9abf 123 if (desc && desc->extra) {
EndaKilgarriff 9:9e247b9c9abf 124 gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
EndaKilgarriff 9:9e247b9c9abf 125 if (gpio_desc_extra->gpio_pin == NULL) {
EndaKilgarriff 9:9e247b9c9abf 126 // Configure and instantiate GPIO pin as input
EndaKilgarriff 9:9e247b9c9abf 127 gpio_input = new DigitalIn((PinName)desc->number);
EndaKilgarriff 9:9e247b9c9abf 128 if (gpio_input == NULL) {
EndaKilgarriff 9:9e247b9c9abf 129 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 130 }
mahphalke 8:70fc373a5f46 131
EndaKilgarriff 9:9e247b9c9abf 132 gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input;
EndaKilgarriff 9:9e247b9c9abf 133 gpio_desc_extra->direction = GPIO_IN;
mahphalke 8:70fc373a5f46 134
EndaKilgarriff 9:9e247b9c9abf 135 // Set the gpio pin mode
EndaKilgarriff 9:9e247b9c9abf 136 gpio_input->mode((PinMode)((mbed_gpio_desc *)desc->extra)->pin_mode);
mahphalke 8:70fc373a5f46 137
EndaKilgarriff 9:9e247b9c9abf 138 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 139 }
mahphalke 8:70fc373a5f46 140 }
mahphalke 8:70fc373a5f46 141
mahphalke 8:70fc373a5f46 142 return FAILURE;
mahphalke 8:70fc373a5f46 143 }
mahphalke 8:70fc373a5f46 144
mahphalke 8:70fc373a5f46 145
mahphalke 8:70fc373a5f46 146 /**
mahphalke 8:70fc373a5f46 147 * @brief Enable the output direction of the specified GPIO.
mahphalke 8:70fc373a5f46 148 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 149 * @param value - The value.
mahphalke 8:70fc373a5f46 150 * Example: GPIO_HIGH
mahphalke 8:70fc373a5f46 151 * GPIO_LOW
mahphalke 8:70fc373a5f46 152 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 153 * @note does not support reconfiguration of already set pin direction
mahphalke 8:70fc373a5f46 154 */
mahphalke 8:70fc373a5f46 155 int32_t gpio_direction_output(struct gpio_desc *desc, uint8_t value)
mahphalke 8:70fc373a5f46 156 {
EndaKilgarriff 9:9e247b9c9abf 157 DigitalOut *gpio_output; // pointer to gpio output object
EndaKilgarriff 9:9e247b9c9abf 158 mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters
mahphalke 8:70fc373a5f46 159
EndaKilgarriff 9:9e247b9c9abf 160 if(desc && desc->extra) {
EndaKilgarriff 9:9e247b9c9abf 161 gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
EndaKilgarriff 9:9e247b9c9abf 162 if (gpio_desc_extra->gpio_pin == NULL) {
EndaKilgarriff 9:9e247b9c9abf 163
EndaKilgarriff 9:9e247b9c9abf 164 // Configure and instantiate GPIO pin as output
EndaKilgarriff 9:9e247b9c9abf 165 gpio_output = new DigitalOut((PinName)desc->number);
mahphalke 8:70fc373a5f46 166
EndaKilgarriff 9:9e247b9c9abf 167 if (gpio_output == NULL) {
EndaKilgarriff 9:9e247b9c9abf 168 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 169 }
mahphalke 8:70fc373a5f46 170
EndaKilgarriff 9:9e247b9c9abf 171 gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output;
EndaKilgarriff 9:9e247b9c9abf 172 gpio_desc_extra->direction = GPIO_OUT;
mahphalke 8:70fc373a5f46 173
EndaKilgarriff 9:9e247b9c9abf 174 // Set the GPIO value
EndaKilgarriff 9:9e247b9c9abf 175 if(gpio_set_value(desc, value) == SUCCESS) {
EndaKilgarriff 9:9e247b9c9abf 176 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 177 }
EndaKilgarriff 9:9e247b9c9abf 178 }
mahphalke 8:70fc373a5f46 179 }
mahphalke 8:70fc373a5f46 180
mahphalke 8:70fc373a5f46 181 return FAILURE;
mahphalke 8:70fc373a5f46 182 }
mahphalke 8:70fc373a5f46 183
mahphalke 8:70fc373a5f46 184
mahphalke 8:70fc373a5f46 185 /**
mahphalke 8:70fc373a5f46 186 * @brief Get the direction of the specified GPIO.
mahphalke 8:70fc373a5f46 187 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 188 * @param direction - The direction.
mahphalke 8:70fc373a5f46 189 * Example: GPIO_OUT
mahphalke 8:70fc373a5f46 190 * GPIO_IN
mahphalke 8:70fc373a5f46 191 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 192 */
mahphalke 8:70fc373a5f46 193 int32_t gpio_get_direction(struct gpio_desc *desc, uint8_t *direction)
mahphalke 8:70fc373a5f46 194 {
EndaKilgarriff 9:9e247b9c9abf 195 mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters
EndaKilgarriff 9:9e247b9c9abf 196
EndaKilgarriff 9:9e247b9c9abf 197 if(desc && desc->extra) {
EndaKilgarriff 9:9e247b9c9abf 198 gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
EndaKilgarriff 9:9e247b9c9abf 199
EndaKilgarriff 9:9e247b9c9abf 200 if (gpio_desc_extra->gpio_pin) {
EndaKilgarriff 9:9e247b9c9abf 201 *direction = gpio_desc_extra->direction;
EndaKilgarriff 9:9e247b9c9abf 202 }
EndaKilgarriff 9:9e247b9c9abf 203
EndaKilgarriff 9:9e247b9c9abf 204 return SUCCESS;
mahphalke 8:70fc373a5f46 205 }
mahphalke 8:70fc373a5f46 206
EndaKilgarriff 9:9e247b9c9abf 207 return FAILURE;
mahphalke 8:70fc373a5f46 208 }
mahphalke 8:70fc373a5f46 209
mahphalke 8:70fc373a5f46 210
mahphalke 8:70fc373a5f46 211 /**
mahphalke 8:70fc373a5f46 212 * @brief Set the value of the specified GPIO.
mahphalke 8:70fc373a5f46 213 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 214 * @param value - The value.
mahphalke 8:70fc373a5f46 215 * Example: GPIO_HIGH
mahphalke 8:70fc373a5f46 216 * GPIO_LOW
mahphalke 8:70fc373a5f46 217 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 218 */
mahphalke 8:70fc373a5f46 219 int32_t gpio_set_value(struct gpio_desc *desc, uint8_t value)
mahphalke 8:70fc373a5f46 220 {
EndaKilgarriff 9:9e247b9c9abf 221 DigitalOut *gpio_output; // pointer to gpio output object
EndaKilgarriff 9:9e247b9c9abf 222 mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters
mahphalke 8:70fc373a5f46 223
EndaKilgarriff 9:9e247b9c9abf 224 if(desc && desc->extra) {
EndaKilgarriff 9:9e247b9c9abf 225 gpio_desc_extra = (mbed_gpio_desc *)(desc->extra);
EndaKilgarriff 9:9e247b9c9abf 226
EndaKilgarriff 9:9e247b9c9abf 227 if (gpio_desc_extra->gpio_pin) {
EndaKilgarriff 9:9e247b9c9abf 228 gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
EndaKilgarriff 9:9e247b9c9abf 229 gpio_output->write(value);
EndaKilgarriff 9:9e247b9c9abf 230 }
mahphalke 8:70fc373a5f46 231
mahphalke 8:70fc373a5f46 232 return SUCCESS;
mahphalke 8:70fc373a5f46 233 }
mahphalke 8:70fc373a5f46 234
mahphalke 8:70fc373a5f46 235 return FAILURE;
mahphalke 8:70fc373a5f46 236 }
mahphalke 8:70fc373a5f46 237
mahphalke 8:70fc373a5f46 238
mahphalke 8:70fc373a5f46 239 /**
mahphalke 8:70fc373a5f46 240 * @brief Get the value of the specified GPIO.
mahphalke 8:70fc373a5f46 241 * @param desc - The GPIO descriptor.
mahphalke 8:70fc373a5f46 242 * @param value - The value.
mahphalke 8:70fc373a5f46 243 * Example: GPIO_HIGH
mahphalke 8:70fc373a5f46 244 * GPIO_LOW
mahphalke 8:70fc373a5f46 245 * @return SUCCESS in case of success, FAILURE otherwise.
mahphalke 8:70fc373a5f46 246 */
mahphalke 8:70fc373a5f46 247 int32_t gpio_get_value(struct gpio_desc *desc, uint8_t *value)
mahphalke 8:70fc373a5f46 248 {
EndaKilgarriff 9:9e247b9c9abf 249 DigitalIn *gpio_input; // pointer to gpio input object
mahphalke 8:70fc373a5f46 250 uint8_t returnVal = FAILURE;
mahphalke 8:70fc373a5f46 251
EndaKilgarriff 9:9e247b9c9abf 252 if (desc && desc->extra) {
mahphalke 8:70fc373a5f46 253 gpio_input = (DigitalIn *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
mahphalke 8:70fc373a5f46 254 *value = (uint8_t)gpio_input->read();
mahphalke 8:70fc373a5f46 255 returnVal = gpio_input->is_connected() ? SUCCESS : FAILURE;
mahphalke 8:70fc373a5f46 256
mahphalke 8:70fc373a5f46 257 return returnVal;
mahphalke 8:70fc373a5f46 258 }
mahphalke 8:70fc373a5f46 259
mahphalke 8:70fc373a5f46 260 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 261 }