summaryrefslogtreecommitdiff
path: root/src/driver
diff options
context:
space:
mode:
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/Kconfig4
-rw-r--r--src/driver/ads111x.cc56
2 files changed, 60 insertions, 0 deletions
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);