summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2023-01-01 18:03:19 +0100
committerDaniel Friesel <derf@finalrewind.org>2023-01-01 18:03:19 +0100
commit7f0db02066a7554b959fa264fcc200b2a2e4c929 (patch)
treea50fad76f732daa8938b0c06393a439f345ee14d
parent2501cd668a26bf6d4e9153f18a6e14695c4b5ed1 (diff)
Add driver for ADS111x 16-Bit ADC
-rw-r--r--Makefile4
-rw-r--r--include/driver/ads111x.h88
-rw-r--r--src/app/datalogger/main.cc13
-rw-r--r--src/driver/Kconfig4
-rw-r--r--src/driver/ads111x.cc56
5 files changed, 165 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index ca3816c..cc8e840 100644
--- a/Makefile
+++ b/Makefile
@@ -46,6 +46,10 @@ ifdef CONFIG_driver_dummy
CXX_TARGETS += src/driver/dummy.cc
endif
+ifdef CONFIG_driver_ads111x
+ CXX_TARGETS += src/driver/ads111x.cc
+endif
+
ifdef CONFIG_driver_lm75
CXX_TARGETS += src/driver/lm75.cc
endif
diff --git a/include/driver/ads111x.h b/include/driver/ads111x.h
new file mode 100644
index 0000000..f25dcde
--- /dev/null
+++ b/include/driver/ads111x.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2023 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Driver for ADS111x 16-Bit ADCs
+ */
+#ifndef ADS111x_H
+#define ADS111x_H
+
+class ADS111x {
+ private:
+ ADS111x(const ADS111x &copy);
+ unsigned char const address;
+ unsigned char fsr_scale;
+
+ unsigned char txbuf[3];
+ unsigned char rxbuf[2];
+
+ unsigned char const P_CONVERSION = 0;
+ unsigned char const P_CONFIG = 1;
+ unsigned char const P_LO_THRESH = 2;
+ unsigned char const P_HI_THRESH = 3;
+
+ public:
+ ADS111x(unsigned char const addr) : address(addr), fsr_scale(8) {}
+
+ unsigned short const START_CONVERSION = 1 << 15;
+ enum conf_mux : unsigned short {
+ DIFFERENTIAL_01 = 0 << 12,
+ DIFFERENTIAL_03 = 1 << 12,
+ DIFFERENTIAL_13 = 2 << 12,
+ DIFFERENTIAL_23 = 3 << 12,
+ SINGLE_0 = 4 << 12,
+ SINGLE_1 = 5 << 12,
+ SINGLE_2 = 6 << 12,
+ SINGLE_3 = 7 << 12
+ };
+ unsigned short const CONF_PGA_MASK = 0b0000111000000000;
+ unsigned short const CONF_PGA_OFFSET = 9;
+ enum conf_pga : unsigned short {
+ FSR_6V = 0 << 9,
+ FSR_4V = 1 << 9,
+ FSR_2V = 2 << 9,
+ FSR_1V = 3 << 9,
+ FSR_0V5 = 4 << 9,
+ FSR_0V25 = 5 << 9
+ };
+ enum conf_mode : unsigned short {
+ CONTINUOUS = 0 << 8,
+ SINGLE_SHORT = 1 << 8,
+ };
+ enum conf_data_rate : unsigned short {
+ SPS_8 = 0 << 5,
+ SPS_16 = 1 << 5,
+ SPS_32 = 2 << 5,
+ SPS_64 = 3 << 5,
+ SPS_128 = 4 << 5,
+ SPS_250 = 5 << 5,
+ SPS_475 = 6 << 5,
+ SPS_860 = 7 << 5
+ };
+ enum conf_comp_mode : unsigned short {
+ TRADITIONAL = 0 << 4,
+ WINDOW = 1 << 4
+ };
+ enum conf_comp_pol : unsigned short {
+ ACTIVE_LOW = 0 << 3,
+ ACTIVE_HIGH = 1 << 3,
+ };
+ enum conf_comp_lat : unsigned short {
+ NONLATCHING = 0 << 2,
+ LATCHING = 1 << 2
+ };
+ enum conf_comp_queue : unsigned short {
+ ONE_CONVERSON = 0,
+ TWO_CONVERSIONS,
+ FOUR_CONVERSIONS,
+ DISABLED
+ };
+
+ void configure(unsigned short config);
+ float readVoltage();
+};
+
+extern ADS111x ads111x;
+
+#endif
diff --git a/src/app/datalogger/main.cc b/src/app/datalogger/main.cc
index 251d1fa..1d41ee0 100644
--- a/src/app/datalogger/main.cc
+++ b/src/app/datalogger/main.cc
@@ -13,6 +13,9 @@
#include "driver/soft_i2c.h"
#endif
+#ifdef CONFIG_driver_ads111x
+#include "driver/ads111x.h"
+#endif
#ifdef CONFIG_driver_lm75
#include "driver/lm75.h"
#endif
@@ -63,6 +66,12 @@
void loop(void)
{
+#ifdef CONFIG_driver_ads111x
+ kout << "Reading: ";
+ kout.printf_float(ads111x.readVoltage());
+ kout << " V" << endl;
+#endif
+
#ifdef CONFIG_driver_lm75
kout << "temperature_celsius: ";
kout.printf_float(lm75.getTemp());
@@ -284,6 +293,10 @@ int main(void)
kout << "I2C setup OK" << endl;
#endif
+#ifdef CONFIG_driver_ads111x
+ ads111x.configure(ADS111x::CONTINUOUS | ADS111x::FSR_4V | ADS111x::SPS_8 | ADS111x::SINGLE_3);
+#endif
+
#ifdef CONFIG_driver_bme280
bme280.intf = BME280_I2C_INTF;
bme280.read = bme680_i2c_read;
diff --git a/src/driver/Kconfig b/src/driver/Kconfig
index 581ef10..163671f 100644
--- a/src/driver/Kconfig
+++ b/src/driver/Kconfig
@@ -25,6 +25,10 @@ bool
menu "Drivers"
+config driver_ads111x
+bool "ADS111x 16-bit ADC"
+depends on meta_driver_i2c
+
config driver_am2320
bool "AM2320 TH Sensor"
depends on meta_driver_i2c
diff --git a/src/driver/ads111x.cc b/src/driver/ads111x.cc
new file mode 100644
index 0000000..53be2a4
--- /dev/null
+++ b/src/driver/ads111x.cc
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2023 Daniel Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Driver for ADS111x 16-Bit ADCs
+ */
+#include "driver/ads111x.h"
+#if defined(CONFIG_meta_driver_hardware_i2c)
+#include "driver/i2c.h"
+#elif defined(CONFIG_driver_softi2c)
+#include "driver/soft_i2c.h"
+#endif
+
+#include <stdint.h>
+
+void ADS111x::configure(unsigned short config)
+{
+ txbuf[0] = P_CONFIG;
+ txbuf[1] = config >> 8;
+ txbuf[2] = config & 0x00ff;
+ i2c.xmit(address, 3, txbuf, 0, rxbuf);
+
+ uint8_t pga_config = (config & CONF_PGA_MASK) >> CONF_PGA_OFFSET;
+ switch (pga_config) {
+ case 0:
+ fsr_scale = 24;
+ break;
+ case 1:
+ fsr_scale = 16;
+ break;
+ case 2:
+ fsr_scale = 8;
+ break;
+ case 3:
+ fsr_scale = 4;
+ break;
+ case 4:
+ fsr_scale = 2;
+ break;
+ default:
+ fsr_scale = 1;
+ }
+}
+
+float ADS111x::readVoltage()
+{
+ txbuf[0] = P_CONVERSION;
+ i2c.xmit(address, 1, txbuf, 2, rxbuf);
+
+ int16_t intermediate = ((int8_t)rxbuf[0]) * 256 + (uint8_t) rxbuf[1];
+
+ return intermediate * 0.256 / 0x7fff * fsr_scale;
+}
+
+ADS111x ads111x(0x48);