Platform drivers for Mbed.

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

Committer:
mahphalke
Date:
Tue Jul 13 13:58:07 2021 +0530
Revision:
17:af1f2416dd26
Restructured the directory- Removed inc/ and src/ folders and moved all source/header files at root

Who changed what in which revision?

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