Pratyush Mallick
/
nano_dac
this is testing
Revision 0:e8a1ba50c46b, committed 2021-01-14
- Comitter:
- pmallick
- Date:
- Thu Jan 14 19:12:57 2021 +0530
- Commit message:
- this is testing
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.mbed Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,3 @@ +TARGET=SDP_K1 +TOOLCHAIN=GCC_ARM +ROOT=.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.mbedignore Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,2 @@ +noos_mbed/include/irq.h +noos_mbed/drivers/platform/mbed/src/irq.cpp \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/License.txt Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,14 @@ +Copyright (c) 2019 Analog Devices, Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + - Modified versions of the software must be conspicuously marked as such. + - This software is licensed solely and exclusively for use with processors/products manufactured by or for Analog Devices, Inc. + - This software may not be combined or merged with other code in any manner that would cause the software to become subject to terms and conditions which differ from those listed here. + - Neither the name of Analog Devices, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + - The use of this software may or may not infringe the patent rights of one or more patent holders. This license does not release you from the requirement that you obtain separate licenses from these patent holders to use this software. + +THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +2019-01-10-7CBSD SLA
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/app_config.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,259 @@ +/*************************************************************************//** + * @file app_config.h + * @brief Configuration file of nanodac firmware example program +****************************************************************************** +* Copyright (c) 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. +*****************************************************************************/ + +#ifndef _APP_CONFIG_H_ +#define _APP_CONFIG_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> +#include <PinNames.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +// **** Note for User: ACTIVE_DEVICE selection **** +// Define the device type here from the list of below device type defines +// e.g. #define DEV_AD5677R -> This will make AD5677R as an ACTIVE_DEVICE. +// The ACTIVE_DEVICE is default set to AD5686, if device type is not defined. + +//#define DEV_AD5677R + +/** + The ADI SDP_K1 can be used with both arduino headers + or the 120-pin SDP connector found on ADI evaluation + boards. The default is the SDP connector. + + Uncomment the ARDUINO #define below to enable the ARDUINO connector +*/ + +//#define ARDUINO + + +#if defined(DEV_AD5671R) +#define ACTIVE_DEVICE ID_AD5671R +#define ACTIVE_DEVICE_NAME "AD5671R" +#elif defined(DEV_AD5672R) +#define ACTIVE_DEVICE ID_AD5672R +#define ACTIVE_DEVICE_NAME "AD5672R" +#elif defined(DEV_AD5673R) +#define ACTIVE_DEVICE ID_AD5673R +#define ACTIVE_DEVICE_NAME "AD5673R" +#elif defined(DEV_AD5674) +#define ACTIVE_DEVICE ID_AD5674 +#define ACTIVE_DEVICE_NAME "AD5674" +#elif defined(DEV_AD5674R) +#define ACTIVE_DEVICE ID_AD5674R +#define ACTIVE_DEVICE_NAME "AD5674R" +#elif defined(DEV_AD5675R) +#define ACTIVE_DEVICE ID_AD5675R +#define ACTIVE_DEVICE_NAME "AD5675R" +#elif defined(DEV_AD5676) +#define ACTIVE_DEVICE ID_AD5676 +#define ACTIVE_DEVICE_NAME "AD5676" +#elif defined(DEV_AD5676R) +#define ACTIVE_DEVICE ID_AD5676R +#define ACTIVE_DEVICE_NAME "AD5676R" +#elif defined(DEV_AD5677R) +#define ACTIVE_DEVICE ID_AD5677R +#define ACTIVE_DEVICE_NAME "AD5677R" +#elif defined(DEV_AD5679) +#define ACTIVE_DEVICE ID_AD5679 +#define ACTIVE_DEVICE_NAME "AD5679" +#elif defined(DEV_AD5679R) +#define ACTIVE_DEVICE ID_AD5679R +#define ACTIVE_DEVICE_NAME "AD5679R" +#elif defined(DEV_AD5686) +#define ACTIVE_DEVICE ID_AD5686 +#define ACTIVE_DEVICE_NAME "AD5686" +#elif defined(DEV_AD5684R) +#define ACTIVE_DEVICE ID_AD5684R +#define ACTIVE_DEVICE_NAME "AD5684R" +#elif defined(DEV_AD5685R) +#define ACTIVE_DEVICE ID_AD5685R +#define ACTIVE_DEVICE_NAME "AD5685R" +#elif defined(DEV_AD5686R) +#define ACTIVE_DEVICE ID_AD5686R +#define ACTIVE_DEVICE_NAME "AD5686R" +#elif defined(DEV_AD5687) +#define ACTIVE_DEVICE ID_AD5687 +#define ACTIVE_DEVICE_NAME "AD5687" +#elif defined(DEV_AD5687R) +#define ACTIVE_DEVICE ID_AD5687R +#define ACTIVE_DEVICE_NAME "AD5687R" +#elif defined(DEV_AD5689) +#define ACTIVE_DEVICE ID_AD5689 +#define ACTIVE_DEVICE_NAME "AD5689" +#elif defined(DEV_AD5689R) +#define ACTIVE_DEVICE ID_AD5689R +#define ACTIVE_DEVICE_NAME "AD5689R" +#elif defined(DEV_AD5697R) +#define ACTIVE_DEVICE ID_AD5697R +#define ACTIVE_DEVICE_NAME "AD5697R" +#elif defined(DEV_AD5694) +#define ACTIVE_DEVICE ID_AD5694 +#define ACTIVE_DEVICE_NAME "AD5694" +#elif defined(DEV_AD5694R) +#define ACTIVE_DEVICE ID_AD5694R +#define ACTIVE_DEVICE_NAME "AD5694R" +#elif defined(DEV_AD5695R) +#define ACTIVE_DEVICE ID_AD5695R +#define ACTIVE_DEVICE_NAME "AD5695R" +#elif defined(DEV_AD5696) +#define ACTIVE_DEVICE ID_AD5696 +#define ACTIVE_DEVICE_NAME "AD5696" +#elif defined(DEV_AD5696R) +#define ACTIVE_DEVICE ID_AD5696R +#define ACTIVE_DEVICE_NAME "AD5696R" +#elif defined(DEV_AD5681R) +#define ACTIVE_DEVICE ID_AD5681R +#define ACTIVE_DEVICE_NAME "AD5681R" +#elif defined(DEV_AD5682R) +#define ACTIVE_DEVICE ID_AD5682R +#define ACTIVE_DEVICE_NAME "AD5682R" +#elif defined(DEV_AD5683R) +#define ACTIVE_DEVICE ID_AD5683R +#define ACTIVE_DEVICE_NAME "AD5683R" +#elif defined(DEV_AD5683) +#define ACTIVE_DEVICE ID_AD5683 +#define ACTIVE_DEVICE_NAME "AD5683" +#elif defined(DEV_AD5691R) +#define ACTIVE_DEVICE ID_AD5691R +#define ACTIVE_DEVICE_NAME "AD5691R" +#elif defined(DEV_AD5692R) +#define ACTIVE_DEVICE ID_AD5692R +#define ACTIVE_DEVICE_NAME "AD5692R" +#elif defined(DEV_AD5693R) +#define ACTIVE_DEVICE ID_AD5693R +#define ACTIVE_DEVICE_NAME "AD5693R" +#elif defined(DEV_AD5693) +#define ACTIVE_DEVICE ID_AD5693 +#define ACTIVE_DEVICE_NAME "AD5693" +#else +#warning No/Unsupported ADxxxxy symbol defined. AD5686R defined +#define DEV_AD5686R +#define ACTIVE_DEVICE ID_AD5686R +#define ACTIVE_DEVICE_NAME "AD5686R" +#endif + + +// Pin mapping of AD7124 with SDP-K1/Arduino (reference: PinNames.h) +#ifdef ARDUINO +#define I2C_SCL D15 // I2C_SCL +#define I2C_SDA D14 // I2C_SDA + +#define SPI_SS D10 // SPI_CS +#define SPI_MOSI D11 // SPI_MOSI +#define SPI_MISO D12 // SPI_MISO +#define SPI_SCK D13 // SPI_SCK + +#define GAIN_PIN D8 +#define RESET_PIN D9 +#define LDAC_PIN D7 +#define ADDR0_PIN D6 +#else +#define I2C_SCL SDP_I2C_SCL // PH_7 +#define I2C_SDA SDP_I2C_SDA // PC_9 + +#define SPI_SS SDP_SPI_CS_A // PB_9 +#define SPI_MISO SDP_SPI_MISO // PF_8 +#define SPI_MOSI SDP_SPI_MOSI // PF_9 +#define SPI_SCK SDP_SPI_SCK // PH_6 +#endif + +// Define the other GPIO mapping based on the compatible EVAL board +// *Note: The 7-bit I2C slave address mentioned below is the default address for the +// device, set by combination of slave address bits (7:3) from the device +// datasheet and default logic level of A1 and A0 pins (bits 2:1) on the +// respective device EVAL board. For more information, refer the device +// datasheet and EVAL board manual. + +#if defined(DEV_AD5686R) || defined(DEV_AD5686) || \ + defined(DEV_AD5684R) || defined(DEV_AD5684) || \ + defined(DEV_AD5685R) +// These devices support EVAL-AD5686RSDZ board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_0 +#define RESET_PIN SDP_GPIO_2 +#define LDAC_PIN SDP_GPIO_3 +#endif +#elif defined(DEV_AD5696R) || defined(DEV_AD5696) || \ + defined(DEV_AD5694R) || defined(DEV_AD5694) || \ + defined(DEV_AD5695R) || defined(DEV_AD5697R) +// These devices support EVAL-AD5696RSDZ board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_0 +#define RESET_PIN SDP_GPIO_2 +#define LDAC_PIN SDP_GPIO_3 +#endif +#define I2C_SLAVE_ADDRESS 0x18 +#elif defined(DEV_AD5683) || defined(DEV_AD5683R) || defined(DEV_AD5682R) || \ + defined(DEV_AD5681R) +// These devices uses EVAL-AD5683R board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_2 +#define RESET_PIN SDP_GPIO_1 +#define LDAC_PIN SDP_GPIO_0 +#endif +#elif defined(DEV_AD5693) || defined(DEV_AD5693R) || defined(DEV_AD5692R) || \ + defined(DEV_AD5691R) +// These devices uses EVAL-AD5693R board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_2 +#define RESET_PIN SDP_GPIO_1 +#define LDAC_PIN SDP_GPIO_0 +#endif +#define I2C_SLAVE_ADDRESS 0x98 +#elif defined (DEV_AD5674R) || defined (DEV_AD5674) || \ + defined (DEV_AD5679R) || defined (DEV_AD5679) || \ + defined (DEV_AD5677R) || defined (DEV_AD5673R) +// These devices uses EVAL-AD5679RSDZ/EVAL-AD567xRSDZ board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_0 +#define RESET_PIN SDP_GPIO_2 +#define LDAC_PIN SDP_GPIO_1 +#endif +#define I2C_SLAVE_ADDRESS 0x1E +#elif defined (DEV_AD5676R) || defined (DEV_AD5676) || \ + defined (DEV_AD5672R) +// These devices uses EVAL-AD5676RSDZ board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_2 +#define RESET_PIN SDP_GPIO_1 +#define LDAC_PIN SDP_GPIO_0 +#endif +#elif defined (DEV_AD5671R) || defined (DEV_AD5675R) +// These devices uses EVAL-AD5675RSDZ board +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_2 +#define RESET_PIN SDP_GPIO_1 +#define LDAC_PIN SDP_GPIO_0 +#endif +#define I2C_SLAVE_ADDRESS 0x18 +#else +#warning No/Unsupported EVAL board found. Using EVAL-AD5686R as default. +#if !defined ARDUINO +#define GAIN_PIN SDP_GPIO_0 +#define RESET_PIN SDP_GPIO_2 +#define LDAC_PIN SDP_GPIO_3 +#endif +#endif + + +// Common pin mappings +#define LED_GREEN LED3 // PK_5 + +#endif //_APP_CONFIG_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/main.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,52 @@ +/*! + ***************************************************************************** + @file: main.c + + @brief: main module for nanodac application interface + + @details: main module for nanodac application interface + + ----------------------------------------------------------------------------- + Copyright (c) 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. +******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdio.h> +#include "nanodac_console_app.h" + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/* @brief Main function + * + * @param None + * + * @return SUCCESS(0), FAILURE (Negative) + */ +int main() +{ + int32_t setupResult; + + /* Initialize the nanodac application */ + if ((setupResult = nanodac_app_initialize()) < 0) { + printf("Error setting up nanodac (%ld)\r\n\r\n", setupResult); + } + + /* Infinite loop */ + while (1) { + // display the console menu for the nanodac application + adi_do_console_menu(&nanodac_main_menu); + } + + // this line should not be reached + return - 1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/nanodac_console_app.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,766 @@ +/*! + ***************************************************************************** + @file: nanodac_console_app.c + + @brief: Implementation for the menu functions that handles the nanodac + functionality + + @details: This file is specific to nanodac console menu application handle. + The functions defined in this file performs the action + based on user selected console menu. + ----------------------------------------------------------------------------- + Copyright (c) 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. +*****************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdio.h> +#include <string.h> +#include <stdbool.h> + +#include "app_config.h" + +#include "ad5686.h" +#include "platform_support.h" +#include "platform_drivers.h" +#include "i2c_extra.h" +#include "spi_extra.h" + +#include "nanodac_console_app.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +#if !defined(I2C_SLAVE_ADDRESS) +#define I2C_SLAVE_ADDRESS 0 // For non I2C devices +#endif + +#define INTERNAL_VREF_VOLTAGE 2.5 +#define INTERNAL_VREF_SOURCE 0U +#define EXTERNAL_VREF_SOURCE 1U + +#define GAIN_LOW 1 +#define GAIN_HIGH 2 + +/******************************************************************************/ +/********************** Variables and User Defined Data Types *****************/ +/******************************************************************************/ + +// Pointer to a nanodac device structure +static struct ad5686_dev *nanodac_dev = NULL; + +// Current DAC channel (default value is channel 0) +static enum ad5686_dac_channels current_dac_channel = AD5686_CH_0; + +// Define the Vref source and voltage +#if !defined(EXT_VREF_SOURCE_ONLY) +// Default Vref voltage is 2.5v for internal reference source +static uint32_t vref_source = INTERNAL_VREF_SOURCE; +static float vref_voltage = INTERNAL_VREF_VOLTAGE; +#else +// Vref voltage for external reference source is user selectable +static uint32_t vref_source = EXTERNAL_VREF_SOURCE; +static float vref_voltage = 0; +#endif + +// Gain value (default is 1) +static uint32_t gain = GAIN_LOW; + +// LDAC pin state (default is High/Vlogic) +static uint32_t ldac_pin_state = GPIO_HIGH; + +// LDAC mask status (default is False/Disable) +static bool ldac_mask_status = false; + + +// Vref sources string +static const char *vref_source_str[] = { + "Internal", + "External" +}; + +// Operating mode string +static const char *operating_mode_str[] = { + "Normal Power-Up", + "1K to GND", + "100K to GND", + "Three State" +}; + +// Menu pre-declarations +extern console_menu dac_channel_select_menu; +extern console_menu vref_select_menu; +extern console_menu gain_select_menu; +extern console_menu dac_readback_select_menu; +extern console_menu ldac_mask_select_menu; +extern console_menu operating_mode_select_menu; +extern console_menu ldac_pin_select_menu; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +static void gpio_power_up_configuration(void); + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/* @brief Initialize the nanodac device + * @param none + * @return SUCCESS(0), FAILURE(negative) + **/ +int32_t nanodac_app_initialize(void) +{ + int32_t device_init_status; // Init status of device + + // Initialize the exra parameters for I2C initialization + mbed_i2c_init_param i2c_init_extra_params = { + .i2c_sda_pin = I2C_SDA, + .i2c_scl_pin = I2C_SCL + }; + + // Initialize the exra parameters for SPI initialization + mbed_spi_init_param spi_init_extra_params = { + .spi_miso_pin = SPI_MISO, + .spi_mosi_pin = SPI_MOSI, + .spi_clk_pin = SPI_SCK + }; + + // Initialize the device structure + struct ad5686_init_param nanodac_init_params = { + // i2c_init_param + { + 100000, // I2C max speed (Hz) + I2C_SLAVE_ADDRESS, // I2C slave address + &i2c_init_extra_params // I2C extra init parameters + }, + + // spi_init_param + { + 2000000, // SPI max speed (Hz) + SPI_SS, // Chip select + SPI_MODE_2, // SPI Mode + &spi_init_extra_params // SPI extra init parameters + }, + + // gpio_init_param + { RESET_PIN, NULL }, // gpio_reset + { LDAC_PIN, NULL }, // gpio_ldac + { GAIN_PIN, NULL }, // gpio_gain + + ACTIVE_DEVICE // Active device + }; + + // Initialize the device + device_init_status = ad5686_init(&nanodac_dev, nanodac_init_params); + + // Configure the GPIOs specific to application upon power-up + gpio_power_up_configuration(); + + return device_init_status; +} + + +/*! + * @brief Set the power-up GPIO configurations + * @return None + */ +static void gpio_power_up_configuration(void) +{ + // Set the directions for GPIO pins + (void)gpio_direction_output(nanodac_dev->gpio_gain, GPIO_OUT); + (void)gpio_direction_output(nanodac_dev->gpio_ldac, GPIO_OUT); + (void)gpio_direction_output(nanodac_dev->gpio_reset, GPIO_OUT); + + // Set the GPIO values + gpio_set_value(nanodac_dev->gpio_reset, GPIO_HIGH); + gpio_set_value(nanodac_dev->gpio_gain, GPIO_LOW); + gpio_set_value(nanodac_dev->gpio_ldac, GPIO_HIGH); +} + + +/*! + * @brief Display the header info for menu + * @return None + */ +static void display_menu_header(void) +{ + printf("\t%s (nanodac) | ", ACTIVE_DEVICE_NAME); + printf("Vref:%s (%.1fV) | ", vref_source_str[vref_source], vref_voltage); + printf("Gain:%ld"EOL, gain); +} + + +/*! + * @brief Display the footer info for menu + * @return None + */ +static void display_menu_footer(void) +{ + // Display the device name + printf("\tActive Channel: %d | ", current_dac_channel); + printf("LDAC Pin: %ld | ", ldac_pin_state); + printf("LDAC Mask: %d"EOL, ldac_mask_status); +} + + +/*! + * @brief Handle the DAC channel selection menu + * @param uint32_t channel_id- Selected DAC channel number + * @return MENU_CONTINUE + */ +static int32_t menu_select_dac_channel(uint32_t channel_id) +{ + // Store the dac channel for future read/write operations + current_dac_channel = (enum ad5686_dac_channels)channel_id; + + printf(EOL EOL"\tDAC Channel %ld is selected..."EOL, channel_id); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to display DAC channel selection + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_dac_channels(uint32_t menu_id) +{ + // Display the dac channel selection menu + adi_do_console_menu(&dac_channel_select_menu); + + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to write DAC input register + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_write_to_input_register(uint32_t menu_id) +{ + uint16_t dac_data_input; // Data to be written to DAC + + printf(EOL"\tEnter the Data/Code (in decimal): "); + dac_data_input = (uint16_t)adi_get_decimal_int(5); + + // Write DAC input register for current selected channel + ad5686_write_register(nanodac_dev, current_dac_channel, dac_data_input); + + printf(EOL EOL"\tData %d written to DAC input register..."EOL, dac_data_input); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to update DAC register with value from input register + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_update_dac_from_input(uint32_t menu_id) +{ + // Update the DAC with input register data for current selected channel + ad5686_update_register(nanodac_dev, current_dac_channel); + + printf(EOL EOL"\tUpdated DAC register with contents of input register..."EOL); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to update DAC register by asserting LDAC pin + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_update_dac_by_ldac_assert(uint32_t menu_id) +{ + // Update DAC registers by asserting LDAC pin High to Low + gpio_set_value(nanodac_dev->gpio_ldac, GPIO_HIGH); + mdelay(1); + gpio_set_value(nanodac_dev->gpio_ldac, GPIO_LOW); + mdelay(1); + + // Restore the previous state of LDAC pin + if (ldac_pin_state == GPIO_HIGH) { + gpio_set_value(nanodac_dev->gpio_ldac, GPIO_HIGH); + } + + printf(EOL EOL"\tUpdated DAC register with contents of input register..."EOL); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to write and update DAC register directly + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_write_and_update_dac(uint32_t menu_id) +{ + uint16_t dac_data_input; // Data to be written to DAC + + printf(EOL"\tEnter the Data/Code (in decimal): "); + dac_data_input = (uint16_t)adi_get_decimal_int(5); + + // Update DAC data register for current selected channel + ad5686_write_update_register(nanodac_dev, current_dac_channel, dac_data_input); + + printf(EOL EOL"\tDAC updated with Data %d"EOL, dac_data_input); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to select operating mode of DAC + * @param uint32_t operating_mode_input- User input operating mode + * @return MENU_CONTINUE + */ +static int32_t menu_select_operating_mode(uint32_t operating_mode_input) +{ + // Select the operating mode of DAC + ad5686_power_mode(nanodac_dev, current_dac_channel, operating_mode_input); + + printf(EOL EOL"\tSelected operating mode as %s"EOL, + operating_mode_str[operating_mode_input]); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to display operating modes of DAC + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_dac_operating_modes(uint32_t menu_id) +{ + // Display the operating mode select menu + adi_do_console_menu(&operating_mode_select_menu); + + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to select the reference source for DAC + * @param uint32_t ref_source_input- user selected reference source + * @return MENU_CONTINUE + */ +static int32_t menu_select_vref_source(uint32_t ref_source_input) +{ + float user_vref_value = INTERNAL_VREF_VOLTAGE; // user input reference + // voltage value + + if (ref_source_input == INTERNAL_VREF_SOURCE) { + // Enable the internal reference source + ad5686_internal_reference(nanodac_dev, AD5686_INTREF_EN); + vref_source = INTERNAL_VREF_SOURCE; + vref_voltage = INTERNAL_VREF_VOLTAGE; + + // Display the vref selections on console window + printf(EOL"\tVref Source: %s"EOL, vref_source_str[vref_source]); + printf("\tVref Voltage: %f"EOL, vref_voltage); + } else if (ref_source_input == EXTERNAL_VREF_SOURCE) { + printf(EOL EOL"\tEnter the external reference voltage"EOL); + user_vref_value = adi_get_decimal_float(5); + + // Disable the internal reference source + ad5686_internal_reference(nanodac_dev, AD5686_INTREF_DIS); + vref_source = EXTERNAL_VREF_SOURCE; + vref_voltage = user_vref_value; + + // Display the vref selections on console window + printf(EOL"\tVref Source: %s"EOL, vref_source_str[vref_source]); + printf("\tVref Voltage: %f"EOL, vref_voltage); + } else { + printf(EOL EOL"\tInvalid Vref Source selection"EOL); + } + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to display the reference source for DAC + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_vref_sources(uint32_t menu_id) +{ + // Display the Vref selection menu + adi_do_console_menu(&vref_select_menu); + + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to readback DAC register for selected channel + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_dac_readback(uint32_t menu_id) +{ + uint16_t dac_data; // Readback DAC data + float output_voltage; // Output voltage of DAC channel + + // Readback data for current selected DAC channel + dac_data = ad5686_read_back_register(nanodac_dev, current_dac_channel); + + // Calculate equivalent output voltage + output_voltage = (vref_voltage * gain * ((float)dac_data / TOTAL_OUTPUT_CODES)); + + printf(EOL EOL"\tDAC Channel %d Data: %d"EOL, current_dac_channel, dac_data); + printf(EOL"\tVoltage: %.3f V"EOL EOL, output_voltage); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to select LDACx mask (x:DAC channel) + * @param uint32_t mask_status- user input LDAC mask + * @return MENU_CONTINUE + */ +static int32_t menu_set_ldac_mask(uint32_t mask_status) +{ + // Set the LDAC mask (0/1) + ad5686_ldac_mask(nanodac_dev, current_dac_channel, mask_status); + + ldac_mask_status = mask_status; + printf(EOL EOL"\tLDAC Mask for Channel %d: %ld"EOL, current_dac_channel, + mask_status); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to display LDACx mask menu selections + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_ldac_masks(uint32_t menu_id) +{ + // Display the LDAC mask selection menu + adi_do_console_menu(&ldac_mask_select_menu); + + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to set LDAC pin state + * @param uint32_t pin_state- user input pin state (HIGH/LOW) + * @return MENU_CONTINUE + */ +static int32_t menu_set_ldac_pin(uint32_t pin_state) +{ + if (pin_state == GPIO_HIGH) { + gpio_set_value(nanodac_dev->gpio_ldac, GPIO_HIGH); + } else { + gpio_set_value(nanodac_dev->gpio_ldac, GPIO_LOW); + } + + ldac_pin_state = pin_state; + printf(EOL EOL"\tLDAC pin set to %ld"EOL, ldac_pin_state); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to assert LDAC pin to update DAC + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_select_ldac_pin_state(uint32_t menu_id) +{ + // Display the LDAC pin selection menu + adi_do_console_menu(&ldac_pin_select_menu); + + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to set the DAC gain + * @param uint32_t gain_input- Gain input value + * @return MENU_CONTINUE + */ +static int32_t menu_set_gain(uint32_t gain_input) +{ + // Set the device gain + gain = gain_input; + +#if defined (SOFTWARE_CONTROLLED_GAIN) + ad5686_gain_mode(nanodac_dev, gain); +#else + if (gain == GAIN_LOW) { + gpio_set_value(nanodac_dev->gpio_gain, GPIO_LOW); + } else { + gpio_set_value(nanodac_dev->gpio_gain, GPIO_HIGH); + } +#endif + + printf(EOL EOL"\tGain set to %ld"EOL,gain); + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to display DAC gain selections + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_gain_selection(uint32_t menu_id) +{ + // Display the DAC gain selection menu + adi_do_console_menu(&gain_select_menu); + + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to perform DAC software reset + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_assert_software_reset(uint32_t menu_id) +{ + // Do device software reset + ad5686_software_reset(nanodac_dev); + + printf(EOL"\tSoftware Reset Complete..."EOL); + + // Device reset disables the LDAC mask through hardware. + // This needs to be synched with software. + ldac_mask_status = false; + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +/*! + * @brief Handle the menu to perform DAC hardware reset + * @param uint32_t menu_id- (Optional parameter) + * @return MENU_CONTINUE + */ +static int32_t menu_assert_hardware_reset(uint32_t menu_id) +{ + // Do device hardware reset + gpio_set_value(nanodac_dev->gpio_reset, GPIO_LOW); + mdelay(1); + gpio_set_value(nanodac_dev->gpio_reset, GPIO_HIGH); + + printf(EOL"\tHardware Reset Complete..."EOL); + + // Device reset disables the LDAC mask through hardware. + // This needs to be synched with software. + ldac_mask_status = false; + + adi_press_any_key_to_continue(); + return MENU_CONTINUE; +} + + +// Operating mode menu for DAC +static console_menu_item operating_mode_select_items[] = { + { "Normal Power-Up", 'A', menu_select_operating_mode, AD5686_PWRM_NORMAL }, + +#if defined(_1K_TO_GND_POWER_DOWN) + { "1K to GND (Power-Down)", 'B', menu_select_operating_mode, AD5686_PWRM_1K }, +#endif +#if defined(_100K_TO_GND_POWER_DOWN) + { "100K to GND (Power-Down)", 'C', menu_select_operating_mode, AD5686_PWRM_100K }, +#endif +#if defined(THREE_STATE_POWER_DOWN) + { "Three-State (Power-Down)", 'D', menu_select_operating_mode, AD5686_PWRM_THREESTATE }, +#endif +}; + +console_menu operating_mode_select_menu = { + .title = "Select Operating Mode", + .items = operating_mode_select_items, + .itemCount = ARRAY_SIZE(operating_mode_select_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = true +}; + + +// LDAC pin set menu for DAC +static console_menu_item ldac_pin_select_items[] = { + { "High (VLogic)", 'H', menu_set_ldac_pin, GPIO_HIGH }, + { "Low (GND)", 'L', menu_set_ldac_pin, GPIO_LOW } +}; + +console_menu ldac_pin_select_menu = { + .title = "Select LDAC Mask", + .items = ldac_pin_select_items, + .itemCount = ARRAY_SIZE(ldac_pin_select_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = true +}; + + +// LDAC mask menu for DAC +static console_menu_item ldac_mask_select_items[] = { + { "Disable (LDAC Pin Controlled)", 'D', menu_set_ldac_mask, 0 }, + { "Enable (LDAC Pin Ignored)", 'E', menu_set_ldac_mask, 1 } +}; + +console_menu ldac_mask_select_menu = { + .title = "Select LDAC Mask", + .items = ldac_mask_select_items, + .itemCount = ARRAY_SIZE(ldac_mask_select_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = true +}; + + +// Gain select menu for DAC +static console_menu_item gain_select_items[] = { + { "Gain= 1 (Vout: 0-Vref)", '1', menu_set_gain, GAIN_LOW }, + { "Gain= 2 (Vout: 0-2*Vref)", '2', menu_set_gain, GAIN_HIGH } +}; + +console_menu gain_select_menu = { + .title = "Select Gain", + .items = gain_select_items, + .itemCount = ARRAY_SIZE(gain_select_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = true +}; + + +// Vref select menu for DAC +static console_menu_item vref_select_items[] = { +#if !defined(EXT_VREF_SOURCE_ONLY) + { "Internal Vref", 'I', menu_select_vref_source, INTERNAL_VREF_SOURCE }, +#endif + { "External Vref", 'E', menu_select_vref_source, EXTERNAL_VREF_SOURCE } +}; + +console_menu vref_select_menu = { + .title = "Select Vref Source", + .items = vref_select_items, + .itemCount = ARRAY_SIZE(vref_select_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = true +}; + + +// Channel selection menu for DAC +static console_menu_item dac_channel_select_items[] = { + { "Channel 0", 'A', menu_select_dac_channel, (uint32_t)AD5686_CH_0 }, + { "Channel 1", 'B', menu_select_dac_channel, (uint32_t)AD5686_CH_1 }, +#if DAC_CHANNEL_COUNT > 2 + { "Channel 2", 'C', menu_select_dac_channel, (uint32_t)AD5686_CH_2 }, + { "Channel 3", 'D', menu_select_dac_channel, (uint32_t)AD5686_CH_3 }, +#endif +#if DAC_CHANNEL_COUNT > 4 + { "Channel 4", 'E', menu_select_dac_channel, (uint32_t)AD5686_CH_4 }, + { "Channel 5", 'F', menu_select_dac_channel, (uint32_t)AD5686_CH_5 }, + { "Channel 6", 'G', menu_select_dac_channel, (uint32_t)AD5686_CH_6 }, + { "Channel 7", 'H', menu_select_dac_channel, (uint32_t)AD5686_CH_7 }, +#endif +#if DAC_CHANNEL_COUNT > 8 + { "Channel 8", 'I', menu_select_dac_channel, (uint32_t)AD5686_CH_8 }, + { "Channel 9", 'J', menu_select_dac_channel, (uint32_t)AD5686_CH_9 }, + { "Channel 10", 'K', menu_select_dac_channel, (uint32_t)AD5686_CH_10 }, + { "Channel 11", 'L', menu_select_dac_channel, (uint32_t)AD5686_CH_11 }, + { "Channel 12", 'M', menu_select_dac_channel, (uint32_t)AD5686_CH_12 }, + { "Channel 13", 'N', menu_select_dac_channel, (uint32_t)AD5686_CH_13 }, + { "Channel 14", 'O', menu_select_dac_channel, (uint32_t)AD5686_CH_14 }, + { "Channel 15", 'P', menu_select_dac_channel, (uint32_t)AD5686_CH_15 }, +#endif +}; + +console_menu dac_channel_select_menu = { + .title = "Select DAC Channel", + .items = dac_channel_select_items, + .itemCount = ARRAY_SIZE(dac_channel_select_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = true +}; + + +/* + * Definition of the Main Menu Items and menu itself + */ +static console_menu_item main_menu_items[] = { +#ifdef DISPLAY_DAC_CHANNEL_SELECT_MENU + { "Select DAC Channel", 'A', menu_dac_channels }, + {""}, +#endif + { "Write to Input Register (LDAC Dependent)", 'B',menu_write_to_input_register }, + {""}, + { "Update DAC from Input Register", 'C', menu_update_dac_from_input }, + {""}, + { "Update DAC by LDAC Assert (H->L)", 'D', menu_update_dac_by_ldac_assert }, + {""}, + { "Write and Update DAC (Direct Update)", 'E', menu_write_and_update_dac }, + {""}, + { "Read Back DAC Channel", 'F', menu_dac_readback }, + {""}, +#ifdef DISPLAY_LDAC_MASK_SELECT_MENU + { "Set LDAC# Mask", 'G', menu_ldac_masks }, + {""}, +#endif + { "Select LDAC Pin State", 'H', menu_select_ldac_pin_state }, + {""}, + { "Select Operating Mode", 'I', menu_dac_operating_modes }, + {""}, + { "Select Reference Source (Vref)", 'J', menu_vref_sources }, + {""}, + { "Set Gain", 'K', menu_gain_selection }, + {""}, + { "Assert Software Reset", 'L', menu_assert_software_reset }, + {""}, + { "Assert Hardware Reset", 'M', menu_assert_hardware_reset }, + {""}, +}; + +console_menu nanodac_main_menu = { + .title = "Main Menu", + .items = main_menu_items, + .itemCount = ARRAY_SIZE(main_menu_items), + .headerItem = display_menu_header, + .footerItem = display_menu_footer, + .enableEscapeKey = false +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/nanodac_console_app.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,122 @@ +/*! + ***************************************************************************** + @file: nanodac_console_app.h + + @brief: defines the console menu structure for the nanodac example code + + @details: + ----------------------------------------------------------------------------- + Copyright (c) 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. +*****************************************************************************/ + +#ifndef NANODAC_CONSOLE_APP_H_ +#define NANODAC_CONSOLE_APP_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include "adi_console_menu.h" +#include "app_config.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +// Define the DAC channel menu selection. The following devices from nanodac +// family have only single channel and does not need menu to select DAC channel. +#if !(defined(DEV_AD5683) || defined(DEV_AD5683R) || defined(DEV_AD5682R) || \ + defined(DEV_AD5681R) || defined(DEV_AD5693) || defined(DEV_AD5693R) || \ + defined(DEV_AD5692R) || defined(DEV_AD5691R)) +#define DISPLAY_DAC_CHANNEL_SELECT_MENU +// Define the number of DAC channels (for devices having more than 1 channel) +#if (defined(DEV_AD5687) || defined(DEV_AD5687R) || defined(DEV_AD5697R) || \ + defined(DEV_AD5689) || defined(DEV_AD5689R)) +#define DAC_CHANNEL_COUNT 2 +#elif (defined(DEV_AD5686) || defined(DEV_AD5686R) || defined(DEV_AD5685R) || \ + defined(DEV_AD5684) || defined(DEV_AD5684R) || \ + defined(DEV_AD5696) || defined(DEV_AD5696R) || defined(DEV_AD5695R) || \ + defined(DEV_AD5694) || defined(DEV_AD5694R)) +#define DAC_CHANNEL_COUNT 4 +#elif (defined(DEV_AD5676) || defined(DEV_AD5676R) || defined(DEV_AD5672R) || \ + defined(DEV_AD5675R) || defined(DEV_AD5671R)) +#define DAC_CHANNEL_COUNT 8 +#elif (defined(DEV_AD5679) || defined(DEV_AD5679R) || defined(DEV_AD5673R) || \ + defined(DEV_AD5674) || defined(DEV_AD5674R) || defined(DEV_AD5677R)) +#define DAC_CHANNEL_COUNT 16 +#endif +#endif + +// Define the LDAC masking menu. The following devices from nanodac +// family have only single channel and does not need menu to select LDAC masking. +#if !(defined(DEV_AD5683) || defined(DEV_AD5683R) || \ + defined(DEV_AD5682R) || defined(DEV_AD5681R) || defined(DEV_AD5693) || \ + defined(DEV_AD5693R) || defined(DEV_AD5692R) || defined(DEV_AD5691R)) +#define DISPLAY_LDAC_MASK_SELECT_MENU +#endif + +// Define the Vref source selections. The following devices from nanodac +// family have only external Vref source. +#if (defined(DEV_AD5674) || defined(DEV_AD5676) || defined(DEV_AD5686) || \ + defined(DEV_AD5684) || defined(DEV_AD5696) || defined(DEV_AD5694) || \ + defined(DEV_AD5683) || defined(DEV_AD5693) || defined(DEV_AD5679) || \ + defined(DEV_AD5687) || defined(DEV_AD5689)) +#define EXT_VREF_SOURCE_ONLY +#endif + +// Define the gain source (software controlled or hardware controlled) +#if (defined(DEV_AD5683) || defined(DEV_AD5683R) || defined(DEV_AD5682R) || \ + defined(DEV_AD5681R) || defined(DEV_AD5693) || defined(DEV_AD5693R) || \ + defined(DEV_AD5692R) || defined(DEV_AD5691R)) +#define SOFTWARE_CONTROLLED_GAIN +#else +#define HARDWARE_CONTROLLED_GAIN +#endif + +// Define the operating mode selections +#if (defined(DEV_AD5674) || defined(DEV_AD5674R) || defined(DEV_AD5673R) || \ + defined(DEV_AD5679) || defined(DEV_AD5677R)) +#define _1K_TO_GND_POWER_DOWN +#elif (defined(DEV_AD5676) || defined(DEV_AD5676R) || defined(DEV_AD5675R) || \ + defined(DEV_AD5671R) || defined(DEV_AD5672R)) +#define _1K_TO_GND_POWER_DOWN +#define THREE_STATE_POWER_DOWN +#else +#define _1K_TO_GND_POWER_DOWN +#define _100K_TO_GND_POWER_DOWN +#define THREE_STATE_POWER_DOWN +#endif + +// define the DAC resolution +#if (defined(DEV_AD5671R) || defined(DEV_AD5672R) || defined(DEV_AD5673R) || \ + defined(DEV_AD5674) || defined(DEV_AD5674R) || defined(DEV_AD5684R) || \ + defined(DEV_AD5687) || defined(DEV_AD5687R) || defined(DEV_AD5697R) || \ + defined(DEV_AD5694) || defined(DEV_AD5694R) || defined(DEV_AD5681R) || \ + defined(DEV_AD5691R)) +#define TOTAL_OUTPUT_CODES ((1U << 12) - 1) // For 12-bit DAC (2^12 - 1) +#elif (defined(DEV_AD5685R) || defined(DEV_AD5695R) || defined(DEV_AD5682R) || \ + defined(DEV_AD5692R)) +#define TOTAL_OUTPUT_CODES ((1U << 14) - 1) // For 14-bit DAC (2^14 - 1) +#else +#define TOTAL_OUTPUT_CODES ((1U << 16) - 1) // For 16-bit DAC (2^16 - 1) +#endif + +/******************************************************************************/ +/********************** Variables and User Defined Data Types *****************/ +/******************************************************************************/ + +extern console_menu nanodac_main_menu; + +/******************************************************************************/ +/************************ Public Declarations *********************************/ +/******************************************************************************/ + +int32_t nanodac_app_initialize(void); + + +#endif /* NANODAC_CONSOLE_APP_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#5941d1718339116cd12914238ec331c84da3d08f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed_app.json Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,16 @@ +{ + "requires": ["bare-metal"], + "macros": [ + "TINYIIOD_VERSION_MAJOR=0", + "TINYIIOD_VERSION_MINOR=1", + "TINYIIOD_VERSION_GIT=\"0.1\"", + "IIOD_BUFFER_SIZE=0x1000", + "_USE_STD_INT_TYPES" + ], + "target_overrides": { + "*": { + "platform.default-serial-baud-rate": 115200, + "target.printf_lib": "std" + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed.lib Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +https://bitbucket.analog.com/scm/pcts/noos_mbed.git/#f9f32e227d02859c98ce533d507eb48d372ee6fb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/FETCH_HEAD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,3 @@ +f9f32e227d02859c98ce533d507eb48d372ee6fb not-for-merge branch 'develop' of https://bitbucket.analog.com/scm/pcts/noos_mbed +4845c2586327565faa9de97a5f5ef6f05c758339 not-for-merge branch 'noos_gh_master' of https://bitbucket.analog.com/scm/pcts/noos_mbed +72e85bcaab60dfc4da4ed6730c8c1b9c1eb1b5f2 not-for-merge tag 'Initial_Version' of https://bitbucket.analog.com/scm/pcts/noos_mbed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/HEAD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +ref: refs/heads/develop
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/ORIG_HEAD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +f9f32e227d02859c98ce533d507eb48d372ee6fb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/config Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,13 @@ +[core] + repositoryformatversion = 0 + filemode = false + bare = false + logallrefupdates = true + symlinks = false + ignorecase = true +[remote "origin"] + url = https://bitbucket.analog.com/scm/pcts/noos_mbed.git + fetch = +refs/heads/*:refs/remotes/origin/* +[branch "develop"] + remote = origin + merge = refs/heads/develop
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/description Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +Unnamed repository; edit this file 'description' to name the repository.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/applypatch-msg.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,15 @@ +#!/bin/sh +# +# An example hook script to check the commit log message taken by +# applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. The hook is +# allowed to edit the commit message file. +# +# To enable this hook, rename this file to "applypatch-msg". + +. git-sh-setup +commitmsg="$(git rev-parse --git-path hooks/commit-msg)" +test -x "$commitmsg" && exec "$commitmsg" ${1+"$@"} +:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/commit-msg.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to check the commit log message. +# Called by "git commit" with one argument, the name of the file +# that has the commit message. The hook should exit with non-zero +# status after issuing an appropriate message if it wants to stop the +# commit. The hook is allowed to edit the commit message file. +# +# To enable this hook, rename this file to "commit-msg". + +# Uncomment the below to add a Signed-off-by line to the message. +# Doing this in a hook is a bad idea in general, but the prepare-commit-msg +# hook is more suited to it. +# +# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" + +# This example catches duplicate Signed-off-by lines. + +test "" = "$(grep '^Signed-off-by: ' "$1" | + sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || { + echo >&2 Duplicate Signed-off-by lines. + exit 1 +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/fsmonitor-watchman.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,173 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use IPC::Open2; + +# An example hook script to integrate Watchman +# (https://facebook.github.io/watchman/) with git to speed up detecting +# new and modified files. +# +# The hook is passed a version (currently 2) and last update token +# formatted as a string and outputs to stdout a new update token and +# all files that have been modified since the update token. Paths must +# be relative to the root of the working tree and separated by a single NUL. +# +# To enable this hook, rename this file to "query-watchman" and set +# 'git config core.fsmonitor .git/hooks/query-watchman' +# +my ($version, $last_update_token) = @ARGV; + +# Uncomment for debugging +# print STDERR "$0 $version $last_update_token\n"; + +# Check the hook interface version +if ($version ne 2) { + die "Unsupported query-fsmonitor hook version '$version'.\n" . + "Falling back to scanning...\n"; +} + +my $git_work_tree = get_working_dir(); + +my $retry = 1; + +my $json_pkg; +eval { + require JSON::XS; + $json_pkg = "JSON::XS"; + 1; +} or do { + require JSON::PP; + $json_pkg = "JSON::PP"; +}; + +launch_watchman(); + +sub launch_watchman { + my $o = watchman_query(); + if (is_work_tree_watched($o)) { + output_result($o->{clock}, @{$o->{files}}); + } +} + +sub output_result { + my ($clockid, @files) = @_; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # binmode $fh, ":utf8"; + # print $fh "$clockid\n@files\n"; + # close $fh; + + binmode STDOUT, ":utf8"; + print $clockid; + print "\0"; + local $, = "\0"; + print @files; +} + +sub watchman_clock { + my $response = qx/watchman clock "$git_work_tree"/; + die "Failed to get clock id on '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + + return $json_pkg->new->utf8->decode($response); +} + +sub watchman_query { + my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') + or die "open2() failed: $!\n" . + "Falling back to scanning...\n"; + + # In the query expression below we're asking for names of files that + # changed since $last_update_token but not from the .git folder. + # + # To accomplish this, we're using the "since" generator to use the + # recency index to select candidate nodes and "fields" to limit the + # output to file names only. Then we're using the "expression" term to + # further constrain the results. + if (substr($last_update_token, 0, 1) eq "c") { + $last_update_token = "\"$last_update_token\""; + } + my $query = <<" END"; + ["query", "$git_work_tree", { + "since": $last_update_token, + "fields": ["name"], + "expression": ["not", ["dirname", ".git"]] + }] + END + + # Uncomment for debugging the watchman query + # open (my $fh, ">", ".git/watchman-query.json"); + # print $fh $query; + # close $fh; + + print CHLD_IN $query; + close CHLD_IN; + my $response = do {local $/; <CHLD_OUT>}; + + # Uncomment for debugging the watch response + # open ($fh, ">", ".git/watchman-response.json"); + # print $fh $response; + # close $fh; + + die "Watchman: command returned no output.\n" . + "Falling back to scanning...\n" if $response eq ""; + die "Watchman: command returned invalid output: $response\n" . + "Falling back to scanning...\n" unless $response =~ /^\{/; + + return $json_pkg->new->utf8->decode($response); +} + +sub is_work_tree_watched { + my ($output) = @_; + my $error = $output->{error}; + if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { + $retry--; + my $response = qx/watchman watch "$git_work_tree"/; + die "Failed to make watchman watch '$git_work_tree'.\n" . + "Falling back to scanning...\n" if $? != 0; + $output = $json_pkg->new->utf8->decode($response); + $error = $output->{error}; + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + # Uncomment for debugging watchman output + # open (my $fh, ">", ".git/watchman-output.out"); + # close $fh; + + # Watchman will always return all files on the first query so + # return the fast "everything is dirty" flag to git and do the + # Watchman query just to get it over with now so we won't pay + # the cost in git to look up each individual file. + my $o = watchman_clock(); + $error = $output->{error}; + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + output_result($o->{clock}, ("/")); + $last_update_token = $o->{clock}; + + eval { launch_watchman() }; + return 0; + } + + die "Watchman: $error.\n" . + "Falling back to scanning...\n" if $error; + + return 1; +} + +sub get_working_dir { + my $working_dir; + if ($^O =~ 'msys' || $^O =~ 'cygwin') { + $working_dir = Win32::GetCwd(); + $working_dir =~ tr/\\/\//; + } else { + require Cwd; + $working_dir = Cwd::cwd(); + } + + return $working_dir; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/post-update.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/pre-applypatch.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,14 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed +# by applypatch from an e-mail message. +# +# The hook should exit with non-zero status after issuing an +# appropriate message if it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-applypatch". + +. git-sh-setup +precommit="$(git rev-parse --git-path hooks/pre-commit)" +test -x "$precommit" && exec "$precommit" ${1+"$@"} +:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/pre-commit.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,49 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. +# +# To enable this hook, rename this file to "pre-commit". + +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(git hash-object -t tree /dev/null) +fi + +# If you want to allow non-ASCII filenames set this variable to true. +allownonascii=$(git config --type=bool hooks.allownonascii) + +# Redirect output to stderr. +exec 1>&2 + +# Cross platform projects tend to avoid non-ASCII filenames; prevent +# them from being added to the repository. We exploit the fact that the +# printable range starts at the space character and ends with tilde. +if [ "$allownonascii" != "true" ] && + # Note that the use of brackets around a tr range is ok here, (it's + # even required, for portability to Solaris 10's /usr/bin/tr), since + # the square bracket bytes happen to fall in the designated range. + test $(git diff --cached --name-only --diff-filter=A -z $against | + LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 +then + cat <<\EOF +Error: Attempt to add a non-ASCII file name. + +This can cause problems if you want to work with people on other platforms. + +To be portable it is advisable to rename the file. + +If you know what you are doing you can disable this check using: + + git config hooks.allownonascii true +EOF + exit 1 +fi + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached $against --
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/pre-merge-commit.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,13 @@ +#!/bin/sh +# +# An example hook script to verify what is about to be committed. +# Called by "git merge" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message to +# stderr if it wants to stop the merge commit. +# +# To enable this hook, rename this file to "pre-merge-commit". + +. git-sh-setup +test -x "$GIT_DIR/hooks/pre-commit" && + exec "$GIT_DIR/hooks/pre-commit" +:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/pre-push.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,53 @@ +#!/bin/sh + +# An example hook script to verify what is about to be pushed. Called by "git +# push" after it has checked the remote status, but before anything has been +# pushed. If this script exits with a non-zero status nothing will be pushed. +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# <local ref> <local sha1> <remote ref> <remote sha1> +# +# This sample shows how to prevent push of commits where the log message starts +# with "WIP" (work in progress). + +remote="$1" +url="$2" + +z40=0000000000000000000000000000000000000000 + +while read local_ref local_sha remote_ref remote_sha +do + if [ "$local_sha" = $z40 ] + then + # Handle delete + : + else + if [ "$remote_sha" = $z40 ] + then + # New branch, examine all commits + range="$local_sha" + else + # Update to existing branch, examine new commits + range="$remote_sha..$local_sha" + fi + + # Check for WIP commit + commit=`git rev-list -n 1 --grep '^WIP' "$range"` + if [ -n "$commit" ] + then + echo >&2 "Found WIP commit in $local_ref, not pushing" + exit 1 + fi + fi +done + +exit 0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/pre-rebase.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,169 @@ +#!/bin/sh +# +# Copyright (c) 2006, 2008 Junio C Hamano +# +# The "pre-rebase" hook is run just before "git rebase" starts doing +# its job, and can prevent the command from running by exiting with +# non-zero status. +# +# The hook is called with the following parameters: +# +# $1 -- the upstream the series was forked from. +# $2 -- the branch being rebased (or empty when rebasing the current branch). +# +# This sample shows how to prevent topic branches that are already +# merged to 'next' branch from getting rebased, because allowing it +# would result in rebasing already published history. + +publish=next +basebranch="$1" +if test "$#" = 2 +then + topic="refs/heads/$2" +else + topic=`git symbolic-ref HEAD` || + exit 0 ;# we do not interrupt rebasing detached HEAD +fi + +case "$topic" in +refs/heads/??/*) + ;; +*) + exit 0 ;# we do not interrupt others. + ;; +esac + +# Now we are dealing with a topic branch being rebased +# on top of master. Is it OK to rebase it? + +# Does the topic really exist? +git show-ref -q "$topic" || { + echo >&2 "No such branch $topic" + exit 1 +} + +# Is topic fully merged to master? +not_in_master=`git rev-list --pretty=oneline ^master "$topic"` +if test -z "$not_in_master" +then + echo >&2 "$topic is fully merged to master; better remove it." + exit 1 ;# we could allow it, but there is no point. +fi + +# Is topic ever merged to next? If so you should not be rebasing it. +only_next_1=`git rev-list ^master "^$topic" ${publish} | sort` +only_next_2=`git rev-list ^master ${publish} | sort` +if test "$only_next_1" = "$only_next_2" +then + not_in_topic=`git rev-list "^$topic" master` + if test -z "$not_in_topic" + then + echo >&2 "$topic is already up to date with master" + exit 1 ;# we could allow it, but there is no point. + else + exit 0 + fi +else + not_in_next=`git rev-list --pretty=oneline ^${publish} "$topic"` + /usr/bin/perl -e ' + my $topic = $ARGV[0]; + my $msg = "* $topic has commits already merged to public branch:\n"; + my (%not_in_next) = map { + /^([0-9a-f]+) /; + ($1 => 1); + } split(/\n/, $ARGV[1]); + for my $elem (map { + /^([0-9a-f]+) (.*)$/; + [$1 => $2]; + } split(/\n/, $ARGV[2])) { + if (!exists $not_in_next{$elem->[0]}) { + if ($msg) { + print STDERR $msg; + undef $msg; + } + print STDERR " $elem->[1]\n"; + } + } + ' "$topic" "$not_in_next" "$not_in_master" + exit 1 +fi + +<<\DOC_END + +This sample hook safeguards topic branches that have been +published from being rewound. + +The workflow assumed here is: + + * Once a topic branch forks from "master", "master" is never + merged into it again (either directly or indirectly). + + * Once a topic branch is fully cooked and merged into "master", + it is deleted. If you need to build on top of it to correct + earlier mistakes, a new topic branch is created by forking at + the tip of the "master". This is not strictly necessary, but + it makes it easier to keep your history simple. + + * Whenever you need to test or publish your changes to topic + branches, merge them into "next" branch. + +The script, being an example, hardcodes the publish branch name +to be "next", but it is trivial to make it configurable via +$GIT_DIR/config mechanism. + +With this workflow, you would want to know: + +(1) ... if a topic branch has ever been merged to "next". Young + topic branches can have stupid mistakes you would rather + clean up before publishing, and things that have not been + merged into other branches can be easily rebased without + affecting other people. But once it is published, you would + not want to rewind it. + +(2) ... if a topic branch has been fully merged to "master". + Then you can delete it. More importantly, you should not + build on top of it -- other people may already want to + change things related to the topic as patches against your + "master", so if you need further changes, it is better to + fork the topic (perhaps with the same name) afresh from the + tip of "master". + +Let's look at this example: + + o---o---o---o---o---o---o---o---o---o "next" + / / / / + / a---a---b A / / + / / / / + / / c---c---c---c B / + / / / \ / + / / / b---b C \ / + / / / / \ / + ---o---o---o---o---o---o---o---o---o---o---o "master" + + +A, B and C are topic branches. + + * A has one fix since it was merged up to "next". + + * B has finished. It has been fully merged up to "master" and "next", + and is ready to be deleted. + + * C has not merged to "next" at all. + +We would want to allow C to be rebased, refuse A, and encourage +B to be deleted. + +To compute (1): + + git rev-list ^master ^topic next + git rev-list ^master next + + if these match, topic has not merged in next at all. + +To compute (2): + + git rev-list master..topic + + if this is empty, it is fully merged to "master". + +DOC_END
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/pre-receive.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,24 @@ +#!/bin/sh +# +# An example hook script to make use of push options. +# The example simply echoes all push options that start with 'echoback=' +# and rejects all pushes when the "reject" push option is used. +# +# To enable this hook, rename this file to "pre-receive". + +if test -n "$GIT_PUSH_OPTION_COUNT" +then + i=0 + while test "$i" -lt "$GIT_PUSH_OPTION_COUNT" + do + eval "value=\$GIT_PUSH_OPTION_$i" + case "$value" in + echoback=*) + echo "echo from the pre-receive-hook: ${value#*=}" >&2 + ;; + reject) + exit 1 + esac + i=$((i + 1)) + done +fi
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/prepare-commit-msg.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,42 @@ +#!/bin/sh +# +# An example hook script to prepare the commit log message. +# Called by "git commit" with the name of the file that has the +# commit message, followed by the description of the commit +# message's source. The hook's purpose is to edit the commit +# message file. If the hook fails with a non-zero status, +# the commit is aborted. +# +# To enable this hook, rename this file to "prepare-commit-msg". + +# This hook includes three examples. The first one removes the +# "# Please enter the commit message..." help message. +# +# The second includes the output of "git diff --name-status -r" +# into the message, just before the "git status" output. It is +# commented because it doesn't cope with --amend or with squashed +# commits. +# +# The third example adds a Signed-off-by line to the message, that can +# still be edited. This is rarely a good idea. + +COMMIT_MSG_FILE=$1 +COMMIT_SOURCE=$2 +SHA1=$3 + +/usr/bin/perl -i.bak -ne 'print unless(m/^. Please enter the commit message/..m/^#$/)' "$COMMIT_MSG_FILE" + +# case "$COMMIT_SOURCE,$SHA1" in +# ,|template,) +# /usr/bin/perl -i.bak -pe ' +# print "\n" . `git diff --cached --name-status -r` +# if /^#/ && $first++ == 0' "$COMMIT_MSG_FILE" ;; +# *) ;; +# esac + +# SOB=$(git var GIT_COMMITTER_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p') +# git interpret-trailers --in-place --trailer "$SOB" "$COMMIT_MSG_FILE" +# if test -z "$COMMIT_SOURCE" +# then +# /usr/bin/perl -i.bak -pe 'print "\n" if !$first_line++' "$COMMIT_MSG_FILE" +# fi
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/hooks/update.sample Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,128 @@ +#!/bin/sh +# +# An example hook script to block unannotated tags from entering. +# Called by "git receive-pack" with arguments: refname sha1-old sha1-new +# +# To enable this hook, rename this file to "update". +# +# Config +# ------ +# hooks.allowunannotated +# This boolean sets whether unannotated tags will be allowed into the +# repository. By default they won't be. +# hooks.allowdeletetag +# This boolean sets whether deleting tags will be allowed in the +# repository. By default they won't be. +# hooks.allowmodifytag +# This boolean sets whether a tag may be modified after creation. By default +# it won't be. +# hooks.allowdeletebranch +# This boolean sets whether deleting branches will be allowed in the +# repository. By default they won't be. +# hooks.denycreatebranch +# This boolean sets whether remotely creating branches will be denied +# in the repository. By default this is allowed. +# + +# --- Command line +refname="$1" +oldrev="$2" +newrev="$3" + +# --- Safety check +if [ -z "$GIT_DIR" ]; then + echo "Don't run this script from the command line." >&2 + echo " (if you want, you could supply GIT_DIR then run" >&2 + echo " $0 <ref> <oldrev> <newrev>)" >&2 + exit 1 +fi + +if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then + echo "usage: $0 <ref> <oldrev> <newrev>" >&2 + exit 1 +fi + +# --- Config +allowunannotated=$(git config --type=bool hooks.allowunannotated) +allowdeletebranch=$(git config --type=bool hooks.allowdeletebranch) +denycreatebranch=$(git config --type=bool hooks.denycreatebranch) +allowdeletetag=$(git config --type=bool hooks.allowdeletetag) +allowmodifytag=$(git config --type=bool hooks.allowmodifytag) + +# check for no description +projectdesc=$(sed -e '1q' "$GIT_DIR/description") +case "$projectdesc" in +"Unnamed repository"* | "") + echo "*** Project description file hasn't been set" >&2 + exit 1 + ;; +esac + +# --- Check types +# if $newrev is 0000...0000, it's a commit to delete a ref. +zero="0000000000000000000000000000000000000000" +if [ "$newrev" = "$zero" ]; then + newrev_type=delete +else + newrev_type=$(git cat-file -t $newrev) +fi + +case "$refname","$newrev_type" in + refs/tags/*,commit) + # un-annotated tag + short_refname=${refname##refs/tags/} + if [ "$allowunannotated" != "true" ]; then + echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2 + echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 + exit 1 + fi + ;; + refs/tags/*,delete) + # delete tag + if [ "$allowdeletetag" != "true" ]; then + echo "*** Deleting a tag is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/tags/*,tag) + # annotated tag + if [ "$allowmodifytag" != "true" ] && git rev-parse $refname > /dev/null 2>&1 + then + echo "*** Tag '$refname' already exists." >&2 + echo "*** Modifying a tag is not allowed in this repository." >&2 + exit 1 + fi + ;; + refs/heads/*,commit) + # branch + if [ "$oldrev" = "$zero" -a "$denycreatebranch" = "true" ]; then + echo "*** Creating a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/heads/*,delete) + # delete branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + refs/remotes/*,commit) + # tracking branch + ;; + refs/remotes/*,delete) + # delete tracking branch + if [ "$allowdeletebranch" != "true" ]; then + echo "*** Deleting a tracking branch is not allowed in this repository" >&2 + exit 1 + fi + ;; + *) + # Anything else (is there anything else?) + echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 + exit 1 + ;; +esac + +# --- Finished +exit 0
Binary file noos_mbed/.git/index has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/info/exclude Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,27 @@ +.hg +.git +.svn +.CVS +.cvs +*.orig +BUILD +.build +.export +.msub +.meta +.ctags* +*.uvproj +*.uvopt +*.project +*.cproject +*.launch +*.ewp +*.eww +/Makefile +Debug +*.htm +.mbed +*.settings +mbed_settings.py +*.py[cod] +# subrepo ignores
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/logs/HEAD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,25 @@ +0000000000000000000000000000000000000000 d6b88804a7a728a71f3bf012ce90df2caebd1827 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608188764 +0530 clone: from https://bitbucket.analog.com/scm/pcts/noos_mbed.git +d6b88804a7a728a71f3bf012ce90df2caebd1827 d6b88804a7a728a71f3bf012ce90df2caebd1827 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608197041 +0530 checkout: moving from develop to HEAD +d6b88804a7a728a71f3bf012ce90df2caebd1827 d6b88804a7a728a71f3bf012ce90df2caebd1827 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608197044 +0530 checkout: moving from d6b88804a7a728a71f3bf012ce90df2caebd1827 to develop +d6b88804a7a728a71f3bf012ce90df2caebd1827 d6b88804a7a728a71f3bf012ce90df2caebd1827 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608269801 +0530 checkout: moving from develop to HEAD +d6b88804a7a728a71f3bf012ce90df2caebd1827 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608269807 +0530 checkout: moving from d6b88804a7a728a71f3bf012ce90df2caebd1827 to develop +3a0ab7463e5d770043d2bb15f50caaeb69f405e6 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608270682 +0530 checkout: moving from develop to HEAD +3a0ab7463e5d770043d2bb15f50caaeb69f405e6 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608270686 +0530 checkout: moving from 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 to develop +3a0ab7463e5d770043d2bb15f50caaeb69f405e6 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608276597 +0530 checkout: moving from develop to HEAD +3a0ab7463e5d770043d2bb15f50caaeb69f405e6 bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608276603 +0530 checkout: moving from 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 to develop +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608307981 +0530 checkout: moving from develop to HEAD +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608307986 +0530 checkout: moving from bb555b5984b42b60bceabc3bd6c5adcaee5df6cd to develop +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608313516 +0530 checkout: moving from develop to HEAD +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608313520 +0530 checkout: moving from bb555b5984b42b60bceabc3bd6c5adcaee5df6cd to develop +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608314986 +0530 checkout: moving from develop to HEAD +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608314989 +0530 checkout: moving from bb555b5984b42b60bceabc3bd6c5adcaee5df6cd to develop +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608316332 +0530 checkout: moving from develop to HEAD +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608316335 +0530 checkout: moving from bb555b5984b42b60bceabc3bd6c5adcaee5df6cd to develop +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608320736 +0530 checkout: moving from develop to HEAD +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608320739 +0530 checkout: moving from bb555b5984b42b60bceabc3bd6c5adcaee5df6cd to develop +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608636952 +0530 checkout: moving from develop to HEAD +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1608636955 +0530 checkout: moving from bb555b5984b42b60bceabc3bd6c5adcaee5df6cd to develop +f9f32e227d02859c98ce533d507eb48d372ee6fb f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1610531967 +0530 checkout: moving from develop to HEAD +f9f32e227d02859c98ce533d507eb48d372ee6fb f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1610531971 +0530 checkout: moving from f9f32e227d02859c98ce533d507eb48d372ee6fb to develop +f9f32e227d02859c98ce533d507eb48d372ee6fb f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1610532625 +0530 checkout: moving from develop to HEAD +f9f32e227d02859c98ce533d507eb48d372ee6fb f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1610532627 +0530 checkout: moving from f9f32e227d02859c98ce533d507eb48d372ee6fb to develop
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/logs/refs/heads/develop Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +0000000000000000000000000000000000000000 f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1610532627 +0530 branch: Created from refs/remotes/origin/develop
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/logs/refs/remotes/origin/HEAD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +0000000000000000000000000000000000000000 d6b88804a7a728a71f3bf012ce90df2caebd1827 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608188764 +0530 clone: from https://bitbucket.analog.com/scm/pcts/noos_mbed.git
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/logs/refs/remotes/origin/develop Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,3 @@ +d6b88804a7a728a71f3bf012ce90df2caebd1827 3a0ab7463e5d770043d2bb15f50caaeb69f405e6 Pratyush Mallick <Pratyush.Mallick@analog.com> 1608269806 +0530 fetch --append --no-auto-gc --no-write-commit-graph --force --tags -q origin: forced-update +3a0ab7463e5d770043d2bb15f50caaeb69f405e6 bb555b5984b42b60bceabc3bd6c5adcaee5df6cd Pratyush Mallick <Pratyush.Mallick@analog.com> 1608276602 +0530 fetch --append --no-auto-gc --no-write-commit-graph --force --tags -q origin: forced-update +bb555b5984b42b60bceabc3bd6c5adcaee5df6cd f9f32e227d02859c98ce533d507eb48d372ee6fb Pratyush Mallick <Pratyush.Mallick@analog.com> 1608636954 +0530 fetch --append --no-auto-gc --no-write-commit-graph --force --tags origin: fast-forward
Binary file noos_mbed/.git/objects/02/829415305ed6865a8357da6db7b0a6e086270a has changed
Binary file noos_mbed/.git/objects/16/3e6f77818e4440696128cfdf55f54217bfcf61 has changed
Binary file noos_mbed/.git/objects/20/2e3efd4a016ccd92edd6195dc4fc38550524e4 has changed
Binary file noos_mbed/.git/objects/3c/5b1334ad9ac2768380cf99dc4159f2b878e386 has changed
Binary file noos_mbed/.git/objects/5d/9b842d2eb68d0f29e88173eb836f2d072aac03 has changed
Binary file noos_mbed/.git/objects/63/d256ae51bd92ff4f5995da30af940d35babe18 has changed
Binary file noos_mbed/.git/objects/72/e85bcaab60dfc4da4ed6730c8c1b9c1eb1b5f2 has changed
Binary file noos_mbed/.git/objects/a9/2bc99b55bb770b986a08a1544e301db3cbfca2 has changed
Binary file noos_mbed/.git/objects/b7/a52920d7ebe45ce080ffe39e3e49a95b83cee0 has changed
Binary file noos_mbed/.git/objects/c3/468457cc9578fc811043c209ba298d9032ce27 has changed
Binary file noos_mbed/.git/objects/d6/89c5951e39eff5b2b867419f71ae0c88e8570e has changed
Binary file noos_mbed/.git/objects/da/fae2f35db5c8e9809accb81359502ad4ac48d1 has changed
Binary file noos_mbed/.git/objects/f9/f32e227d02859c98ce533d507eb48d372ee6fb has changed
Binary file noos_mbed/.git/objects/fc/16134d95ffe2b824f84be51a9398f4c375d22c has changed
Binary file noos_mbed/.git/objects/pack/pack-42929b08c1ea303dc192ad9cb9068b1dcb4e6366.idx has changed
Binary file noos_mbed/.git/objects/pack/pack-42929b08c1ea303dc192ad9cb9068b1dcb4e6366.pack has changed
Binary file noos_mbed/.git/objects/pack/pack-4f91408097e86ea3b7a234eb223ca8cb658655fc.idx has changed
Binary file noos_mbed/.git/objects/pack/pack-4f91408097e86ea3b7a234eb223ca8cb658655fc.pack has changed
Binary file noos_mbed/.git/objects/pack/pack-c2a761531b6f96462bfdfb7184263a45de05f91d.idx has changed
Binary file noos_mbed/.git/objects/pack/pack-c2a761531b6f96462bfdfb7184263a45de05f91d.pack has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/packed-refs Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,3 @@ +# pack-refs with: peeled fully-peeled sorted +d6b88804a7a728a71f3bf012ce90df2caebd1827 refs/remotes/origin/develop +4845c2586327565faa9de97a5f5ef6f05c758339 refs/remotes/origin/noos_gh_master
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/refs/heads/develop Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +f9f32e227d02859c98ce533d507eb48d372ee6fb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/refs/remotes/origin/HEAD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +ref: refs/remotes/origin/develop
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/refs/remotes/origin/develop Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +f9f32e227d02859c98ce533d507eb48d372ee6fb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/.git/refs/tags/Initial_Version Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1 @@ +72e85bcaab60dfc4da4ed6730c8c1b9c1eb1b5f2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/README.md Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,23 @@ + +# no-OS ( Mbed Version ) + +[Analog Devices Inc.](http://www.analog.com/en/index.html) Mbed Version of Software drivers for systems with mbed OS + + +## Software + +The goal of these noos-mbed drivers is to be able to provide reference drivers for precision converters ADCs and DACs compatible with mbed supported boards, as mbed platform enables us work with diverse protofio of microcontroller while minimizing the hadware dependency. +The mbed version of the no-OS is quite similar to the original version with inclusion of platform drivers changes to resolve naming conflicts with Mbed-OS files. + +## Documentation + +For more information about projects develop using noos-mbed drivers, please visit our [wiki](https://wiki.analog.com/resources/tools-software/product-support-software) page. + +## Which branch should I use? + + * If you want to use the latest releases, check out the master branch + + * If you want to use the drivers under development, check out the develop branch. + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc-dac/ad5592r/ad5592r-base.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,328 @@ +/***************************************************************************//** + * @file ad5592r-base.c + * @brief Implementation of AD5592R Base Driver. + * @author Mircea Caprioru (mircea.caprioru@analog.com) +******************************************************************************** + * Copyright 2018, 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#include "error.h" +#include "ad5592r-base.h" + +/** + * Write register. + * + * @param dev - The device structure. + * @param reg - The register address. + * @param value - register value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_base_reg_write(struct ad5592r_dev *dev, uint8_t reg, + uint16_t value) +{ + return dev->ops->reg_write(dev, reg, value); +} + +/** + * Read register. + * + * @param dev - The device structure. + * @param reg - The register address. + * @param value - register value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_base_reg_read(struct ad5592r_dev *dev, uint8_t reg, + uint16_t *value) +{ + return dev->ops->reg_read(dev, reg, value); +} + +/** + * Get GPIO value + * + * @param dev - The device structure. + * @param offset - The channel number. + * @return 0 or 1 depending on the GPIO value. + */ +int32_t ad5592r_gpio_get(struct ad5592r_dev *dev, uint8_t offset) +{ + int32_t ret = 0; + uint8_t val; + + if (!dev) + return FAILURE; + + if (dev->gpio_out & BIT(offset)) + val = dev->gpio_val; + else + ret = dev->ops->gpio_read(dev, &val); + + if (ret < 0) + return ret; + + return !!(val & BIT(offset)); +} + +/** + * Set GPIO value + * + * @param dev - The device structure. + * @param offset - The channel number. + * @param value - the GPIO value (0 or 1) + */ +int32_t ad5592r_gpio_set(struct ad5592r_dev *dev, uint8_t offset, int32_t value) +{ + if (!dev) + return FAILURE; + + if (value) + dev->gpio_val |= BIT(offset); + else + dev->gpio_val &= ~BIT(offset); + + return ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_SET, + dev->gpio_val); +} + +/** + * Set GPIO as input + * + * @param dev - The device structure. + * @param offset - The channel number. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_gpio_direction_input(struct ad5592r_dev *dev, uint8_t offset) +{ + int32_t ret; + + if (!dev) + return FAILURE; + + dev->gpio_out &= ~BIT(offset); + dev->gpio_in |= BIT(offset); + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_OUT_EN, + dev->gpio_out); + if (ret < 0) + return ret; + + return ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_IN_EN, + dev->gpio_in); +} + +/** + * Set GPIO as output + * + * @param dev - The device structure. + * @param offset - The channel number. + * @param value - GPIO value to set. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_gpio_direction_output(struct ad5592r_dev *dev, + uint8_t offset, int32_t value) +{ + int32_t ret; + + if (!dev) + return FAILURE; + + if (value) + dev->gpio_val |= BIT(offset); + else + dev->gpio_val &= ~BIT(offset); + + dev->gpio_in &= ~BIT(offset); + dev->gpio_out |= BIT(offset); + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_SET, dev->gpio_val); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_OUT_EN, + dev->gpio_out); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_IN_EN, + dev->gpio_in); + + return ret; +} + +/** + * Software reset device. + * + * @param dev - The device structure. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_software_reset(struct ad5592r_dev *dev) +{ + int32_t ret; + + if (!dev) + return FAILURE; + + /* Writing this magic value resets the device */ + ret = ad5592r_base_reg_write(dev, AD5592R_REG_RESET, 0xdac); + + mdelay(10); + + return ret; +} + +/** + * Set channels modes. + * + * @param dev - The device structure. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_set_channel_modes(struct ad5592r_dev *dev) +{ + int32_t ret; + uint8_t i; + uint8_t pulldown = 0, tristate = 0, dac = 0, adc = 0; + uint16_t read_back; + + if (!dev) + return FAILURE; + + dev->gpio_in = 0; + dev->gpio_out = 0; + + for (i = 0; i < dev->num_channels; i++) { + switch (dev->channel_modes[i]) { + case CH_MODE_DAC: + dac |= BIT(i); + break; + + case CH_MODE_ADC: + adc |= BIT(i); + break; + + case CH_MODE_DAC_AND_ADC: + dac |= BIT(i); + adc |= BIT(i); + break; + + case CH_MODE_GPI: + dev->gpio_in |= BIT(i); + break; + + case CH_MODE_GPO: + dev->gpio_out |= BIT(i); + break; + + case CH_MODE_UNUSED: + /* fall-through */ + default: + switch (dev->channel_offstate[i]) { + case CH_OFFSTATE_OUT_TRISTATE: + tristate |= BIT(i); + break; + + case CH_OFFSTATE_OUT_LOW: + dev->gpio_out |= BIT(i); + break; + + case CH_OFFSTATE_OUT_HIGH: + dev->gpio_out |= BIT(i); + dev->gpio_val |= BIT(i); + break; + + case CH_OFFSTATE_PULLDOWN: + /* fall-through */ + default: + pulldown |= BIT(i); + break; + } + } + } + + /* Pull down unused pins to GND */ + ret = ad5592r_base_reg_write(dev, AD5592R_REG_PULLDOWN, pulldown); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_TRISTATE, tristate); + if (ret < 0) + return ret; + + /* Configure pins that we use */ + ret = ad5592r_base_reg_write(dev, AD5592R_REG_DAC_EN, dac); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_ADC_EN, adc); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_SET, dev->gpio_val); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_OUT_EN, + dev->gpio_out); + if (ret < 0) + return ret; + + ret = ad5592r_base_reg_write(dev, AD5592R_REG_GPIO_IN_EN, + dev->gpio_in); + if (ret < 0) + return ret; + + /* Verify that we can read back at least one register */ + ret = ad5592r_base_reg_read(dev, AD5592R_REG_ADC_EN, &read_back); + if (!ret && (read_back & 0xff) != adc) + return FAILURE; + + return ret; +} + +/** + * Reset channels and set GPIO to unused. + * + * @param dev - The device structure. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_reset_channel_modes(struct ad5592r_dev *dev) +{ + uint32_t i; + + if (!dev) + return FAILURE; + + for (i = 0; i < sizeof(dev->channel_modes); i++) + dev->channel_modes[i] = CH_MODE_UNUSED; + + return ad5592r_set_channel_modes(dev); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc-dac/ad5592r/ad5592r-base.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,149 @@ +/***************************************************************************//** + * @file ad5592r-base.h + * @brief Header file of AD5592R Base Driver. + * @author Mircea Caprioru (mircea.caprioru@analog.com) +******************************************************************************** + * Copyright 2018, 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef AD5592R_BASE_H_ +#define AD5592R_BASE_H_ + +#include "stdint.h" +#include "platform_drivers.h" + +#include <stdbool.h> + +#define CH_MODE_UNUSED 0 +#define CH_MODE_ADC 1 +#define CH_MODE_DAC 2 +#define CH_MODE_DAC_AND_ADC 3 +#define CH_MODE_GPI 4 +#define CH_MODE_GPO 5 + +#define CH_OFFSTATE_PULLDOWN 0 +#define CH_OFFSTATE_OUT_LOW 1 +#define CH_OFFSTATE_OUT_HIGH 2 +#define CH_OFFSTATE_OUT_TRISTATE 3 + +enum ad5592r_registers { + AD5592R_REG_NOOP = 0x0, + AD5592R_REG_DAC_READBACK = 0x1, + AD5592R_REG_ADC_SEQ = 0x2, + AD5592R_REG_CTRL = 0x3, + AD5592R_REG_ADC_EN = 0x4, + AD5592R_REG_DAC_EN = 0x5, + AD5592R_REG_PULLDOWN = 0x6, + AD5592R_REG_LDAC = 0x7, + AD5592R_REG_GPIO_OUT_EN = 0x8, + AD5592R_REG_GPIO_SET = 0x9, + AD5592R_REG_GPIO_IN_EN = 0xA, + AD5592R_REG_PD = 0xB, + AD5592R_REG_OPEN_DRAIN = 0xC, + AD5592R_REG_TRISTATE = 0xD, + AD5592R_REG_RESET = 0xF, +}; + +#define AD5592R_REG_PD_PD_ALL BIT(10) +#define AD5592R_REG_PD_EN_REF BIT(9) + +#define AD5592R_REG_CTRL_ADC_PC_BUFF BIT(9) +#define AD5592R_REG_CTRL_ADC_BUFF_EN BIT(8) +#define AD5592R_REG_CTRL_CONFIG_LOCK BIT(7) +#define AD5592R_REG_CTRL_W_ALL_DACS BIT(6) +#define AD5592R_REG_CTRL_ADC_RANGE BIT(5) +#define AD5592R_REG_CTRL_DAC_RANGE BIT(4) + +#define AD5592R_REG_ADC_SEQ_REP BIT(9) +#define AD5592R_REG_ADC_SEQ_TEMP_READBACK BIT(8) +#define AD5592R_REG_ADC_SEQ_CODE_MSK(x) ((x) & 0x0FFF) + +#define AD5592R_REG_GPIO_OUT_EN_ADC_NOT_BUSY BIT(8) + +#define AD5592R_REG_LDAC_IMMEDIATE_OUT 0x00 +#define AD5592R_REG_LDAC_INPUT_REG_ONLY 0x01 +#define AD5592R_REG_LDAC_INPUT_REG_OUT 0x02 + +#define INTERNAL_VREF_VOLTAGE 2.5 + +struct ad5592r_dev; + +struct ad5592r_rw_ops { + int32_t (*write_dac)(struct ad5592r_dev *dev, uint8_t chan, + uint16_t value); + int32_t (*read_adc)(struct ad5592r_dev *dev, uint8_t chan, + uint16_t *value); + int32_t(*multi_read_adc)(struct ad5592r_dev *dev, + uint16_t chans, uint16_t *value); + int32_t (*reg_write)(struct ad5592r_dev *dev, uint8_t reg, + uint16_t value); + int32_t (*reg_read)(struct ad5592r_dev *dev, uint8_t reg, + uint16_t *value); + int32_t (*gpio_read)(struct ad5592r_dev *dev, uint8_t *value); +}; + +struct ad5592r_init_param { + bool int_ref; +}; + +struct ad5592r_dev { + const struct ad5592r_rw_ops *ops; + i2c_desc *i2c; + spi_desc *spi; + uint16_t spi_msg; + uint8_t num_channels; + uint16_t cached_dac[8]; + uint16_t cached_gp_ctrl; + uint8_t channel_modes[8]; + uint8_t channel_offstate[8]; + uint8_t gpio_out; + uint8_t gpio_in; + uint8_t gpio_val; + uint8_t ldac_mode; +}; + +int32_t ad5592r_base_reg_write(struct ad5592r_dev *dev, uint8_t reg, + uint16_t value); +int32_t ad5592r_base_reg_read(struct ad5592r_dev *dev, uint8_t reg, + uint16_t *value); +int32_t ad5592r_gpio_get(struct ad5592r_dev *dev, uint8_t offset); +int32_t ad5592r_gpio_set(struct ad5592r_dev *dev, uint8_t offset, + int32_t value); +int32_t ad5592r_gpio_direction_input(struct ad5592r_dev *dev, uint8_t offset); +int32_t ad5592r_gpio_direction_output(struct ad5592r_dev *dev, + uint8_t offset, int32_t value); +int32_t ad5592r_software_reset(struct ad5592r_dev *dev); +int32_t ad5592r_set_channel_modes(struct ad5592r_dev *dev); +int32_t ad5592r_reset_channel_modes(struct ad5592r_dev *dev); + +#endif /* AD5592R_BASE_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc-dac/ad5592r/ad5592r.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,295 @@ +/***************************************************************************//** + * @file ad5592r.c + * @brief Implementation of AD5592R driver. + * @author Mircea Caprioru (mircea.caprioru@analog.com) +******************************************************************************** + * Copyright 2018, 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#include "error.h" +#include "ad5592r-base.h" +#include "ad5592r.h" + +const struct ad5592r_rw_ops ad5592r_rw_ops = { + .write_dac = ad5592r_write_dac, + .read_adc = ad5592r_read_adc, + .multi_read_adc = ad5592r_multi_read_adc, + .reg_write = ad5592r_reg_write, + .reg_read = ad5592r_reg_read, + .gpio_read = ad5592r_gpio_read, +}; + +/** + * Write NOP and read value. + * + * @param dev - The device structure. + * @param buf - buffer where to read + * @return 0 in case of success, negative error code otherwise + */ +static int32_t ad5592r_spi_wnop_r16(struct ad5592r_dev *dev, uint16_t *buf) +{ + int32_t ret; + uint16_t spi_msg_nop = 0; /* NOP */ + + ret = spi_write_and_read(dev->spi, (uint8_t *)&spi_msg_nop, + sizeof(spi_msg_nop)); + if (ret < 0) + return ret; + + *buf = swab16(spi_msg_nop); + + return ret; +} + +/** + * Write DAC channel. + * + * @param dev - The device structure. + * @param chan - The channel number. + * @param value - DAC value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_write_dac(struct ad5592r_dev *dev, uint8_t chan, + uint16_t value) +{ + if (!dev) + return FAILURE; + + dev->spi_msg = swab16( BIT(15) | (uint16_t)(chan << 12) | value); + + return spi_write_and_read(dev->spi, (uint8_t *)&dev->spi_msg, + sizeof(dev->spi_msg)); +} + +/** + * Read ADC channel. + * + * @param dev - The device structure. + * @param chan - The channel number. + * @param value - ADC value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_read_adc(struct ad5592r_dev *dev, uint8_t chan, + uint16_t *value) +{ + int32_t ret; + + if (!dev) + return FAILURE; + + dev->spi_msg = swab16((uint16_t)(AD5592R_REG_ADC_SEQ << 11) | + BIT(chan)); + + ret = spi_write_and_read(dev->spi, (uint8_t *)&dev->spi_msg, + sizeof(dev->spi_msg)); + if (ret < 0) + return ret; + + /* + * Invalid data: + * See Figure 40. Single-Channel ADC Conversion Sequence + */ + ret = ad5592r_spi_wnop_r16(dev, &dev->spi_msg); + if (ret < 0) + return ret; + + ret = ad5592r_spi_wnop_r16(dev, &dev->spi_msg); + if (ret < 0) + return ret; + + *value = dev->spi_msg; + + return 0; +} + +/** + * Read Multiple ADC Channels. + * + * @param dev - The device structure. + * @param chans - The ADC channels to be readback + * @param values - ADC value array + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_multi_read_adc(struct ad5592r_dev *dev, uint16_t chans, + uint16_t *values) +{ + int32_t ret; + uint8_t samples; + uint8_t i; + + if (!dev) + return FAILURE; + + samples = hweight8(chans); + + dev->spi_msg = swab16((uint16_t)(AD5592R_REG_ADC_SEQ << 11) | chans); + + ret = spi_write_and_read(dev->spi, (uint8_t *)&dev->spi_msg, + sizeof(dev->spi_msg)); + if (ret < 0) + return ret; + + /* + * Invalid data: + * See Figure 40. Single-Channel ADC Conversion Sequence + */ + ret = ad5592r_spi_wnop_r16(dev, &dev->spi_msg); + if (ret < 0) + return ret; + + for (i = 0; i < samples; i++) { + ret = ad5592r_spi_wnop_r16(dev, &dev->spi_msg); + if (ret < 0) + return ret; + values[i] = dev->spi_msg; + } + + return 0; +} + +/** + * Write register. + * + * @param dev - The device structure. + * @param reg - The register address. + * @param value - register value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_reg_write(struct ad5592r_dev *dev, uint8_t reg, uint16_t value) +{ + if (!dev) + return FAILURE; + + dev->spi_msg = swab16((reg << 11) | value); + + return spi_write_and_read(dev->spi, (uint8_t *)&dev->spi_msg, + sizeof(dev->spi_msg)); +} + +/** + * Read register. + * + * @param dev - The device structure. + * @param reg - The register address. + * @param value - register value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_reg_read(struct ad5592r_dev *dev, uint8_t reg, uint16_t *value) +{ + int32_t ret; + + if (!dev) + return FAILURE; + + dev->spi_msg = swab16((AD5592R_REG_LDAC << 11) | + AD5592R_LDAC_READBACK_EN | (reg << 2) | dev->ldac_mode); + + ret = spi_write_and_read(dev->spi, (uint8_t *)&dev->spi_msg, + sizeof(dev->spi_msg)); + if (ret < 0) + return ret; + + ret = ad5592r_spi_wnop_r16(dev, &dev->spi_msg); + if (ret < 0) + return ret; + + *value = dev->spi_msg; + + return 0; +} + +/** + * Read GPIOs. + * + * @param dev - The device structure. + * @param value - GPIOs value. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_gpio_read(struct ad5592r_dev *dev, uint8_t *value) +{ + int32_t ret; + + if (!dev) + return FAILURE; + + ret = ad5592r_reg_write(dev, AD5592R_REG_GPIO_IN_EN, + AD5592R_GPIO_READBACK_EN | dev->gpio_in); + if (ret < 0) + return ret; + + ret = ad5592r_spi_wnop_r16(dev, &dev->spi_msg); + if (ret < 0) + return ret; + + *value = (uint8_t)dev->spi_msg; + + return 0; +} + +/** + * Initialize AD5593r device. + * + * @param dev - The device structure. + * @param init_param - The initial parameters of the device. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5592r_init(struct ad5592r_dev *dev, + struct ad5592r_init_param *init_param) +{ + int32_t ret; + uint16_t temp_reg_val; + + if (!dev) + return FAILURE; + + dev->ops = &ad5592r_rw_ops; + + ret = ad5592r_software_reset(dev); + if (ret < 0) + return ret; + + ret = ad5592r_set_channel_modes(dev); + if (ret < 0) + return ret; + + if(init_param->int_ref) { + ret = ad5592r_reg_read(dev, AD5592R_REG_PD, &temp_reg_val); + if (ret < 0) + return ret; + temp_reg_val |= AD5592R_REG_PD_EN_REF; + + return ad5592r_reg_write(dev, AD5592R_REG_PD, temp_reg_val); + } + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc-dac/ad5592r/ad5592r.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,66 @@ +/***************************************************************************//** + * @file ad5592r.h + * @brief Header file of AD5592R driver. + * @author Mircea Caprioru (mircea.caprioru@analog.com) +******************************************************************************** + * Copyright 2018, 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef AD5592R_H_ +#define AD5592R_H_ + +#include "stdint.h" +#include "ad5592r-base.h" + +#define AD5592R_GPIO_READBACK_EN BIT(10) +#define AD5592R_LDAC_READBACK_EN BIT(6) + +#define swab16(x) \ + ((((x) & 0x00ff) << 8) | \ + (((x) & 0xff00) >> 8)) + +int32_t ad5592r_write_dac(struct ad5592r_dev *dev, uint8_t chan, + uint16_t value); +int32_t ad5592r_read_adc(struct ad5592r_dev *dev, uint8_t chan, + uint16_t *value); +int32_t ad5592r_multi_read_adc(struct ad5592r_dev *dev, + uint16_t chans, uint16_t *value); +int32_t ad5592r_reg_write(struct ad5592r_dev *dev, uint8_t reg, + uint16_t value); +int32_t ad5592r_reg_read(struct ad5592r_dev *dev, uint8_t reg, + uint16_t *value); +int32_t ad5592r_gpio_read(struct ad5592r_dev *dev, uint8_t *value); +int32_t ad5592r_init(struct ad5592r_dev *dev, + struct ad5592r_init_param *init_param); + +#endif /* AD5592R_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc-dac/ad5592r/ad5593r.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,300 @@ +/***************************************************************************//** + * @file ad5593r.c + * @brief Implementation of AD5593R driver. + * @author Mircea Caprioru (mircea.caprioru@analog.com) +******************************************************************************** + * Copyright 2018, 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#include "ad5592r-base.h" +#include "error.h" +#include "ad5593r.h" + +#define AD5593R_MODE_CONF (0 << 4) +#define AD5593R_MODE_DAC_WRITE (1 << 4) +#define AD5593R_MODE_ADC_READBACK (4 << 4) +#define AD5593R_MODE_DAC_READBACK (5 << 4) +#define AD5593R_MODE_GPIO_READBACK (6 << 4) +#define AD5593R_MODE_REG_READBACK (7 << 4) + +#define STOP_BIT 1 +#define RESTART_BIT 0 +#define AD5593R_ADC_VALUES_BUFF_SIZE 18 + +const struct ad5592r_rw_ops ad5593r_rw_ops = { + .write_dac = ad5593r_write_dac, + .read_adc = ad5593r_read_adc, + .multi_read_adc= ad5593r_multi_read_adc, + .reg_write = ad5593r_reg_write, + .reg_read = ad5593r_reg_read, + .gpio_read = ad5593r_gpio_read, +}; + +/** + * Write DAC channel. + * + * @param dev - The device structure. + * @param chan - The channel number. + * @param value - DAC value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_write_dac(struct ad5592r_dev *dev, uint8_t chan, + uint16_t value) +{ + uint8_t data[3]; + + if (!dev) + return FAILURE; + + data[0] = AD5593R_MODE_DAC_WRITE | chan; + data[1] = (value >> 8) & 0xF ; + data[2] = value & 0xFF; + + return i2c_write(dev->i2c, data, sizeof(data), STOP_BIT); +} + +/** + * Read ADC channel. + * + * @param dev - The device structure. + * @param chan - The channel number. + * @param value - ADC value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_read_adc(struct ad5592r_dev *dev, uint8_t chan, + uint16_t *value) +{ + int32_t ret; + uint8_t data[3]; + uint16_t temp; + + if (!dev) + return FAILURE; + + temp = BIT(chan); + + data[0] = AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ; + data[1] = temp >> 8; + data[2] = temp & 0xFF; + + ret = i2c_write(dev->i2c, data, sizeof(data), STOP_BIT); + if (ret < 0) + return ret; + + data[0] = AD5593R_MODE_ADC_READBACK; + ret = i2c_write(dev->i2c, data, 1, STOP_BIT); + if (ret < 0) + return ret; + + ret = i2c_read(dev->i2c, data, 2, STOP_BIT); + if (ret < 0) + return ret; + + *value = (uint16_t)((data[0] & 0x0F) << 8) + data[1]; + + return 0; +} + +/** + * Read Multiple ADC Channels. + * + * @param dev - The device structure. + * @param chans - The ADC channels to be readback + * @param values - ADC value array + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_multi_read_adc(struct ad5592r_dev *dev, uint16_t chans, + uint16_t *values) +{ + uint8_t data[AD5593R_ADC_VALUES_BUFF_SIZE], i; + int32_t ret; + uint8_t samples; + + if (!dev) + return FAILURE; + + samples = hweight8(chans); + + data[0] = AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ; + data[1] = chans >> 8; + data[2] = chans & 0xFF; + + ret = i2c_write(dev->i2c, data, 3, STOP_BIT); + if (ret < 0) + return ret; + + data[0] = AD5593R_MODE_ADC_READBACK; + ret = i2c_write(dev->i2c, data, 1, RESTART_BIT); + if (ret < 0) + return ret; + + ret = i2c_read(dev->i2c, data, (2 * samples), STOP_BIT); + if (ret < 0) + return ret; + + for (i = 0; i < samples; i++) { + values[i] = ((uint16_t)(((data[2 * i] & 0xFF) << 8) + data[(2 * i) + 1])); + } + + return 0; +} + +/** + * Write register. + * + * @param dev - The device structure. + * @param reg - The register address. + * @param value - register value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_reg_write(struct ad5592r_dev *dev, uint8_t reg, + uint16_t value) +{ + int32_t ret; + uint8_t data[3]; + + if (!dev) + return FAILURE; + + data[0] = AD5593R_MODE_CONF | reg; + data[1] = value >> 8; + data[2] = value; + + ret = i2c_write(dev->i2c, data,sizeof(data), STOP_BIT); + + if (reg == AD5592R_REG_RESET && ret < 0) { + return SUCCESS; + } + + return ret; +} + +/** + * Read register. + * + * @param dev - The device structure. + * @param reg - The register address. + * @param value - register value + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_reg_read(struct ad5592r_dev *dev, uint8_t reg, + uint16_t *value) +{ + int32_t ret; + uint8_t data[2]; + + if (!dev) + return FAILURE; + + data[0] = AD5593R_MODE_REG_READBACK | reg; + + ret = i2c_write(dev->i2c, data, 1, STOP_BIT); + if (ret < 0) + return ret; + + ret = i2c_read(dev->i2c, data, sizeof(data), STOP_BIT); + if (ret < 0) + return ret; + + *value = (uint16_t) (data[0] << 8) + data[1]; + + return 0; +} + +/** + * Read GPIOs. + * + * @param dev - The device structure. + * @param value - GPIOs value. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_gpio_read(struct ad5592r_dev *dev, uint8_t *value) +{ + int32_t ret; + uint8_t data[2]; + + if (!dev) + return FAILURE; + + data[0] = AD5593R_MODE_GPIO_READBACK; + ret = i2c_write(dev->i2c, data, 1, STOP_BIT); + if (ret < 0) + return ret; + + ret = i2c_read(dev->i2c, data, sizeof(data), STOP_BIT); + if (ret < 0) + return ret; + + *value = data[1]; + + return 0; +} + +/** + * Initialize AD5593r device. + * + * @param dev - The device structure. + * @param init_param - The initial parameters of the device. + * @return 0 in case of success, negative error code otherwise + */ +int32_t ad5593r_init(struct ad5592r_dev *dev, + struct ad5592r_init_param *init_param) +{ + int32_t ret; + uint16_t temp_reg_val; + + if (!dev) + return FAILURE; + + dev->ops = &ad5593r_rw_ops; + + ret = ad5592r_software_reset(dev); + if (ret < 0) + return ret; + + ret = ad5592r_set_channel_modes(dev); + if (ret < 0) + return ret; + + if(init_param->int_ref) { + ret = ad5593r_reg_read(dev, AD5592R_REG_PD, &temp_reg_val); + if (ret < 0) + return ret; + temp_reg_val |= AD5592R_REG_PD_EN_REF; + + return ad5593r_reg_write(dev, AD5592R_REG_PD, temp_reg_val); + } + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc-dac/ad5592r/ad5593r.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,59 @@ +/***************************************************************************//** + * @file ad5593r.h + * @brief Header file of AD5593R driver. + * @author Mircea Caprioru (mircea.caprioru@analog.com) +******************************************************************************** + * Copyright 2018, 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef AD5593R_H_ +#define AD5593R_H_ + +#include "stdint.h" +#include "ad5592r-base.h" + +int32_t ad5593r_write_dac(struct ad5592r_dev *dev, uint8_t chan, + uint16_t value); +int32_t ad5593r_read_adc(struct ad5592r_dev *dev, uint8_t chan, + uint16_t *value); +int32_t ad5593r_multi_read_adc(struct ad5592r_dev *dev, + uint16_t chans, uint16_t *value); +int32_t ad5593r_reg_write(struct ad5592r_dev *dev, uint8_t reg, + uint16_t value); +int32_t ad5593r_reg_read(struct ad5592r_dev *dev, uint8_t reg, + uint16_t *value); +int32_t ad5593r_gpio_read(struct ad5592r_dev *dev, uint8_t *value); +int32_t ad5593r_init(struct ad5592r_dev *dev, + struct ad5592r_init_param *init_param); + +#endif /* AD5593R_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad7124/ad7124.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,555 @@ +/***************************************************************************//** +* @file ad7124.c +* @brief AD7124 implementation file. +* @devices AD7124-4, AD7124-8 +* +******************************************************************************** +* Copyright 2015-2019(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdlib.h> +#include <stdbool.h> +#include "ad7124.h" + +/* Error codes */ +#define INVALID_VAL -1 /* Invalid argument */ +#define COMM_ERR -2 /* Communication error on receive */ +#define TIMEOUT -3 /* A timeout has occured */ + +/* + * Post reset delay required to ensure all internal config done + * A time of 2ms should be enough based on the data sheet, but 4ms + * chosen to provide enough margin, in case mdelay is not accurate. + */ +#define AD7124_POST_RESET_DELAY 4 + + +/***************************************************************************//** + * @brief Reads the value of the specified register without checking if the + * device is ready to accept user requests. + * + * @param dev - The handler of the instance of the driver. + * @param p_reg - Pointer to the register structure holding info about the + * register to be read. The read value is stored inside the + * register structure. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_no_check_read_register(struct ad7124_dev *dev, + struct ad7124_st_reg* p_reg) +{ + int32_t ret = 0; + uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint8_t i = 0; + uint8_t check8 = 0, add_status_length = 0; + uint8_t msg_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + + if(!dev || !p_reg) + return INVALID_VAL; + + /* Build the Command word */ + buffer[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | + AD7124_COMM_REG_RA(p_reg->addr); + + /* + * If this is an AD7124_DATA register read, and the DATA_STATUS bit is set + * in ADC_CONTROL, need to read 4, not 3 bytes for DATA with STATUS + */ + if ((p_reg->addr == AD7124_DATA_REG) && + (dev->regs[AD7124_ADC_Control].value & AD7124_ADC_CTRL_REG_DATA_STATUS)) { + add_status_length = 1; + } + + /* Read data from the device */ + ret = spi_write_and_read(dev->spi_desc, + buffer, + ((dev->use_crc != AD7124_DISABLE_CRC) ? p_reg->size + 1 + : p_reg->size) + 1 + add_status_length); + if(ret < 0) + return ret; + + /* Check the CRC */ + if(dev->use_crc == AD7124_USE_CRC) { + msg_buf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | + AD7124_COMM_REG_RA(p_reg->addr); + for(i = 1; i < p_reg->size + 2 + add_status_length; ++i) { + msg_buf[i] = buffer[i]; + } + check8 = ad7124_compute_crc8(msg_buf, p_reg->size + 2 + add_status_length); + } + + if(check8 != 0) { + /* ReadRegister checksum failed. */ + return COMM_ERR; + } + + /* + * if reading Data with 4 bytes, need to copy the status byte to the STATUS + * register struct value member + */ + if (add_status_length) { + dev->regs[AD7124_Status].value = buffer[p_reg->size + 1]; + } + + /* Build the result */ + p_reg->value = 0; + for(i = 1; i < p_reg->size + 1; i++) { + p_reg->value <<= 8; + p_reg->value += buffer[i]; + } + + return ret; +} + +/***************************************************************************//** + * @brief Writes the value of the specified register without checking if the + * device is ready to accept user requests. + * + * @param dev - The handler of the instance of the driver. + * @param reg - Register structure holding info about the register to be written + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_no_check_write_register(struct ad7124_dev *dev, + struct ad7124_st_reg reg) +{ + int32_t ret = 0; + int32_t reg_value = 0; + uint8_t wr_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint8_t i = 0; + uint8_t crc8 = 0; + + if(!dev) + return INVALID_VAL; + + /* Build the Command word */ + wr_buf[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_WR | + AD7124_COMM_REG_RA(reg.addr); + + /* Fill the write buffer */ + reg_value = reg.value; + for(i = 0; i < reg.size; i++) { + wr_buf[reg.size - i] = reg_value & 0xFF; + reg_value >>= 8; + } + + /* Compute the CRC */ + if(dev->use_crc != AD7124_DISABLE_CRC) { + crc8 = ad7124_compute_crc8(wr_buf, reg.size + 1); + wr_buf[reg.size + 1] = crc8; + } + + /* Write data to the device */ + ret = spi_write_and_read(dev->spi_desc, + wr_buf, + (dev->use_crc != AD7124_DISABLE_CRC) ? reg.size + 2 + : reg.size + 1); + + return ret; +} + +/***************************************************************************//** + * @brief Reads the value of the specified register only when the device is ready + * to accept user requests. If the device ready flag is deactivated the + * read operation will be executed without checking the device state. + * + * @param dev - The handler of the instance of the driver. + * @param p_reg - Pointer to the register structure holding info about the + * register to be read. The read value is stored inside the + * register structure. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_read_register(struct ad7124_dev *dev, + struct ad7124_st_reg* p_reg) +{ + int32_t ret; + + if (p_reg->addr != AD7124_ERR_REG && dev->check_ready) { + ret = ad7124_wait_for_spi_ready(dev, + dev->spi_rdy_poll_cnt); + if (ret < 0) + return ret; + } + ret = ad7124_no_check_read_register(dev, + p_reg); + + return ret; +} + +/***************************************************************************//** + * @brief Writes the value of the specified register only when the device is + * ready to accept user requests. If the device ready flag is deactivated + * the write operation will be executed without checking the device state. + * + * @param dev - The handler of the instance of the driver. + * @param reg - Register structure holding info about the register to be written + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_write_register(struct ad7124_dev *dev, + struct ad7124_st_reg p_reg) +{ + int32_t ret; + + if (dev->check_ready) { + ret = ad7124_wait_for_spi_ready(dev, + dev->spi_rdy_poll_cnt); + if (ret < 0) + return ret; + } + ret = ad7124_no_check_write_register(dev, + p_reg); + + return ret; +} + +/***************************************************************************//** + * @brief Resets the device. + * + * @param dev - The handler of the instance of the driver. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_reset(struct ad7124_dev *dev) +{ + int32_t ret = 0; + uint8_t wr_buf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + if(!dev) + return INVALID_VAL; + + ret = spi_write_and_read(dev->spi_desc, + wr_buf, + 8); + + /* CRC is disabled after reset */ + dev->use_crc = AD7124_DISABLE_CRC; + + /* Read POR bit to clear */ + ret = ad7124_wait_to_power_on(dev, + dev->spi_rdy_poll_cnt); + + mdelay(AD7124_POST_RESET_DELAY); + + return ret; +} + +/***************************************************************************//** + * @brief Waits until the device can accept read and write user actions. + * + * @param dev - The handler of the instance of the driver. + * @param timeout - Count representing the number of polls to be done until the + * function returns. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_wait_for_spi_ready(struct ad7124_dev *dev, + uint32_t timeout) +{ + struct ad7124_st_reg *regs; + int32_t ret; + int8_t ready = 0; + + if(!dev) + return INVALID_VAL; + + regs = dev->regs; + + while(!ready && --timeout) { + /* Read the value of the Error Register */ + ret = ad7124_read_register(dev, ®s[AD7124_Error]); + if(ret < 0) + return ret; + + /* Check the SPI IGNORE Error bit in the Error Register */ + ready = (regs[AD7124_Error].value & + AD7124_ERR_REG_SPI_IGNORE_ERR) == 0; + } + + return timeout ? 0 : TIMEOUT; +} + +/***************************************************************************//** + * @brief Waits until the device finishes the power-on reset operation. + * + * @param dev - The handler of the instance of the driver. + * @param timeout - Count representing the number of polls to be done until the + * function returns. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_wait_to_power_on(struct ad7124_dev *dev, + uint32_t timeout) +{ + struct ad7124_st_reg *regs; + int32_t ret; + int8_t powered_on = 0; + + if(!dev) + return INVALID_VAL; + + regs = dev->regs; + + while(!powered_on && timeout--) { + ret = ad7124_read_register(dev, + ®s[AD7124_Status]); + if(ret < 0) + return ret; + + /* Check the POR_FLAG bit in the Status Register */ + powered_on = (regs[AD7124_Status].value & + AD7124_STATUS_REG_POR_FLAG) == 0; + } + + return (timeout || powered_on) ? 0 : TIMEOUT; +} + +/***************************************************************************//** + * @brief Waits until a new conversion result is available. + * + * @param dev - The handler of the instance of the driver. + * @param timeout - Count representing the number of polls to be done until the + * function returns if no new data is available. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_wait_for_conv_ready(struct ad7124_dev *dev, + uint32_t timeout) +{ + struct ad7124_st_reg *regs; + int32_t ret; + int8_t ready = 0; + + if(!dev) + return INVALID_VAL; + + regs = dev->regs; + + while(!ready && --timeout) { + /* Read the value of the Status Register */ + ret = ad7124_read_register(dev, ®s[AD7124_Status]); + if(ret < 0) + return ret; + + /* Check the RDY bit in the Status Register */ + ready = (regs[AD7124_Status].value & + AD7124_STATUS_REG_RDY) == 0; + } + + return timeout ? 0 : TIMEOUT; +} + +/***************************************************************************//** + * @brief Reads the conversion result from the device. + * + * @param dev - The handler of the instance of the driver. + * @param p_data - Pointer to store the read data. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_read_data(struct ad7124_dev *dev, + int32_t* p_data) +{ + struct ad7124_st_reg *regs; + int32_t ret; + + if(!dev) + return INVALID_VAL; + + regs = dev->regs; + + /* Read the value of the Status Register */ + ret = ad7124_read_register(dev, ®s[AD7124_Data]); + + /* Get the read result */ + *p_data = regs[AD7124_Data].value; + + return ret; +} + +/***************************************************************************//** + * @brief Computes the CRC checksum for a data buffer. + * + * @param p_buf - Data buffer + * @param buf_size - Data buffer size in bytes + * + * @return Returns the computed CRC checksum. +*******************************************************************************/ +uint8_t ad7124_compute_crc8(uint8_t * p_buf, uint8_t buf_size) +{ + uint8_t i = 0; + uint8_t crc = 0; + + while(buf_size) { + for(i = 0x80; i != 0; i >>= 1) { + bool cmp1 = (crc & 0x80) != 0; + bool cmp2 = (*p_buf & i) != 0; + if(cmp1 != cmp2) { /* MSB of CRC register XOR input Bit from Data */ + crc <<= 1; + crc ^= AD7124_CRC8_POLYNOMIAL_REPRESENTATION; + } else { + crc <<= 1; + } + } + p_buf++; + buf_size--; + } + return crc; +} + +/***************************************************************************//** + * @brief Updates the CRC settings. + * + * @param dev - The handler of the instance of the driver. + * + * @return None. +*******************************************************************************/ +void ad7124_update_crcsetting(struct ad7124_dev *dev) +{ + struct ad7124_st_reg *regs; + + if(!dev) + return; + + regs = dev->regs; + + /* Get CRC State. */ + if (regs[AD7124_Error_En].value & AD7124_ERREN_REG_SPI_CRC_ERR_EN) { + dev->use_crc = AD7124_USE_CRC; + } else { + dev->use_crc = AD7124_DISABLE_CRC; + } +} + +/***************************************************************************//** + * @brief Updates the device SPI interface settings. + * + * @param dev - The handler of the instance of the driver. + * + * @return None. +*******************************************************************************/ +void ad7124_update_dev_spi_settings(struct ad7124_dev *dev) +{ + struct ad7124_st_reg *regs; + + if(!dev) + return; + + regs = dev->regs; + + if (regs[AD7124_Error_En].value & AD7124_ERREN_REG_SPI_IGNORE_ERR_EN) { + dev->check_ready = 1; + } else { + dev->check_ready = 0; + } +} + +/***************************************************************************//** + * @brief Initializes the AD7124. + * + * @param device - The device structure. + * @param init_param - The structure that contains the device initial + * parameters. + * + * @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t ad7124_setup(struct ad7124_dev **device, + struct ad7124_init_param init_param) +{ + int32_t ret; + enum ad7124_registers reg_nr; + struct ad7124_dev *dev; + + dev = (struct ad7124_dev *)malloc(sizeof(*dev)); + if (!dev) + return INVALID_VAL; + + dev->regs = init_param.regs; + dev->spi_rdy_poll_cnt = init_param.spi_rdy_poll_cnt; + + /* Initialize the SPI communication. */ + ret = spi_init(&dev->spi_desc, &init_param.spi_init); + if (ret < 0) + return ret; + + /* Reset the device interface.*/ + ret = ad7124_reset(dev); + if (ret < 0) + return ret; + + /* Update the device structure with power-on/reset settings */ + dev->check_ready = 1; + + /* Initialize registers AD7124_ADC_Control through AD7124_Filter_7. */ + for(reg_nr = AD7124_Status; (reg_nr < AD7124_Offset_0) && !(ret < 0); + reg_nr++) { + if (dev->regs[reg_nr].rw == AD7124_RW) { + ret = ad7124_write_register(dev, dev->regs[reg_nr]); + if (ret < 0) + break; + } + + /* Get CRC State and device SPI interface settings */ + if (reg_nr == AD7124_Error_En) { + ad7124_update_crcsetting(dev); + ad7124_update_dev_spi_settings(dev); + } + } + + *device = dev; + + return ret; +} + +/***************************************************************************//** + * @brief Free the resources allocated by AD7124_Setup(). + * + * @param dev - The device structure. + * + * @return SUCCESS in case of success, negative error code otherwise. +*******************************************************************************/ +int32_t ad7124_remove(struct ad7124_dev *dev) +{ + int32_t ret; + + ret = spi_remove(dev->spi_desc); + + free(dev); + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad7124/ad7124.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,462 @@ +/***************************************************************************//** +* @file ad7124.h +* @brief AD7124 header file. +* Devices AD7124-4, AD7124-8 +* +******************************************************************************** +* Copyright 2015-2020(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef __AD7124_H__ +#define __AD7124_H__ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> +#include "platform_drivers.h" + +/******************************************************************************/ +/******************* Register map and register definitions ********************/ +/******************************************************************************/ +/* + * Create a contiguous bitmask starting at bit position @l and ending at + * position @h. + */ +#ifndef GENMASK +#define GENMASK(h, l) (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (31 - (h)))) +#endif +#define BIT(x) (1UL << (x)) + +#define AD7124_RW 1 /* Read and Write */ +#define AD7124_R 2 /* Read only */ +#define AD7124_W 3 /* Write only */ + +/* AD7124 Register Map */ +#define AD7124_COMM_REG 0x00 +#define AD7124_STATUS_REG 0x00 +#define AD7124_ADC_CTRL_REG 0x01 +#define AD7124_DATA_REG 0x02 +#define AD7124_IO_CTRL1_REG 0x03 +#define AD7124_IO_CTRL2_REG 0x04 +#define AD7124_ID_REG 0x05 +#define AD7124_ERR_REG 0x06 +#define AD7124_ERREN_REG 0x07 +#define AD7124_CH0_MAP_REG 0x09 +#define AD7124_CH1_MAP_REG 0x0A +#define AD7124_CH2_MAP_REG 0x0B +#define AD7124_CH3_MAP_REG 0x0C +#define AD7124_CH4_MAP_REG 0x0D +#define AD7124_CH5_MAP_REG 0x0E +#define AD7124_CH6_MAP_REG 0x0F +#define AD7124_CH7_MAP_REG 0x10 +#define AD7124_CH8_MAP_REG 0x11 +#define AD7124_CH9_MAP_REG 0x12 +#define AD7124_CH10_MAP_REG 0x13 +#define AD7124_CH11_MAP_REG 0x14 +#define AD7124_CH12_MAP_REG 0x15 +#define AD7124_CH13_MAP_REG 0x16 +#define AD7124_CH14_MAP_REG 0x17 +#define AD7124_CH15_MAP_REG 0x18 +#define AD7124_CFG0_REG 0x19 +#define AD7124_CFG1_REG 0x1A +#define AD7124_CFG2_REG 0x1B +#define AD7124_CFG3_REG 0x1C +#define AD7124_CFG4_REG 0x1D +#define AD7124_CFG5_REG 0x1E +#define AD7124_CFG6_REG 0x1F +#define AD7124_CFG7_REG 0x20 +#define AD7124_FILT0_REG 0x21 +#define AD7124_FILT1_REG 0x22 +#define AD7124_FILT2_REG 0x23 +#define AD7124_FILT3_REG 0x24 +#define AD7124_FILT4_REG 0x25 +#define AD7124_FILT5_REG 0x26 +#define AD7124_FILT6_REG 0x27 +#define AD7124_FILT7_REG 0x28 +#define AD7124_OFFS0_REG 0x29 +#define AD7124_OFFS1_REG 0x2A +#define AD7124_OFFS2_REG 0x2B +#define AD7124_OFFS3_REG 0x2C +#define AD7124_OFFS4_REG 0x2D +#define AD7124_OFFS5_REG 0x2E +#define AD7124_OFFS6_REG 0x2F +#define AD7124_OFFS7_REG 0x30 +#define AD7124_GAIN0_REG 0x31 +#define AD7124_GAIN1_REG 0x32 +#define AD7124_GAIN2_REG 0x33 +#define AD7124_GAIN3_REG 0x34 +#define AD7124_GAIN4_REG 0x35 +#define AD7124_GAIN5_REG 0x36 +#define AD7124_GAIN6_REG 0x37 +#define AD7124_GAIN7_REG 0x38 + +/* Communication Register bits */ +#define AD7124_COMM_REG_WEN (0 << 7) +#define AD7124_COMM_REG_WR (0 << 6) +#define AD7124_COMM_REG_RD (1 << 6) +#define AD7124_COMM_REG_RA(x) ((x) & 0x3F) + +/* Status Register bits */ +#define AD7124_STATUS_REG_RDY (1 << 7) +#define AD7124_STATUS_REG_ERROR_FLAG (1 << 6) +#define AD7124_STATUS_REG_POR_FLAG (1 << 4) +#define AD7124_STATUS_REG_CH_ACTIVE(x) ((x) & 0xF) + +/* ADC_Control Register bits */ +#define AD7124_ADC_CTRL_REG_DOUT_RDY_DEL (1 << 12) +#define AD7124_ADC_CTRL_REG_CONT_READ (1 << 11) +#define AD7124_ADC_CTRL_REG_DATA_STATUS (1 << 10) +#define AD7124_ADC_CTRL_REG_CS_EN (1 << 9) +#define AD7124_ADC_CTRL_REG_REF_EN (1 << 8) +#define AD7124_ADC_CTRL_REG_POWER_MODE_MSK GENMASK(7,6) +#define AD7124_ADC_CTRL_REG_POWER_MODE_RD(x) (((x) >> 6) & 0x3) +#define AD7124_ADC_CTRL_REG_POWER_MODE(x) (((x) & 0x3) << 6) +#define AD7124_ADC_CTRL_REG_MSK GENMASK(5,2) +#define AD7124_ADC_CTRL_REG_MODE_OUT(x) (((x) >> 2) & 0xF) +#define AD7124_ADC_CTRL_REG_MODE(x) (((x) & 0xF) << 2) +#define AD7124_ADC_CTRL_REG_CLK_SEL_MSK GENMASK(1,0) +#define AD7124_ADC_CTRL_REG_CLK_SEL_RD(x) (((x) >> 0) & 0x3) +#define AD7124_ADC_CTRL_REG_CLK_SEL(x) (((x) & 0x3) << 0) + +/* IO_Control_1 Register bits */ +#define AD7124_IO_CTRL1_REG_GPIO_DAT2 (1 << 23) +#define AD7124_IO_CTRL1_REG_GPIO_DAT1 (1 << 22) +#define AD7124_IO_CTRL1_REG_GPIO_CTRL2 (1 << 19) +#define AD7124_IO_CTRL1_REG_GPIO_CTRL1 (1 << 18) +#define AD7124_IO_CTRL1_REG_PDSW (1 << 15) +#define AD7124_IO_CTRL1_REG_IOUT1(x) (((x) & 0x7) << 11) +#define AD7124_IO_CTRL1_REG_IOUT0(x) (((x) & 0x7) << 8) +#define AD7124_IO_CTRL1_REG_IOUT_CH1(x) (((x) & 0xF) << 4) +#define AD7124_IO_CTRL1_REG_IOUT_CH0(x) (((x) & 0xF) << 0) + +/* IO_Control_1 AD7124-8 specific bits */ +#define AD7124_8_IO_CTRL1_REG_GPIO_DAT4 (1 << 23) +#define AD7124_8_IO_CTRL1_REG_GPIO_DAT3 (1 << 22) +#define AD7124_8_IO_CTRL1_REG_GPIO_DAT2 (1 << 21) +#define AD7124_8_IO_CTRL1_REG_GPIO_DAT1 (1 << 20) +#define AD7124_8_IO_CTRL1_REG_GPIO_CTRL4 (1 << 19) +#define AD7124_8_IO_CTRL1_REG_GPIO_CTRL3 (1 << 18) +#define AD7124_8_IO_CTRL1_REG_GPIO_CTRL2 (1 << 17) +#define AD7124_8_IO_CTRL1_REG_GPIO_CTRL1 (1 << 16) + +/* IO_Control_2 Register bits */ +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS7 (1 << 15) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS6 (1 << 14) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS5 (1 << 11) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS4 (1 << 10) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS3 (1 << 5) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS2 (1 << 4) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS1 (1 << 1) +#define AD7124_IO_CTRL2_REG_GPIO_VBIAS0 (1 << 0) + +/* IO_Control_2 AD7124-8 specific bits */ +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS15 (1 << 15) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS14 (1 << 14) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS13 (1 << 13) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS12 (1 << 12) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS11 (1 << 11) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS10 (1 << 10) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS9 (1 << 9) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS8 (1 << 8) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS7 (1 << 7) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS6 (1 << 6) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS5 (1 << 5) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS4 (1 << 4) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS3 (1 << 3) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS2 (1 << 2) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS1 (1 << 1) +#define AD7124_8_IO_CTRL2_REG_GPIO_VBIAS0 (1 << 0) + +/* ID Register bits */ +#define AD7124_ID_REG_DEVICE_ID(x) (((x) & 0xF) << 4) +#define AD7124_ID_REG_SILICON_REV(x) (((x) & 0xF) << 0) + +/* Error Register bits */ +#define AD7124_ERR_REG_LDO_CAP_ERR (1 << 19) +#define AD7124_ERR_REG_ADC_CAL_ERR (1 << 18) +#define AD7124_ERR_REG_ADC_CONV_ERR (1 << 17) +#define AD7124_ERR_REG_ADC_SAT_ERR (1 << 16) +#define AD7124_ERR_REG_AINP_OV_ERR (1 << 15) +#define AD7124_ERR_REG_AINP_UV_ERR (1 << 14) +#define AD7124_ERR_REG_AINM_OV_ERR (1 << 13) +#define AD7124_ERR_REG_AINM_UV_ERR (1 << 12) +#define AD7124_ERR_REG_REF_DET_ERR (1 << 11) +#define AD7124_ERR_REG_DLDO_PSM_ERR (1 << 9) +#define AD7124_ERR_REG_ALDO_PSM_ERR (1 << 7) +#define AD7124_ERR_REG_SPI_IGNORE_ERR (1 << 6) +#define AD7124_ERR_REG_SPI_SLCK_CNT_ERR (1 << 5) +#define AD7124_ERR_REG_SPI_READ_ERR (1 << 4) +#define AD7124_ERR_REG_SPI_WRITE_ERR (1 << 3) +#define AD7124_ERR_REG_SPI_CRC_ERR (1 << 2) +#define AD7124_ERR_REG_MM_CRC_ERR (1 << 1) +#define AD7124_ERR_REG_ROM_CRC_ERR (1 << 0) + +/* Error_En Register bits */ +#define AD7124_ERREN_REG_MCLK_CNT_EN (1 << 22) +#define AD7124_ERREN_REG_LDO_CAP_CHK_TEST_EN (1 << 21) +#define AD7124_ERREN_REG_LDO_CAP_CHK(x) (((x) & 0x3) << 19) +#define AD7124_ERREN_REG_ADC_CAL_ERR_EN (1 << 18) +#define AD7124_ERREN_REG_ADC_CONV_ERR_EN (1 << 17) +#define AD7124_ERREN_REG_ADC_SAT_ERR_EN (1 << 16) +#define AD7124_ERREN_REG_AINP_OV_ERR_EN (1 << 15) +#define AD7124_ERREN_REG_AINP_UV_ERR_EN (1 << 14) +#define AD7124_ERREN_REG_AINM_OV_ERR_EN (1 << 13) +#define AD7124_ERREN_REG_AINM_UV_ERR_EN (1 << 12) +#define AD7124_ERREN_REG_REF_DET_ERR_EN (1 << 11) +#define AD7124_ERREN_REG_DLDO_PSM_TRIP_TEST_EN (1 << 10) +#define AD7124_ERREN_REG_DLDO_PSM_ERR_ERR (1 << 9) +#define AD7124_ERREN_REG_ALDO_PSM_TRIP_TEST_EN (1 << 8) +#define AD7124_ERREN_REG_ALDO_PSM_ERR_EN (1 << 7) +#define AD7124_ERREN_REG_SPI_IGNORE_ERR_EN (1 << 6) +#define AD7124_ERREN_REG_SPI_SCLK_CNT_ERR_EN (1 << 5) +#define AD7124_ERREN_REG_SPI_READ_ERR_EN (1 << 4) +#define AD7124_ERREN_REG_SPI_WRITE_ERR_EN (1 << 3) +#define AD7124_ERREN_REG_SPI_CRC_ERR_EN (1 << 2) +#define AD7124_ERREN_REG_MM_CRC_ERR_EN (1 << 1) +#define AD7124_ERREN_REG_ROM_CRC_ERR_EN (1 << 0) + +/* Channel Registers 0-15 bits */ +#define AD7124_CH_MAP_REG_CH_ENABLE_RD(x) (((x) >> 15) & 0x1) +#define AD7124_CH_MAP_REG_CH_ENABLE (1 << 15) +#define AD7124_CH_MAP_REG_SETUP_MSK GENMASK(14, 12) +#define AD7124_CH_MAP_REG_SETUP_RD(x) (((x) >> 12) & 0x7) +#define AD7124_CH_MAP_REG_SETUP(x) (((x) & 0x7) << 12) +#define AD7124_CH_MAP_REG_AINP_MSK GENMASK(9, 5) +#define AD7124_CH_MAP_REG_AINP_RD(x) (((x) >> 5) & 0x1F) +#define AD7124_CH_MAP_REG_AINP(x) (((x) & 0x1F) << 5) +#define AD7124_CH_MAP_REG_AINM_MSK GENMASK(4, 0) +#define AD7124_CH_MAP_REG_AINM_RD(x) (((x) >> 0) & 0x1F) +#define AD7124_CH_MAP_REG_AINM(x) (((x) & 0x1F) << 0) + +/* Configuration Registers 0-7 bits */ +#define AD7124_CFG_REG_BIPOLAR_RD(x) (((x) >> 11) & 0x1) +#define AD7124_CFG_REG_BIPOLAR (1 << 11) +#define AD7124_CFG_REG_BURNOUT_RD(x) (((x) >> 9) & 0x3) +#define AD7124_CFG_REG_BURNOUT(x) (((x) & 0x3) << 9) +#define AD7124_CFG_REG_REF_BUFP_RD(x) (((x) >> 8) & 0x1) +#define AD7124_CFG_REG_REF_BUFP (1 << 8) +#define AD7124_CFG_REG_REF_BUFM_RD(x) (((x) >> 7) & 0x1) +#define AD7124_CFG_REG_REF_BUFM (1 << 7) +#define AD7124_CFG_REG_AIN_BUFP_RD(x) (((x) >> 6) & 0x1) +#define AD7124_CFG_REG_AIN_BUFP (1 << 6) +#define AD7124_CFG_REG_AINM_BUFP_RD(x) (((x) >> 5) & 0x1) +#define AD7124_CFG_REG_AINN_BUFM (1 << 5) +#define AD7124_CFG_REG_REF_SEL_MSK GENMASK(4, 3) +#define AD7124_CFG_REG_REF_SEL_RD(x) (((x) >> 3) & 0x3) +#define AD7124_CFG_REG_REF_SEL(x) (((x) & 0x3) << 3) +#define AD7124_CFG_REG_PGA_MSK GENMASK(2, 0) +#define AD7124_CFG_REG_PGA_RD(x) (((x) >> 0) & 0x7) +#define AD7124_CFG_REG_PGA(x) (((x) & 0x7) << 0) + +/* Filter Register 0-7 bits */ +#define AD7124_FILT_REG_FILTER_MSK GENMASK(23, 21) +#define AD7124_FILT_REG_FILTER_RD(x) (((x) >> 21) & 0x7) +#define AD7124_FILT_REG_FILTER(x) (((x) & 0x7) << 21) +#define AD7124_FILT_REG_REJ60 (1 << 20) +#define AD7124_FILT_REG_POST_FILTER_MSK GENMASK(19, 17) +#define AD7124_FILT_REG_POST_FILTER_RD(x) (((x) >> 17) & 0x7) +#define AD7124_FILT_REG_POST_FILTER(x) (((x) & 0x7) << 17) +#define AD7124_FILT_REG_SINGLE_CYCLE (1 << 16) +#define AD7124_FILT_REG_FS_MSK GENMASK(10, 0) +#define AD7124_FILT_REG_FS_RD(x) (((x) >> 0) & 0x7FF) +#define AD7124_FILT_REG_FS(x) (((x) & 0x7FF) << 0) + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/*! Device register info */ +struct ad7124_st_reg { + int32_t addr; + int32_t value; + int32_t size; + int32_t rw; +}; + +/*! AD7124 registers list*/ +enum ad7124_registers { + AD7124_Status = 0x00, + AD7124_ADC_Control, + AD7124_Data, + AD7124_IOCon1, + AD7124_IOCon2, + AD7124_ID, + AD7124_Error, + AD7124_Error_En, + AD7124_Mclk_Count, + AD7124_Channel_0, + AD7124_Channel_1, + AD7124_Channel_2, + AD7124_Channel_3, + AD7124_Channel_4, + AD7124_Channel_5, + AD7124_Channel_6, + AD7124_Channel_7, + AD7124_Channel_8, + AD7124_Channel_9, + AD7124_Channel_10, + AD7124_Channel_11, + AD7124_Channel_12, + AD7124_Channel_13, + AD7124_Channel_14, + AD7124_Channel_15, + AD7124_Config_0, + AD7124_Config_1, + AD7124_Config_2, + AD7124_Config_3, + AD7124_Config_4, + AD7124_Config_5, + AD7124_Config_6, + AD7124_Config_7, + AD7124_Filter_0, + AD7124_Filter_1, + AD7124_Filter_2, + AD7124_Filter_3, + AD7124_Filter_4, + AD7124_Filter_5, + AD7124_Filter_6, + AD7124_Filter_7, + AD7124_Offset_0, + AD7124_Offset_1, + AD7124_Offset_2, + AD7124_Offset_3, + AD7124_Offset_4, + AD7124_Offset_5, + AD7124_Offset_6, + AD7124_Offset_7, + AD7124_Gain_0, + AD7124_Gain_1, + AD7124_Gain_2, + AD7124_Gain_3, + AD7124_Gain_4, + AD7124_Gain_5, + AD7124_Gain_6, + AD7124_Gain_7, + AD7124_REG_NO +}; + +/* + * The structure describes the device and is used with the ad7124 driver. + * @spi_desc: A reference to the SPI configuration of the device. + * @regs: A reference to the register list of the device that the user must + * provide when calling the Setup() function. + * @userCRC: Whether to do or not a cyclic redundancy check on SPI transfers. + * @check_ready: When enabled all register read and write calls will first wait + * until the device is ready to accept user requests. + * @spi_rdy_poll_cnt: Number of times the driver should read the Error register + * to check if the device is ready to accept user requests, + * before a timeout error will be issued. + */ +struct ad7124_dev { + /* SPI */ + spi_desc *spi_desc; + /* Device Settings */ + struct ad7124_st_reg *regs; + int16_t use_crc; + int16_t check_ready; + int16_t spi_rdy_poll_cnt; +}; + +struct ad7124_init_param { + /* SPI */ + spi_init_param spi_init; + /* Device Settings */ + struct ad7124_st_reg *regs; + int16_t spi_rdy_poll_cnt; +}; + +/******************************************************************************/ +/******************* AD7124 Constants *****************************************/ +/******************************************************************************/ +#define AD7124_CRC8_POLYNOMIAL_REPRESENTATION 0x07 /* x8 + x2 + x + 1 */ +#define AD7124_DISABLE_CRC 0 +#define AD7124_USE_CRC 1 + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/*! Reads the value of the specified register. */ +int32_t ad7124_read_register(struct ad7124_dev *dev, + struct ad7124_st_reg* p_reg); + +/*! Writes the value of the specified register. */ +int32_t ad7124_write_register(struct ad7124_dev *dev, + struct ad7124_st_reg reg); + +/*! Reads the value of the specified register without a device state check. */ +int32_t ad7124_no_check_read_register(struct ad7124_dev *dev, + struct ad7124_st_reg* p_reg); + +/*! Writes the value of the specified register without a device state check. */ +int32_t ad7124_no_check_write_register(struct ad7124_dev *dev, + struct ad7124_st_reg reg); + +/*! Resets the device. */ +int32_t ad7124_reset(struct ad7124_dev *dev); + +/*! Waits until the device can accept read and write user actions. */ +int32_t ad7124_wait_for_spi_ready(struct ad7124_dev *dev, + uint32_t timeout); + +/*! Waits until the device finishes the power-on reset operation. */ +int32_t ad7124_wait_to_power_on(struct ad7124_dev *dev, + uint32_t timeout); + +/*! Waits until a new conversion result is available. */ +int32_t ad7124_wait_for_conv_ready(struct ad7124_dev *dev, + uint32_t timeout); + +/*! Reads the conversion result from the device. */ +int32_t ad7124_read_data(struct ad7124_dev *dev, + int32_t* p_data); + +/*! Computes the CRC checksum for a data buffer. */ +uint8_t ad7124_compute_crc8(uint8_t* p_buf, + uint8_t buf_size); + +/*! Updates the CRC settings. */ +void ad7124_update_crcsetting(struct ad7124_dev *dev); + +/*! Updates the device SPI interface settings. */ +void ad7124_update_dev_spi_settings(struct ad7124_dev *dev); + +/*! Initializes the AD7124. */ +int32_t ad7124_setup(struct ad7124_dev **device, + struct ad7124_init_param init_param); +/*! Free the resources allocated by AD7124_Setup(). */ +int32_t ad7124_remove(struct ad7124_dev *dev); + +#endif /* __AD7124_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad7124/ad7124_regs.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,102 @@ +/***************************************************************************//** +* @file ad7124-regs.c +* @brief AD7124 register file. +* @devices AD7124-4, AD7124-8 +* +******************************************************************************** +* Copyright 2015-2019(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + + +#include "ad7124_regs.h" + +struct ad7124_st_reg ad7124_regs[AD7124_REG_NO] = { + {0x00, 0x00, 1, 2}, /* AD7124_Status */ + {0x01, 0x0000, 2, 1}, /* AD7124_ADC_Control */ + {0x02, 0x0000, 3, 2}, /* AD7124_Data */ + {0x03, 0x0000, 3, 1}, /* AD7124_IOCon1 */ + {0x04, 0x0000, 2, 1}, /* AD7124_IOCon2 */ + {0x05, 0x02, 1, 2}, /* AD7124_ID */ + {0x06, 0x0000, 3, 2}, /* AD7124_Error */ + {0x07, 0x0040, 3, 1}, /* AD7124_Error_En */ + {0x08, 0x00, 1, 2}, /* AD7124_Mclk_Count */ + {0x09, 0x8001, 2, 1}, /* AD7124_Channel_0 */ + {0x0A, 0x0001, 2, 1}, /* AD7124_Channel_1 */ + {0x0B, 0x0001, 2, 1}, /* AD7124_Channel_2 */ + {0x0C, 0x0001, 2, 1}, /* AD7124_Channel_3 */ + {0x0D, 0x0001, 2, 1}, /* AD7124_Channel_4 */ + {0x0E, 0x0001, 2, 1}, /* AD7124_Channel_5 */ + {0x0F, 0x0001, 2, 1}, /* AD7124_Channel_6 */ + {0x10, 0x0001, 2, 1}, /* AD7124_Channel_7 */ + {0x11, 0x0001, 2, 1}, /* AD7124_Channel_8 */ + {0x12, 0x0001, 2, 1}, /* AD7124_Channel_9 */ + {0x13, 0x0001, 2, 1}, /* AD7124_Channel_10 */ + {0x14, 0x0001, 2, 1}, /* AD7124_Channel_11 */ + {0x15, 0x0001, 2, 1}, /* AD7124_Channel_12 */ + {0x16, 0x0001, 2, 1}, /* AD7124_Channel_13 */ + {0x17, 0x0001, 2, 1}, /* AD7124_Channel_14 */ + {0x18, 0x0001, 2, 1}, /* AD7124_Channel_15 */ + {0x19, 0x0860, 2, 1}, /* AD7124_Config_0 */ + {0x1A, 0x0860, 2, 1}, /* AD7124_Config_1 */ + {0x1B, 0x0860, 2, 1}, /* AD7124_Config_2 */ + {0x1C, 0x0860, 2, 1}, /* AD7124_Config_3 */ + {0x1D, 0x0860, 2, 1}, /* AD7124_Config_4 */ + {0x1E, 0x0860, 2, 1}, /* AD7124_Config_5 */ + {0x1F, 0x0860, 2, 1}, /* AD7124_Config_6 */ + {0x20, 0x0860, 2, 1}, /* AD7124_Config_7 */ + {0x21, 0x060180, 3, 1}, /* AD7124_Filter_0 */ + {0x22, 0x060180, 3, 1}, /* AD7124_Filter_1 */ + {0x23, 0x060180, 3, 1}, /* AD7124_Filter_2 */ + {0x24, 0x060180, 3, 1}, /* AD7124_Filter_3 */ + {0x25, 0x060180, 3, 1}, /* AD7124_Filter_4 */ + {0x26, 0x060180, 3, 1}, /* AD7124_Filter_5 */ + {0x27, 0x060180, 3, 1}, /* AD7124_Filter_6 */ + {0x28, 0x060180, 3, 1}, /* AD7124_Filter_7 */ + {0x29, 0x800000, 3, 1}, /* AD7124_Offset_0 */ + {0x2A, 0x800000, 3, 1}, /* AD7124_Offset_1 */ + {0x2B, 0x800000, 3, 1}, /* AD7124_Offset_2 */ + {0x2C, 0x800000, 3, 1}, /* AD7124_Offset_3 */ + {0x2D, 0x800000, 3, 1}, /* AD7124_Offset_4 */ + {0x2E, 0x800000, 3, 1}, /* AD7124_Offset_5 */ + {0x2F, 0x800000, 3, 1}, /* AD7124_Offset_6 */ + {0x30, 0x800000, 3, 1}, /* AD7124_Offset_7 */ + {0x31, 0x500000, 3, 1}, /* AD7124_Gain_0 */ + {0x32, 0x500000, 3, 1}, /* AD7124_Gain_1 */ + {0x33, 0x500000, 3, 1}, /* AD7124_Gain_2 */ + {0x34, 0x500000, 3, 1}, /* AD7124_Gain_3 */ + {0x35, 0x500000, 3, 1}, /* AD7124_Gain_4 */ + {0x36, 0x500000, 3, 1}, /* AD7124_Gain_5 */ + {0x37, 0x500000, 3, 1}, /* AD7124_Gain_6 */ + {0x38, 0x500000, 3, 1}, /* AD7124_Gain_7 */ +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad7124/ad7124_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,51 @@ +/***************************************************************************//** +* @file ad7124-regs.h +* @brief AD7124 register file. +* @devices AD7124-4, AD7124-8 +* +******************************************************************************** +* Copyright 2015-2019(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + + +#ifndef __AD7124_REGS_H__ +#define __AD7124_REGS_H__ + +#include "ad7124.h" + +/*! Array holding the info for the ad7124 registers - address, initial value, + size and access type. */ +extern struct ad7124_st_reg ad7124_regs[AD7124_REG_NO]; + +#endif /* __AD7124_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad713x/ad713x.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,603 @@ +/***************************************************************************//** + * @file ad713x.c + * @brief Implementation of ad713x Driver. + * @author SPopa (stefan.popa@analog.com) + * @author Andrei Drimbarean (andrei.drimbarean@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdlib.h> +#include "ad713x.h" + +/******************************************************************************/ +/***************************** Variable definition ****************************/ +/******************************************************************************/ + +static const int ad713x_output_data_frame[3][9][2] = { + { + {ADC_16_BIT_DATA, CRC_6}, + {ADC_24_BIT_DATA, CRC_6}, + {ADC_32_BIT_DATA, NO_CRC}, + {ADC_32_BIT_DATA, CRC_6}, + {ADC_16_BIT_DATA, NO_CRC}, + {ADC_24_BIT_DATA, NO_CRC}, + {ADC_24_BIT_DATA, CRC_8}, + {ADC_32_BIT_DATA, CRC_8}, + {INVALID} + }, + { + {ADC_16_BIT_DATA, NO_CRC}, + {ADC_16_BIT_DATA, CRC_6}, + {ADC_24_BIT_DATA, NO_CRC}, + {ADC_24_BIT_DATA, CRC_6}, + {ADC_16_BIT_DATA, CRC_8}, + {ADC_24_BIT_DATA, CRC_8}, + {INVALID} + }, + { + {ADC_16_BIT_DATA, NO_CRC}, + {ADC_16_BIT_DATA, CRC_6}, + {ADC_16_BIT_DATA, CRC_8}, + {INVALID} + }, +}; + +/******************************************************************************/ +/************************** Functions Implementation **************************/ +/******************************************************************************/ + +/** + * @brief Read from device. + * @param dev - The device structure. + * @param reg_addr - The register address. + * @param reg_data - The register data. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_spi_reg_read(struct ad713x_dev *dev, + uint8_t reg_addr, + uint8_t *reg_data) +{ + int32_t ret; + uint8_t buf[2]; + + buf[0] = AD713X_REG_READ(reg_addr); + buf[1] = 0x00; + + ret = spi_write_and_read(dev->spi_desc, buf, 2); + if(IS_ERR_VALUE(ret)) + return FAILURE; + *reg_data = buf[1]; + + return SUCCESS; +} + +/** + * @brief Write to device. + * @param dev - The device structure. + * @param reg_addr - The register address. + * @param reg_data - The register data. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_spi_reg_write(struct ad713x_dev *dev, + uint8_t reg_addr, + uint8_t reg_data) +{ + uint8_t buf[2]; + + buf[0] = reg_addr; + buf[1] = reg_data; + + return spi_write_and_read(dev->spi_desc, buf, 2); +} + +/** + * @brief SPI write to device using a mask. + * @param dev - The device structure. + * @param reg_addr - The register address. + * @param mask - The mask. + * @param data - The register data. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_spi_write_mask(struct ad713x_dev *dev, + uint8_t reg_addr, + uint32_t mask, + uint8_t data) +{ + uint8_t reg_data; + int32_t ret; + + ret = ad713x_spi_reg_read(dev, reg_addr, ®_data); + if(IS_ERR_VALUE(ret)) + return FAILURE; + reg_data &= ~mask; + reg_data |= data; + + return ad713x_spi_reg_write(dev, reg_addr, reg_data); +} + +/** + * @brief Device power mode control. + * @param dev - The device structure. + * @param mode - Type of power mode + * Accepted values: LOW_POWER + * HIGH_POWER + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_set_power_mode(struct ad713x_dev *dev, + enum ad713x_power_mode mode) +{ + if (mode == LOW_POWER) + return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG, + AD713X_DEV_CONFIG_PWR_MODE_MSK, 0); + else if (mode == HIGH_POWER) + return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG, + AD713X_DEV_CONFIG_PWR_MODE_MSK, + 1); + + return FAILURE; +} + +/** + * @brief ADC conversion data output frame control. + * @param dev - The device structure. + * @param adc_data_len - Data conversion length + * Accepted values: ADC_16_BIT_DATA + * ADC_24_BIT_DATA + * ADC_32_BIT_DATA + * @param crc_header - CRC header + * Accepted values: NO_CRC + * CRC_6 + * CRC_8 + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_set_out_data_frame(struct ad713x_dev *dev, + enum ad713x_adc_data_len adc_data_len, + enum ad713x_crc_header crc_header) +{ + uint8_t id; + uint8_t i = 0; + + id = dev->dev_id; + + while (ad713x_output_data_frame[id][i][0] != INVALID) { + if((adc_data_len == ad713x_output_data_frame[id][i][0]) && + (crc_header == ad713x_output_data_frame[id][i][1])) { + return ad713x_spi_write_mask(dev, + AD713X_REG_DATA_PACKET_CONFIG, + AD713X_DATA_PACKET_CONFIG_FRAME_MSK, + AD713X_DATA_PACKET_CONFIG_FRAME_MODE(i)); + } + i++; + } + + return FAILURE; +} + +/** + * @brief DOUTx output format configuration. + * @param dev - The device structure. + * @param format - Single channel daisy chain mode. Dual channel daisy chain mode. + * Quad channel parallel output mode. Channel data averaging mode. + * Accepted values: SINGLE_CH_DC + * DUAL_CH_DC + * QUAD_CH_PO + * CH_AVG_MODE + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_dout_format_config(struct ad713x_dev *dev, + enum ad713x_doutx_format format) +{ + return ad713x_spi_write_mask(dev, AD713X_REG_DIGITAL_INTERFACE_CONFIG, + AD713X_DIG_INT_CONFIG_FORMAT_MSK, + AD713X_DIG_INT_CONFIG_FORMAT_MODE(format)); +} + +/** + * @brief Magnitude and phase matching calibration clock delay enable for all + * channels at 2 clock delay. + * This function is kept for backwards compatibility with the current + * application source, but it is deprecated. Use + * ad713x_mag_phase_clk_delay_chan(). + * @param dev - The device structure. + * @param clk_delay_en - Enable or disable Mag/Phase clock delay. + * Accepted values: true + * false + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_mag_phase_clk_delay(struct ad713x_dev *dev, + bool clk_delay_en) +{ + int32_t ret; + int8_t i; + int8_t temp_clk_delay; + + if (clk_delay_en) + temp_clk_delay = DELAY_2_CLOCKS; + else + temp_clk_delay = DELAY_NONE; + + for (i = CH3; i >= 0; i--) { + ret = ad713x_spi_write_mask(dev, AD713X_REG_MPC_CONFIG, + AD713X_MPC_CLKDEL_EN_CH_MSK(i), + AD713X_MPC_CLKDEL_EN_CH_MODE(temp_clk_delay, i)); + if (IS_ERR_VALUE(ret)) + return FAILURE; + } + + return SUCCESS; +} + +/** + * @brief Change magnitude and phase calibration clock delay mode for a specific + * channel. + * @param dev - The device structure. + * @param chan - ID of the channel to be changed. + * Accepted values: CH0, CH1, CH2, CH3 + * @param mode - Delay in clock periods. + * Accepted values: DELAY_NONE, + * DELAY_1_CLOCKS, + * DELAY_2_CLOCKS + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_mag_phase_clk_delay_chan(struct ad713x_dev *dev, + enum ad713x_channels chan, + enum ad717x_mpc_clkdel mode) +{ + return ad713x_spi_write_mask(dev, AD713X_REG_MPC_CONFIG, + AD713X_MPC_CLKDEL_EN_CH_MSK(chan), + AD713X_MPC_CLKDEL_EN_CH_MODE(mode, chan)); +} + +/** + * @brief Digital filter type selection for each channel + * @param dev - The device structure. + * @param filter - Type of filter: Wideband, Sinc6, Sinc3, + * Sinc3 filter with simultaneous 50Hz and 60Hz rejection. + * Accepted values: FIR + * SINC6 + * SINC3 + * SINC3_50_60_REJ + * @param ch - Channel to apply the filter to + * Accepted values: CH0 + * CH1 + * CH2 + * CH3 + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_dig_filter_sel_ch(struct ad713x_dev *dev, + enum ad713x_dig_filter_sel filter, + enum ad713x_channels ch) +{ + return ad713x_spi_write_mask(dev, AD713X_REG_CHAN_DIG_FILTER_SEL, + AD713X_DIGFILTER_SEL_CH_MSK(ch), + AD713X_DIGFILTER_SEL_CH_MODE(filter, ch)); +} + +/** + * @brief Enable/Disable CLKOUT output. + * @param [in] dev - The device structure. + * @param [in] enable - true to enable the clkout output; + * false to disable the clkout output. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_clkout_output_en(struct ad713x_dev *dev, bool enable) +{ + return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG1, + AD713X_DEV_CONFIG1_CLKOUT_EN_MSK, + enable ? AD713X_DEV_CONFIG1_CLKOUT_EN_MSK : 0); +} + +/** + * @brief Enable/Disable reference gain correction. + * @param [in] dev - The device structure. + * @param [in] enable - true to enable the reference gain correction; + * false to disable the reference gain correction. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_ref_gain_correction_en(struct ad713x_dev *dev, bool enable) +{ + return ad713x_spi_write_mask(dev, AD713X_REG_DEVICE_CONFIG1, + AD713X_DEV_CONFIG1_REF_GAIN_CORR_EN_MSK, + enable ? AD713X_DEV_CONFIG1_REF_GAIN_CORR_EN_MSK : 0); +} + +/** + * @brief Select the wideband filter bandwidth for a channel. + * The option is relative to ODR, so it's a fraction of it. + * @param [in] dev - The device structure. + * @param [in] ch - Number of the channel to which to set the wideband filter + * option. + * @param [in] wb_opt - Option to set the wideband filter: + * Values are: + * 0 - bandwidth of 0.443 * ODR; + * 1 - bandwidth of 0.10825 * ODR. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_wideband_bw_sel(struct ad713x_dev *dev, + enum ad713x_channels ch, uint8_t wb_opt) +{ + return ad713x_spi_write_mask(dev, AD713X_REG_FIR_BW_SEL, + AD713X_FIR_BW_SEL_CH_MSK(ch), + wb_opt ? AD713X_FIR_BW_SEL_CH_MSK(ch) : 0); +} + +/** + * @brief Initialize GPIO driver handlers for the GPIOs in the system. + * ad713x_init() helper function. + * @param [out] dev - AD713X device handler. + * @param [in] init_param - Pointer to the initialization structure. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +static int32_t ad713x_init_gpio(struct ad713x_dev *dev, + struct ad713x_init_param *init_param) +{ + + int32_t ret; + + ret = gpio_get_optional(&dev->gpio_mode, init_param->gpio_mode); + if (IS_ERR_VALUE(ret)) + return FAILURE; + + ret = gpio_get_optional(&dev->gpio_dclkmode, init_param->gpio_dclkmode); + if (IS_ERR_VALUE(ret)) + return FAILURE; + + ret = gpio_get_optional(&dev->gpio_dclkio, init_param->gpio_dclkio); + if (IS_ERR_VALUE(ret)) + return FAILURE; + + ret = gpio_get_optional(&dev->gpio_resetn, init_param->gpio_resetn); + if (IS_ERR_VALUE(ret)) + return FAILURE; + + ret = gpio_get_optional(&dev->gpio_pnd, init_param->gpio_pnd); + if (IS_ERR_VALUE(ret)) + return FAILURE; + + /** Tie this pin to IOVDD for master mode operation, tie this pin to + * IOGND for slave mode operation. */ + if (init_param->gpio_mode) { + ret = gpio_direction_output(dev->gpio_mode, + init_param->mode_master_nslave); + if (IS_ERR_VALUE(ret)) + return FAILURE; + } + + /* Tie this pin low to ground to make DLCK operating in gated mode */ + if (init_param->gpio_dclkmode) { + ret = gpio_direction_output(dev->gpio_dclkmode, + init_param->dclkmode_free_ngated); + if (IS_ERR_VALUE(ret)) + return FAILURE; + } + + /** Tie this pin high to make DCLK an output, tie this pin low to make + * DLCK an input. */ + if (init_param->gpio_dclkio) { + ret = gpio_direction_output(dev->gpio_dclkio, + init_param->dclkio_out_nin); + if (IS_ERR_VALUE(ret)) + return FAILURE; + } + + /** Get the ADCs out of power down state */ + if (init_param->gpio_pnd) { + ret = gpio_direction_output(dev->gpio_pnd, init_param->pnd); + if (IS_ERR_VALUE(ret)) + return FAILURE; + } + + /** Reset to configure pins */ + if (init_param->gpio_resetn) { + ret = gpio_direction_output(dev->gpio_resetn, false); + if (IS_ERR_VALUE(ret)) + return FAILURE; + mdelay(100); + ret = gpio_set_value(dev->gpio_resetn, true); + if (IS_ERR_VALUE(ret)) + return FAILURE; + mdelay(100); + } + + return SUCCESS; +} + +/** + * @brief Free the resources allocated by ad713x_init_gpio(). + * @param dev - The device structure. + * @return SUCCESS in case of success, negative error code otherwise. + */ +static int32_t ad713x_remove_gpio(struct ad713x_dev *dev) +{ + int32_t ret; + + if (dev->gpio_dclkio) { + ret = gpio_remove(dev->gpio_dclkio); + if(IS_ERR_VALUE(ret)) + return FAILURE; + } + if (dev->gpio_dclkio) { + ret = gpio_remove(dev->gpio_dclkmode); + if(IS_ERR_VALUE(ret)) + return FAILURE; + } + if (dev->gpio_mode) { + ret = gpio_remove(dev->gpio_mode); + if(IS_ERR_VALUE(ret)) + return FAILURE; + } + if (dev->gpio_pnd) { + ret = gpio_remove(dev->gpio_pnd); + if(IS_ERR_VALUE(ret)) + return FAILURE; + } + if (dev->gpio_resetn) { + ret = gpio_remove(dev->gpio_resetn); + if(IS_ERR_VALUE(ret)) + return FAILURE; + } + + return SUCCESS; +} + +/** + * @brief Initialize the wideband filter bandwidth for every channel. + * ad713x_init() helper function. + * @param [in] dev - AD713X device handler. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +static int32_t ad713x_init_chan_bw(struct ad713x_dev *dev) +{ + int8_t i; + int32_t ret; + + for (i = CH3; i >= 0; i--) { + ret = ad713x_wideband_bw_sel(dev, i, 0); + if (IS_ERR_VALUE(ret)) + return FAILURE; + } + + return SUCCESS; +} + +/** + * @brief Initialize the device. + * @param device - The device structure. + * @param init_param - The structure that contains the device initial + * parameters. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t ad713x_init(struct ad713x_dev **device, + struct ad713x_init_param *init_param) +{ + struct ad713x_dev *dev; + int32_t ret; + uint8_t data; + + dev = (struct ad713x_dev *)calloc(1, sizeof(*dev)); + if (!dev) + return FAILURE; + + if (!init_param->spi_common_dev) { + ret = spi_init(&dev->spi_desc, &init_param->spi_init_prm); + if (IS_ERR_VALUE(ret)) + goto error_dev; + } else { + dev->spi_desc = calloc(1, sizeof *dev->spi_desc); + dev->spi_desc->chip_select = init_param->spi_init_prm.chip_select; + dev->spi_desc->extra = init_param->spi_common_dev->extra; + dev->spi_desc->max_speed_hz = init_param->spi_init_prm.max_speed_hz; + dev->spi_desc->mode = init_param->spi_init_prm.mode; + } + + ret = ad713x_init_gpio(dev, init_param); + if(IS_ERR_VALUE(ret)) + goto error_gpio; + + dev->dev_id = init_param->dev_id; + + ret = ad713x_spi_reg_read(dev, AD713X_REG_DEVICE_CONFIG, &data); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + data |= AD713X_DEV_CONFIG_PWR_MODE_MSK; + ret = ad713x_spi_reg_write(dev, AD713X_REG_DEVICE_CONFIG, data); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + ret = ad713x_clkout_output_en(dev, true); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + ret = ad713x_ref_gain_correction_en(dev, true); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + ret = ad713x_set_out_data_frame(dev, init_param->adc_data_len, + init_param->crc_header); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + ret = ad713x_dout_format_config(dev, init_param->format); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + ret = ad713x_mag_phase_clk_delay(dev, init_param->clk_delay_en); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + ret = ad713x_init_chan_bw(dev); + if (IS_ERR_VALUE(ret)) + goto error_gpio; + + *device = dev; + + return SUCCESS; + +error_gpio: + ad713x_remove_gpio(dev); +error_dev: + ad713x_remove(dev); + + return FAILURE; +} + +/** + * @brief Free the resources allocated by ad713x_init(). + * @param dev - The device structure. + * @return SUCCESS in case of success, negative error code otherwise. + */ +int32_t ad713x_remove(struct ad713x_dev *dev) +{ + int32_t ret; + + if(!dev) + return FAILURE; + + ret = spi_remove(dev->spi_desc); + if(IS_ERR_VALUE(ret)) + return FAILURE; + + ret = ad713x_remove_gpio(dev); + if(IS_ERR_VALUE(ret)) + return FAILURE; + + free(dev); + + return SUCCESS; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad713x/ad713x.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,789 @@ +/***************************************************************************//** + * @file ad713x.h + * @brief Header file for the ad713x Driver. + * @author SPopa (stefan.popa@analog.com) + * @author Andrei Drimbarean (andrei.drimbarean@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef SRC_AD713X_H_ +#define SRC_AD713X_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdbool.h> +#include <stdio.h> + +#include "platform_drivers.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ +/* + * AD713X registers definition + */ +#define AD713X_REG_INTERFACE_CONFIG_A 0x00 +#define AD713X_REG_INTERFACE_CONFIG_B 0x01 +#define AD713X_REG_DEVICE_CONFIG 0x02 +#define AD713X_REG_CHIP_TYPE 0x03 +#define AD713X_REG_PRODUCT_ID_LSB 0x04 +#define AD713X_REG_PRODUCT_ID_MSB 0x05 +#define AD713X_REG_CHIP_GRADE 0x06 +#define AD713X_REG_CHIP_INDEX 0x07 +#define AD713X_REG_SCTATCH_PAD 0x0A +#define AD713X_REG_SPI_REVISION 0x0B +#define AD713X_REG_VENDOR_ID_LSB 0x0C +#define AD713X_REG_VENDOR_ID_MSB 0x0D +#define AD713X_REG_STREAM_MODE 0x0E +#define AD713X_REG_TRANSFER_REGISTER 0x0F +#define AD713X_REG_DEVICE_CONFIG1 0x10 +#define AD713X_REG_DATA_PACKET_CONFIG 0x11 +#define AD713X_REG_DIGITAL_INTERFACE_CONFIG 0x12 +#define AD713X_REG_POWER_DOWN_CONTROL 0x13 +#define AD713X_REG_AIN_RANGE_SELECT 0x14 +#define AD713X_REG_DEVICE_STATUS 0x15 +#define AD713X_REG_ODR_VAL_INT_LSB 0x16 +#define AD713X_REG_ODR_VAL_INT_MID 0x17 +#define AD713X_REG_ODR_VAL_INT_MSB 0x18 +#define AD713X_REG_ODR_VAL_FLT_LSB 0x19 +#define AD713X_REG_ODR_VAL_FLT_MID0 0x1A +#define AD713X_REG_ODR_VAL_FLT_MID1 0x1B +#define AD713X_REG_ODR_VAL_FLT_MSB 0x1C +#define AD713X_REG_CHANNEL_ODR_SELECT 0x1D +#define AD713X_REG_CHAN_DIG_FILTER_SEL 0x1E +#define AD713X_REG_FIR_BW_SEL 0x1F +#define AD713X_REG_GPIO_DIR_CTRL 0x20 +#define AD713X_REG_GPIO_DATA 0x21 +#define AD713X_REG_ERROR_PIN_SRC_CONTROL 0x22 +#define AD713X_REG_ERROR_PIN_CONTROL 0x23 +#define AD713X_REG_VCMBUF_CTRL 0x24 +#define AD713X_REG_DIAGNOSTIC_CONTROL 0x25 +#define AD713X_REG_MPC_CONFIG 0x26 +#define AD713X_REG_CH0_GAIN_LSB 0x27 +#define AD713X_REG_CH0_GAIN_MID 0x28 +#define AD713X_REG_CH0_GAIN_MSB 0x29 +#define AD713X_REG_CH0_OFFSET_LSB 0x2A +#define AD713X_REG_CH0_OFFSET_MID 0x2B +#define AD713X_REG_CH0_OFFSET_MSB 0x2C +#define AD713X_REG_CH1_GAIN_LSB 0x2D +#define AD713X_REG_CH1_GAIN_MID 0x2E +#define AD713X_REG_CH1_GAIN_MSB 0x2F +#define AD713X_REG_CH1_OFFSET_LSB 0x30 +#define AD713X_REG_CH1_OFFSET_MID 0x31 +#define AD713X_REG_CH1_OFFSET_MSB 0x32 +#define AD713X_REG_CH2_GAIN_LSB 0x33 +#define AD713X_REG_CH2_GAIN_MID 0x34 +#define AD713X_REG_CH2_GAIN_MSB 0x35 +#define AD713X_REG_CH2_OFFSET_LSB 0x36 +#define AD713X_REG_CH2_OFFSET_MID 0x37 +#define AD713X_REG_CH2_OFFSET_MSB 0x38 +#define AD713X_REG_CH3_GAIN_LSB 0x39 +#define AD713X_REG_CH3_GAIN_MID 0x3A +#define AD713X_REG_CH3_GAIN_MSB 0x3B +#define AD713X_REG_CH3_OFFSET_LSB 0x3C +#define AD713X_REG_CH3_OFFSET_MID 0x3D +#define AD713X_REG_CH3_OFFSET_MSB 0x3E +#define AD713X_REG_MCLK_COUNTER 0x3F +#define AD713X_REG_DIG_FILTER_OFUF 0x40 +#define AD713X_REG_DIG_FILTER_SETTLED 0x41 +#define AD713X_REG_INTERNAL_ERROR 0x42 +#define AD713X_REG_POWER_OV_ERROR_1 0x43 +#define AD713X_REG_POWER_UV_ERROR_1 0x44 +#define AD713X_REG_POWER_OV_ERROR_2 0x45 +#define AD713X_REG_POWER_UV_ERROR_2 0x46 +#define AD713X_REG_SPI_ERROR 0x47 +#define AD713X_REG_AIN_OR_ERROR 0x48 +#define AD713X_REG_AVDD5_VALUE 0x49 +#define AD713X_REG_DVDD5_VALUE 0x4A +#define AD713X_REG_VREF_VALUE 0x4B +#define AD713X_REG_LDOIN_VALUE 0x4C +#define AD713X_REG_AVDD1V8_VALUE 0x4D +#define AD713X_REG_DVDD1V8_VALUE 0x4E +#define AD713X_REG_CLKVDD_VALUE 0x4F +#define AD713X_REG_IOVDD_VALUE 0x50 +#define AD713X_REG_TEMPERATURE_DATA 0x51 + +/* + * AD713X_REG_INTERFACE_CONFIG_A + */ +#define AD713X_INT_CONFIG_A_SOFT_RESET_MSK BIT(7) +#define AD713X_INT_CONFIG_A_ADDR_ASC_BIT_MSK BIT(5) +#define AD713X_INT_CONFIG_A_SDO_ACTIVE_BIT_MSK BIT(4) +#define AD713X_INT_CONFIG_A_SOFT_RESET_MIRR_MSK BIT(0) +#define AD713X_INT_CONFIG_A_ADDR_ASC_MIRR_MSK BIT(2) +#define AD713X_INT_CONFIG_A_SDO_ACTIVE_MIRR_MSK BIT(3) + +/* + * AD713X_REG_INTERFACE_CONFIG_B + */ +#define AD713X_INT_CONFIG_B_SINGLE_INSTR_MSK BIT(7) +#define AD713X_INT_CONFIG_B_M_S_RD_CTRL_MSK BIT(5) +#define AD713X_INT_CONFIG_B_DIG_IF_RST_MSK BIT(1) + +/* + * AD713X_REG_DEVICE_CONFIG + */ +#define AD713X_DEV_CONFIG_OP_IN_PROGRESS_MSK BIT(5) +#define AD713X_DEV_CONFIG_NO_CHIP_ERR_MSK BIT(4) +#define AD713X_DEV_CONFIG_PWR_MODE_MSK BIT(0) + +/* + * AD713X_REG_CHIP_TYPE + */ +#define AD713X_CHIP_TYPE_BITS_MSK GENMASK(7, 0) +#define AD713X_CHIP_TYPE_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_PRODUCT_ID_LSB + */ +#define AD713X_PRODUCT_ID_LSB_BITS_MSK GENMASK(7, 0) +#define AD713X_PRODUCT_ID_LSB_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_PRODUCT_ID_MSB + */ +#define AD713X_PRODUCT_ID_MSB_BITS_MSK GENMASK(7, 0) +#define AD713X_PRODUCT_ID_MSB_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CHIP_GRADE + */ +#define AD713X_CHIP_GRADE_PROD_GRADE_BITS_MSK GENMASK(7, 4) +#define AD713X_CHIP_GRADE_PROD_GRADE_BITS_MODE(x) (((x) & 0x0F) << 4) +#define AD713X_CHIP_GRADE_DEV_VERSION_BITS_MSK GENMASK(3, 0) +#define AD713X_CHIP_GRADE_DEV_VERSION_BITS_MODE(x) (((x) & 0x0F) << 0) + +/* + * AD713X_REG_CHIP_INDEX + */ +#define AD713X_SILICON_REV_ID_BITS_MSK GENMASK(7, 0) +#define AD713X_SILICON_REV_ID_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_SCRATCH_PAD + */ +#define AD713X_SCRATCH_PAD_BITS_MSK GENMASK(7, 0) +#define AD713X_SCRATCH_PAD_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_SPI_REVISION + */ +#define AD713X_SPI_REVISION_BITS_MSK GENMASK(7, 0) +#define AD713X_SPI_REVISION_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_VENDOR_ID_LSB + */ +#define AD713X_VENDOR_ID_LSB_BITS_MSK GENMASK(7, 0) +#define AD713X_VENDOR_ID_LSB_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_VENDOR_ID_MSB + */ +#define AD713X_VENDOR_ID_MSB_BITS_MSK GENMASK(7, 0) +#define AD713X_VENDOR_ID_MSB_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_STREAM_MODE + */ +#define AD713X_STREAM_MODE_BITS_MSK GENMASK(7, 0) +#define AD713X_STREAM_MODE_BITS_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_TRANSFER_REGISTER + */ +#define AD713X_TRANSFER_MASTER_SLAVE_TX_BIT_MSK BIT(0) + +/* + * AD713X_REG_DEVICE_CONFIG1 + */ +#define AD713X_DEV_CONFIG1_MPC_MAGPHA_EN_MSK BIT(6) +#define AD713X_DEV_CONFIG1_MPC_MAG_EN_MSK BIT(5) +#define AD713X_DEV_CONFIG1_AA_MODE_MSK BIT(4) +#define AD713X_DEV_CONFIG1_SDO_PIN_SRC_SEL_MSK BIT(2) +#define AD713X_DEV_CONFIG1_REF_GAIN_CORR_EN_MSK BIT(1) +#define AD713X_DEV_CONFIG1_CLKOUT_EN_MSK BIT(0) + +/* + * AD713X_REG_DATA_PACKET_CONFIG + */ +#define AD713X_DATA_PACKET_CONFIG_CRC_POLY_RST_MSK BIT(7) +#define AD713X_DATA_PACKET_CONFIG_FRAME_MSK GENMASK(6, 4) +#define AD713X_DATA_PACKET_CONFIG_FRAME_MODE(x) (((x) & 0x7) << 4) +#define AD713X_DATA_PACKET_CONFIG_DCLK_FREQ_MSK GENMASK(3, 0) +#define AD713X_DATA_PACKET_CONFIG_DCLK_FREQ_MODE(x) (((x) & 0xF) << 0) + +/* + * AD713X_REG_DIGITAL_INTERFACE_CONFIG + */ +#define AD713X_DIG_INT_CONFIG_DAISY_CHAIN_NUM_MSK GENMASK(7, 4) +#define AD713X_DIG_INT_CONFIG_DAISY_CHAIN_NUM_MODE(x) (((x) & 0xF) << 4) +#define AD713X_DIG_INT_CONFIG_AVG_SEL_MSK GENMASK(3, 2) +#define AD713X_DIG_INT_CONFIG_AVG_SEL_MODE(x) (((x) & 0x3) << 2) +#define AD713X_DIG_INT_CONFIG_FORMAT_MSK GENMASK(1, 0) +#define AD713X_DIG_INT_CONFIG_FORMAT_MODE(x) (((x) & 0x3) << 0) + +/* + * AD713X_REG_POWER_DOWN_CONTROL + */ +#define AD713X_PWRDN_CTRL_PWRDN_CH_MSK(ch) BIT(ch) +#define AD713X_PWRDN_CTRL_PWRDN_AUXADC_MSK BIT(2) +#define AD713X_PWRDN_CTRL_PWRDN_LDO_MSK BIT(1) +#define AD713X_PWRDN_CTRL_PWRDN_SLEEP_MODE_EN_MSK BIT(0) + +/* + * AD713X_REG_AIN_RANGE_SELECT + */ +#define AD713X_AIN_RANGE_SEL_CH_MSK(ch) BIT(ch) + +/* + * AD713X_REG_DEVICE_STATUS + */ +#define AD713X_DEV_STAT_DCLKMODE_MSK BIT(5) +#define AD713X_DEV_STAT_DCLKIO_MSK BIT(4) +#define AD713X_DEV_STAT_MODE_MSK BIT(3) +#define AD713X_DEV_STAT_CLKSEL_MSK BIT(2) +#define AD713X_DEV_STAT_FUSE_ECC_MSK BIT(1) +#define AD713X_DEV_STAT_PLL_LOCK_MSK BIT(0) + +/* + * AD713X_REG_ODR_VAL_INT_LSB + */ +#define AD713X_ODR_VAL_INT_LSB_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_INT_LSB_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ODR_VAL_INT_MID + */ +#define AD713X_ODR_VAL_INT_MID_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_INT_MID_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ODR_VAL_INT_MSB + */ +#define AD713X_ODR_VAL_INT_MSB_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_INT_MSB_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ODR_VAL_FLT_LSB + */ +#define AD713X_ODR_VAL_FLT_LSB_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_FLT_LSB_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ODR_VAL_FLT_MID0 + */ +#define AD713X_ODR_VAL_FLT_MID0_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_FLT_MID0_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ODR_VAL_FLT_MID1 + */ +#define AD713X_ODR_VAL_FLT_MID1_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_FLT_MID1_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ODR_VAL_FLT_MSB + */ +#define AD713X_ODR_VAL_FLT_MSB_MSK GENMASK(7, 0) +#define AD713X_ODR_VAL_FLT_MSB_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CHANNEL_ODR_SELECT + */ +#define AD713X_ODR_RATE_SEL_CH_MSK(ch) (GENMASK(1, 0) << (2 * ch)) +#define AD713X_ODR_RATE_SEL_CH_MODE(x, ch) (((x) & 0x3) << (2 * ch)) + +/* + * AD713X_REG_CHAN_DIG_FILTER_SEL + */ +#define AD713X_DIGFILTER_SEL_CH_MSK(ch) (GENMASK(1, 0) << (2 * ch)) +#define AD713X_DIGFILTER_SEL_CH_MODE(x, ch) (((x) & 0x3) << (2 * ch)) + +/* + * AD713X_REG_FIR_BW_SEL + */ +#define AD713X_FIR_BW_SEL_CH_MSK(ch) BIT(ch) + +/* + * AD713X_REG_GPIO_DIR_CTRL + */ +#define AD713X_GPIO_IO_CTRL_MSK GENMASK(7, 0) +#define AD713X_GPIO_IO_CTRL_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_GPIO_DATA + */ +#define AD713X_GPIO_DATA_MSK GENMASK(7, 0) +#define AD713X_GPIO_DATA_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_ERROR_PIN_SRC_CONTROL + */ +#define AD713X_ERR_PIN_EN_OR_AIN_MSK BIT(5) +#define AD713X_ERR_PIN_EN_INTERNAL_MSK BIT(4) +#define AD713X_ERR_PIN_EN_SPI_MSK BIT(3) +#define AD713X_ERR_PIN_EN_LDO_XOSC_MSK BIT(2) +#define AD713X_ERR_PIN_EN_TEMP_MSK BIT(1) +#define AD713X_ERR_PIN_EN_PWR_MSK BIT(0) + +/* + * AD713X_REG_ERROR_PIN_CONTROL + */ +#define AD713X_ERR_PIN_IN_STATUS_MSK BIT(2) +#define AD713X_ERR_PIN_IN_EN_MSK BIT(1) +#define AD713X_ERR_PIN_OUT_EN_MSK BIT(0) + +/* + * AD713X_REG_VCMBUF_CTRL + */ +#define AD713X_VCMBUF_CTRL_PWRDN_MSK BIT(6) +#define AD713X_VCMBUF_CTRL_REF_DIV_SEL_MSK GENMASK(5, 1) +#define AD713X_VCMBUF_CTRL_REF_DIV_SEL_MODE(x) (((x) & 0x1F) << 1) +#define AD713X_VCMBUF_CTRL_REF_SEL_MSK BIT(0) + +/* + * AD713X_REG_DIAGNOSTIC_CONTROL + */ +#define AD713X_DIAGCTRL_ERR_OR_AIN_EN_MSK BIT(5) +#define AD713X_DIAGCTRL_ERR_PWR_MON_EN_MSK BIT(4) +#define AD713X_DIAGCTRL_MCLK_CNT_EN_MSK BIT(3) +#define AD713X_DIAGCTRL_ERR_SPI_CRC_EN_MSK BIT(2) +#define AD713X_DIAGCTRL_ERR_MM_CRC_EN_MSK BIT(1) +#define AD713X_DIAGCTRL_FUSE_CRC_CHECK_MSK BIT(0) + +/* + * AD713X_REG_MPC_CONFIG + */ +#define AD713X_MPC_CLKDEL_EN_CH_MSK(ch) (GENMASK(1, 0) << (2 * ch)) +#define AD713X_MPC_CLKDEL_EN_CH_MODE(x, ch) (((x) & 0x3) << (2 * ch)) + +/* + * AD713X_REG_CHx_GAIN_LSB + */ +#define AD713X_CH_GAIN_LSB_MSK GENMASK(7, 0) +#define AD713X_CH_GAIN_LSB_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CHx_GAIN_MID + */ +#define AD713X_CH_GAIN_MID_MSK GENMASK(7, 0) +#define AD713X_CH_GAIN_MID_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CHx_GAIN_MSB + */ +#define AD713X_CH_GAIN_CAL_SEL_MSK BIT(4) +#define AD713X_CH_GAIN_MSB_MSK GENMASK(3, 0) +#define AD713X_CH_GAIN_MSB_MODE(x) (((x) & 0xF) << 0) + +/* + * AD713X_REG_CHx_OFFSET_LSB + */ +#define AD713X_CH_OFFSET_LSB_MSK GENMASK(7, 0) +#define AD713X_CH_OFFSET_LSB_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CHx_OFFSET_MID + */ +#define AD713X_CH_OFFSET_MID_MSK GENMASK(7, 0) +#define AD713X_CH_OFFSET_MID_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CHx_OFFSET_MSB + */ +#define AD713X_CH_OFFSET_CAL_EN_MSK BIT(7) +#define AD713X_CH_OFFSET_MSB_MSK GENMASK(6, 0) +#define AD713X_CH_OFFSET_MSB_MODE(x) (((x) & 0x7F) << 0) + +/* + * AD713X_REG_MCLK_COUNTER + */ +#define AD713X_MCLK_COUNT_MSK GENMASK(7, 0) +#define AD713X_MCLK_COUNT_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_DIG_FILTER_OFUF + */ +#define AD713X_DIGFILTER_ERR_OFUF_CH_MSK(ch) BIT(ch) + +/* + * AD713X_REG_DIG_FILTER_SETTLED + */ +#define AD713X_DIGFILTER_CH_SETTLED_MSK(ch) BIT(ch) + +/* + * AD713X_REG_INTERNAL_ERROR + */ +#define AD713X_INT_ERR_NO_CLOCK_MSK BIT(5) +#define AD713X_INT_ERR_TEMP_MSK BIT(4) +#define AD713X_INT_ERR_DCLK_MSK BIT(3) +#define AD713X_INT_ERR_FUSE_CRC_MSK BIT(2) +#define AD713X_INT_ERR_ASRC_MSK BIT(1) +#define AD713X_INT_ERR_MM_CRC_MSK BIT(0) + +/* + * AD713X_REG_POWER_OV_ERROR_1 + */ +#define AD713X_POWER_ERR_OV_IOVDD_MSK BIT(3) +#define AD713X_POWER_ERR_OV_CLKVDD_MSK BIT(2) +#define AD713X_POWER_ERR_OV_DVDD1V8_MSK BIT(1) +#define AD713X_POWER_ERR_OV_AVDD1V8_MSK BIT(0) + +/* + * AD713X_REG_POWER_UV_ERROR_1 + */ +#define AD713X_POWER_ERR_UV_IOVDD_MSK BIT(3) +#define AD713X_POWER_ERR_UV_CLKVDD_MSK BIT(2) +#define AD713X_POWER_ERR_UV_DVDD1V8_MSK BIT(1) +#define AD713X_POWER_ERR_UV_AVDD1V8_MSK BIT(0) + +/* + * AD713X_REG_POWER_OV_ERROR_2 + */ +#define AD713X_POWER_ERR_OV_VREF_MSK BIT(3) +#define AD713X_POWER_ERR_OV_LDOIN_MSK BIT(2) +#define AD713X_POWER_ERR_OV_DVDD5_MSK BIT(1) +#define AD713X_POWER_ERR_OV_AVDD5_MSK BIT(0) + +/* + * AD713X_REG_POWER_UV_ERROR_2 + */ +#define AD713X_POWER_ERR_UV_VREF_MSK BIT(3) +#define AD713X_POWER_ERR_UV_LDOIN_MSK BIT(2) +#define AD713X_POWER_ERR_UV_DVDD5_MSK BIT(1) +#define AD713X_POWER_ERR_UV_AVDD5_MSK BIT(0) + +/* + * AD713X_REG_SPI_ERROR + */ +#define AD713X_SPI_ERROR_CRC_MSK BIT(3) +#define AD713X_SPI_ERROR_SCLK_CNT_MSK BIT(2) +#define AD713X_SPI_ERROR_WRITE_MSK BIT(1) +#define AD713X_SPI_ERROR_READ_MSK BIT(0) + +/* + * AD713X_REG_AIN_OR_ERROR + */ +#define AD713X_ERR_OR_AIN_MSK(ch) BIT(ch) + +/* + * AD713X_REG_AVDD5_VALUE + */ +#define AD713X_AVDD5_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_AVDD5_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_DVDD5_VALUE + */ +#define AD713X_DVDD5_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_DVDD5_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_VREF_VALUE + */ +#define AD713X_VREF_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_VREF_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_LDOIN_VALUE + */ +#define AD713X_LDOIN_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_LDOIN_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_AVDD1V8_VALUE + */ +#define AD713X_AVDD1V8_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_AVDD1V8_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_DVDD1V8_VALUE + */ +#define AD713X_DVDD1V8_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_DVDD1V8_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_CLKVDD_VALUE + */ +#define AD713X_CLKVDD_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_CLKVDD_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_IOVDD_VALUE + */ +#define AD713X_IOVDD_VALUE_PIN_MSK GENMASK(7, 0) +#define AD713X_IOVDD_VALUE_PIN_MODE(x) (((x) & 0xFF) << 0) + +/* + * AD713X_REG_TEMPERATURE_DATA + */ +#define AD713X_TEMP_DATA_MSK GENMASK(7, 0) +#define AD713X_TEMP_DATA_MODE(x) (((x) & 0xFF) << 0) + +#define AD713X_REG_READ(x) ((1 << 7) | (x & 0x7F)) + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @enum ad713x_supported_dev_ids + * @brief ID of devices supported by the driver + */ +enum ad713x_supported_dev_ids { + ID_AD7132, + ID_AD7134, + ID_AD7136 +}; + +/** + * @enum ad713x_power_mode + * @brief AD713x power modes + */ +enum ad713x_power_mode { + /** Low power mode option */ + LOW_POWER, + /** Full power mode option */ + HIGH_POWER +}; + +/** + * @enum ad713x_adc_data_len + * @brief AD713x possible number of bits per data sample + */ +enum ad713x_adc_data_len { + /** 16 bit data sample */ + ADC_16_BIT_DATA, + /** 24 bit data sample */ + ADC_24_BIT_DATA, + /** 32 bit data sample */ + ADC_32_BIT_DATA, + /** To know when to stop when cycling between them */ + INVALID +}; + +/** + * @enum ad713x_crc_header + * @brief AD713x possible data CRC header choices + */ +enum ad713x_crc_header { + /** Data sample comes with no CRC attached */ + NO_CRC, + /** Data sample comes with 6-bit CRC attached */ + CRC_6, + /** Data sample comes with 8-bit CRC attached */ + CRC_8 +}; + +/** + * @enum ad713x_doutx_format + * @brief AD713x list for possible output modes + */ +enum ad713x_doutx_format { + /** Single channel Daisy-chain mode */ + SINGLE_CH_DC, + /** Dual channel Daisy-chain mode */ + DUAL_CH_DC, + /** Quad-channel parallel output mode */ + QUAD_CH_PO, + /** Channel average mode */ + CH_AVG_MODE +}; + +/** + * @enum ad713x_dig_filter_sel + * @brief AD713x list of input filters. + */ +enum ad713x_dig_filter_sel { + /** Wideband filter (Finite impulse response) */ + FIR, + /** Sinc6 filter */ + SINC6, + /** Sinc3 filter */ + SINC3, + /** Sinc3 filter with 50Hz and 60Hz rejection */ + SINC3_50_60_REJ +}; + +/** + * @enum ad713x_channels + * @brief AD713x list of channels + */ +enum ad713x_channels { + /** Channel 0 */ + CH0, + /** Channel 1 */ + CH1, + /** Channel 2 */ + CH2, + /** Channel 3 */ + CH3 +}; + +/** + * @enum ad717x_mpc_clkdel + * @brief AD713x list of clock delays + */ +enum ad717x_mpc_clkdel { + /** No delay */ + DELAY_NONE, + /** One clock cycle delay */ + DELAY_1_CLOCKS, + /** Two clock cycles delay */ + DELAY_2_CLOCKS +}; + +/** + * @struct ad713x_dev + * @brief AD713x driver handler structure + */ +struct ad713x_dev { + /** SPI layer handler. */ + struct spi_desc *spi_desc; + /** MODE GPIO handler. */ + struct gpio_desc *gpio_mode; + /** DCLKMODE GPIO handler. */ + struct gpio_desc *gpio_dclkmode; + /** DCLKIO GPIO handler. */ + struct gpio_desc *gpio_dclkio; + /** RESET GPIO handler. */ + struct gpio_desc *gpio_resetn; + /** PDN GPIO handler. */ + struct gpio_desc *gpio_pnd; + /** ID of supported device. */ + enum ad713x_supported_dev_ids dev_id; + /** Length of data in bits. */ + enum ad713x_adc_data_len adc_data_len; + /** CRC option. */ + enum ad713x_crc_header crc_header; +}; + +/** + * @struct ad713x_init_param + * @brief AD713x driver initialization structure + */ +struct ad713x_init_param { + /** SPI layer initialization structure. */ + struct spi_init_param spi_init_prm; + /** MODE GPIO initialization structure. */ + struct gpio_init_param *gpio_mode; + /** DCLKMODE GPIO initialization structure. */ + struct gpio_init_param *gpio_dclkmode; + /** DCLKIO GPIO initialization structure. */ + struct gpio_init_param *gpio_dclkio; + /** RESET GPIO initialization structure. */ + struct gpio_init_param *gpio_resetn; + /** PDN GPIO initialization structure. */ + struct gpio_init_param *gpio_pnd; + /** MODE GPIO starting value */ + bool mode_master_nslave; + /** DCLKMODE GPIO starting value */ + bool dclkmode_free_ngated; + /** DCLKIO GPIO starting value */ + bool dclkio_out_nin; + /** PDN GPIO starting value */ + bool pnd; + /** ID of supported device. */ + enum ad713x_supported_dev_ids dev_id; + /** Length of data in bits. */ + enum ad713x_adc_data_len adc_data_len; + /** CRC option. */ + enum ad713x_crc_header crc_header; + enum ad713x_doutx_format format; + /** Clock delay state. */ + bool clk_delay_en; + /** SPI layer handler if the SPI bus is shared with another device. In this + * case the SPI should not be initialized again. */ + struct spi_desc *spi_common_dev; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/** Read from device. */ +int32_t ad713x_spi_reg_read(struct ad713x_dev *dev, uint8_t reg_addr, + uint8_t *reg_data); + +/** Write to device. */ +int32_t ad713x_spi_reg_write(struct ad713x_dev *dev, uint8_t reg_addr, + uint8_t reg_data); + +/** SPI write to device using a mask. */ +int32_t ad713x_spi_write_mask(struct ad713x_dev *dev, uint8_t reg_addr, + uint32_t mask, uint8_t data); + +/** Device power mode control. */ +int32_t ad713x_set_power_mode(struct ad713x_dev *dev, + enum ad713x_power_mode mode); + +/** ADC conversion data output frame control. */ +int32_t ad713x_set_out_data_frame(struct ad713x_dev *dev, + enum ad713x_adc_data_len adc_data_len, + enum ad713x_crc_header crc_header); + +/** DOUTx output format configuration. */ +int32_t ad713x_dout_format_config(struct ad713x_dev *dev, + enum ad713x_doutx_format format); + +/** Magnitude and phase matching calibration clock delay enable for all channels + * at 2 clock delay. */ +int32_t ad713x_mag_phase_clk_delay(struct ad713x_dev *dev, bool clk_delay_en); + +/** Digital filter type selection for each channel. */ +int32_t ad713x_dig_filter_sel_ch(struct ad713x_dev *dev, + enum ad713x_dig_filter_sel filter, enum ad713x_channels ch); + +/** Enable/Disable CLKOUT output. */ +int32_t ad713x_clkout_output_en(struct ad713x_dev *dev, bool enable); + +/** Enable/Disable reference gain correction. */ +int32_t ad713x_ref_gain_correction_en(struct ad713x_dev *dev, bool enable); + +/** Select the wideband filter bandwidth for a channel. */ +int32_t ad713x_wideband_bw_sel(struct ad713x_dev *dev, + enum ad713x_channels ch, uint8_t wb_opt); + +/** Initialize the device. */ +int32_t ad713x_init(struct ad713x_dev **device, + struct ad713x_init_param *init_param); + +/** Free the resources allocated by ad713x_init(). */ +int32_t ad713x_remove(struct ad713x_dev *dev); + +#endif /* SRC_AD713X_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad411x_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,127 @@ +/***************************************************************************//** +* @file ad411x_regs.h +* @brief ad4111 Registers Definitions. +* @author Andrei Drimbarean (andrei.drimbarean@analog.com) +******************************************************************************** +* Copyright 2018(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef AD4111_CFG_H_ +#define AD4111_CFG_H_ + +#include "ad717x.h" + +ad717x_st_reg ad4111_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { + AD717X_ADCMODE_REG, + AD717X_ADCMODE_REG_MODE(0), + 2 + }, + { AD717X_IFMODE_REG, 0x0000, 2 }, + { AD717X_REGCHECK_REG, 0x0000, 3 }, + { AD717X_DATA_REG, 0x0000, 3 }, + { + AD717X_GPIOCON_REG, + AD717X_GPIOCON_REG_SYNC_EN, + 2 + }, + { AD717X_ID_REG, 0x0000, 2 }, + { AD717X_CHMAP0_REG, 0x0000, 2 }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_CHMAP4_REG, 0x0000, 2 }, + { AD717X_CHMAP5_REG, 0x0000, 2 }, + { AD717X_CHMAP6_REG, 0x0000, 2 }, + { AD717X_CHMAP7_REG, 0x0000, 2 }, + { AD717X_CHMAP8_REG, 0x0000, 2 }, + { AD717X_CHMAP9_REG, 0x0000, 2 }, + { AD717X_CHMAP10_REG, 0x0000, 2 }, + { AD717X_CHMAP11_REG, 0x0000, 2 }, + { AD717X_CHMAP12_REG, 0x0000, 2 }, + { AD717X_CHMAP13_REG, 0x0000, 2 }, + { AD717X_CHMAP14_REG, 0x0000, 2 }, + { AD717X_CHMAP15_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON1_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON2_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON3_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON4_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON5_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON6_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { AD717X_SETUPCON7_REG, 0x0000 | AD717X_SETUP_CONF_REG_REF_SEL(2), 2 }, + { + AD717X_FILTCON0_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON1_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON2_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON3_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON4_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON5_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON6_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + { + AD717X_FILTCON7_REG, AD717X_FILT_CONF_REG_ENHFILT(2), 2 + }, + {AD717X_OFFSET0_REG, 0, 3 }, + {AD717X_OFFSET1_REG, 0, 3 }, + {AD717X_OFFSET2_REG, 0, 3 }, + {AD717X_OFFSET3_REG, 0, 3 }, + {AD717X_OFFSET4_REG, 0, 3 }, + {AD717X_OFFSET5_REG, 0, 3 }, + {AD717X_OFFSET6_REG, 0, 3 }, + {AD717X_OFFSET7_REG, 0, 3 }, + {AD717X_GAIN0_REG, 0, 3 }, + {AD717X_GAIN1_REG, 0, 3 }, + {AD717X_GAIN2_REG, 0, 3 }, + {AD717X_GAIN3_REG, 0, 3 }, + {AD717X_GAIN4_REG, 0, 3 }, + {AD717X_GAIN5_REG, 0, 3 }, + {AD717X_GAIN6_REG, 0, 3 }, + {AD717X_GAIN7_REG, 0, 3 }, +}; + +#endif /* AD4111_CFG_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad7172_2_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,105 @@ +/**************************************************************************//** +* @file AD7172_2_regs.h +* @brief AD7172_2 Registers Definitions. +* @author dnechita (dan.nechita@analog.com) +* +******************************************************************************* +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD7172_2_REGS_H__ +#define __AD7172_2_REGS_H__ + +#include "ad717x.h" + +#ifdef AD7172_2_INIT +/*! Array holding the info for the AD7172_2 registers - address, initial value, size */ +ad717x_st_reg ad7172_2_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { AD717X_ADCMODE_REG, 0x0000, 2 }, + { + AD717X_IFMODE_REG, + AD717X_IFMODE_REG_DOUT_RESET | AD717X_IFMODE_REG_CRC_EN, + 2 + }, + { AD717X_REGCHECK_REG, 0x0000, 3}, + { AD717X_DATA_REG, 0x0000, 3 }, + { AD717X_GPIOCON_REG, 0x0000, 2 }, + { AD717X_ID_REG, 0x0000, 2 }, + { + AD717X_CHMAP0_REG, + AD717X_CHMAP_REG_CH_EN | AD717X_CHMAP_REG_AINNEG(2), + 2 + }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000, 2 }, + { AD717X_SETUPCON1_REG, 0x0000, 2 }, + { AD717X_SETUPCON2_REG, 0x0000, 2 }, + { AD717X_SETUPCON3_REG, 0x0000, 2 }, + { + AD717X_FILTCON0_REG, + AD717X_FILT_CONF_REG_ENHFILT(2) | AD717X_FILT_CONF_REG_ODR(10), + 2 + }, + { + AD717X_FILTCON1_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON2_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON3_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { AD717X_OFFSET0_REG, 0, 3 }, + { AD717X_OFFSET1_REG, 0, 3 }, + { AD717X_OFFSET2_REG, 0, 3 }, + { AD717X_OFFSET3_REG, 0, 3 }, + { AD717X_GAIN0_REG, 0, 3 }, + { AD717X_GAIN1_REG, 0, 3 }, + { AD717X_GAIN2_REG, 0, 3 }, + { AD717X_GAIN3_REG, 0, 3 }, +}; +#endif + +#define AD7172_2_SLAVE_ID 1 + +#endif /*__AD7172_2_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad7172_4_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,141 @@ +/**************************************************************************//** +* @file AD7172_4_regs.h +* @brief AD7172_4 Registers Definitions. +* @author dnechita (dan.nechita@analog.com) +* +******************************************************************************* +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD7172_4_REGS_H__ +#define __AD7172_4_REGS_H__ + +#include "ad717x.h" + +#ifdef AD7172_4_INIT +/*! Array holding the info for the AD7172_4 registers - address, initial value, size */ +ad717x_st_reg ad7172_4_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { AD717X_ADCMODE_REG, 0x0000, 2 }, + { + AD717X_IFMODE_REG, + AD717X_IFMODE_REG_DOUT_RESET | AD717X_IFMODE_REG_CRC_EN, + 2 + }, + { AD717X_REGCHECK_REG, 0x0000, 3 }, + { AD717X_DATA_REG, 0x0000, 3 }, + { AD717X_GPIOCON_REG, 0x0000, 2 }, + { AD717X_ID_REG, 0x0000, 2 }, + { + AD717X_CHMAP0_REG, + AD717X_CHMAP_REG_CH_EN | AD717X_CHMAP_REG_AINNEG(2), + 2 + }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_CHMAP4_REG, 0x0000, 2 }, + { AD717X_CHMAP5_REG, 0x0000, 2 }, + { AD717X_CHMAP6_REG, 0x0000, 2 }, + { AD717X_CHMAP7_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000, 2 }, + { AD717X_SETUPCON1_REG, 0x0000, 2 }, + { AD717X_SETUPCON2_REG, 0x0000, 2 }, + { AD717X_SETUPCON3_REG, 0x0000, 2 }, + { AD717X_SETUPCON4_REG, 0x0000, 2 }, + { AD717X_SETUPCON5_REG, 0x0000, 2 }, + { AD717X_SETUPCON6_REG, 0x0000, 2 }, + { AD717X_SETUPCON7_REG, 0x0000, 2 }, + { + AD717X_FILTCON0_REG, + AD717X_FILT_CONF_REG_ENHFILT(2) | AD717X_FILT_CONF_REG_ODR(10), + 2 + }, + { + AD717X_FILTCON1_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON2_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON3_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON4_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON5_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON6_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON7_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + {AD717X_OFFSET0_REG, 0, 3 }, + {AD717X_OFFSET1_REG, 0, 3 }, + {AD717X_OFFSET2_REG, 0, 3 }, + {AD717X_OFFSET3_REG, 0, 3 }, + {AD717X_OFFSET4_REG, 0, 3 }, + {AD717X_OFFSET5_REG, 0, 3 }, + {AD717X_OFFSET6_REG, 0, 3 }, + {AD717X_OFFSET7_REG, 0, 3 }, + {AD717X_GAIN0_REG, 0, 3 }, + {AD717X_GAIN1_REG, 0, 3 }, + {AD717X_GAIN2_REG, 0, 3 }, + {AD717X_GAIN3_REG, 0, 3 }, + {AD717X_GAIN5_REG, 0, 3 }, + {AD717X_GAIN6_REG, 0, 3 }, + {AD717X_GAIN7_REG, 0, 3 }, + {AD717X_GAIN8_REG, 0, 3 }, +}; +#endif + +#define AD7172_4_SLAVE_ID 1 + +#endif /*__AD7172_4_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad7173_8_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,149 @@ +/**************************************************************************//** +* @file AD7173_8_regs.h +* @brief AD7173_8 Registers Definitions. +* @author dnechita (dan.nechita@analog.com) +* +******************************************************************************* +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD7173_8_REGS_H__ +#define __AD7173_8_REGS_H__ + +#include "ad717x.h" + +#ifdef AD7173_8_INIT +/*! Array holding the info for the AD7173_8 registers - address, initial value, size */ +ad717x_st_reg ad7173_8_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { AD717X_ADCMODE_REG, 0x0000, 2 }, + { + AD717X_IFMODE_REG, + AD717X_IFMODE_REG_DOUT_RESET | AD717X_IFMODE_REG_CRC_EN, + 2 + }, + { AD717X_REGCHECK_REG, 0x0000, 3 }, + { AD717X_DATA_REG, 0x0000, 3 }, + { AD717X_GPIOCON_REG, 0x0000, 2 }, + { AD717X_ID_REG, 0x0000, 2 }, + { + AD717X_CHMAP0_REG, + AD717X_CHMAP_REG_CH_EN | AD717X_CHMAP_REG_AINNEG(2), + 2 + }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_CHMAP4_REG, 0x0000, 2 }, + { AD717X_CHMAP5_REG, 0x0000, 2 }, + { AD717X_CHMAP6_REG, 0x0000, 2 }, + { AD717X_CHMAP7_REG, 0x0000, 2 }, + { AD717X_CHMAP8_REG, 0x0000, 2 }, + { AD717X_CHMAP9_REG, 0x0000, 2 }, + { AD717X_CHMAP10_REG, 0x0000, 2 }, + { AD717X_CHMAP11_REG, 0x0000, 2 }, + { AD717X_CHMAP12_REG, 0x0000, 2 }, + { AD717X_CHMAP13_REG, 0x0000, 2 }, + { AD717X_CHMAP14_REG, 0x0000, 2 }, + { AD717X_CHMAP15_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000, 2 }, + { AD717X_SETUPCON1_REG, 0x0000, 2 }, + { AD717X_SETUPCON2_REG, 0x0000, 2 }, + { AD717X_SETUPCON3_REG, 0x0000, 2 }, + { AD717X_SETUPCON4_REG, 0x0000, 2 }, + { AD717X_SETUPCON5_REG, 0x0000, 2 }, + { AD717X_SETUPCON6_REG, 0x0000, 2 }, + { AD717X_SETUPCON7_REG, 0x0000, 2 }, + { + AD717X_FILTCON0_REG, + AD717X_FILT_CONF_REG_ENHFILT(2) | AD717X_FILT_CONF_REG_ODR(10), + 2 + }, + { + AD717X_FILTCON1_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON2_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON3_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON4_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON5_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON6_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON7_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + {AD717X_OFFSET0_REG, 0, 3 }, + {AD717X_OFFSET1_REG, 0, 3 }, + {AD717X_OFFSET2_REG, 0, 3 }, + {AD717X_OFFSET3_REG, 0, 3 }, + {AD717X_OFFSET4_REG, 0, 3 }, + {AD717X_OFFSET5_REG, 0, 3 }, + {AD717X_OFFSET6_REG, 0, 3 }, + {AD717X_OFFSET7_REG, 0, 3 }, + {AD717X_GAIN0_REG, 0, 3 }, + {AD717X_GAIN1_REG, 0, 3 }, + {AD717X_GAIN2_REG, 0, 3 }, + {AD717X_GAIN3_REG, 0, 3 }, + {AD717X_GAIN5_REG, 0, 3 }, + {AD717X_GAIN6_REG, 0, 3 }, + {AD717X_GAIN7_REG, 0, 3 }, + {AD717X_GAIN8_REG, 0, 3 }, +}; +#endif + +#define AD7173_8_SLAVE_ID 1 + +#endif /*__AD7173_8_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad7175_2_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,105 @@ +/**************************************************************************//** +* @file AD7175_2_regs.h +* @brief AD7175_2 Registers Definitions. +* @author dnechita (dan.nechita@analog.com) +* +******************************************************************************* +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD7175_2_REGS_H__ +#define __AD7175_2_REGS_H__ + +#include "ad717x.h" + +#ifdef AD7175_2_INIT +/*! Array holding the info for the AD7175_2 registers - address, initial value, size */ +ad717x_st_reg ad7175_2_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { AD717X_ADCMODE_REG, 0x0000, 2 }, + { + AD717X_IFMODE_REG, + AD717X_IFMODE_REG_DOUT_RESET | AD717X_IFMODE_REG_CRC_EN, + 2 + }, + { AD717X_REGCHECK_REG, 0x0000, 3}, + { AD717X_DATA_REG, 0x0000, 3 }, + { AD717X_GPIOCON_REG, 0x0000, 2 }, + { AD717X_ID_REG, 0x0000, 2 }, + { + AD717X_CHMAP0_REG, + AD717X_CHMAP_REG_CH_EN | AD717X_CHMAP_REG_AINNEG(2), + 2 + }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000, 2 }, + { AD717X_SETUPCON1_REG, 0x0000, 2 }, + { AD717X_SETUPCON2_REG, 0x0000, 2 }, + { AD717X_SETUPCON3_REG, 0x0000, 2 }, + { + AD717X_FILTCON0_REG, + AD717X_FILT_CONF_REG_ENHFILT(2) | AD717X_FILT_CONF_REG_ODR(10), + 2 + }, + { + AD717X_FILTCON1_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON2_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON3_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { AD717X_OFFSET0_REG, 0, 3 }, + { AD717X_OFFSET1_REG, 0, 3 }, + { AD717X_OFFSET2_REG, 0, 3 }, + { AD717X_OFFSET3_REG, 0, 3 }, + { AD717X_GAIN0_REG, 0, 3 }, + { AD717X_GAIN1_REG, 0, 3 }, + { AD717X_GAIN2_REG, 0, 3 }, + { AD717X_GAIN3_REG, 0, 3 }, +}; +#endif + +#define AD7175_2_SLAVE_ID 1 + +#endif /*__AD7175_2_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad7175_8_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,149 @@ +/**************************************************************************//** +* @file AD7175_8_regs.h +* @brief AD7175_8 Registers Definitions. +* @author dnechita (dan.nechita@analog.com) +* +******************************************************************************* +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD7175_8_REGS_H__ +#define __AD7175_8_REGS_H__ + +#include "ad717x.h" + +#ifdef AD7175_8_INIT +/*! Array holding the info for the AD7175_8 registers - address, initial value, size */ +ad717x_st_reg ad7175_8_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { AD717X_ADCMODE_REG, 0x0000, 2 }, + { + AD717X_IFMODE_REG, + AD717X_IFMODE_REG_DOUT_RESET | AD717X_IFMODE_REG_CRC_EN, + 2 + }, + { AD717X_REGCHECK_REG, 0x0000, 3 }, + { AD717X_DATA_REG, 0x0000, 3 }, + { AD717X_GPIOCON_REG, 0x0000, 2 }, + { AD717X_ID_REG, 0x0000, 2 }, + { + AD717X_CHMAP0_REG, + AD717X_CHMAP_REG_CH_EN | AD717X_CHMAP_REG_AINNEG(2), + 2 + }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_CHMAP4_REG, 0x0000, 2 }, + { AD717X_CHMAP5_REG, 0x0000, 2 }, + { AD717X_CHMAP6_REG, 0x0000, 2 }, + { AD717X_CHMAP7_REG, 0x0000, 2 }, + { AD717X_CHMAP8_REG, 0x0000, 2 }, + { AD717X_CHMAP9_REG, 0x0000, 2 }, + { AD717X_CHMAP10_REG, 0x0000, 2 }, + { AD717X_CHMAP11_REG, 0x0000, 2 }, + { AD717X_CHMAP12_REG, 0x0000, 2 }, + { AD717X_CHMAP13_REG, 0x0000, 2 }, + { AD717X_CHMAP14_REG, 0x0000, 2 }, + { AD717X_CHMAP15_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000, 2 }, + { AD717X_SETUPCON1_REG, 0x0000, 2 }, + { AD717X_SETUPCON2_REG, 0x0000, 2 }, + { AD717X_SETUPCON3_REG, 0x0000, 2 }, + { AD717X_SETUPCON4_REG, 0x0000, 2 }, + { AD717X_SETUPCON5_REG, 0x0000, 2 }, + { AD717X_SETUPCON6_REG, 0x0000, 2 }, + { AD717X_SETUPCON7_REG, 0x0000, 2 }, + { + AD717X_FILTCON0_REG, + AD717X_FILT_CONF_REG_ENHFILT(2) | AD717X_FILT_CONF_REG_ODR(10), + 2 + }, + { + AD717X_FILTCON1_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON2_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON3_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON4_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON5_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON6_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON7_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + {AD717X_OFFSET0_REG, 0, 3 }, + {AD717X_OFFSET1_REG, 0, 3 }, + {AD717X_OFFSET2_REG, 0, 3 }, + {AD717X_OFFSET3_REG, 0, 3 }, + {AD717X_OFFSET4_REG, 0, 3 }, + {AD717X_OFFSET5_REG, 0, 3 }, + {AD717X_OFFSET6_REG, 0, 3 }, + {AD717X_OFFSET7_REG, 0, 3 }, + {AD717X_GAIN0_REG, 0, 3 }, + {AD717X_GAIN1_REG, 0, 3 }, + {AD717X_GAIN2_REG, 0, 3 }, + {AD717X_GAIN3_REG, 0, 3 }, + {AD717X_GAIN4_REG, 0, 3 }, + {AD717X_GAIN5_REG, 0, 3 }, + {AD717X_GAIN6_REG, 0, 3 }, + {AD717X_GAIN7_REG, 0, 3 }, +}; +#endif + +#define AD7175_8_SLAVE_ID 1 + +#endif /*__AD7175_8_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad7176_2_regs.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,106 @@ +/**************************************************************************//** +* @file AD7176_2_regs.h +* @brief AD7176_2 Registers Definitions. +* @author acozma (andrei.cozma@analog.com) +* dnechita (dan.nechita@analog.com) +* +******************************************************************************* +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD7176_2_REGS_H__ +#define __AD7176_2_REGS_H__ + +#include "ad717x.h" + +#ifdef AD7176_2_INIT +/*! Array holding the info for the AD7176_2 registers - address, initial value, size */ +ad717x_st_reg ad7176_2_regs[] = { + { AD717X_STATUS_REG, 0x00, 1 }, + { AD717X_ADCMODE_REG, 0x0000, 2 }, + { + AD717X_IFMODE_REG, + AD717X_IFMODE_REG_DOUT_RESET | AD717X_IFMODE_REG_CRC_EN, + 2 + }, + { AD717X_REGCHECK_REG, 0x0000, 3 }, + { AD717X_DATA_REG, 0x0000, 3 }, + { AD717X_GPIOCON_REG, 0x0000, 2 }, + { AD717X_ID_REG, 0x0000, 2 }, + { + AD717X_CHMAP0_REG, + AD717X_CHMAP_REG_CH_EN | AD717X_CHMAP_REG_AINNEG(2), + 2 + }, + { AD717X_CHMAP1_REG, 0x0000, 2 }, + { AD717X_CHMAP2_REG, 0x0000, 2 }, + { AD717X_CHMAP3_REG, 0x0000, 2 }, + { AD717X_SETUPCON0_REG, 0x0000, 2 }, + { AD717X_SETUPCON1_REG, 0x0000, 2 }, + { AD717X_SETUPCON2_REG, 0x0000, 2 }, + { AD717X_SETUPCON3_REG, 0x0000, 2 }, + { + AD717X_FILTCON0_REG, + AD717X_FILT_CONF_REG_ENHFILT(2) | AD717X_FILT_CONF_REG_ODR(10), + 2 + }, + { + AD717X_FILTCON1_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON2_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { + AD717X_FILTCON3_REG, + AD717X_FILT_CONF_REG_ENHFILT(2), + 2 + }, + { AD717X_OFFSET0_REG, 0, 3 }, + { AD717X_OFFSET1_REG, 0, 3 }, + { AD717X_OFFSET2_REG, 0, 3 }, + { AD717X_OFFSET3_REG, 0, 3 }, + { AD717X_GAIN0_REG, 0, 3 }, + { AD717X_GAIN1_REG, 0, 3 }, + { AD717X_GAIN2_REG, 0, 3 }, + { AD717X_GAIN3_REG, 0, 3 }, +}; +#endif + +#define AD7176_2_SLAVE_ID 1 + +#endif /*__AD7176_2_REGS_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad717x.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,506 @@ +/***************************************************************************//** +* @file AD717X.c +* @brief AD717X implementation file. +* Devices: AD7172-2, AD7172-4, AD7173-8, AD7175-2, AD7175-8, AD7176-2 +* AD7177-2, AD4111, AD4112, AD4114, AD4115 +* @author acozma (andrei.cozma@analog.com) +* dnechita (dan.nechita@analog.com) +* +******************************************************************************** +* Copyright 2015(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdlib.h> +#include "ad717x.h" + +/* Error codes */ +#define INVALID_VAL -1 /* Invalid argument */ +#define COMM_ERR -2 /* Communication error on receive */ +#define TIMEOUT -3 /* A timeout has occured */ + +/***************************************************************************//** +* @brief Searches through the list of registers of the driver instance and +* retrieves a pointer to the register that matches the given address. +* +* @param device - The handler of the instance of the driver. +* @param reg_address - The address to be used to find the register. +* +* @return A pointer to the register if found or 0. +*******************************************************************************/ +ad717x_st_reg *AD717X_GetReg(ad717x_dev *device, + uint8_t reg_address) +{ + uint8_t i; + ad717x_st_reg *reg = 0; + + if (!device || !device->regs) + return 0; + + for (i = 0; i < device->num_regs; i++) { + if (device->regs[i].addr == reg_address) { + reg = &device->regs[i]; + break; + } + } + + return reg; +} + +/***************************************************************************//** +* @brief Reads the value of the specified register. +* +* @param device - The handler of the instance of the driver. +* @param addr - The address of the register to be read. The value will be stored +* inside the register structure that holds info about this register. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_ReadRegister(ad717x_dev *device, + uint8_t addr) +{ + int32_t ret = 0; + uint8_t buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint8_t i = 0; + uint8_t check8 = 0; + uint8_t msgBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + ad717x_st_reg *pReg; + + if(!device) + return INVALID_VAL; + + pReg = AD717X_GetReg(device, addr); + if (!pReg) + return INVALID_VAL; + + /* Build the Command word */ + buffer[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD | + AD717X_COMM_REG_RA(pReg->addr); + + /* Read data from the device */ + ret = spi_write_and_read(device->spi_desc, + buffer, + ((device->useCRC != AD717X_DISABLE) ? pReg->size + 1 + : pReg->size) + 1); + if(ret < 0) + return ret; + + /* Check the CRC */ + if(device->useCRC == AD717X_USE_CRC) { + msgBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD | + AD717X_COMM_REG_RA(pReg->addr); + for(i = 1; i < pReg->size + 2; ++i) { + msgBuf[i] = buffer[i]; + } + check8 = AD717X_ComputeCRC8(msgBuf, pReg->size + 2); + } + if(device->useCRC == AD717X_USE_XOR) { + msgBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_RD | + AD717X_COMM_REG_RA(pReg->addr); + for(i = 1; i < pReg->size + 2; ++i) { + msgBuf[i] = buffer[i]; + } + check8 = AD717X_ComputeXOR8(msgBuf, pReg->size + 2); + } + + if(check8 != 0) { + /* ReadRegister checksum failed. */ + return COMM_ERR; + } + + /* Build the result */ + pReg->value = 0; + for(i = 1; i < pReg->size + 1; i++) { + pReg->value <<= 8; + pReg->value += buffer[i]; + } + + return ret; +} + +/***************************************************************************//** +* @brief Writes the value of the specified register. +* +* @param device - The handler of the instance of the driver. +* @param addr - The address of the register to be written with the value stored +* inside the register structure that holds info about this +* register. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_WriteRegister(ad717x_dev *device, + uint8_t addr) +{ + int32_t ret = 0; + int32_t regValue = 0; + uint8_t wrBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0}; + uint8_t i = 0; + uint8_t crc8 = 0; + ad717x_st_reg *preg; + + if(!device) + return INVALID_VAL; + + preg = AD717X_GetReg(device, addr); + if (!preg) + return INVALID_VAL; + + /* Build the Command word */ + wrBuf[0] = AD717X_COMM_REG_WEN | AD717X_COMM_REG_WR | + AD717X_COMM_REG_RA(preg->addr); + + /* Fill the write buffer */ + regValue = preg->value; + for(i = 0; i < preg->size; i++) { + wrBuf[preg->size - i] = regValue & 0xFF; + regValue >>= 8; + } + + /* Compute the CRC */ + if(device->useCRC != AD717X_DISABLE) { + crc8 = AD717X_ComputeCRC8(wrBuf, preg->size + 1); + wrBuf[preg->size + 1] = crc8; + } + + /* Write data to the device */ + ret = spi_write_and_read(device->spi_desc, + wrBuf, + (device->useCRC != AD717X_DISABLE) ? + preg->size + 2 : preg->size + 1); + + return ret; +} + +/***************************************************************************//** +* @brief Resets the device. +* +* @param device - The handler of the instance of the driver. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_Reset(ad717x_dev *device) +{ + int32_t ret = 0; + uint8_t wrBuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + + if(!device) + return INVALID_VAL; + + ret = spi_write_and_read(device->spi_desc, + wrBuf, + 8); + + return ret; +} + +/***************************************************************************//** +* @brief Waits until a new conversion result is available. +* +* @param device - The handler of the instance of the driver. +* @param timeout - Count representing the number of polls to be done until the +* function returns if no new data is available. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_WaitForReady(ad717x_dev *device, + uint32_t timeout) +{ + ad717x_st_reg *statusReg; + int32_t ret; + int8_t ready = 0; + + if(!device || !device->regs) + return INVALID_VAL; + + statusReg = AD717X_GetReg(device, AD717X_STATUS_REG); + if (!statusReg) + return INVALID_VAL; + + while(!ready && --timeout) { + /* Read the value of the Status Register */ + ret = AD717X_ReadRegister(device, AD717X_STATUS_REG); + if(ret < 0) + return ret; + + /* Check the RDY bit in the Status Register */ + ready = (statusReg->value & AD717X_STATUS_REG_RDY) == 0; + } + + return timeout ? 0 : TIMEOUT; +} + +/***************************************************************************//** +* @brief Reads the conversion result from the device. +* +* @param device - The handler of the instance of the driver. +* @param pData - Pointer to store the read data. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_ReadData(ad717x_dev *device, + int32_t* pData) +{ + ad717x_st_reg *dataReg; + int32_t ret; + + if(!device || !device->regs) + return INVALID_VAL; + + dataReg = AD717X_GetReg(device, AD717X_DATA_REG); + if (!dataReg) + return INVALID_VAL; + + /* Update the data register length with respect to device and options */ + ret = AD717X_ComputeDataregSize(device); + + /* Read the value of the Status Register */ + ret |= AD717X_ReadRegister(device, AD717X_DATA_REG); + + /* Get the read result */ + *pData = dataReg->value; + + return ret; +} + +/***************************************************************************//** +* @brief Computes data register read size to account for bit number and status +* read. +* +* @param device - The handler of the instance of the driver. +* +* @return 0in case of success or negative code in case of failure. +*******************************************************************************/ +int32_t AD717X_ComputeDataregSize(ad717x_dev *device) +{ + ad717x_st_reg *reg_ptr; + ad717x_st_reg *datareg_ptr; + uint16_t case_var; + + /* Get interface mode register pointer */ + reg_ptr = AD717X_GetReg(device, AD717X_IFMODE_REG); + /* Get data register pointer */ + datareg_ptr = AD717X_GetReg(device, AD717X_DATA_REG); + case_var = reg_ptr->value & (AD717X_IFMODE_REG_DATA_STAT | + AD717X_IFMODE_REG_DATA_WL16); + + /* Compute data register size */ + datareg_ptr->size = 3; + if ((case_var & AD717X_IFMODE_REG_DATA_WL16) == AD717X_IFMODE_REG_DATA_WL16) + datareg_ptr->size--; + if ((case_var & AD717X_IFMODE_REG_DATA_STAT) == AD717X_IFMODE_REG_DATA_STAT) + datareg_ptr->size++; + + /* Get ID register pointer */ + reg_ptr = AD717X_GetReg(device, AD717X_ID_REG); + + /* If the part is 32/24 bit wide add a byte to the read */ + if((reg_ptr->value & AD717X_ID_REG_MASK) == AD7177_2_ID_REG_VALUE) + datareg_ptr->size++; + + return 0; +} + +/***************************************************************************//** +* @brief Computes the CRC checksum for a data buffer. +* +* @param pBuf - Data buffer +* @param bufSize - Data buffer size in bytes +* +* @return Returns the computed CRC checksum. +*******************************************************************************/ +uint8_t AD717X_ComputeCRC8(uint8_t * pBuf, + uint8_t bufSize) +{ + uint8_t i = 0; + uint8_t crc = 0; + + while(bufSize) { + for(i = 0x80; i != 0; i >>= 1) { + if(((crc & 0x80) != 0) != ((*pBuf & i) != + 0)) { /* MSB of CRC register XOR input Bit from Data */ + crc <<= 1; + crc ^= AD717X_CRC8_POLYNOMIAL_REPRESENTATION; + } else { + crc <<= 1; + } + } + pBuf++; + bufSize--; + } + return crc; +} + +/***************************************************************************//** +* @brief Computes the XOR checksum for a data buffer. +* +* @param pBuf - Data buffer +* @param bufSize - Data buffer size in bytes +* +* @return Returns the computed XOR checksum. +*******************************************************************************/ +uint8_t AD717X_ComputeXOR8(uint8_t * pBuf, + uint8_t bufSize) +{ + uint8_t xor = 0; + + while(bufSize) { + xor ^= *pBuf; + pBuf++; + bufSize--; + } + return xor; +} + +/***************************************************************************//** +* @brief Updates the CRC settings. +* +* @param device - The handler of the instance of the driver. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_UpdateCRCSetting(ad717x_dev *device) +{ + ad717x_st_reg *interfaceReg; + + if(!device || !device->regs) + return INVALID_VAL; + + interfaceReg = AD717X_GetReg(device, AD717X_IFMODE_REG); + if (!interfaceReg) + return INVALID_VAL; + + /* Get CRC State. */ + if(AD717X_IFMODE_REG_CRC_STAT(interfaceReg->value)) { + device->useCRC = AD717X_USE_CRC; + } else if(AD717X_IFMODE_REG_XOR_STAT(interfaceReg->value)) { + device->useCRC = AD717X_USE_XOR; + } else { + device->useCRC = AD717X_DISABLE; + } + + return 0; +} + +/***************************************************************************//** +* @brief Initializes the AD717X. +* +* @param device - The device structure. +* @param init_param - The structure that contains the device initial +* parameters. +* +* @return Returns 0 for success or negative error code. +*******************************************************************************/ +int32_t AD717X_Init(ad717x_dev **device, + ad717x_init_param init_param) +{ + ad717x_dev *dev; + int32_t ret; + ad717x_st_reg *preg; + + dev = (ad717x_dev *)malloc(sizeof(*dev)); + if (!dev) + return -1; + + dev->regs = init_param.regs; + dev->num_regs = init_param.num_regs; + + /* Initialize the SPI communication. */ + ret = spi_init(&dev->spi_desc, &init_param.spi_init); + if (ret < 0) + return ret; + + /* Reset the device interface.*/ + ret = AD717X_Reset(dev); + if (ret < 0) + return ret; + + /* Initialize ADC mode register. */ + ret = AD717X_WriteRegister(dev, AD717X_ADCMODE_REG); + if(ret < 0) + return ret; + + /* Initialize Interface mode register. */ + ret = AD717X_WriteRegister(dev, AD717X_IFMODE_REG); + if(ret < 0) + return ret; + + /* Get CRC State */ + ret = AD717X_UpdateCRCSetting(dev); + if(ret < 0) + return ret; + + /* Initialize registers AD717X_GPIOCON_REG through AD717X_OFFSET0_REG */ + preg = AD717X_GetReg(dev, AD717X_GPIOCON_REG); + if (!preg) + return INVALID_VAL; + + while (preg && preg->addr != AD717X_OFFSET0_REG) { + if (preg->addr == AD717X_ID_REG) { + preg ++; + continue; + } + + ret = AD717X_WriteRegister(dev, preg->addr); + if (ret < 0) + break; + preg ++; + } + + /* Read ID register to identify the part */ + ret = AD717X_ReadRegister(dev, AD717X_ID_REG); + if(ret < 0) + return ret; + + *device = dev; + + return ret; +} + +/***************************************************************************//** + * @brief Free the resources allocated by AD717X_Init(). + * @param dev - The device structure. + * @return SUCCESS in case of success, negative error code otherwise. +*******************************************************************************/ +int32_t AD717X_remove(ad717x_dev *dev) +{ + int32_t ret; + + ret = spi_remove(dev->spi_desc); + + free(dev); + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad717x/ad717x.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,334 @@ +/**************************************************************************//** +* @file AD717X.h +* @brief AD717X header file. +* Devices: AD7172-2, AD7172-4, AD7173-8, AD7175-2, AD7175-8, AD7176-2, +* AD7177-2, AD4111, AD4112, AD4114, AD4115 +* @author acozma (andrei.cozma@analog.com) +* dnechita (dan.nechita@analog.com) +******************************************************************************* +* Copyright 2015, 2020(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +* INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************/ + +#ifndef __AD717X_H__ +#define __AD717X_H__ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> +#include "platform_drivers.h" + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +typedef enum { + AD717X_DISABLE, + AD717X_USE_CRC, + AD717X_USE_XOR, +} ad717x_crc_mode; + +/*! AD717X register info */ +typedef struct { + int32_t addr; + int32_t value; + int32_t size; +} ad717x_st_reg; + +/* + * The structure describes the device and is used with the ad717x driver. + * @slave_select_id: The ID of the Slave Select to be passed to the SPI calls. + * @regs: A reference to the register list of the device that the user must + * provide when calling the Setup() function. + * @num_regs: The length of the register list. + * @userCRC: Error check type to use on SPI transfers. + */ +typedef struct { + /* SPI */ + spi_desc *spi_desc; + /* Device Settings */ + ad717x_st_reg *regs; + uint8_t num_regs; + ad717x_crc_mode useCRC; +} ad717x_dev; + +typedef struct { + /* SPI */ + spi_init_param spi_init; + /* Device Settings */ + ad717x_st_reg *regs; + uint8_t num_regs; +} ad717x_init_param; + +/*****************************************************************************/ +/***************** AD717X Register Definitions *******************************/ +/*****************************************************************************/ + +/* AD717X Register Map */ +#define AD717X_COMM_REG 0x00 +#define AD717X_STATUS_REG 0x00 +#define AD717X_ADCMODE_REG 0x01 +#define AD717X_IFMODE_REG 0x02 +#define AD717X_REGCHECK_REG 0x03 +#define AD717X_DATA_REG 0x04 +#define AD717X_GPIOCON_REG 0x06 +#define AD717X_ID_REG 0x07 +#define AD717X_CHMAP0_REG 0x10 +#define AD717X_CHMAP1_REG 0x11 +#define AD717X_CHMAP2_REG 0x12 +#define AD717X_CHMAP3_REG 0x13 +#define AD717X_CHMAP4_REG 0x14 +#define AD717X_CHMAP5_REG 0x15 +#define AD717X_CHMAP6_REG 0x16 +#define AD717X_CHMAP7_REG 0x17 +#define AD717X_CHMAP8_REG 0x18 +#define AD717X_CHMAP9_REG 0x19 +#define AD717X_CHMAP10_REG 0x1A +#define AD717X_CHMAP11_REG 0x1B +#define AD717X_CHMAP12_REG 0x1C +#define AD717X_CHMAP13_REG 0x1D +#define AD717X_CHMAP14_REG 0x1E +#define AD717X_CHMAP15_REG 0x1F +#define AD717X_SETUPCON0_REG 0x20 +#define AD717X_SETUPCON1_REG 0x21 +#define AD717X_SETUPCON2_REG 0x22 +#define AD717X_SETUPCON3_REG 0x23 +#define AD717X_SETUPCON4_REG 0x24 +#define AD717X_SETUPCON5_REG 0x25 +#define AD717X_SETUPCON6_REG 0x26 +#define AD717X_SETUPCON7_REG 0x27 +#define AD717X_FILTCON0_REG 0x28 +#define AD717X_FILTCON1_REG 0x29 +#define AD717X_FILTCON2_REG 0x2A +#define AD717X_FILTCON3_REG 0x2B +#define AD717X_FILTCON4_REG 0x2C +#define AD717X_FILTCON5_REG 0x2D +#define AD717X_FILTCON6_REG 0x2E +#define AD717X_FILTCON7_REG 0x2F +#define AD717X_OFFSET0_REG 0x30 +#define AD717X_OFFSET1_REG 0x31 +#define AD717X_OFFSET2_REG 0x32 +#define AD717X_OFFSET3_REG 0x33 +#define AD717X_OFFSET4_REG 0x34 +#define AD717X_OFFSET5_REG 0x35 +#define AD717X_OFFSET6_REG 0x36 +#define AD717X_OFFSET7_REG 0x37 +#define AD717X_GAIN0_REG 0x38 +#define AD717X_GAIN1_REG 0x39 +#define AD717X_GAIN2_REG 0x3A +#define AD717X_GAIN3_REG 0x3B +#define AD717X_GAIN4_REG 0x3C +#define AD717X_GAIN5_REG 0x3D +#define AD717X_GAIN6_REG 0x3E +#define AD717X_GAIN7_REG 0x3F + +/* Communication Register bits */ +#define AD717X_COMM_REG_WEN (0 << 7) +#define AD717X_COMM_REG_WR (0 << 6) +#define AD717X_COMM_REG_RD (1 << 6) +#define AD717X_COMM_REG_RA(x) ((x) & 0x3F) + +/* Status Register bits */ +#define AD717X_STATUS_REG_RDY (1 << 7) +#define AD717X_STATUS_REG_ADC_ERR (1 << 6) +#define AD717X_STATUS_REG_CRC_ERR (1 << 5) +#define AD717X_STATUS_REG_REG_ERR (1 << 4) +#define AD717X_STATUS_REG_CH(x) ((x) & 0x0F) + +/* ADC Mode Register bits */ +#define AD717X_ADCMODE_REG_REF_EN (1 << 15) +#define AD717X_ADCMODE_SING_CYC (1 << 13) +#define AD717X_ADCMODE_REG_DELAY(x) (((x) & 0x7) << 8) +#define AD717X_ADCMODE_REG_MODE(x) (((x) & 0x7) << 4) +#define AD717X_ADCMODE_REG_CLKSEL(x) (((x) & 0x3) << 2) + +/* ADC Mode Register additional bits for AD7172-2, AD7172-4, AD4111 and AD4112 */ +#define AD717X_ADCMODE_REG_HIDE_DELAY (1 << 14) + +/* Interface Mode Register bits */ +#define AD717X_IFMODE_REG_ALT_SYNC (1 << 12) +#define AD717X_IFMODE_REG_IOSTRENGTH (1 << 11) +#define AD717X_IFMODE_REG_HIDE_DELAY (1 << 10) +#define AD717X_IFMODE_REG_DOUT_RESET (1 << 8) +#define AD717X_IFMODE_REG_CONT_READ (1 << 7) +#define AD717X_IFMODE_REG_DATA_STAT (1 << 6) +#define AD717X_IFMODE_REG_REG_CHECK (1 << 5) +#define AD717X_IFMODE_REG_XOR_EN (0x01 << 2) +#define AD717X_IFMODE_REG_CRC_EN (0x02 << 2) +#define AD717X_IFMODE_REG_XOR_STAT(x) (((x) & AD717X_IFMODE_REG_XOR_EN) == AD717X_IFMODE_REG_XOR_EN) +#define AD717X_IFMODE_REG_CRC_STAT(x) (((x) & AD717X_IFMODE_REG_CRC_EN) == AD717X_IFMODE_REG_CRC_EN) +#define AD717X_IFMODE_REG_DATA_WL16 (1 << 0) + +/* GPIO Configuration Register bits */ +#define AD717X_GPIOCON_REG_MUX_IO (1 << 12) +#define AD717X_GPIOCON_REG_SYNC_EN (1 << 11) +#define AD717X_GPIOCON_REG_ERR_EN(x) (((x) & 0x3) << 9) +#define AD717X_GPIOCON_REG_ERR_DAT (1 << 8) +#define AD717X_GPIOCON_REG_IP_EN1 (1 << 5) +#define AD717X_GPIOCON_REG_IP_EN0 (1 << 4) +#define AD717X_GPIOCON_REG_OP_EN1 (1 << 3) +#define AD717X_GPIOCON_REG_OP_EN0 (1 << 2) +#define AD717X_GPIOCON_REG_DATA1 (1 << 1) +#define AD717X_GPIOCON_REG_DATA0 (1 << 0) + +/* GPIO Configuration Register additional bits for AD7172-4, AD7173-8 */ +#define AD717X_GPIOCON_REG_GP_DATA3 (1 << 7) +#define AD717X_GPIOCON_REG_GP_DATA2 (1 << 6) +#define AD717X_GPIOCON_REG_GP_DATA1 (1 << 1) +#define AD717X_GPIOCON_REG_GP_DATA0 (1 << 0) + +/* GPIO Configuration Register additional bits for AD7173-8 */ +#define AD717X_GPIOCON_REG_PDSW (1 << 14) +#define AD717X_GPIOCON_REG_OP_EN2_3 (1 << 13) + +/* GPIO Configuration Register additional bits for AD4111, AD4112, AD4114, AD4115 */ +#define AD4111_GPIOCON_REG_OP_EN0_1 (1 << 13) +#define AD4111_GPIOCON_REG_DATA1 (1 << 7) +#define AD4111_GPIOCON_REG_DATA0 (1 << 6) + +/* GPIO Configuration Register additional bits for AD4111 */ +#define AD4111_GPIOCON_REG_OW_EN (1 << 12) + +/* Channel Map Register 0-3 bits */ +#define AD717X_CHMAP_REG_CH_EN (1 << 15) +#define AD717X_CHMAP_REG_SETUP_SEL(x) (((x) & 0x7) << 12) +#define AD717X_CHMAP_REG_AINPOS(x) (((x) & 0x1F) << 5) +#define AD717X_CHMAP_REG_AINNEG(x) (((x) & 0x1F) << 0) + +/* Channel Map Register additional bits for AD4111, AD4112 */ +#define AD4111_CHMAP_REG_INPUT(x) (((x) & 0x3FF) << 0) + +/* Setup Configuration Register 0-3 bits */ +#define AD717X_SETUP_CONF_REG_BI_UNIPOLAR (1 << 12) +#define AD717X_SETUP_CONF_REG_REF_SEL(x) (((x) & 0x3) << 4) + +/* Setup Configuration Register additional bits for AD7173-8 */ +#define AD717X_SETUP_CONF_REG_REF_BUF(x) (((x) & 0x3) << 10) +#define AD717X_SETUP_CONF_REG_AIN_BUF(x) (((x) & 0x3) << 8) +#define AD717X_SETUP_CONF_REG_BURNOUT_EN (1 << 7) +#define AD717X_SETUP_CONF_REG_BUFCHOPMAX (1 << 6) + +/* Setup Configuration Register additional bits for AD7172-2, AD7172-4, AD7175-2 */ +#define AD717X_SETUP_CONF_REG_REFBUF_P (1 << 11) +#define AD717X_SETUP_CONF_REG_REFBUF_N (1 << 10) +#define AD717X_SETUP_CONF_REG_AINBUF_P (1 << 9) +#define AD717X_SETUP_CONF_REG_AINBUF_N (1 << 8) + +/* Setup Configuration Register additional bits for AD4111, AD4112 */ +#define AD4111_SETUP_CONF_REG_REFPOS_BUF (1 << 11) +#define AD4111_SETUP_CONF_REG_REFNEG_BUF (1 << 10) +#define AD4111_SETUP_CONF_REG_AIN_BUF(x) (((x) & 0x3) << 8) +#define AD4111_SETUP_CONF_REG_BUFCHOPMAX (1 << 6) + +/* Filter Configuration Register 0-3 bits */ +#define AD717X_FILT_CONF_REG_SINC3_MAP (1 << 15) +#define AD717X_FILT_CONF_REG_ENHFILTEN (1 << 11) +#define AD717X_FILT_CONF_REG_ENHFILT(x) (((x) & 0x7) << 8) +#define AD717X_FILT_CONF_REG_ORDER(x) (((x) & 0x3) << 5) +#define AD717X_FILT_CONF_REG_ODR(x) (((x) & 0x1F) << 0) + +/* ID register mask for relevant bits */ +#define AD717X_ID_REG_MASK 0xFFF0 +/* AD7172-2 ID */ +#define AD7172_2_ID_REG_VALUE 0x00D0 +/* AD7172-4 ID */ +#define AD7172_4_ID_REG_VALUE 0x2050 +/* AD7173-8 ID */ +#define AD7173_8_ID_REG_VALUE 0x30D0 +/* AD7175-2 ID */ +#define AD7175_2_ID_REG_VALUE 0x0CD0 +/* AD7175-8 ID */ +#define AD7175_8_ID_REG_VALUE 0x3CD0 +/* AD7176-2 ID */ +#define AD7176_2_ID_REG_VALUE 0x0C90 +/* AD7177-2 ID */ +#define AD7177_2_ID_REG_VALUE 0x4FD0 +/* AD411x ID */ +#define AD411X_ID_REG_VALUE 0x30D0 + +/*****************************************************************************/ +/******************* AD717X Constants ****************************************/ +/*****************************************************************************/ +#define AD717X_CRC8_POLYNOMIAL_REPRESENTATION 0x07 /* x8 + x2 + x + 1 */ + +/*****************************************************************************/ +/************************ Functions Declarations *****************************/ +/*****************************************************************************/ + +/*! Retrieves a pointer to the register that matches the given address */ +ad717x_st_reg *AD717X_GetReg(ad717x_dev *device, + uint8_t reg_address); + +/*! Reads the value of the specified register. */ +int32_t AD717X_ReadRegister(ad717x_dev *device, + uint8_t addr); + +/*! Writes the value of the specified register. */ +int32_t AD717X_WriteRegister(ad717x_dev *device, + uint8_t); + +/*! Resets the device. */ +int32_t AD717X_Reset(ad717x_dev *device); + +/*! Waits until a new conversion result is available. */ +int32_t AD717X_WaitForReady(ad717x_dev *device, + uint32_t timeout); + +/*! Reads the conversion result from the device. */ +int32_t AD717X_ReadData(ad717x_dev *device, + int32_t* pData); + +/*! Computes data register read size to account for bit number and status + * read. */ +int32_t AD717X_ComputeDataregSize(ad717x_dev *device); + +/*! Computes the CRC checksum for a data buffer. */ +uint8_t AD717X_ComputeCRC8(uint8_t* pBuf, + uint8_t bufSize); + +/*! Computes the XOR checksum for a data buffer. */ +uint8_t AD717X_ComputeXOR8(uint8_t * pBuf, + uint8_t bufSize); + +/*! Updates the CRC settings. */ +int32_t AD717X_UpdateCRCSetting(ad717x_dev *device); + +/*! Initializes the AD717X. */ +int32_t AD717X_Init(ad717x_dev **device, + ad717x_init_param init_param); + +/*! Free the resources allocated by AD717X_Init(). */ +int32_t AD717X_remove(ad717x_dev *dev); + +#endif /* __AD717X_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad7606/ad7606.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1251 @@ +/***************************************************************************//** + * @file ad7606.c + * @brief Implementation of ad7606 Driver. + * @author Stefan Popa (stefan.popa@analog.com) + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <errno.h> +#include "ad7606.h" +#include "error.h" +#include "util.h" +#include "crc.h" + +struct ad7606_chip_info { + uint8_t num_channels; + uint8_t bits; + uint8_t max_dout_lines; + bool has_oversampling; + bool has_registers; + uint8_t device_id; + const struct ad7606_range *hw_range_table; + uint32_t hw_range_table_sz; + const struct ad7606_range *sw_range_table; + uint32_t sw_range_table_sz; +}; + +DECLARE_CRC8_TABLE(ad7606_crc8); +DECLARE_CRC16_TABLE(ad7606_crc16); + +static const struct ad7606_range ad7606_range_table[] = { + {-5000, 5000, false}, /* RANGE pin LOW */ + {-10000, 10000, false}, /* RANGE pin HIGH */ +}; + +static const struct ad7606_range ad7609_range_table[] = { + {-10000, 10000, true}, /* RANGE pin LOW */ + {-20000, 20000, true}, /* RANGE pin HIGH */ +}; + +static const struct ad7606_range ad7606b_range_table[] = { + {-2500, 2500, false}, /* 0000 */ + {-5000, 5000, false}, /* 0001 */ + {-10000, 10000, false}, /* 0010 */ + {-10000, 10000, false}, /* 0011 */ + {-10000, 10000, false}, /* 0100 */ + {-10000, 10000, false}, /* 0101 */ + {-10000, 10000, false}, /* 0110 */ + {-10000, 10000, false}, /* 0111 */ + {-10000, 10000, false}, /* 1000 */ + {-10000, 10000, false}, /* 1001 */ + {-10000, 10000, false}, /* 1010 */ + {-10000, 10000, false}, /* 1011 */ +}; + +static const struct ad7606_range ad7606c_range_table[] = { + {-2500, 2500, false}, /* 0000 */ + {-5000, 5000, false}, /* 0001 */ + {-6250, 6250, false}, /* 0010 */ + {-10000, 10000, false}, /* 0011 */ + {-12500, 12500, false}, /* 0100 */ + {0, 5000, false}, /* 0101 */ + {0, 10000, false}, /* 0110 */ + {0, 12500, false}, /* 0111 */ + {-5000, 5000, true}, /* 1000 */ + {-10000, 10000, true}, /* 1001 */ + {-12500, 12500, true}, /* 1010 */ + {-20000, 20000, true}, /* 1011 */ + {-20000, 20000, true}, /* 1100 */ + {-20000, 20000, true}, /* 1101 */ + {-20000, 20000, true}, /* 1110 */ + {-20000, 20000, true}, /* 1111 */ +}; + +static const struct ad7606_chip_info ad7606_chip_info_tbl[] = { + [ID_AD7605_4] = { + .num_channels = 4, + .bits = 16, + .max_dout_lines = AD7606_2_DOUT, + .has_oversampling = false, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + }, + [ID_AD7606_4] = { + .num_channels = 4, + .bits = 16, + .max_dout_lines = AD7606_2_DOUT, + .has_oversampling = true, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + }, + [ID_AD7606_6] = { + .num_channels = 6, + .bits = 16, + .max_dout_lines = AD7606_2_DOUT, + .has_oversampling = true, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + }, + [ID_AD7606_8] = { + .num_channels = 8, + .bits = 16, + .max_dout_lines = AD7606_2_DOUT, + .has_oversampling = true, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + }, + [ID_AD7606B] = { + .num_channels = 8, + .bits = 16, + .max_dout_lines = AD7606_4_DOUT, + .has_oversampling = true, + .has_registers = true, + .device_id = 0x1, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + .sw_range_table = ad7606b_range_table, + .sw_range_table_sz = ARRAY_SIZE(ad7606b_range_table), + }, + [ID_AD7606C_16] = { + .num_channels = 8, + .bits = 16, + .max_dout_lines = AD7606_8_DOUT, + .has_oversampling = true, + .has_registers = true, + .device_id = 0x3, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + .sw_range_table = ad7606c_range_table, + .sw_range_table_sz = ARRAY_SIZE(ad7606c_range_table), + }, + [ID_AD7606C_18] = { + .num_channels = 8, + .bits = 18, + .max_dout_lines = AD7606_8_DOUT, + .has_oversampling = true, + .has_registers = true, + .device_id = 0x3, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + .sw_range_table = ad7606c_range_table, + .sw_range_table_sz = ARRAY_SIZE(ad7606c_range_table), + }, + [ID_AD7608] = { + .num_channels = 8, + .bits = 18, + .max_dout_lines = AD7606_2_DOUT, + .has_oversampling = true, + .hw_range_table = ad7606_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7606_range_table), + }, + [ID_AD7609] = { + .num_channels = 8, + .bits = 18, + .max_dout_lines = AD7606_2_DOUT, + .has_oversampling = true, + .hw_range_table = ad7609_range_table, + .hw_range_table_sz = ARRAY_SIZE(ad7609_range_table), + }, +}; + +static const uint16_t tconv_max[] = { + 1, /* AD7606_OSR_1 */ + 3, /* AD7606_OSR_2 */ + 5, /* AD7606_OSR_4 */ + 10, /* AD7606_OSR_8 */ + 20, /* AD7606_OSR_16 */ + 41, /* AD7606_OSR_32 */ + 81, /* AD7606_OSR_64 */ + 162, /* AD7606_OSR_128 */ + 324 /* AD7606_OSR_256 */ +}; + + +/***************************************************************************//** + * @brief Read a device register via SPI. + * + * This function performs CRC8 computation and checking if enabled in the device. + * + * @param dev - The device structure. + * @param reg_addr - Register address in device memory. + * @param reg_data - Pointer to the location where to store the register value. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_spi_reg_read(struct ad7606_dev *dev, + uint8_t reg_addr, + uint8_t *reg_data) +{ + uint8_t buf[3]; + uint8_t crc; + uint32_t sz = 2; + int32_t ret; + + if (!dev->sw_mode) + return -ENOTSUP; + + buf[0] = AD7606_RD_FLAG_MSK(reg_addr); + buf[1] = 0x00; + if (dev->digital_diag_enable.int_crc_err_en) { + crc = crc8(ad7606_crc8, buf, 2, 0); + buf[2] = crc; + sz += 1; + } + ret = spi_write_and_read(dev->spi_desc, buf, sz); + if (ret < 0) + return ret; + + dev->reg_mode = true; + + buf[0] = AD7606_RD_FLAG_MSK(reg_addr); + buf[1] = 0x00; + if (dev->digital_diag_enable.int_crc_err_en) { + crc = crc8(ad7606_crc8, buf, 2, 0); + buf[2] = crc; + } + ret = spi_write_and_read(dev->spi_desc, buf, sz); + if (ret < 0) + return ret; + + if (dev->digital_diag_enable.int_crc_err_en) { + crc = crc8(ad7606_crc8, buf, 2, 0); + if (crc != buf[2]) + return -EBADMSG; + } + + if (reg_data) + *reg_data = buf[1]; + + return ret; +} + +/***************************************************************************//** + * @brief Write a device register via SPI. + * + * This function performs CRC8 computation and checking if enabled in the device. + * + * @param dev - The device structure. + * @param reg_addr - Register address in device memory. + * @param reg_data - Value to write to register. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_spi_reg_write(struct ad7606_dev *dev, + uint8_t reg_addr, + uint8_t reg_data) +{ + uint8_t buf[3]; + int32_t ret; + uint8_t crc; + uint32_t sz = 2; + + if (!dev->sw_mode) + return -ENOTSUP; + + /* Dummy read to place the chip in register mode. */ + if (!dev->reg_mode) { + ret = ad7606_spi_reg_read(dev, reg_addr, NULL); + if (ret < 0) + return ret; + } + + buf[0] = AD7606_WR_FLAG_MSK(reg_addr); + buf[1] = reg_data; + if (dev->digital_diag_enable.int_crc_err_en) { + crc = crc8(ad7606_crc8, buf, 2, 0); + buf[2] = crc; + sz += 1; + } + + ret = spi_write_and_read(dev->spi_desc, buf, sz); + if (ret < 0) + return ret; + + return ret; +} + +/***************************************************************************//** + * @brief Write a device register via SPI with masking. + * + * @param dev - The device structure. + * @param addr - Register address in device memory. + * @param mask - Only bits set to 1 in mask will be modified. + * @param val - Value to write to register. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_spi_write_mask(struct ad7606_dev *dev, + uint32_t addr, + uint32_t mask, + uint32_t val) +{ + uint8_t reg_data; + int ret; + + ret = ad7606_spi_reg_read(dev, addr, ®_data); + if (ret < 0) + return ret; + + reg_data &= ~mask; + reg_data |= val; + + return ad7606_spi_reg_write(dev, addr, reg_data); +} + +/* Internal function to copy the content of a buffer in 18-bit chunks to a 32-bit buffer by + * extending the chunks to 32-bit size. */ +static int32_t cpy18b32b(uint8_t *psrc, uint32_t srcsz, uint32_t *pdst) +{ + unsigned int i, j; + + if (srcsz % 9) + return -EINVAL; + + for(i = 0; i < srcsz; i += 9) { + j = 4 * (i / 9); + pdst[j+0] = ((uint32_t)(psrc[i+0] & 0xff) << 10) | ((uint32_t)psrc[i+1] << 2) + | ((uint32_t)psrc[i+2] >> 6); + pdst[j+1] = ((uint32_t)(psrc[i+2] & 0x3f) << 12) | ((uint32_t)psrc[i+3] << 4) + | ((uint32_t)psrc[i+4] >> 4); + pdst[j+2] = ((uint32_t)(psrc[i+4] & 0x0f) << 14) | ((uint32_t)psrc[i+5] << 6) + | ((uint32_t)psrc[i+6] >> 2); + pdst[j+3] = ((uint32_t)(psrc[i+6] & 0x03) << 16) | ((uint32_t)psrc[i+7] << 8) + | ((uint32_t)psrc[i+8] >> 0); + } + return SUCCESS; +} + +/* Internal function to copy the content of a buffer in 26-bit chunks to a 32-bit buffer by + * extending the chunks to 32-bit size. */ +static int32_t cpy26b32b(uint8_t *psrc, uint32_t srcsz, uint32_t *pdst) +{ + unsigned int i, j; + + if (srcsz % 13) + return -EINVAL; + + for(i = 0; i < srcsz; i += 13) { + j = 4 * (i / 13); + pdst[j+0] = ((uint32_t)(psrc[i+0] & 0xff) << 18) | ((uint32_t)psrc[i+1] << 10) + | ((uint32_t)psrc[i+2] << 2) | ((uint32_t)psrc[i+3] >> 6); + pdst[j+1] = ((uint32_t)(psrc[i+3] & 0x3f) << 20) | ((uint32_t)psrc[i+4] << 12) + | ((uint32_t)psrc[i+5] << 4) | ((uint32_t)psrc[i+6] >> 4); + pdst[j+2] = ((uint32_t)(psrc[i+6] & 0x0f) << 22) | ((uint32_t)psrc[i+7] << 14) + | ((uint32_t)psrc[i+8] << 6) | ((uint32_t)psrc[i+9] >> 2); + pdst[j+3] = ((uint32_t)(psrc[i+9] & 0x03) << 24) | ((uint32_t)psrc[i+10] << 16) + | ((uint32_t)psrc[i+11] << 8) | ((uint32_t)psrc[i+12] >> 0); + } + return SUCCESS; +} + +/***************************************************************************//** + * @brief Toggle the CONVST pin to start a conversion. + * + * If needed, this function also puts the device in ADC reading mode by a write + * at address zero. + * + * @param dev - The device structure. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EIO - CONVST GPIO not available. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_convst(struct ad7606_dev *dev) +{ + int32_t ret; + + if (dev->reg_mode) { + /* Enter ADC reading mode by writing at address zero. */ + ret = ad7606_spi_reg_write(dev, 0, 0); + if (ret < 0) + return ret; + + dev->reg_mode = false; + } + + ret = gpio_set_value(dev->gpio_convst, 0); + if (ret < 0) + return ret; + + /* wait LP_CNV time */ + udelay(1); + + return gpio_set_value(dev->gpio_convst, 1); +} + +/***************************************************************************//** + * @brief Read conversion data. + * + * This function performs CRC16 computation and checking if enabled in the device. + * If the status is enabled in device settings, each sample of data will contain + * status information in the lowest 8 bits. + * + * The output buffer provided by the user should be as wide as to be able to + * contain 1 sample from each channel since this function reads conversion data + * across all channels. + * + * @param dev - The device structure. + * @param data - Pointer to location of buffer where to store the data. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EBADMSG - CRC computation mismatch. + * -ENOTSUP - Device bits per sample not supported. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_spi_data_read(struct ad7606_dev *dev, uint32_t *data) +{ + uint32_t sz; + int32_t ret, i; + uint16_t crc, icrc; + uint8_t bits = ad7606_chip_info_tbl[dev->device_id].bits; + uint8_t sbits = dev->config.status_header ? 8 : 0; + uint8_t nchannels = ad7606_chip_info_tbl[dev->device_id].num_channels; + + sz = nchannels * (bits + sbits); + + /* Number of bits to read, corresponds to SCLK cycles in transfer. + * This should always be a multiple of 8 to work with most SPI's. + * With this chip family this holds true because we either: + * - multiply 8 channels * bits per sample + * - multiply 4 channels * bits per sample (always multiple of 2) + * Therefore, due to design reasons, we don't check for the + * remainder of this division because it is zero by design. + */ + sz /= 8; + + if (dev->digital_diag_enable.int_crc_err_en) { + sz += 2; + } + + memset(dev->data, 0, sz); + ret = spi_write_and_read(dev->spi_desc, dev->data, sz); + if (ret < 0) + return ret; + + if (dev->digital_diag_enable.int_crc_err_en) { + sz -= 2; + crc = crc16(ad7606_crc16, dev->data, sz, 0); + icrc = ((uint16_t)dev->data[sz] << 8) | + dev->data[sz+1]; + if (icrc != crc) + return -EBADMSG; + } + + switch(bits) { + case 18: + if (dev->config.status_header) + ret = cpy26b32b(dev->data, sz, data); + else + ret = cpy18b32b(dev->data, sz, data); + if (ret < 0) + return ret; + break; + case 16: + for(i = 0; i < nchannels; i++) { + if (dev->config.status_header) { + data[i] = (uint32_t)dev->data[i*3] << 16; + data[i] |= (uint32_t)dev->data[i*3+1] << 8; + data[i] |= (uint32_t)dev->data[i*3+2]; + } else { + data[i] = (uint32_t)dev->data[i*2] << 8; + data[i] |= (uint32_t)dev->data[i*2+1]; + } + } + break; + default: + ret = -ENOTSUP; + break; + }; + + return ret; +} + +/***************************************************************************//** + * @brief Blocking conversion start and data read. + * + * This function performs a conversion start and then proceeds to reading + * the conversion data. + * + * @param dev - The device structure. + * @param data - Pointer to location of buffer where to store the data. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -ETIME - Timeout while waiting for the BUSY signal. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_read(struct ad7606_dev *dev, uint32_t * data) +{ + int32_t ret; + uint8_t busy; + uint32_t timeout = tconv_max[AD7606_OSR_256]; + + ret = ad7606_convst(dev); + if (ret < 0) + return ret; + + if (dev->gpio_busy) { + /* Wait for BUSY falling edge */ + while(timeout) { + ret = gpio_get_value(dev->gpio_busy, &busy); + if (ret < 0) + return ret; + + if (busy == 0) + break; + + udelay(1); + timeout--; + } + + if (timeout == 0) + return -ETIME; + } else { + /* wait CONV time */ + udelay(tconv_max[dev->oversampling.os_ratio]); + } + + return ad7606_spi_data_read(dev, data); +} + +/* Internal function to reset device settings to default state after chip reset. */ +static inline void ad7606_reset_settings(struct ad7606_dev *dev) +{ + int i; + const struct ad7606_range *rt = dev->sw_mode ? + ad7606_chip_info_tbl[dev->device_id].sw_range_table: + ad7606_chip_info_tbl[dev->device_id].hw_range_table; + + for(i = 0; i < dev->num_channels; i++) { + if (dev->sw_mode) + dev->range_ch[i] = rt[3]; + else + dev->range_ch[i] = rt[0]; + + dev->offset_ch[i] = 0; + dev->phase_ch[i] = 0; + dev->gain_ch[i] = 0; + } + + dev->oversampling.os_ratio = AD7606_OSR_1; + dev->oversampling.os_pad = 0; + dev->config.op_mode = AD7606_NORMAL; + dev->config.dout_format = AD7606_2_DOUT; + dev->config.ext_os_clock = false; + dev->config.status_header = false; + dev->digital_diag_enable.rom_crc_err_en = true; + dev->digital_diag_enable.mm_crc_err_en = false; + dev->digital_diag_enable.int_crc_err_en = false; + dev->digital_diag_enable.spi_write_err_en = false; + dev->digital_diag_enable.spi_read_err_en = false; + dev->digital_diag_enable.busy_stuck_high_err_en = false; + dev->digital_diag_enable.clk_fs_os_counter_en = false; + dev->digital_diag_enable.interface_check_en = false; + dev->reg_mode = false; +} + +/***************************************************************************//** + * @brief Reset the device by toggling the reset GPIO. + * + * @param dev - The device structure. + * + * @return ret - return code. + * Example: -EIO - Reset GPIO not available. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_reset(struct ad7606_dev *dev) +{ + int32_t ret; + + ret = gpio_set_value(dev->gpio_reset, 1); + if (ret < 0) + return ret; + + udelay(3); + + ret = gpio_set_value(dev->gpio_reset, 0); + if (ret < 0) + return ret; + + ad7606_reset_settings(dev); + + return ret; +} + +/* Internal function that initializes GPIOs. */ +static int32_t ad7606_request_gpios(struct ad7606_dev *dev, + struct ad7606_init_param *init_param) +{ + int32_t ret; + + ret = gpio_get_optional(&dev->gpio_reset, init_param->gpio_reset); + if (ret < 0) + return ret; + + if (dev->gpio_reset) { + ret = gpio_direction_output(dev->gpio_reset, GPIO_LOW); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_convst, init_param->gpio_convst); + if (ret < 0) + return ret; + + if (dev->gpio_convst) { + ret = gpio_direction_output(dev->gpio_convst, GPIO_LOW); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_busy, init_param->gpio_busy); + if (ret < 0) + return ret; + + if (dev->gpio_busy) { + ret = gpio_direction_input(dev->gpio_busy); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_stby_n, init_param->gpio_stby_n); + if (ret < 0) + return ret; + + if (dev->gpio_stby_n) { + ret = gpio_direction_output(dev->gpio_stby_n, GPIO_HIGH); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_range, init_param->gpio_range); + if (ret < 0) + return ret; + + if (dev->gpio_range) { + ret = gpio_direction_output(dev->gpio_range, GPIO_LOW); + if (ret < 0) + return ret; + } + + if (!ad7606_chip_info_tbl[dev->device_id].has_oversampling) + return ret; + + ret = gpio_get_optional(&dev->gpio_os0, init_param->gpio_os0); + if (ret < 0) + return ret; + + if (dev->gpio_os0) { + ret = gpio_direction_output(dev->gpio_os0, GPIO_LOW); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_os1, init_param->gpio_os1); + if (ret < 0) + return ret; + + if (dev->gpio_os1) { + ret = gpio_direction_output(dev->gpio_os1, GPIO_LOW); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_os2, init_param->gpio_os2); + if (ret < 0) + return ret; + + if (dev->gpio_os2) { + ret = gpio_direction_output(dev->gpio_os2, GPIO_LOW); + if (ret < 0) + return ret; + } + + ret = gpio_get_optional(&dev->gpio_par_ser, init_param->gpio_par_ser); + if (ret < 0) + return ret; + + if (dev->gpio_par_ser) { + /* Driver currently supports only serial interface, therefore, + * if available, pull the GPIO HIGH. */ + ret = gpio_direction_output(dev->gpio_par_ser, GPIO_HIGH); + if (ret < 0) + return ret; + } + + return ret; +} + +/***************************************************************************//** + * @brief Set the oversampling ratio. + * + * In hardware mode, it silently sets AD7606_OSR_64 if higher oversampling + * is provided. + * + * @param dev - The device structure. + * @param oversampling - Oversampling settings. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_oversampling(struct ad7606_dev *dev, + struct ad7606_oversampling oversampling) +{ + int32_t ret; + uint8_t val; + + if (dev->sw_mode) { + val = field_prep(AD7606_OS_RATIO_MSK, oversampling.os_ratio); + val |= field_prep(AD7606_OS_PAD_MSK, oversampling.os_pad); + ret = ad7606_spi_reg_write(dev, AD7606_REG_OVERSAMPLING, val); + if (ret < 0) + return ret; + } else { + /* In hardware mode, OSR 128 and 256 are not avaialable */ + if (oversampling.os_ratio > AD7606_OSR_64) + oversampling.os_ratio = AD7606_OSR_64; + + ret = gpio_set_value(dev->gpio_os0, ((oversampling.os_ratio & 0x01) >> 0)); + if (ret < 0) + return ret; + + ret = gpio_set_value(dev->gpio_os1, ((oversampling.os_ratio & 0x02) >> 1)); + if (ret < 0) + return ret; + + ret = gpio_set_value(dev->gpio_os2, ((oversampling.os_ratio & 0x04) >> 2)); + if (ret < 0) + return ret; + } + + dev->oversampling = oversampling; + + return SUCCESS; +} + +/* Internal function to find the index of a given operation range in the + * operation range table specific to a device. */ +static int8_t ad7606_find_range(struct ad7606_dev *dev, + struct ad7606_range range) +{ + uint8_t i; + int8_t v = -1; + const struct ad7606_range *rt = dev->sw_mode ? + ad7606_chip_info_tbl[dev->device_id].sw_range_table: + ad7606_chip_info_tbl[dev->device_id].hw_range_table; + + uint32_t rtsz = dev->sw_mode ? + ad7606_chip_info_tbl[dev->device_id].sw_range_table_sz: + ad7606_chip_info_tbl[dev->device_id].hw_range_table_sz; + + for (i = 0; i < rtsz; i++) { + if (range.min != rt[i].min) + continue; + if (range.max != rt[i].max) + continue; + if (range.differential != rt[i].differential) + continue; + v = i; + break; + } + + return v; +} + +/***************************************************************************//** + * @brief Set the channel operation range. + * + * @param dev - The device structure. + * @param ch - Channel number (0-7). + * @param range - Operation range. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EINVAL - Invalid input. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_ch_range(struct ad7606_dev *dev, uint8_t ch, + struct ad7606_range range) +{ + int value; + int32_t ret; + + if (range.min > range.max) + return -EINVAL; + + if (ch >= dev->num_channels) + return -EINVAL; + + value = ad7606_find_range(dev, range); + if (value < 0) + return -EINVAL; + + if (dev->sw_mode) + ret = ad7606_spi_write_mask(dev, AD7606_REG_RANGE_CH_ADDR(ch), + AD7606_RANGE_CH_MSK(ch), + AD7606_RANGE_CH_MODE(ch, value)); + else + ret = gpio_set_value(dev->gpio_range, value); + + if (ret) + return ret; + + dev->range_ch[ch] = range; + + return ret; +} + +/***************************************************************************//** + * @brief Set the channel offset. + * + * The offset parameter is a signed 8-bit integer ranging from -128 to 127 to + * make it intuitive and user-friendly. + * + * This offset gets converted to the register representation where 0x80 is + * calibration offset 0, 0x0 is calibration offset -128 and 0xFF is calibration + * offset 127, etc. + * + * @param dev - The device structure. + * @param ch - Channel number (0-7). + * @param offset - Offset calibration amount (-128...127). + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EINVAL - Invalid input. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_ch_offset(struct ad7606_dev *dev, uint8_t ch, + int8_t offset) +{ + int ret; + uint8_t value = (uint8_t)(offset - 0x80); + + if (ch >= dev->num_channels) + return -EINVAL; + + if (!dev->sw_mode) + return -ENOTSUP; + + ret = ad7606_spi_reg_write(dev, AD7606_REG_OFFSET_CH(ch), value); + if (ret < 0) + return ret; + + dev->offset_ch[ch] = offset; + + return ret; +} + +/***************************************************************************//** + * @brief Set the channel phase. + * + * @param dev - The device structure. + * @param ch - Channel number (0-7). + * @param phase - Phase calibration amount. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EINVAL - Invalid input. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_ch_phase(struct ad7606_dev *dev, uint8_t ch, + uint8_t phase) +{ + int ret; + + if (ch >= dev->num_channels) + return -EINVAL; + + if (!dev->sw_mode) + return -ENOTSUP; + + ret = ad7606_spi_reg_write(dev, AD7606_REG_PHASE_CH(ch), phase); + if (ret < 0) + return ret; + + dev->phase_ch[ch] = phase; + + return ret; +} + +/***************************************************************************//** + * @brief Set the channel gain. + * + * @param dev - The device structure. + * @param ch - Channel number (0-7). + * @param gain - Gain calibration amount. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EINVAL - Invalid input. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_ch_gain(struct ad7606_dev *dev, uint8_t ch, + uint8_t gain) +{ + int ret; + + if (ch >= dev->num_channels) + return -EINVAL; + + if (!dev->sw_mode) + return -ENOTSUP; + + gain = field_get(AD7606_GAIN_MSK, gain); + ret = ad7606_spi_reg_write(dev, AD7606_REG_GAIN_CH(ch), gain); + if (ret < 0) + return ret; + + dev->gain_ch[ch] = gain; + + return ret; +} + +/***************************************************************************//** + * @brief Set the device config register. + * + * Configuration structure affects the CONFIG register of the device. + * + * @param dev - The device structure. + * @param config - Configuration structure. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -EIO - GPIO not available. + * -EINVAL - Invalid input. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_config(struct ad7606_dev *dev, + struct ad7606_config config) +{ + int32_t ret; + uint8_t val = 0; + uint8_t range_pin, stby_n_pin; + + if (dev->sw_mode) { + + val |= field_prep(AD7606_CONFIG_OPERATION_MODE_MSK, config.op_mode); + /* This driver currently supports only normal SPI with 1 DOUT line. + * TODO: remove this check when implementing multi-line DOUT. */ + if ((uint8_t)config.dout_format > AD7606_1_DOUT) + return -EINVAL; + if ((uint8_t)config.dout_format > (uint8_t)dev->max_dout_lines) + return -EINVAL; + val |= field_prep(AD7606_CONFIG_DOUT_FORMAT_MSK, config.dout_format); + val |= field_prep(AD7606_CONFIG_EXT_OS_CLOCK_MSK, config.ext_os_clock); + val |= field_prep(AD7606_CONFIG_STATUS_HEADER_MSK, config.status_header); + + ret = ad7606_spi_reg_write(dev, AD7606_REG_CONFIG, val); + if (ret) + return ret; + } else { + switch(config.op_mode) { + case AD7606_NORMAL: + range_pin = GPIO_LOW; + stby_n_pin = GPIO_HIGH; + break; + case AD7606_STANDBY: + range_pin = GPIO_LOW; + stby_n_pin = GPIO_LOW; + break; + case AD7606_SHUTDOWN: + range_pin = GPIO_HIGH; + stby_n_pin = GPIO_LOW; + break; + default: + return -EINVAL; + }; + + ret = gpio_set_value(dev->gpio_stby_n, stby_n_pin); + if (ret) + return ret; + + ret = gpio_set_value(dev->gpio_range, range_pin); + if (ret) + return ret; + } + + dev->config = config; + + return ret; +} + +/***************************************************************************//** + * @brief Set the device digital diagnostics configuration. + * + * Digital diagnostics structure affects the DIGITAL_DIAG register of the device. + * + * @param dev - The device structure. + * @param diag - Configuration structure. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * -ENOTSUP - Device not in software mode. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_set_digital_diag(struct ad7606_dev *dev, + struct ad7606_digital_diag diag) +{ + int32_t ret; + uint8_t val = 0; + + if (!dev->sw_mode) + return -ENOTSUP; + + val |= field_prep(AD7606_ROM_CRC_ERR_EN_MSK, diag.rom_crc_err_en); + val |= field_prep(AD7606_MM_CRC_ERR_EN_MSK, diag.mm_crc_err_en); + val |= field_prep(AD7606_INT_CRC_ERR_EN_MSK, diag.int_crc_err_en); + val |= field_prep(AD7606_SPI_WRITE_ERR_EN_MSK, diag.spi_write_err_en); + val |= field_prep(AD7606_SPI_READ_ERR_EN_MSK, diag.spi_read_err_en); + val |= field_prep(AD7606_BUSY_STUCK_HIGH_ERR_EN_MSK, + diag.busy_stuck_high_err_en); + val |= field_prep(AD7606_CLK_FS_OS_COUNTER_EN_MSK, diag.clk_fs_os_counter_en); + val |= field_prep(AD7606_INTERFACE_CHECK_EN_MSK, diag.interface_check_en); + + ret = ad7606_spi_reg_write(dev, AD7606_REG_DIGITAL_DIAG_ENABLE, val); + if (ret < 0) + return ret; + + dev->digital_diag_enable = diag; + + return ret; +} + +/***************************************************************************//** + * @brief Initialize the ad7606 device structure. + * + * Performs memory allocation of the device structure. + * + * @param device - Pointer to location of device structure to write. + * @param init_param - Pointer to configuration of the driver. + * + * @return ret - return code. + * Example: -ENOMEM - Memory allocation error. + * -EIO - SPI communication error. + * -EIO - GPIO initialization error. + * -ENODEV - Unexpected device id. + * -EBADMSG - CRC computation mismatch. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_init(struct ad7606_dev **device, + struct ad7606_init_param *init_param) +{ + struct ad7606_dev *dev; + uint8_t reg, id; + int32_t i, ret; + + crc8_populate_msb(ad7606_crc8, 0x7); + crc16_populate_msb(ad7606_crc16, 0x755b); + + dev = (struct ad7606_dev *)calloc(1, sizeof(*dev)); + if (!dev) + return -ENOMEM; + + dev->device_id = init_param->device_id; + dev->num_channels = ad7606_chip_info_tbl[dev->device_id].num_channels; + dev->max_dout_lines = ad7606_chip_info_tbl[dev->device_id].max_dout_lines; + if (ad7606_chip_info_tbl[dev->device_id].has_registers) + dev->sw_mode = init_param->sw_mode; + + ret = ad7606_request_gpios(dev, init_param); + if (ret < 0) + goto error; + + if (init_param->sw_mode) { + ret = gpio_set_value(dev->gpio_os0, GPIO_HIGH); + if (ret < 0) + goto error; + + ret = gpio_set_value(dev->gpio_os1, GPIO_HIGH); + if (ret < 0) + goto error; + + ret = gpio_set_value(dev->gpio_os2, GPIO_HIGH); + if (ret < 0) + goto error; + } + + ret = ad7606_reset(dev); + if (ret < 0) + goto error; + + /* wait DEVICE_SETUP time */ + udelay(253); + + ret = spi_init(&dev->spi_desc, &init_param->spi_init); + if (ret < 0) + goto error; + + if (dev->sw_mode) { + ret = ad7606_spi_reg_read(dev, AD7606_REG_ID, ®); + if (ret < 0) + goto error; + + id = ad7606_chip_info_tbl[dev->device_id].device_id; + if (field_get(AD7606_ID_DEVICE_ID_MSK, reg) != id) { + printf("ad7606: device id mismatch, expected 0x%.2x, got 0x%.2x\n", + id, + (int)field_get(AD7606_ID_DEVICE_ID_MSK, reg)); + ret = -ENODEV; + goto error; + } + + ret = ad7606_set_digital_diag(dev, init_param->digital_diag_enable); + if (ret < 0) + goto error; + + ret = ad7606_set_config(dev, init_param->config); + if (ret < 0) + goto error; + + for (i = 0; i < dev->num_channels; i++) { + ret = ad7606_set_ch_range(dev, i, init_param->range_ch[i]); + if (ret < 0) + goto error; + } + + for(i = 0; i < dev->num_channels; i++) { + ret = ad7606_set_ch_offset(dev, i, init_param->offset_ch[i]); + if (ret < 0) + goto error; + } + + for(i = 0; i < dev->num_channels; i++) { + ret = ad7606_set_ch_phase(dev, i, init_param->phase_ch[i]); + if (ret < 0) + goto error; + } + + for(i = 0; i < dev->num_channels; i++) { + ret = ad7606_set_ch_gain(dev, i, init_param->gain_ch[i]); + if (ret < 0) + goto error; + } + } else { + ret = ad7606_set_ch_range(dev, 0, init_param->range_ch[0]); + if (ret < 0) + goto error; + } + + ret = gpio_set_value(dev->gpio_convst, 1); + if (ret < 0) + goto error; + + if (ad7606_chip_info_tbl[dev->device_id].has_oversampling) + ad7606_set_oversampling(dev, init_param->oversampling); + + *device = dev; + + printf("ad7606 successfully initialized\n"); + + return ret; +error: + printf("ad7606 initialization failed\n"); + ad7606_remove(dev); + return ret; +} + +/***************************************************************************//** + * @brief Free any resource used by the driver. + * + * @param dev - The device structure. + * + * @return ret - return code. + * Example: -EIO - SPI communication error. + * SUCCESS - No errors encountered. +*******************************************************************************/ +int32_t ad7606_remove(struct ad7606_dev *dev) +{ + int32_t ret; + + gpio_remove(dev->gpio_reset); + gpio_remove(dev->gpio_convst); + gpio_remove(dev->gpio_busy); + gpio_remove(dev->gpio_stby_n); + gpio_remove(dev->gpio_range); + gpio_remove(dev->gpio_os0); + gpio_remove(dev->gpio_os1); + gpio_remove(dev->gpio_os2); + gpio_remove(dev->gpio_par_ser); + + ret = spi_remove(dev->spi_desc); + + free(dev); + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/adc/ad7606/ad7606.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,404 @@ +/***************************************************************************//** + * @file ad7606.h + * @brief Header file for the ad7606 Driver. + * @author Stefan Popa (stefan.popa@analog.com) + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef AD7606_H_ +#define AD7606_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> +#include <stdbool.h> + +#include "platform_drivers.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ +/* Error codes defined in 'errno.h' file but can't find by Mbed online/offline + * compiler + * */ +#define ENOTSUP 134 +#define EBADMSG 77 /* Bad message */ +#define ETIME 62 /* Stream ioctl timeout */ +#define ENODEV 19 /* No such device */ + +#define AD7606_REG_STATUS 0x01 +#define AD7606_REG_CONFIG 0x02 +#define AD7606_REG_RANGE_CH_ADDR(ch) (0x03 + ((ch) >> 1)) +#define AD7606_REG_BANDWIDTH 0x07 +#define AD7606_REG_OVERSAMPLING 0x08 +#define AD7606_REG_GAIN_CH(ch) (0x09 + (ch)) +#define AD7606_REG_OFFSET_CH(ch) (0x11 + (ch)) +#define AD7606_REG_PHASE_CH(ch) (0x19 + (ch)) +#define AD7606_REG_DIGITAL_DIAG_ENABLE 0x21 +#define AD7606_REG_DIGITAL_DIAG_ERR 0x22 +#define AD7606_REG_OPEN_DETECT_ENABLE 0x23 +#define AD7606_REG_OPEN_DETECTED 0x24 +#define AD7606_REG_AIN_OV_UV_DIAG_ENABLE 0x25 +#define AD7606_REG_AIN_OV_DIAG_ERROR 0x26 +#define AD7606_REG_AIN_UV_DIAG_ERROR 0x27 +#define AD7606_REG_DIAGNOSTIC_MUX_CH(ch) (0x28 + ((ch) >> 1)) +#define AD7606_REG_OPEN_DETECT_QUEUE 0x2C +#define AD7606_REG_CLK_FS_COUNTER 0x2D +#define AD7606_REG_CLK_OS_COUNTER 0x2E +#define AD7606_REG_ID 0x2F + +/* AD7606_REG_STATUS */ +#define AD7606_STATUS_CHANNEL_MSK GENMASK(2,0) +#define AD7606_AIN_UV_ERR_MSK BIT(3) +#define AD7606_AIN_OV_ERR_MSK BIT(4) +#define AD7606_OPEN_DETECTED_MSK BIT(5) +#define AD7606_DIGITAL_ERROR_MSK BIT(6) +#define AD7606_RESET_DETECT_MSK BIT(7) + +/* AD7606_REG_CONFIG */ +#define AD7606_CONFIG_OPERATION_MODE_MSK GENMASK(1,0) +#define AD7606_CONFIG_DOUT_FORMAT_MSK GENMASK(4,3) +#define AD7606_CONFIG_EXT_OS_CLOCK_MSK BIT(5) +#define AD7606_CONFIG_STATUS_HEADER_MSK BIT(6) + +/* AD7606_REG_RANGE_CH_X_Y */ +#define AD7606_RANGE_CH_MSK(ch) (GENMASK(3, 0) << (4 * ((ch) % 2))) +#define AD7606_RANGE_CH_MODE(ch, mode) \ + ((GENMASK(3, 0) & mode) << (4 * ((ch) % 2))) + +/* AD7606_REG_OVERSAMPLING */ +#define AD7606_OS_PAD_MSK GENMASK(7,4) +#define AD7606_OS_RATIO_MSK GENMASK(3,0) + +/* AD7606_REG_ID */ +#define AD7606_ID_DEVICE_ID_MSK GENMASK(7,4) +#define AD7606_ID_SILICON_REVISION_MSK GENMASK(3,0) + +/* AD7606_REG_GAIN_CH */ +#define AD7606_GAIN_MSK GENMASK(5,0) + +/* AD7606_REG_DIGITAL_DIAG_ENABLE */ +#define AD7606_ROM_CRC_ERR_EN_MSK BIT(0) +#define AD7606_MM_CRC_ERR_EN_MSK BIT(1) +#define AD7606_INT_CRC_ERR_EN_MSK BIT(2) +#define AD7606_SPI_WRITE_ERR_EN_MSK BIT(3) +#define AD7606_SPI_READ_ERR_EN_MSK BIT(4) +#define AD7606_BUSY_STUCK_HIGH_ERR_EN_MSK BIT(5) +#define AD7606_CLK_FS_OS_COUNTER_EN_MSK BIT(6) +#define AD7606_INTERFACE_CHECK_EN_MSK BIT(7) + +/* AD7606_REG_DIAGNOSTIC_MUX_CH */ +#define AD7606_DIAGN_MUX_CH_MSK(ch) (GENMASK(2, 0) << (3 * (ch & 0x1))) + +#define AD7606_RD_FLAG_MSK(x) (BIT(6) | ((x) & 0x3F)) +#define AD7606_WR_FLAG_MSK(x) ((x) & 0x3F) + +#define AD7606_MAX_CHANNELS 8 + +/** + * @enum ad7606_device_id + * @brief Device ID definitions + */ +enum ad7606_device_id { + /** 4-Channel DAS with 16-Bit, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7605_4, + /** 4-Channel DAS with 16-Bit, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7606_4, + /** 6-Channel DAS with 16-Bit, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7606_6, + /** 8-Channel DAS with 16-Bit, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7606_8, + /** 8-Channel DAS with 16-Bit, 800 kSPS, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7606B, + /** 8-Channel DAS with 16-Bit, 1 MSPS, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7606C_16, + /** 8-Channel DAS with 18-Bit, 1 MSPS, Bipolar Input, Simultaneous Sampling ADC */ + ID_AD7606C_18, + /** 8-Channel DAS with 18-Bit, Bipolar, Simultaneous Sampling ADC */ + ID_AD7608, + /** 8-Channel Differential DAS with 18-Bit, Bipolar, Simultaneous Sampling ADC */ + ID_AD7609, +}; + +/** + * @enum ad7606_osr + * @brief Oversampling ratio + */ +enum ad7606_osr { + /** Oversample by 1 */ + AD7606_OSR_1, + /** Oversample by 2 */ + AD7606_OSR_2, + /** Oversample by 4 */ + AD7606_OSR_4, + /** Oversample by 8 */ + AD7606_OSR_8, + /** Oversample by 16 */ + AD7606_OSR_16, + /** Oversample by 32 */ + AD7606_OSR_32, + /** Oversample by 64 */ + AD7606_OSR_64, + /** Oversample by 128, available for chips that have software mode only */ + AD7606_OSR_128, + /** Oversample by 256, available for chips that have software mode only */ + AD7606_OSR_256 +}; + +/** + * @enum ad7606_op_mode + * @brief Operation mode + */ +enum ad7606_op_mode { + /** Normal operation mode */ + AD7606_NORMAL, + /** Standby mode, all the PGAs, and all the SAR ADCs enter a low power mode */ + AD7606_STANDBY, + /** Autostandby mode, available only in software mode */ + AD7606_AUTOSTANDBY, + /** Shutdown mode, all circuitry is powered down */ + AD7606_SHUTDOWN +}; + +/** + * @enum ad7606_dout_format + * @brief Number of DOUT lines + */ +enum ad7606_dout_format { + /** DOUT A line is used */ + AD7606_1_DOUT, + /** DOUT A,B lines are used. */ + AD7606_2_DOUT, + /** DOUT A,B,C,D lines are used. */ + AD7606_4_DOUT, + /** DOUT A,B,C,D,E,F,G,H lines are used. */ + AD7606_8_DOUT +}; + +/** + * @struct ad7606_config + * @brief AD7606_REG_CONFIG configuration parameters + */ +struct ad7606_config { + /** Operation mode */ + enum ad7606_op_mode op_mode; + /** Number of DOUT lines */ + enum ad7606_dout_format dout_format; + /** External oversampling clock switch */ + bool ext_os_clock; + /** Status header switch */ + bool status_header; +}; + +/** + * @struct ad7606_range + * @brief Operation range as specified in datasheet (in uV) + */ +struct ad7606_range { + /** Minimum range value (may be negative) */ + int32_t min; + /** Maximum range value */ + int32_t max; + /** Whether the range is differential */ + bool differential; +}; + +/** + * @struct ad7606_digital_diag + * @brief Oversampling settings + */ +struct ad7606_oversampling { + /** Oversampling padding */ + uint8_t os_pad:4; + /** Oversampling ratio */ + enum ad7606_osr os_ratio:4; +}; + +/** + * @struct ad7606_digital_diag + * @brief Digital diagnostics configuration switches + */ +struct ad7606_digital_diag { + /** ROM CRC check switch */ + bool rom_crc_err_en: 1; + /** Mempry map CRC check switch */ + bool mm_crc_err_en: 1; + /** Conversion and register data CRC check switch */ + bool int_crc_err_en: 1; + /** SPI write error switch */ + bool spi_write_err_en: 1; + /** SPI read error switch */ + bool spi_read_err_en: 1; + /** Busy stuck high for more than 4us error switch */ + bool busy_stuck_high_err_en: 1; + /** Frame sync and oversampling clock counter switch */ + bool clk_fs_os_counter_en: 1; + /** Interface check switch */ + bool interface_check_en: 1; +}; + +/** + * @struct ad7606_dev + * @brief Device driver structure + */ +struct ad7606_dev { + /** SPI descriptor*/ + spi_desc *spi_desc; + /** RESET GPIO descriptor */ + struct gpio_desc *gpio_reset; + /** CONVST GPIO descriptor */ + struct gpio_desc *gpio_convst; + /** BUSY GPIO descriptor */ + struct gpio_desc *gpio_busy; + /** STBYn GPIO descriptor */ + struct gpio_desc *gpio_stby_n; + /** RANGE GPIO descriptor */ + struct gpio_desc *gpio_range; + /** OS0 GPIO descriptor */ + struct gpio_desc *gpio_os0; + /** OS1 GPIO descriptor */ + struct gpio_desc *gpio_os1; + /** OS2 GPIO descriptor */ + struct gpio_desc *gpio_os2; + /** PARn/SER GPIO descriptor */ + struct gpio_desc *gpio_par_ser; + /** Device ID */ + enum ad7606_device_id device_id; + /** Oversampling settings */ + struct ad7606_oversampling oversampling; + /** Whether the device is running in hardware or software mode */ + bool sw_mode; + /** Whether the device is running in register or ADC reading mode */ + bool reg_mode; + /** Number of DOUT lines supported by the device */ + enum ad7606_dout_format max_dout_lines; + /** Configuration register settings */ + struct ad7606_config config; + /** Digital diagnostics register settings */ + struct ad7606_digital_diag digital_diag_enable; + /** Number of input channels of the device */ + uint8_t num_channels; + /** Channel offset calibration */ + int8_t offset_ch[AD7606_MAX_CHANNELS]; + /** Channel phase calibration */ + uint8_t phase_ch[AD7606_MAX_CHANNELS]; + /** Channel gain calibration */ + uint8_t gain_ch[AD7606_MAX_CHANNELS]; + /** Channel operating range */ + struct ad7606_range range_ch[AD7606_MAX_CHANNELS]; + /** Data buffer (used internally by the SPI communication functions) */ + uint8_t data[28]; +}; + +/** + * @struct ad7606_dev + * @brief Device driver initialization parameters + */ +struct ad7606_init_param { + /** SPI initialization parameters */ + spi_init_param spi_init; + /** RESET GPIO initialization parameters */ + struct gpio_init_param *gpio_reset; + /** CONVST GPIO initialization parameters */ + struct gpio_init_param *gpio_convst; + /** BUSY GPIO initialization parameters */ + struct gpio_init_param *gpio_busy; + /** STBYn GPIO initialization parameters */ + struct gpio_init_param *gpio_stby_n; + /** RANGE GPIO initialization parameters */ + struct gpio_init_param *gpio_range; + /** OS0 GPIO initialization parameters */ + struct gpio_init_param *gpio_os0; + /** OS1 GPIO initialization parameters */ + struct gpio_init_param *gpio_os1; + /** OS2 GPIO initialization parameters */ + struct gpio_init_param *gpio_os2; + /** PARn/SER GPIO initialization parameters */ + struct gpio_init_param *gpio_par_ser; + /** Device ID */ + enum ad7606_device_id device_id; + /** Oversampling settings */ + struct ad7606_oversampling oversampling; + /** Whether the device is running in hardware or software mode */ + bool sw_mode; + /** Configuration register settings */ + struct ad7606_config config; + /** Digital diagnostics register settings */ + struct ad7606_digital_diag digital_diag_enable; + /** Channel offset calibration */ + int8_t offset_ch[AD7606_MAX_CHANNELS]; + /** Channel phase calibration */ + uint8_t phase_ch[AD7606_MAX_CHANNELS]; + /** Channel gain calibration */ + uint8_t gain_ch[AD7606_MAX_CHANNELS]; + /** Channel operating range */ + struct ad7606_range range_ch[AD7606_MAX_CHANNELS]; +}; + +int32_t ad7606_spi_reg_read(struct ad7606_dev *dev, + uint8_t reg_addr, + uint8_t *reg_data); +int32_t ad7606_spi_reg_write(struct ad7606_dev *dev, + uint8_t reg_addr, + uint8_t reg_data); +int32_t ad7606_spi_write_mask(struct ad7606_dev *dev, + uint32_t addr, + uint32_t mask, + uint32_t val); +int32_t ad7606_spi_data_read(struct ad7606_dev *dev, + uint32_t *data); +int32_t ad7606_read(struct ad7606_dev *dev, + uint32_t *data); +int32_t ad7606_convst(struct ad7606_dev *dev); +int32_t ad7606_reset(struct ad7606_dev *dev); +int32_t ad7606_set_oversampling(struct ad7606_dev *dev, + struct ad7606_oversampling oversampling); +int32_t ad7606_set_ch_range(struct ad7606_dev *dev, uint8_t ch, + struct ad7606_range range); +int32_t ad7606_set_ch_offset(struct ad7606_dev *dev, uint8_t ch, + int8_t offset); +int32_t ad7606_set_ch_phase(struct ad7606_dev *dev, uint8_t ch, + uint8_t phase); +int32_t ad7606_set_ch_gain(struct ad7606_dev *dev, uint8_t ch, + uint8_t gain); +int32_t ad7606_set_config(struct ad7606_dev *dev, + struct ad7606_config config); +int32_t ad7606_set_digital_diag(struct ad7606_dev *dev, + struct ad7606_digital_diag diag); +int32_t ad7606_init(struct ad7606_dev **device, + struct ad7606_init_param *init_param); +int32_t ad7606_remove(struct ad7606_dev *dev); +#endif /* AD7606_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/dac/ad5686/ad5686.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,764 @@ +/***************************************************************************//** +* @file ad5686.c +* @brief Implementation of AD5686 Driver. +* @author Istvan Csomortani (istvan.csomortani@analog.com) +******************************************************************************* +* Copyright 2013, 2020(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS +* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +* DAMAGE. +* +******************************************************************************/ + +/*****************************************************************************/ +/***************************** Include Files *********************************/ +/*****************************************************************************/ +#include <stdlib.h> +#include "ad5686.h" + +/*****************************************************************************/ +/***************************** Constant definition ***************************/ +/*****************************************************************************/ +static const uint32_t ad5683_channel_addr [] = { + [AD5686_CH_0] = 0, +}; + +static const uint32_t ad5689_channel_addr[] = { + [AD5686_CH_0] = 1, + [AD5686_CH_1] = 8, +}; + +static const uint32_t ad5686_channel_addr[] = { + [AD5686_CH_0] = 1, + [AD5686_CH_1] = 2, + [AD5686_CH_2] = 4, + [AD5686_CH_3] = 8, +}; + +static const uint32_t ad5676_channel_addr[] = { + [AD5686_CH_0] = 0, + [AD5686_CH_1] = 1, + [AD5686_CH_2] = 2, + [AD5686_CH_3] = 3, + [AD5686_CH_4] = 4, + [AD5686_CH_5] = 5, + [AD5686_CH_6] = 6, + [AD5686_CH_7] = 7, +}; + +static const uint32_t ad5679_channel_addr[] = { + [AD5686_CH_0] = 0, + [AD5686_CH_1] = 1, + [AD5686_CH_2] = 2, + [AD5686_CH_3] = 3, + [AD5686_CH_4] = 4, + [AD5686_CH_5] = 5, + [AD5686_CH_6] = 6, + [AD5686_CH_7] = 7, + [AD5686_CH_8] = 8, + [AD5686_CH_9] = 9, + [AD5686_CH_10] = 10, + [AD5686_CH_11] = 11, + [AD5686_CH_12] = 12, + [AD5686_CH_13] = 13, + [AD5686_CH_14] = 14, + [AD5686_CH_15] = 15, +}; + +static const struct ad5686_chip_info chip_info[] = { + [ID_AD5671R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5676_channel_addr, + }, + [ID_AD5672R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5676_channel_addr, + }, + [ID_AD5673R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5679_channel_addr, + }, + [ID_AD5674] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5679_channel_addr, + }, + [ID_AD5674R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5679_channel_addr, + }, + [ID_AD5675R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5676_channel_addr, + }, + [ID_AD5676] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5676_channel_addr, + }, + [ID_AD5676R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5676_channel_addr, + }, + [ID_AD5677R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5679_channel_addr, + }, + [ID_AD5679] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5679_channel_addr, + }, + [ID_AD5679R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5679_channel_addr, + }, + [ID_AD5684R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5685R] = { + .resolution = 14, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5686] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5686R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5687] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5689_channel_addr, + }, + [ID_AD5687R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5689_channel_addr, + }, + [ID_AD5689] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5689_channel_addr, + }, + [ID_AD5689R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = SPI, + .channel_addr = ad5689_channel_addr, + }, + [ID_AD5697R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5689_channel_addr, + }, + [ID_AD5694] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5694R] = { + .resolution = 12, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5695R] = { + .resolution = 14, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5696] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5696R] = { + .resolution = 16, + .register_map = AD5686_REG_MAP, + .communication = I2C, + .channel_addr = ad5686_channel_addr, + }, + [ID_AD5681R] = { + .resolution = 12, + .register_map = AD5683_REG_MAP, + .communication = SPI, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5682R] = { + .resolution = 14, + .register_map = AD5683_REG_MAP, + .communication = SPI, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5683R] = { + .resolution = 16, + .register_map = AD5683_REG_MAP, + .communication = SPI, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5683] = { + .resolution = 16, + .register_map = AD5683_REG_MAP, + .communication = SPI, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5691R] = { + .resolution = 12, + .register_map = AD5683_REG_MAP, + .communication = I2C, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5692R] = { + .resolution = 14, + .register_map = AD5683_REG_MAP, + .communication = I2C, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5693R] = { + .resolution = 16, + .register_map = AD5683_REG_MAP, + .communication = I2C, + .channel_addr = ad5683_channel_addr, + }, + [ID_AD5693] = { + .resolution = 16, + .register_map = AD5683_REG_MAP, + .communication = I2C, + .channel_addr = ad5683_channel_addr, + } +}; + +/***************************************************************************//** + * @brief Initializes the communication peripheral and the initial Values for + * AD5686 Board. + * + * @param device - The device structure. + * @param init_param - The structure that contains the device initial + * parameters. + * + * @return ret - The result of the initialization procedure. + * Example: -1 - I2C peripheral was not initialized or the + * device is not present. + * 0 - I2C peripheral was initialized and the + * device is present. +*******************************************************************************/ +int32_t ad5686_init(struct ad5686_dev **device, + struct ad5686_init_param init_param) +{ + struct ad5686_dev *dev; + int32_t ret; + + dev = (struct ad5686_dev *)malloc(sizeof(*dev)); + if (!dev) + return -1; + + dev->act_device = init_param.act_device; + dev->power_down_mask = 0; + dev->ldac_mask = 0; + + if (chip_info[dev->act_device].communication == SPI) + ret = spi_init(&dev->spi_desc, &init_param.spi_init); + else + ret = i2c_init(&dev->i2c_desc, &init_param.i2c_init); + + + /* GPIO */ + ret |= gpio_get(&dev->gpio_reset, &init_param.gpio_reset); + ret |= gpio_get(&dev->gpio_ldac, &init_param.gpio_ldac); + ret |= gpio_get(&dev->gpio_gain, &init_param.gpio_gain); + + if (dev->gpio_ldac) + ret |= gpio_direction_output(dev->gpio_ldac, GPIO_LOW); + + if (dev->gpio_reset) + ret |= gpio_direction_output(dev->gpio_reset, GPIO_HIGH); + + if (dev->gpio_gain) + ret |= gpio_direction_output(dev->gpio_gain, GPIO_LOW); + *device = dev; + + return ret; +} + +/***************************************************************************//** + * @brief Free the resources allocated by ad5686_init(). + * + * @param dev - The device structure. + * + * @return ret - The result of the remove procedure. +*******************************************************************************/ +int32_t ad5686_remove(struct ad5686_dev *dev) +{ + int32_t ret; + + if (chip_info[dev->act_device].communication == SPI) + ret = spi_remove(dev->spi_desc); + else + ret = i2c_remove(dev->i2c_desc); + + if (dev->gpio_ldac) + ret |= gpio_remove(dev->gpio_ldac); + + if (dev->gpio_reset) + ret |= gpio_remove(dev->gpio_reset); + + if (dev->gpio_gain) + ret |= gpio_remove(dev->gpio_gain); + + free(dev); + + return ret; +} + +/**************************************************************************//** + * @brief Write to input shift register. + * + * @param dev - The device structure. + * @param command - Command control bits. + * @param address - The address bits. + * @param data - Data to be written in input register. + * + * @return read_back_data - value read from register. +******************************************************************************/ +uint16_t ad5686_set_shift_reg(struct ad5686_dev *dev, + uint8_t command, + uint8_t address, + uint16_t data) +{ + uint8_t data_buff [ PKT_LENGTH ] = {0, 0, 0}; + uint16_t read_back_data = 0; + + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) { + data_buff[0] = ((command & AD5686_CMD_MASK) << CMD_OFFSET) | \ + (address & ADDR_MASK); + data_buff[1] = (data & AD5686_MSB_MASK) >> AD5686_MSB_OFFSET; + data_buff[2] = (data & AD5686_LSB_MASK); + } else { + data_buff[0] = ((command & AD5683_CMD_MASK) << CMD_OFFSET) | + ((data >> AD5683_MSB_OFFSET) & AD5683_MSB_MASK); + data_buff[1] = (data >> AD5683_MIDB_OFFSET) & AD5683_MIDB_MASK; + data_buff[2] = (data & AD5683_LSB_MASK) << AD5683_LSB_OFFSET; + } + + if(chip_info[dev->act_device].communication == SPI) { + spi_write_and_read(dev->spi_desc, data_buff, PKT_LENGTH); + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) + read_back_data = (data_buff[1] << AD5686_MSB_OFFSET) | data_buff[2]; + else + read_back_data = (data_buff[0] & AD5683_CMD_MASK) << AD5683_MSB_OFFSET | + data_buff[1] << AD5683_MIDB_OFFSET | + data_buff[2] >> AD5683_LSB_OFFSET; + } else + i2c_write(dev->i2c_desc, data_buff, PKT_LENGTH, 1); + + return read_back_data; +} + +/**************************************************************************//** + * @brief Write to Input Register n (dependent on LDAC) + * + * @param dev - The device structure. + * @param channel - The chosen channel to write to. + * Accepted values: AD5686_CH_0 + * AD5686_CH_1 + * AD5686_CH_2 + * AD5686_CH_3 + * AD5686_CH_4 + * AD5686_CH_5 + * AD5686_CH_6 + * AD5686_CH_7 + * AD5686_CH_8 + * AD5686_CH_9 + * AD5686_CH_10 + * AD5686_CH_11 + * AD5686_CH_12 + * AD5686_CH_13 + * AD5686_CH_14 + * AD5686_CH_15 + * @param data - desired value to be written in register. + * + * @return None. +******************************************************************************/ +void ad5686_write_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint16_t data) +{ + uint8_t data_offset = MAX_RESOLUTION - \ + chip_info[dev->act_device].resolution; + uint8_t address = chip_info[dev->act_device].channel_addr[channel]; + + ad5686_set_shift_reg(dev, AD5686_CTRL_WRITE, address, + data << data_offset); +} + +/**************************************************************************//** + * @brief Update DAC Register n with contents of Input Register n + * + * @param dev - The device structure. + * @param channel - The chosen channel to write to. + * Accepted values: AD5686_CH_0 + * AD5686_CH_1 + * AD5686_CH_2 + * AD5686_CH_3 + * AD5686_CH_4 + * AD5686_CH_5 + * AD5686_CH_6 + * AD5686_CH_7 + * AD5686_CH_8 + * AD5686_CH_9 + * AD5686_CH_10 + * AD5686_CH_11 + * AD5686_CH_12 + * AD5686_CH_13 + * AD5686_CH_14 + * AD5686_CH_15 + * @return None. +******************************************************************************/ +void ad5686_update_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel) +{ + uint8_t address = chip_info[dev->act_device].channel_addr[channel]; + + ad5686_set_shift_reg(dev, AD5686_CTRL_UPDATE, address, 0); +} + +/**************************************************************************//** + * @brief Write to and update DAC channel n + * + * @param dev - The device structure. + * @param channel - The chosen channel to write to. + * Accepted values: AD5686_CH_0 + * AD5686_CH_1 + * AD5686_CH_2 + * AD5686_CH_3 + * AD5686_CH_4 + * AD5686_CH_5 + * AD5686_CH_6 + * AD5686_CH_7 + * AD5686_CH_8 + * AD5686_CH_9 + * AD5686_CH_10 + * AD5686_CH_11 + * AD5686_CH_12 + * AD5686_CH_13 + * AD5686_CH_14 + * AD5686_CH_15 + * @param data - Desired value to be written in register. + * + * @return None. +******************************************************************************/ +void ad5686_write_update_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint16_t data) +{ + uint8_t data_offset = MAX_RESOLUTION - \ + chip_info[dev->act_device].resolution; + uint8_t address = chip_info[dev->act_device].channel_addr[channel]; + + ad5686_set_shift_reg(dev, AD5686_CTRL_WRITEUPDATE, address, + data << data_offset); +} + +/**************************************************************************//** + * @brief Read back Input Register n + * + * @param dev - The device structure. + * @param channel - The channel which will be read back. Note: only one + * channel should be selected, if there will be selected + * more than one channel, the channel A will be read back + * by default + * Accepted values: AD5686_CH_0 + * AD5686_CH_1 + * AD5686_CH_2 + * AD5686_CH_3 + * AD5686_CH_4 + * AD5686_CH_5 + * AD5686_CH_6 + * AD5686_CH_7 + * AD5686_CH_8 + * AD5686_CH_9 + * AD5686_CH_10 + * AD5686_CH_11 + * AD5686_CH_12 + * AD5686_CH_13 + * AD5686_CH_14 + * AD5686_CH_15 + * @return read_back_data - value read from register. +******************************************************************************/ +uint16_t ad5686_read_back_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel) +{ + + uint16_t read_back_data = 0; + uint16_t offset = MAX_RESOLUTION - \ + chip_info[dev->act_device].resolution; + uint8_t address = chip_info[dev->act_device].channel_addr[channel]; + uint8_t rb_data_i2c[3] = { 0 }; + + if(chip_info[dev->act_device].communication == SPI) { + ad5686_set_shift_reg(dev, AD5686_CTRL_RB_REG, address, 0); + read_back_data = ad5686_set_shift_reg(dev, AD5686_CTRL_NOP, 0, + 0); + read_back_data >>= offset; + } else { + if (chip_info[dev->act_device].register_map == AD5683_REG_MAP) + rb_data_i2c[0] = (AD5683_CTRL_RB_REG << CMD_OFFSET) | + address; + else + rb_data_i2c[0] = (AD5686_CTRL_RB_REG << CMD_OFFSET) | + address; + + i2c_write(dev->i2c_desc, rb_data_i2c, 3, 0); + i2c_read(dev->i2c_desc, rb_data_i2c, 2, 1); + read_back_data = (rb_data_i2c[0] << 8) | rb_data_i2c[1]; + } + + return read_back_data; +} + +/**************************************************************************//** + * @brief Set Power-down mode for DAC channel n + * + * @param dev - The device structure. + * @param channel - The chosen channel to change the power-down mode. + * Accepted values: AD5686_CH_0 + * AD5686_CH_1 + * AD5686_CH_2 + * AD5686_CH_3 + * AD5686_CH_4 + * AD5686_CH_5 + * AD5686_CH_6 + * AD5686_CH_7 + * AD5686_CH_8 + * AD5686_CH_9 + * AD5686_CH_10 + * AD5686_CH_11 + * AD5686_CH_12 + * AD5686_CH_13 + * AD5686_CH_14 + * AD5686_CH_15 + * @param mode - Power-down operation modes. + * Accepted values: + * 'AD5686_PWRM_NORMAL' - Normal Mode + * 'AD5686_PWRM_1K' - Power-down mode 1kOhm to GND + * 'AD5686_PWRM_100K' - Power-down mode 100kOhm to GND + * 'AD5686_PWRM_THREESTATE' - Three-State + * 'AD5686_PWRM_100K' is not available for AD5674R/AD5679R. + * + * @return None. +******************************************************************************/ +void ad5686_power_mode(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint8_t mode) +{ + uint8_t address = chip_info[dev->act_device].channel_addr[channel]; + + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) { + /* AD5674R/AD5679R have 16 channels and 2 powerdown registers */ + if (channel > AD5686_CH_7) + channel -= AD5686_CH_7 + 1; + dev->power_down_mask &= ~(0x3 << (channel *2)); + dev->power_down_mask |= (mode << (channel *2)); + ad5686_set_shift_reg(dev, AD5686_CTRL_PWR, address, + dev->power_down_mask); + } else { + ad5686_set_shift_reg(dev, AD5683_CMD_WR_CTRL_REG, address, + AD5683_CTRL_PWRM(mode)); + } + +} + +/**************************************************************************//** + * @brief Set hardware LDAC mask register + * + * @param dev - The device structure. + * @param channel - In case of which channel ignore transitions on the LDAC + * pin. + * Accepted values: AD5686_CH_0 + * AD5686_CH_1 + * AD5686_CH_2 + * AD5686_CH_3 + * AD5686_CH_4 + * AD5686_CH_5 + * AD5686_CH_6 + * AD5686_CH_7 + * AD5686_CH_8 + * AD5686_CH_9 + * AD5686_CH_10 + * AD5686_CH_11 + * AD5686_CH_12 + * AD5686_CH_13 + * AD5686_CH_14 + * AD5686_CH_15 + * @param enable - Enable/disable channel. + * @return None. +******************************************************************************/ +void ad5686_ldac_mask(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint8_t enable) +{ + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) { + dev->ldac_mask &= ~(0x1 << channel); + dev->ldac_mask |= (enable << channel); + ad5686_set_shift_reg(dev, AD5686_CTRL_LDAC_MASK, 0, dev->ldac_mask); + } +} + +/**************************************************************************//** + * @brief Software reset (power-on reset) + * + * @param dev - The device structure. + * + * @return None. +******************************************************************************/ +void ad5686_software_reset(struct ad5686_dev *dev) +{ + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) + ad5686_set_shift_reg(dev, AD5686_CTRL_SWRESET, 0, 0); + else + ad5686_set_shift_reg(dev, AD5683_CMD_WR_CTRL_REG, 0, AD5683_SW_RESET); +} + + +/**************************************************************************//** + * @brief Write to Internal reference setup register + * + * @param dev - The device structure. + * @param value - The internal reference register value + * Example : 'AD5686_INTREF_EN' - enable internal reference + * 'AD5686_INTREF_DIS' - disable internal reference + * + * @return None. +******************************************************************************/ +void ad5686_internal_reference(struct ad5686_dev *dev, + uint8_t value) +{ + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) + ad5686_set_shift_reg(dev, AD5686_CTRL_IREF_REG, 0, value); + else + ad5686_set_shift_reg(dev, AD5683_CMD_WR_CTRL_REG, 0, + AD5683_CTRL_INT_REF(value)); +} + +/**************************************************************************//** + * @brief Set up DCEN register (daisy-chain enable) + * + * @param dev - The device structure. + * @param value - Enable or disable daisy-chain mode + * Example : 'AD5686_DC_EN' - daisy-chain enable + * 'AD5686_DC_DIS' - daisy-chain disable + * + * @return None. +******************************************************************************/ +void ad5686_daisy_chain_en(struct ad5686_dev *dev, + uint8_t value) +{ + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) + ad5686_set_shift_reg(dev, AD5686_CTRL_DCEN, 0, value); + else + ad5686_set_shift_reg(dev, AD5683_CMD_WR_CTRL_REG, 0, AD5683_CTRL_DCEN(value)); +} + +/**************************************************************************//** + * @brief Set up readback register (readback enable) + * + * @param dev - The device structure. + * @param value - Enable or disable daisy-chain mode + * Example : 'AD5686_RB_EN' - daisy-chain enable + * 'AD5686_RB_DIS' - daisy-chain disable + * + * @return None. +******************************************************************************/ +void ad5686_read_back_en(struct ad5686_dev *dev, + uint8_t value) +{ + if(chip_info[dev->act_device].register_map == AD5686_REG_MAP) + ad5686_set_shift_reg(dev, AD5686_CTRL_RB_REG, 0, value); +} + +/**************************************************************************//** + * @brief Set Gain mode + * + * @param dev - The device structure. + * @param value - Gain modes. + * Accepted values: + * Example : 'AD5683_GB_VREF' - 0V to VREF + * 'AD5683_GB_2VREF' - 0V to 2xVREF + * + * @return None. +******************************************************************************/ +int32_t ad5686_gain_mode(struct ad5686_dev *dev, uint8_t value) +{ + if(chip_info[dev->act_device].register_map == AD5683_REG_MAP) + return ad5686_set_shift_reg(dev, AD5683_CMD_WR_CTRL_REG, 0, + AD5683_CTRL_GM(value)); + return -1; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/dac/ad5686/ad5686.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,274 @@ +/***************************************************************************//** +* @file ad5686.h +* @brief Header file of AD5686 Driver. This driver supporting the following +* devices: AD5684R, AD5685R, AD5686R, AD5694R, AD5695R, AD5696R, +* +* @author Istvan Csomortani (istvan.csomortani@analog.com) +******************************************************************************** +* Copyright 2013, 2020(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, +* are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR +* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, +* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS +* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +* DAMAGE. +* +*******************************************************************************/ + +/*****************************************************************************/ +/***************************** Include Files *********************************/ +/*****************************************************************************/ +#include <stdint.h> +#include "i2c.h" + +/* Control Bits */ +#define AD5686_CTRL_NOP 0 +#define AD5686_CTRL_WRITE 1 +#define AD5686_CTRL_UPDATE 2 +#define AD5686_CTRL_WRITEUPDATE 3 +#define AD5686_CTRL_PWR 4 +#define AD5686_CTRL_LDAC_MASK 5 +#define AD5686_CTRL_SWRESET 6 +#define AD5686_CTRL_IREF_REG 7 +#define AD5686_CTRL_DCEN 8 +#define AD5686_CTRL_RB_REG 9 + +#define AD5683_CMD_WR_CTRL_REG 4 +#define AD5683_CTRL_RB_REG 5 + +/* Power-down operation modes masks */ +#define AD5686_PWRM_NORMAL 0 +#define AD5686_PWRM_1K 1 +#define AD5686_PWRM_100K 2 +#define AD5686_PWRM_THREESTATE 3 + +#define AD5686_PWRM_MASK 3 + +/* Enable/disable defines */ +#define AD5686_INTREF_EN 1 +#define AD5686_INTREF_DIS 0 +#define AD5686_DC_EN 1 +#define AD5686_DC_DIS 0 +#define AD5686_RB_EN 1 +#define AD5686_RB_DIS 0 + +#define MAX_RESOLUTION 16 // Maximum resolution of the supported devices + +#define PKT_LENGTH 3 // SPI packet length in byte + +#define ADDR_MASK 0xFF // Mask for Address bits +#define CMD_OFFSET 4 // Offset for Command + +#define AD5686_CMD_MASK 0xFF +#define AD5686_MSB_MASK 0xFF00 // Most significant byte of the data word +#define AD5686_MSB_OFFSET 8 +#define AD5686_LSB_MASK 0x00FF // Least significant byte of the data word +#define AD5686_LSB_OFFSET 0 + +#define AD5683_MIDB_OFFSET 4 // Offset for middle bits +#define AD5683_MIDB_MASK 0xFF +#define AD5683_MSB_OFFSET 12 +#define AD5683_MSB_MASK 0xF +#define AD5683_CMD_MASK 0xF +#define AD5683_LSB_MASK 0xF +#define AD5683_LSB_OFFSET 4 + +#define AD5683_REG_MAP 0 +#define AD5686_REG_MAP 1 + +/********************** AD5683 Write Control Register Bits ********************/ + +#define AD5683_CTRL_DCEN(x) (((((x) & 0x1) << 0) << 10) & 0xFC00) +#define AD5683_CTRL_GM(x) (((((x) & 0x1) << 1) << 10) & 0xFC00) +#define AD5683_CTRL_INT_REF(x) (((((x) & 0x1) << 2) << 10) & 0xFC00) +#define AD5683_CTRL_PWRM(x) (((((x) & 0x3) << 3) << 10) & 0xFC00) +#define AD5683_SW_RESET ((((0x1) << 5) << 10) & 0xFC00) + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/* Supported devices */ +enum ad5686_type { + ID_AD5671R, + ID_AD5672R, + ID_AD5673R, + ID_AD5674, + ID_AD5674R, + ID_AD5675R, + ID_AD5676, + ID_AD5676R, + ID_AD5677R, + ID_AD5679, + ID_AD5679R, + ID_AD5686, + ID_AD5684R, + ID_AD5685R, + ID_AD5686R, + ID_AD5687, + ID_AD5687R, + ID_AD5689, + ID_AD5689R, + ID_AD5697R, + ID_AD5694, + ID_AD5694R, + ID_AD5695R, + ID_AD5696, + ID_AD5696R, + ID_AD5681R, + ID_AD5682R, + ID_AD5683R, + ID_AD5683, + ID_AD5691R, + ID_AD5692R, + ID_AD5693R, + ID_AD5693 +}; + +enum comm_type { + SPI, + I2C, +}; + +enum ad5686_dac_channels { + AD5686_CH_0 = 0, + AD5686_CH_1, + AD5686_CH_2, + AD5686_CH_3, + AD5686_CH_4, + AD5686_CH_5, + AD5686_CH_6, + AD5686_CH_7, + AD5686_CH_8, + AD5686_CH_9, + AD5686_CH_10, + AD5686_CH_11, + AD5686_CH_12, + AD5686_CH_13, + AD5686_CH_14, + AD5686_CH_15, +}; + +struct ad5686_chip_info { + uint8_t resolution; + uint8_t register_map; + enum comm_type communication; + const uint32_t *channel_addr; +}; + +struct ad5686_dev { + /* I2C */ + i2c_desc *i2c_desc; + /* SPI */ + spi_desc *spi_desc; + /* GPIO */ + struct gpio_desc *gpio_reset; + struct gpio_desc *gpio_ldac; + struct gpio_desc *gpio_gain; + /* Device Settings */ + enum ad5686_type act_device; + uint32_t power_down_mask; + uint32_t ldac_mask; +}; + +struct ad5686_init_param { + /* I2C */ + i2c_init_param i2c_init; + /* SPI */ + spi_init_param spi_init; + /* GPIO */ + struct gpio_init_param gpio_reset; + struct gpio_init_param gpio_ldac; + struct gpio_init_param gpio_gain; + /* Device Settings */ + enum ad5686_type act_device; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ +/* Initialize SPI and Initial Values for AD5686 Board. */ +int32_t ad5686_init(struct ad5686_dev **device, + struct ad5686_init_param init_param); + +/* Free the resources allocated by ad5686_init(). */ +int32_t ad5686_remove(struct ad5686_dev *dev); + +/* Write to input register */ +uint16_t ad5686_set_shift_reg(struct ad5686_dev *dev, + uint8_t command, + uint8_t address, + uint16_t data); + +/* Write to Input Register n (dependent on LDAC) */ +void ad5686_write_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint16_t data); + +/* Update DAC Register n with contents of Input Register n */ +void ad5686_update_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel); + +/* Write to and update DAC channel n */ +void ad5686_write_update_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint16_t data); + +/* Read back Input Register n */ +uint16_t ad5686_read_back_register(struct ad5686_dev *dev, + enum ad5686_dac_channels channel); + +/* Power down / power up DAC */ +void ad5686_power_mode(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint8_t mode); + +/* Set up LDAC mask register */ +void ad5686_ldac_mask(struct ad5686_dev *dev, + enum ad5686_dac_channels channel, + uint8_t enable); + +/* Software reset (power-on reset) */ +void ad5686_software_reset(struct ad5686_dev *dev); + +/* Write to Internal reference setup register */ +void ad5686_internal_reference(struct ad5686_dev *dev, + uint8_t value); + +/* Set up DCEN register (daisy-chain enable) */ +void ad5686_daisy_chain_en(struct ad5686_dev *dev, + uint8_t value); + +/* Set up readback register (readback enable) */ +void ad5686_read_back_en(struct ad5686_dev *dev, + uint8_t value); + +/* Set Gain mode */ +int32_t ad5686_gain_mode(struct ad5686_dev *dev, uint8_t value);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/impedance-analyzer/ad5933/ad5933.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,494 @@ +/***************************************************************************//** + * @file AD5933.c + * @brief Implementation of AD5933 Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2012(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/*****************************************************************************/ +/***************************** Include Files *********************************/ +/*****************************************************************************/ +#include <stdint.h> +#include <stdlib.h> +#include "platform_drivers.h" +#include "ad5933.h" + + +/******************************************************************************/ +/************************** Constants Definitions *****************************/ +/******************************************************************************/ +const int32_t pow_2_27 = 134217728ul; // 2 to the power of 27 + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/***************************************************************************//** + * @brief Initializes the communication peripheral and the initial Values for + * AD5933 Board. + * + * @param device - The device structure. + * @param init_param - The structure that contains the device initial + * parameters. + * + * @return ret - The result of the initialization procedure. + * Example: -1 - I2C peripheral was not initialized or the + * device is not present. + * 0 - I2C peripheral was initialized and the + * device is present. +*******************************************************************************/ +int32_t ad5933_init(struct ad5933_dev **device, + struct ad5933_init_param init_param) +{ + struct ad5933_dev *dev; + int32_t status; + + dev = (struct ad5933_dev *)malloc(sizeof(*dev)); + if (!dev) + return -1; + + dev->current_sys_clk = init_param.current_sys_clk; + dev->current_clock_source = init_param.current_clock_source; + dev->current_gain = init_param.current_gain; + dev->current_range = init_param.current_range; + dev->current_settling = init_param.current_settling; + + status = i2c_init(&dev->i2c_desc, &init_param.i2c_init); + + *device = dev; + + return status; +} + +/***************************************************************************//** + * @brief Free the resources allocated by ad5933_init(). + * + * @param dev - The device structure. + * + * @return ret - The result of the remove procedure. +*******************************************************************************/ +int32_t ad5933_remove(struct ad5933_dev *dev) +{ + int32_t status; + + status = i2c_remove(dev->i2c_desc); + + free(dev); + + return status; +} + +/***************************************************************************//** + * @brief Writes data into a register. + * + * @param dev - The device structure. + * @param register_address - Address of the register. + * @param register_value - Data value to write. + * @param bytes_number - Number of bytes. + * + * @return None. +*******************************************************************************/ +void ad5933_set_register_value(struct ad5933_dev *dev, + uint8_t register_address, + uint32_t register_value, + uint8_t bytes_number) +{ + uint8_t byte = 0; + uint8_t write_data[2] = {0, 0}; + + for(byte = 0; byte < bytes_number; byte++) { + write_data[0] = register_address + bytes_number - byte - 1; + write_data[1] = (uint8_t)((register_value >> (byte * 8)) & 0xFF); + i2c_write(dev->i2c_desc, write_data, 2, 1); + } +} + +/***************************************************************************//** + * @brief Reads the value of a register. + * + * @param dev - The device structure. + * @param register_address - Address of the register. + * @param bytes_number - Number of bytes. + * + * @return registerValue - Value of the register. +*******************************************************************************/ +uint32_t ad5933_get_register_value(struct ad5933_dev *dev, + uint8_t register_address, + uint8_t bytes_number) +{ + uint32_t register_value = 0; + uint8_t byte = 0; + uint8_t write_data[2] = {0, 0}; + uint8_t read_data[2] = {0, 0}; + + for(byte = 0; byte < bytes_number; byte ++) { + /* Set the register pointer. */ + write_data[0] = AD5933_ADDR_POINTER; + write_data[1] = register_address + byte; + i2c_write(dev->i2c_desc, write_data, 2, 1); + /* Read Register Data. */ + read_data[0] = 0xFF; + i2c_read(dev->i2c_desc, read_data, 1, 1); + register_value = register_value << 8; + register_value += read_data[0]; + } + + return register_value; +} + +/***************************************************************************//** + * @brief Resets the device. + * + * @param dev - The device structure. + * + * @return None. +*******************************************************************************/ +void ad5933_reset(struct ad5933_dev *dev) +{ + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_LB, + AD5933_CONTROL_RESET | dev->current_clock_source, + 1); +} + +/***************************************************************************//** + * @brief Selects the source of the system clock. + * + * @param dev - The device structure. + * @param clk_source - Selects the source of the system clock. + * Example: AD5933_CONTROL_INT_SYSCLK + * AD5933_CONTROL_EXT_SYSCLK + * @param ext_clk_freq - Frequency value of the external clock, if used. + * + * @return None. +*******************************************************************************/ +void ad5933_set_system_clk(struct ad5933_dev *dev, + int8_t clk_source, + uint32_t ext_clk_freq) +{ + dev->current_clock_source = clk_source; + if(clk_source == AD5933_CONTROL_EXT_SYSCLK) { + dev->current_sys_clk = ext_clk_freq; // External clock frequency + } else { + dev->current_sys_clk = AD5933_INTERNAL_SYS_CLK; // 16 MHz + } + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_LB, + dev->current_clock_source, + 1); +} + + +/***************************************************************************//** + * @brief Selects the range and gain of the device. + * + * @param dev - The device structure. + * @param range - Range option. + * Example: AD5933_RANGE_2000mVpp + * AD5933_RANGE_200mVpp + * AD5933_RANGE_400mVpp + + * AD5933_RANGE_1000mVpp + * @param gain - Gain option. + * Example: AD5933_GAIN_X5 + * AD5933_GAIN_X1 + * + * @return None. +*******************************************************************************/ +void ad5933_set_range_and_gain(struct ad5933_dev *dev, + int8_t range, + int8_t gain) +{ + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_NOP) | + AD5933_CONTROL_RANGE(range) | + AD5933_CONTROL_PGA_GAIN(gain), + 1); + /* Store the last settings made to range and gain. */ + dev->current_range = range; + dev->current_gain = gain; +} + +/***************************************************************************//** + * @brief Reads the temperature from the part and returns the data in + * degrees Celsius. + * + * @param dev - The device structure. + * + * @return temperature - Temperature. +*******************************************************************************/ +float ad5933_get_temperature(struct ad5933_dev *dev) +{ + float temperature = 0; + uint8_t status = 0; + + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_MEASURE_TEMP) | + AD5933_CONTROL_RANGE(dev->current_range) | + AD5933_CONTROL_PGA_GAIN(dev->current_gain), + 1); + while((status & AD5933_STAT_TEMP_VALID) == 0) { + status = ad5933_get_register_value(dev, + AD5933_REG_STATUS, + 1); + } + + temperature = ad5933_get_register_value(dev, + AD5933_REG_TEMP_DATA, + 2); + if(temperature < 8192) { + temperature /= 32; + } else { + temperature -= 16384; + temperature /= 32; + } + + return temperature; +} + +/***************************************************************************//** + * @brief Configures the sweep parameters: Start frequency, Frequency increment + * and Number of increments. + * + * @param dev - The device structure. + * @param start_freq - Start frequency in Hz; + * @param inc_freq - Frequency increment in Hz; + * @param inc_num - Number of increments. Maximum value is 511(0x1FF). + * + * @return None. +*******************************************************************************/ +void ad5933_config_sweep(struct ad5933_dev *dev, + uint32_t start_freq, + uint32_t inc_freq, + uint16_t inc_num) +{ + uint32_t start_freq_reg = 0; + uint32_t inc_freq_reg = 0; + uint16_t inc_num_reg = 0; + + /* Ensure that incNum is a valid data. */ + if(inc_num > AD5933_MAX_INC_NUM) { + inc_num_reg = AD5933_MAX_INC_NUM; + } else { + inc_num_reg = inc_num; + } + + /* Convert users start frequency to binary code. */ + start_freq_reg = (uint32_t)((double)start_freq * 4 / dev->current_sys_clk * + pow_2_27); + + /* Convert users increment frequency to binary code. */ + inc_freq_reg = (uint32_t)((double)inc_freq * 4 / dev->current_sys_clk * + pow_2_27); + + /* Configure the device with the sweep parameters. */ + ad5933_set_register_value(dev, + AD5933_REG_FREQ_START, + start_freq_reg, + 3); + ad5933_set_register_value(dev, + AD5933_REG_FREQ_INC, + inc_freq_reg, + 3); + ad5933_set_register_value(dev, + AD5933_REG_INC_NUM, + inc_num_reg, + 2); +} + +/***************************************************************************//** + * @brief Starts the sweep operation. + * + * @param dev - The device structure. + * + * @return None. +*******************************************************************************/ +void ad5933_start_sweep(struct ad5933_dev *dev) +{ + uint8_t status = 0; + + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_STANDBY) | + AD5933_CONTROL_RANGE(dev->current_range) | + AD5933_CONTROL_PGA_GAIN(dev->current_gain), + 1); + ad5933_reset(dev); + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_INIT_START_FREQ)| + AD5933_CONTROL_RANGE(dev->current_range) | + AD5933_CONTROL_PGA_GAIN(dev->current_gain), + 1); + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_START_SWEEP) | + AD5933_CONTROL_RANGE(dev->current_range) | + AD5933_CONTROL_PGA_GAIN(dev->current_gain), + 1); + status = 0; + while((status & AD5933_STAT_DATA_VALID) == 0) { + status = ad5933_get_register_value(dev, + AD5933_REG_STATUS, + 1); + }; +} + +/***************************************************************************//** + * @brief Reads the real and the imaginary data and calculates the Gain Factor. + * + * @param dev - The device structure. + * @param calibration_impedance - The calibration impedance value. + * @param freq_function - Frequency function. + * Example: AD5933_FUNCTION_INC_FREQ - Increment + freq.; + * AD5933_FUNCTION_REPEAT_FREQ - Repeat + freq.. + * + * @return gainFactor - Calculated gain factor. +*******************************************************************************/ +double ad5933_calculate_gain_factor(struct ad5933_dev *dev, + uint32_t calibration_impedance, + uint8_t freq_function) +{ + double gain_factor = 0; + double magnitude = 0; + signed short real_data = 0; + signed short imag_data = 0; + uint8_t status = 0; + + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(freq_function) | + AD5933_CONTROL_RANGE(dev->current_range) | + AD5933_CONTROL_PGA_GAIN(dev->current_gain), + 1); + status = 0; + while((status & AD5933_STAT_DATA_VALID) == 0) { + status = ad5933_get_register_value(dev, + AD5933_REG_STATUS, + 1); + } + real_data = ad5933_get_register_value(dev, + AD5933_REG_REAL_DATA, + 2); + imag_data = ad5933_get_register_value(dev, + AD5933_REG_IMAG_DATA, + 2); + magnitude = sqrt((real_data * real_data) + (imag_data * imag_data)); + gain_factor = 1 / (magnitude * calibration_impedance); + + return gain_factor; +} + +/***************************************************************************//** + * @brief Reads the real and the imaginary data and calculates the Impedance. + * + * @param dev - The device structure. + * @param gain_factor - The gain factor. + * @param freq_function - Frequency function. + * Example: AD5933_FUNCTION_INC_FREQ - Increment freq.; + * AD5933_FUNCTION_REPEAT_FREQ - Repeat freq.. + * + * @return ad5933_results - Struct of calculated values. +*******************************************************************************/ +ad5933_result ad5933_calculate_impedance(struct ad5933_dev *dev, + double gain_factor, + uint8_t freq_function) +{ + signed short real_data = 0; + signed short imag_data = 0; + double magnitude = 0; + uint8_t status = 0; + ad5933_result result; + + ad5933_set_register_value(dev, + AD5933_REG_CONTROL_HB, + AD5933_CONTROL_FUNCTION(freq_function) | + AD5933_CONTROL_RANGE(dev->current_range) | + AD5933_CONTROL_PGA_GAIN(dev->current_gain), + 1); + status = 0; + while((status & AD5933_STAT_DATA_VALID) == 0) { + status = ad5933_get_register_value(dev, + AD5933_REG_STATUS, + 1); + } + real_data = ad5933_get_register_value(dev, + AD5933_REG_REAL_DATA, + 2); + imag_data = ad5933_get_register_value(dev, + AD5933_REG_IMAG_DATA, + 2); + magnitude = sqrt((real_data * real_data) + (imag_data * imag_data)); + + + result.magnitude = magnitude; + result.phase = atan2(imag_data,real_data) * radToDeg180ovPi; + + return result; +} + +/***************************************************************************//** + * @brief Selects the number of settling cycles of the device. + * + * @param dev - The device structure. + * @param cycles - 9-bit number of cycles to wait before triggering ADC + * + * @param multiplier - Multiply number of cycles by X1, X2 or X4 + * Example: AD5933_SETTLING_X1 + * AD5933_SETTLING_X2 + * AD5933_SETTLING_X4 + * + * @return None. +*******************************************************************************/ +void ad5933_set_settling_time(struct ad5933_dev *dev, + uint8_t multiplier, + uint16_t number_cycles) +{ + + + if ((multiplier != AD5933_SETTLING_X2) && (multiplier != AD5933_SETTLING_X4)) + multiplier = AD5933_SETTLING_X1; + + ad5933_set_register_value(dev, + AD5933_REG_SETTLING_CYCLES, + number_cycles | (multiplier << 9), + 2); + /* Store the last settings made. */ + dev->current_settling = number_cycles; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/impedance-analyzer/ad5933/ad5933.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,213 @@ +/***************************************************************************//** + * @file AD5933.h + * @brief Header file of AD5933 Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2012(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef __AD5933_H__ +#define __AD5933_H__ + +#include <math.h> + +#ifndef M_PI +#define M_PI 3.14159265359 +#endif +const double radToDeg180ovPi = 180 / M_PI; + +/******************************************************************************/ +/************************** AD5933 Definitions ********************************/ +/******************************************************************************/ + +/* AD5933 Registers */ +#define AD5933_REG_CONTROL_HB 0x80 // HB of the Control register +#define AD5933_REG_CONTROL_LB 0x81 // LB of the Control register +#define AD5933_REG_FREQ_START 0x82 // Start frequency +#define AD5933_REG_FREQ_INC 0x85 // Frequency increment +#define AD5933_REG_INC_NUM 0x88 // Number of increments +#define AD5933_REG_SETTLING_CYCLES 0x8A // Number of settling time cycles +#define AD5933_REG_STATUS 0x8F // Status +#define AD5933_REG_TEMP_DATA 0x92 // Temperature data +#define AD5933_REG_REAL_DATA 0x94 // Real data +#define AD5933_REG_IMAG_DATA 0x96 // Imaginary data + +/* AD5933_REG_CONTROL_HB Bits */ +#define AD5933_CONTROL_FUNCTION(x) ((x) << 4) +#define AD5933_CONTROL_RANGE(x) ((x) << 1) +#define AD5933_CONTROL_PGA_GAIN(x) ((x) << 0) + +/* AD5933_REG_CONTROL_LB Bits */ +#define AD5933_CONTROL_RESET (0x1 << 4) +#define AD5933_CONTROL_INT_SYSCLK (0x0 << 3) +#define AD5933_CONTROL_EXT_SYSCLK (0x1 << 3) + +/* AD5933_CONTROL_FUNCTION(x) options */ +#define AD5933_FUNCTION_NOP 0x0 +#define AD5933_FUNCTION_INIT_START_FREQ 0x1 +#define AD5933_FUNCTION_START_SWEEP 0x2 +#define AD5933_FUNCTION_INC_FREQ 0x3 +#define AD5933_FUNCTION_REPEAT_FREQ 0x4 +#define AD5933_FUNCTION_MEASURE_TEMP 0x9 +#define AD5933_FUNCTION_POWER_DOWN 0xA +#define AD5933_FUNCTION_STANDBY 0xB + +/* AD5933_CONTROL_RANGE(x) options */ +#define AD5933_RANGE_2000mVpp 0x0 +#define AD5933_RANGE_200mVpp 0x1 +#define AD5933_RANGE_400mVpp 0x2 +#define AD5933_RANGE_1000mVpp 0x3 + +/* AD5933_CONTROL_PGA_GAIN(x) options */ +#define AD5933_GAIN_X5 0 +#define AD5933_GAIN_X1 1 + +/* AD5933 Default number of settling cycles */ +#define AD5933_15_CYCLES 15 + +/* AD5933 settling cycles mulitiplier */ +#define AD5933_SETTLING_X1 0 +#define AD5933_SETTLING_X2 1 +#define AD5933_SETTLING_X4 3 + +/* AD5933_REG_STATUS Bits */ +#define AD5933_STAT_TEMP_VALID (0x1 << 0) +#define AD5933_STAT_DATA_VALID (0x1 << 1) +#define AD5933_STAT_SWEEP_DONE (0x1 << 2) + +/* AD5933 Address */ +#define AD5933_ADDRESS 0x0D + +/* AD5933 Block Commands */ +#define AD5933_BLOCK_WRITE 0xA0 +#define AD5933_BLOCK_READ 0xA1 +#define AD5933_ADDR_POINTER 0xB0 + +/* AD5933 Specifications */ +#define AD5933_INTERNAL_SYS_CLK 16000000ul // 16MHz +#define AD5933_MAX_INC_NUM 511 // Maximum increment number +#define AD5933_MAX_SETTLING_CYCLES 511 + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +struct ad5933_dev { + /* I2C */ + i2c_desc *i2c_desc; + /* Device Settings */ + uint32_t current_sys_clk; + uint8_t current_clock_source; + uint8_t current_gain; + uint8_t current_range; + uint16_t current_settling; +}; + +struct ad5933_init_param { + /* I2C */ + i2c_init_param i2c_init; + /* Device Settings */ + uint32_t current_sys_clk; + uint8_t current_clock_source; + uint8_t current_gain; + uint8_t current_range; + uint16_t current_settling; +}; + +typedef struct ad5933_result { + double magnitude; + double phase; +}ad5933_result; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/*! Initializes the communication peripheral. */ +int32_t ad5933_init(struct ad5933_dev **device, + struct ad5933_init_param init_param); + +/*! Free the resources allocated by ad5686_init(). */ +int32_t ad5933_remove(struct ad5933_dev *dev); + +/*! Writes data into a register. */ +void ad5933_set_register_value(struct ad5933_dev *dev, + uint8_t register_address, + uint32_t register_value, + uint8_t bytes_number); + +/*! Reads the value of a register. */ +uint32_t ad5933_get_register_value(struct ad5933_dev *dev, + uint8_t register_address, + uint8_t bytes_number); + +/*! Resets the device. */ +void ad5933_reset(struct ad5933_dev *dev); + +/*! Selects the source of the system clock. */ +void ad5933_set_system_clk(struct ad5933_dev *dev, + int8_t clk_source, + uint32_t ext_clk_freq); + +/*! Selects the range and gain of the device. */ +void ad5933_set_range_and_gain(struct ad5933_dev *dev, + int8_t range, + int8_t gain); + +/*! Reads the temp. from the part and returns the data in degrees Celsius. */ +float ad5933_get_temperature(struct ad5933_dev *dev); + +/*! Configures the sweep parameters. */ +void ad5933_config_sweep(struct ad5933_dev *dev, + uint32_t start_freq, + uint32_t inc_freq, + uint16_t inc_num); + +/*! Starts the sweep operation. */ +void ad5933_start_sweep(struct ad5933_dev *dev); + +/*! Reads the real and the imaginary data and calculates the Gain Factor. */ +double ad5933_calculate_gain_factor(struct ad5933_dev *dev, + uint32_t calibration_impedance, + uint8_t freq_function); + +/*! Reads the real and the imaginary data and calculates the Impedance. */ +ad5933_result ad5933_calculate_impedance(struct ad5933_dev *dev, + double gain_factor, + uint8_t freq_function); + +void ad5933_set_settling_time(struct ad5933_dev *dev, + uint8_t mulitplier, + uint16_t number_cycles); + +#endif /* __AD5933_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/License.txt Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,14 @@ +Copyright (c) 2019 Analog Devices, Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + - Modified versions of the software must be conspicuously marked as such. + - This software is licensed solely and exclusively for use with processors/products manufactured by or for Analog Devices, Inc. + - This software may not be combined or merged with other code in any manner that would cause the software to become subject to terms and conditions which differ from those listed here. + - Neither the name of Analog Devices, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + - The use of this software may or may not infringe the patent rights of one or more patent holders. This license does not release you from the requirement that you obtain separate licenses from these patent holders to use this software. + +THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +2019-01-10-7CBSD SLA
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/inc/platform_drivers.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,56 @@ +/***************************************************************************//** + * @file platform_drivers.h + * @brief Header file of Generic Platform Drivers. +******************************************************************************** + * Copyright 2017, 2019-20(c) 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 PLATFORM_DRIVERS_H_ +#define PLATFORM_DRIVERS_H_ + + +// Platform drivers needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif // _cplusplus + + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +// spi_init(), i2c_init(), i2c_write() and i2c_read() function are already defined +// in mbed-os libraries. To avoid this naming conflict, the functions are wrapped +// with suffix _noos using macros. + +#define spi_init(desc, init_param) spi_init_noos(desc, init_param) + +#define i2c_init(desc, init_param) i2c_init_noos(desc, init_param) +#define i2c_write(desc, data, bytes_number, stop_bits) i2c_write_noos(desc, data, bytes_number, stop_bits) +#define i2c_read(desc, data, bytes_number, stop_bits) i2c_read_noos(desc, data, bytes_number, stop_bits) + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include "include/util.h" +#include "include/delay.h" +#include "include/error.h" +#include "include/gpio.h" +#include "include/spi.h" +#include "include/uart.h" +#include "include/irq.h" + + +#ifdef __cplusplus // Closing extern c +} +#endif // _cplusplus + +#endif // PLATFORM_DRIVERS_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/inc/platform_support.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,51 @@ +/***************************************************************************//** + * @file platform_support.h + * @brief: support functions and declarations for selected platform + * @details: This is a platform specific file that supports functionality + * required from application generic file. This file should be + * modified according to platform that you are working with. +******************************************************************************** + * 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. +*******************************************************************************/ + +#ifndef PLATFORM_SUPPORT_H_ +#define PLATFORM_SUPPORT_H_ + + +// Platform support needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +char getchar_noblock(void); + + +#ifdef __cplusplus // Closing extern c +} +#endif + +#endif /* PLATFORM_SUPPORT_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/delay.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,49 @@ +/***************************************************************************//** + * @file delay.cpp + * @brief Implementation of delay functionality +******************************************************************************** + * 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. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <mbed.h> +#include "platform_drivers.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Generate microseconds delay. + * @param usecs - Delay in microseconds. + * @return None. + */ +void udelay(uint32_t usecs) +{ + wait_us(usecs); +} + +/** + * @brief Generate miliseconds delay. + * @param msecs - Delay in miliseconds. + * @return None. + */ +void mdelay(uint32_t msecs) +{ + if (msecs) { + HAL_Delay(msecs); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/gpio.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,261 @@ +/***************************************************************************//** + * @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 <stdlib.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 struct gpio_init_param *param) +{ + if (desc && param) { + // Create the gpio description object for the device + gpio_desc *new_gpio = (gpio_desc *)calloc(1, sizeof(gpio_desc)) ; + if (new_gpio == NULL) { + return FAILURE; + } + + // Create the gpio extra descriptor object to store extra mbed gpio info + mbed_gpio_desc *gpio_desc_extra = (mbed_gpio_desc *)calloc(1, + sizeof(mbed_gpio_desc)) ; + if (gpio_desc_extra == NULL) { + return FAILURE; + } + + new_gpio->number = param->number; + new_gpio->extra = gpio_desc_extra; + + gpio_desc_extra->direction = GPIO_IN; + gpio_desc_extra->gpio_pin = NULL; + gpio_desc_extra->pin_mode = ((mbed_gpio_init_param *)(*desc)->extra)->pin_mode; + + *desc = new_gpio; + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Get the value of an optional GPIO. + * @param desc - The GPIO descriptor. + * @param param - GPIO Initialization parameters. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t gpio_get_optional(struct gpio_desc **desc, + const struct gpio_init_param *param) +{ + if (param) { + return gpio_get(desc, param); + } else { + *desc = NULL; + return SUCCESS; + } +} + + +/** + * @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. + * @note does not support reconfiguration of already set pin direction + */ +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 && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + if (gpio_desc_extra->gpio_pin == NULL) { + // Configure and instantiate GPIO pin as input + gpio_input = new DigitalIn((PinName)desc->number); + if (gpio_input == NULL) { + return FAILURE; + } + + gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_input; + gpio_desc_extra->direction = GPIO_IN; + + // Set the gpio pin mode + gpio_input->mode((PinMode)((mbed_gpio_desc *)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. + * @note does not support reconfiguration of already set pin direction + */ +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 && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + if (gpio_desc_extra->gpio_pin == NULL) { + + // Configure and instantiate GPIO pin as output + gpio_output = new DigitalOut((PinName)desc->number); + + if (gpio_output == NULL) { + return FAILURE; + } + + gpio_desc_extra->gpio_pin = (mbed_gpio_desc *)gpio_output; + gpio_desc_extra->direction = GPIO_OUT; + + // Set the GPIO value + if(gpio_set_value(desc, value) == SUCCESS) { + return SUCCESS; + } + } + } + + 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) +{ + mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters + + if(desc && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + + if (gpio_desc_extra->gpio_pin) { + *direction = gpio_desc_extra->direction; + } + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @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 + mbed_gpio_desc *gpio_desc_extra; // pointer to gpio desc extra parameters + + if(desc && desc->extra) { + gpio_desc_extra = (mbed_gpio_desc *)(desc->extra); + + if (gpio_desc_extra->gpio_pin) { + gpio_output = (DigitalOut *)((mbed_gpio_desc *)desc->extra)->gpio_pin; + gpio_output->write(value); + } + + return SUCCESS; + } + + 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 && desc->extra) { + 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; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/gpio_extra.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,73 @@ +/***************************************************************************//** + * @file gpio_extra.h + * @brief: Header containing extra types required for GPIO interface +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +#ifndef GPIO_EXTRA_H +#define GPIO_EXTRA_H + + +// Platform support needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +/* + * Note: The structure members are not strongly typed, as this file is included + * in application specific '.c' files. The mbed code structure does not + * allow inclusion of mbed driver files (e.g. mbed.h) into '.c' files. + * All the members are hence typecasted to mbed specific type during + * gpio init and read/write operations. + **/ + +/** +* @struct mbed_gpio_init_param +* @brief Structure holding the GPIO init parameters for mbed platform. +*/ +typedef struct mbed_gpio_init_param { + uint8_t pin_mode; // GPIO pin mode (PinMode) +} mbed_gpio_init_param; + +/** +* @struct mbed_gpio_desc +* @brief GPIO specific descriptor for the mbed platform. +*/ +typedef struct mbed_gpio_desc { + uint8_t direction; + void *gpio_pin; // GPIO pin instance (DigitalIn/DigitalOut) + uint8_t pin_mode; +} mbed_gpio_desc; + + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + + +#ifdef __cplusplus // Closing extern c +} +#endif + +#endif /* GPIO_EXTRA_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/i2c.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,177 @@ +/***************************************************************************//** + * @file i2c.cpp + * @brief Implementation of I2C 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. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdio.h> +#include <mbed.h> + +#include "platform_drivers.h" +#include "i2c_extra.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Initialize the I2C communication peripheral. + * @param desc - The I2C descriptor. + * @param param - The structure that contains the I2C parameters. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t i2c_init_noos(struct i2c_desc **desc, + const struct i2c_init_param *param) +{ + mbed::I2C *i2c; // pointer to new I2C instance + mbed_i2c_desc *mbed_desc; // pointer to mbed i2c desc + + if (desc) { + // Create an i2c descriptor object for the device + i2c_desc *new_desc = (i2c_desc *)malloc(sizeof(i2c_desc)); + if (new_desc == NULL) { + return FAILURE; + } + + // Address passed in parameter shifted left by 1 to form + // 7-bit i2c slave address (7 MSBs) and the LSB acts as + // r/w bit during i2c read/write operations + new_desc->slave_address = ((param->slave_address) << 1); + + // Configure and instantiate I2C protocol + i2c = new I2C( + (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_sda_pin), + (PinName)(((mbed_i2c_init_param *)param->extra)->i2c_scl_pin)); + + if (i2c == NULL) { + return FAILURE; + } + + // Create the i2c mbed descriptor object to store new i2c instance + mbed_desc = (mbed_i2c_desc *)malloc(sizeof(mbed_i2c_desc)); + if (mbed_desc == NULL) { + return FAILURE; + } + + mbed_desc->i2c_port = (I2C *)i2c; + new_desc->extra = (mbed_i2c_desc *)mbed_desc; + + *desc = new_desc; + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Free the resources allocated by i2c_init_noos(). + * @param desc - The I2C descriptor. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t i2c_remove(struct i2c_desc *desc) +{ + if (desc) { + // Free the I2C port object + if ((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)) { + delete((I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port)); + } + + // Free the I2C extra descriptor object + if ((mbed_i2c_desc *)(desc->extra)) { + free((mbed_i2c_desc *)(desc->extra)); + } + + // Free the I2C descriptor object + free(desc); + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Write data to a slave device. + * @param desc - The I2C descriptor. + * @param data - Buffer that stores the transmission data. + * @param bytes_number - Number of bytes to write. + * @param stop_bit - Stop condition control. + * Example: 0 - A stop condition will not be generated; + * 1 - A stop condition will be generated. + * @return SUCCESS in case of success, FAILURE otherwise. + */ + +int32_t i2c_write_noos(struct i2c_desc *desc, + uint8_t *data, + uint8_t bytes_number, + uint8_t stop_bit) +{ + mbed::I2C *i2c; + i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); + + /** + The MBED I2C API is reversed for parameter 4 + Instead of stop_bit - it has + @param repeated - Repeated start, true - don't send stop at end default value is false. + Inverting here to keep the no-OS/platform_drivers API + */ + if (!(i2c->write(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { + return SUCCESS; + } else { + return FAILURE; + } +} + + +/** + * @brief Read data from a slave device. + * @param desc - The I2C descriptor. + * @param data - Buffer that will store the received data. + * @param bytes_number - Number of bytes to read. + * @param stop_bit - Stop condition control. + * Example: 0 - A stop condition will not be generated; + * 1 - A stop condition will be generated. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t i2c_read_noos(struct i2c_desc *desc, + uint8_t *data, + uint8_t bytes_number, + uint8_t stop_bit) +{ + mbed::I2C *i2c; + i2c = (I2C *)(((mbed_i2c_desc *)(desc->extra))->i2c_port); + + /** + The MBED I2C API is reversed for parameter 4 + Instead of stop_bit - it has + @param repeated - Repeated start, true - don't send stop at end default value is false. + Inverting here to keep the no-OS/platform_drivers API + */ + if (!(i2c->read(desc->slave_address, (char *)data, bytes_number, !stop_bit))) { + return SUCCESS; + } else { + return FAILURE; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/i2c_extra.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,72 @@ +/***************************************************************************//** + * @file i2c_extra.h + * @brief: Header containing extra types required for I2C interface +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +#ifndef I2C_EXTRA_H +#define I2C_EXTRA_H + + +// Platform support needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +/* + * Note: The structure members are not strongly typed, as this file is included + * in application specific '.c' files. The mbed code structure does not + * allow inclusion of mbed driver files (e.g. mbed.h) into '.c' files. + * All the members are hence typecasted to mbed specific type during + * i2c init and read/write operations. + **/ + +/** +* @struct mbed_i2c_init_param +* @brief Structure holding the I2C init parameters for mbed platform. +*/ +typedef struct mbed_i2c_init_param { + uint8_t i2c_sda_pin; // I2C SDA pin (PinName) + uint8_t i2c_scl_pin; // I2C SCL pin (PinName) +} mbed_i2c_init_param; + +/** +* @struct mbed_i2c_desc +* @brief I2C specific descriptor for the mbed platform. +*/ +typedef struct mbed_i2c_desc { + void *i2c_port; // I2C port instance (mbed::I2C) +} mbed_i2c_desc; + + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + + +#ifdef __cplusplus // Closing extern c +} +#endif + +#endif /* I2C_EXTRA_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/irq.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,166 @@ +/***************************************************************************//** + * @file irq.cpp + * @brief Implementation of Interrupt No-OS platform driver interfaces +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdio.h> +#include <mbed.h> + +#include "platform_drivers.h" +#include "irq_extra.h" +#include "uart_extra.h" + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Initialized the controller for the peripheral interrupts + * @param desc[in, out] - Pointer where the configured instance is stored + * @param param[in] - Configuration information for the instance + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t irq_ctrl_init(struct irq_ctrl_desc **desc, + const struct irq_init_param *param) +{ + if (!desc || !param) { + return FAILURE; + } + + irq_ctrl_desc *new_desc = (irq_ctrl_desc *)malloc(sizeof(irq_ctrl_desc)); + if (!new_desc) { + return FAILURE; + } + + mbed_irq_desc *new_mbed_desc = (mbed_irq_desc *)malloc(sizeof(mbed_irq_desc)); + if (!new_mbed_desc) { + free(new_desc); + return FAILURE; + } + + new_desc->irq_ctrl_id = param->irq_ctrl_id; + + new_mbed_desc->int_mode = ((mbed_irq_init_param *)param->extra)->int_mode; + new_mbed_desc->int_obj_type = ((mbed_irq_init_param *) + param->extra)->int_obj_type; + + new_desc->extra = (irq_ctrl_desc *)new_mbed_desc; + + *desc = new_desc; + + return SUCCESS; +} + + +/** + * @brief Free the resources allocated by irq_ctrl_init() + * @param desc[in, out] - Interrupt controller descriptor. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t irq_ctrl_remove(struct irq_ctrl_desc *desc) +{ + if (!desc) { + return FAILURE; + } + + if (((mbed_irq_desc *)desc->extra)->int_obj) { + free(((mbed_irq_desc *)desc->extra)->int_obj); + } + + if ((irq_ctrl_desc *)desc->extra) { + free((irq_ctrl_desc *)desc->extra); + } + + free(desc); + + return SUCCESS; +} + + +/** + * @brief Registers a IRQ callback function to irq controller. + * @param desc[in] - The IRQ controller descriptor. + * @param irq_id[in] - Interrupt identifier. + * @param callback_desc - Descriptor of the callback. If it is NULL, the + * callback will be unregistered + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t irq_register_callback(struct irq_ctrl_desc *desc, + uint32_t irq_id, + struct callback_desc *callback_desc) +{ + InterruptIn *ext_interrupt; + mbed::UnbufferedSerial *uart_rx_port; + PinName *ext_int_pin; + + if (!desc || !callback_desc) { + return FAILURE; + } + + switch (irq_id) { + case EXTERNAL_INT_ID: + /* Attach external interrupt to input pin */ + ext_int_pin = (PinName *)((mbed_irq_desc *)(desc->extra))->int_obj_type; + if (!ext_int_pin) { + return FAILURE; + } + + ext_interrupt = new InterruptIn(*ext_int_pin); + if (!ext_interrupt) { + return FAILURE; + } + + /* Register a callback function to get external interrupt input */ + if (((mbed_irq_desc *)(desc->extra))->int_mode == EXT_IRQ_FALL) { + ext_interrupt->fall(callback_desc->callback); + } else if (((mbed_irq_desc *)(desc->extra))->int_mode == EXT_IRQ_RISE) { + ext_interrupt->rise(callback_desc->callback); + } else { + return FAILURE; + } + + /* Store the external interrupt object to be freed from irq_ctrl_remove() */ + ((mbed_irq_desc *)(desc->extra))->int_obj = ext_interrupt; + + ext_interrupt->enable_irq(); + + break; + + case UART_RX_INT_ID: + uart_rx_port = (mbed::UnbufferedSerial *)(((mbed_irq_desc *)( + desc->extra))->int_obj_type); + if (!uart_rx_port) { + return FAILURE; + } + + /* Register a callback function to receive UnbufferedSerial port event */ + uart_rx_port->attach(callback_desc->callback, UnbufferedSerial::RxIrq); + + break; + + default: + return FAILURE; + } + + return SUCCESS; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/irq_extra.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,84 @@ +/***************************************************************************//** + * @file irq_extra.h + * @brief: Header containing extra types required for IRQ drivers +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +#ifndef IRQ_EXTRA_H +#define IRQ_EXTRA_H + + +// Platform support needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdbool.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @enum irq_id + * @brief Interrupts IDs supported by the irq driver + */ +enum irq_id { + /** External interrupt ID */ + EXTERNAL_INT_ID, + /** UART Rx interrupt ID */ + UART_RX_INT_ID, + /* Number of available interrupts */ + NB_INTERRUPTS +}; + +/* + * External IRQ events + * */ +typedef enum { + EXT_IRQ_NONE, + EXT_IRQ_RISE, + EXT_IRQ_FALL +} ext_irq_event; + +/** + * @struct mbed_irq_init_param + * @brief Structure holding the extra parameters for Interrupt Request. + */ +typedef struct { + uint32_t int_mode; // Interrupt mode (falling/rising etc) + void *int_obj_type; // Interrupt handling object +} mbed_irq_init_param; + +/** + * @struct aducm410_irq_desc + * @brief Structure holding the platform descriptor for Interrupt Request. + */ +typedef struct { + uint32_t int_mode; // Interrupt mode (falling/rising etc) + void *int_obj_type; // Interrupt handling object + void *int_obj; // Interrupt object (e.g. InterruptIn) +} mbed_irq_desc; + + +#ifdef __cplusplus // Closing extern c +} +#endif + +#endif // IRQ_EXTRA_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/platform_support.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,56 @@ +/***************************************************************************//** + * @file platform_support.cpp + * @brief: support functions and declarations for particular platform + * @details: This is a platform specific file that supports functionality + * required from application generic file. This file should be + * modified according to platform that you are working with. +******************************************************************************** + * 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 <mbed.h> +#include "platform_support.h" + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Variable Declarations *******************************/ +/******************************************************************************/ + +// Configure and instantiate UnbufferedSerial object to access the stdin. +// The default mbed baud rate is 9600, unless is it overriden in the +// mbed_app.json file, or by creating another UnbufferedSerial object using +// the same pins. +static UnbufferedSerial port(USBTX, USBRX); + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief getchar, but does not block if nothing waiting to be read + * @param None + * @return character if available, NULL otherwise + */ +char getchar_noblock(void) +{ + char rx_char = '\0'; + + // Return the character read from the serial port + if (port.readable() > 0) { + port.read(&rx_char, 1); + } + + return rx_char; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/spi.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,222 @@ +/***************************************************************************//** + * @file spi.cpp + * @brief Implementation of SPI 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. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdio.h> +#include <mbed.h> + +#include "platform_drivers.h" +#include "spi_extra.h" + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +#define SPI_16_BIT_FRAME 16 // SPI 16-bit frame size +#define SPI_8_BIT_FRAME 8 // SPI 8-bit frame size + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +static uint8_t spi_format_bytes = SPI_16_BIT_FRAME; // SPI format + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Initialize the SPI communication peripheral. + * @param desc - The SPI descriptor. + * @param init_param - The structure that contains the SPI parameters. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t spi_init_noos(struct spi_desc **desc, + const struct spi_init_param *param) +{ + mbed::SPI *spi; // pointer to new spi instance + DigitalOut *ss; // pointer to new SS instance + mbed_spi_desc *mbed_desc; // Pointer to mbed spi descriptor + + if (desc) { + // Create the spi description object for the device + spi_desc * new_desc = (spi_desc *)malloc(sizeof(spi_desc)); + if (new_desc == NULL) { + return FAILURE; + } + + new_desc->chip_select = param->chip_select; + new_desc->mode = param->mode; + new_desc->max_speed_hz = param->max_speed_hz; + + // Configure and instantiate SPI protocol + spi = new SPI( + (PinName)(((mbed_spi_init_param *)param->extra)->spi_mosi_pin), + (PinName)(((mbed_spi_init_param *)param->extra)->spi_miso_pin), + (PinName)(((mbed_spi_init_param *)param->extra)->spi_clk_pin)); + + if (spi == NULL) { + return FAILURE; + } + + // Configure and instantiate slave select pin + ss = new DigitalOut((PinName)(new_desc->chip_select)); + if (ss == NULL) { + return FAILURE; + } + + // Create the SPI extra descriptor object to store new SPI instances + mbed_desc = (mbed_spi_desc *)malloc(sizeof(mbed_spi_desc)); + if (mbed_desc == NULL) { + return FAILURE; + } + + mbed_desc->spi_port = (SPI *)spi; + mbed_desc->slave_select = (DigitalOut *)ss; + new_desc->extra = (mbed_spi_desc *)mbed_desc; + + *desc = new_desc; + + /** + NOTE: Actual frequency of SPI clk will be somewhat device + dependent, relating to clock-settings, prescalars etc. If absolute + SPI frequency is required, consult your device documentation. + **/ + spi->frequency(param->max_speed_hz); + spi->format(SPI_16_BIT_FRAME, param->mode); // data write/read format + spi_format_bytes = SPI_16_BIT_FRAME; + spi->set_default_write_value(0x00); // code to write when reading back + ss->write(GPIO_HIGH); // set SS high + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Free the resources allocated by spi_init(). + * @param desc - The SPI descriptor. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t spi_remove(struct spi_desc *desc) +{ + if (desc) { + // Free the SPI port object + if ((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)) { + delete((SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port)); + } + + // Free the SS port object + if ((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)) { + delete((DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select)); + } + + // Free the SPI extra descriptor object + if ((mbed_spi_desc *)(desc->extra)) { + free((mbed_spi_desc *)(desc->extra)); + } + + // Free the SPI descriptor object + free(desc); + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Write and read data to/from SPI. + * @param desc - The SPI descriptor. + * @param data - The buffer with the transmitted/received data. + * @param bytes_number - Number of bytes to write/read. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t spi_write_and_read(struct spi_desc *desc, + uint8_t *data, + uint16_t bytes_number) +{ + mbed::SPI *spi; // pointer to new spi instance + mbed::DigitalOut *ss; // pointer to new SS instance + uint16_t num_of_words; // Number of words in SPI frame + uint16_t rw_data; // SPI read data (16-bit) + uint16_t data_index = 0; // Data index + size_t byte; // Byte read/write index + + if (desc) { + spi = (SPI *)(((mbed_spi_desc *)(desc->extra))->spi_port); + ss = (DigitalOut *)(((mbed_spi_desc *)(desc->extra))->slave_select); + + /* Get the total number of words (16-bit) */ + num_of_words = bytes_number / 2; + + /* Determine the data transmit/receive format based on parity of data */ + if (!(bytes_number % 2)) { + if (spi_format_bytes != SPI_16_BIT_FRAME) { + spi->format(SPI_16_BIT_FRAME, desc->mode); + spi_format_bytes = SPI_16_BIT_FRAME; + } + } else { + if (spi_format_bytes != SPI_8_BIT_FRAME) { + spi->format(SPI_8_BIT_FRAME, desc->mode); + spi_format_bytes = SPI_8_BIT_FRAME; + } + } + + ss->write(GPIO_LOW); + + /* **Note: It is not possible to change the format of data transfer when SPI + * communication is in progress. If format is attempted to change (from 8-bit + * to 16-bit or vice a versa), the SPI communication is reset and master generates + * a single Clock signal during format change. This triggers false transfer on slave + * which results into incorrect data transfer. For this reason, the bytes with even parity + * are transferred in 16-bit format and odd parity bytes are transferred in 8-bit format. + * Application layer doesn't have any control to stop SPI reset during format change. */ + + if (!(bytes_number % 2)) { + while (num_of_words) { + /* Form a 16-bit data to be written (LE format) */ + rw_data = ((uint16_t)data[data_index + 1] | ((uint16_t)data[data_index] << 8)); + + /* Transmit a 16-bit data over SPI */ + rw_data = (uint16_t)spi->write(rw_data); + + /* Extract the MSB and LSB from 16-bit read data (LE format) */ + data[data_index++] = (uint8_t)(rw_data >> 8); + data[data_index++] = (uint8_t)rw_data; + + num_of_words--; + } + } else { + for (byte = 0; byte < bytes_number; byte++) { + data[byte] = spi->write(data[byte]); + } + } + + ss->write(GPIO_HIGH); + + return SUCCESS; + } + + return FAILURE; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/spi_extra.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,74 @@ +/***************************************************************************//** + * @file spi_extra.h + * @brief: Header containing extra types required for SPI interface +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +#ifndef SPI_EXTRA_H +#define SPI_EXTRA_H + + +// Platform support needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +/* + * Note: The structure members are not strongly typed, as this file is included + * in application specific '.c' files. The mbed code structure does not + * allow inclusion of mbed driver files (e.g. mbed.h) into '.c' files. + * All the members are hence typecasted to mbed specific type during + * spi init and read/write operations. + **/ + +/** + * @struct mbed_spi_init_param + * @brief Structure holding the SPI init parameters for mbed platform. + */ +typedef struct mbed_spi_init_param { + uint8_t spi_miso_pin; // SPI MISO pin (PinName) + uint8_t spi_mosi_pin; // SPI MOSI pin (PinName) + uint8_t spi_clk_pin; // SPI CLK pin (PinName) +} mbed_spi_init_param; + +/** + * @struct mbed_spi_desc + * @brief SPI specific descriptor for the mbed platform. + */ +typedef struct mbed_spi_desc { + void *spi_port; // SPI port instance (mbed::SPI) + void *slave_select; // SPI slave select gpio instance (DigitalOut) +} mbed_spi_desc; + + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + + +#ifdef __cplusplus // Closing extern c +} +#endif + +#endif /* SPI_EXTRA_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/uart.cpp Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,234 @@ +/***************************************************************************//** + * @file uart.cpp + * @brief Implementation of UART No-OS platform driver interfaces +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> +#include <mbed.h> + +#include "platform_drivers.h" +#include "uart_extra.h" + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Read data from UART device. + * @param desc - Instance of UART. + * @param data - Pointer to buffer containing data. + * @param bytes_number - Number of bytes to read. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t uart_read(struct uart_desc *desc, uint8_t *data, uint32_t bytes_number) +{ + mbed::BufferedSerial *uart; // pointer to BufferedSerial/UART instance + + if (desc) { + if (((mbed_uart_desc *)(desc->extra))->uart_port) { + uart = (BufferedSerial *)(((mbed_uart_desc *)(desc->extra))->uart_port); + + if (data) { + return uart->read(data, bytes_number); + } + } + } + + return FAILURE; +} + + +/** + * @brief Write data to UART device. + * @param desc - Instance of UART. + * @param data - Pointer to buffer containing data. + * @param bytes_number - Number of bytes to read. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t uart_write(struct uart_desc *desc, const uint8_t *data, + uint32_t bytes_number) +{ + mbed::BufferedSerial *uart; // pointer to BufferedSerial/UART instance + + if (desc) { + if (((mbed_uart_desc *)(desc->extra))->uart_port) { + uart = (BufferedSerial *)(((mbed_uart_desc *)(desc->extra))->uart_port); + + if (data) { + return uart->write(data, bytes_number); + } + } + } + + return FAILURE; +} + + +/** + * @brief Submit reading buffer to the UART driver. + * + * Buffer is used until bytes_number bytes are read. + * @param desc: Descriptor of the UART device + * @param data: Buffer where data will be read + * @param bytes_number: Number of bytes to be read. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t uart_read_nonblocking(struct uart_desc *desc, + uint8_t *data, + uint32_t bytes_number) +{ + mbed::BufferedSerial *uart; // pointer to BufferedSerial/UART instance + + if (desc) { + if (((mbed_uart_desc *)(desc->extra))->uart_port) { + uart = (BufferedSerial *)(((mbed_uart_desc *)(desc->extra))->uart_port); + + if (data) { + for (size_t i = 0; i < bytes_number; i++) { + if (uart->readable() > 0) { + uart->read(&data[i], 1); + } + } + + return bytes_number; + } + } + } + + return FAILURE; +} + + +/** + * @brief Submit writting buffer to the UART driver. + * + * Data from the buffer is sent over the UART, the function returns imediatly. + * @param desc: Descriptor of the UART device + * @param data: Buffer where data will be written + * @param bytes_number: Number of bytes to be written. + * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. + */ +int32_t uart_write_nonblocking(struct uart_desc *desc, + const uint8_t *data, + uint32_t bytes_number) +{ + mbed::BufferedSerial *uart; // pointer to BufferedSerial/UART instance + + if (desc) { + if (((mbed_uart_desc *)(desc->extra))->uart_port) { + uart = (BufferedSerial *)(((mbed_uart_desc *)(desc->extra))->uart_port); + + if (data) { + for (size_t i = 0; i < bytes_number; i++) { + uart->write(&data[i], 1); + } + + return bytes_number; + } + } + } + + return FAILURE; +} + + +/** + * @brief Initialize the UART communication peripheral. + * @param desc - The UART descriptor. + * @param param - The structure that contains the UART parameters. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t uart_init(struct uart_desc **desc, struct uart_init_param *param) +{ + mbed::BufferedSerial *uart; // pointer to new BufferedSerial/UART instance + mbed_uart_desc *mbed_desc; // Pointer to mbed uart descriptor + + if (desc && param) { + // Create the UART description object for the device + uart_desc *new_desc = (uart_desc *)malloc(sizeof(uart_desc)); + if (new_desc == NULL) { + return FAILURE; + } + + new_desc->baud_rate = param->baud_rate; + + // Create and configure a new instance of BufferedSerial/UART port + uart = new BufferedSerial( + (PinName)(((mbed_uart_init_param *)param->extra)->uart_tx_pin), + (PinName)(((mbed_uart_init_param *)param->extra)->uart_rx_pin), + (int)param->baud_rate); + + if (uart == NULL) { + return FAILURE; + } + + // Create a new mbed descriptor to store new UART instances + mbed_desc = (mbed_uart_desc *)malloc(sizeof(mbed_uart_desc)); + if (mbed_desc == NULL) { + return FAILURE; + } + + mbed_desc->uart_port = (BufferedSerial *)uart; + new_desc->extra = (mbed_uart_desc *)mbed_desc; + + *desc = new_desc; + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Free the resources allocated by uart_init(). + * @param desc - The UART descriptor. + * @return SUCCESS in case of success, FAILURE otherwise. + */ +int32_t uart_remove(struct uart_desc *desc) +{ + if (desc) { + // Free the UART port object + if ((BufferedSerial *)(((mbed_uart_desc *)(desc->extra))->uart_port)) { + delete((BufferedSerial *)(((mbed_uart_desc *)(desc->extra))->uart_port)); + } + + // Free the UART extra descriptor object + if ((mbed_uart_desc *)(desc->extra)) { + free((mbed_uart_desc *)(desc->extra)); + } + + // Free the UART descriptor object + free(desc); + + return SUCCESS; + } + + return FAILURE; +} + + +/** + * @brief Get number of UART errors. + * @param desc - The UART descriptor. + * @return number of errors. + */ +uint32_t uart_get_errors(struct uart_desc *desc) +{ + if (desc) { + // Unused variable - fix compiler warning + } + + return SUCCESS; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/platform/mbed/src/uart_extra.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,71 @@ +/***************************************************************************//** + * @file uart_extra.h + * @brief: Header containing extra types required for UART interface +******************************************************************************** + * Copyright (c) 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. +*******************************************************************************/ + +#ifndef UART_EXTRA_H +#define UART_EXTRA_H + + +// Platform support needs to be C-compatible to work with other drivers +#ifdef __cplusplus +extern "C" +{ +#endif + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdio.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +/******************************************************************************/ +/********************** Variables and User defined data types *****************/ +/******************************************************************************/ + +/* + * Note: The structure members are not strongly typed, as this file is included + * in application specific '.c' files. The mbed code structure does not + * allow inclusion of mbed driver files (e.g. mbed.h) into '.c' files. + * All the members are hence typecasted to mbed specific type during + * uart init and read/write operations. + **/ + +/** + * @struct mbed_uart_init_param + * @brief Structure holding the UART init parameters for mbed platform. + */ +typedef struct { + uint8_t uart_tx_pin; // UART Transmit Pin + uint8_t uart_rx_pin; // UART Receive Pin +} mbed_uart_init_param; + +/** + * @struct mbed_uart_desc + * @brief UART specific descriptor for the mbed platform. + */ +typedef struct { + void *uart_port; // UART port instance +} mbed_uart_desc; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + + +#ifdef __cplusplus // Closing extern c +} +#endif + +#endif /* UART_EXTRA_H */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/temperature/adt7420/adt7420.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,252 @@ +/***************************************************************************//** + * @file adt7420.c + * @brief Implementation of ADT7420 Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2012(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdlib.h> +#include "adt7420.h" + +/***************************************************************************//** + * @brief Reads the value of a register. + * + * @param dev - The device structure. + * @param register_address - Address of the register. + * + * @return register_value - Value of the register. +*******************************************************************************/ +uint8_t adt7420_get_register_value(struct adt7420_dev *dev, + uint8_t register_address) +{ + uint8_t register_value = 0; + + i2c_write(dev->i2c_desc, + ®ister_address, + 1, + 0); + i2c_read(dev->i2c_desc, + ®ister_value, + 1, + 1); + + return register_value; +} + +/***************************************************************************//** + * @brief Sets the value of a register. + * + * @param dev - The device structure. + * @param register_address - Address of the register. + * @param register_value - Value of the register. + * + * @return None. +*******************************************************************************/ +void adt7420_set_register_value(struct adt7420_dev *dev, + uint8_t register_address, + uint8_t register_value) +{ + uint8_t data_buffer[2] = {0, 0}; + + data_buffer[0] = register_address; + data_buffer[1] = register_value; + i2c_write(dev->i2c_desc, + data_buffer, + 2, + 1); +} + +/***************************************************************************//** + * @brief Initializes the communication peripheral and checks if the device is + * present. + * + * @param device - The device structure. + * @param init_param - The structure that contains the device initial + * parameters. + * + * @return status - The result of the initialization procedure. + * Example: -1 - I2C peripheral was not initialized or the + * device is not present. + * 0 - I2C peripheral was initialized and the + * device is present. +*******************************************************************************/ +int32_t adt7420_init(struct adt7420_dev **device, + struct adt7420_init_param init_param) +{ + struct adt7420_dev *dev; + int32_t status; + uint8_t test = 0; + + dev = (struct adt7420_dev *)malloc(sizeof(*dev)); + if (!dev) + return -1; + + /* I2C */ + status = i2c_init(&dev->i2c_desc, &init_param.i2c_init); + + /* Device Settings */ + dev->resolution_setting = init_param.resolution_setting; + + test = adt7420_get_register_value(dev, ADT7420_REG_ID); + if(test != ADT7420_DEFAULT_ID) + status = -1; + + *device = dev; + + return status; +} + +/***************************************************************************//** + * @brief Free the resources allocated by adt7420_init(). + * + * @param dev - The device structure. + * + * @return ret - The result of the remove procedure. +*******************************************************************************/ +int32_t adt7420_remove(struct adt7420_dev *dev) +{ + int32_t ret; + + ret = i2c_remove(dev->i2c_desc); + + free(dev); + + return ret; +} + +/***************************************************************************//** + * @brief Resets the ADT7420. + * The ADT7420 does not respond to I2C bus commands while the default + * values upload (approximately 200 us). + * + * @param dev - The device structure. + * + * @return None. +*******************************************************************************/ +void adt7420_reset(struct adt7420_dev *dev) +{ + uint8_t register_address = ADT7420_REG_RESET; + + i2c_write(dev->i2c_desc, + ®ister_address, + 1, + 1); + dev->resolution_setting = 0; +} + +/***************************************************************************//** + * @brief Sets the operational mode for ADT7420. + * + * @param dev - The device structure. + * @param mode - Operation mode. + * Example: ADT7420_OP_MODE_CONT_CONV - continuous conversion; + * ADT7420_OP_MODE_ONE_SHOT - one shot; + * ADT7420_OP_MODE_1_SPS - 1 SPS mode; + * ADT7420_OP_MODE_SHUTDOWN - shutdown. + * + * @return None. +*******************************************************************************/ +void adt7420_set_operation_mode(struct adt7420_dev *dev, + uint8_t mode) +{ + uint8_t register_value = 0; + + register_value = adt7420_get_register_value(dev, ADT7420_REG_CONFIG); + register_value &= ~ADT7420_CONFIG_OP_MODE(ADT7420_OP_MODE_SHUTDOWN); + register_value |= ADT7420_CONFIG_OP_MODE(mode); + adt7420_set_register_value(dev, ADT7420_REG_CONFIG, register_value); +} + +/***************************************************************************//** + * @brief Sets the resolution for ADT7420. + * + * @param dev - The device structure. + * @param resolution - Resolution. + * Example: 0 - 13-bit resolution; + * 1 - 16-bit resolution. + * + * @return None. +*******************************************************************************/ +void adt7420_set_resolution(struct adt7420_dev *dev, + uint8_t resolution) +{ + uint8_t register_value = 0; + + register_value = adt7420_get_register_value(dev, ADT7420_REG_CONFIG); + register_value &= ~ADT7420_CONFIG_RESOLUTION; + register_value |= (resolution * ADT7420_CONFIG_RESOLUTION); + adt7420_set_register_value(dev, ADT7420_REG_CONFIG, register_value); + dev->resolution_setting = resolution; +} + +/***************************************************************************//** + * @brief Reads the temperature data and converts it to Celsius degrees. + * + * @param dev - The device structure. + * + * @return temperature - Temperature in degrees Celsius. +*******************************************************************************/ +float adt7420_get_temperature(struct adt7420_dev *dev) +{ + uint8_t msb_temp = 0; + uint8_t lsb_temp = 0; + uint16_t temp = 0; + float temp_c = 0; + + msb_temp = adt7420_get_register_value(dev, ADT7420_REG_TEMP_MSB); + lsb_temp = adt7420_get_register_value(dev, ADT7420_REG_TEMP_LSB); + temp = ((uint16_t)msb_temp << 8) + lsb_temp; + if(dev->resolution_setting) { + if(temp & 0x8000) + /*! Negative temperature */ + temp_c = (float)((int32_t)temp - 65536) / 128; + else + /*! Positive temperature */ + temp_c = (float)temp / 128; + } else { + temp >>= 3; + if(temp & 0x1000) + /*! Negative temperature */ + temp_c = (float)((int32_t)temp - 8192) / 16; + else + /*! Positive temperature */ + temp_c = (float)temp / 16; + } + + return temp_c; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/drivers/temperature/adt7420/adt7420.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,153 @@ +/***************************************************************************//** + * @file adt7420.h + * @brief Header file of ADT7420 Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2012(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef __ADT7420_H__ +#define __ADT7420_H__ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> +#include "platform_drivers.h" + +/******************************************************************************/ +/************************** ADT7420 Definitions *******************************/ +/******************************************************************************/ + +/* ADT7420 address */ +#define ADT7420_A0_PIN(x) (((x) & 0x1) << 0) // I2C Serial Bus Address Selection Pin +#define ADT7420_A1_PIN(x) (((x) & 0x1) << 1) // I2C Serial Bus Address Selection Pin +#define ADT7420_ADDRESS(x,y) (0x48 + ADT7420_A1_PIN(x) + ADT7420_A0_PIN(y)) + +/* ADT7420 registers */ +#define ADT7420_REG_TEMP_MSB 0x00 // Temperature value MSB +#define ADT7420_REG_TEMP_LSB 0x01 // Temperature value LSB +#define ADT7420_REG_STATUS 0x02 // Status +#define ADT7420_REG_CONFIG 0x03 // Configuration +#define ADT7420_REG_T_HIGH_MSB 0x04 // Temperature HIGH setpoint MSB +#define ADT7420_REG_T_HIGH_LSB 0x05 // Temperature HIGH setpoint LSB +#define ADT7420_REG_T_LOW_MSB 0x06 // Temperature LOW setpoint MSB +#define ADT7420_REG_T_LOW_LSB 0x07 // Temperature LOW setpoint LSB +#define ADT7420_REG_T_CRIT_MSB 0x08 // Temperature CRIT setpoint MSB +#define ADT7420_REG_T_CRIT_LSB 0x09 // Temperature CRIT setpoint LSB +#define ADT7420_REG_HIST 0x0A // Temperature HYST setpoint +#define ADT7420_REG_ID 0x0B // ID +#define ADT7420_REG_RESET 0x2F // Software reset + +/* ADT7420_REG_STATUS definition */ +#define ADT7420_STATUS_T_LOW (1 << 4) +#define ADT7420_STATUS_T_HIGH (1 << 5) +#define ADT7420_STATUS_T_CRIT (1 << 6) +#define ADT7420_STATUS_RDY (1 << 7) + +/* ADT7420_REG_CONFIG definition */ +#define ADT7420_CONFIG_FAULT_QUEUE(x) (x & 0x3) +#define ADT7420_CONFIG_CT_POL (1 << 2) +#define ADT7420_CONFIG_INT_POL (1 << 3) +#define ADT7420_CONFIG_INT_CT_MODE (1 << 4) +#define ADT7420_CONFIG_OP_MODE(x) ((x & 0x3) << 5) +#define ADT7420_CONFIG_RESOLUTION (1 << 7) + +/* ADT7420_CONFIG_FAULT_QUEUE(x) options */ +#define ADT7420_FAULT_QUEUE_1_FAULT 0 +#define ADT7420_FAULT_QUEUE_2_FAULTS 1 +#define ADT7420_FAULT_QUEUE_3_FAULTS 2 +#define ADT7420_FAULT_QUEUE_4_FAULTS 3 + +/* ADT7420_CONFIG_OP_MODE(x) options */ +#define ADT7420_OP_MODE_CONT_CONV 0 +#define ADT7420_OP_MODE_ONE_SHOT 1 +#define ADT7420_OP_MODE_1_SPS 2 +#define ADT7420_OP_MODE_SHUTDOWN 3 + +/* ADT7420 default ID */ +#define ADT7420_DEFAULT_ID 0xCB + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +struct adt7420_dev { + /* I2C */ + i2c_desc *i2c_desc; + /* Device Settings */ + uint8_t resolution_setting; +}; + +struct adt7420_init_param { + /* I2C */ + i2c_init_param i2c_init; + /* Device Settings */ + uint8_t resolution_setting; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/*! Reads the value of a register. */ +uint8_t adt7420_get_register_value(struct adt7420_dev *dev, + uint8_t register_address); + +/*! Sets the value of a register. */ +void adt7420_set_register_value(struct adt7420_dev *dev, + uint8_t register_address, + uint8_t register_value); + +/*! Initializes the comm. peripheral and checks if the device is present. */ +int32_t adt7420_init(struct adt7420_dev **device, + struct adt7420_init_param init_param); + +/* Free the resources allocated by adt7420_init(). */ +int32_t adt7420_remove(struct adt7420_dev *dev); + +/*! Resets the ADT7420. */ +void adt7420_reset(struct adt7420_dev *dev); + +/*! Sets the operational mode for ADT7420. */ +void adt7420_set_operation_mode(struct adt7420_dev *dev, + uint8_t mode); + +/*! Sets the resolution for ADT7420. */ +void adt7420_set_resolution(struct adt7420_dev *dev, + uint8_t resolution); + +/*! Reads the temperature data and converts it to Celsius degrees. */ +float adt7420_get_temperature(struct adt7420_dev *dev); + +#endif /* __ADT7420_H__ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/axi_io.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,59 @@ +/***************************************************************************//** + * @file axi_io.h + * @brief Header file of AXI IO. + * @author Antoniu Miclaus (antoniu.miclaus@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef AXI_IO_H_ +#define AXI_IO_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* AXI IO Read data */ +int32_t axi_io_read(uint32_t base, uint32_t offset, uint32_t *data); + +/* AXI IO Write data */ +int32_t axi_io_write(uint32_t base, uint32_t offset, uint32_t data); + +#endif // AXI_IO_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/circular_buffer.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,85 @@ +/***************************************************************************//** + * @file circular_buffer.h + * @brief Circular buffer library header + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * @copyright + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef CIRCULAR_BUFFER_H +#define CIRCULAR_BUFFER_H + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @brief Reference type for circular buffer + * + * Abstract type of the circular buffer, used as reference for the functions. + */ +struct circular_buffer; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +int32_t cb_init(struct circular_buffer **desc, uint32_t size); +int32_t cb_remove(struct circular_buffer *desc); +int32_t cb_size(struct circular_buffer *desc, uint32_t *size); + +int32_t cb_write(struct circular_buffer *desc, const void *data, + uint32_t nb_elements); +int32_t cb_read(struct circular_buffer *desc, void *data, uint32_t nb_elements); + +int32_t cb_prepare_async_write(struct circular_buffer *desc, + uint32_t raw_size_to_write, + void **write_buff, + uint32_t *raw_size_avilable); +int32_t cb_end_async_write(struct circular_buffer *desc); + +int32_t cb_prepare_async_read(struct circular_buffer *desc, + uint32_t raw_size_to_read, + void **read_buff, + uint32_t *raw_size_avilable); +int32_t cb_end_async_read(struct circular_buffer *desc); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/clk.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,88 @@ +/***************************************************************************//** + * @file clk.h + * @brief Header file of Clock Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef CLK_H_ +#define CLK_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> + +/******************************************************************************/ +/************************* Structure Declarations *****************************/ +/******************************************************************************/ +struct clk_hw { + void *dev; + int32_t (*dev_clk_enable)(); + int32_t (*dev_clk_disable)(); + int32_t (*dev_clk_recalc_rate)(); + int32_t (*dev_clk_set_rate)(); + int32_t (*dev_clk_round_rate)(); +}; + +struct clk { + struct clk_hw *hw; + uint32_t hw_ch_num; + const char *name; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Start the clock. */ +int32_t clk_enable(struct clk * clk); + +/* Stop the clock. */ +int32_t clk_disable(struct clk * clk); + +/* Get the current frequency of the clock. */ +int32_t clk_recalc_rate(struct clk *clk, + uint64_t *rate); + +/* Round the desired frequency to a rate that the clock can actually output. */ +int32_t clk_round_rate(struct clk *clk, + uint64_t rate, + uint64_t *rounded_rate); + +/* Change the frequency of the clock. */ +int32_t clk_set_rate(struct clk *clk, + uint64_t rate); + +#endif // CLK_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/crc.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,45 @@ +/***************************************************************************//** + * @file crc.h + * @brief Generic header file for all CRC computation algorithms. + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef __CRC_H +#define __CRC_H + +#include "crc8.h" +#include "crc16.h" + +#endif // __CRC_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/crc16.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,54 @@ +/***************************************************************************//** + * @file crc16.h + * @brief Header file of CRC-16 computation. + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef __CRC16_H +#define __CRC16_H + +#include <stdint.h> +#include <stddef.h> + +#define CRC16_TABLE_SIZE 256 + +#define DECLARE_CRC16_TABLE(_table) \ + static uint16_t _table[CRC16_TABLE_SIZE] + +void crc16_populate_msb(uint16_t * table, const uint16_t polynomial); +uint16_t crc16(const uint16_t * table, const uint8_t *pdata, size_t nbytes, + uint16_t crc); + +#endif // __CRC16_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/crc8.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,54 @@ +/***************************************************************************//** + * @file crc8.h + * @brief Header file of CRC-8 computation. + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef __CRC8_H +#define __CRC8_H + +#include <stdint.h> +#include <stddef.h> + +#define CRC8_TABLE_SIZE 256 + +#define DECLARE_CRC8_TABLE(_table) \ + static uint8_t _table[CRC8_TABLE_SIZE] + +void crc8_populate_msb(uint8_t * table, const uint8_t polynomial); +uint8_t crc8(const uint8_t * table, const uint8_t *pdata, size_t nbytes, + uint8_t crc); + +#endif // __CRC8_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/delay.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,59 @@ +/***************************************************************************//** + * @file delay.h + * @brief Header file of Delay functions + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef DELAY_H_ +#define DELAY_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Generate microseconds delay. */ +void udelay(uint32_t usecs); + +/* Generate miliseconds delay. */ +void mdelay(uint32_t msecs); + +#endif // DELAY_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/error.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,64 @@ +/***************************************************************************//** + * @file error.h + * @brief Error codes definition + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef ERROR_H_ +#define ERROR_H_ + +#include <errno.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +#ifndef SUCCESS +#define SUCCESS 0 +#endif +#ifndef FAILURE +#define FAILURE -1 +#endif + +/* + * Last error from errno.h is __ELASTERROR 2000 . After it, can be declared + * user errors + */ +#define EOVERRUN (__ELASTERROR + 1) /* Circular buffer overrun */ + +#define IS_ERR_VALUE(x) ((x) < 0) + +#endif // ERROR_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/fifo.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,75 @@ +/***************************************************************************//** + * @file fifo.h + * @brief Implementation of fifo + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef FIFO_H_ +#define FIFO_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct fifo_element + * @brief Structure holding the fifo element parameters. + */ +struct fifo_element { + /** next FIFO element */ + struct fifo_element *next; + /** FIFO data pointer */ + char *data; + /** FIFO length */ + uint32_t len; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Insert element to fifo tail. */ +int32_t fifo_insert(struct fifo_element **p_fifo, char *buff, uint32_t len); + +/* Remove fifo head. */ +struct fifo_element *fifo_remove(struct fifo_element *p_fifo); + +#endif /* FIFO_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/gpio.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,165 @@ +/***************************************************************************//** + * @file gpio.h + * @brief Header file of GPIO Interface + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef GPIO_H_ +#define GPIO_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +#define GPIO_OUT 0x01 +#define GPIO_IN 0x00 + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct gpio_platform_ops + * @brief Structure holding gpio function pointers that point to the platform + * specific function + */ +struct gpio_platform_ops ; + +/** + * @struct gpio_init_param + * @brief Structure holding the parameters for GPIO initialization. + */ +typedef struct gpio_init_param { + /** GPIO number */ + uint32_t number; + /** GPIO platform specific functions */ + const struct gpio_platform_ops *platform_ops; + /** GPIO extra parameters (device specific) */ + void *extra; +} gpio_init_param; + +/** + * @struct gpio_desc + * @brief Structure holding the GPIO descriptor. + */ +typedef struct gpio_desc { + /** GPIO number */ + uint32_t number; + /** GPIO platform specific functions */ + const struct gpio_platform_ops *platform_ops; + /** GPIO extra parameters (device specific) */ + void *extra; +} gpio_desc; + +/** + * @enum gpio_values + * @brief Enum that holds the possible output states of a GPIO. + */ +enum gpio_values { + /** GPIO logic low */ + GPIO_LOW, + /** GPIO logic high */ + GPIO_HIGH, + /** GPIO high impedance */ + GPIO_HIGH_Z +}; + +/** + * @struct gpio_platform_ops + * @brief Structure holding gpio function pointers that point to the platform + * specific function + */ +struct gpio_platform_ops { + /** gpio initialization function pointer */ + int32_t (*gpio_ops_get)(struct gpio_desc **, const struct gpio_init_param *); + /** gpio optional descriptor function pointer */ + int32_t (*gpio_ops_get_optional)(struct gpio_desc **, + const struct gpio_init_param *); + /** gpio remove function pointer */ + int32_t (*gpio_ops_remove)(struct gpio_desc *); + /** gpio direction input function pointer */ + int32_t (*gpio_ops_direction_input)(struct gpio_desc *); + /** gpio direction output function pointer */ + int32_t (*gpio_ops_direction_output)(struct gpio_desc *, uint8_t); + /** gpio get direction function pointer */ + int32_t (*gpio_ops_get_direction)(struct gpio_desc *, uint8_t *); + /** gpio set value function pointer */ + int32_t (*gpio_ops_set_value)(struct gpio_desc *, uint8_t); + /** gpio get value function pointer */ + int32_t (*gpio_ops_get_value)(struct gpio_desc *, uint8_t *); +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Obtain the GPIO decriptor. */ +int32_t gpio_get(struct gpio_desc **desc, + const struct gpio_init_param *param); + +/* Obtain optional GPIO descriptor. */ +int32_t gpio_get_optional(struct gpio_desc **desc, + const struct gpio_init_param *param); + +/* Free the resources allocated by gpio_get() */ +int32_t gpio_remove(struct gpio_desc *desc); + +/* Enable the input direction of the specified GPIO. */ +int32_t gpio_direction_input(struct gpio_desc *desc); + +/* Enable the output direction of the specified GPIO. */ +int32_t gpio_direction_output(struct gpio_desc *desc, + uint8_t value); + +/* Get the direction of the specified GPIO. */ +int32_t gpio_get_direction(struct gpio_desc *desc, + uint8_t *direction); + +/* Set the value of the specified GPIO. */ +int32_t gpio_set_value(struct gpio_desc *desc, + uint8_t value); + +/* Get the value of the specified GPIO. */ +int32_t gpio_get_value(struct gpio_desc *desc, + uint8_t *value); + +#endif // GPIO_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/i2c.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,129 @@ +/***************************************************************************//** + * @file i2c.h + * @brief Header file of I2C Interface + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef I2C_H_ +#define I2C_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct i2c_platform_ops + * @brief Structure holding I2C function pointers that point to the platform + * specific function + */ +struct i2c_platform_ops ; + +/** + * @struct i2c_init_param + * @brief Structure holding the parameters for I2C initialization. + */ +typedef struct i2c_init_param { + /** I2C maximum transfer speed supported */ + uint32_t max_speed_hz; + /** Slave address */ + uint8_t slave_address; + /** I2C platform specific functions */ + const struct i2c_platform_ops *platform_ops; + /** I2C extra parameters (device specific parameters) */ + void *extra; +} i2c_init_param; + +/** + * @struct i2c_desc + * @brief Structure holding I2C descriptor + */ +typedef struct i2c_desc { + /** I2C maximum transfer speed supported */ + uint32_t max_speed_hz; + /** Slave address */ + uint8_t slave_address; + /** I2C platform specific functions */ + const struct i2c_platform_ops *platform_ops; + /** I2C extra parameters (device specific parameters) */ + void *extra; +} i2c_desc; + +/** + * @struct i2c_platform_ops + * @brief Structure holding i2c function pointers that point to the platform + * specific function + */ +struct i2c_platform_ops { + /** i2c initialization function pointer */ + int32_t (*i2c_ops_init)(struct i2c_desc **, const struct i2c_init_param *); + /** i2c write function pointer */ + int32_t (*i2c_ops_write)(struct i2c_desc *, uint8_t *, uint8_t, uint8_t); + /** i2c write function pointer */ + int32_t (*i2c_ops_read)(struct i2c_desc *, uint8_t *, uint8_t, uint8_t); + /** i2c remove function pointer */ + int32_t (*i2c_ops_remove)(struct i2c_desc *); +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Initialize the I2C communication peripheral. */ +int32_t i2c_init(struct i2c_desc **desc, + const struct i2c_init_param *param); + +/* Free the resources allocated by i2c_init(). */ +int32_t i2c_remove(struct i2c_desc *desc); + +/* Write data to a slave device. */ +int32_t i2c_write(struct i2c_desc *desc, + uint8_t *data, + uint8_t bytes_number, + uint8_t stop_bit); + +/* Read data from a slave device. */ +int32_t i2c_read(struct i2c_desc *desc, + uint8_t *data, + uint8_t bytes_number, + uint8_t stop_bit); + +#endif // I2C_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/irq.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,136 @@ +/***************************************************************************//** + * @file irq.h + * @brief Header file of IRQ interface. + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef IRQ_H_ +#define IRQ_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @enum irq_uart_event_e + * @brief Possible events for uart interrupt + */ +enum irq_uart_event_e { + /** Write operation finalized */ + WRITE_DONE, + /** Read operation finalized */ + READ_DONE, + /** An error occurred */ + ERROR_RW +}; + +/** + * @struct irq_init_param + * @brief Structure holding the initial parameters for Interrupt Request. + */ +struct irq_init_param { + /** Interrupt request controller ID. */ + uint32_t irq_ctrl_id; + /** IRQ extra parameters (device specific) */ + void *extra; +}; + +/** + * @struct irq_desc + * @brief Structure for Interrupt Request descriptor. + */ +struct irq_ctrl_desc { + /** Interrupt request controller ID. */ + uint32_t irq_ctrl_id; + /** IRQ extra parameters (device specific) */ + void *extra; +}; + +/** + * @struct callback_desc + * @brief Structure describing a callback to be registered + */ +struct callback_desc { + /** + * Callback to be called when the event an event occurs + * @param ctx - Same as \ref callback_desc.ctx + * @param event - Event that generated the callback + * @param extra - Platform specific data + */ + void (*callback)(void *ctx, uint32_t event, void *extra); + /** Parameter to be passed when the callback is called */ + void *ctx; + /** Platform specific configuration for a callback */ + void *config; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Initialize a interrupt controller peripheral. */ +int32_t irq_ctrl_init(struct irq_ctrl_desc **desc, + const struct irq_init_param *param); + +/* Free the resources allocated by irq_ctrl_init(). */ +int32_t irq_ctrl_remove(struct irq_ctrl_desc *desc); + +/* Register a callback to handle the irq events */ +int32_t irq_register_callback(struct irq_ctrl_desc *desc, uint32_t irq_id, + struct callback_desc *callback_desc); + +/* Unregisters a generic IRQ handling function */ +int32_t irq_unregister(struct irq_ctrl_desc *desc, uint32_t irq_id); + +/* Global interrupt enable */ +int32_t irq_global_enable(struct irq_ctrl_desc *desc); + +/* Global interrupt disable */ +int32_t irq_global_disable(struct irq_ctrl_desc *desc); + +/* Enable specific interrupt */ +int32_t irq_enable(struct irq_ctrl_desc *desc, uint32_t irq_id); + +/* Disable specific interrupt */ +int32_t irq_disable(struct irq_ctrl_desc *desc, uint32_t irq_id); + +#endif // IRQ_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/list.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,317 @@ +/***************************************************************************//** + * @file list.h + * @brief List library header + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * @copyright + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +******************************************************************************** + * + * @section list_details Library description + * This library handles double linked lists and it expose inseart, + * read, get and delete functions. \n + * It also can be accesed using it member functions which wrapp function for + * usual list types.\n + * @subsection example Sample code + * @code{.c} + * // -- Use a generic list + * struct list_desc *list1; + * struct iterator *it; + * uint32_t a; + * // Create list list1 + * list_init(&list1, LIST_DEFAULT, NULL); + * // Add items to the list + * list_add_last(list1, 1); + * list_add_last(list1, 2); + * list_add_last(list1, 3); + * // Here the list will be: 1 -> 2 - > 3 + * + * list_read_last(list1, &a); + * printf("Last: %d\n", a); + * // 3 will be printed + * // Create an iterator on the end of the list + * iterator_init(&it, list1, 0); + * // Move the iterator backword with one position + * iterator_move(it, -1); + * // Read the data at the current position + * iterator_read(it, &a); + * printf("Current: %d\n", a); + * // 2 will be printed + * iterator_remove(it); + * list_remove(list1); + * + * // -- Use a popular list + * struct list_desc *stack; + * // Create a FIFO list + * list_init(&stack, LIST_STACK, NULL); + * // Put elements in the list + * stack->push(stack, 1); + * stack->push(stack, 2); + * stack->push(stack, 3); + * // Read from the stack + * stack->pop(stack, &a); + * printf("Last: %d\n", a); + * // 3 will be printed + * list_remove(stack); + * @endcode +*******************************************************************************/ + +#ifndef LIST_H +#define LIST_H + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> +#include <stdbool.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct list_desc + * @brief Structure storing the list and function wrapper for usual list types + * + * With this structure the funtionalities of usual list types + * ( \ref adapter_type ) can be accesed with the functions referenced in this + * structure. + * For example: + * @code{.c} + * my_queue->push(my_queue, my_data); + * @endcode + */ +struct list_desc; + +/** + * @struct list_iterator + * @brief Structure used to iterate through the list using Iterator functions. + */ +struct iterator; + +/** + * @brief Prototype of the compare function. + * + * The function used to compare the elements of the liste when doing + * operations on an ordered list. + * @param data1 - First element to be compared + * @param data2 - Second element to be compared + * @return + * - -1 - If data1 < data2 + * - 0 - If data1 == data2 + * - 1 - If data1 > data2 + */ +typedef int32_t (*f_cmp)(void *data1, void *data2); + +/** + * @name Generic functions + * Each function interacting with the list have one of the following formats.\n + * Aditionaly they may have one more parametere for specific functionalities.\n + * In the Iterator functions, the list reference is replaced by the iterator's + * one. + * @{ + */ + +/** + * @brief Add an element in the list. + * + * The element of the list is created and the data field is stored in it. + * @param list_desc - Reference to the list. Created by \ref list_init + * @param data - Data to store in a list element + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +typedef int32_t (*f_add)(struct list_desc *list_desc, void *data); + +/** + * @brief Edit an element in the list. The content is replaced by new_data. + * @param list_desc - Reference to the list. Created by \ref list_init . + * @param new_data - New data to replace the old one + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +typedef int32_t (*f_edit)(struct list_desc *list_desc, void *new_data); + +/** + * @brief Read an element from the list. + * @param list_desc - Reference to the list. Created by \ref list_init + * @param result - If not null, result is filled with: + * @param data - Content of the list element, NULL if some error occur. + * @return \n + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + * @note If the content of an element can be 0 then the result must be checked + * to see if the functions has succeded + */ +typedef int32_t (*f_read)(struct list_desc *list_desc, void **data); + +/** + * @brief Read and remove an element from the list. + * @param list_desc - Reference to the list. Created by \ref list_init + * @param result - If not null, result is filled with: + * @param data - Content of the list element, NULL if some error occur. + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +typedef int32_t (*f_get)(struct list_desc *list_desc, void **data); + +/** @} */ + +/** + * @enum adapter_type + * @brief Selects functionalities for functions in \ref list_desc + */ +enum adapter_type { + /** Default type is LIST_STACK */ + LIST_DEFAULT, + /** + * Functions for a FIFO list (First-in first-out). Elements are inserted + * in one end and extracted from the other end. + * - \e Push: Insert element + * - \e Pop: Get next element (Read and remove) + * - \e Top_next: Read next element + * - \e Back: Read first element + * - \e Swap: Edit the content of the next element + */ + LIST_QUEUE, + /** + * Functions for a LIFO list (Last-in first-out). Elements are inserted + * and extracted only from the same end. + * - \e Push: Insert element + * - \e Pop: Get top element (Read and remove) + * - \e Top_next: Read top element + * - \e Back: Read bottom element + * - \e Swap: Edit the content of the top element + */ + LIST_STACK, + /** + * Functions for ordered list. The order of element is determinated + * usinge the \ref f_cmp. + * - \e Push: Insert element + * - \e Pop: Get lowest element (Read and remove) + * - \e Top_next: Read lowest element + * - \e Back: Read the biggest element + * - \e Swap: Edit the lowest element + */ + LIST_PRIORITY_LIST +}; + +struct list_desc { + /** Refer to \ref adapter_type */ + f_add push; + /** Refer to \ref adapter_type */ + f_get pop; + /** Refer to \ref adapter_type */ + f_read top_next; + /** Refer to \ref adapter_type */ + f_read back; + /** Refer to \ref adapter_type */ + f_edit swap; + /** Structure storing the list internal parameters */ + void *priv_desc; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +int32_t list_init(struct list_desc **list_desc, enum adapter_type type, + f_cmp comparator); +int32_t list_remove(struct list_desc *list_desc); +int32_t list_get_size(struct list_desc *list_desc, uint32_t *out_size); + +/** + * @name Iterator functions + * An iterator is used to iterate through the list. For a list, any number of + * iterators can be created. All must be removed before removing a list. + * @{ + */ +int32_t iterator_init(struct iterator **iter, struct list_desc *list_desc, + bool start); +int32_t iterator_remove(struct iterator *iter); +int32_t iterator_move(struct iterator *iter, int32_t idx); +int32_t iterator_find(struct iterator *iter, void *cmp_data); +int32_t iterator_insert(struct iterator *iter, void *data, bool after); +int32_t iterator_edit(struct iterator *iter, void *new_data); +int32_t iterator_read(struct iterator *iter, void **data); +int32_t iterator_get(struct iterator *iter, void **data); +/** @}*/ + +/** + * @name Operations on the ends of the list + * These functions will operate on the first or last element of the list + * @{ + */ +int32_t list_add_first(struct list_desc *list_desc, void *data); +int32_t list_edit_first(struct list_desc *list_desc, void *new_data); +int32_t list_read_first(struct list_desc *list_desc, void **data); +int32_t list_get_first(struct list_desc *list_desc, void **data); + +int32_t list_add_last(struct list_desc *list_desc, void *data); +int32_t list_edit_last(struct list_desc *list_desc, void *new_data); +int32_t list_read_last(struct list_desc *list_desc, void **data); +int32_t list_get_last(struct list_desc *list_desc, void **data); +/** @}*/ + +/** + * @name Operations by index + * These functions use an index to identify the element in the list. + * @{ + */ +int32_t list_add_idx(struct list_desc *list_desc, void *data, uint32_t idx); +int32_t list_edit_idx(struct list_desc *list_desc, void *new_data, + uint32_t idx); +int32_t list_read_idx(struct list_desc *list_desc, void **data, uint32_t idx); +int32_t list_get_idx(struct list_desc *list_desc, void **data, uint32_t idx); +/** @}*/ + +/** + * @name Operations by comparation + * These functions use the specified \ref f_cmp at \ref list_init to identify + * the element this will operate on. + * @{ + */ +int32_t list_add_find(struct list_desc *list_desc, void *data); +int32_t list_edit_find(struct list_desc *list_desc, void *new_data, + void *cmp_data); +int32_t list_read_find(struct list_desc *list_desc, void **data, + void *cmp_data); +int32_t list_get_find(struct list_desc *list_desc, void **data, void *cmp_data); +/** @}*/ + +#endif //LIST_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/pwm.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,134 @@ +/***************************************************************************//** + * @file pwm.h + * @brief Header file of PWM Interface + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef PWM_H_ +#define PWM_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> +#include <stdbool.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ +/** + * @enum pwm_polarity + * @brief Possible polarities of the PWM signal + */ +enum pwm_polarity { + /** PWM duty cycle is high, idle low */ + PWM_POLARITY_HIGH, + /** PWM duty cycle is low, idle high */ + PWM_POLARITY_LOW, +}; + +/** + * @struct pwm_init_param + * @brief Structure containing the init parameters needed by the PWM generator + */ +struct pwm_init_param { + /** PWM generator period */ + uint32_t period_ns; + /** PWM generator duty cycle */ + uint32_t duty_cycle_ns; + /** PWM generator polarity */ + enum pwm_polarity polarity; + /** PWM extra parameters (device specific) */ + void *extra; +}; + +/** + * @struct pwm_desc + * @brief Structure representing an PWM generator device + */ +struct pwm_desc { + /** PWM generator period */ + uint32_t period_ns; + /** PWM generator duty cycle */ + uint32_t duty_cycle_ns; + /** PWM generator polarity */ + enum pwm_polarity polarity; + /** PWM generator enabled */ + bool enabled; + /** PWM extra parameters (device specific) */ + void *extra; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ +/* Initialize the PWM generator device */ +int32_t pwm_init(struct pwm_desc **desc, + const struct pwm_init_param *param); + +/* Free the resources used by the PWM generator device */ +int32_t pwm_remove(struct pwm_desc *desc); + +/* Enable PWM generator device */ +int32_t pwm_enable(struct pwm_desc *desc); + +/* Disable PWM generator device */ +int32_t pwm_disable(struct pwm_desc *desc); + +/* Set period of PWM generator device */ +int32_t pwm_set_period(struct pwm_desc *desc, + uint32_t period_ns); + +/* Get period of PWM generator device */ +int32_t pwm_get_period(struct pwm_desc *desc, + uint32_t *period_ns); + +/* Set duty cycle of PWM generator device */ +int32_t pwm_set_duty_cycle(struct pwm_desc *desc, + uint32_t duty_cycle_ns); + +/* Get period of PWM generator device */ +int32_t pwm_get_duty_cycle(struct pwm_desc *desc, + uint32_t *duty_cycle_ns); + +/* Set polarity of PWM generator device */ +int32_t pwm_set_polarity(struct pwm_desc *desc, + enum pwm_polarity polarity); + +/* Set polarity of PWM generator device */ +int32_t pwm_get_polarity(struct pwm_desc *desc, + enum pwm_polarity *polarity); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/rtc.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,105 @@ +/***************************************************************************//** + * @file rtc.h + * @brief Header of the RTC interface + * @author Andrei Drimbarean (andrei.drimbarean@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef RTC_H_ +#define RTC_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct rtc_desc + * @brief Structure holding RTC descriptor. + */ +struct rtc_desc { + /** ID of the real time clock core. */ + uint8_t id; + /** Frequency of the RTC */ + uint32_t freq; + /** Load value of the RTC */ + uint32_t load; + /** Device specific RTC parameters. */ + void *extra; +}; + +/** + * @struct rtc_init_param + * @brief Structure holding RTC initialization parameters. + */ +struct rtc_init_param { + /** ID of the real time clock core. */ + uint8_t id; + /** Frequency of the RTC */ + uint32_t freq; + /** Load value of the RTC */ + uint32_t load; + /** Device specific RTC parameters. */ + void *extra; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/** Initialize the RTC peripheral. */ +int32_t rtc_init(struct rtc_desc **device, struct rtc_init_param *init_param); + +/** Free the resources allocated by rtc_init(). */ +int32_t rtc_remove(struct rtc_desc *dev); + +/** Start the real time clock. */ +int32_t rtc_start(struct rtc_desc *dev); + +/** Stop the real time clock. */ +int32_t rtc_stop(struct rtc_desc *dev); + +/** Get the current count for the real time clock. */ +int32_t rtc_get_cnt(struct rtc_desc *dev, uint32_t *tmr_cnt); + +/** Set the current count for the real time clock. */ +int32_t rtc_set_cnt(struct rtc_desc *dev, uint32_t tmr_cnt); + +#endif /* RTC_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/spi.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,144 @@ +/***************************************************************************//** + * @file spi.h + * @brief Header file of SPI Interface + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef SPI_H_ +#define SPI_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +#define SPI_CPHA 0x01 +#define SPI_CPOL 0x02 + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @enum spi_mode + * @brief SPI configuration for clock phase and polarity. + */ +typedef enum spi_mode { + /** Data on rising, shift out on falling */ + SPI_MODE_0 = (0 | 0), + /** Data on falling, shift out on rising */ + SPI_MODE_1 = (0 | SPI_CPHA), + /** Data on falling, shift out on rising */ + SPI_MODE_2 = (SPI_CPOL | 0), + /** Data on rising, shift out on falling */ + SPI_MODE_3 = (SPI_CPOL | SPI_CPHA) +} spi_mode; + +/** + * @struct spi_platform_ops + * @brief Structure holding SPI function pointers that point to the platform + * specific function + */ +struct spi_platform_ops ; + +/** + * @struct spi_init_param + * @brief Structure holding the parameters for SPI initialization + */ +typedef struct spi_init_param { + /** maximum transfer speed */ + uint32_t max_speed_hz; + /** SPI chip select */ + uint8_t chip_select; + /** SPI mode */ + enum spi_mode mode; + const struct spi_platform_ops *platform_ops; + /** SPI extra parameters (device specific) */ + void *extra; +} spi_init_param; + +/** + * @struct spi_desc + * @brief Structure holding SPI descriptor. + */ +typedef struct spi_desc { + /** maximum transfer speed */ + uint32_t max_speed_hz; + /** SPI chip select */ + uint8_t chip_select; + /** SPI mode */ + enum spi_mode mode; + const struct spi_platform_ops *platform_ops; + /** SPI extra parameters (device specific) */ + void *extra; +} spi_desc; + +/** + * @struct spi_platform_ops + * @brief Structure holding SPI function pointers that point to the platform + * specific function + */ +struct spi_platform_ops { + /** SPI initialization function pointer */ + int32_t (*spi_ops_init)(struct spi_desc **, const struct spi_init_param *); + /** SPI write/read function pointer */ + int32_t (*spi_ops_write_and_read)(struct spi_desc *, uint8_t *, uint16_t); + /** SPI remove function pointer */ + int32_t (*spi_ops_remove)(struct spi_desc *); +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Initialize the SPI communication peripheral. */ +int32_t spi_init(struct spi_desc **desc, + const struct spi_init_param *param); + +/* Free the resources allocated by spi_init(). */ +int32_t spi_remove(struct spi_desc *desc); + +/* Write and read data to/from SPI. */ +int32_t spi_write_and_read(struct spi_desc *desc, + uint8_t *data, + uint16_t bytes_number); + +#endif // SPI_H_
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/timer.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,113 @@ +/***************************************************************************//** +* @file timer.h +* @brief Timer control module header. +* @author Andrei Drimbarean (andrei.drimbarean@analog.com) +******************************************************************************** +* Copyright 2019(c) Analog Devices, Inc. +* +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* - Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in +* the documentation and/or other materials provided with the +* distribution. +* - Neither the name of Analog Devices, Inc. nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* - The use of this software may or may not infringe the patent rights +* of one or more patent holders. This license does not release you +* from the requirement that you obtain separate licenses from these +* patent holders to use this software. +* - Use of the software either in source or binary form, must be run +* on or directly connected to an Analog Devices Inc. component. +* +* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY +* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +* IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, +* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +* LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR +* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef SRC_TIMER_H_ +#define SRC_TIMER_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct timer_desc + * @brief Structure holding timer descriptor + */ +struct timer_desc { + /** timer ID */ + uint16_t id; + /** timer count frequency (Hz) */ + uint32_t freq_hz; + /** counter start value */ + uint32_t load_value; + /** timer extra parameters (device specific) */ + void *extra; +}; + +/** + * @struct timer_init_param + * @brief Structure holding the parameters for timer initialization + */ +struct timer_init_param { + /** timer ID */ + uint16_t id; + /** timer count frequency (Hz) */ + uint32_t freq_hz; + /** counter start value */ + uint32_t load_value; + /** timer extra parameters (device specific) */ + void *extra; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Initialize hardware timer and the handler structure associated with it. */ +int32_t timer_init(struct timer_desc **desc, + struct timer_init_param *param); + +/* Free the memory allocated by timer_setup(). */ +int32_t timer_remove(struct timer_desc *desc); + +/* Start a timer. */ +int32_t timer_start(struct timer_desc *desc); + +/* Stop a timer from counting. */ +int32_t timer_stop(struct timer_desc *desc); + +/* Get the value of the counter register for the timer. */ +int32_t timer_counter_get(struct timer_desc *desc, uint32_t *counter); + +/* Set the timer counter register value. */ +int32_t timer_counter_set(struct timer_desc *desc, uint32_t new_val); + +/* Get the timer clock frequency. */ +int32_t timer_count_clk_get(struct timer_desc *desc, uint32_t *freq_hz); + +/* Set the timer clock frequency. */ +int32_t timer_count_clk_set(struct timer_desc *desc, uint32_t freq_hz); + +#endif /* SRC_TIMER_H_ */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/trng.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,84 @@ +/***************************************************************************//** + * @file trng.h + * @brief Header file of true random number generator + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * @copyright + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef TRNG_H +#define TRNG_H + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct trng_desc + * @brief TRNG Descriptor + */ +struct trng_desc; + +/** + * @struct trng_init_param + * @brief Init parameter for TRNG + */ +struct trng_init_param { + /** Device id */ + uint32_t dev_id; + /** Platform specific parameter */ + void *extra; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Initialize descriptor */ +int32_t trng_init(struct trng_desc **desc, struct trng_init_param *param); + +/* Free resources allocated in descriptor */ +void trng_remove(struct trng_desc *desc); + +/* Fill buffer with random numbers */ +int32_t trng_fill_buffer(struct trng_desc *desc, uint8_t *buff, uint32_t len); + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/uart.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,111 @@ +/***************************************************************************//** + * @file uart.h + * @brief Header file of UART interface. + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef UART_H_ +#define UART_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdint.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct uart_init_param + * @brief Structure holding the parameters for UART initialization + */ +struct uart_init_param { + /** UART Device ID */ + uint8_t device_id; + /** UART Baud Rate */ + uint32_t baud_rate; + /** UART extra parameters (device specific) */ + void *extra; +}; + +/** + * @struct uart_desc + * @brief Stucture holding the UART descriptor. + */ +struct uart_desc { + /** UART Device ID */ + uint8_t device_id; + /** UART Baud Rate */ + uint32_t baud_rate; + /** Callback to be called when an operation is done (optional) */ + void (*callback)(void *callback_ctx, uint32_t event, + void *extra); + /** Parameter to be passed to the callback as app_param */ + void *callback_ctx; + /** UART extra parameters (device specific) */ + void *extra; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Read data from UART. Blocking function */ +int32_t uart_read(struct uart_desc *desc, uint8_t *data, uint32_t bytes_number); + +/* Write data to UART. Blocking function */ +int32_t uart_write(struct uart_desc *desc, const uint8_t *data, + uint32_t bytes_number); + +/* Read data from UART. Non blocking function */ +int32_t uart_read_nonblocking(struct uart_desc *desc, uint8_t *data, + uint32_t bytes_number); + +/* Write data to UART. Non blocking function*/ +int32_t uart_write_nonblocking(struct uart_desc *desc, const uint8_t *data, + uint32_t bytes_number); + +/* Initialize the UART communication peripheral. */ +int32_t uart_init(struct uart_desc **desc, struct uart_init_param *param); + +/* Free the resources allocated by uart_init(). */ +int32_t uart_remove(struct uart_desc *desc); + +/* Check if UART errors occurred. */ +uint32_t uart_get_errors(struct uart_desc *desc); + +#endif /* UART_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/util.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,150 @@ +/***************************************************************************//** + * @file util.h + * @brief Implementation of utility functions. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2018(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef UTIL_H_ +#define UTIL_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <stdint.h> +#include <stdbool.h> +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ +#define BIT(x) (1 << (x)) + +#define ARRAY_SIZE(x) \ + (sizeof(x) / sizeof((x)[0])) + +#define DIV_ROUND_UP(x,y) \ + (((x) + (y) - 1) / (y)) +#define DIV_ROUND_CLOSEST(x, y) \ + (((x) + (y) / 2) / (y)) +#define DIV_ROUND_CLOSEST_ULL(x, y) \ + DIV_ROUND_CLOSEST(x, y) + +#define min(x, y) \ + (((x) < (y)) ? (x) : (y)) +#define min_t(type, x, y) \ + (type)min((type)(x), (type)(y)) + +#define max(x, y) \ + (((x) > (y)) ? (x) : (y)) +#define max_t(type, x, y) \ + (type)max((type)(x), (type)(y)) + +#define clamp(val, min_val, max_val) \ + (max(min((val), (max_val)), (min_val))) +#define clamp_t(type, val, min_val, max_val) \ + (type)clamp((type)(val), (type)(min_val), (type)(max_val)) + +#define abs(x) \ + ((x) < 0 ? (-(x)) : (x)) + +#define swap(x, y) \ + {typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;} + +#define round_up(x,y) \ + (((x)+(y)-1)/(y)) + +#define BITS_PER_LONG 32 + +#define GENMASK(h, l) ({ \ + uint32_t t = (uint32_t)(~0UL); \ + t = t << (BITS_PER_LONG - (h - l + 1)); \ + t = t >> (BITS_PER_LONG - (h + 1)); \ + t; \ +}) + +#define bswap_constant_32(x) \ + ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \ + (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24)) + +#define U16_MAX ((uint16_t)~0U) +#define S16_MAX ((int16_t)(U16_MAX>>1)) + +#define DIV_U64(x, y) (x / y) + +#define UNUSED_PARAM(x) ((void)x) + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ +/* Find first set bit in word. */ +uint32_t find_first_set_bit(uint32_t word); +/* Find last set bit in word. */ +uint32_t find_last_set_bit(uint32_t word); +/* Locate the closest element in an array. */ +uint32_t find_closest(int32_t val, + const int32_t *array, + uint32_t size); +/* Shift the value and apply the specified mask. */ +uint32_t field_prep(uint32_t mask, uint32_t val); +/* Get a field specified by a mask from a word. */ +uint32_t field_get(uint32_t mask, uint32_t word); +/* Log base 2 of the given number. */ +int32_t log_base_2(uint32_t x); +/* Find greatest common divisor of the given two numbers. */ +uint32_t greatest_common_divisor(uint32_t a, + uint32_t b); +/* Calculate best rational approximation for a given fraction. */ +void rational_best_approximation(uint32_t given_numerator, + uint32_t given_denominator, + uint32_t max_numerator, + uint32_t max_denominator, + uint32_t *best_numerator, + uint32_t *best_denominator); +/* Calculate the number of set bits. */ +uint32_t hweight8(uint32_t word); +/* Calculate the quotient and the remainder of an integer division. */ +uint64_t do_div(uint64_t* n, + uint64_t base); +/* Unsigned 64bit divide with 64bit divisor and remainder */ +uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, + uint64_t *remainder); +/* Unsigned 64bit divide with 32bit divisor with remainder */ +uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder); +/* Unsigned 64bit divide with 32bit divisor */ +uint64_t div_u64(uint64_t dividend, uint32_t divisor); +/* Converts from string to int32_t */ +int32_t str_to_int32(const char *str); +/* Converts from string to uint32_t */ +uint32_t srt_to_uint32(const char *str); +#endif // UTIL_H_ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/include/xml.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,123 @@ +/***************************************************************************//** + * @file xml.h + * @brief Header file of xml + * @author CPop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2013(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef ___XML_H__ +#define ___XML_H__ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include "stdio.h" + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct xml_attribute + * @brief Structure holding the parameters for XML attribute + */ +struct xml_attribute { + /** XML attribute name */ + char *name; + /** XML attribute value */ + char *value; +}; + +/** + * @struct xml_node + * @brief Structure holding the parameters for XML node + */ +struct xml_node { + /** XML node name */ + char *name; + /** Node's attributes */ + struct xml_attribute **attributes; + /** Number of attributes */ + uint16_t attr_cnt; + /** XML children nodes */ + struct xml_node **children; + /** Number of children */ + uint16_t children_cnt; +}; + +/** + * @struct xml_document + * @brief Structure holding the parameters for XML document + */ +struct xml_document { + /** XML Document buffer */ + char *buff; + /** Buffer length */ + uint32_t index; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Create xml attribute. */ +ssize_t xml_create_attribute(struct xml_attribute **attribute, char *name, + const char *value); + +/* Delete xml attribute. */ +ssize_t xml_delete_attribute(struct xml_attribute *attribute); + +/* Add xml attribute to node. */ +ssize_t xml_add_attribute(struct xml_node *node, + struct xml_attribute *attribute); + +/* Create xml node. */ +ssize_t xml_create_node(struct xml_node **node, char *name); + +/* Delete xml node. */ +ssize_t xml_delete_node(struct xml_node *node); + +/* Add xml node to a parent node. */ +ssize_t xml_add_node(struct xml_node *node_parent, struct xml_node *node_child); + +/* Create xml document. */ +ssize_t xml_create_document(struct xml_document **document, + struct xml_node *node); + +/* Delete xml document. */ +ssize_t xml_delete_document(struct xml_document *document); + +#endif // ___XML_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/adi_console_menu/License.txt Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,14 @@ +Copyright (c) 2019 Analog Devices, Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + - Modified versions of the software must be conspicuously marked as such. + - This software is licensed solely and exclusively for use with processors/products manufactured by or for Analog Devices, Inc. + - This software may not be combined or merged with other code in any manner that would cause the software to become subject to terms and conditions which differ from those listed here. + - Neither the name of Analog Devices, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + - The use of this software may or may not infringe the patent rights of one or more patent holders. This license does not release you from the requirement that you obtain separate licenses from these patent holders to use this software. + +THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, TITLE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANALOG DEVICES, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, DAMAGES ARISING OUT OF CLAIMS OF INTELLECTUAL PROPERTY RIGHTS INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +2019-01-10-7CBSD SLA
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/adi_console_menu/adi_console_menu.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,300 @@ +/*! + ***************************************************************************** + @file: adi_console_menu.c + + @brief: A simple console menu manager handler + + @details: A way to define using arrays of structs a set of menus that can + be displayed to a user, easily, with all user interaction handled + by the library, leaving only the implementation of the menu actions + to be done by the library user. + ----------------------------------------------------------------------------- + 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. + +*****************************************************************************/ + +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include <stdbool.h> +#include <assert.h> + +#include "adi_console_menu.h" + + +#define DIV_STRING "\t==================================================" + +/*! + * @brief displays the text of a console menu + * + * @details + */ +static void adi_display_console_menu(const console_menu * menu) +{ + adi_clear_console(); + + // call headerItem to allow display of other content + if (menu->headerItem != NULL) { + menu->headerItem(); + printf(DIV_STRING EOL); + } + + /* + * Display the menu title and menuItems + * The shortcutKey is used to display '[A]' before the dispayText + */ + printf("\t%s" EOL "\t", menu->title); + // show an underline to distinguish title from item + for (uint8_t i = 0; i < strlen(menu->title); i++) { + putchar('-'); + } + // Extend underline past end of string, and then new line + printf("--" EOL); + + // If the shortcutKey is not unique, first found is used + for (uint8_t i = 0; i < menu->itemCount; i ++) { + if (menu->items[i].shortcutKey == '\00') { + // No shortcut key defined, but display item text if available + printf("\t%s" EOL, menu->items[i].text); + } else { + printf("\t[%c] %s" EOL, toupper(menu->items[i].shortcutKey), + menu->items[i].text); + } + } + if (menu->enableEscapeKey) { + printf(EOL "\t[ESC] Exit Menu" EOL); + } + + printf(EOL "\tPlease make a selection." EOL); + + // call footerItem to allow display of other content + if (menu->footerItem != NULL) { + printf(DIV_STRING EOL); + menu->footerItem(); + } +} + + +/*! + * @brief Display a consoleMenu and handle User interaction + * + * @details This displays the menuItems defined by the console menu, and + * handles all user interaction for the menu. + */ +int32_t adi_do_console_menu(const console_menu * menu) +{ + int32_t itemSelected = MENU_ESCAPED; + bool enableKeyScan = true; + + adi_display_console_menu(menu); + + /* + * Loop waiting for valid user input. menuItem index is returned if + * user presses a valid menu option. + */ + do { + char keyPressed = toupper(getchar()); + + if (menu->enableEscapeKey) { + if (keyPressed == ESCAPE_KEY_CODE) { + itemSelected = MENU_ESCAPED; + enableKeyScan = false; + break; + } + } + + for (uint8_t i = 0; i < menu->itemCount; i ++) { + if (toupper(menu->items[i].shortcutKey) == keyPressed) { + itemSelected = i; + + // If the menuAction function pointer is not NULL, call the action + if (menu->items[i].action != NULL) { + switch (menu->items[i].action(menu->items[i].id)) { + case MENU_DONE: { + enableKeyScan = false; + break; + } + case MENU_CONTINUE: + default: { + enableKeyScan = true; + adi_display_console_menu(menu); + break; + } + } + } + break; + } + } + } while (enableKeyScan); + + return (itemSelected); +} + +/*! + * @brief Reads a decimal string from the user + * + * @param input_len max number of character to accept from the user + * + * @return The integer value entered + * + * @details Allows a user to type in number, echoing back to the user, + * up to input_len chars + * + * @note Only positive integer numbers are supported currently + */ +int32_t adi_get_decimal_int(uint8_t input_len) +{ + char buf[20] = {0}; + uint8_t buf_index = 0; + char ch; + bool loop = true; + + assert(input_len < 19); + + do { + ch = getchar(); + if (isdigit(ch) && buf_index < (input_len)) { + // echo and store it as buf not full + buf[buf_index++] = ch; + putchar(ch); + } + if ((ch == '\x7F') && (buf_index > 0)) { + //backspace and at least 1 char in buffer + buf[buf_index--] = '\x00'; + putchar(ch); + } + if ((ch == '\x0D') || (ch == '\x0A')) { + // return key pressed, all done, null terminate string + buf[buf_index] = '\x00'; + loop = false; + } + } while(loop); + + return atoi(buf); +} + +/*! + * @brief Reads a hexadecimal number from the user + * + * @param input_len max number of character to accept from the user + * + * @return The integer value entered + * +* @details Allows a user to type in a hexnumber, echoing back to the user, + * up to input_len chars + */ +uint32_t adi_get_hex_integer(uint8_t input_len) +{ + char buf[9] = {0}; + uint8_t buf_index = 0; + char ch; + bool loop = true; + + assert(input_len < 8); + + do { + ch = getchar(); + if (isxdigit(ch) && buf_index < (input_len)) { + // echo and store it as buf not full + buf[buf_index++] = ch; + putchar(ch); + } + if ((ch == '\x7F') && (buf_index > 0)) { + //backspace and at least 1 char in buffer + buf[buf_index--] = '\x00'; + putchar(ch); + } + if ((ch == '\x0D') || (ch == '\x0A')) { + // return key pressed, all done, null terminate string + buf[buf_index] = '\x00'; + loop = false; + } + } while(loop); + + return strtol(buf, NULL, 16); +} + + +/*! + * @brief Reads a floating string from the user + * + * @param input_len max number of character to accept from the user + * + * @return The float value entered + * + * @details Allows a user to type in number, echoing back to the user, + * up to input_len chars + * + * @note Only positive floating point numbers are supported currently + */ +float adi_get_decimal_float(uint8_t input_len) +{ + char buf[20] = { 0 }; + uint8_t buf_index = 0; + char ch; + bool loop = true; + + assert(input_len < 19); + + do { + ch = getchar(); + if ((isdigit(ch) || (ch == '.')) && buf_index < (input_len)) { + // echo and store it as buf not full + buf[buf_index++] = ch; + putchar(ch); + } + if ((ch == '\x7F') && (buf_index > 0)) { + //backspace and at least 1 char in buffer + buf[buf_index--] = '\x00'; + putchar(ch); + } + if ((ch == '\x0D') || (ch == '\x0A')) { + // return key pressed, all done, null terminate string + buf[buf_index] = '\x00'; + loop = false; + } + } while (loop); + + return atof(buf); +} + + +/*! + * @brief Clears the console terminal + * + * @details Clears the console terminal using VT100 escape code, or can be changed to + * output blank lines if serial link doesn't support VT100. + */ +void adi_clear_console(void) +{ + /* + * clear console and move cursor to home location, followed by move to home location. + * Dedicated call to move home is because sometimes first move home doesn't work + * \r\n required to flush the uart buffer. + */ + printf("\x1B[2J\x1B[H\r\n"); + + /* + * if VT100 is not supported, this can be enabled instead, but menu display may not work well + */ +// for (uint8_t = 0; i < 100; i++) +// printf("\r\n\r"); +} + + +/*! + * @brief waits for any key to be pressed, and displays a prompt to the user + * + * @details + */ +void adi_press_any_key_to_continue(void) +{ + printf("\r\nPress any key to continue...\r\n"); + getchar(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/adi_console_menu/adi_console_menu.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,74 @@ +/*! + ***************************************************************************** + @file: adi_console_menu.h + + @brief: A simple console menu manager handler + + @details: + ----------------------------------------------------------------------------- + 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. + +*****************************************************************************/ + +#ifndef ADI_CONSOLE_MENU_H_ +#define ADI_CONSOLE_MENU_H_ + +#include <stdbool.h> +#include <stdint.h> + +#define MENU_ESCAPED -1 +#define MENU_CONTINUE 0 +#define MENU_DONE 1 + +#define ESCAPE_KEY_CODE (char)0x1B + +#define EOL "\r\n" + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(x) ((sizeof (x)) / (sizeof ((x)[0]))) +#endif + +/* Type Definitions */ +// Each menu item is defined by this struct +typedef struct { + // String displayed for menu item + char * text; + // character that can be pressed to select menu item + char shortcutKey; + // Function to be called when menu item is selected, if NULL, no function is called + int32_t (*action)(uint32_t option); + // id value passed as the option value when calling menuAction + uint32_t id; +} console_menu_item; + +// This defines a complete menu with items +typedef struct { + // String to be displayed as the menu title + char * title; + // Array of all the menu items + console_menu_item * items; + // Number of menuItems + uint8_t itemCount; + // Function alled before Menu title is displayed if defined + void (*headerItem)(void); + // Function called after menu items are displayed if defined + void (*footerItem)(void); + // Should the escape key to exit the menu be enabled? + bool enableEscapeKey; +} console_menu; + +/* Function Declarations */ +/* Display a console menu, and handle user interactions */ +int32_t adi_do_console_menu(const console_menu * menu); +int32_t adi_get_decimal_int(uint8_t input_len); +uint32_t adi_get_hex_integer(uint8_t input_len); +float adi_get_decimal_float(uint8_t input_len); +void adi_clear_console(void); +void adi_press_any_key_to_continue(void); + +#endif /* ADI_CONSOLE_MENU_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/iio.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,1356 @@ +/***************************************************************************//** + * @file iio.c + * @brief Implementation of iio. + * This module implements read/write ops, required by libtinyiiod and further + * calls show/store functions, corresponding to device/channel/attribute. + * @author Cristian Pop (cristian.pop@analog.com) + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include "iio.h" +#include "iio_types.h" +#include "ctype.h" +#include "tinyiiod.h" +#include "util.h" +#include "list.h" +#include "delay.h" +#include "error.h" +#include "uart.h" +#include <inttypes.h> + +#ifdef ENABLE_IIO_NETWORK +#include "tcp_socket.h" +#include "circular_buffer.h" +#endif + +/******************************************************************************/ +/********************** Macros and Constants Definitions **********************/ +/******************************************************************************/ + +#define IIOD_PORT 30431 +#define MAX_SOCKET_TO_HANDLE 4 +#define REG_ACCESS_ATTRIBUTE "direct_reg_access" + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +static char header[] = + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<!DOCTYPE context [" + "<!ELEMENT context (device | context-attribute)*>" + "<!ELEMENT context-attribute EMPTY>" + "<!ELEMENT device (channel | attribute | debug-attribute | buffer-attribute)*>" + "<!ELEMENT channel (scan-element?, attribute*)>" + "<!ELEMENT attribute EMPTY>" + "<!ELEMENT scan-element EMPTY>" + "<!ELEMENT debug-attribute EMPTY>" + "<!ELEMENT buffer-attribute EMPTY>" + "<!ATTLIST context name CDATA #REQUIRED description CDATA #IMPLIED>" + "<!ATTLIST context-attribute name CDATA #REQUIRED value CDATA #REQUIRED>" + "<!ATTLIST device id CDATA #REQUIRED name CDATA #IMPLIED>" + "<!ATTLIST channel id CDATA #REQUIRED type (input|output) #REQUIRED name CDATA #IMPLIED>" + "<!ATTLIST scan-element index CDATA #REQUIRED format CDATA #REQUIRED scale CDATA #IMPLIED>" + "<!ATTLIST attribute name CDATA #REQUIRED filename CDATA #IMPLIED>" + "<!ATTLIST debug-attribute name CDATA #REQUIRED>" + "<!ATTLIST buffer-attribute name CDATA #REQUIRED>" + "]>" + "<context name=\"xml\" description=\"no-OS analog 1.1.0-g0000000 #1 Tue Nov 26 09:52:32 IST 2019 armv7l\" >" + "<context-attribute name=\"no-OS\" value=\"1.1.0-g0000000\" />"; +static char header_end[] = "</context>"; + +static const char * const iio_modifier_names[] = { + [IIO_MOD_X] = "x", + [IIO_MOD_Y] = "y", +}; + +/* Parameters used in show and store functions */ +struct attr_fun_params { + void *dev_instance; + char *buf; + size_t len; + struct iio_ch_info *ch_info; +}; + +/** + * @struct iio_interface + * @brief Links a physical device instance "void *dev_instance" + * with a "iio_device *iio" that describes capabilities of the device. + */ +struct iio_interface { + /** Will be: device[0...n] n beeing the count of registerd devices */ + char dev_id[10]; + /** Device name */ + const char *name; + /** Opened channels */ + uint32_t ch_mask; + /** Physical instance of a device */ + void *dev_instance; + /** Used to read debug attributes */ + uint32_t active_reg_addr; + /** Device descriptor(describes channels and attributes) */ + struct iio_device *dev_descriptor; + struct iio_data_buffer *write_buffer; + struct iio_data_buffer *read_buffer; +}; + +struct iio_desc { + struct tinyiiod *iiod; + struct tinyiiod_ops *iiod_ops; + enum pysical_link_type phy_type; + void *phy_desc; + struct list_desc *interfaces_list; + char *xml_desc; + uint32_t xml_size; + uint32_t xml_size_to_last_dev; + uint32_t dev_count; + struct uart_desc *uart_desc; +#ifdef ENABLE_IIO_NETWORK + /* FIFO for socket descriptors */ + struct circular_buffer *sockets; + /* Client socket active during an iio_step */ + struct tcp_socket_desc *current_sock; + /* Instance of server socket */ + struct tcp_socket_desc *server; +#endif +}; + +static struct iio_desc *g_desc; + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +#ifdef ENABLE_IIO_NETWORK + +static inline int32_t _pop_sock(struct iio_desc *desc, + struct tcp_socket_desc **sock) +{ + return cb_read(desc->sockets, sock, sizeof(*sock)); +} + +static inline int32_t _push_sock(struct iio_desc *desc, + struct tcp_socket_desc *sock) +{ + return cb_write(desc->sockets, &sock, sizeof(sock)); +} + +static inline int32_t _nb_active_sockets(struct iio_desc *desc) +{ + uint32_t size; + + cb_size(desc->sockets, &size); + + return size / sizeof(struct tcp_socket_desc *); +} + +/* Blocking until new socket available. + * Will iterate through a list of sockets */ +static int32_t _get_next_socket(struct iio_desc *desc) +{ + struct tcp_socket_desc *sock; + int32_t ret; + uint32_t nb_active_sockets; + + /* Get all new waiting sockets. + * If none is available, wait until first connection */ + do { + ret = socket_accept(desc->server, &sock); + if (ret == -EAGAIN) { + nb_active_sockets = _nb_active_sockets(desc); + if (nb_active_sockets == 0) { + /* Wait until a connection exists */ + mdelay(1); + continue; + } else { + break; + } + } else if (IS_ERR_VALUE(ret)) { + return ret; + } + /* Add socket to queue */ + ret = _push_sock(desc, sock); + if (IS_ERR_VALUE(ret)) + return ret; + } while (true); + + ret = _pop_sock(desc, &desc->current_sock); + if (IS_ERR_VALUE(ret)) { + desc->current_sock = NULL; + return ret; + } + + return SUCCESS; +} + +static int32_t network_read(const void *data, uint32_t len) +{ + uint32_t i; + int32_t ret; + + if ((int32_t)g_desc->current_sock == -1) + return -1; + + if (g_desc->current_sock == NULL) { + ret = _get_next_socket(g_desc); + if (IS_ERR_VALUE(ret)) + return ret; + } + + i = 0; + do { + ret = socket_recv(g_desc->current_sock, + (void *)((uint8_t *)data + i), len - i); + if (IS_ERR_VALUE(ret)) { + *(int8_t *)data = '*'; + break; + } + + i += ret; + } while (i < len); + + if (ret == -ENOTCONN) { + /* A socket connection is disconnected, so we release + * the resources and don't add it again in the list */ + socket_remove(g_desc->current_sock); + g_desc->current_sock = (void *)-1; + } + + return i; +} +#endif + +static ssize_t iio_phy_read(char *buf, size_t len) +{ + if (g_desc->phy_type == USE_UART) + return (ssize_t)uart_read(g_desc->uart_desc, (uint8_t *)buf, + (size_t)len); +#ifdef ENABLE_IIO_NETWORK + else + return network_read((void *)buf, (uint32_t)len); +#endif + + return -EINVAL; +} + +/** Write to a peripheral device (UART, USB, NETWORK) */ +static ssize_t iio_phy_write(const char *buf, size_t len) +{ + if (g_desc->phy_type == USE_UART) + return (ssize_t)uart_write(g_desc->uart_desc, + (uint8_t *)buf, (size_t)len); +#ifdef ENABLE_IIO_NETWORK + else + return socket_send(g_desc->current_sock, buf, len); +#endif + + return -EINVAL; +} + +/* Get string for channel id from channel type */ +static char *get_channel_id(enum iio_chan_type type) +{ + switch (type) { + case IIO_VOLTAGE: + return "voltage"; + case IIO_CURRENT: + return "current"; + case IIO_ALTVOLTAGE: + return "altvoltage"; + case IIO_ANGL_VEL: + return "anglvel"; + case IIO_TEMP: + return "temp"; + default: + return ""; + } + + return ""; +} + +static inline void _print_ch_id(char *buff, struct iio_channel *ch) +{ + if(ch->modified) { + sprintf(buff, "%s_%s", get_channel_id(ch->ch_type), + iio_modifier_names[ch->channel2]); + } else { + if(ch->indexed) { + if (ch->diferential) + sprintf(buff, "%s%d-%s%d", get_channel_id(ch->ch_type), + (int)ch->channel, get_channel_id(ch->ch_type), + (int)ch->channel2); + else + sprintf(buff, "%s%d", get_channel_id(ch->ch_type), + (int)ch->channel); + } else { + sprintf(buff, "%s", get_channel_id(ch->ch_type)); + } + } +} + +/** + * @brief Get channel ID from a list of channels. + * @param channel - Channel name. + * @param desc - Device descriptor + * @param ch_out - If "true" is output channel, if "false" is input channel. + * @return Channel ID, or negative value if attribute is not found. + */ +static inline struct iio_channel *iio_get_channel(const char *channel, + struct iio_device *desc, bool ch_out) +{ + int16_t i = 0; + char ch_id[20]; + + while (i < desc->num_ch) { + _print_ch_id(ch_id, &desc->channels[i]); + if (!strcmp(channel, ch_id) && + (desc->channels[i].ch_out == ch_out)) + return &desc->channels[i]; + i++; + } + + return NULL; +} + +/** + * @brief Find interface with "device_name". + * @param device_name - Device name. + * @param iio_interfaces - List of interfaces. + * @return Interface pointer if interface is found, NULL otherwise. + */ +static struct iio_interface *iio_get_interface(const char *device_name) +{ + struct iio_interface *interface; + struct iio_interface cmp_val; + int32_t ret; + + strcpy(cmp_val.dev_id, device_name); + + ret = list_read_find(g_desc->interfaces_list, + (void **)&interface, &cmp_val); + if (IS_ERR_VALUE(ret)) + return NULL; + + return interface; +} + +/** + * @brief Read all attributes from an attribute list. + * @param device - Physical instance of a device. + * @param buf - Buffer where values are read. + * @param len - Maximum length of value to be stored in buf. + * @param channel - Channel properties. + * @param attributes - List of attributes to be read. + * @return Number of bytes read or negative value in case of error. + */ +static ssize_t iio_read_all_attr(struct attr_fun_params *params, + struct iio_attribute *attributes) +{ + int16_t i = 0, j = 0; + char local_buf[256]; + ssize_t attr_length; + uint32_t *pattr_length; + + while (attributes[i].name) { + attr_length = attributes[i].show(params->dev_instance, + local_buf, params->len, + params->ch_info, + attributes[i].priv); + pattr_length = (uint32_t *)(params->buf + j); + *pattr_length = bswap_constant_32(attr_length); + j += 4; + if (attr_length >= 0) { + sprintf(params->buf + j, "%s", local_buf); + if (attr_length & 0x3) /* multiple of 4 */ + attr_length = ((attr_length >> 2) + 1) << 2; + j += attr_length; + } + i++; + } + + return j; +} + +/** + * @brief Write all attributes from an attribute list. + * @param device - Physical instance of a device. + * @param buf - Values to be written. + * @param len - Length of buf. + * @param channel - Channel properties. + * @param attributes - List of attributes to be written. + * @return Number of written bytes or negative value in case of error. + */ +static ssize_t iio_write_all_attr(struct attr_fun_params *params, + struct iio_attribute *attributes) +{ + int16_t i = 0, j = 0; + int16_t attr_length; + + while (attributes[i].name) { + attr_length = bswap_constant_32((uint32_t)(params->buf + j)); + j += 4; + attributes[i].store(params->dev_instance, (params->buf + j), + attr_length, params->ch_info, + attributes[i].priv); + j += attr_length; + if (j & 0x3) + j = ((j >> 2) + 1) << 2; + i++; + } + + return params->len; +} + +/** + * @brief Read/write attribute. + * @param params - Structure describing parameters for store and show functions + * @param attributes - Array of attributes. + * @param attr_name - Attribute name to be modified + * @param is_write -If it has value "1", writes attribute, otherwise reads + * attribute. + * @return Length of chars written/read or negative value in case of error. + */ +static ssize_t iio_rd_wr_attribute(struct attr_fun_params *params, + struct iio_attribute *attributes, + char *attr_name, + bool is_write) +{ + int16_t i = 0; + + /* Search attribute */ + while (attributes[i].name) { + if (!strcmp(attr_name, attributes[i].name)) + break; + i++; + } + + if (!attributes[i].name) + return -ENOENT; + + if (is_write) { + if (!attributes[i].store) + return -ENOENT; + + return attributes[i].store(params->dev_instance, params->buf, + params->len, params->ch_info, + attributes[i].priv); + } else { + if (!attributes[i].show) + return -ENOENT; + return attributes[i].show(params->dev_instance, params->buf, + params->len, params->ch_info, + attributes[i].priv); + } +} + +/* Read a device register. The register address to read is set on + * in desc->active_reg_addr in the function set_demo_reg_attr + */ +static int32_t debug_reg_read(struct iio_interface *dev, char *buf, size_t len) +{ + uint32_t value; + int32_t ret; + + /* Set to -1 for debug purposes. If the function don't modify the value, + * then it can be easily seen the default value */ + value = -1; + ret = dev->dev_descriptor->debug_reg_read(dev->dev_instance, + dev->active_reg_addr, + &value); + if (IS_ERR_VALUE(ret)) + return ret; + + return snprintf(buf, len, "%"PRIu32"", value); +} + +/* Flow of reading and writing registers. This is how iio works for + * direct_reg_access attribute: + * Read register: + * //Reg_addr in decimal + * reg_addr = "10"; + * 1. debug_reg_write(dev, reg_addr, len); + * 2. debug_reg_read(dev, out_buf, out_len); + * Write register: + * sprintf(write_buf, "0x%x 0x%x", reg_addr, value); + * 1. debug_reg_write(dev, write_buf,len); + */ +static int32_t debug_reg_write(struct iio_interface *dev, const char *buf, + size_t len) +{ + uint32_t nb_filled; + uint32_t addr; + uint32_t value; + int32_t ret; + + nb_filled = sscanf(buf, "0x%"PRIx32" 0x%"PRIx32"", &addr, &value); + if (nb_filled == 2) { + /* Write register */ + ret = dev->dev_descriptor->debug_reg_write(dev->dev_instance, + addr, value); + if (IS_ERR_VALUE(ret)) + return ret; + } else { + nb_filled = sscanf(buf, "%"PRIu32, &addr); + if (nb_filled == 1) { + dev->active_reg_addr = addr; + return len; + } else { + return -EINVAL; + } + } + + return len; +} + +/** + * @brief Read global attribute of a device. + * @param device - String containing device name. + * @param attr - String containing attribute name. + * @param buf - Buffer where value is read. + * @param len - Maximum length of value to be stored in buf. + * @param debug - Read raw value if set. + * @return Number of bytes read. + */ +static ssize_t iio_read_attr(const char *device_id, const char *attr, char *buf, + size_t len, enum iio_attr_type type) +{ + struct iio_interface *dev; + struct attr_fun_params params; + struct iio_attribute *attributes; + + dev = iio_get_interface(device_id); + if (!dev) + return FAILURE; + + params.buf = buf; + params.len = len; + params.dev_instance = dev->dev_instance; + params.ch_info = NULL; + attributes = NULL; + switch (type) { + case IIO_ATTR_TYPE_DEBUG: + if (strcmp(attr, REG_ACCESS_ATTRIBUTE) == 0) { + if (dev->dev_descriptor->debug_reg_read) + return debug_reg_read(dev, buf, len); + else + return -ENOENT; + } + attributes = dev->dev_descriptor->debug_attributes; + break; + case IIO_ATTR_TYPE_DEVICE: + attributes = dev->dev_descriptor->attributes; + break; + case IIO_ATTR_TYPE_BUFFER: + attributes = dev->dev_descriptor->buffer_attributes; + break; + } + + if (!strcmp(attr, "")) + return iio_read_all_attr(¶ms, attributes); + else + return iio_rd_wr_attribute(¶ms,attributes, (char *)attr, 0); +} + +/** + * @brief Write global attribute of a device. + * @param device - String containing device name. + * @param attr - String containing attribute name. + * @param buf - Value to be written. + * @param len - Length of data. + * @param debug - Write raw value if set. + * @return Number of written bytes. + */ +static ssize_t iio_write_attr(const char *device_id, const char *attr, + const char *buf, + size_t len, enum iio_attr_type type) +{ + struct iio_interface *dev; + struct attr_fun_params params; + struct iio_attribute *attributes; + + dev = iio_get_interface(device_id); + if (!dev) + return -ENODEV; + + params.buf = (char *)buf; + params.len = len; + params.dev_instance = dev->dev_instance; + params.ch_info = NULL; + attributes = NULL; + switch (type) { + case IIO_ATTR_TYPE_DEBUG: + if (strcmp(attr, REG_ACCESS_ATTRIBUTE) == 0) { + if (dev->dev_descriptor->debug_reg_write) + return debug_reg_write(dev, buf, len); + else + return -ENOENT; + } + attributes = dev->dev_descriptor->debug_attributes; + break; + case IIO_ATTR_TYPE_DEVICE: + attributes = dev->dev_descriptor->attributes; + break; + case IIO_ATTR_TYPE_BUFFER: + attributes = dev->dev_descriptor->buffer_attributes; + break; + } + + if (!strcmp(attr, "")) + return iio_write_all_attr(¶ms, attributes); + else + return iio_rd_wr_attribute(¶ms, attributes, (char *)attr, 1); +} + +/** + * @brief Read channel attribute. + * @param device_name - String containing device name. + * @param channel - String containing channel name. + * @param ch_out -Channel type input/output. + * @param attr - String containing attribute name. + * @param buf - Buffer where value is stored. + * @param len - Maximum length of value to be stored in buf. + * @return - Number of bytes read. + */ +static ssize_t iio_ch_read_attr(const char *device_id, const char *channel, + bool ch_out, const char *attr, char *buf, size_t len) +{ + struct iio_interface *dev; + struct iio_ch_info ch_info; + struct iio_channel *ch; + struct attr_fun_params params; + + dev = iio_get_interface(device_id); + if (!dev) + return FAILURE; + + ch = iio_get_channel(channel, dev->dev_descriptor, ch_out); + if (!ch) + return -ENOENT; + + ch_info.ch_out = ch_out; + ch_info.ch_num = ch->scan_index; + params.buf = buf; + params.len = len; + params.dev_instance = dev->dev_instance; + params.ch_info = &ch_info; + if (!strcmp(attr, "")) + return iio_read_all_attr(¶ms, ch->attributes); + else + return iio_rd_wr_attribute(¶ms, ch->attributes, (char *)attr, 0); +} + +/** + * @brief Write channel attribute. + * @param device - String containing device name. + * @param channel - String containing channel name. + * @param ch_out - Channel type input/output. + * @param attr - String containing attribute name. + * @param buf - Value to be written. + * @param len - Length of data in "buf" parameter. + * @return Number of written bytes. + */ +static ssize_t iio_ch_write_attr(const char *device_id, const char *channel, + bool ch_out, const char *attr, const char *buf, size_t len) +{ + struct iio_interface *dev; + struct iio_ch_info ch_info; + struct iio_channel *ch; + struct attr_fun_params params; + + dev = iio_get_interface(device_id); + if (!dev) + return -ENOENT; + + ch = iio_get_channel(channel, dev->dev_descriptor, ch_out); + if (!ch) + return -ENOENT; + + ch_info.ch_out = ch_out; + ch_info.ch_num = ch->scan_index; + params.buf = (char *)buf; + params.len = len; + params.dev_instance = dev->dev_instance; + params.ch_info = &ch_info; + if (!strcmp(attr, "")) + return iio_write_all_attr(¶ms, ch->attributes); + else + return iio_rd_wr_attribute(¶ms, ch->attributes, (char *)attr, 1); +} + +/** + * @brief Open device. + * @param device - String containing device name. + * @param sample_size - Sample size. + * @param mask - Channels to be opened. + * @return SUCCESS, negative value in case of failure. + */ +static int32_t iio_open_dev(const char *device, size_t sample_size, + uint32_t mask) +{ + struct iio_interface *iface; + uint32_t ch_mask; + + iface = iio_get_interface(device); + if (!iface) + return -ENODEV; + + ch_mask = 0xFFFFFFFF >> (32 - iface->dev_descriptor->num_ch); + + if (mask & ~ch_mask) + return -ENOENT; + + iface->ch_mask = mask; + + if (iface->dev_descriptor->prepare_transfer) + return iface->dev_descriptor->prepare_transfer( + iface->dev_instance, mask); + + return SUCCESS; +} + +/** + * @brief Close device. + * @param device - String containing device name. + * @return SUCCESS, negative value in case of failure. + */ +static int32_t iio_close_dev(const char *device) +{ + struct iio_interface *iface; + + iface = iio_get_interface(device); + if (!iface) + return FAILURE; + + iface->ch_mask = 0; + if (iface->dev_descriptor->end_transfer) + return iface->dev_descriptor->end_transfer(iface->dev_instance); + + return SUCCESS; +} + +/** + * @brief Get device mask, this specifies the channels that are used. + * @param device - String containing device name. + * @param mask - Channels that are opened. + * @return SUCCESS, negative value in case of failure. + */ +static int32_t iio_get_mask(const char *device, uint32_t *mask) +{ + struct iio_interface *iface; + + iface = iio_get_interface(device); + if (!iface) + return -ENODEV; + + *mask = iface->ch_mask; + + return SUCCESS; +} + +static uint32_t bytes_to_samples(struct iio_interface *intf, uint32_t bytes) +{ + uint32_t bytes_per_sample; + uint32_t first_ch; + bool first_ch_found; + uint32_t nb_active_ch; + uint32_t mask; + + mask = intf->ch_mask; + first_ch = 0; + nb_active_ch = 0; + first_ch_found = false; + while (mask) { + if ((mask & 1)) { + if (!first_ch_found) + first_ch_found = true; + else + first_ch++; + nb_active_ch++; + } + mask >>= 1; + } + bytes_per_sample = intf->dev_descriptor->channels[first_ch] + .scan_type->storagebits / 8; + + return bytes / bytes_per_sample / nb_active_ch; +} + +/** + * @brief Transfer data from device into RAM. + * @param device - String containing device name. + * @param bytes_count - Number of bytes. + * @return Bytes_count or negative value in case of error. + */ +static ssize_t iio_transfer_dev_to_mem(const char *device, size_t bytes_count) +{ + struct iio_interface *iio_interface = iio_get_interface(device); + + if (iio_interface->dev_descriptor->transfer_dev_to_mem) + return iio_interface->dev_descriptor->transfer_dev_to_mem( + iio_interface->dev_instance, + bytes_count, iio_interface->ch_mask); + //else + struct iio_data_buffer *r_buff; + uint32_t samples; + ssize_t ret; + + r_buff = iio_interface->read_buffer; + if (r_buff && iio_interface->dev_descriptor->read_dev) { + if (bytes_count > r_buff->size) + return -ENOMEM; + samples = bytes_to_samples(iio_interface, bytes_count); + ret = iio_interface->dev_descriptor->read_dev( + iio_interface->dev_instance, + r_buff->buff, samples); + return ret < 0 ? ret : (ssize_t)bytes_count; + } + + return -ENOENT; +} + +/** + * @brief Read chunk of data from RAM to pbuf. Call + * "iio_transfer_dev_to_mem()" first. + * This function is probably called multiple times by libtinyiiod after a + * "iio_transfer_dev_to_mem" call, since we can only read "bytes_count" bytes. + * @param device - String containing device name. + * @param pbuf - Buffer where value is stored. + * @param offset - Offset to the remaining data after reading n chunks. + * @param bytes_count - Number of bytes to read. + * @return: Bytes_count or negative value in case of error. + */ +static ssize_t iio_read_dev(const char *device, char *pbuf, size_t offset, + size_t bytes_count) +{ + struct iio_interface *iio_interface = iio_get_interface(device); + + if (iio_interface->dev_descriptor->read_data) + return iio_interface->dev_descriptor->read_data( + iio_interface->dev_instance, + pbuf, offset, + bytes_count, iio_interface->ch_mask); + + //else + struct iio_data_buffer *r_buff; + + r_buff = iio_interface->read_buffer; + if (r_buff) { + if (offset + bytes_count > r_buff->size) + return -ENOMEM; + + memcpy(pbuf, r_buff->buff + offset, bytes_count); + + return bytes_count; + } + + return -ENOENT; +} + +/** + * @brief Transfer memory to device. + * @param device - String containing device name. + * @param bytes_count - Number of bytes to transfer. + * @return Bytes_count or negative value in case of error. + */ +static ssize_t iio_transfer_mem_to_dev(const char *device, size_t bytes_count) +{ + struct iio_interface *iio_interface = iio_get_interface(device); + + if (iio_interface->dev_descriptor->transfer_mem_to_dev) + return iio_interface->dev_descriptor->transfer_mem_to_dev( + iio_interface->dev_instance, + bytes_count, iio_interface->ch_mask); + + //else + struct iio_data_buffer *w_buff; + ssize_t ret; + uint32_t samples; + + w_buff = iio_interface->write_buffer; + if (w_buff && iio_interface->dev_descriptor->write_dev) { + if (bytes_count > w_buff->size) + return -ENOMEM; + samples = bytes_to_samples(iio_interface, bytes_count); + ret = iio_interface->dev_descriptor->write_dev( + iio_interface->dev_instance, + w_buff->buff, samples); + return ret < 0 ? ret : (ssize_t)bytes_count; + } + + return -ENOENT; +} + +/** + * @brief Write chunk of data into RAM. + * This function is probably called multiple times by libtinyiiod before a + * "iio_transfer_mem_to_dev" call, since we can only write "bytes_count" bytes + * at a time. + * @param device - String containing device name. + * @param buf - Values to write. + * @param offset - Offset in memory after the nth chunk of data. + * @param bytes_count - Number of bytes to write. + * @return Bytes_count or negative value in case of error. + */ +static ssize_t iio_write_dev(const char *device, const char *buf, + size_t offset, size_t bytes_count) +{ + struct iio_interface *iio_interface = iio_get_interface(device); + if(iio_interface->dev_descriptor->write_data) + return iio_interface->dev_descriptor->write_data( + iio_interface->dev_instance, + (char*)buf, offset, bytes_count, + iio_interface->ch_mask); + + //else + struct iio_data_buffer *w_buff; + + w_buff = iio_interface->write_buffer; + if (w_buff) { + if (offset + bytes_count > w_buff->size) + return -ENOMEM; + memcpy(w_buff->buff + offset, buf, bytes_count); + return bytes_count; + } + + return -ENOENT; +} + +/** + * @brief Get a merged xml containing all devices. + * @param outxml - Generated xml. + * @return SUCCESS in case of success or negative value otherwise. + */ +static ssize_t iio_get_xml(char **outxml) +{ + if (!outxml) + return FAILURE; + + *outxml = g_desc->xml_desc; + + return g_desc->xml_size; +} + +/** + * @brief Execute an iio step + * @param desc - IIo descriptor + * @return SUCCESS in case of success or negative value otherwise. + */ +ssize_t iio_step(struct iio_desc *desc) +{ +#ifdef ENABLE_IIO_NETWORK + int32_t ret; + + if (desc->phy_type == USE_NETWORK) { + if (desc->current_sock != NULL && + (int32_t)desc->current_sock != -1) { + ret = _push_sock(desc, desc->current_sock); + if (IS_ERR_VALUE(ret)) + return ret; + } + desc->current_sock = NULL; + } +#endif + return tinyiiod_read_command(desc->iiod); +} + +/* + * Generate an xml describing a device and write it to buff. + * Will return the size of the xml. + * If buff_size is 0, no data will be written to buff, but size will be returned + */ +static uint32_t iio_generate_device_xml(struct iio_device *device, char *name, + int32_t id, char *buff, + uint32_t buff_size) +{ + struct iio_channel *ch; + struct iio_attribute *attr; + char ch_id[50]; + int32_t i; + int32_t j; + int32_t k; + int32_t n; + + if ((int32_t)buff_size == -1) + n = 0; + else + n = buff_size; + + if (buff == NULL) + /* Set dummy value for buff. It is used only for counting */ + buff = ch_id; + + i = 0; + i += snprintf(buff, max(n - i, 0), + "<device id=\"device%"PRIi32"\" name=\"%s\">", id, name); + + /* Write channels */ + if (device->channels) + for (j = 0; j < device->num_ch; j++) { + ch = &device->channels[j]; + _print_ch_id(ch_id, ch); + i += snprintf(buff + i, max(n - i, 0), + "<channel id=\"%s\"", + ch_id); + if(ch->name) + i += snprintf(buff + i, max(n - i, 0), + " name=\"%s\"", + ch->name); + i += snprintf(buff + i, max(n - i, 0), + " type=\"%s\" >", + ch->ch_out ? "output" : "input"); + + if (ch->scan_type) + i += snprintf(buff + i, max(n - i, 0), + "<scan-element index=\"%d\"" + " format=\"%s:%c%d/%d>>%d\" />", + ch->scan_index, + ch->scan_type->is_big_endian ? "be" : "le", + ch->scan_type->sign, + ch->scan_type->realbits, + ch->scan_type->storagebits, + ch->scan_type->shift); + + /* Write channel attributes */ + if (ch->attributes) + for (k = 0; ch->attributes[k].name; k++) { + attr = &ch->attributes[k]; + i += snprintf(buff + i, max(n - i, 0), + "<attribute name=\"%s\"" + " filename=\"%s_%s_%s_%s\" />", + attr->name, + ch->ch_out ? "out" : "in", + ch_id, ch->name, + attr->name); + } + + i += snprintf(buff + i, max(n - i, 0), "</channel>"); + } + + /* Write device attributes */ + if (device->attributes) + for (j = 0; device->attributes[j].name; j++) + i += snprintf(buff + i, max(n - i, 0), + "<attribute name=\"%s\" />", + device->attributes[j].name); + + /* Write debug attributes */ + if (device->debug_attributes) + for (j = 0; device->debug_attributes[j].name; j++) + i += snprintf(buff + i, max(n - i, 0), + "<debug-attribute name=\"%s\" />", + device->debug_attributes[j].name); + if (device->debug_reg_read || device->debug_reg_write) + i += snprintf(buff + i, max(n - i, 0), + "<debug-attribute name=\""REG_ACCESS_ATTRIBUTE"\" />"); + + /* Write buffer attributes */ + if (device->buffer_attributes) + for (j = 0; device->buffer_attributes[j].name; j++) + i += snprintf(buff + i, max(n - i, 0), + "<buffer-attribute name=\"%s\" />", + device->buffer_attributes[j].name); + + i += snprintf(buff + i, max(n - i, 0), "</device>"); + + return i; +} + +/** + * @brief Register interface. + * @param desc - iio descriptor + * @param dev_descriptor - Device descriptor + * @param name - Name to identify the registered device + * @param dev_instance - Opened instance of the device + * @return SUCCESS in case of success or negative value otherwise. + */ +ssize_t iio_register(struct iio_desc *desc, struct iio_device *dev_descriptor, + char *name, void *dev_instance, + struct iio_data_buffer *read_buff, + struct iio_data_buffer *write_buff) +{ + struct iio_interface *iio_interface; + int32_t ret; + int32_t n; + int32_t new_size; + char *aux; + + iio_interface = (struct iio_interface *)calloc(1, + sizeof(*iio_interface)); + if (!iio_interface) + return -ENOMEM; + iio_interface->dev_instance = dev_instance; + iio_interface->name = name; + iio_interface->dev_descriptor = dev_descriptor; + iio_interface->read_buffer = read_buff; + iio_interface->write_buffer = write_buff; + + /* Get number of bytes needed for the xml of the new device */ + n = iio_generate_device_xml(iio_interface->dev_descriptor, + (char *)iio_interface->name, + desc->dev_count, NULL, -1); + + new_size = desc->xml_size + n; + aux = realloc(desc->xml_desc, new_size); + if (!aux) { + free(iio_interface); + return -ENOMEM; + } + + ret = desc->interfaces_list->push(desc->interfaces_list, iio_interface); + if (IS_ERR_VALUE(ret)) { + free(iio_interface); + free(aux); + return ret; + } + + desc->xml_desc = aux; + /* Print the new device xml at the end of the xml */ + iio_generate_device_xml(iio_interface->dev_descriptor, + (char *)iio_interface->name, + desc->dev_count, + desc->xml_desc + desc->xml_size_to_last_dev, + new_size - desc->xml_size_to_last_dev); + sprintf((char *)iio_interface->dev_id, "device%d", (int)desc->dev_count); + desc->xml_size_to_last_dev += n; + desc->xml_size += n; + /* Copy end header at the end */ + strcat(desc->xml_desc, header_end); + + desc->dev_count++; + + return SUCCESS; +} + +/** + * @brief Unregister interface. + * @param name - Name of the registered device + * @return SUCCESS in case of success or negative value otherwise. + */ +ssize_t iio_unregister(struct iio_desc *desc, char *name) +{ + struct iio_interface *to_remove_interface; + struct iio_interface search_interface; + int32_t ret; + int32_t n; + char *aux; + + /* Get if the item is found, get will remove it from the list */ + search_interface.name = name; + ret = list_get_find(desc->interfaces_list, + (void **)&to_remove_interface, &search_interface); + if (IS_ERR_VALUE(ret)) + return ret; + free(to_remove_interface); + + /* Get number of bytes needed for the xml of the device */ + n = iio_generate_device_xml(to_remove_interface->dev_descriptor, + (char *)to_remove_interface->name, + desc->dev_count, NULL, -1); + + /* Overwritte the deleted device */ + aux = desc->xml_desc + desc->xml_size_to_last_dev - n; + memmove(aux, aux + n, strlen(aux + n)); + + /* Decrease the xml size */ + desc->xml_size -= n; + desc->xml_size_to_last_dev -= n; + + return SUCCESS; +} + +static int32_t iio_cmp_interfaces(struct iio_interface *a, + struct iio_interface *b) +{ + return strcmp(a->dev_id, b->dev_id); +} + +/** + * @brief Set communication ops and read/write ops that will be called + * from "libtinyiiod". + * @param iiod - Structure containing new tinyiiod instance. + * @param iio_server_ops - Structure containing read/write ops (Ex: read/write to + * UART). + * @return SUCCESS in case of success or negative value otherwise. + */ +ssize_t iio_init(struct iio_desc **desc, struct iio_init_param *init_param) +{ + int32_t ret; + struct iio_desc *ldesc; + struct tinyiiod_ops *ops; + + if (!init_param) + return -EINVAL; + + ops = (struct tinyiiod_ops *)calloc(1, sizeof(struct tinyiiod_ops)); + if (!ops) + return FAILURE; + + ldesc = (struct iio_desc *)calloc(1, sizeof(*ldesc)); + if (!ldesc) + goto free_ops; + ldesc->iiod_ops = ops; + + /* device operations */ + ops->read_attr = iio_read_attr; + ops->write_attr = iio_write_attr; + ops->ch_read_attr = iio_ch_read_attr; + ops->ch_write_attr = iio_ch_write_attr; + ops->transfer_dev_to_mem = iio_transfer_dev_to_mem; + ops->read_data = iio_read_dev; + ops->transfer_mem_to_dev = iio_transfer_mem_to_dev; + ops->write_data = iio_write_dev; + + ops->open = iio_open_dev; + ops->close = iio_close_dev; + ops->get_mask = iio_get_mask; + + ops->read = iio_phy_read; + ops->write = iio_phy_write; + + ldesc->xml_size = sizeof(header) + sizeof(header_end); + ldesc->xml_desc = (char *)malloc(ldesc->xml_size); + if (!ldesc->xml_desc) + goto free_desc; + + strcpy(ldesc->xml_desc, header); + strcat(ldesc->xml_desc, header_end); + ldesc->xml_size_to_last_dev = sizeof(header) - 1; + + ldesc->phy_type = init_param->phy_type; + if (init_param->phy_type == USE_UART) { + ret = uart_init((struct uart_desc **)&ldesc->uart_desc, + init_param->uart_init_param); + if (IS_ERR_VALUE(ret)) + goto free_desc; + } +#ifdef ENABLE_IIO_NETWORK + else if (init_param->phy_type == USE_NETWORK) { + ret = socket_init(&ldesc->server, + init_param->tcp_socket_init_param); + if (IS_ERR_VALUE(ret)) + goto free_desc; + ret = socket_bind(ldesc->server, IIOD_PORT); + if (IS_ERR_VALUE(ret)) + goto free_pylink; + ret = socket_listen(ldesc->server, 0); + if (IS_ERR_VALUE(ret)) + goto free_pylink; + ret = cb_init(&ldesc->sockets, sizeof(struct tcp_socket_desc *) + * MAX_SOCKET_TO_HANDLE); + if (IS_ERR_VALUE(ret)) + goto free_pylink; + } +#endif + else { + goto free_desc; + } + + ops->read = iio_phy_read; + ops->write = iio_phy_write; + + ops->get_xml = iio_get_xml; + + ret = list_init(&ldesc->interfaces_list, LIST_PRIORITY_LIST, + (f_cmp)iio_cmp_interfaces); + if (IS_ERR_VALUE(ret)) + goto free_pylink; + + ldesc->iiod = tinyiiod_create(ops); + if (!(ldesc->iiod)) + goto free_list; + + *desc = ldesc; + g_desc = ldesc; + + return SUCCESS; + +free_list: + list_remove(ldesc->interfaces_list); +free_pylink: + if (ldesc->phy_type == USE_UART) + uart_remove(ldesc->uart_desc); +#ifdef ENABLE_IIO_NETWORK + else { + socket_remove(ldesc->server); + if (ldesc->sockets) + cb_remove(ldesc->sockets); + } +#endif +free_desc: + free(ldesc); +free_ops: + free(ops); + + return FAILURE; +} + +/** + * @brief Free the resources allocated by "iio_init()". + * @param iiod: Structure containing tinyiiod instance. + * @return SUCCESS in case of success or negative value otherwise. + */ +ssize_t iio_remove(struct iio_desc *desc) +{ + struct iio_interface *iio_interface; + + while (SUCCESS == list_get_first(desc->interfaces_list, + (void **)&iio_interface)) + free(iio_interface); + list_remove(desc->interfaces_list); + + free(desc->iiod_ops); + tinyiiod_destroy(desc->iiod); + + free(desc->xml_desc); + + if (desc->phy_type == USE_UART) { + uart_remove(desc->phy_desc); + } +#ifdef ENABLE_IIO_NETWORK + else { + socket_remove(desc->server); + cb_remove(desc->sockets); + } +#endif + + free(desc); + + return SUCCESS; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/iio.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,96 @@ +/***************************************************************************//** + * @file iio.h + * @brief Header file of iio + * @author Cristian Pop (cristian.pop@analog.com) + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * Copyright 2013(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +#ifndef IIO_H_ +#define IIO_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include "iio_types.h" +#include "uart.h" +#ifdef ENABLE_IIO_NETWORK +#include "tcp_socket.h" +#endif + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +enum pysical_link_type { + USE_UART, +#ifdef ENABLE_IIO_NETWORK + USE_NETWORK +#endif +}; + +struct iio_desc; + +struct iio_init_param { + enum pysical_link_type phy_type; + union { + struct uart_init_param *uart_init_param; +#ifdef ENABLE_IIO_NETWORK + struct tcp_socket_init_param *tcp_socket_init_param; +#endif + }; +}; + +/******************************************************************************/ +/************************ Functions Declarations ******************************/ +/******************************************************************************/ + +/* Set communication ops and read/write ops that will be called from + * "libtinyiiod". */ +ssize_t iio_init(struct iio_desc **desc, struct iio_init_param *init_param); +/* Free the resources allocated by iio_init(). */ +ssize_t iio_remove(struct iio_desc *desc); +/* Execut an iio step. */ +ssize_t iio_step(struct iio_desc *desc); +/* Register interface. */ +ssize_t iio_register(struct iio_desc *desc, struct iio_device *dev_descriptor, + char *name, void *dev_instance, + struct iio_data_buffer *read_buff, + struct iio_data_buffer *write_buff); +/* Unregister interface. */ +ssize_t iio_unregister(struct iio_desc *desc, char *name); + +#endif /* IIO_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/iio_types.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,211 @@ +/***************************************************************************//** + * @file iio_types.h + * @brief Header file for iio_types + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2013(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#ifndef IIO_TYPES_H_ +#define IIO_TYPES_H_ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <stdbool.h> +#include <stdint.h> +#include <sys/types.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct iio_chan_type + * @brief IIO channel types + */ +enum iio_chan_type { + IIO_VOLTAGE, + IIO_CURRENT, + IIO_ALTVOLTAGE, + IIO_ANGL_VEL, + IIO_TEMP, + /* All new types must be added before this field */ + IIO_LAST_TYPE +}; + +/** + * @struct iio_modifier + * @brief IIO channel modifier + */ +enum iio_modifier { + IIO_NO_MOD, + IIO_MOD_X, + IIO_MOD_Y, + IIO_MOD_Z, +}; + +/** + * @struct iio_ch_info + * @brief Structure holding channel attributess. + */ +struct iio_ch_info { + /** Channel number */ + int16_t ch_num; + /** Channel type: input/output */ + bool ch_out; +}; + +#define END_ATTRIBUTES_ARRAY {.name = NULL} + +/** + * @struct iio_attribute + * @brief Structure holding pointers to show and store functions. + */ +struct iio_attribute { + /** Attribute name */ + const char *name; + /** Attribute id */ + intptr_t priv; + /** Show function pointer */ + ssize_t (*show)(void *device, char *buf, size_t len, + const struct iio_ch_info *channel, intptr_t priv); + /** Store function pointer */ + ssize_t (*store)(void *device, char *buf, size_t len, + const struct iio_ch_info *channel, intptr_t priv); +}; + +/** + * @struct iio_channel + * @brief Struct describing the scan type + */ +struct scan_type { + /** 's' or 'u' to specify signed or unsigned */ + char sign; + /** Number of valid bits of data */ + uint8_t realbits; + /** Realbits + padding */ + uint8_t storagebits; + /** Shift right by this before masking out realbits. */ + uint8_t shift; + /** True if big endian, false if little endian */ + bool is_big_endian; +}; + +/** + * @struct iio_channel + * @brief Structure holding attributes of a channel. + */ +struct iio_channel { + /** channel name */ + char *name; + /** Chanel type */ + enum iio_chan_type ch_type; + /** Channel number when the same channel type */ + int channel; + /** If modified is set, this provides the modifier. E.g. IIO_MOD_X + * for angular rate when applied to channel2 will make make the + * IIO_ANGL_VEL have anglvel_x which corresponds to the x-axis. */ + int channel2; + /** Index to give ordering in scans when read from a buffer. */ + int scan_index; + /** */ + struct scan_type *scan_type; + /** Array of attributes. Last one should have its name set to NULL */ + struct iio_attribute *attributes; + /** if true, the channel is an output channel */ + bool ch_out; + /** Set if channel has a modifier. Use channel2 property to + * select the modifier to use.*/ + bool modified; + /** Specify if channel has a numerical index. If not set, channel + * number will be suppressed. */ + bool indexed; + /* Set if the channel is differential. */ + bool diferential; +}; + +struct iio_data_buffer { + uint32_t size; + void *buff; +}; + +/** + * @struct iio_device + * @brief Structure holding channels and attributes of a device. + */ +struct iio_device { + /** Device number of channels */ + uint16_t num_ch; + /** List of channels */ + struct iio_channel *channels; + /** Array of attributes. Last one should have its name set to NULL */ + struct iio_attribute *attributes; + /** Array of attributes. Last one should have its name set to NULL */ + struct iio_attribute *debug_attributes; + /** Array of attributes. Last one should have its name set to NULL */ + struct iio_attribute *buffer_attributes; + /** Transfer data from device into RAM */ + ssize_t (*transfer_dev_to_mem)(void *dev_instance, size_t bytes_count, + uint32_t ch_mask); + /** Read data from RAM to pbuf. It should be called after "transfer_dev_to_mem" */ + ssize_t (*read_data)(void *dev_instance, char *pbuf, size_t offset, + size_t bytes_count, uint32_t ch_mask); + /** Transfer data from RAM to device */ + ssize_t (*transfer_mem_to_dev)(void *dev_instance, size_t bytes_count, + uint32_t ch_mask); + /** Write data to RAM. It should be called before "transfer_mem_to_dev" */ + ssize_t (*write_data)(void *dev_instance, char *pbuf, size_t offset, + size_t bytes_count, uint32_t ch_mask); + /** Called before a transfer starts. The device should activate the + * channels from the mask */ + int32_t (*prepare_transfer)(void *dev, uint32_t mask); + /** Called after a tranfer ends */ + int32_t (*end_transfer)(void *dev); + /* Numbers of bytes will be: + * samples * (storage_size_of_first_active_ch / 8) * nb_active_channels + */ + int32_t (*read_dev)(void *dev, void *buff, uint32_t nb_samples); + /* Numbers of bytes will be: + * samples * (storage_size_of_first_active_ch / 8) * nb_active_channels + */ + int32_t (*write_dev)(void *dev, void *buff, uint32_t nb_samples); + /* Read device register */ + int32_t (*debug_reg_read)(void *dev, uint32_t reg, uint32_t *readval); + /* Write device register */ + int32_t (*debug_reg_write)(void *dev, uint32_t reg, uint32_t writeval); + +}; + +#endif /* IIO_TYPES_H_ */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/LICENSE Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,504 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +(This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.) + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + {description} + Copyright (C) {year} {fullname} + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random + Hacker. + + {signature of Ty Coon}, 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it!
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/LICENSE_ADIBSD Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,33 @@ + +Copyright 2011(c) Analog Devices, Inc. + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + - Neither the name of Analog Devices, Inc. nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + - The use of this software may or may not infringe the patent rights + of one or more patent holders. This license does not release you + from the requirement that you obtain separate licenses from these + patent holders to use this software. + - Use of the software either in source or binary form, must be run + on or directly connected to an Analog Devices Inc. component. + +THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. + +IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY +RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/README.md Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,14 @@ +# libtinyiiod + +Tiny IIO Daemon Library + +## License + +This work is dual-licensed under the ADI-BSD License and GNU Lesser General Public License v2.1 (or later). +You can choose between one of them if you use this work. + +`SPDX-License-Identifier: LGPL-2.1-or-later` + + * [LICENSE_ADIBSD](./LICENSE_ADIBSD) + + * [LICENSE_LGPL](./LICENSE) \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/compat.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,58 @@ +/* + * libtinyiiod - Tiny IIO Daemon Library + * + * Copyright (C) 2019 Analog Devices, Inc. + * Author: Cristian Pop <cristian.pop@analog.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#ifndef COMPAT_H +#define COMPAT_H + +#ifdef _USE_STD_INT_TYPES +#include <stdint.h> +#include <inttypes.h> +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.h> +#include <inttypes.h> +#include <errno.h> +#include <string.h> +#else // _USE_STD_INT_TYPES +#if !defined(__ssize_t_defined) && !defined(_SSIZE_T_DEFINED) +typedef long int ssize_t; +typedef unsigned long int size_t; +#define __ssize_t_defined +#define _SSIZE_T_DEFINED +#endif + +typedef long int32_t; +typedef unsigned long uint32_t; +typedef int bool; + +#define true 1 +#define false 0 + +#define NULL (void*)0 + +#define ENOENT 2 /* No such file or directory */ +#define EIO 5 /* I/O error */ +#define ENODEV 19 /* No such device */ +#define EINVAL 22 /* Invalid argument */ +#define ENOSYS 38 /* Function not implemented */ + +#define PRIi32 "li" +# define PRIx32 "x" +# define PRIu16 "u" +#endif //_USE_STD_INT_TYPES + +#endif /* COMPAT_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/parser.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,217 @@ +/* + * libtinyiiod - Tiny IIO Daemon Library + * + * Copyright (C) 2016 Analog Devices, Inc. + * Author: Paul Cercueil <paul.cercueil@analog.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#include "tinyiiod-private.h" + +#include "compat.h" + +static int32_t parse_rw_string(struct tinyiiod *iiod, char *str, bool write) +{ + char *device, *channel, *attr, *ptr; + bool is_channel = false, output = false; + enum iio_attr_type type = IIO_ATTR_TYPE_DEVICE; + long bytes; + + ptr = strchr(str, ' '); + if (!ptr) + return -EINVAL; + + *ptr = '\0'; + device = str; + str = ptr + 1; + + if (!strncmp(str, "INPUT ", sizeof("INPUT ") - 1)) { + is_channel = true; + str += sizeof("INPUT ") - 1; + } else if (!strncmp(str, "OUTPUT ", sizeof("OUTPUT ") - 1)) { + is_channel = true; + output = true; + str += sizeof("OUTPUT ") - 1; + } else if (!strncmp(str, "DEBUG ", sizeof("DEBUG ") - 1)) { + type = IIO_ATTR_TYPE_DEBUG; + str += sizeof("DEBUG ") - 1; + } else if (!strncmp(str, "BUFFER ", sizeof("BUFFER ") - 1)) { + type = IIO_ATTR_TYPE_BUFFER; + str += sizeof("BUFFER ") - 1; + } + + if (is_channel) { + ptr = strchr(str, ' '); + if (!ptr) + return -EINVAL; + + *ptr = '\0'; + channel = str; + str = ptr + 1; + } else { + channel = NULL; + } + + ptr = strchr(str, ' '); + if ((!!ptr) ^ write) + return -EINVAL; + + attr = str; + + if (write) { + *ptr = '\0'; + str = ptr + 1; + } else { + tinyiiod_do_read_attr(iiod, device, channel, + output, attr, type); + return 0; + } + + bytes = strtol(str, &ptr, 10); + if (str == ptr || bytes < 0) + return -EINVAL; + + tinyiiod_do_write_attr(iiod, device, channel, + output, attr, (size_t) bytes, type); + + return 0; +} + +static int32_t parse_open_string(struct tinyiiod *iiod, char *str) +{ + char *device, *ptr; + long samples_count; + uint32_t mask = 0; + + ptr = strchr(str, ' '); + if (!ptr) + return -EINVAL; + + *ptr = '\0'; + device = str; + str = ptr + 1; + + samples_count = strtol(str, &ptr, 10); + if (str == ptr || *ptr != ' ' || samples_count < 0) + return -EINVAL; + + str = ptr + 1; + + mask = strtoul(str, NULL, 16); + + tinyiiod_do_open(iiod, device, (size_t) samples_count, mask); + + return 0; +} + +static int32_t parse_timeout_string(struct tinyiiod *iiod, char *str) +{ + uint32_t timeout = strtoul(str, NULL, 10); + + return tinyiiod_set_timeout(iiod, timeout); +} + +static int32_t parse_writebuf_string(struct tinyiiod *iiod, char *str) +{ + char *device, *ptr; + long bytes_count; + + ptr = strchr(str, ' '); + if (!ptr) + return -EINVAL; + + *ptr = '\0'; + device = str; + str = ptr + 1; + + bytes_count = strtol(str, &ptr, 10); + if (str == ptr || *ptr != '\0' || bytes_count < 0) + return -EINVAL; + + return tinyiiod_do_writebuf(iiod, device, (size_t) bytes_count); +} + +static int32_t parse_readbuf_string(struct tinyiiod *iiod, char *str) +{ + char *device, *ptr; + long bytes_count; + + ptr = strchr(str, ' '); + if (!ptr) + return -EINVAL; + + *ptr = '\0'; + device = str; + str = ptr + 1; + + bytes_count = strtol(str, &ptr, 10); + if (str == ptr || *ptr != '\0' || bytes_count < 0) + return -EINVAL; + + return tinyiiod_do_readbuf(iiod, device, (size_t) bytes_count); +} + +int32_t tinyiiod_parse_string(struct tinyiiod *iiod, char *str) +{ + while (*str == '\n' || *str == '\r') + str++; + + if (str[0] == '\0') + return 0; + + if (!strncmp(str, "VERSION", sizeof("VERSION"))) { + char buf[32]; + + snprintf(buf, sizeof(buf), "%"PRIu16".%"PRIu16".%07x\n", + TINYIIOD_VERSION_MAJOR, + TINYIIOD_VERSION_MINOR, + TINYIIOD_VERSION_GIT); + tinyiiod_write_string(iiod, buf); + return 0; + } + + if (!strncmp(str, "PRINT", sizeof("PRINT"))) { + tinyiiod_write_xml(iiod); + return 0; + } + + if (!strncmp(str, "READ ", sizeof("READ ") - 1)) + return parse_rw_string(iiod, str + sizeof("READ ") - 1, false); + + if (!strncmp(str, "WRITE ", sizeof("WRITE ") -1)) + return parse_rw_string(iiod, str + sizeof("WRITE ") - 1, true); + + if (!strncmp(str, "OPEN ", sizeof("OPEN ") -1)) + return parse_open_string(iiod, str + sizeof("OPEN ") - 1); + + if (!strncmp(str, "CLOSE ", sizeof("CLOSE ") -1)) { + tinyiiod_do_close(iiod, str + sizeof("CLOSE ") - 1); + return 0; + } + + if (!strncmp(str, "READBUF ", sizeof("READBUF ") -1)) + return parse_readbuf_string(iiod, str + sizeof("READBUF ") - 1); + + if (!strncmp(str, "TIMEOUT ", sizeof("TIMEOUT ") - 1)) + return parse_timeout_string(iiod, str + sizeof("TIMEOUT ") - 1); + + if (!strncmp(str, "WRITEBUF ", sizeof("WRITEBUF ") -1)) + return parse_writebuf_string(iiod, str + sizeof("WRITEBUF ") - 1); + + if (!strncmp(str, "EXIT", sizeof("EXIT") - 1)) + return tinyiiod_do_close_instance(iiod); + + if (!strncmp(str, "GETTRIG", sizeof("GETTRIG") - 1)) + return tinyiiod_write_value(iiod, -ENODEV); + + return -EINVAL; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/tinyiiod-private.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,59 @@ +/* + * libtinyiiod - Tiny IIO Daemon Library + * + * Copyright (C) 2016 Analog Devices, Inc. + * Author: Paul Cercueil <paul.cercueil@analog.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#ifndef TINYIIOD_PRIVATE_H +#define TINYIIOD_PRIVATE_H + +#include "tinyiiod.h" + +char tinyiiod_read_char(struct tinyiiod *iiod); +ssize_t tinyiiod_read(struct tinyiiod *iiod, char *buf, size_t len); +ssize_t tinyiiod_read_line(struct tinyiiod *iiod, char *buf, size_t len); + +ssize_t tinyiiod_write_char(struct tinyiiod *iiod, char c); +ssize_t tinyiiod_write(struct tinyiiod *iiod, const char *data, size_t len); +ssize_t tinyiiod_write_string(struct tinyiiod *iiod, const char *str); +ssize_t tinyiiod_write_value(struct tinyiiod *iiod, int32_t value); + +void tinyiiod_write_xml(struct tinyiiod *iiod); + +void tinyiiod_do_read_attr(struct tinyiiod *iiod, const char *device, + const char *channel, bool ch_out, const char *attr, enum iio_attr_type type); + +void tinyiiod_do_write_attr(struct tinyiiod *iiod, const char *device, + const char *channel, bool ch_out, const char *attr, + size_t bytes, enum iio_attr_type type); + +void tinyiiod_do_open(struct tinyiiod *iiod, const char *device, + size_t sample_size, uint32_t mask); +void tinyiiod_do_close(struct tinyiiod *iiod, const char *device); + +int32_t tinyiiod_do_open_instance(struct tinyiiod *iiod); + +int32_t tinyiiod_do_close_instance(struct tinyiiod *iiod); + +int32_t tinyiiod_do_readbuf(struct tinyiiod *iiod, + const char *device, size_t bytes_count); + +int32_t tinyiiod_do_writebuf(struct tinyiiod *iiod, const char *device, + size_t bytes_count); + +int32_t tinyiiod_parse_string(struct tinyiiod *iiod, char *str); + +int32_t tinyiiod_set_timeout(struct tinyiiod *iiod, uint32_t timeout); + +#endif /* TINYIIOD_PRIVATE_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/tinyiiod.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,286 @@ +/* + * libtinyiiod - Tiny IIO Daemon Library + * + * Copyright (C) 2016 Analog Devices, Inc. + * Author: Paul Cercueil <paul.cercueil@analog.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#include "tinyiiod-private.h" + +#include "compat.h" + +struct tinyiiod { + struct tinyiiod_ops *ops; + char *buf; +}; + +struct tinyiiod * tinyiiod_create(struct tinyiiod_ops *ops) +{ + struct tinyiiod *iiod = malloc(sizeof(*iiod)); + + if (!iiod) + return NULL; + + iiod->buf = malloc(IIOD_BUFFER_SIZE); + if (!iiod->buf) { + free(iiod); + return NULL; + } + iiod->ops = ops; + + return iiod; +} + +void tinyiiod_destroy(struct tinyiiod *iiod) +{ + free(iiod->buf); + free(iiod); +} + +int32_t tinyiiod_read_command(struct tinyiiod *iiod) +{ + char buf[128]; + int32_t ret; + + ret = tinyiiod_read_line(iiod, buf, sizeof(buf)); + if (ret < 0) + return ret; + + ret = tinyiiod_parse_string(iiod, buf); + if (ret < 0) + tinyiiod_write_value(iiod, ret); + + return ret; +} + +ssize_t tinyiiod_read(struct tinyiiod *iiod, char *buf, size_t len) +{ + return iiod->ops->read(buf, len); +} + +ssize_t tinyiiod_read_line(struct tinyiiod *iiod, char *buf, size_t len) +{ + char ch; + uint32_t i; + bool found = false; + int32_t ret; + + if (iiod->ops->read_line) + return iiod->ops->read_line(buf, len); + + for (i = 0; i < len - 1; i++) { + ret = iiod->ops->read(&ch, 1); + if (ret <= 0) + return -EIO; + buf[i] = ch; + if (buf[i] != '\n') + found = true; + else if (found) + break; + } + + if (!found || i == len - 1) { + /* No \n found -> garbage data */ + return -EIO; + } + + buf[i - 1] = '\0'; + + return i; +} + +ssize_t tinyiiod_write_char(struct tinyiiod *iiod, char c) +{ + return iiod->ops->write(&c, 1); +} + +ssize_t tinyiiod_write(struct tinyiiod *iiod, const char *data, size_t len) +{ + return iiod->ops->write(data, len); +} + +ssize_t tinyiiod_write_string(struct tinyiiod *iiod, const char *str) +{ + return tinyiiod_write(iiod, str, strlen(str)); +} + +ssize_t tinyiiod_write_value(struct tinyiiod *iiod, int32_t value) +{ + char buf[16]; + + snprintf(buf, sizeof(buf), "%"PRIi32"\n", value); + return tinyiiod_write_string(iiod, buf); +} + +void tinyiiod_write_xml(struct tinyiiod *iiod) +{ + char *xml; + iiod->ops->get_xml(&xml); + size_t len = strlen(xml); + + tinyiiod_write_value(iiod, len); + tinyiiod_write(iiod, xml, len); + tinyiiod_write_char(iiod, '\n'); +} + +void tinyiiod_do_read_attr(struct tinyiiod *iiod, const char *device, + const char *channel, bool ch_out, const char *attr, enum iio_attr_type type) +{ + ssize_t ret; + + if (channel) + ret = iiod->ops->ch_read_attr(device, channel, + ch_out, attr, iiod->buf, IIOD_BUFFER_SIZE); + else + ret = iiod->ops->read_attr(device, attr, + iiod->buf, IIOD_BUFFER_SIZE, type); + + tinyiiod_write_value(iiod, (int32_t) ret); + if (ret > 0) { + iiod->buf[ret] = '\n'; + tinyiiod_write(iiod, iiod->buf, (size_t) ret + 1); + } +} + +void tinyiiod_do_write_attr(struct tinyiiod *iiod, const char *device, + const char *channel, bool ch_out, const char *attr, + size_t bytes, enum iio_attr_type type) +{ + ssize_t ret; + + if (bytes > IIOD_BUFFER_SIZE - 1) + bytes = IIOD_BUFFER_SIZE - 1; + + tinyiiod_read(iiod, iiod->buf, bytes); + iiod->buf[bytes] = '\0'; + + if (channel) + ret = iiod->ops->ch_write_attr(device, channel, ch_out, + attr, iiod->buf, bytes); + else + ret = iiod->ops->write_attr(device, attr, iiod->buf, bytes, type); + + tinyiiod_write_value(iiod, (int32_t) ret); +} + +void tinyiiod_do_open(struct tinyiiod *iiod, const char *device, + size_t sample_size, uint32_t mask) +{ + int32_t ret = iiod->ops->open(device, sample_size, mask); + tinyiiod_write_value(iiod, ret); +} + +void tinyiiod_do_close(struct tinyiiod *iiod, const char *device) +{ + int32_t ret = iiod->ops->close(device); + tinyiiod_write_value(iiod, ret); +} + +int32_t tinyiiod_do_open_instance(struct tinyiiod *iiod) +{ + if (iiod->ops->open_instance) + return iiod->ops->open_instance(); + + return 0; +} + +int32_t tinyiiod_do_close_instance(struct tinyiiod *iiod) +{ + if (iiod->ops->close_instance) + return iiod->ops->close_instance(); + + return 0; +} + +int32_t tinyiiod_do_writebuf(struct tinyiiod *iiod, + const char *device, size_t bytes_count) +{ + size_t bytes, offset = 0, total_bytes = bytes_count; + char buf[256]; + int32_t ret = 0; + + tinyiiod_write_value(iiod, bytes_count); + while (bytes_count) { + bytes = bytes_count > sizeof(buf) ? sizeof(buf) : bytes_count; + ret = tinyiiod_read(iiod, buf, bytes); + if (ret > 0) { + ret = iiod->ops->write_data(device, buf, offset, ret); + offset += ret; + if (ret < 0) + return ret; + bytes_count -= ret; + } else + return ret; + } + if (iiod->ops->transfer_mem_to_dev) { + ret = iiod->ops->transfer_mem_to_dev(device, total_bytes); + if (ret < 0) + return ret; + } + tinyiiod_write_value(iiod, (int) total_bytes); + + return ret; +} + +int32_t tinyiiod_do_readbuf(struct tinyiiod *iiod, + const char *device, size_t bytes_count) +{ + int32_t ret; + char buf[256]; + uint32_t mask; + bool print_mask = true; + size_t offset = 0; + + ret = iiod->ops->get_mask(device, &mask); + if (ret < 0) { + return ret; + } + if (iiod->ops->transfer_dev_to_mem) { + ret = iiod->ops->transfer_dev_to_mem(device, bytes_count); + if (ret < 0) + return ret; + } + while (bytes_count) { + size_t bytes = bytes_count > sizeof(buf) ? sizeof(buf) : bytes_count; + + ret = (int) iiod->ops->read_data(device, buf, offset, bytes); + offset += bytes; + tinyiiod_write_value(iiod, ret); + if (ret < 0) + return ret; + + if (print_mask) { + char buf_mask[10]; + + snprintf(buf_mask, sizeof(buf_mask), "%08"PRIx32"\n", mask); + tinyiiod_write_string(iiod, buf_mask); + print_mask = false; + } + + tinyiiod_write(iiod, buf, (size_t) ret); + bytes_count -= (size_t) ret; + } + + return ret; +} + +int32_t tinyiiod_set_timeout(struct tinyiiod *iiod, uint32_t timeout) +{ + int32_t ret = 0; + + if (iiod->ops->set_timeout) + ret = iiod->ops->set_timeout(timeout); + tinyiiod_write_value(iiod, ret); + + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/libraries/iio/libtinyiiod/tinyiiod.h Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,76 @@ +/* + * libtinyiiod - Tiny IIO Daemon Library + * + * Copyright (C) 2016 Analog Devices, Inc. + * Author: Paul Cercueil <paul.cercueil@analog.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ + +#ifndef TINYIIOD_H +#define TINYIIOD_H + +#include "compat.h" + +struct tinyiiod; + +enum iio_attr_type { + IIO_ATTR_TYPE_DEVICE = 0, + IIO_ATTR_TYPE_DEBUG = 1, + IIO_ATTR_TYPE_BUFFER = 2, +}; + +struct tinyiiod_ops { + /* Read from the input stream */ + ssize_t (*read)(char *buf, size_t len); + + /* Write to the output stream */ + ssize_t (*write)(const char *buf, size_t len); + ssize_t (*read_line)(char *buf, size_t len); + + ssize_t (*open_instance)(); + + ssize_t (*close_instance)(); + + ssize_t (*read_attr)(const char *device, const char *attr, + char *buf, size_t len, enum iio_attr_type type); + ssize_t (*write_attr)(const char *device, const char *attr, + const char *buf, size_t len, enum iio_attr_type type); + + ssize_t (*ch_read_attr)(const char *device, const char *channel, + bool ch_out, const char *attr, char *buf, size_t len); + ssize_t (*ch_write_attr)(const char *device, const char *channel, + bool ch_out, const char *attr, + const char *buf, size_t len); + + int32_t (*open)(const char *device, size_t sample_size, uint32_t mask); + int32_t (*close)(const char *device); + + ssize_t (*transfer_dev_to_mem)(const char *device, size_t bytes_count); + ssize_t (*read_data)(const char *device, char *buf, size_t offset, + size_t bytes_count); + + ssize_t (*transfer_mem_to_dev)(const char *device, size_t bytes_count); + ssize_t (*write_data)(const char *device, const char *buf, size_t offset, + size_t bytes_count); + + int32_t (*get_mask)(const char *device, uint32_t *mask); + + int32_t (*set_timeout)(uint32_t timeout); + + ssize_t (*get_xml)(char **outxml); +}; + +struct tinyiiod * tinyiiod_create(struct tinyiiod_ops *ops); +void tinyiiod_destroy(struct tinyiiod *iiod); +int32_t tinyiiod_read_command(struct tinyiiod *iiod); + +#endif /* TINYIIOD_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/circular_buffer.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,412 @@ +/***************************************************************************//** + * @file circular_buffer.c + * @brief Circular buffer implementation + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * @copyright + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> +#include "circular_buffer.h" +#include "error.h" +#include "util.h" + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct cb_ptr + * @brief Circular buffer pointer + */ +struct cb_ptr { + /** Index of data in the buffer */ + uint32_t idx; + /** Counts the number of times idx exceeds the liniar buffer */ + uint32_t spin_count; + /** Set if async transaction is active */ + bool async_started; + /** Number of bytes to update after an async transaction is finished */ + uint32_t async_size; +}; + +/** + * @struct circular_buffer + * @brief Circular buffer descriptor + */ +struct circular_buffer { + /** Size of the buffer in bytes */ + uint32_t size; + /** Address of the buffer */ + int8_t *buff; + /** Write pointer */ + struct cb_ptr write; + /** Read pointer */ + struct cb_ptr read; +}; + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Create circular buffer structure + * + * @note Circular buffer implementation is thread safe for one write + * and one reader. + * If multiple writer or multiple readers access the circular buffer then + * function that updates the structure should be called inside a critical + * critical section. + * + * @param desc - Where to store the circular buffer reference + * @param buff_size - Buffer size + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t cb_init(struct circular_buffer **desc, uint32_t buff_size) +{ + struct circular_buffer *ldesc; + + if (!desc || !buff_size) + return -EINVAL; + + ldesc = (struct circular_buffer*)calloc(1, sizeof(*ldesc)); + if (!ldesc) + return -ENOMEM; + + *desc = ldesc; + + ldesc->size = buff_size; + ldesc->buff = calloc(1, buff_size); + if (!ldesc->buff) { + free(ldesc); + return -ENOMEM; + } + + return SUCCESS; +} + +/** + * @brief Free the resources allocated for the circular buffer structure + * @param desc - Circular buffer reference + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t cb_remove(struct circular_buffer *desc) +{ + if (!desc) + return FAILURE; + + if (desc->buff) + free(desc->buff); + free(desc); + + return SUCCESS; +} + +/** + * @brief Get the number of elements in the buffer + * @param desc - Circular buffer reference + * @param size - Where to store size of data available to read + * @return + * - \ref SUCCESS - No errors + * - -EINVAL - Wrong parameters used + * - -EOVERRUN - A buffer overrun occurred + */ +int32_t cb_size(struct circular_buffer *desc, uint32_t *size) +{ + uint32_t nb_spins; + + if (!desc || !size) + return -EINVAL; + + if (desc->write.spin_count > desc->read.spin_count) + nb_spins = desc->write.spin_count - desc->read.spin_count; + else + /* Integer overflow on desc->write.spin_count */ + nb_spins = UINT32_MAX - desc->read.spin_count + + desc->write.spin_count + 1; + + if (nb_spins > 0) + *size = desc->size + desc->write.idx - desc->read.idx; + else + *size = desc->write.idx - desc->read.idx; + + if (*size > desc->size) { + *size = desc->size; + return -EOVERRUN; + } + + return SUCCESS; +} + +/* + * Functionality described at cb_prepare_async_write/read having the is_read + * parameter to specifiy if it is a read or write operation + */ +static int32_t cb_prepare_async_operation(struct circular_buffer *desc, + uint32_t requested_size, + void **buff, + uint32_t *raw_size_available, + bool is_read) +{ + struct cb_ptr *ptr; + uint32_t available_size; + uint32_t ret; + + if (!desc || !buff || !raw_size_available) + return -EINVAL; + + ret = SUCCESS; + /* Select if read or write index will be updated */ + ptr = is_read ? &desc->read : &desc->write; + + /* Only one transaction type possible at a single time */ + if (ptr->async_started) + return -EBUSY; + + if (is_read) { + ret = cb_size(desc, &available_size); + if (ret == -EOVERRUN) { + /* Update read index */ + desc->read.spin_count = desc->write.spin_count - 1; + desc->read.idx = desc->write.idx; + } + + /* We can only read available data */ + requested_size = min(requested_size, available_size); + if (!requested_size) + return -EAGAIN; + } + + /* Size to end of buffer */ + ptr->async_size = min(requested_size, desc->size - ptr->idx); + + *raw_size_available = ptr->async_size; + + /* Convert index to address in the buffer */ + *buff = (void *)(desc->buff + ptr->idx); + + ptr->async_started = true; + + return ret; +} + +/* + * Functionality described at cb_end_async_write/read having the is_read + * parameter to specifiy if it is a read or write operation + */ +static int32_t cb_end_async_operation(struct circular_buffer *desc, + bool is_read) +{ + struct cb_ptr *ptr; + uint32_t new_val; + + + if (!desc) + return -EINVAL; + + /* Select if read or write index will be updated */ + ptr = is_read ? &desc->read : &desc->write; + + /* Transaction not started */ + if (!ptr->async_started) + return FAILURE; + + /* Update pointer value */ + new_val = ptr->idx + ptr->async_size; + if (new_val >= desc->size) { + ptr->spin_count++; + new_val %= desc->size; + } + ptr->idx = new_val; + ptr->async_size = 0; + ptr->async_started = false; + + return SUCCESS; +} + +/* + * Functionality described at cb_write/read having the is_read + * parameter to specifiy if it is a read or write operation + */ +static int32_t cb_operation(struct circular_buffer *desc, + void *data, uint32_t size, + bool is_read) +{ + uint8_t *buff; + uint32_t available_size; + int32_t ret; + uint32_t i; + bool sticky_overrun; + + if (!desc || !data || !size) + return -EINVAL; + + sticky_overrun = 0; + i = 0; + while (i < size) { + do { + ret = cb_prepare_async_operation(desc, size - i, + (void **)&buff, + &available_size, + is_read); + } while (ret == -EBUSY || ret == -EAGAIN); + if (ret == -EOVERRUN) + sticky_overrun = true; + + if (is_read) + memcpy((uint8_t *)data + i, buff, available_size); + else + memcpy(buff, (uint8_t *)data + i, available_size); + + cb_end_async_operation(desc, is_read); + + i += available_size; + } + + if (sticky_overrun) + return -EOVERRUN; + + return SUCCESS; +} + +/** + * @brief Prepare asynchronous write + * + * Get the inside raw buffer to be used in DMA transactions. + * + * @param desc - Circular buffer reference + * @param size_to_write - Number of bytes needed to write to the buffer. + * @param write_buff - Address where to store the buffer where to write to. + * @param size_avilable - min(size_to_write, size until end of allocated buffer) + * @return + * - \ref SUCCESS - No errors + * - -EINVAL - Wrong parameters used + * - -EBUSY - Asynchronous transaction already started + */ +int32_t cb_prepare_async_write(struct circular_buffer *desc, + uint32_t size_to_write, + void **write_buff, + uint32_t *size_avilable) +{ + return cb_prepare_async_operation(desc, size_to_write, write_buff, + size_avilable, 0); +} + +/** + * @brief Prepare asynchronous read + * + * Get the inside raw buffer to be used in DMA transactions. + * + * @param desc - Circular buffer reference + * @param size_to_read - Number of bytes needed to write to the buffer. + * @param read_buff - Address where to store the buffer where data will be read. + * @param size_avilable - min(size_to_read, size until end of allocated buffer) + * @return + * - \ref SUCCESS - No errors + * - -EAGAIN - No data available at this moment + * - -EINVAL - Wrong parameters used + * - -EBUSY - Asynchronous transaction already started + * - -EOVERRUN - An overrun occurred and some data have been overwritten + */ +int32_t cb_prepare_async_read(struct circular_buffer *desc, + uint32_t size_to_read, + void **read_buff, + uint32_t *size_avilable) +{ + return cb_prepare_async_operation(desc, size_to_read, read_buff, + size_avilable, 1); +} + +/** + * \defgroup end_async_group End Ashyncronous functions + * @brief End asynchronous transaction + * + * @param desc - Circular buffer reference + * @return + * - \ref SUCCESS - No errors + * - \ref FAILURE - Asynchronous transaction not started + * - -EINVAL - Wrong parameters used + * @{ + */ +int32_t cb_end_async_write(struct circular_buffer *desc) +{ + return cb_end_async_operation(desc, 0); +} + +int32_t cb_end_async_read(struct circular_buffer *desc) +{ + + return cb_end_async_operation(desc, 1); +} +/** @} */ + +/** + * @brief Write data to the buffer (Blocking) + * @param desc - Circular buffer reference + * @param data - Buffer from where data is copied to the circular buffer + * @param size - Size to write + * @return + * - \ref SUCCESS - No errors + * - -EINVAL - Wrong parameters used + */ +int32_t cb_write(struct circular_buffer *desc, const void *data, uint32_t size) +{ + return cb_operation(desc, (void *)data, size, 0); +} + +/** + * @brief Read data from the buffer (Blocking) + * @param desc - Circular buffer reference + * @param data - Buffer where to data is copied from the circular buffer + * @param size - Size to read + * @return + * - \ref SUCCESS - No errors + * - -EINVAL - Wrong parameters used + * - -EOVERRUN - An overrun occurred and some data have been overwritten + */ +int32_t cb_read(struct circular_buffer *desc, void *data, uint32_t size) +{ + return cb_operation(desc, data, size, 1); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/clk.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,128 @@ +/***************************************************************************//** + * @file clk.c + * @brief Implementation of Clock Driver. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include "error.h" +#include "clk.h" + +/******************************************************************************/ +/************************** Functions Implementation **************************/ +/******************************************************************************/ + +/** + * Start the clock. + * @param clk - The clock structure. + * @return SUCCESS in case of success, negative error code otherwise. + */ +int32_t clk_enable(struct clk * clk) +{ + if (clk->hw->dev_clk_enable) + return clk->hw->dev_clk_enable(clk->hw->dev); + else + return FAILURE; +} + +/** + * Stop the clock. + * @param clk - The clock structure. + * @return SUCCESS in case of success, negative error code otherwise. + */ +int32_t clk_disable(struct clk * clk) +{ + if (clk->hw->dev_clk_disable) + return clk->hw->dev_clk_disable(clk->hw->dev); + else + return FAILURE; +} + +/** + * Get the current frequency of the clock. + * @param clk - The clock structure. + * @param rate - The current frequency. + * @return SUCCESS in case of success, negative error code otherwise. + */ +int32_t clk_recalc_rate(struct clk *clk, + uint64_t *rate) +{ + if (clk->hw->dev_clk_recalc_rate) + return clk->hw->dev_clk_recalc_rate(clk->hw->dev, + clk->hw_ch_num, + rate); + else + return FAILURE; +} + +/** + * Round the desired frequency to a rate that the clock can actually output. + * @param clk - The clock structure. + * @param rate - The desired frequency. + * @param rounded_rate - The rounded frequency. + * @return SUCCESS in case of success, negative error code otherwise. + */ +int32_t clk_round_rate(struct clk *clk, + uint64_t rate, + uint64_t *rounded_rate) +{ + if (clk->hw->dev_clk_round_rate) + return clk->hw->dev_clk_round_rate(clk->hw->dev, + clk->hw_ch_num, + rate, + rounded_rate); + else + return FAILURE; +} + +/** + * Change the frequency of the clock. + * @param clk - The clock structure. + * @param rate - The desired frequency. + * @return SUCCESS in case of success, negative error code otherwise. + */ +int32_t clk_set_rate(struct clk *clk, + uint64_t rate) +{ + if (clk->hw->dev_clk_set_rate) + return clk->hw->dev_clk_set_rate(clk->hw->dev, + clk->hw_ch_num, + rate); + else + return FAILURE; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/crc16.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,102 @@ +/***************************************************************************//** + * @file crc16.c + * @brief Source file of CRC-16 computation. + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#include "crc16.h" + +/***************************************************************************//** + * @brief Creates the CRC-16 lookup table for a given polynomial. + * + * @param table - Pointer to a CRC-16 lookup table to write to. + * @param polynomial - msb-first representation of desired polynomial. + * + * Polynomials in CRC algorithms are typically represented as shown below. + * + * poly = x^16 + x^14 + x^13 + x^12 + x^10 + x^8 + x^6 + x^4 + x^3 + + * x^1 + 1 + * + * Using msb-first direction, x^15 maps to the msb. + * + * msb first: poly = (1)0111010101011011 = 0x755B + * ^ + * + * @return None. +*******************************************************************************/ +void crc16_populate_msb(uint16_t * table, const uint16_t polynomial) +{ + if (!table) + return; + + for (int16_t n = 0; n < CRC16_TABLE_SIZE; n++) { + uint16_t currByte = (uint16_t)(n << 8); + for (uint8_t bit = 0; bit < 8; bit++) { + if ((currByte & 0x8000) != 0) { + currByte <<= 1; + currByte ^= polynomial; + } else { + currByte <<= 1; + } + } + table[n] = currByte; + } +} + +/***************************************************************************//** + * @brief Computes the CRC-16 over a buffer of data. + * + * @param table - Pointer to a CRC-16 lookup table for the desired polynomial. + * @param pdata - Pointer to data buffer. + * @param nbytes - Number of bytes to compute the CRC-16 over. + * @param crc - Initial value for the CRC-16 computation. Can be used to + * cascade calls to this function by providing a previous + * output of this function as the crc parameter. + * + * @return crc - Computed CRC-16 value. +*******************************************************************************/ +uint16_t crc16(const uint16_t * table, const uint8_t *pdata, size_t nbytes, + uint16_t crc) +{ + unsigned int idx; + + while (nbytes--) { + idx = ((crc >> 8) ^ *pdata) & 0xff; + crc = (table[idx] ^ (crc << 8)) & 0xffff; + pdata++; + } + + return crc; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/crc8.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,100 @@ +/***************************************************************************//** + * @file crc8.c + * @brief Source file of CRC-8 computation. + * @author Darius Berghe (darius.berghe@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ +#include "crc8.h" + +/***************************************************************************//** + * @brief Creates the CRC-8 lookup table for a given polynomial. + * + * @param table - Pointer to a CRC-8 lookup table to write to. + * @param polynomial - msb-first representation of desired polynomial. + * + * Polynomials in CRC algorithms are typically represented as shown below. + * + * poly = x^8 + x^2 + x^1 + 1 + * + * Using msb-first direction, x^7 maps to the msb. + * + * msb first: poly = (1)00000111 = 0x07 + * + * @return None. +*******************************************************************************/ +void crc8_populate_msb(uint8_t * table, const uint8_t polynomial) +{ + if (!table) + return; + + for (int16_t n = 0; n < CRC8_TABLE_SIZE; n++) { + uint8_t currByte = (uint8_t)n; + for (uint8_t bit = 0; bit < 8; bit++) { + if ((currByte & 0x80) != 0) { + currByte <<= 1; + currByte ^= polynomial; + } else { + currByte <<= 1; + } + } + table[n] = currByte; + } +} + +/***************************************************************************//** + * @brief Computes the CRC-8 over a buffer of data. + * + * @param table - Pointer to a CRC-8 lookup table for the desired polynomial. + * @param pdata - Pointer to 8-bit data buffer. + * @param nbytes - Number of bytes to compute the CRC-8 over. + * @param crc - Initial value for the CRC-8 computation. Can be used to + * cascade calls to this function by providing a previous + * output of this function as the crc parameter. + * + * @return crc - Computed CRC-8 value. +*******************************************************************************/ +uint8_t crc8(const uint8_t * table, const uint8_t *pdata, size_t nbytes, + uint8_t crc) +{ + unsigned int idx; + + while (nbytes--) { + idx = (crc ^ *pdata); + crc = (table[idx]) & 0xff; + pdata++; + } + + return crc; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/fifo.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,136 @@ +/***************************************************************************//** + * @file fifo.c + * @brief Implementation of fifo. + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <string.h> +#include <stdlib.h> +#include "fifo.h" +#include "error.h" + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * @brief Create new fifo element + * @param buff - Data to be saved in fifo. + * @param len - Length of the data. + * @return fifo element in case of success, NULL otherwise + */ +static struct fifo_element * fifo_new_element(char *buff, uint32_t len) +{ + struct fifo_element *q = calloc(1, sizeof(struct fifo_element)); + if (!q) + return NULL; + + q->len = len; + q->data = calloc(1, len); + if (!(q->data)) { + free(q); + return NULL; + } + memcpy(q->data, buff, len); + + return q; +} + +/** + * @brief Get last element in fifo + * @param p_fifo - pointer to fifo + * @return fifo last element if exists, NULL otherwise + */ +static struct fifo_element *fifo_get_last(struct fifo_element *p_fifo) +{ + if(p_fifo == NULL) + return NULL; + while (p_fifo->next) { + p_fifo = p_fifo->next; + } + + return p_fifo; +} + +/** + * @brief Insert element to fifo, in the last position. + * @param p_fifo - Pointer to fifo. + * @param buff - Data to be saved in fifo. + * @param len - Length of the data. + * @return SUCCESS in case of success, FAILURE otherwise + */ +int32_t fifo_insert(struct fifo_element **p_fifo, char *buff, uint32_t len) +{ + struct fifo_element *p, *q; + + if (len <= 0) + return FAILURE; + + q = fifo_new_element(buff, len); + if (!q) + return FAILURE; + + if (!(*p_fifo)) { + *p_fifo = q; + } else { + p = fifo_get_last(*p_fifo); + p->next = q; + } + + return SUCCESS; +} + +/** + * @brief Remove fifo head + * @param p_fifo - Pointer to fifo. + * @return next element in fifo if exists, NULL otherwise. + */ +struct fifo_element * fifo_remove(struct fifo_element *p_fifo) +{ + struct fifo_element *p = p_fifo; + + if (p_fifo != NULL) { + p_fifo = p_fifo->next; + free(p->data); + free(p); + } + + return p_fifo; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/list.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,842 @@ +/***************************************************************************//** + * @file list.c + * @brief List library implementation + * @author Mihail Chindris (mihail.chindris@analog.com) +******************************************************************************** + * Copyright 2020(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include "list.h" +#include "error.h" +#include <stdlib.h> + +/******************************************************************************/ +/*************************** Types Declarations *******************************/ +/******************************************************************************/ + +/** + * @struct list_elem + * @brief Format of each element of the list + */ +struct list_elem { + /** User data */ + void *data; + /** Reference to previous element */ + struct list_elem *prev; + /** Reference to next element */ + struct list_elem *next; +}; + +/** + * @struct list_iterator + * @brief Structure used to iterate through the list + */ +struct iterator { + /** List reference */ + struct _list_desc *list; + /** Current element reference */ + struct list_elem *elem; +}; + +/** + * @struct _list_desc + * @brief List descriptor + */ +struct _list_desc { + /** Reference to first element in the list */ + struct list_elem *first; + /** Reference to last element in the list*/ + struct list_elem *last; + /** Number of elements in the list */ + uint32_t nb_elements; + /** Function used to compare elements */ + f_cmp comparator; + /** Number of current active iterators */ + uint32_t nb_iterators; + /** Internal list iterator */ + struct iterator l_it; +}; + +/** @brief Default function used to compare element in the list ( \ref f_cmp) */ +static int32_t default_comparator(void *data1, void *data2) +{ + return (int32_t)(data1 - data2); +} + +/** + * @brief Creates a new list elements an configure its value + * @param data - To set list_elem.data + * @param prev - To set list_elem.prev + * @param next - To set list_elem.next + * @return Address of the new element or NULL if allocation fails. + */ +static inline struct list_elem *create_element(void *data, + struct list_elem *prev, + struct list_elem *next) +{ + struct list_elem *elem; + + elem = (struct list_elem *)calloc(1, sizeof(*elem)); + if (!elem) + return NULL; + elem->data = data; + elem->prev = prev; + elem->next = next; + + return (elem); +} + +/** + * @brief Updates the necesary link on the list elements to add or remove one + * @param prev - Low element + * @param elem - Middle element + * @param next - High element + */ +static inline void update_links(struct list_elem *prev, struct list_elem *elem, + struct list_elem *next) +{ + if (prev) + prev->next = elem ? elem : next; + if (elem) { + elem->prev = prev; + elem->next = next; + } + if (next) + next->prev = elem ? elem : prev; +} + +/** + * @brief Update according to modification the list descriptor + * @param list - List reference + * @param new_first - New first element + * @param new_last - New last element + */ +static inline void update_desc(struct _list_desc *list, + struct list_elem *new_first, + struct list_elem *new_last) +{ + if (new_first == list->first) { + list->last = new_last; + if (new_first == NULL || new_last == NULL) + list->first = new_last; + } else { /* if (new_last == list->last) */ + list->first = new_first; + if (new_last == NULL || new_first == NULL) + list->last = new_first; + } +} + +/** + * @brief Set the adapter functions acording to the adapter type + * @param ad - Reference of the adapter + * @param type - Type of the adapter + */ +static inline void set_adapter(struct list_desc *ad, enum adapter_type type) +{ + switch (type) { + case LIST_PRIORITY_LIST: + ad->push = list_add_find; + ad->pop = list_get_first; + ad->top_next = list_read_first; + ad->back = list_read_last; + ad->swap = list_edit_first; + break; + case LIST_QUEUE: + ad->push = list_add_last; + ad->pop = list_get_first; + ad->top_next = list_read_first; + ad->back = list_read_last; + ad->swap = list_edit_first; + break; + case LIST_DEFAULT: + case LIST_STACK: + default: + ad->push = list_add_last; + ad->pop = list_get_last; + ad->top_next = list_read_last; + ad->back = list_read_first; + ad->swap = list_edit_last; + break; + } +} + +/** + * @brief Create a new empty list + * @param list_desc - Where to store the reference of the new created list + * @param type - Type of adapter to use. + * @param comparator - Used to compare item when using an ordered list or when + * using the \em find functions. + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t list_init(struct list_desc **list_desc, enum adapter_type type, + f_cmp comparator) +{ + struct list_desc *l_desc; + struct _list_desc *list; + + + if (!list_desc) + return FAILURE; + l_desc = (struct list_desc *)calloc(1, sizeof(*l_desc)); + if (!l_desc) + return FAILURE; + list = (struct _list_desc *)calloc(1, sizeof(*list)); + if (!list) { + free(l_desc); + return FAILURE; + } + + *list_desc = l_desc; + l_desc->priv_desc = list; + list->comparator = comparator ? comparator : default_comparator; + + /* Configure wrapper */ + set_adapter(l_desc, type); + list->l_it.list = list; + + return SUCCESS; +} + +/** + * @brief Remove the created list. + * + * All its elements will be cleared and the data inside each will be lost. If + * not all iterators have been removed, the list will not be removed. + * @param list_desc - Reference to the list + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t list_remove(struct list_desc *list_desc) +{ + void *data; + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + + list = list_desc->priv_desc; + if (list->nb_iterators != 0) + return FAILURE; + + /* Remove all the elements */ + while (SUCCESS == list_get_first(list_desc, &data)) + ; + free(list_desc->priv_desc); + free(list_desc); + + return SUCCESS; +} + +/** + * @brief Get the number of elements inside the list + * @param list_desc - List reference + * @param out_size - Where to store the number of elements + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t list_get_size(struct list_desc *list_desc, uint32_t *out_size) +{ + struct _list_desc *list; + + if (!list_desc || !out_size) + return FAILURE; + + list = list_desc->priv_desc; + *out_size = list->nb_elements; + + return SUCCESS; +} + +/** @brief Add element at the begining of the list. Refer to \ref f_add */ +int32_t list_add_first(struct list_desc *list_desc, void *data) +{ + struct list_elem *prev; + struct list_elem *next; + struct list_elem *elem; + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + + list = list_desc->priv_desc; + + prev = NULL; + next = list->first; + elem = create_element(data, prev, next); + if (!elem) + return FAILURE; + + update_links(prev, elem, next); + + update_desc(list, elem, list->last); + + list->nb_elements++; + + return SUCCESS; +} + +/** @brief Add element at the end of the list. Refer to \ref f_add */ +int32_t list_add_last(struct list_desc *list_desc, void *data) +{ + struct list_elem *prev; + struct list_elem *next; + struct list_elem *elem; + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + list = list_desc->priv_desc; + + prev = list->last; + next = NULL; + elem = create_element(data, prev, next); + if (!elem) + return FAILURE; + + update_links(prev, elem, next); + + update_desc(list, list->first, elem); + + list->nb_elements++; + + return SUCCESS; +} + +/** @brief Add element at the specified idx. Refer to \ref f_add */ +int32_t list_add_idx(struct list_desc *list_desc, void *data, uint32_t idx) +{ + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + list = list_desc->priv_desc; + + /* If there are no elements the creation of an iterator will fail */ + if (list->nb_elements == 0 || idx == 0) + return list_add_first(list_desc, data); + if (list->nb_elements == idx) + return list_add_last(list_desc, data); + + list->l_it.elem = list->first; + if (SUCCESS != iterator_move(&(list->l_it), idx)) + return FAILURE; + + return iterator_insert(&(list->l_it), data, 0); +} + +/** @brief Add element in ascending order. Refer to \ref f_add */ +int32_t list_add_find(struct list_desc *list_desc, void *data) +{ + struct list_elem *elem; + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + list = list_desc->priv_desc; + + + /* Based on place iterator */ + elem = list->first; + while (elem) { + if (0 < list->comparator(elem->data, data)) + break; + elem = elem->next; + } + if (elem == NULL) { + list->l_it.elem = list->last; + return iterator_insert(&(list->l_it), data, 1); + } else { + list->l_it.elem = elem; + return iterator_insert(&(list->l_it), data, 0); + } + +} + +/** @brief Edit the first element of the list. Refer to \ref f_edit */ +int32_t list_edit_first(struct list_desc *list_desc, void *new_data) +{ + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + + list = list_desc->priv_desc; + list->first->data = new_data; + + return SUCCESS; +} + +/** @brief Edit the last element of the list. Refer to \ref f_edit */ +int32_t list_edit_last(struct list_desc *list_desc, void *new_data) +{ + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + + list = list_desc->priv_desc; + list->last->data = new_data; + + return SUCCESS; +} + +/** @brief Edit the element at the specified idx. Refer to \ref f_edit */ +int32_t list_edit_idx(struct list_desc *list_desc, void *new_data, uint32_t idx) +{ + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + list = list_desc->priv_desc; + + list->l_it.elem = list->first; + if (SUCCESS != iterator_move(&(list->l_it), idx)) + return FAILURE; + + return iterator_edit(&(list->l_it), new_data); +} + +/** @brief Edit the element which match with cmp_data. Refer to \ref f_edit */ +int32_t list_edit_find(struct list_desc *list_desc, void *new_data, + void *cmp_data) +{ + struct _list_desc *list; + + if (!list_desc) + return FAILURE; + list = list_desc->priv_desc; + + list->l_it.elem = list->first; + if (SUCCESS != iterator_find(&(list->l_it), cmp_data)) + return FAILURE; + + return iterator_edit(&(list->l_it), new_data); +} + +/** @brief Read the first element of the list. Refer to \ref f_read */ +int32_t list_read_first(struct list_desc *list_desc, void **data) +{ + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + if (!list->first) + return FAILURE; + + *data = list->first->data; + + return SUCCESS; +} + +/** @brief Read the last element of the list. Refer to \ref f_read */ +int32_t list_read_last(struct list_desc *list_desc, void **data) +{ + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + if (!list->last) + return FAILURE; + + *data = list->last->data; + + return SUCCESS; +} + +/** @brief Read the element at the specified idx. Refer to \ref f_read */ +int32_t list_read_idx(struct list_desc *list_desc, void **data, uint32_t idx) +{ + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + if (!list) + return FAILURE; + + if (idx >= list->nb_elements) + return FAILURE; + + list->l_it.elem = list->first; + if (SUCCESS != iterator_move(&(list->l_it), idx)) + return FAILURE; + + return iterator_read(&(list->l_it), data); +} + +/** @brief Read the element which match with cmp_data. Refer to \ref f_read */ +int32_t list_read_find(struct list_desc *list_desc, void **data, + void *cmp_data) +{ + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + if (!list) + return FAILURE; + + list = list_desc->priv_desc; + list->l_it.elem = list->first; + if (SUCCESS != iterator_find(&(list->l_it), cmp_data)) + return FAILURE; + + return iterator_read(&(list->l_it), data); +} + +/** @brief Read and delete the first element of the list. Refer to \ref f_get */ +int32_t list_get_first(struct list_desc *list_desc, void **data) +{ + struct list_elem *prev; + struct list_elem *next; + struct list_elem *elem; + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + if (!list->nb_elements) + return FAILURE; + + elem = list->first; + prev = elem->prev; + next = elem->next; + + update_links(prev, NULL, next); + update_desc(list, next, list->last); + list->nb_elements--; + + *data = elem->data; + free(elem); + + return SUCCESS; +} + +/** @brief Read and delete the last element of the list. Refer to \ref f_get */ +int32_t list_get_last(struct list_desc *list_desc, void **data) +{ + struct list_elem *prev; + struct list_elem *next; + struct list_elem *elem; + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + if (!list->nb_elements) + return FAILURE; + + elem = list->last; + prev = elem->prev; + next = elem->next; + + update_links(prev, NULL, next); + update_desc(list, list->first, prev); + list->nb_elements--; + + *data = elem->data; + free(elem); + + return SUCCESS; +} + +/** @brief Read and delete the element at idx. Refer to \ref f_get */ +int32_t list_get_idx(struct list_desc *list_desc, void **data, uint32_t idx) +{ + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + list->l_it.elem = list->first; + if (SUCCESS != iterator_move(&(list->l_it), idx)) + return FAILURE; + + return iterator_get(&(list->l_it), data); +} + +/** + * @brief Read and delete the element which match with cmp_data. + * Refer to \ref f_get + */ +int32_t list_get_find(struct list_desc *list_desc, void **data, void *cmp_data) +{ + struct _list_desc *list; + + if (!list_desc || !data) + return FAILURE; + + *data = NULL; + list = list_desc->priv_desc; + list->l_it.elem = list->first; + if (SUCCESS != iterator_find(&(list->l_it), cmp_data)) + return FAILURE; + + return iterator_get(&(list->l_it), data); +} + +/** + * @brief Create a new iterator + * @param iter - Where to store the reference for the new iterator + * @param list_desc - Reference of the list the iterator will be used for + * @param start - If it is true the iterator will be positioned at the first + * element of the list, else it will be positioned at the last. + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t iterator_init(struct iterator **iter, struct list_desc *list_desc, + bool start) +{ + struct iterator *it; + + if (!list_desc) + return FAILURE; + + it = (struct iterator *)calloc(1, sizeof(*it)); + if (!it) + return FAILURE; + it->list = list_desc->priv_desc; + it->list->nb_iterators++; + it->elem = start ? it->list->first : it->list->last; + *iter = it; + + return SUCCESS; +} + +/** + * @brief Remove the created iterator + * @param iter - Reference of the iterator + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t iterator_remove(struct iterator *iter) +{ + struct iterator *it = iter; + + if (!it) + return FAILURE; + + it->list->nb_iterators--; + free(it); + + return SUCCESS; +} + +/** + * @brief Move the position of the iteration through the list. + * + * If the required position is outside the list, the call will fail and the + * iterator will keep its position. + * @param iter - Reference of the iterator + * @param idx - Number of positions to be move. If positive, it will be moved + * forward, otherwise backwords. + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t iterator_move(struct iterator *iter, int32_t idx) +{ + struct iterator *it = iter; + struct list_elem *elem; + int32_t dir = (idx < 0) ? -1 : 1; + + if (!it) + return FAILURE; + + idx = abs(idx); + elem = it->elem; + while (idx > 0 && elem) { + elem = dir > 0 ? elem->next : elem->prev; + idx--; + } + if (!elem) + return FAILURE; + + it->elem = elem; + + return SUCCESS; +} + +/** + * @brief Place the iterator where cmp_data if found. + * @param iter - Reference to the iterator + * @param cmp_data - Data to be found + * @return + * - \ref SUCCESS : On success + * - \ref FAILURE : Otherwise + */ +int32_t iterator_find(struct iterator *iter, void *cmp_data) +{ + struct iterator *it = iter; + struct list_elem *elem; + + if (!it) + return FAILURE; + + elem = it->list->first; + while (elem) { + if (0 == it->list->comparator(elem->data, cmp_data)) { + it->elem = elem; + return SUCCESS; + } + elem = elem->next; + } + + return FAILURE; +} + +/** + * @brief Replace the data at the current position. Refer to \ref f_edit + */ +int32_t iterator_edit(struct iterator *iter, void *new_data) +{ + struct iterator *it = iter; + + if (!it) + return FAILURE; + + it->elem->data = new_data; + + return SUCCESS; +} + +/** + * @brief Read and remove the data at the current position. Refer to \ref f_get. + * + * If the current item is the last one, the iterator will be moved to the + * previous one. + */ +int32_t iterator_get(struct iterator *iter, void **data) +{ + struct iterator *it = iter; + struct list_elem *next; + + + if (!it || !it->elem || !data) + return FAILURE; + + update_links(it->elem->prev, NULL, it->elem->next); + if (it->elem == it->list->first) + update_desc(it->list, it->elem->next, it->list->last); + else if (it->elem == it->list->last) + update_desc(it->list, it->list->first, it->elem->prev); + it->list->nb_elements--; + + *data = it->elem->data; + if (it->elem == it->list->last) + next = it->elem->prev; + else + next = it->elem->next; + free(it->elem); + it->elem = next; + + return SUCCESS; +} + +/** + * @brief Read the data at the current position. Refer to \ref f_read + */ +int32_t iterator_read(struct iterator *iter, void **data) +{ + struct iterator *it = iter; + + if (!it || !it->elem || !data) + return FAILURE; + + *data = it->elem->data; + + return SUCCESS; +} + +/** + * @brief Insert an item in the list. Refer to \ref f_add + * @param iter + * @param data + * @param after - If true, the item will be inserted after the current position. + * Otherwise it will be inserted before. + */ +int32_t iterator_insert(struct iterator *iter, void *data, bool after) +{ + struct iterator *it = iter; + struct list_elem *elem; + struct list_desc list_desc; + + if (!it) + return FAILURE; + + list_desc.priv_desc = iter->list; + if (after && it->elem == it->list->last) + return list_add_last(&list_desc, data); + if (!after && it->elem == it->list->first) + return list_add_first(&list_desc, data); + + if (after) + elem = create_element(data, it->elem, it->elem->next); + else + elem = create_element(data, it->elem->prev, it->elem); + if (!elem) + return FAILURE; + + update_links(elem->prev, elem, elem->next); + + it->list->nb_elements++; + + return SUCCESS; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/util.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,264 @@ +/***************************************************************************//** + * @file util.c + * @brief Implementation of utility functions. + * @author DBogdan (dragos.bogdan@analog.com) +******************************************************************************** + * Copyright 2018(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ +#include <string.h> +#include <stdlib.h> +#include "util.h" +#include "errno.h" +/******************************************************************************/ +/************************** Functions Implementation **************************/ +/******************************************************************************/ + +/** + * Find first set bit in word. + */ +uint32_t find_first_set_bit(uint32_t word) +{ + uint32_t first_set_bit = 0; + + while (word) { + if (word & 0x1) + return first_set_bit; + word >>= 1; + first_set_bit ++; + } + + return 32; +} + +/** + * Find last set bit in word. + */ +uint32_t find_last_set_bit(uint32_t word) +{ + uint32_t bit = 0; + uint32_t last_set_bit = 32; + + while (word) { + if (word & 0x1) + last_set_bit = bit; + word >>= 1; + bit ++; + } + + return last_set_bit; +} + +/** + * Locate the closest element in an array. + */ +uint32_t find_closest(int32_t val, + const int32_t *array, + uint32_t size) +{ + int32_t diff = abs(array[0] - val); + uint32_t ret = 0; + uint32_t i; + + for (i = 1; i < size; i++) { + if (abs(array[i] - val) < diff) { + diff = abs(array[i] - val); + ret = i; + } + } + + return ret; +} + +/** + * Shift the value and apply the specified mask. + */ +uint32_t field_prep(uint32_t mask, uint32_t val) +{ + return (val << find_first_set_bit(mask)) & mask; +} + +/** + * Get a field specified by a mask from a word. + */ +uint32_t field_get(uint32_t mask, uint32_t word) +{ + return (word & mask) >> find_first_set_bit(mask); +} + +/** + * Log base 2 of the given number. + */ +int32_t log_base_2(uint32_t x) +{ + return find_last_set_bit(x); +} + +/** + * Find greatest common divisor of the given two numbers. + */ +uint32_t greatest_common_divisor(uint32_t a, + uint32_t b) +{ + uint32_t div; + uint32_t common_div = 1; + + if ((a == 0) || (b == 0)) + return max(a, b); + + for (div = 1; (div <= a) && (div <= b); div++) + if (!(a % div) && !(b % div)) + common_div = div; + + return common_div; +} + +/** + * Calculate best rational approximation for a given fraction. + */ +void rational_best_approximation(uint32_t given_numerator, + uint32_t given_denominator, + uint32_t max_numerator, + uint32_t max_denominator, + uint32_t *best_numerator, + uint32_t *best_denominator) +{ + uint32_t gcd; + + gcd = greatest_common_divisor(given_numerator, given_denominator); + + *best_numerator = given_numerator / gcd; + *best_denominator = given_denominator / gcd; + + if ((*best_numerator > max_numerator) || + (*best_denominator > max_denominator)) { + *best_numerator = 0; + *best_denominator = 0; + } +} + +/** + * Calculate the number of set bits. + */ +uint32_t hweight8(uint32_t word) +{ + uint32_t count = 0; + + while (word) { + if (word & 0x1) + count++; + word >>= 1; + } + + return count; +} + +/** + * Calculate the quotient and the remainder of an integer division. + */ +uint64_t do_div(uint64_t* n, + uint64_t base) +{ + uint64_t mod = 0; + + mod = *n % base; + *n = *n / base; + + return mod; +} + +/** + * Unsigned 64bit divide with 64bit divisor and remainder + */ +uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor, uint64_t *remainder) +{ + *remainder = dividend % divisor; + + return dividend / divisor; +} + +/** + * Unsigned 64bit divide with 32bit divisor with remainder + */ +uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor, uint32_t *remainder) +{ + *remainder = do_div(÷nd, divisor); + + return dividend; +} + +/** + * Unsigned 64bit divide with 32bit divisor + */ +uint64_t div_u64(uint64_t dividend, uint32_t divisor) +{ + uint32_t remainder; + + return div_u64_rem(dividend, divisor, &remainder); +} + +/** + * Converts from string to int32_t + * @param *str + * @return int32_t + */ +int32_t str_to_int32(const char *str) +{ + char *end; + int32_t value = strtol(str, &end, 0); + + if (end == str) + return -EINVAL; + else + return value; +} + +/** + * Converts from string to uint32_t + * @param *str + * @return uint32_t + */ +uint32_t srt_to_uint32(const char *str) +{ + char *end; + uint32_t value = strtoul(str, &end, 0); + + if (end == str) + return -EINVAL; + else + return value; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/noos_mbed/util/xml.c Thu Jan 14 19:12:57 2021 +0530 @@ -0,0 +1,336 @@ +/***************************************************************************//** + * @file xml.c + * @brief Implementation of xml + * @author Cristian Pop (cristian.pop@analog.com) +******************************************************************************** + * Copyright 2019(c) Analog Devices, Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * - Neither the name of Analog Devices, Inc. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * - The use of this software may or may not infringe the patent rights + * of one or more patent holders. This license does not release you + * from the requirement that you obtain separate licenses from these + * patent holders to use this software. + * - Use of the software either in source or binary form, must be run + * on or directly connected to an Analog Devices Inc. component. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*******************************************************************************/ + +/******************************************************************************/ +/***************************** Include Files **********************************/ +/******************************************************************************/ + +#include <string.h> +#include <stdlib.h> +#include "xml.h" +#include "error.h" + +/******************************************************************************/ +/************************ Functions Definitions *******************************/ +/******************************************************************************/ + +/** + * create attribute + * @param **attribute pointer to new attribute + * @param *name attribute name + * @param *value attribute value + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_create_attribute(struct xml_attribute **attribute, char *name, + const char *value) +{ + if(!attribute) + return FAILURE; + if(!name) + return FAILURE; + if(!value) + return FAILURE; + + *attribute = calloc(1, sizeof(struct xml_attribute)); + if (!(*attribute)) + return FAILURE; + + (*attribute)->name = calloc(1, strlen(name) + 1); + if (!(*attribute)->name) { + free(*attribute); + return FAILURE; + } + strcpy((*attribute)->name, name); + + (*attribute)->value = calloc(1, strlen(value) + 1); + if (!(*attribute)->value) { + free((*attribute)->name); + free(*attribute); + return FAILURE; + } + strcpy((*attribute)->value, value); + + return SUCCESS; +} + +/** + * add attribute to a xml node + * @param *node pointer to the node, where the attribute is inserted + * @param *attribute attribute + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_add_attribute(struct xml_node *node, + struct xml_attribute *attribute) +{ + if(!node) + return FAILURE; + if(!attribute) + return FAILURE; + + if (!node->attributes) { + node->attributes = calloc(1, sizeof(struct xml_attribute*)); + if (!node->attributes) + return FAILURE; + } else { + struct xml_attribute **buff = realloc(node->attributes, + (node->attr_cnt + 1) * sizeof(struct xml_attribute*)); + if (!buff) + return FAILURE; + node->attributes = buff; + } + node->attributes[node->attr_cnt] = attribute; + node->attr_cnt++; + + return SUCCESS; +} + +/** + * create new xml node + * @param **node pointer to the new node + * @param *name + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_create_node(struct xml_node **node, char *name) +{ + if(!node) + return FAILURE; + if(!name) + return FAILURE; + + *node = calloc(1, sizeof(struct xml_node)); + if (!(*node)) + return FAILURE; + (*node)->name = calloc(1, strlen(name) + 1); + if (!(*node)->name) { + free(*node); + return FAILURE; + } + strcpy((*node)->name, name); + + return SUCCESS; +} + +/** + * add child node to a parent node + * @param *node_parent + * @param *node_child + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_add_node(struct xml_node *node_parent, struct xml_node *node_child) +{ + if(!node_parent) + return FAILURE; + if(!node_child) + return FAILURE; + + if (!node_parent->children) { + node_parent->children = calloc(1, sizeof(struct xml_node*)); + if (!node_parent->children) + return FAILURE; + } else { + struct xml_node **buff = realloc(node_parent->children, + (node_parent->children_cnt + 1) * sizeof(struct xml_node*)); + if (!buff) + return FAILURE; + node_parent->children = buff; + } + node_parent->children[node_parent->children_cnt] = node_child; + node_parent->children_cnt++; + + return SUCCESS; +} + +/** + * delete attribute + * @param *attribute + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_delete_attribute(struct xml_attribute *attribute) +{ + free(attribute->name); + free(attribute->value); + free(attribute); + + return SUCCESS; +} + +/** + * delete xml node + * @param *node + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_delete_node(struct xml_node *node) +{ + uint16_t i; + for (i = 0; i < node->attr_cnt; i++) { + xml_delete_attribute(node->attributes[i]); + } + for (i = 0; i < node->children_cnt; i++) { + xml_delete_node(node->children[i]); + } + free(node->name); + free(node->attributes); + free(node->children); + free(node); + + return SUCCESS; +} + +/** + * print string to xml_document. + * @param *doc + * @param *data to be written. + * @return SUCCESS in case of success or negative value otherwise + */ +static ssize_t xml_print_to_doc(struct xml_document *doc, char *data) +{ + uint32_t calc_len, print_len; + + calc_len = strlen(data) + 1; + char *buff = realloc(doc->buff, doc->index + calc_len); + if (!buff) + return FAILURE; + doc->buff = buff; + print_len = sprintf(&(doc->buff[doc->index]), "%s", data); + if(print_len != calc_len - 1) + return FAILURE; + doc->index +=print_len; + + return SUCCESS; +} + +/** + * print xml tree into a xml document + * @param **document + * @param *node pointer to parent node, that contains the xml tree + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_create_document(struct xml_document **document, + struct xml_node *node) +{ + uint16_t i; + ssize_t ret; + struct xml_document *doc; + + if(!document) + return FAILURE; + if(!node) + return FAILURE; + + if (!(*document)) { + *document = calloc(1, sizeof(struct xml_document)); + if (!(*document)) + return FAILURE; + } + doc = *document; + + ret = xml_print_to_doc(doc, "<"); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, node->name); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, " "); + if (ret < 0) + goto error; + + for (i = 0; i < node->attr_cnt; i++) { + ret = xml_print_to_doc(doc, node->attributes[i]->name); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, "=\""); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, node->attributes[i]->value); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, "\" "); + if (ret < 0) + goto error; + } + + if (node->children_cnt == 0) { + ret = xml_print_to_doc(doc, "/>\n"); + if (ret < 0) + goto error; + + return SUCCESS; + } else { + ret = xml_print_to_doc(doc, ">\n"); + if (ret < 0) + goto error; + } + + for (i = 0; i < node->children_cnt; i++) { + ret = xml_create_document(document, node->children[i]); + if (ret < 0) + return ret; + } + + ret = xml_print_to_doc(doc, "</"); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, node->name); + if (ret < 0) + goto error; + ret = xml_print_to_doc(doc, ">\n"); + if (ret < 0) + goto error; + + return SUCCESS; + +error: + free(doc->buff); + free(doc); + + return FAILURE; +} + +/** + * delete xml document + * @param **document pointer to document + * @return SUCCESS in case of success or negative value otherwise + */ +ssize_t xml_delete_document(struct xml_document *document) +{ + free(document->buff); + free(document); + + return SUCCESS; +}