Example program for EVAL-ADMX2001

Dependencies:   ADMX2001

Committer:
nsheth
Date:
Wed Nov 17 18:15:37 2021 +0000
Revision:
15:fca7551aaf0a
Parent:
9:29db35656fcb
Updating default .lib file for eval platform

Who changed what in which revision?

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