diff --git a/doc/sphinx/source/drivers/adc/ltc2378.rst b/doc/sphinx/source/drivers/adc/ltc2378.rst new file mode 100644 index 00000000000..43c8abd9ca6 --- /dev/null +++ b/doc/sphinx/source/drivers/adc/ltc2378.rst @@ -0,0 +1 @@ +.. include:: ../../../../../drivers/adc/ltc2378/README.rst \ No newline at end of file diff --git a/doc/sphinx/source/projects/adc/ltc2378.rst b/doc/sphinx/source/projects/adc/ltc2378.rst new file mode 100644 index 00000000000..50edac78e5b --- /dev/null +++ b/doc/sphinx/source/projects/adc/ltc2378.rst @@ -0,0 +1 @@ +.. include:: ../../../../../projects/ltc2378/README.rst \ No newline at end of file diff --git a/drivers/adc/ltc2378/README.rst b/drivers/adc/ltc2378/README.rst new file mode 100644 index 00000000000..3388181c27c --- /dev/null +++ b/drivers/adc/ltc2378/README.rst @@ -0,0 +1,230 @@ +LTC2378-20 no-OS driver +======================= + +.. no-os-doxygen:: + +Supported Devices +----------------- + +`LTC2378-20 `_ + +Overview +-------- + +The LTC2378-20 is a high-speed, low-power, and low-noise 20-bit SAR +(Successive Approximation Register) analog-to-digital converter (ADC). It +operates from a 2.5V supply and supports both unipolar (0 to VREF) and +bipolar (±VREF) input ranges, with VREF values ranging from 2.5V to 5.1V. +Despite its high resolution, the device consumes only 21mW and delivers +excellent performance with a maximum ±2ppm INL and a signal-to-noise ratio +(SNR) of 104dB, ensuring no missing codes at 20-bit resolution. + +The ADC features a high-speed SPI-compatible serial interface that supports +multiple logic levels —1.8V, 2.5V, 3.3V, and 5V—and includes a daisy-chain +mode for connecting multiple devices. Its fast 1Msps throughput and zero +cycle latency make it ideal for high-speed applications. An internal +oscillator simplifies timing by setting the conversion time automatically, +and the device powers down between conversions to reduce power consumption, +which scales with the sampling rate. + +The LTC2378-20 supports both unipolar and bipolar input modes. In unipolar +mode, the input range is from 0V to VREF with 20-bit resolution (2^20 codes). +In bipolar mode, the input range is from -VREF to +VREF with effective +19-bit resolution (2^19 codes for each polarity). The device uses a BUSY +signal to indicate conversion completion and a CNV signal to initiate +conversions. + +Applications +------------ + +* Medical Imaging +* High Speed Data Acquisition +* Portable or Compact Instrumentation +* Industrial Process Control +* Low Power Battery-Operated Instrumentation +* Automated Test Equipment (ATE) + +LTC2378-20 Device Configuration +------------------------------- + +Driver Initialization +--------------------- + +In order to be able to use the device, you will have to provide the support +for the communication protocol (SPI) alongside GPIO pins for conversion +control (CNV) and busy indication (BUSY). + +The first API to be called is **ltc2378_init**. Make sure that it returns 0, +which means that the driver was initialized correctly. + +The initialization API uses the device descriptor and an initialization +parameter. The initialization parameter contains the SPI configuration, +GPIO parameters for CNV and BUSY pins, reference voltage (VREF) in microvolts, +and input mode (unipolar or bipolar). These are defined in the header file +of the driver. + +ADC Configuration +----------------- + +The LTC2378-20 can be configured for either unipolar or bipolar input modes +using the **input_mode** parameter during initialization. + +In **unipolar mode** (LTC2378_UNIPOLAR): +- Input range: 0V to VREF +- Resolution: 20 bits (2^20 = 1,048,576 codes) +- Code 0 represents 0V, code 1048575 represents VREF + +In **bipolar mode** (LTC2378_BIPOLAR): +- Input range: -VREF to +VREF +- Resolution: 19 bits effective (2^19 = 524,288 codes per polarity) +- Two's complement format with proper sign extension + +ADC Data Acquisition +-------------------- + +Data acquisition is performed using the **ltc2378_read_raw** API, which: + +1. Initiates a conversion using the CNV signal +2. Waits for the BUSY signal to indicate conversion completion +3. Reads the 20-bit result via SPI +4. Returns the raw digital code + +The **ltc2378_raw_to_uv** API converts the raw digital code to voltage +in microvolts, taking into account the configured input mode and reference +voltage. + +Power Management +---------------- + +The LTC2378-20 automatically powers down between conversions to minimize +power consumption. The **ltc2378_power_down** API can be used to manually +put the device into power-down mode by holding the CNV signal low. + +LTC2378-20 Driver Initialization Example +---------------------------------------- + +.. code-block:: bash + + struct ltc2378_dev *ltc2378_dev; + struct no_os_spi_init_param ltc2378_spi_ip = { + .device_id = SPI_DEVICE_ID, + .max_speed_hz = 1000000, + .chip_select = SPI_CS, + .mode = NO_OS_SPI_MODE_0, + .bit_order = NO_OS_SPI_BIT_ORDER_MSB_FIRST, + .platform_ops = SPI_OPS, + .extra = SPI_EXTRA, + }; + + struct no_os_gpio_init_param ltc2378_gpio_cnv = { + .port = GPIO_CNV_PORT_NUM, + .number = GPIO_CNV_PIN_NUM, + .platform_ops = GPIO_OPS, + .extra = GPIO_EXTRA + }; + + struct no_os_gpio_init_param ltc2378_gpio_busy = { + .port = GPIO_BUSY_PORT_NUM, + .number = GPIO_BUSY_PIN_NUM, + .platform_ops = GPIO_OPS, + .extra = GPIO_EXTRA + }; + + struct ltc2378_init_param ltc2378_ip = { + .spi_init = <c2378_spi_ip, + .gpio_cnv_init = <c2378_gpio_cnv, + .gpio_busy_init = <c2378_gpio_busy, + .vref_uv = 2500000, // 2.5V reference + .input_mode = LTC2378_BIPOLAR + }; + + ret = ltc2378_init(<c2378_dev, <c2378_ip); + if (ret) + goto error; + + /* Read ADC value */ + uint32_t raw_data; + int32_t voltage_uv; + + ret = ltc2378_read_raw(ltc2378_dev, &raw_data); + if (ret) + goto error; + + ret = ltc2378_raw_to_uv(ltc2378_dev, raw_data, &voltage_uv); + if (ret) + goto error; + +LTC2378-20 no-OS IIO support +---------------------------- + +The LTC2378-20 IIO driver comes on top of the LTC2378-20 driver and offers support +for interfacing IIO clients through libiio. + +LTC2378-20 IIO Device Configuration +----------------------------------- + +Voltage Channel Attributes +-------------------------- + +The LTC2378-20 IIO device provides a single voltage input channel with the +following attributes: + +* ``raw`` - The raw digital code from the ADC (0 to 1048575 for unipolar, -524288 to 524287 for bipolar) +* ``scale`` - The scale factor to convert raw values to microvolts +* ``processed`` - The processed voltage value in microvolts (raw * scale) + +The scale factor is automatically calculated based on the configured reference +voltage and input mode: + +- **Unipolar mode**: scale = VREF / 2^20 +- **Bipolar mode**: scale = VREF / 2^19 + +For example, with VREF = 2.5V: +- Unipolar scale = 2500000 µV / 1048576 ≈ 2.384 µV/LSB +- Bipolar scale = 2500000 µV / 524288 ≈ 4.768 µV/LSB + +LTC2378-20 IIO Driver Initialization Example +-------------------------------------------- + +.. code-block:: bash + + int ret; + + struct ltc2378_iio_desc *ltc2378_iio_desc; + struct ltc2378_iio_desc_init_param ltc2378_iio_ip = { + .ltc2378_init_param = <c2378_ip, + }; + + struct iio_app_desc *app; + struct iio_app_init_param app_init_param = { 0 }; + + ret = ltc2378_iio_init(<c2378_iio_desc, <c2378_iio_ip); + if (ret) + goto exit; + + struct iio_app_device iio_devices[] = { + { + .name = "ltc2378-20", + .dev = ltc2378_iio_desc, + .dev_descriptor = ltc2378_iio_desc->iio_dev, + }, + }; + + app_init_param.devices = iio_devices; + app_init_param.nb_devices = NO_OS_ARRAY_SIZE(iio_devices); + app_init_param.uart_init_params = uip; + + ret = iio_app_init(&app, app_init_param); + if (ret) + goto remove_iio_ltc2378; + + ret = iio_app_run(app); + + iio_app_remove(app); + + remove_iio_ltc2378: + ltc2378_iio_remove(ltc2378_iio_desc); + exit: + if (ret) + pr_info("Error!\n"); + return ret; diff --git a/drivers/adc/ltc2378/iio_ltc2378.c b/drivers/adc/ltc2378/iio_ltc2378.c new file mode 100644 index 00000000000..060446c8751 --- /dev/null +++ b/drivers/adc/ltc2378/iio_ltc2378.c @@ -0,0 +1,195 @@ +/***************************************************************************//** + * @file iio_ltc2378.c + * @brief Implementation of iio_ltc2378.c. + * @author Cherrence Sarip (cherrence.sarip@analog.com) +******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 +#include +#include +#include "no_os_alloc.h" +#include "no_os_error.h" +#include "no_os_units.h" +#include "no_os_util.h" +#include "iio.h" +#include "ltc2378.h" +#include "iio_ltc2378.h" + +enum ltc2378_iio_attr { + LTC2378_IIO_RAW, + LTC2378_IIO_SCALE, + LTC2378_IIO_PROCESSED, +}; + +/** + * @brief Read from ADC and provide IIO data + * @param dev - The iio device structure. + * @param buf - Buffer to be filled with requested attr val. + * @param len - Length of the received command buffer in bytes. + * @param channel - Command channel info. + * @param priv - Command attribute id. + * @return 0 in case of success, negative error code otherwise. + */ +static int ltc2378_iio_read(void *dev, char *buf, uint32_t len, + const struct iio_ch_info *channel, intptr_t priv) +{ + struct ltc2378_iio_desc *iio_ltc2378 = dev; + struct ltc2378_dev *ltc2378 = iio_ltc2378->ltc2378_dev; + int ret; + int64_t vals[2]; + uint32_t raw_data; + int32_t voltage_uv; + + if (!dev || !iio_ltc2378->ltc2378_dev) + return -EINVAL; + + switch (priv) { + case LTC2378_IIO_RAW: + ret = ltc2378_read_raw(ltc2378, &raw_data); + if (ret) + return ret; + + vals[0] = raw_data; + vals[1] = 0; + return iio_format_value(buf, len, IIO_VAL_INT, 1, vals); + + case LTC2378_IIO_SCALE: + vals[0] = ltc2378->vref_uv; + if (ltc2378->input_mode == LTC2378_UNIPOLAR) { + vals[1] = LTC2378_IIO_SCALE_UNIPOLAR_SHIFT; + } else { + vals[1] = LTC2378_IIO_SCALE_BIPOLAR_SHIFT; + } + return iio_format_value(buf, len, IIO_VAL_FRACTIONAL_LOG2, 2, vals); + + case LTC2378_IIO_PROCESSED: + ret = ltc2378_read_raw(ltc2378, &raw_data); + if (ret) + return ret; + + ret = ltc2378_raw_to_uv(ltc2378, raw_data, &voltage_uv); + if (ret) + return ret; + + vals[0] = voltage_uv; + vals[1] = 0; + return iio_format_value(buf, len, IIO_VAL_INT, 1, vals); + + default: + return -EINVAL; + } +} + +static struct iio_attribute ltc2378_iio_attrs[] = { + { + .name = "raw", + .show = ltc2378_iio_read, + .store = NULL, + .priv = LTC2378_IIO_RAW, + }, + { + .name = "scale", + .show = ltc2378_iio_read, + .store = NULL, + .priv = LTC2378_IIO_SCALE, + }, + { + .name = "processed", + .show = ltc2378_iio_read, + .store = NULL, + .priv = LTC2378_IIO_PROCESSED, + }, + END_ATTRIBUTES_ARRAY +}; + +static struct iio_channel ltc2378_iio_channels[] = { + { + .name = "voltage0", + .attributes = ltc2378_iio_attrs, + .ch_type = IIO_VOLTAGE, + .indexed = true, + } +}; + +static struct iio_device ltc2378_iio_dev = { + .channels = ltc2378_iio_channels, + .num_ch = NO_OS_ARRAY_SIZE(ltc2378_iio_channels), + .debug_reg_read = NULL, + .debug_reg_write = NULL, +}; + +/** + * @brief Initialize the IIO LTC2378 driver + * @param iio_desc - IIO device descriptor. + * @param init_param - Init parameter structure. + * @return 0 in case of success, negative error code otherwise. + */ +int ltc2378_iio_init(struct ltc2378_iio_desc **iio_desc, + struct ltc2378_iio_desc_init_param *init_param) +{ + struct ltc2378_iio_desc *descriptor; + int ret; + + if (!iio_desc || !init_param || !init_param->ltc2378_init_param) + return -EINVAL; + + descriptor = no_os_calloc(1, sizeof(*descriptor)); + if (!descriptor) + return -ENOMEM; + + ret = ltc2378_init(&descriptor->ltc2378_dev, init_param->ltc2378_init_param); + if (ret) + goto error_free_desc; + + descriptor->iio_dev = <c2378_iio_dev; + + *iio_desc = descriptor; + + return 0; + +error_free_desc: + no_os_free(descriptor); + return ret; +} + +/** + * @brief Free the resources allocated by ltc2378_iio_init() + * @param iio_desc - IIO device descriptor. + * @return 0 in case of success, negative error code otherwise. + */ +int ltc2378_iio_remove(struct ltc2378_iio_desc *iio_desc) +{ + if (!iio_desc) + return -EINVAL; + + ltc2378_remove(iio_desc->ltc2378_dev); + no_os_free(iio_desc); + + return 0; +} diff --git a/drivers/adc/ltc2378/iio_ltc2378.h b/drivers/adc/ltc2378/iio_ltc2378.h new file mode 100644 index 00000000000..9ae5e1463b5 --- /dev/null +++ b/drivers/adc/ltc2378/iio_ltc2378.h @@ -0,0 +1,56 @@ +/***************************************************************************//** + * @file iio_ltc2378.h +* @brief Header file of the LTC2378 IIO Driver + * @author Cherrence Sarip (cherrence.sarip@analog.com) +******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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_LTC2378_H +#define IIO_LTC2378_H + +#include +#include "ltc2378.h" + +struct ltc2378_iio_desc { + struct ltc2378_dev *ltc2378_dev; + struct iio_device *iio_dev; +}; + +struct ltc2378_iio_desc_init_param { + struct ltc2378_init_param *ltc2378_init_param; +}; + +/* Initialize the LTC2378 IIO descriptor */ +int ltc2378_iio_init(struct ltc2378_iio_desc **, + struct ltc2378_iio_desc_init_param *); + +/* Free resources allocated by the IIO initialization function */ +int ltc2378_iio_remove(struct ltc2378_iio_desc *); + +#endif /* IIO_LTC2378_H */ diff --git a/drivers/adc/ltc2378/ltc2378.c b/drivers/adc/ltc2378/ltc2378.c new file mode 100644 index 00000000000..bceea336bd7 --- /dev/null +++ b/drivers/adc/ltc2378/ltc2378.c @@ -0,0 +1,249 @@ +/***************************************************************************//** + * @file ltc2378.c + * @brief Source file of LTC2378 driver. + * @author Cherrence Sarip (cherrence.sarip@analog.com) +******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 "ltc2378.h" +#include +#include "no_os_alloc.h" +#include "no_os_delay.h" +#include "no_os_util.h" + +/** + * @brief Initialize the LTC2378-20 device + * @param device - Pointer to device structure pointer + * @param init_param - Pointer to initialization parameters structure + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_init(struct ltc2378_dev **device, + struct ltc2378_init_param *init_param) +{ + struct ltc2378_dev *dev; + int ret; + + dev = (struct ltc2378_dev *)no_os_malloc(sizeof(*dev)); + if (!dev) + return -ENOMEM; + + dev->input_mode = init_param->input_mode; + dev->vref_uv = init_param->vref_uv; + + ret = no_os_spi_init(&dev->spi_desc, init_param->spi_init); + if (ret) + goto error; + + ret = no_os_gpio_get(&dev->gpio_cnv, init_param->gpio_cnv_init); + if (ret) + goto error_spi; + + ret = no_os_gpio_direction_output(dev->gpio_cnv, NO_OS_GPIO_LOW); + if (ret) + goto error_cnv; + + ret = no_os_gpio_get_optional(&dev->gpio_busy, init_param->gpio_busy_init); + if (ret) + goto error_cnv; + + if (dev->gpio_busy) { + ret = no_os_gpio_direction_input(dev->gpio_busy); + if (ret) + goto error_busy; + } + + *device = dev; + return 0; + +error_busy: + no_os_gpio_remove(dev->gpio_busy); +error_cnv: + no_os_gpio_remove(dev->gpio_cnv); +error_spi: + no_os_spi_remove(dev->spi_desc); +error: + no_os_free(dev); + return ret; +} + +/** + * @brief Remove the LTC2378-20 device and free allocated resources + * @param dev - Device structure pointer + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_remove(struct ltc2378_dev *dev) +{ + if (!dev) + return -EINVAL; + + no_os_spi_remove(dev->spi_desc); + no_os_gpio_remove(dev->gpio_cnv); + if (dev->gpio_busy) + no_os_gpio_remove(dev->gpio_busy); + no_os_free(dev); + + return 0; +} + + +/** + * @brief Start a single ADC conversion + * @param dev - Device structure pointer + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_start_conversion(struct ltc2378_dev *dev) +{ + if (!dev) + return -EINVAL; + + no_os_gpio_set_value(dev->gpio_cnv, 1); + no_os_udelay(LTC2378_CNV_PULSE_WIDTH_US); + no_os_gpio_set_value(dev->gpio_cnv, 0); + + return 0; +} + +/** + * @brief Read a single raw ADC conversion result + * @param dev - Device structure pointer + * @param data - Pointer to store the raw ADC data (20-bit value) + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_read_raw(struct ltc2378_dev *dev, uint32_t *data) +{ + int ret; + uint8_t val; + int timeout; + uint8_t buf[3] = {0}; + + if (!dev || !data) + return -EINVAL; + + ret = ltc2378_start_conversion(dev); + if (ret) + return ret; + + if (dev->gpio_busy) { + timeout = LTC2378_BUSY_TIMEOUT_US; + do { + no_os_gpio_get_value(dev->gpio_busy, &val); + no_os_udelay(1); + } while (val && --timeout); + + if (timeout == 0) + return -ETIMEDOUT; + } else { + no_os_udelay(LTC2378_CONVERSION_DELAY_US); + } + + ret = no_os_spi_write_and_read(dev->spi_desc, buf, sizeof(buf)); + if (ret) + return ret; + + *data = (((uint32_t)buf[0] << LTC2378_DATA_SHIFT_BYTE0) | + ((uint32_t)buf[1] << LTC2378_DATA_SHIFT_BYTE1) | + ((uint32_t)buf[2] >> LTC2378_DATA_SHIFT_BYTE2)) & LTC2378_DATA_MASK; + + return 0; +} + +/** + * @brief Read and average multiple ADC conversion results + * @param dev - Device structure pointer + * @param avg_data - Pointer to store the averaged raw ADC data + * @param samples - Number of samples to average (1-65535) + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_read_avg(struct ltc2378_dev *dev, uint32_t *avg_data, + uint16_t samples) +{ + if (!dev || !avg_data || samples == 0) + return -EINVAL; + + uint64_t sum = 0; + uint32_t raw; + + for (uint16_t i = 0; i < samples; i++) { + int ret = ltc2378_read_raw(dev, &raw); + if (ret) + return ret; + sum += raw; + } + + *avg_data = (uint32_t)(sum / samples); + return 0; +} + +/** + * @brief Convert raw ADC value to voltage in microvolts + * @param dev - Device structure pointer + * @param raw - Raw ADC value (20-bit) + * @param voltage_uv - Pointer to store the voltage in microvolts + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_raw_to_uv(struct ltc2378_dev *dev, uint32_t raw, + int32_t *voltage_uv) +{ + int64_t uv; + int32_t signed_val; + + if (!dev || !voltage_uv) + return -EINVAL; + + if (dev->input_mode == LTC2378_UNIPOLAR) { + /* Unipolar mode: raw value is unsigned (0 to VREF) */ + uv = (int64_t)raw * dev->vref_uv; + uv /= LTC2378_RESOLUTION_UNIPOLAR; + } else if (dev->input_mode == LTC2378_BIPOLAR) { + /* Bipolar mode: sign-extend the 20-bit raw value (-VREF to +VREF) */ + signed_val = no_os_sign_extend32(raw, LTC2378_SIGN_BIT_POSITION); + uv = (int64_t)signed_val * dev->vref_uv; + uv /= LTC2378_RESOLUTION_BIPOLAR; + } else { + return -EINVAL; + } + + *voltage_uv = (int32_t)uv; + return 0; +} + +/** + * @brief Put the LTC2378-20 into power-down mode + * @param dev - Device structure pointer + * @return 0 in case of success, negative error code otherwise + */ +int ltc2378_power_down(struct ltc2378_dev *dev) +{ + if (!dev) + return -EINVAL; + + no_os_gpio_set_value(dev->gpio_cnv, 0); + no_os_udelay(LTC2378_POWERDOWN_DELAY_US); + return 0; +} diff --git a/drivers/adc/ltc2378/ltc2378.h b/drivers/adc/ltc2378/ltc2378.h new file mode 100644 index 00000000000..37ff1546bb2 --- /dev/null +++ b/drivers/adc/ltc2378/ltc2378.h @@ -0,0 +1,122 @@ +/***************************************************************************//** + * @file ltc2378.h +* @brief Header file of the LTC2378 Driver + * @author Cherrence Sarip (cherrence.sarip@analog.com) +******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 __LTC2378_H__ +#define __LTC2378_H__ + +#include +#include "no_os_spi.h" +#include "no_os_gpio.h" +#include "no_os_delay.h" +#include "no_os_error.h" + +#define LTC2378_RESOLUTION_UNIPOLAR (1 << 20) /* 2^20 = 1048576 */ +#define LTC2378_RESOLUTION_BIPOLAR (1 << 19) /* 2^19 = 524288 */ + +/* Timing constants */ +#define LTC2378_BUSY_TIMEOUT_US 1000 /* Timeout for BUSY signal in microseconds */ +#define LTC2378_CNV_PULSE_WIDTH_US 1 /* CNV pulse width in microseconds */ +#define LTC2378_CONVERSION_DELAY_US 1 /* Conversion delay when BUSY not available */ +#define LTC2378_POWERDOWN_DELAY_US 2 /* Power-down setup time in microseconds */ + +/* IIO scale calculation constants */ +#define LTC2378_IIO_SCALE_UNIPOLAR_SHIFT 20 /* 2^20 for unipolar mode */ +#define LTC2378_IIO_SCALE_BIPOLAR_SHIFT 19 /* 2^19 for bipolar mode */ + +/* SPI data parsing constants */ +#define LTC2378_DATA_MASK 0xFFFFF /* 20-bit data mask */ +#define LTC2378_DATA_SHIFT_BYTE0 12 /* Shift for byte 0 */ +#define LTC2378_DATA_SHIFT_BYTE1 4 /* Shift for byte 1 */ +#define LTC2378_DATA_SHIFT_BYTE2 4 /* Right shift for byte 2 */ +#define LTC2378_SIGN_BIT_POSITION 19 /* Sign bit position for bipolar mode */ + +/** + * @enum ltc2378_input_mode + * @brief Input mode configuration for LTC2378-20 + */ +enum ltc2378_input_mode { + LTC2378_BIPOLAR, /* Bipolar input: -Vref to +Vref */ + LTC2378_UNIPOLAR /* Unipolar input: 0 to Vref */ +}; + +/** + * @struct ltc2378_dev + * @brief Device descriptor for LTC2378-20 + */ +struct ltc2378_dev { + struct no_os_spi_desc *spi_desc; + struct no_os_gpio_desc *gpio_cnv; + struct no_os_gpio_desc *gpio_busy; + uint32_t vref_uv; + enum ltc2378_input_mode input_mode; +}; + +/** + * @struct ltc2378_init_param + * @brief Initialization parameters for LTC2378-20 + */ +struct ltc2378_init_param { + const struct no_os_spi_init_param *spi_init; + const struct no_os_gpio_init_param *gpio_cnv_init; + const struct no_os_gpio_init_param *gpio_busy_init; + uint32_t vref_uv; + enum ltc2378_input_mode input_mode; +}; + +/* Function declarations */ + +/* Initialize the LTC2378-20 device */ +int ltc2378_init(struct ltc2378_dev **device, + struct ltc2378_init_param *init_param); + +/* Remove the LTC2378-20 device and free allocated resources */ +int ltc2378_remove(struct ltc2378_dev *dev); + +/* Start a single ADC conversion */ +int ltc2378_start_conversion(struct ltc2378_dev *dev); + +/* Read a single raw ADC conversion result */ +int ltc2378_read_raw(struct ltc2378_dev *dev, uint32_t *data); + +/* Read and average multiple ADC conversion results */ +int ltc2378_read_avg(struct ltc2378_dev *dev, uint32_t *avg_data, + uint16_t samples); + +/* Convert raw ADC value to voltage in microvolts */ +int ltc2378_raw_to_uv(struct ltc2378_dev *dev, uint32_t raw, + int32_t *voltage_uv); + +/* Put the LTC2378-20 into power-down mode */ +int ltc2378_power_down(struct ltc2378_dev *dev); + +#endif /* __LTC2378_H__ */ diff --git a/projects/ltc2378/Makefile b/projects/ltc2378/Makefile new file mode 100644 index 00000000000..b5686d677dd --- /dev/null +++ b/projects/ltc2378/Makefile @@ -0,0 +1,9 @@ +EXAMPLE ?= basic + +include ../../tools/scripts/generic_variables.mk + +include ../../tools/scripts/examples.mk + +include src.mk + +include ../../tools/scripts/generic.mk diff --git a/projects/ltc2378/README.rst b/projects/ltc2378/README.rst new file mode 100644 index 00000000000..150259fe68e --- /dev/null +++ b/projects/ltc2378/README.rst @@ -0,0 +1,162 @@ +Evaluating the LTC2378-20 +========================= + +.. no-os-doxygen:: + +.. contents:: + :depth: 3 + +Supported Evaluation Boards +--------------------------- + +`DC2135A `_ + +Overview +-------- + +The DC2135A evaluation board features the LTC2378-20, a high-speed 20-bit SAR +ADC that can be configured for both unipolar (0 to VREF) and bipolar (±VREF) +input ranges. The board provides all necessary hardware connections and signal +conditioning for comprehensive ADC evaluation. + +The evaluation board provides easy access to all necessary signals through +standard connectors, enabling quick setup for high-speed data acquisition +applications up to 1Msps sample rate. + +For full performance details, refer to the LTC2378-20 data sheet, which should +be consulted in conjunction with the user guide. + +Hardware Specifications +----------------------- + +Power Supply Requirements +^^^^^^^^^^^^^^^^^^^^^^^^^ + +The DC2135A evaluation board requires external ±16V supplies for the analog +input circuitry and signal conditioning. The LTC2378-20 ADC itself operates +from a regulated 2.5V supply that is generated on-board. + +**Pin Description** + + Please see the following table for the pin assignments: + + +----------+-------------------------------------------+ + | Name | Description | + +----------+-------------------------------------------+ + | GND | Connect to Ground | + +----------+-------------------------------------------+ + | SCK | Connect to SPI Clock (SCK) | + +----------+-------------------------------------------+ + | SDO | Connect to SPI Master In Slave Out (MISO) | + +----------+-------------------------------------------+ + | SDI | Connect to SPI Master Out Slave In (MOSI) | + +----------+-------------------------------------------+ + | CNV | Connect to GPIO pin (CNV) | + +----------+-------------------------------------------+ + | BUSY | Connect to GPIO pin (BUSY) | + +----------+-------------------------------------------+ + +**Hardware Bringup** + +For reference, consult the Quick Start Procedure section in the user guide for the corresponding demo board: +`DC2135A user guide `_. + +No-OS Build Setup +----------------- + +Please see: https://wiki.analog.com/resources/no-os/build + +No-OS Supported Examples +------------------------ + +The initialization data used in the examples is taken out from: +`Project Common Data Path `_ + +The macros used in Common Data are defined in platform specific files found in: +`Project Platform Configuration Path `_ + +Basic example +^^^^^^^^^^^^^ + +This is a simple example that initializes the LTC2378-20, perform the start-up +sequence and configure the output voltage. + +In order to build the basic example make sure you have the following +configuration in the +`Makefile `_ + +.. code-block:: bash + + # Select the example you want by choosing basic or iio_example + EXAMPLE ?= basic + EXAMPLE ?= iio_example + +IIO example +^^^^^^^^^^^ + +This project is actually an IIOD demo for DC2135A evaluation board. +The project launches a IIOD server on the board so that the user may connect +to it via an IIO client. + +Using IIO-Oscilloscope, the user can configure the device. + +If you are not familiar with ADI IIO Application, please take a look at: +`IIO No-OS `_ + +If you are not familiar with ADI IIO-Oscilloscope Client, please take a look at: +`IIO Oscilloscope `_ + +The No-OS IIO Application together with the No-OS IIO LTC2378-20 driver take care of +all the back-end logic needed to setup the IIO server. + +This example initializes the IIO device and calls the IIO app as shown in: +`IIO Example `_ + +In order to build the IIO project make sure you have the following configuration +in the +`Makefile `_ + +.. code-block:: bash + + # Select the example you want by choosing basic or iio_example + EXAMPLE ?= basic + EXAMPLE ?= iio_example + +No-OS Supported Platforms +------------------------- + +Maxim Platform +^^^^^^^^^^^^^^ + +**Used hardware** + +* `DC2135A `_ +* `MAX32666FTHR `_ + +**Connections**: + + ++--------------------------+----------------------------------------------+------------------+ +| DC2135A Pin | Function | MAX32666FTHR Pin | ++--------------------------+----------------------------------------------+------------------+ +| SCK | SPI Clock (SCK) | AIN3 (SPI1_SCK) | ++--------------------------+----------------------------------------------+------------------+ +| SDO | SPI Master In Slave Out (MISO) | AIN2 (SPI1_MISO) | ++--------------------------+----------------------------------------------+------------------+ +| SDI | SPI Master Out Slave In (MOSI) | AIN1 (SPI1_MOSI) | ++--------------------------+----------------------------------------------+------------------+ +| CNV | GPIO (CNV Pin) | P0_9 | ++--------------------------+----------------------------------------------+------------------+ +| BUSY | GPIO (BUSY Pin) | P0_10 | ++--------------------------+----------------------------------------------+------------------+ +| GND | Ground (GND) | GND | ++--------------------------+----------------------------------------------+------------------+ + +**Build Command** + +.. code-block:: bash + + # to delete current build + make PLATFORM=maxim TARGET=max32665 reset + # to build the project and flash the code + make PLATFORM=maxim TARGET=max32665 run diff --git a/projects/ltc2378/builds.json b/projects/ltc2378/builds.json new file mode 100644 index 00000000000..4f718013cec --- /dev/null +++ b/projects/ltc2378/builds.json @@ -0,0 +1,10 @@ +{ + "maxim": { + "basic_example": { + "flags": "EXAMPLE=basic TARGET=max32665" + }, + "iio": { + "flags": "EXAMPLE=iio_example TARGET=max32665" + } + } +} diff --git a/projects/ltc2378/src.mk b/projects/ltc2378/src.mk new file mode 100644 index 00000000000..7f530c01804 --- /dev/null +++ b/projects/ltc2378/src.mk @@ -0,0 +1,28 @@ +INCS += $(INCLUDE)/no_os_delay.h \ + $(INCLUDE)/no_os_error.h \ + $(INCLUDE)/no_os_gpio.h \ + $(INCLUDE)/no_os_print_log.h \ + $(INCLUDE)/no_os_spi.h \ + $(INCLUDE)/no_os_alloc.h \ + $(INCLUDE)/no_os_irq.h \ + $(INCLUDE)/no_os_list.h \ + $(INCLUDE)/no_os_dma.h \ + $(INCLUDE)/no_os_uart.h \ + $(INCLUDE)/no_os_lf256fifo.h \ + $(INCLUDE)/no_os_util.h \ + $(INCLUDE)/no_os_units.h \ + $(INCLUDE)/no_os_mutex.h + +SRCS += $(DRIVERS)/api/no_os_gpio.c \ + $(NO-OS)/util/no_os_lf256fifo.c \ + $(DRIVERS)/api/no_os_irq.c \ + $(DRIVERS)/api/no_os_spi.c \ + $(DRIVERS)/api/no_os_uart.c \ + $(DRIVERS)/api/no_os_dma.c \ + $(NO-OS)/util/no_os_list.c \ + $(NO-OS)/util/no_os_util.c \ + $(NO-OS)/util/no_os_alloc.c \ + $(NO-OS)/util/no_os_mutex.c + +INCS += $(DRIVERS)/adc/ltc2378/ltc2378.h +SRCS += $(DRIVERS)/adc/ltc2378/ltc2378.c diff --git a/projects/ltc2378/src/common/common_data.c b/projects/ltc2378/src/common/common_data.c new file mode 100644 index 00000000000..9f3254fb1e3 --- /dev/null +++ b/projects/ltc2378/src/common/common_data.c @@ -0,0 +1,81 @@ +/******************************************************************************* + * @file common_data.c + * @brief Defines common data to be used by ltc2378 examples. + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 "common_data.h" +#include +#include "parameters.h" + +struct no_os_uart_init_param uip = { + .device_id = UART_DEVICE_ID, + .irq_id = UART_IRQ_ID, + .asynchronous_rx = true, + .baud_rate = UART_BAUDRATE, + .size = NO_OS_UART_CS_8, + .parity = NO_OS_UART_PAR_NO, + .stop = NO_OS_UART_STOP_1_BIT, + .platform_ops = UART_OPS, + .extra = UART_EXTRA, +}; + +const struct no_os_spi_init_param ltc2378_spi_ip = { + .device_id = SPI_DEVICE_ID, + .max_speed_hz = SPI_MAX_SPEED, + .chip_select = SPI_CS, + .mode = NO_OS_SPI_MODE_0, + .bit_order = NO_OS_SPI_BIT_ORDER_MSB_FIRST, + .platform_ops = SPI_OPS, + .extra = SPI_EXTRA, + .parent = NULL, +}; + +const struct no_os_gpio_init_param ltc2378_gpio_cnv = { + .port = GPIO_CNV_PORT_NUM, + .number = GPIO_CNV_PIN_NUM, + .platform_ops = GPIO_OPS, + .extra = GPIO_EXTRA +}; + +const struct no_os_gpio_init_param ltc2378_gpio_busy = { + .port = GPIO_BUSY_PORT_NUM, + .number = GPIO_BUSY_PIN_NUM, + .platform_ops = GPIO_OPS, + .extra = GPIO_EXTRA +}; + +const struct ltc2378_init_param ltc2378_ip = { + .spi_init = <c2378_spi_ip, + .gpio_cnv_init = <c2378_gpio_cnv, + .gpio_busy_init = <c2378_gpio_busy, + .vref_uv = LTC2378_DEFAULT_VREF_UV, + .input_mode = LTC2378_UNIPOLAR +}; diff --git a/projects/ltc2378/src/common/common_data.h b/projects/ltc2378/src/common/common_data.h new file mode 100644 index 00000000000..e7b5d9e7533 --- /dev/null +++ b/projects/ltc2378/src/common/common_data.h @@ -0,0 +1,48 @@ +/******************************************************************************* + * @file common_data.h + * @brief Defines common data to be used by ltc2378 examples. + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 __COMMON_DATA_H__ +#define __COMMON_DATA_H__ + +#include "parameters.h" +#include "ltc2378.h" +#include "no_os_spi.h" +#include "iio_ltc2378.h" + +extern struct no_os_uart_init_param uip; + +extern const struct no_os_spi_init_param ltc2378_spi_ip; +extern const struct no_os_gpio_init_param ltc2378_gpio_cnv; +extern const struct no_os_gpio_init_param ltc2378_gpio_busy; +extern const struct ltc2378_init_param ltc2378_ip; + +#endif /* __COMMON_DATA_H__ */ diff --git a/projects/ltc2378/src/examples/basic/basic_example.c b/projects/ltc2378/src/examples/basic/basic_example.c new file mode 100644 index 00000000000..656341ef990 --- /dev/null +++ b/projects/ltc2378/src/examples/basic/basic_example.c @@ -0,0 +1,100 @@ +/******************************************************************************* + * @file basic_example.c + * @brief Basic example code for ltc2378 project + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 "no_os_delay.h" +#include "no_os_print_log.h" +#include "no_os_spi.h" +#include "no_os_util.h" +#include "no_os_error.h" +#include "common_data.h" +#include "ltc2378.h" +#include "parameters.h" +#include +#include "no_os_gpio.h" +#include "maxim_gpio.h" + +/***************************************************************************** + * @brief Basic example main execution. + * + * @return ret - Result of the example execution. If working correctly, will + * execute continuously the while(1) loop and will not return. + *******************************************************************************/ + +int example_main() +{ + struct ltc2378_dev *dev; + int ret; + + pr_info("Enter basic example \n"); + + struct no_os_uart_desc *uart_desc; + + ret = no_os_uart_init(&uart_desc, &uip); + if (ret) + return ret; + + no_os_uart_stdio(uart_desc); + + ret = ltc2378_init(&dev, <c2378_ip); + if (ret) { + pr_info("Init failed: %d\n", ret); + return ret; + } + + pr_info("VREF: %lu uV, Mode: %s\n", + dev->vref_uv, + (dev->input_mode == LTC2378_UNIPOLAR) ? "Unipolar" : "Bipolar"); + + while (1) { + uint32_t raw; + int32_t voltage_uv; + + ret = ltc2378_read_raw(dev, &raw); + if (ret) { + pr_info("Read failed: %d\n", ret); + continue; + } + + ret = ltc2378_raw_to_uv(dev, raw, &voltage_uv); + if (ret) { + pr_info("Convert failed: %d\n", ret); + continue; + } + + pr_info("Raw: %lu, Voltage: %ld uV\n", raw, voltage_uv); + + no_os_mdelay(BASIC_EXAMPLE_DELAY_MS); + } + + return 0; +} diff --git a/projects/ltc2378/src/examples/iio_example/iio_example.c b/projects/ltc2378/src/examples/iio_example/iio_example.c new file mode 100644 index 00000000000..2a9286c63dc --- /dev/null +++ b/projects/ltc2378/src/examples/iio_example/iio_example.c @@ -0,0 +1,96 @@ +/******************************************************************************** + * @file iio_example.c + * @brief IIO example code for the ltc2378 project + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 +#include +#include +#include "no_os_alloc.h" +#include "no_os_error.h" +#include "no_os_units.h" +#include "no_os_util.h" +#include "no_os_print_log.h" + +#include "ltc2378.h" +#include "iio_ltc2378.h" +#include "iio_app.h" +#include "common_data.h" + +/******************************************************************************* + * @brief IIO example main execution. + * + * @return ret - Result of the example execution. If working correctly, will + * execute continuously function iio_app_run and will not return. + *******************************************************************************/ +int example_main() +{ + int ret; + + struct ltc2378_iio_desc *ltc2378_iio_desc; + struct ltc2378_iio_desc_init_param ltc2378_iio_ip = { + .ltc2378_init_param = <c2378_ip, + }; + + struct iio_app_desc *app; + struct iio_app_init_param app_init_param = { 0 }; + + ret = ltc2378_iio_init(<c2378_iio_desc, <c2378_iio_ip); + if (ret) + goto exit; + + struct iio_app_device iio_devices[] = { + { + .name = "ltc2378-20", + .dev = ltc2378_iio_desc, + .dev_descriptor = ltc2378_iio_desc->iio_dev, + }, + }; + + app_init_param.devices = iio_devices; + app_init_param.nb_devices = NO_OS_ARRAY_SIZE(iio_devices); + app_init_param.uart_init_params = uip; + + ret = iio_app_init(&app, app_init_param); + if (ret) + goto remove_iio_ltc2378; + + ret = iio_app_run(app); + + iio_app_remove(app); + +remove_iio_ltc2378: + ltc2378_iio_remove(ltc2378_iio_desc); +exit: + if (ret) + pr_info("Error!\n"); + return ret; +} diff --git a/projects/ltc2378/src/examples/iio_example/iio_example.mk b/projects/ltc2378/src/examples/iio_example/iio_example.mk new file mode 100644 index 00000000000..e987a6702b1 --- /dev/null +++ b/projects/ltc2378/src/examples/iio_example/iio_example.mk @@ -0,0 +1,3 @@ +IIOD = y +INCS += $(DRIVERS)/adc/ltc2378/iio_ltc2378.h +SRCS += $(DRIVERS)/adc/ltc2378/iio_ltc2378.c diff --git a/projects/ltc2378/src/platform/maxim/main.c b/projects/ltc2378/src/platform/maxim/main.c new file mode 100644 index 00000000000..2e270dfca2a --- /dev/null +++ b/projects/ltc2378/src/platform/maxim/main.c @@ -0,0 +1,63 @@ +/******************************************************************************** + * @file main.c + * @brief Main file for Maxim platform of ltc2378 project. + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 "parameters.h" +#include "common_data.h" +#include "no_os_error.h" + +extern int example_main(); + +/******************************************************************************* + * @brief Main function execution for LTC2378 platform. + * + * @return ret - Result of the enabled examples execution. + *******************************************************************************/ +int main() +{ + int ret; + struct no_os_uart_desc *uart; + + ret = no_os_uart_init(&uart, &uip); + if (ret) + goto error; + + no_os_uart_stdio(uart); + ret = example_main(); + if (ret) + goto error; + +error: + no_os_uart_remove(uart); + + return 0; +} diff --git a/projects/ltc2378/src/platform/maxim/parameters.c b/projects/ltc2378/src/platform/maxim/parameters.c new file mode 100644 index 00000000000..715423d34de --- /dev/null +++ b/projects/ltc2378/src/platform/maxim/parameters.c @@ -0,0 +1,47 @@ +/******************************************************************************** + * @file parameters.c + * @brief Definition of maxim platform data used by ltc2378 project. + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 "parameters.h" + +struct max_uart_init_param max_uart_extra = { + .flow = MAX_UART_FLOW_DIS, +}; + +struct max_spi_init_param max_spi_extra = { + .num_slaves = 1, + .polarity = SPI_SS_POL_LOW, + .vssel = MXC_GPIO_VSSEL_VDDIOH, +}; + +struct max_gpio_init_param max_gpio_extra = { + .vssel = MXC_GPIO_VSSEL_VDDIOH +}; diff --git a/projects/ltc2378/src/platform/maxim/parameters.h b/projects/ltc2378/src/platform/maxim/parameters.h new file mode 100644 index 00000000000..7178772a6aa --- /dev/null +++ b/projects/ltc2378/src/platform/maxim/parameters.h @@ -0,0 +1,72 @@ +/******************************************************************************** + * @brief Definitions specific to Maxim platform used by ltc2378 project. + * @author Cherrence Sarip (cherrence.sarip@analog.com) + ******************************************************************************** + * Copyright 2025(c) Analog Devices, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. 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. + * + * 3. 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. + * + * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. “AS IS” AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, 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 __PARAMETERS_H__ +#define __PARAMETERS_H__ + +#include "maxim_uart.h" +#include "maxim_uart_stdio.h" +#include "maxim_spi.h" +#include "maxim_gpio.h" + +#ifdef IIO_SUPPORT +#define INTC_DEVICE_ID 0 +#endif + +#define UART_DEVICE_ID 1 +#define UART_IRQ_ID UART1_IRQn +#define UART_BAUDRATE 115200 +#define UART_OPS &max_uart_ops +#define UART_EXTRA &max_uart_extra + +#define SPI_DEVICE_ID 1 +#define SPI_CS 0 + +#define SPI_MAX_SPEED 1000000 /* 1 MHz SPI clock */ +#define SPI_OPS &max_spi_ops +#define SPI_EXTRA &max_spi_extra + +/* Hardware configuration constants */ +#define LTC2378_DEFAULT_VREF_UV 2500000 /* 2.5V reference voltage in microvolts */ +#define BASIC_EXAMPLE_DELAY_MS 500 /* Delay between measurements in basic example */ + +#define GPIO_CNV_PORT_NUM 0 +#define GPIO_CNV_PIN_NUM 9 +#define GPIO_BUSY_PORT_NUM 0 +#define GPIO_BUSY_PIN_NUM 10 +#define GPIO_OPS &max_gpio_ops +#define GPIO_EXTRA &max_gpio_extra + +extern struct max_uart_init_param max_uart_extra; +extern struct max_spi_init_param max_spi_extra; +extern struct max_gpio_init_param max_gpio_extra; + +#endif /* __PARAMETERS_H__ */ diff --git a/projects/ltc2378/src/platform/maxim/platform_src.mk b/projects/ltc2378/src/platform/maxim/platform_src.mk new file mode 100644 index 00000000000..784dcf675ca --- /dev/null +++ b/projects/ltc2378/src/platform/maxim/platform_src.mk @@ -0,0 +1,14 @@ +INCS += $(PLATFORM_DRIVERS)/maxim_gpio.h \ + $(PLATFORM_DRIVERS)/maxim_spi.h \ + $(PLATFORM_DRIVERS)/../common/maxim_dma.h \ + $(PLATFORM_DRIVERS)/maxim_irq.h \ + $(PLATFORM_DRIVERS)/maxim_uart.h \ + $(PLATFORM_DRIVERS)/maxim_uart_stdio.h + +SRCS += $(PLATFORM_DRIVERS)/maxim_delay.c \ + $(PLATFORM_DRIVERS)/maxim_gpio.c \ + $(PLATFORM_DRIVERS)/maxim_spi.c \ + $(PLATFORM_DRIVERS)/../common/maxim_dma.c \ + $(PLATFORM_DRIVERS)/maxim_irq.c \ + $(PLATFORM_DRIVERS)/maxim_uart.c \ + $(PLATFORM_DRIVERS)/maxim_uart_stdio.c