Forked repository for pushing changes

Files at this revision

API Documentation at this revision

Comitter:
pmallick
Date:
Mon Sep 27 04:22:15 2021 +0000
Parent:
18:5ae03a197e59
Commit message:
Added support for analog read and write functionality

Changed in this revision

ain.cpp Show annotated file Show diff for this revision Revisions of this file
ain.h Show annotated file Show diff for this revision Revisions of this file
aout.cpp Show annotated file Show diff for this revision Revisions of this file
aout.h Show annotated file Show diff for this revision Revisions of this file
mbed_ain_aout_extra.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ain.cpp	Mon Sep 27 04:22:15 2021 +0000
@@ -0,0 +1,167 @@
+/***************************************************************************//**
+ *   @file   ain.cpp
+ *   @brief  Implementation of mbed specific analog input functionality
+********************************************************************************
+ * Copyright (c) 2021 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.
+*******************************************************************************/
+
+/******************************************************************************/
+/***************************** Include Files **********************************/
+/******************************************************************************/
+#include <mbed.h>
+
+// Platform drivers needs to be C-compatible to work with other drivers
+#ifdef __cplusplus
+extern "C"
+{
+#endif //  _cplusplus
+#include "ain.h"
+#include "mbed_ain_aout_extra.h"
+#include "error.h"
+
+/******************************************************************************/
+/********************** Variables and User defined data types *****************/
+/******************************************************************************/
+
+/******************************************************************************/
+/************************ Functions Declarations ******************************/
+/******************************************************************************/
+
+/******************************************************************************/
+/************************ Functions Definitions *******************************/
+/******************************************************************************/
+/**
+ * @brief Read voltage from analog input pin
+ * @param desc[in] - Analog input descriptor
+ * @param value[out] - Buffer to pass analog reading.
+ * @return SUCCESS in case of success, FAILURE otherwise
+ */
+int32_t ain_get_voltage(struct ain_desc *desc, float *value)
+{
+	/* Pointer to analog input object */
+	mbed::AnalogIn *analog_input;
+
+	if (desc && desc->extra) {
+		analog_input = (AnalogIn *)((mbed_analog_in_desc *)
+					    desc->extra)->analog_in_obj;
+		*value = analog_input->read_voltage();
+
+		return SUCCESS;
+	}
+	return FAILURE;
+}
+
+/**
+ * @brief Initialize the Analog Input Pin
+ * @param desc[in, out] - Analog input descriptor structure
+ * @param param[in] - Structure that contains analog input
+ *                    initialization parameters
+ * @return SUCCESS in case of success, FAILURE otherwise
+ */
+int32_t ain_init(struct ain_desc **desc,
+		 const struct ain_init_param *param)
+{
+	mbed::AnalogIn *analog_input;
+	struct ain_desc *new_analog_in_desc;
+	struct mbed_analog_in_desc *new_mbed_analog_in_desc;
+
+	if (!desc || !param ) {
+		return FAILURE;
+	}
+
+	/* Allocate memory for general analog input descriptor */
+	new_analog_in_desc = (ain_desc *)malloc(sizeof(ain_desc));
+	if (!new_analog_in_desc) {
+		goto err_new_in_desc;
+	}
+
+	/* Configure pin number and reference voltage*/
+	new_analog_in_desc->number = param->number;
+	new_analog_in_desc->vref = param->vref;
+
+	/* Allocate memory for mbed specific analog input descriptor
+	 * for future use */
+	new_mbed_analog_in_desc = (mbed_analog_in_desc *)malloc(sizeof(
+					  mbed_analog_in_desc));
+	if (!new_mbed_analog_in_desc) {
+		goto err_mbed_analog_in_desc;
+	}
+
+	/* Create and initialize mbed analog input object */
+	analog_input = new AnalogIn((PinName)param->number, param->vref);
+	if (analog_input) {
+		new_mbed_analog_in_desc->analog_in_obj = analog_input;
+	}
+
+	else {
+		goto err_analog_in_instance;
+	}
+
+	new_analog_in_desc->extra = (mbed_analog_in_desc *)new_mbed_analog_in_desc;
+
+	*desc = new_analog_in_desc;
+	return SUCCESS;
+
+err_analog_in_instance:
+	free(new_mbed_analog_in_desc);
+err_mbed_analog_in_desc:
+	free(new_analog_in_desc);
+err_new_in_desc:
+	// Nothing to free
+
+	return FAILURE;
+}
+
+/**
+ * @brief Deallocate resources allocated by ain_init()
+ * @param desc[in, out] - Analog input descriptor
+ * @return SUCCESS in case of success, FAILURE otherwise.
+ */
+int32_t ain_remove(struct ain_desc *desc)
+{
+	if (desc) {
+
+		if (((mbed_analog_in_desc *)desc->extra)->analog_in_obj) {
+			delete((mbed::AnalogIn *)((mbed_analog_in_desc *)desc->
+						  extra)->analog_in_obj);
+		}
+
+		if ((mbed_analog_in_desc *)desc->extra) {
+			free((mbed_analog_in_desc *)desc->extra);
+		}
+
+		if (((mbed_analog_in_desc *)desc->extra)->analog_in_obj) {
+			delete((mbed::AnalogOut *)((mbed_analog_in_desc *)
+						   desc->extra)->analog_in_obj);
+		}
+
+		if ((mbed_analog_in_desc *)desc->extra) {
+			free((mbed_analog_in_desc *)desc->extra);
+		}
+
+		free(desc);
+
+		return SUCCESS;
+	}
+
+	return FAILURE;
+}
+
+/**
+ * @brief Mbed specific analog input platform ops structure
+ */
+const struct ain_platform_ops mbed_ain_ops = {
+	.init = &ain_init,
+	.read = &ain_get_voltage,
+	.remove = &ain_remove
+};
+
+// Platform drivers needs to be C-compatible to work with other drivers
+#ifdef __cplusplus
+}
+#endif //  _cplusplus
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ain.h	Mon Sep 27 04:22:15 2021 +0000
@@ -0,0 +1,84 @@
+/***************************************************************************//**
+ *   @file   ain.h
+ *   @author PMallick (Pratyush.Mallick@analog.com)
+********************************************************************************
+ * Copyright (c) 2021 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.
+*******************************************************************************/
+
+#ifndef AIN_H
+#define AIN_H
+
+/******************************************************************************/
+/***************************** Include Files **********************************/
+/******************************************************************************/
+
+#include <stdint.h>
+
+/******************************************************************************/
+/********************** Macros and Constants Definitions **********************/
+/******************************************************************************/
+
+/******************************************************************************/
+/*************************** Types Declarations *******************************/
+/******************************************************************************/
+/**
+ * @struct ain_init_param
+ * @brief Structure holding parameters for analog input initialization
+ */
+struct ain_init_param {
+	/* Analog input pin number */
+	int32_t	number;
+	/* Analog input reference voltage */
+	float vref;
+	/* Analog input platform specific functions */
+	const struct ain_platform_ops *platform_ops;
+};
+
+/**
+ * @struct ain_desc
+ * @brief Structure holding analog input descriptor
+ */
+struct ain_desc {
+	/* Analog input pin number */
+	int32_t	number;
+	/* Analog input reference voltage */
+	float vref;
+	/* Analog input platform specific functions */
+	const struct ain_platform_ops *platform_ops;
+	/* Analog extra parameters (device specific) */
+	void *extra;
+};
+
+/**
+ * @struct ain_platform_ops
+ * @brief Structure holding analog input function pointers that 
+ *        point to the platform specific function
+ */
+struct ain_platform_ops {
+	/** Analog input initialization function pointer */
+	int32_t(*init)(struct ain_desc **, const struct ain_init_param *);
+	/** Analog input read function pointer */
+	int32_t(*read)(struct ain_desc *, float *);
+	/** Analog input remove function pointer */
+	int32_t(*remove)(struct ain_desc *);
+};
+
+/******************************************************************************/
+/************************ Functions Declarations ******************************/
+/******************************************************************************/
+/* Read analog input voltage */
+int32_t ain_get_voltage(struct ain_desc *desc, float *value);
+
+/* Initialize the analog input pin */
+int32_t ain_init(struct ain_desc **desc,
+		 const struct ain_init_param *param);
+
+/* Free the resources allocated by ain_init() */
+int32_t ain_remove(struct ain_desc *desc);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aout.cpp	Mon Sep 27 04:22:15 2021 +0000
@@ -0,0 +1,180 @@
+/***************************************************************************//**
+ *   @file   aout.cpp
+ *   @brief  Implementation of mbed specific analog output functionality
+********************************************************************************
+ * Copyright (c) 2021 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.
+*******************************************************************************/
+
+/******************************************************************************/
+/***************************** Include Files **********************************/
+/******************************************************************************/
+#include <mbed.h>
+
+// Platform drivers needs to be C-compatible to work with other drivers
+#ifdef __cplusplus
+extern "C"
+{
+#endif //  _cplusplus
+#include "aout.h"
+#include "mbed_ain_aout_extra.h"
+#include "error.h"
+
+/******************************************************************************/
+/********************** Variables and User defined data types *****************/
+/******************************************************************************/
+
+/******************************************************************************/
+/************************ Functions Declarations ******************************/
+/******************************************************************************/
+
+/******************************************************************************/
+/************************ Functions Definitions *******************************/
+/******************************************************************************/
+/**
+ * @brief Write voltage to analog output pin
+ * @param desc[in] - Analog output descriptor
+ * @param value[in] - Analog output value in volts
+ * @return SUCCESS in case of success, FAILURE otherwise.
+ */
+int32_t aout_set_voltage(struct aout_desc *desc, float value)
+{
+	/* Pointer to analog output object */
+	mbed::AnalogOut *analog_output;
+	float volt_mapped;
+
+	/* Mbed AnalogOut APIs accept voltage in range from 0.0f
+	 * (representing DAC min output range / 0%) to 1.0f (representing
+	 * DAC max output range / 100%). Hence we need to map the voltage levels
+	 * before passing it to mbed APIs */
+	if (value >= desc->aout_min_v && value <= desc->aout_max_v) {
+		volt_mapped = ((value - desc->aout_min_v) /
+			       (desc->aout_max_v - desc->aout_min_v));
+	} else {
+		return -EINVAL;
+	}
+
+	if (desc && desc->extra) {
+		analog_output = (AnalogOut *)((mbed_analog_out_desc *)
+					      desc->extra)->analog_out_obj ;
+		analog_output->write(volt_mapped);
+
+		return SUCCESS;
+	}
+	return FAILURE;
+}
+
+/**
+ * @brief Initialize the analog output pin
+ * @param desc[in, out] - Analog output descriptor structure
+ * @param param[in] - Structure that contains analog output
+ *                    initialization parameters
+ * @return SUCCESS in case of success, FAILURE otherwise
+ */
+int32_t aout_init(struct aout_desc **desc,
+		  const struct aout_init_param *param)
+{
+	mbed::AnalogOut *analog_output;
+	struct aout_desc *new_analog_out_desc;
+	struct mbed_analog_out_desc *new_mbed_analog_out_desc;
+
+	if (!desc || !param ) {
+		return FAILURE;
+	}
+
+	/* Allocate memory for general analog output descriptor */
+	new_analog_out_desc = (aout_desc *)malloc(sizeof(aout_desc));
+	if (!new_analog_out_desc) {
+		goto err_new_out_desc;
+	}
+
+	/* Configure pin number,DAC max and min range */
+	new_analog_out_desc->number = param->number;
+	new_analog_out_desc->aout_min_v = param->aout_min_v;
+	new_analog_out_desc->aout_max_v = param->aout_max_v;
+
+	/* Allocate memory for mbed specific analog output descriptor
+	 * for future use */
+	new_mbed_analog_out_desc = (mbed_analog_out_desc *)malloc
+				   (sizeof(new_mbed_analog_out_desc));
+	if (!new_mbed_analog_out_desc) {
+		goto err_mbed_analog_out_desc;
+	}
+
+	/* Create and initialize mbed analog output object */
+	analog_output = new AnalogOut((PinName)param->number);
+	if (analog_output) {
+		new_mbed_analog_out_desc->analog_out_obj = analog_output;
+	} else {
+		goto err_analog_out_instance;
+	}
+
+	new_analog_out_desc->extra = (mbed_analog_out_desc *)
+				     new_mbed_analog_out_desc;
+
+	*desc = new_analog_out_desc;
+	return SUCCESS;
+
+err_analog_out_instance:
+	free(new_mbed_analog_out_desc);
+err_mbed_analog_out_desc:
+	free(new_analog_out_desc);
+err_new_out_desc:
+	// Nothing to free
+
+	return FAILURE;
+}
+
+/**
+ * @brief Deallocate resources allocated by aout_init()
+ * @param desc[in, out] - Analog output descriptor
+ * @return SUCCESS in case of success, FAILURE otherwise.
+ */
+int32_t aout_remove(struct aout_desc *desc)
+{
+	if (desc) {
+
+		if (((mbed_analog_out_desc *)desc->extra)->analog_out_obj) {
+			delete((mbed::AnalogIn *)((mbed_analog_out_desc *)desc->
+						  extra)->analog_out_obj);
+		}
+
+		if ((mbed_analog_out_desc *)desc->extra) {
+			free((mbed_analog_out_desc *)desc->extra);
+		}
+
+		if (((mbed_analog_out_desc *)desc->extra)->analog_out_obj) {
+			delete((mbed::AnalogOut *)((mbed_analog_out_desc *)
+						   desc->extra)->analog_out_obj);
+		}
+
+		if ((mbed_analog_out_desc *)desc->extra) {
+			free((mbed_analog_out_desc *)desc->extra);
+		}
+
+		free(desc);
+
+		return SUCCESS;
+	}
+
+	return FAILURE;
+}
+
+/**
+ * @brief Mbed specific analog output platform ops structure
+ */
+const struct aout_platform_ops mbed_aout_ops = {
+	.init = &aout_init,
+	.write = &aout_set_voltage,
+	.remove = &aout_remove
+};
+
+
+// Platform drivers needs to be C-compatible to work with other drivers
+#ifdef __cplusplus
+}
+#endif //  _cplusplus
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/aout.h	Mon Sep 27 04:22:15 2021 +0000
@@ -0,0 +1,92 @@
+/***************************************************************************//**
+ *   @file   aout.h
+ *   @author PMallick (Pratyush.Mallick@analog.com)
+********************************************************************************
+ * Copyright (c) 2021 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.
+*******************************************************************************/
+
+#ifndef AOUT_H
+#define AOUT_H
+
+/******************************************************************************/
+/***************************** Include Files **********************************/
+/******************************************************************************/
+
+#include <stdint.h>
+
+/******************************************************************************/
+/********************** Macros and Constants Definitions **********************/
+/******************************************************************************/
+
+/******************************************************************************/
+/*************************** Types Declarations *******************************/
+/******************************************************************************/
+/**
+ * @struct aout_init_param
+ * @brief Structure holding the parameters for analog output initialization
+ */
+struct aout_init_param {
+	/* Analog output pin number */
+	int32_t	number;
+	/* Min output range of DAC in volts */
+	float aout_min_v;
+	/* Max output range of DAC in volts */
+	float aout_max_v;
+	/* Analog output reference voltage */
+	float vref;
+	/* Analog output platform specific functions */
+	const struct aout_platform_ops *platform_ops;
+};
+
+/**
+ * @struct aout_desc
+ * @brief Structure holding analog output descriptor
+ */
+struct aout_desc {
+	/* Analog output pin number */
+	int32_t	number;
+	/* Min output value of DAC in volts */
+	float aout_min_v;
+	/* Max output value of DAC in volts */
+	float aout_max_v;
+	/* Analog output reference voltage */
+	float vref;
+	/* Analog output platform specific functions */
+	const struct aout_platform_ops *platform_ops;
+	/* Analog extra parameters (device specific) */
+	void *extra;
+};
+
+/**
+ * @struct aout_platform_ops
+ * @brief Structure holding analog output function pointers that 
+ * point to the platform specific function
+ */
+struct aout_platform_ops {
+	/** Analog output initialization function pointer */
+	int32_t(*init)(struct aout_desc **, const struct aout_init_param *);
+	/** Analog output write function pointer */
+	int32_t(*write)(struct aout_desc *, float);
+	/** Analog output remove function pointer */
+	int32_t(*remove)(struct aout_desc *);
+};
+
+/******************************************************************************/
+/************************ Functions Declarations ******************************/
+/******************************************************************************/
+/* Write analog output voltage */
+int32_t aout_set_voltage(struct aout_desc *desc, float value);
+
+/* Initialize the analog output pin */
+int32_t aout_init(struct aout_desc **desc,
+		  const struct aout_init_param *param);
+
+/* Free the resources allocated by analog_out_init() */
+int32_t aout_remove(struct aout_desc *desc);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_ain_aout_extra.h	Mon Sep 27 04:22:15 2021 +0000
@@ -0,0 +1,74 @@
+/***************************************************************************//*
+* @file    mbed_ain_aout_extra.h
+* @brief   Header containing extra types required for
+*          analog in/output functionality
+******************************************************************************
+* Copyright (c) 2021 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.
+******************************************************************************/
+
+#ifndef MBED_AIN_AOUT_EXTRA_H_
+#define MBED_AIN_AOUT_EXTRA_H_
+
+// Platform support needs to be C-compatible to work with other drivers
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+/*****************************************************************************/
+/***************************** Include Files *********************************/
+/*****************************************************************************/
+
+#include "stdio.h"
+#include "stdint.h"
+
+/*****************************************************************************/
+/********************** Macros and Constants Definition **********************/
+/*****************************************************************************/
+
+/******************************************************************************/
+/********************** Variables and User defined data types *****************/
+/******************************************************************************/
+/**
+ * @struct mbed_analog_in_desc
+ * @brief  Analog input pin specific descriptor for the mbed platform.
+ */
+struct mbed_analog_in_desc {
+	/* Analog Input instance (mbed::AnalogIn) */
+	void *analog_in_obj;		
+};
+
+/**
+ * @struct mbed_analog_out_desc
+ * @brief  Analog output pin specific descriptor for the mbed platform.
+ */
+struct mbed_analog_out_desc {
+	/* Analog Output instance (mbed::AnalogOut) */
+	void *analog_out_obj;		
+};
+
+/**
+ * @brief mbed specific analog input platform ops structure
+ */
+extern const struct ain_platform_ops mbed_ain_ops;
+	
+/**
+ * @brief mbed specific analog output platform ops structure
+ */
+extern const struct aout_platform_ops mbed_out_ops;
+
+/******************************************************************************/
+/*****************************Function Declarations****************************/
+/******************************************************************************/
+
+#ifdef __cplusplus // Closing extern c
+}
+#endif
+
+#endif /* MBED_AIN_AOUT_EXTRA_H_ */
+