Platform drivers for Mbed.

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

src/gpio.cpp

Committer:
mahphalke
Date:
2020-02-26
Revision:
8:70fc373a5f46
Child:
9:9e247b9c9abf

File content as of revision 8:70fc373a5f46:

/***************************************************************************//**
 *   @file   gpio.cpp
 *   @brief  Implementation of GPIO No-OS platform driver interfaces
********************************************************************************
 * Copyright (c) 2019, 2020 Analog Devices, Inc.
 *
 * All rights reserved.
 *
 * This software is proprietary to Analog Devices, Inc. and its licensors.
 * By using this software you agree to the terms of the associated
 * Analog Devices Software License Agreement.
*******************************************************************************/

/******************************************************************************/
/************************ Includes Files *******************************/
/******************************************************************************/
#include <stdio.h>
#include <mbed.h>

#include "platform_drivers.h"
#include "gpio_extra.h"

/******************************************************************************/
/************************ Functions Definitions *******************************/
/******************************************************************************/

/**
 * @brief Obtain the GPIO decriptor.
 * @param desc - The GPIO descriptor.
 * @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)
{
	if (desc) {
		// Create the gpio description object for the device
		gpio_desc *new_gpio = (gpio_desc *)malloc(sizeof(gpio_desc));
		if (new_gpio == NULL) {
			return FAILURE;
		}

		new_gpio->number = param->number;
		*desc = new_gpio;

		return SUCCESS;
	}

	return FAILURE;
}


/**
 * @brief Free the resources allocated by gpio_get().
 * @param desc - The GPIO descriptor.
 * @return SUCCESS in case of success, FAILURE otherwise.
 */
int32_t gpio_remove(struct gpio_desc *desc)
{
	if (desc) {
		// Free the gpio object
		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)) {
			free((mbed_gpio_desc *)(desc->extra));
		}

		// Free the gpio descriptor object
		free(desc);

		return SUCCESS;
	}

	return FAILURE;
}


/**
 * @brief Enable the input direction of the specified GPIO.
 * @param desc - The GPIO descriptor.
 * @return SUCCESS in case of success, FAILURE otherwise.
 */
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;
		}

		// 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;
		}

		gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input;
		desc->extra = (mbed_gpio_desc *)gpio_desc_extra;

		// Set the gpio pin mode
		gpio_input->mode((PinMode)((mbed_gpio_init_param *)desc->extra)->pin_mode);

		return SUCCESS;
	}

	return FAILURE;
}


/**
 * @brief Enable the output direction of the specified GPIO.
 * @param desc - The GPIO descriptor.
 * @param value - The value.
 *                Example: GPIO_HIGH
 *                         GPIO_LOW
 * @return SUCCESS in case of success, FAILURE otherwise.
 */
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

	if(desc) {
		// Configure and instantiate GPIO pin as output
		gpio_output = new DigitalOut((PinName)desc->number);
		if (gpio_output == NULL) {
			return FAILURE;
		}

		// 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;
		}

		gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output;
		desc->extra = (mbed_gpio_desc *)gpio_desc_extra;

		return SUCCESS;
	}

	if (value) {
		// Unused variable - fix compiler warning
	}

	return FAILURE;
}


/**
 * @brief Get the direction of the specified GPIO.
 * @param desc - The GPIO descriptor.
 * @param direction - The direction.
 *                    Example: GPIO_OUT
 *                             GPIO_IN
 * @return SUCCESS in case of success, FAILURE otherwise.
 */
int32_t gpio_get_direction(struct gpio_desc *desc, uint8_t *direction)
{
	if (desc) {
		// Unused variable - fix compiler warning
	}

	if (direction) {
		// Unused variable - fix compiler warning
	}

	return SUCCESS;
}


/**
 * @brief Set the value of the specified GPIO.
 * @param desc - The GPIO descriptor.
 * @param value - The value.
 *                Example: GPIO_HIGH
 *                         GPIO_LOW
 * @return SUCCESS in case of success, FAILURE otherwise.
 */
int32_t gpio_set_value(struct gpio_desc *desc, uint8_t value)
{
	DigitalOut *gpio_output;		// pointer to gpio output object

	if (desc) {
		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;
}


/**
 * @brief Get the value of the specified GPIO.
 * @param desc - The GPIO descriptor.
 * @param value - The value.
 *                Example: GPIO_HIGH
 *                         GPIO_LOW
 * @return SUCCESS in case of success, FAILURE otherwise.
 */
int32_t gpio_get_value(struct gpio_desc *desc, uint8_t *value)
{
	DigitalIn *gpio_input;   	// pointer to gpio input object
	uint8_t returnVal = FAILURE;

	if (desc) {
		gpio_input = (DigitalIn *)((mbed_gpio_desc *)desc->extra)->gpio_pin;
		*value = (uint8_t)gpio_input->read();
		returnVal = gpio_input->is_connected() ? SUCCESS : FAILURE;

		return returnVal;
	}

	return FAILURE;
}