Update platform drivers
src/gpio.cpp@8:70fc373a5f46, 2020-02-26 (annotated)
- Committer:
- mahphalke
- Date:
- Wed Feb 26 06:09:13 2020 +0000
- Revision:
- 8:70fc373a5f46
- Child:
- 9:9e247b9c9abf
Structured platform drivers similar to github version by splitting functionality from single file into multiple modules.
Who changed what in which revision?
User | Revision | Line number | New 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> |
mahphalke | 8:70fc373a5f46 | 18 | #include <mbed.h> |
mahphalke | 8:70fc373a5f46 | 19 | |
mahphalke | 8:70fc373a5f46 | 20 | #include "platform_drivers.h" |
mahphalke | 8:70fc373a5f46 | 21 | #include "gpio_extra.h" |
mahphalke | 8:70fc373a5f46 | 22 | |
mahphalke | 8:70fc373a5f46 | 23 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 24 | /************************ Functions Definitions *******************************/ |
mahphalke | 8:70fc373a5f46 | 25 | /******************************************************************************/ |
mahphalke | 8:70fc373a5f46 | 26 | |
mahphalke | 8:70fc373a5f46 | 27 | /** |
mahphalke | 8:70fc373a5f46 | 28 | * @brief Obtain the GPIO decriptor. |
mahphalke | 8:70fc373a5f46 | 29 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 30 | * @param gpio_number - The number of the GPIO. |
mahphalke | 8:70fc373a5f46 | 31 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 32 | */ |
mahphalke | 8:70fc373a5f46 | 33 | int32_t gpio_get(struct gpio_desc **desc, const gpio_init_param *param) |
mahphalke | 8:70fc373a5f46 | 34 | { |
mahphalke | 8:70fc373a5f46 | 35 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 36 | // Create the gpio description object for the device |
mahphalke | 8:70fc373a5f46 | 37 | gpio_desc *new_gpio = (gpio_desc *)malloc(sizeof(gpio_desc)); |
mahphalke | 8:70fc373a5f46 | 38 | if (new_gpio == NULL) { |
mahphalke | 8:70fc373a5f46 | 39 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 40 | } |
mahphalke | 8:70fc373a5f46 | 41 | |
mahphalke | 8:70fc373a5f46 | 42 | new_gpio->number = param->number; |
mahphalke | 8:70fc373a5f46 | 43 | *desc = new_gpio; |
mahphalke | 8:70fc373a5f46 | 44 | |
mahphalke | 8:70fc373a5f46 | 45 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 46 | } |
mahphalke | 8:70fc373a5f46 | 47 | |
mahphalke | 8:70fc373a5f46 | 48 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 49 | } |
mahphalke | 8:70fc373a5f46 | 50 | |
mahphalke | 8:70fc373a5f46 | 51 | |
mahphalke | 8:70fc373a5f46 | 52 | /** |
mahphalke | 8:70fc373a5f46 | 53 | * @brief Free the resources allocated by gpio_get(). |
mahphalke | 8:70fc373a5f46 | 54 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 55 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 56 | */ |
mahphalke | 8:70fc373a5f46 | 57 | int32_t gpio_remove(struct gpio_desc *desc) |
mahphalke | 8:70fc373a5f46 | 58 | { |
mahphalke | 8:70fc373a5f46 | 59 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 60 | // Free the gpio object |
mahphalke | 8:70fc373a5f46 | 61 | if (((mbed_gpio_desc *)(desc->extra))->gpio_pin) { |
mahphalke | 8:70fc373a5f46 | 62 | free(((mbed_gpio_desc *)(desc->extra))->gpio_pin); |
mahphalke | 8:70fc373a5f46 | 63 | } |
mahphalke | 8:70fc373a5f46 | 64 | |
mahphalke | 8:70fc373a5f46 | 65 | // Free the gpio extra descriptor object |
mahphalke | 8:70fc373a5f46 | 66 | if ((mbed_gpio_desc *)(desc->extra)) { |
mahphalke | 8:70fc373a5f46 | 67 | free((mbed_gpio_desc *)(desc->extra)); |
mahphalke | 8:70fc373a5f46 | 68 | } |
mahphalke | 8:70fc373a5f46 | 69 | |
mahphalke | 8:70fc373a5f46 | 70 | // Free the gpio descriptor object |
mahphalke | 8:70fc373a5f46 | 71 | free(desc); |
mahphalke | 8:70fc373a5f46 | 72 | |
mahphalke | 8:70fc373a5f46 | 73 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 74 | } |
mahphalke | 8:70fc373a5f46 | 75 | |
mahphalke | 8:70fc373a5f46 | 76 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 77 | } |
mahphalke | 8:70fc373a5f46 | 78 | |
mahphalke | 8:70fc373a5f46 | 79 | |
mahphalke | 8:70fc373a5f46 | 80 | /** |
mahphalke | 8:70fc373a5f46 | 81 | * @brief Enable the input direction of the specified GPIO. |
mahphalke | 8:70fc373a5f46 | 82 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 83 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 84 | */ |
mahphalke | 8:70fc373a5f46 | 85 | int32_t gpio_direction_input(struct gpio_desc *desc) |
mahphalke | 8:70fc373a5f46 | 86 | { |
mahphalke | 8:70fc373a5f46 | 87 | DigitalIn *gpio_input; // pointer to gpio input object |
mahphalke | 8:70fc373a5f46 | 88 | mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters |
mahphalke | 8:70fc373a5f46 | 89 | |
mahphalke | 8:70fc373a5f46 | 90 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 91 | // Configure and instantiate GPIO pin as input |
mahphalke | 8:70fc373a5f46 | 92 | gpio_input = new DigitalIn((PinName)desc->number); |
mahphalke | 8:70fc373a5f46 | 93 | if (gpio_input == NULL) { |
mahphalke | 8:70fc373a5f46 | 94 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 95 | } |
mahphalke | 8:70fc373a5f46 | 96 | |
mahphalke | 8:70fc373a5f46 | 97 | // Create the gpio extra descriptor object to store new gpio instance |
mahphalke | 8:70fc373a5f46 | 98 | gpio_desc_extra = (mbed_gpio_desc *)malloc(sizeof(mbed_gpio_desc)); |
mahphalke | 8:70fc373a5f46 | 99 | if (gpio_desc_extra == NULL) { |
mahphalke | 8:70fc373a5f46 | 100 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 101 | } |
mahphalke | 8:70fc373a5f46 | 102 | |
mahphalke | 8:70fc373a5f46 | 103 | gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input; |
mahphalke | 8:70fc373a5f46 | 104 | desc->extra = (mbed_gpio_desc *)gpio_desc_extra; |
mahphalke | 8:70fc373a5f46 | 105 | |
mahphalke | 8:70fc373a5f46 | 106 | // Set the gpio pin mode |
mahphalke | 8:70fc373a5f46 | 107 | gpio_input->mode((PinMode)((mbed_gpio_init_param *)desc->extra)->pin_mode); |
mahphalke | 8:70fc373a5f46 | 108 | |
mahphalke | 8:70fc373a5f46 | 109 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 110 | } |
mahphalke | 8:70fc373a5f46 | 111 | |
mahphalke | 8:70fc373a5f46 | 112 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 113 | } |
mahphalke | 8:70fc373a5f46 | 114 | |
mahphalke | 8:70fc373a5f46 | 115 | |
mahphalke | 8:70fc373a5f46 | 116 | /** |
mahphalke | 8:70fc373a5f46 | 117 | * @brief Enable the output direction of the specified GPIO. |
mahphalke | 8:70fc373a5f46 | 118 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 119 | * @param value - The value. |
mahphalke | 8:70fc373a5f46 | 120 | * Example: GPIO_HIGH |
mahphalke | 8:70fc373a5f46 | 121 | * GPIO_LOW |
mahphalke | 8:70fc373a5f46 | 122 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 123 | */ |
mahphalke | 8:70fc373a5f46 | 124 | int32_t gpio_direction_output(struct gpio_desc *desc, uint8_t value) |
mahphalke | 8:70fc373a5f46 | 125 | { |
mahphalke | 8:70fc373a5f46 | 126 | DigitalOut *gpio_output; // pointer to gpio output object |
mahphalke | 8:70fc373a5f46 | 127 | mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters |
mahphalke | 8:70fc373a5f46 | 128 | |
mahphalke | 8:70fc373a5f46 | 129 | if(desc) { |
mahphalke | 8:70fc373a5f46 | 130 | // Configure and instantiate GPIO pin as output |
mahphalke | 8:70fc373a5f46 | 131 | gpio_output = new DigitalOut((PinName)desc->number); |
mahphalke | 8:70fc373a5f46 | 132 | if (gpio_output == NULL) { |
mahphalke | 8:70fc373a5f46 | 133 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 134 | } |
mahphalke | 8:70fc373a5f46 | 135 | |
mahphalke | 8:70fc373a5f46 | 136 | // Create the gpio extra descriptor object to store new gpio instance |
mahphalke | 8:70fc373a5f46 | 137 | gpio_desc_extra = (mbed_gpio_desc *)malloc(sizeof(mbed_gpio_desc)); |
mahphalke | 8:70fc373a5f46 | 138 | if (gpio_desc_extra == NULL) { |
mahphalke | 8:70fc373a5f46 | 139 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 140 | } |
mahphalke | 8:70fc373a5f46 | 141 | |
mahphalke | 8:70fc373a5f46 | 142 | gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output; |
mahphalke | 8:70fc373a5f46 | 143 | desc->extra = (mbed_gpio_desc *)gpio_desc_extra; |
mahphalke | 8:70fc373a5f46 | 144 | |
mahphalke | 8:70fc373a5f46 | 145 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 146 | } |
mahphalke | 8:70fc373a5f46 | 147 | |
mahphalke | 8:70fc373a5f46 | 148 | if (value) { |
mahphalke | 8:70fc373a5f46 | 149 | // Unused variable - fix compiler warning |
mahphalke | 8:70fc373a5f46 | 150 | } |
mahphalke | 8:70fc373a5f46 | 151 | |
mahphalke | 8:70fc373a5f46 | 152 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 153 | } |
mahphalke | 8:70fc373a5f46 | 154 | |
mahphalke | 8:70fc373a5f46 | 155 | |
mahphalke | 8:70fc373a5f46 | 156 | /** |
mahphalke | 8:70fc373a5f46 | 157 | * @brief Get the direction of the specified GPIO. |
mahphalke | 8:70fc373a5f46 | 158 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 159 | * @param direction - The direction. |
mahphalke | 8:70fc373a5f46 | 160 | * Example: GPIO_OUT |
mahphalke | 8:70fc373a5f46 | 161 | * GPIO_IN |
mahphalke | 8:70fc373a5f46 | 162 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 163 | */ |
mahphalke | 8:70fc373a5f46 | 164 | int32_t gpio_get_direction(struct gpio_desc *desc, uint8_t *direction) |
mahphalke | 8:70fc373a5f46 | 165 | { |
mahphalke | 8:70fc373a5f46 | 166 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 167 | // Unused variable - fix compiler warning |
mahphalke | 8:70fc373a5f46 | 168 | } |
mahphalke | 8:70fc373a5f46 | 169 | |
mahphalke | 8:70fc373a5f46 | 170 | if (direction) { |
mahphalke | 8:70fc373a5f46 | 171 | // Unused variable - fix compiler warning |
mahphalke | 8:70fc373a5f46 | 172 | } |
mahphalke | 8:70fc373a5f46 | 173 | |
mahphalke | 8:70fc373a5f46 | 174 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 175 | } |
mahphalke | 8:70fc373a5f46 | 176 | |
mahphalke | 8:70fc373a5f46 | 177 | |
mahphalke | 8:70fc373a5f46 | 178 | /** |
mahphalke | 8:70fc373a5f46 | 179 | * @brief Set the value of the specified GPIO. |
mahphalke | 8:70fc373a5f46 | 180 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 181 | * @param value - The value. |
mahphalke | 8:70fc373a5f46 | 182 | * Example: GPIO_HIGH |
mahphalke | 8:70fc373a5f46 | 183 | * GPIO_LOW |
mahphalke | 8:70fc373a5f46 | 184 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 185 | */ |
mahphalke | 8:70fc373a5f46 | 186 | int32_t gpio_set_value(struct gpio_desc *desc, uint8_t value) |
mahphalke | 8:70fc373a5f46 | 187 | { |
mahphalke | 8:70fc373a5f46 | 188 | DigitalOut *gpio_output; // pointer to gpio output object |
mahphalke | 8:70fc373a5f46 | 189 | |
mahphalke | 8:70fc373a5f46 | 190 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 191 | gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin; |
mahphalke | 8:70fc373a5f46 | 192 | gpio_output->write(value); |
mahphalke | 8:70fc373a5f46 | 193 | |
mahphalke | 8:70fc373a5f46 | 194 | return SUCCESS; |
mahphalke | 8:70fc373a5f46 | 195 | } |
mahphalke | 8:70fc373a5f46 | 196 | |
mahphalke | 8:70fc373a5f46 | 197 | if (value) { |
mahphalke | 8:70fc373a5f46 | 198 | // Unused variable - fix compiler warning |
mahphalke | 8:70fc373a5f46 | 199 | } |
mahphalke | 8:70fc373a5f46 | 200 | |
mahphalke | 8:70fc373a5f46 | 201 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 202 | } |
mahphalke | 8:70fc373a5f46 | 203 | |
mahphalke | 8:70fc373a5f46 | 204 | |
mahphalke | 8:70fc373a5f46 | 205 | /** |
mahphalke | 8:70fc373a5f46 | 206 | * @brief Get the value of the specified GPIO. |
mahphalke | 8:70fc373a5f46 | 207 | * @param desc - The GPIO descriptor. |
mahphalke | 8:70fc373a5f46 | 208 | * @param value - The value. |
mahphalke | 8:70fc373a5f46 | 209 | * Example: GPIO_HIGH |
mahphalke | 8:70fc373a5f46 | 210 | * GPIO_LOW |
mahphalke | 8:70fc373a5f46 | 211 | * @return SUCCESS in case of success, FAILURE otherwise. |
mahphalke | 8:70fc373a5f46 | 212 | */ |
mahphalke | 8:70fc373a5f46 | 213 | int32_t gpio_get_value(struct gpio_desc *desc, uint8_t *value) |
mahphalke | 8:70fc373a5f46 | 214 | { |
mahphalke | 8:70fc373a5f46 | 215 | DigitalIn *gpio_input; // pointer to gpio input object |
mahphalke | 8:70fc373a5f46 | 216 | uint8_t returnVal = FAILURE; |
mahphalke | 8:70fc373a5f46 | 217 | |
mahphalke | 8:70fc373a5f46 | 218 | if (desc) { |
mahphalke | 8:70fc373a5f46 | 219 | gpio_input = (DigitalIn *)((mbed_gpio_desc *)desc->extra)->gpio_pin; |
mahphalke | 8:70fc373a5f46 | 220 | *value = (uint8_t)gpio_input->read(); |
mahphalke | 8:70fc373a5f46 | 221 | returnVal = gpio_input->is_connected() ? SUCCESS : FAILURE; |
mahphalke | 8:70fc373a5f46 | 222 | |
mahphalke | 8:70fc373a5f46 | 223 | return returnVal; |
mahphalke | 8:70fc373a5f46 | 224 | } |
mahphalke | 8:70fc373a5f46 | 225 | |
mahphalke | 8:70fc373a5f46 | 226 | return FAILURE; |
mahphalke | 8:70fc373a5f46 | 227 | } |