From 196bcda1feff6256d2ed59e23db1bd86a46e5cd7 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Thu, 17 Jul 2025 14:08:22 +0200 Subject: [PATCH 01/16] feat: initial driver with init() --- drivers/ads1115/Makefile | 1 + drivers/ads1115/Makefile.dep | 1 + drivers/ads1115/Makefile.include | 2 + drivers/ads1115/ads1115.c | 102 +++++++++++ drivers/ads1115/include/ads1115_internal.h | 56 ++++++ drivers/ads1115/include/ads1115_params.h | 88 +++++++++ drivers/include/ads1115.h | 204 +++++++++++++++++++++ 7 files changed, 454 insertions(+) create mode 100644 drivers/ads1115/Makefile create mode 100644 drivers/ads1115/Makefile.dep create mode 100644 drivers/ads1115/Makefile.include create mode 100644 drivers/ads1115/ads1115.c create mode 100644 drivers/ads1115/include/ads1115_internal.h create mode 100644 drivers/ads1115/include/ads1115_params.h create mode 100644 drivers/include/ads1115.h diff --git a/drivers/ads1115/Makefile b/drivers/ads1115/Makefile new file mode 100644 index 000000000000..48422e909a47 --- /dev/null +++ b/drivers/ads1115/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/ads1115/Makefile.dep b/drivers/ads1115/Makefile.dep new file mode 100644 index 000000000000..e67057d4639d --- /dev/null +++ b/drivers/ads1115/Makefile.dep @@ -0,0 +1 @@ +FEATURES_REQUIRED += periph_i2c diff --git a/drivers/ads1115/Makefile.include b/drivers/ads1115/Makefile.include new file mode 100644 index 000000000000..d2d705c6a57a --- /dev/null +++ b/drivers/ads1115/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE_INCLUDES_ads1115 := $(LAST_MAKEFILEDIR)/include +USEMODULE_INCLUDES += $(USEMODULE_INCLUDES_ads1115) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c new file mode 100644 index 000000000000..f170e3699b6b --- /dev/null +++ b/drivers/ads1115/ads1115.c @@ -0,0 +1,102 @@ + +/* + * Copyright (C) Baptiste Le Duc + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_ads1115 + * @{ + * @file + * @brief ADS1115 Analog-to-digital converter driver + * @author Baptiste Le Duc + * @} + */ + +#include + +#include "ads1115.h" +#include "ads1115_params.h" +#include "ads1115_internal.h" + +#include "periph/i2c.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +#define DEV (dev->params.i2c) +#define ADDR (dev->params.addr) + +#define ADS1115_CONF_TEST_VALUE (1) + + +/** + * @brief Builds the configuration register value from the parameters + * + * @param[in] params ADS1115 parameters + * + * @return Configuration register value + */ +static uint16_t _build_config_reg(const ads1115_params_t *params) +{ + uint16_t conf = 0; + + conf |= (params->mux << ADS1115_CONF_MUX_BIT); + conf |= (params->pga << ADS1115_CONF_PGA_BIT); + conf |= (params->mode << ADS1115_CONF_MODE_BIT); + conf |= (params->dr << ADS1115_CONF_DR_BIT); + conf |= (params->comp_mode << ADS1115_CONF_COMP_MODE_BIT); + conf |= (params->comp_polarity << ADS1115_CONF_COMP_POLARITY_BIT); + conf |= (params->comp_latch << ADS1115_CONF_COMP_LATCH_BIT); + conf |= (params->comp_queue << ADS1115_CONF_COMP_QUEUE_BIT); + + return conf; +} + +int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) +{ + assert(dev && params); + + dev->params = *params; + DEBUG("[ads1115] init - i2c=%d, addr=0x%02x\n", dev->params.i2c, dev->params.addr); + + int res = ADS1115_NOI2C; + + i2c_acquire(DEV); + + // Test communication + uint8_t test_conf[2] = {0x00, ADS1115_CONF_TEST_VALUE}; + if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, test_conf, 2, 0) < 0) { + DEBUG("[ads1115] init - error: write test failed\n"); + res = ADS1115_NODEV; + } + + uint8_t reg[2]; + if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, reg, 2, 0) < 0 || + (((uint16_t)reg[0] << 8) | reg[1]) != ADS1115_CONF_TEST_VALUE) { + DEBUG("[ads1115] init - error: read test failed (reg=%02x%02x)\n", reg[0], reg[1]); + res = ADS1115_NOI2C; + goto release; + } + + // Apply actual configuration + uint16_t conf = _build_config_reg(&dev->params); + uint8_t conf_bytes[2] = { conf >> 8, conf & 0xFF }; + + if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, conf_bytes, 2, 0) < 0) { + DEBUG("[ads1115] init - error: setting config failed\n"); + res = ADS1115_NOI2C; + goto release; + } + + res = ADS1115_OK; + +release: + i2c_release(DEV); + return res; +} + + diff --git a/drivers/ads1115/include/ads1115_internal.h b/drivers/ads1115/include/ads1115_internal.h new file mode 100644 index 000000000000..0a1debfb988e --- /dev/null +++ b/drivers/ads1115/include/ads1115_internal.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) Baptiste Le Duc + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#pragma once + +/** + * @ingroup drivers_ads1115 + * @{ + * + * @file + * @brief Internal definitions for ADS1115 Analog-to-digital converter + * + * @author Baptiste Le Duc + * @} + */ + + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @name ADS1115 registers + * @{ + */ + +#define ADS1115_REG_CONVERSION (0x00) /**< Conversion register */ +#define ADS1115_REG_CONFIG (0x01) /**< Configuration register */ +/** @} */ + + +/** + * @name ADS1115 configuration register bits + * @{ + */ +#define ADS1115_CONF_OS_BIT (15) /**< Operational status */ +#define ADS1115_CONF_MUX_BIT (12) /**< Input multiplexer configuration */ +#define ADS1115_CONF_PGA_BIT (9) /**< Programmable gain amplifier configuration */ +#define ADS1115_CONF_MODE_BIT (8) /**< Device mode */ +#define ADS1115_CONF_DR_BIT (5) /**< Data rate configuration */ +#define ADS1115_CONF_COMP_MODE_BIT (4) /**< Comparator mode */ +#define ADS1115_CONF_COMP_POLARITY_BIT (3) /**< Comparator polarity */ +#define ADS1115_CONF_COMP_LATCH_BIT (2) /**< Comparator latch */ +#define ADS1115_CONF_COMP_QUEUE_BIT (0) /**< Comparator queue */ +/** @} */ + + +#ifdef __cplusplus +} +#endif diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h new file mode 100644 index 000000000000..42722d1b4b3e --- /dev/null +++ b/drivers/ads1115/include/ads1115_params.h @@ -0,0 +1,88 @@ +/* + * Copyright (C) Baptiste Le Duc + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + + +#pragma once + +/** + * @ingroup drivers_ads1115 + * @{ + * + * @file + * @brief Default configuration for ADS1115 device + * + * @author Baptiste Le Duc + */ + +#include "board.h" +#include "ads1115.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * @name Set default configuration parameters for the ADS1115 driver + * @{ + */ +#ifndef ADS1115_PARAM_I2C +#define ADS1115_PARAM_I2C (I2C_DEV(0)) +#endif +#ifndef ADS1115_PARAM_ADDR +#define ADS1115_PARAM_ADDR (0x48) +#endif +#ifndef ADS1115_PARAM_MUX +#define ADS1115_PARAM_MUX (ADS1115_MUX_AIN0_AIN1) +#endif +#ifndef ADS1115_PARAM_PGA +#define ADS1115_PARAM_PGA (ADS1115_PGA_2_048V) +#endif +#ifndef ADS1115_PARAM_MODE +#define ADS1115_PARAM_MODE (ADS1115_MODE_SINGLE) +#endif +#ifndef ADS1115_PARAM_DR +#define ADS1115_PARAM_DR (ADS1115_DR_128) +#endif +#ifndef ADS1115_PARAM_COMP_MODE +#define ADS1115_PARAM_COMP_MODE (ADS1115_COMP_MODE_TRADITIONAL) +#endif +#ifndef ADS1115_PARAM_COMP_POLARITY +#define ADS1115_PARAM_COMP_POLARITY (ADS1115_COMP_POLARITY_LOW) +#endif +#ifndef ADS1115_PARAM_COMP_LATCH +#define ADS1115_PARAM_COMP_LATCH (ADS1115_COMP_LATCH_DISABLE) +#endif +#ifndef ADS1115_PARAM_COMP_QUEUE +#define ADS1115_PARAM_COMP_QUEUE (ADS1115_COMP_QUEUE_DISABLE) +#endif + +#ifndef ADS1115_PARAMS +#define ADS1115_PARAMS { \ + .i2c = ADS1115_PARAM_I2C, \ + .addr = ADS1115_PARAM_ADDR, \ + .mux = ADS1115_PARAM_MUX, \ + .pga = ADS1115_PARAM_PGA, \ + .mode = ADS1115_PARAM_MODE, \ + .dr = ADS1115_PARAM_DR, \ + .comp_mode = ADS1115_PARAM_COMP_MODE, \ + .comp_polarity = ADS1115_PARAM_COMP_POLARITY, \ + .comp_latch = ADS1115_PARAM_COMP_LATCH, \ + .comp_queue = ADS1115_PARAM_COMP_QUEUE \ +} +#endif + +static const ads1115_params_t ads1115_params[] = { + ADS1115_PARAMS +}; + + + +#ifdef __cplusplus +} +#endif diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h new file mode 100644 index 000000000000..388f3f38451a --- /dev/null +++ b/drivers/include/ads1115.h @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2025 Baptiste Le Duc + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#pragma once + +/** + * @defgroup drivers_ads1115 ADS1115 Analog-to-digital converter driver + * @ingroup drivers_sensors + * @ingroup drivers_saul + * @brief I2C Analog-to-digital converter with programmable gain amplifier. + * The device has four input channels: AIN0, AIN1, AIN2, and AIN3, that can be selected by using + * @ref ads1115_set_ain_ch_input. + * + * @file + * @brief ADS1115 Analog-to-digital converter driver + * + * @author Baptiste Le Duc + */ + + +#ifdef __cplusplus +extern "C" { +#endif + +#include "periph/i2c.h" + +/** + * @brief Named return values + */ +enum { + ADS1115_OK = 0, /**< Operation successful */ + ADS1115_NODEV = -1, /**< No device found on the bus */ + ADS1115_NOI2C = -2, /**< I2C communication error */ + ADS1115_NODATA = -3, /**< No data available */ +}; +/** + * @brief Operational status bit in write operations + */ +typedef enum{ + ADS1115_WRITE_NO_EFFECT = 0, /** */ + ADS1115_WRITE_SINGLE_SHOT = 1, /** */ +} ads1115_os_write_t; + +/** + * @brief Operational status bit in read operations + */ +typedef enum{ + ADS1115_READ_BUSY = 0, /** */ + ADS1115_READ_IDLE = 1, /** */ +} ads1115_os_read_t; + +/** + * @brief Input multiplexer configuration + */ +typedef enum { + ADS1115_MUX_AIN0_AIN1 = 0, /** Date: Thu, 17 Jul 2025 15:31:07 +0200 Subject: [PATCH 02/16] feat: add read, set input and convert to mV funcs --- drivers/ads1115/ads1115.c | 67 +++++++++++++++++++++++++++++++++++++++ drivers/include/ads1115.h | 50 ++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 11 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index f170e3699b6b..7b045847e988 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -56,6 +56,7 @@ static uint16_t _build_config_reg(const ads1115_params_t *params) return conf; } + int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) { assert(dev && params); @@ -99,4 +100,70 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) return res; } +int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux) +{ + assert(dev); + + int res = ADS1115_NOI2C; + + i2c_acquire(DEV); + + // Read current configuration + uint8_t reg[2]; + if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, reg, 2, 0) < 0) { + i2c_release(DEV); + goto release; + } + + // Update MUX bits + uint16_t conf = ((uint16_t)reg[0] << 8) | reg[1]; + conf &= ~(0x07 << ADS1115_CONF_MUX_BIT); // Clear MUX bits + conf |= (mux << ADS1115_CONF_MUX_BIT); // Set new MUX + + // Write back updated configuration + reg[0] = conf >> 8; + reg[1] = conf & 0xFF; + + if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, reg, 2, 0) < 0) { + goto release; + } + + res = ADS1115_OK; + dev->params.mux = mux; + + +release: + i2c_release(DEV); + return res; +} + +int ads1115_read_conversion(ads1115_t *dev, uint16_t *value) +{ + assert(dev && value); + + int res = ADS1115_NOI2C; + uint8_t buf[2]; + + i2c_acquire(DEV); + + // Read conversion register + if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONVERSION, buf, 2, 0) < 0) { + goto release; + } + + // Combine bytes into a single value + *value = ((int16_t)buf[0] << 8) | buf[1]; + res = ADS1115_OK; + +release: + i2c_release(DEV); + return res; +} + +int ads1115_convert_to_mv(ads1115_t *dev, uint16_t value) +{ + assert(dev); + return 1000 * value * _ads1115_get_pga_voltage(dev->params.pga) / (1 << 15); // Msb is sign bit +} + diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 388f3f38451a..d6c90364fbb8 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -34,8 +34,8 @@ extern "C" { */ enum { ADS1115_OK = 0, /**< Operation successful */ - ADS1115_NODEV = -1, /**< No device found on the bus */ - ADS1115_NOI2C = -2, /**< I2C communication error */ + ADS1115_NOI2C = -1, /**< I2C communication error */ + ADS1115_NODEV = -2, /**< No device found on the bus */ ADS1115_NODATA = -3, /**< No data available */ }; /** @@ -73,16 +73,16 @@ typedef enum { * @brief Programmable gain amplifier configuration */ typedef enum { - ADS1115_PGA_6_144V = 0x0, /**< FSR = ±6.144V */ - ADS1115_PGA_4_096V = 0x1, /**< FSR = ±4.096V */ - ADS1115_PGA_2_048V = 0x2, /**< FSR = ±2.048V (default) */ - ADS1115_PGA_1_024V = 0x3, /**< FSR = ±1.024V */ - ADS1115_PGA_0_512V = 0x4, /**< FSR = ±0.512V */ - ADS1115_PGA_0_256V = 0x5, /**< FSR = ±0.256V */ + ADS1115_PGA_6_144V, /**< FSR = ±6.144V */ + ADS1115_PGA_4_096V, /**< FSR = ±4.096V */ + ADS1115_PGA_2_048V, /**< FSR = ±2.048V (default) */ + ADS1115_PGA_1_024V, /**< FSR = ±1.024V */ + ADS1115_PGA_0_512V, /**< FSR = ±0.512V */ + ADS1115_PGA_0_256V, /**< FSR = ±0.256V */ // Aliases (same behavior) - ADS1115_PGA_0_256V_B = 0x6, /**< Alias: same as 0x5 */ - ADS1115_PGA_0_256V_C = 0x7 /**< Alias: same as 0x5 */ + ADS1115_PGA_0_256V_B, + ADS1115_PGA_0_256V_C } ads1115_pga_t; /** @@ -196,7 +196,35 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux); * @return ADS1115_NODATA if no data is available * @return ADS1115_NOI2C if other error occurs */ -int ads1115_read_conversion(ads1115_t *dev, int16_t *value); +int ads1115_read_conversion(ads1115_t *dev, uint16_t *value); + + +static inline float _ads1115_get_pga_voltage(ads1115_pga_t pga) +{ + switch (pga) { + case ADS1115_PGA_6_144V: return 6.144f; + case ADS1115_PGA_4_096V: return 4.096f; + case ADS1115_PGA_2_048V: return 2.048f; + case ADS1115_PGA_1_024V: return 1.024f; + case ADS1115_PGA_0_512V: return 0.512f; + case ADS1115_PGA_0_256V: + case ADS1115_PGA_0_256V_B: + case ADS1115_PGA_0_256V_C: + return 0.256f; + default: + return 0.0f; + } +} + +/** + * @brief Converts the digital value from the ADS1115 device to millivolts. + * + * @param[in] dev Device descriptor + * @param[in] value Raw value from the ADS1115 device + * + * @return Converted value in millivolts + */ +int ads1115_convert_to_mv(ads1115_t *dev, uint16_t value); #ifdef __cplusplus From 0f1490f23fb8fea20f8fd0f35ee6fe6090a95996 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Thu, 17 Jul 2025 15:53:30 +0200 Subject: [PATCH 03/16] style: uncrustify formatting --- drivers/ads1115/ads1115.c | 10 +- drivers/ads1115/include/ads1115_internal.h | 4 +- drivers/ads1115/include/ads1115_params.h | 20 ++-- drivers/include/ads1115.h | 122 ++++++++++----------- 4 files changed, 77 insertions(+), 79 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index 7b045847e988..50388f58b861 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -69,7 +69,7 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) i2c_acquire(DEV); // Test communication - uint8_t test_conf[2] = {0x00, ADS1115_CONF_TEST_VALUE}; + uint8_t test_conf[2] = { 0x00, ADS1115_CONF_TEST_VALUE }; if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, test_conf, 2, 0) < 0) { DEBUG("[ads1115] init - error: write test failed\n"); res = ADS1115_NODEV; @@ -103,7 +103,7 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux) { assert(dev); - + int res = ADS1115_NOI2C; i2c_acquire(DEV); @@ -117,8 +117,8 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux) // Update MUX bits uint16_t conf = ((uint16_t)reg[0] << 8) | reg[1]; - conf &= ~(0x07 << ADS1115_CONF_MUX_BIT); // Clear MUX bits - conf |= (mux << ADS1115_CONF_MUX_BIT); // Set new MUX + conf &= ~(0x07 << ADS1115_CONF_MUX_BIT); // Clear MUX bits + conf |= (mux << ADS1115_CONF_MUX_BIT); // Set new MUX // Write back updated configuration reg[0] = conf >> 8; @@ -165,5 +165,3 @@ int ads1115_convert_to_mv(ads1115_t *dev, uint16_t value) assert(dev); return 1000 * value * _ads1115_get_pga_voltage(dev->params.pga) / (1 << 15); // Msb is sign bit } - - diff --git a/drivers/ads1115/include/ads1115_internal.h b/drivers/ads1115/include/ads1115_internal.h index 0a1debfb988e..76379fba6199 100644 --- a/drivers/ads1115/include/ads1115_internal.h +++ b/drivers/ads1115/include/ads1115_internal.h @@ -30,8 +30,8 @@ extern "C" { * @{ */ -#define ADS1115_REG_CONVERSION (0x00) /**< Conversion register */ -#define ADS1115_REG_CONFIG (0x01) /**< Configuration register */ +#define ADS1115_REG_CONVERSION (0x00) /**< Conversion register */ +#define ADS1115_REG_CONFIG (0x01) /**< Configuration register */ /** @} */ diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 42722d1b4b3e..488dccb2bfda 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -64,16 +64,16 @@ extern "C" { #ifndef ADS1115_PARAMS #define ADS1115_PARAMS { \ - .i2c = ADS1115_PARAM_I2C, \ - .addr = ADS1115_PARAM_ADDR, \ - .mux = ADS1115_PARAM_MUX, \ - .pga = ADS1115_PARAM_PGA, \ - .mode = ADS1115_PARAM_MODE, \ - .dr = ADS1115_PARAM_DR, \ - .comp_mode = ADS1115_PARAM_COMP_MODE, \ - .comp_polarity = ADS1115_PARAM_COMP_POLARITY, \ - .comp_latch = ADS1115_PARAM_COMP_LATCH, \ - .comp_queue = ADS1115_PARAM_COMP_QUEUE \ + .i2c = ADS1115_PARAM_I2C, \ + .addr = ADS1115_PARAM_ADDR, \ + .mux = ADS1115_PARAM_MUX, \ + .pga = ADS1115_PARAM_PGA, \ + .mode = ADS1115_PARAM_MODE, \ + .dr = ADS1115_PARAM_DR, \ + .comp_mode = ADS1115_PARAM_COMP_MODE, \ + .comp_polarity = ADS1115_PARAM_COMP_POLARITY, \ + .comp_latch = ADS1115_PARAM_COMP_LATCH, \ + .comp_queue = ADS1115_PARAM_COMP_QUEUE \ } #endif diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index d6c90364fbb8..9d45a69b081d 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -15,7 +15,7 @@ * @brief I2C Analog-to-digital converter with programmable gain amplifier. * The device has four input channels: AIN0, AIN1, AIN2, and AIN3, that can be selected by using * @ref ads1115_set_ain_ch_input. - * + * * @file * @brief ADS1115 Analog-to-digital converter driver * @@ -33,39 +33,39 @@ extern "C" { * @brief Named return values */ enum { - ADS1115_OK = 0, /**< Operation successful */ - ADS1115_NOI2C = -1, /**< I2C communication error */ - ADS1115_NODEV = -2, /**< No device found on the bus */ - ADS1115_NODATA = -3, /**< No data available */ + ADS1115_OK = 0, /**< Operation successful */ + ADS1115_NOI2C = -1, /**< I2C communication error */ + ADS1115_NODEV = -2, /**< No device found on the bus */ + ADS1115_NODATA = -3, /**< No data available */ }; /** * @brief Operational status bit in write operations */ -typedef enum{ - ADS1115_WRITE_NO_EFFECT = 0, /** */ - ADS1115_WRITE_SINGLE_SHOT = 1, /** */ +typedef enum { + ADS1115_WRITE_NO_EFFECT = 0, /** */ + ADS1115_WRITE_SINGLE_SHOT = 1, /** */ } ads1115_os_write_t; /** * @brief Operational status bit in read operations */ -typedef enum{ - ADS1115_READ_BUSY = 0, /** */ - ADS1115_READ_IDLE = 1, /** */ +typedef enum { + ADS1115_READ_BUSY = 0, /** */ + ADS1115_READ_IDLE = 1, /** */ } ads1115_os_read_t; /** * @brief Input multiplexer configuration */ typedef enum { - ADS1115_MUX_AIN0_AIN1 = 0, /** Date: Fri, 18 Jul 2025 09:44:20 +0200 Subject: [PATCH 04/16] refactor: return PGA values as int mV instead of float Co-authored-by: benpicco --- drivers/include/ads1115.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 9d45a69b081d..210f3d6fdf22 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -198,21 +198,20 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux); */ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value); - -static inline float _ads1115_get_pga_voltage(ads1115_pga_t pga) +static inline int _ads1115_get_pga_voltage(ads1115_pga_t pga) { switch (pga) { - case ADS1115_PGA_6_144V: return 6.144f; - case ADS1115_PGA_4_096V: return 4.096f; - case ADS1115_PGA_2_048V: return 2.048f; - case ADS1115_PGA_1_024V: return 1.024f; - case ADS1115_PGA_0_512V: return 0.512f; + case ADS1115_PGA_6_144V: return 6144; + case ADS1115_PGA_4_096V: return 4096; + case ADS1115_PGA_2_048V: return 2048; + case ADS1115_PGA_1_024V: return 1024; + case ADS1115_PGA_0_512V: return 512; case ADS1115_PGA_0_256V: case ADS1115_PGA_0_256V_B: case ADS1115_PGA_0_256V_C: - return 0.256f; + return 256; default: - return 0.0f; + return 0; } } From 4a409b15979eb22976558bb1a5cf13f2fa4e639f Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Fri, 18 Jul 2025 11:17:36 +0200 Subject: [PATCH 05/16] refactor: use htons/ntohs for byte swapping --- drivers/ads1115/ads1115.c | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index 50388f58b861..be47add9a7ad 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -23,6 +23,7 @@ #include "ads1115_internal.h" #include "periph/i2c.h" +#include "byteorder.h" #define ENABLE_DEBUG 0 #include "debug.h" @@ -69,25 +70,23 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) i2c_acquire(DEV); // Test communication - uint8_t test_conf[2] = { 0x00, ADS1115_CONF_TEST_VALUE }; - if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, test_conf, 2, 0) < 0) { + uint16_t test_conf = htons(ADS1115_CONF_TEST_VALUE); + if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, &test_conf, sizeof(test_conf), 0) < 0) { DEBUG("[ads1115] init - error: write test failed\n"); res = ADS1115_NODEV; } - uint8_t reg[2]; - if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, reg, 2, 0) < 0 || - (((uint16_t)reg[0] << 8) | reg[1]) != ADS1115_CONF_TEST_VALUE) { - DEBUG("[ads1115] init - error: read test failed (reg=%02x%02x)\n", reg[0], reg[1]); + uint16_t reg; + if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, ®, sizeof(reg), 0) < 0 || + ntohs(reg) != ADS1115_CONF_TEST_VALUE) { + DEBUG("[ads1115] init - error: read test failed (reg=%04x)\n", ntohs(reg)); res = ADS1115_NOI2C; goto release; } // Apply actual configuration - uint16_t conf = _build_config_reg(&dev->params); - uint8_t conf_bytes[2] = { conf >> 8, conf & 0xFF }; - - if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, conf_bytes, 2, 0) < 0) { + uint16_t conf = htons(_build_config_reg(&dev->params)); + if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, &conf, sizeof(conf), 0) < 0) { DEBUG("[ads1115] init - error: setting config failed\n"); res = ADS1115_NOI2C; goto release; @@ -109,22 +108,21 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux) i2c_acquire(DEV); // Read current configuration - uint8_t reg[2]; - if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, reg, 2, 0) < 0) { + uint16_t reg; + if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, ®, sizeof(reg), 0) < 0) { i2c_release(DEV); goto release; } // Update MUX bits - uint16_t conf = ((uint16_t)reg[0] << 8) | reg[1]; + uint16_t conf = ntohs(reg); conf &= ~(0x07 << ADS1115_CONF_MUX_BIT); // Clear MUX bits conf |= (mux << ADS1115_CONF_MUX_BIT); // Set new MUX // Write back updated configuration - reg[0] = conf >> 8; - reg[1] = conf & 0xFF; + reg = htons(conf); - if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, reg, 2, 0) < 0) { + if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, ®, sizeof(reg), 0) < 0) { goto release; } @@ -142,17 +140,17 @@ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value) assert(dev && value); int res = ADS1115_NOI2C; - uint8_t buf[2]; + uint16_t buf; i2c_acquire(DEV); // Read conversion register - if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONVERSION, buf, 2, 0) < 0) { + if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONVERSION, &buf, sizeof(buf), 0) < 0) { goto release; } // Combine bytes into a single value - *value = ((int16_t)buf[0] << 8) | buf[1]; + *value = ntohs(buf); res = ADS1115_OK; release: @@ -163,5 +161,5 @@ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value) int ads1115_convert_to_mv(ads1115_t *dev, uint16_t value) { assert(dev); - return 1000 * value * _ads1115_get_pga_voltage(dev->params.pga) / (1 << 15); // Msb is sign bit + return value * _ads1115_get_pga_voltage(dev->params.pga) / (1 << 15); // Msb is sign bit } From b71cc2f1e1d571cdc5ee8f0394490599ca8c6690 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Fri, 18 Jul 2025 14:28:22 +0200 Subject: [PATCH 06/16] refactor: move a private inline static function to .c file --- drivers/ads1115/ads1115.c | 16 ++++++++++++++++ drivers/include/ads1115.h | 16 ---------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index be47add9a7ad..63f4fcb65820 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -33,6 +33,22 @@ #define ADS1115_CONF_TEST_VALUE (1) +static inline int _ads1115_get_pga_voltage(ads1115_pga_t pga) +{ + switch (pga) { + case ADS1115_PGA_6_144V: return 6144; + case ADS1115_PGA_4_096V: return 4096; + case ADS1115_PGA_2_048V: return 2048; + case ADS1115_PGA_1_024V: return 1024; + case ADS1115_PGA_0_512V: return 512; + case ADS1115_PGA_0_256V: + case ADS1115_PGA_0_256V_B: + case ADS1115_PGA_0_256V_C: + return 256; + default: + return 0; + } +} /** * @brief Builds the configuration register value from the parameters diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 210f3d6fdf22..16658fdfdae1 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -198,22 +198,6 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux); */ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value); -static inline int _ads1115_get_pga_voltage(ads1115_pga_t pga) -{ - switch (pga) { - case ADS1115_PGA_6_144V: return 6144; - case ADS1115_PGA_4_096V: return 4096; - case ADS1115_PGA_2_048V: return 2048; - case ADS1115_PGA_1_024V: return 1024; - case ADS1115_PGA_0_512V: return 512; - case ADS1115_PGA_0_256V: - case ADS1115_PGA_0_256V_B: - case ADS1115_PGA_0_256V_C: - return 256; - default: - return 0; - } -} /** * @brief Converts the digital value from the ADS1115 device to millivolts. From 99230a913cc9b9a7fead410e8a5b1fb08327216a Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Mon, 21 Jul 2025 20:51:06 +0200 Subject: [PATCH 07/16] driver: change C++ comments style to C style --- drivers/ads1115/ads1115.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index 63f4fcb65820..aea5aa9ac2f9 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -85,7 +85,7 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) i2c_acquire(DEV); - // Test communication + /* Test communication */ uint16_t test_conf = htons(ADS1115_CONF_TEST_VALUE); if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, &test_conf, sizeof(test_conf), 0) < 0) { DEBUG("[ads1115] init - error: write test failed\n"); @@ -100,7 +100,7 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) goto release; } - // Apply actual configuration + /* Apply actual configuration */ uint16_t conf = htons(_build_config_reg(&dev->params)); if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, &conf, sizeof(conf), 0) < 0) { DEBUG("[ads1115] init - error: setting config failed\n"); @@ -123,19 +123,19 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux) i2c_acquire(DEV); - // Read current configuration + /* Read current configuration */ uint16_t reg; if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONFIG, ®, sizeof(reg), 0) < 0) { i2c_release(DEV); goto release; } - // Update MUX bits + /* Update MUX bits */ uint16_t conf = ntohs(reg); - conf &= ~(0x07 << ADS1115_CONF_MUX_BIT); // Clear MUX bits - conf |= (mux << ADS1115_CONF_MUX_BIT); // Set new MUX + conf &= ~(0x07 << ADS1115_CONF_MUX_BIT); /* Clear MUX bits */ + conf |= (mux << ADS1115_CONF_MUX_BIT); /* Set new MUX */ - // Write back updated configuration + /* Write back updated configuration */ reg = htons(conf); if (i2c_write_regs(DEV, ADDR, ADS1115_REG_CONFIG, ®, sizeof(reg), 0) < 0) { @@ -160,12 +160,12 @@ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value) i2c_acquire(DEV); - // Read conversion register + /* Read conversion register */ if (i2c_read_regs(DEV, ADDR, ADS1115_REG_CONVERSION, &buf, sizeof(buf), 0) < 0) { goto release; } - // Combine bytes into a single value + /* Combine bytes into a single value */ *value = ntohs(buf); res = ADS1115_OK; @@ -177,5 +177,5 @@ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value) int ads1115_convert_to_mv(ads1115_t *dev, uint16_t value) { assert(dev); - return value * _ads1115_get_pga_voltage(dev->params.pga) / (1 << 15); // Msb is sign bit + return value * _ads1115_get_pga_voltage(dev->params.pga) / (1 << 15); /* Msb is sign bit */ } From e24b9256c7716b43ce6672e0962293f85fe7ace6 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Mon, 21 Jul 2025 20:55:22 +0200 Subject: [PATCH 08/16] driver: remove two consecutives empty lines --- drivers/ads1115/ads1115.c | 2 -- drivers/ads1115/include/ads1115_internal.h | 4 ---- drivers/ads1115/include/ads1115_params.h | 3 --- drivers/include/ads1115.h | 6 +----- 4 files changed, 1 insertion(+), 14 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index aea5aa9ac2f9..4d1378513f71 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -73,7 +73,6 @@ static uint16_t _build_config_reg(const ads1115_params_t *params) return conf; } - int ads1115_init(ads1115_t *dev, const ads1115_params_t *params) { assert(dev && params); @@ -145,7 +144,6 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux) res = ADS1115_OK; dev->params.mux = mux; - release: i2c_release(DEV); return res; diff --git a/drivers/ads1115/include/ads1115_internal.h b/drivers/ads1115/include/ads1115_internal.h index 76379fba6199..623ff82151c6 100644 --- a/drivers/ads1115/include/ads1115_internal.h +++ b/drivers/ads1115/include/ads1115_internal.h @@ -19,12 +19,10 @@ * @} */ - #ifdef __cplusplus extern "C" { #endif - /** * @name ADS1115 registers * @{ @@ -34,7 +32,6 @@ extern "C" { #define ADS1115_REG_CONFIG (0x01) /**< Configuration register */ /** @} */ - /** * @name ADS1115 configuration register bits * @{ @@ -50,7 +47,6 @@ extern "C" { #define ADS1115_CONF_COMP_QUEUE_BIT (0) /**< Comparator queue */ /** @} */ - #ifdef __cplusplus } #endif diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 488dccb2bfda..746c8c337fda 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -26,7 +26,6 @@ extern "C" { #endif - /** * @name Set default configuration parameters for the ADS1115 driver * @{ @@ -81,8 +80,6 @@ static const ads1115_params_t ads1115_params[] = { ADS1115_PARAMS }; - - #ifdef __cplusplus } #endif diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 16658fdfdae1..291cc4703a65 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -22,7 +22,6 @@ * @author Baptiste Le Duc */ - #ifdef __cplusplus extern "C" { #endif @@ -38,6 +37,7 @@ enum { ADS1115_NODEV = -2, /**< No device found on the bus */ ADS1115_NODATA = -3, /**< No data available */ }; + /** * @brief Operational status bit in write operations */ @@ -68,7 +68,6 @@ typedef enum { ADS1115_MUX_AIN3 = 7 /** Date: Mon, 21 Jul 2025 20:58:10 +0200 Subject: [PATCH 09/16] driver: adapt comments to Doxygen convention --- drivers/include/ads1115.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 291cc4703a65..14406a8b5813 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -42,8 +42,8 @@ enum { * @brief Operational status bit in write operations */ typedef enum { - ADS1115_WRITE_NO_EFFECT = 0, /** */ - ADS1115_WRITE_SINGLE_SHOT = 1, /** */ + ADS1115_WRITE_NO_EFFECT = 0, /**< No effect */ + ADS1115_WRITE_SINGLE_SHOT = 1, /**< Start a single-shot conversion when in power-down mode */ } ads1115_os_write_t; /** @@ -51,21 +51,21 @@ typedef enum { */ typedef enum { ADS1115_READ_BUSY = 0, /** */ - ADS1115_READ_IDLE = 1, /** */ + ADS1115_READ_IDLE = 1, /**< Device is not performing a conversion */ } ads1115_os_read_t; /** * @brief Input multiplexer configuration */ typedef enum { - ADS1115_MUX_AIN0_AIN1 = 0, /** Date: Mon, 21 Jul 2025 21:07:41 +0200 Subject: [PATCH 10/16] driver: use retval to avoid multiple return in doc --- drivers/include/ads1115.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 14406a8b5813..a7f03e685090 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -168,9 +168,9 @@ typedef struct { * @param[in,out] dev Device descriptor * @param[in] params Device configuration parameters * - * @return ADS1115_OK on success - * @return ADS1115_NODEV if no device is found on the bus - * @return ADS1115_NOI2C if other error occurs + * @retval ADS1115_OK on success + * @retval ADS1115_NODEV if no device is found on the bus + * @retval ADS1115_NOI2C if other error occurs */ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params); @@ -180,9 +180,9 @@ int ads1115_init(ads1115_t *dev, const ads1115_params_t *params); * @param[in,out] dev Device descriptor * @param[in] mux Input multiplexer configuration * - * @return ADS1115_OK on success - * @return ADS1115_NODEV if no device is found on the bus - * @return ADS1115_NOI2C if other error occurs + * @retval ADS1115_OK on success + * @retval ADS1115_NODEV if no device is found on the bus + * @retval ADS1115_NOI2C if other error occurs */ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux); @@ -190,9 +190,9 @@ int ads1115_set_ain_ch_input(ads1115_t *dev, ads1115_mux_t mux); * @brief Read the conversion register of the ADS1115 device. * @param[in,out] dev Device descriptor * @param[out] value Pointer to store the conversion result - * @return ADS1115_OK on success - * @return ADS1115_NODATA if no data is available - * @return ADS1115_NOI2C if other error occurs + * @retval ADS1115_OK on success + * @retval ADS1115_NODATA if no data is available + * @retval ADS1115_NOI2C if other error occurs */ int ads1115_read_conversion(ads1115_t *dev, uint16_t *value); From 79130606627bc09af48196a55bd3fb479a1edae2 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Mon, 21 Jul 2025 21:19:04 +0200 Subject: [PATCH 11/16] driver: indent #define macro and add comments to them --- drivers/ads1115/include/ads1115_params.h | 100 ++++++++++++++++++----- 1 file changed, 79 insertions(+), 21 deletions(-) diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 746c8c337fda..40251f506f44 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -30,56 +30,114 @@ extern "C" { * @name Set default configuration parameters for the ADS1115 driver * @{ */ + +/** + * @def ADS1115_PARAM_I2C + * @brief Default I2C bus + */ #ifndef ADS1115_PARAM_I2C -#define ADS1115_PARAM_I2C (I2C_DEV(0)) +# define ADS1115_PARAM_I2C (I2C_DEV(0)) #endif + +/** + * @def ADS1115_PARAM_ADDR + * @brief Default I2C address + */ #ifndef ADS1115_PARAM_ADDR -#define ADS1115_PARAM_ADDR (0x48) +# define ADS1115_PARAM_ADDR (0x48) #endif + +/** + * @def ADS1115_PARAM_MUX + * @brief Default multiplexer configuration + */ #ifndef ADS1115_PARAM_MUX -#define ADS1115_PARAM_MUX (ADS1115_MUX_AIN0_AIN1) +# define ADS1115_PARAM_MUX (ADS1115_MUX_AIN0_AIN1) #endif + +/** + * @def ADS1115_PARAM_PGA + * @brief Default programmable gain amplifier configuration + */ #ifndef ADS1115_PARAM_PGA -#define ADS1115_PARAM_PGA (ADS1115_PGA_2_048V) +# define ADS1115_PARAM_PGA (ADS1115_PGA_2_048V) #endif + +/** + * @def ADS1115_PARAM_MODE + * @brief Default device mode + */ #ifndef ADS1115_PARAM_MODE -#define ADS1115_PARAM_MODE (ADS1115_MODE_SINGLE) +# define ADS1115_PARAM_MODE (ADS1115_MODE_SINGLE) #endif + +/** + * @def ADS1115_PARAM_DR + * @brief Default data rate configuration + */ #ifndef ADS1115_PARAM_DR -#define ADS1115_PARAM_DR (ADS1115_DR_128) +# define ADS1115_PARAM_DR (ADS1115_DR_128) #endif + +/** + * @def ADS1115_PARAM_COMP_MODE + * @brief Default comparator mode + */ #ifndef ADS1115_PARAM_COMP_MODE -#define ADS1115_PARAM_COMP_MODE (ADS1115_COMP_MODE_TRADITIONAL) +# define ADS1115_PARAM_COMP_MODE (ADS1115_COMP_MODE_TRADITIONAL) #endif + +/** + * @def ADS1115_PARAM_COMP_POLARITY + * @brief Default comparator polarity + */ #ifndef ADS1115_PARAM_COMP_POLARITY -#define ADS1115_PARAM_COMP_POLARITY (ADS1115_COMP_POLARITY_LOW) +# define ADS1115_PARAM_COMP_POLARITY (ADS1115_COMP_POLARITY_LOW) #endif + +/** + * @def ADS1115_PARAM_COMP_LATCH + * @brief Default comparator latch + */ #ifndef ADS1115_PARAM_COMP_LATCH -#define ADS1115_PARAM_COMP_LATCH (ADS1115_COMP_LATCH_DISABLE) +# define ADS1115_PARAM_COMP_LATCH (ADS1115_COMP_LATCH_DISABLE) #endif + +/** + * @def ADS1115_PARAM_COMP_QUEUE + * @brief Default comparator queue + */ #ifndef ADS1115_PARAM_COMP_QUEUE -#define ADS1115_PARAM_COMP_QUEUE (ADS1115_COMP_QUEUE_DISABLE) +# define ADS1115_PARAM_COMP_QUEUE (ADS1115_COMP_QUEUE_DISABLE) #endif +/** + * @def ADS1115_PARAMS + * @brief Default configuration structure for the ADS1115 driver + */ #ifndef ADS1115_PARAMS -#define ADS1115_PARAMS { \ - .i2c = ADS1115_PARAM_I2C, \ - .addr = ADS1115_PARAM_ADDR, \ - .mux = ADS1115_PARAM_MUX, \ - .pga = ADS1115_PARAM_PGA, \ - .mode = ADS1115_PARAM_MODE, \ - .dr = ADS1115_PARAM_DR, \ - .comp_mode = ADS1115_PARAM_COMP_MODE, \ - .comp_polarity = ADS1115_PARAM_COMP_POLARITY, \ - .comp_latch = ADS1115_PARAM_COMP_LATCH, \ - .comp_queue = ADS1115_PARAM_COMP_QUEUE \ +# define ADS1115_PARAMS { \ + .i2c = ADS1115_PARAM_I2C, \ + .addr = ADS1115_PARAM_ADDR, \ + .mux = ADS1115_PARAM_MUX, \ + .pga = ADS1115_PARAM_PGA, \ + .mode = ADS1115_PARAM_MODE, \ + .dr = ADS1115_PARAM_DR, \ + .comp_mode = ADS1115_PARAM_COMP_MODE, \ + .comp_polarity = ADS1115_PARAM_COMP_POLARITY, \ + .comp_latch = ADS1115_PARAM_COMP_LATCH, \ + .comp_queue = ADS1115_PARAM_COMP_QUEUE \ } #endif +/** @} */ +/* Default parameters structure */ static const ads1115_params_t ads1115_params[] = { ADS1115_PARAMS }; +/** @} */ // close @ingroup drivers_ads1115 + #ifdef __cplusplus } #endif From 9ebfa9dea5861d6a27fb9d006faa66ea58c7349c Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Mon, 21 Jul 2025 21:21:12 +0200 Subject: [PATCH 12/16] driver: change copyright statements to SPDX format --- drivers/ads1115/ads1115.c | 7 ++----- drivers/ads1115/include/ads1115_params.h | 7 ++----- drivers/include/ads1115.h | 7 ++----- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index 4d1378513f71..39acfa2c4cf9 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -1,10 +1,7 @@ /* - * Copyright (C) Baptiste Le Duc - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. + * SPDX-FileCopyrightText: 2025 Baptiste Le Duc + * SPDX-License-Identifier: LGPL-2.1-only */ /** diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 40251f506f44..7fd29560c10a 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -1,9 +1,6 @@ /* - * Copyright (C) Baptiste Le Duc - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. + * SPDX-FileCopyrightText: 2025 Baptiste Le Duc + * SPDX-License-Identifier: LGPL-2.1-only */ diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index a7f03e685090..74d10ace332c 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -1,9 +1,6 @@ /* - * Copyright (C) 2025 Baptiste Le Duc - * - * This file is subject to the terms and conditions of the GNU Lesser - * General Public License v2.1. See the file LICENSE in the top level - * directory for more details. + * SPDX-FileCopyrightText: 2025 Baptiste Le Duc + * SPDX-License-Identifier: LGPL-2.1-only */ #pragma once From 915158c5ad429cda95955ae70a79cb8c54916d48 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Mon, 21 Jul 2025 21:22:34 +0200 Subject: [PATCH 13/16] driver: remove leading empty line --- drivers/ads1115/ads1115.c | 1 - drivers/ads1115/include/ads1115_params.h | 1 - 2 files changed, 2 deletions(-) diff --git a/drivers/ads1115/ads1115.c b/drivers/ads1115/ads1115.c index 39acfa2c4cf9..1fcdc7822e1e 100644 --- a/drivers/ads1115/ads1115.c +++ b/drivers/ads1115/ads1115.c @@ -1,4 +1,3 @@ - /* * SPDX-FileCopyrightText: 2025 Baptiste Le Duc * SPDX-License-Identifier: LGPL-2.1-only diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 7fd29560c10a..8ccd323a58f7 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -3,7 +3,6 @@ * SPDX-License-Identifier: LGPL-2.1-only */ - #pragma once /** From 5c30d11e84eea96b712156655d5187b33d1d4a11 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Tue, 22 Jul 2025 21:24:34 +0200 Subject: [PATCH 14/16] drivers: add missing doxygen docstring --- drivers/ads1115/include/ads1115_params.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 8ccd323a58f7..9d3f70c3f284 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -127,7 +127,9 @@ extern "C" { #endif /** @} */ -/* Default parameters structure */ +/** + * @brief Default parameters structure + */ static const ads1115_params_t ads1115_params[] = { ADS1115_PARAMS }; From 8ee90ac02b9936cb7f57c0aef9a437037199b2f0 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Tue, 22 Jul 2025 23:13:59 +0200 Subject: [PATCH 15/16] driver: reformat comments to be under 100 characters --- drivers/include/ads1115.h | 104 ++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index 74d10ace332c..eadbaeff31a0 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -10,8 +10,8 @@ * @ingroup drivers_sensors * @ingroup drivers_saul * @brief I2C Analog-to-digital converter with programmable gain amplifier. - * The device has four input channels: AIN0, AIN1, AIN2, and AIN3, that can be selected by using - * @ref ads1115_set_ain_ch_input. + * The device has four input channels: AIN0, AIN1, AIN2, and AIN3, + * that can be selected by using @ref ads1115_set_ain_ch_input. * * @file * @brief ADS1115 Analog-to-digital converter driver @@ -29,40 +29,48 @@ extern "C" { * @brief Named return values */ enum { - ADS1115_OK = 0, /**< Operation successful */ - ADS1115_NOI2C = -1, /**< I2C communication error */ - ADS1115_NODEV = -2, /**< No device found on the bus */ - ADS1115_NODATA = -3, /**< No data available */ + ADS1115_OK = 0, /**< Operation successful */ + ADS1115_NOI2C = -1, /**< I2C communication error */ + ADS1115_NODEV = -2, /**< No device found on the bus */ + ADS1115_NODATA = -3, /**< No data available */ }; /** * @brief Operational status bit in write operations */ typedef enum { - ADS1115_WRITE_NO_EFFECT = 0, /**< No effect */ - ADS1115_WRITE_SINGLE_SHOT = 1, /**< Start a single-shot conversion when in power-down mode */ + ADS1115_WRITE_NO_EFFECT = 0, /**< No effect */ + ADS1115_WRITE_SINGLE_SHOT = 1, /**< Start single-shot conversion in power-down mode */ } ads1115_os_write_t; /** * @brief Operational status bit in read operations */ typedef enum { - ADS1115_READ_BUSY = 0, /** */ - ADS1115_READ_IDLE = 1, /**< Device is not performing a conversion */ + ADS1115_READ_BUSY = 0, /** */ + ADS1115_READ_IDLE = 1, /**< Device is not performing a conversion */ } ads1115_os_read_t; /** * @brief Input multiplexer configuration */ typedef enum { - ADS1115_MUX_AIN0_AIN1 = 0, /**< AIN0 - AIN1 differential input (AINp = AIN0 && AINn = AIN1) (default) */ - ADS1115_MUX_AIN0_AIN3 = 1, /**< AIN0 - AIN3 differential input (AINp = AIN0 && AINn = AIN3) */ - ADS1115_MUX_AIN1_AIN3 = 2, /**< AIN1 - AIN3 differential input (AINp = AIN1 && AINn = AIN3) */ - ADS1115_MUX_AIN2_AIN3 = 3, /**< AIN2 - AIN3 differential input (AINp = AIN2 && AINn = AIN3) */ - ADS1115_MUX_AIN0 = 4, /**< AIN0 single-ended input (AINp = AIN0 && AINn = GND) */ - ADS1115_MUX_AIN1 = 5, /**< AIN1 single-ended input (AINp = AIN1 && AINn = GND) */ - ADS1115_MUX_AIN2 = 6, /**< AIN2 single-ended input (AINp = AIN2 && AINn = GND) */ - ADS1115_MUX_AIN3 = 7 /**< AIN3 single-ended input (AINp = AIN3 && AINn = GND) */ + /** AIN0 - AIN1 differential input (AINp = AIN0 && AINn = AIN1) (default) */ + ADS1115_MUX_AIN0_AIN1 = 0, + /** AIN0 - AIN3 differential input (AINp = AIN0 && AINn = AIN3) */ + ADS1115_MUX_AIN0_AIN3 = 1, + /** AIN1 - AIN3 differential input (AINp = AIN1 && AINn = AIN3) */ + ADS1115_MUX_AIN1_AIN3 = 2, + /** AIN2 - AIN3 differential input (AINp = AIN2 && AINn = AIN3) */ + ADS1115_MUX_AIN2_AIN3 = 3, + /** AIN0 single-ended input (AINp = AIN0 && AINn = GND) */ + ADS1115_MUX_AIN0 = 4, + /** AIN1 single-ended input (AINp = AIN1 && AINn = GND) */ + ADS1115_MUX_AIN1 = 5, + /** AIN2 single-ended input (AINp = AIN2 && AINn = GND) */ + ADS1115_MUX_AIN2 = 6, + /** AIN3 single-ended input (AINp = AIN3 && AINn = GND) */ + ADS1115_MUX_AIN3 = 7 } ads1115_mux_t; /** @@ -85,71 +93,71 @@ typedef enum { * @brief Device mode */ typedef enum { - ADS1115_MODE_CONTINUOUS = 0, /**< Continuous conversion mode */ - ADS1115_MODE_SINGLE = 1 /**< Single-shot mode or power-down mode (default) */ + ADS1115_MODE_CONTINUOUS = 0, /**< Continuous conversion mode */ + ADS1115_MODE_SINGLE = 1 /**< Single-shot mode or power-down mode (default) */ } ads1115_mode_t; /** * @brief Data rate configuration */ typedef enum { - ADS1115_DR_8 = 0, /**< 8 SPS */ - ADS1115_DR_16 = 1, /**< 16 SPS */ - ADS1115_DR_32 = 2, /**< 32 SPS */ - ADS1115_DR_64 = 3, /**< 64 SPS */ - ADS1115_DR_128 = 4, /**< 128 SPS (default) */ - ADS1115_DR_250 = 5, /**< 250 SPS */ - ADS1115_DR_475 = 6, /**< 475 SPS */ - ADS1115_DR_860 = 7 /**< 860 SPS */ + ADS1115_DR_8 = 0, /**< 8 SPS */ + ADS1115_DR_16 = 1, /**< 16 SPS */ + ADS1115_DR_32 = 2, /**< 32 SPS */ + ADS1115_DR_64 = 3, /**< 64 SPS */ + ADS1115_DR_128 = 4, /**< 128 SPS (default) */ + ADS1115_DR_250 = 5, /**< 250 SPS */ + ADS1115_DR_475 = 6, /**< 475 SPS */ + ADS1115_DR_860 = 7 /**< 860 SPS */ } ads1115_dr_t; /** * @brief Comparator mode */ typedef enum { - ADS1115_COMP_MODE_TRADITIONAL = 0, /**< Traditional comparator (default) */ - ADS1115_COMP_MODE_WINDOW = 1 /**< Window comparator */ + ADS1115_COMP_MODE_TRADITIONAL = 0, /**< Traditional comparator (default) */ + ADS1115_COMP_MODE_WINDOW = 1 /**< Window comparator */ } ads1115_comp_mode_t; /** * @brief Comparator polarity */ typedef enum { - ADS1115_COMP_POLARITY_LOW = 0, /**< Active low (default) */ - ADS1115_COMP_POLARITY_HIGH = 1 /**< Active high */ + ADS1115_COMP_POLARITY_LOW = 0, /**< Active low (default) */ + ADS1115_COMP_POLARITY_HIGH = 1 /**< Active high */ } ads1115_comp_polarity_t; /** * @brief Comparator latch */ typedef enum { - ADS1115_COMP_LATCH_DISABLE = 0, /**< Comparator latch disabled (default) */ - ADS1115_COMP_LATCH_ENABLE = 1 /**< Comparator latch enabled */ + ADS1115_COMP_LATCH_DISABLE = 0, /**< Comparator latch disabled (default) */ + ADS1115_COMP_LATCH_ENABLE = 1 /**< Comparator latch enabled */ } ads1115_comp_latch_t; /** * @brief Comparator queue */ typedef enum { - ADS1115_COMP_QUEUE_1 = 0, /**< Assert after one conversion */ - ADS1115_COMP_QUEUE_2 = 1, /**< Assert after two conversions */ - ADS1115_COMP_QUEUE_4 = 2, /**< Assert after four conversions */ - ADS1115_COMP_QUEUE_DISABLE = 3 /**< Disable comparator (default) */ + ADS1115_COMP_QUEUE_1 = 0, /**< Assert after one conversion */ + ADS1115_COMP_QUEUE_2 = 1, /**< Assert after two conversions */ + ADS1115_COMP_QUEUE_4 = 2, /**< Assert after four conversions */ + ADS1115_COMP_QUEUE_DISABLE = 3 /**< Disable comparator (default) */ } ads1115_comp_queue_t; /** * @brief ADS1115 parameters */ typedef struct { - i2c_t i2c; /**< I2C device */ - uint8_t addr; /**< I2C address */ - ads1115_mux_t mux; /**< Input multiplexer configuration */ - ads1115_pga_t pga; /**< Programmable gain amplifier configuration */ - ads1115_mode_t mode; /**< Device mode */ - ads1115_dr_t dr; /**< Data rate configuration */ - ads1115_comp_mode_t comp_mode; /**< Comparator mode */ - ads1115_comp_polarity_t comp_polarity; /**< Comparator polarity */ - ads1115_comp_latch_t comp_latch; /**< Comparator latch */ - ads1115_comp_queue_t comp_queue; /**< Comparator queue */ + i2c_t i2c; /**< I2C device */ + uint8_t addr; /**< I2C address */ + ads1115_mux_t mux; /**< Input multiplexer configuration */ + ads1115_pga_t pga; /**< Programmable gain amplifier configuration */ + ads1115_mode_t mode; /**< Device mode */ + ads1115_dr_t dr; /**< Data rate configuration */ + ads1115_comp_mode_t comp_mode; /**< Comparator mode */ + ads1115_comp_polarity_t comp_polarity; /**< Comparator polarity */ + ads1115_comp_latch_t comp_latch; /**< Comparator latch */ + ads1115_comp_queue_t comp_queue; /**< Comparator queue */ } ads1115_params_t; /** From b4b33ab19ed558b3204ac7145e8ff1faa4b16ba0 Mon Sep 17 00:00:00 2001 From: Baptiste Le Duc Date: Sun, 27 Jul 2025 16:38:38 +0200 Subject: [PATCH 16/16] driver: change missing C++ comments style to C style --- drivers/ads1115/include/ads1115_params.h | 2 +- drivers/include/ads1115.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/ads1115/include/ads1115_params.h b/drivers/ads1115/include/ads1115_params.h index 9d3f70c3f284..9b9f82719469 100644 --- a/drivers/ads1115/include/ads1115_params.h +++ b/drivers/ads1115/include/ads1115_params.h @@ -134,7 +134,7 @@ static const ads1115_params_t ads1115_params[] = { ADS1115_PARAMS }; -/** @} */ // close @ingroup drivers_ads1115 +/** @} */ /* close @ingroup drivers_ads1115 */ #ifdef __cplusplus } diff --git a/drivers/include/ads1115.h b/drivers/include/ads1115.h index eadbaeff31a0..bd6c1f1e3f6d 100644 --- a/drivers/include/ads1115.h +++ b/drivers/include/ads1115.h @@ -84,7 +84,7 @@ typedef enum { ADS1115_PGA_0_512V, /**< FSR = ±0.512V */ ADS1115_PGA_0_256V, /**< FSR = ±0.256V */ - // Aliases (same behavior) + /* Aliases (same behavior) */ ADS1115_PGA_0_256V_B, ADS1115_PGA_0_256V_C } ads1115_pga_t;