M5Unit-ANADIG 0.4.1 git rev:a449179
Loading...
Searching...
No Matches
unit_ADS11xx.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_ANADIG_UNIT_ADS11XX_HPP
11#define M5_UNIT_ANADIG_UNIT_ADS11XX_HPP
12#include <M5UnitComponent.hpp>
13#include <m5_utility/stl/extension.hpp>
14#include <m5_utility/container/circular_buffer.hpp>
15#include <array>
16#include <limits> // NaN
17
18namespace m5 {
19namespace unit {
20
25namespace ads11xx {
26
31enum class PGA : uint8_t {
32 Gain1,
33 Gain2,
34 Gain4,
35 Gain8,
36};
37
42struct Data {
43 std::array<uint8_t, 2> raw{};
44 uint8_t rate{};
45 PGA pga{};
46 float vdd{2048.f};
47 float factor{1.0f};
48
50 inline int16_t differentialValue() const
51 {
52 return static_cast<int16_t>(m5::types::big_uint16_t(raw[0], raw[1]).get());
53 }
55 inline float differentialVoltage() const
56 {
57 return differentialValue() / (-min_code_table[rate & 0x03] / vdd * (1U << m5::stl::to_underlying(pga))) /
58 factor;
59 }
61 static const int32_t min_code_table[4];
62};
63
64} // namespace ads11xx
65
70class UnitADS11XX : public Component, public PeriodicMeasurementAdapter<UnitADS11XX, ads11xx::Data> {
71 M5_UNIT_COMPONENT_HPP_BUILDER(UnitADS11XX, 0x00);
72
73public:
76 explicit UnitADS11XX(const uint8_t addr = DEFAULT_ADDRESS)
77 : Component(addr), _data{new m5::container::CircularBuffer<ads11xx::Data>(1)}
78 {
79 auto ccfg = component_config();
80 ccfg.clock = 400 * 1000U;
81 component_config(ccfg);
82 }
83 virtual ~UnitADS11XX()
84 {
85 }
86
88 virtual bool begin() override;
90 virtual void update(const bool force = false) override;
91
95 inline int16_t differentialValue() const
96 {
97 return !empty() ? oldest().differentialValue() : 0;
98 }
100 inline float differentialVoltage() const
101 {
102 return !empty() ? oldest().differentialVoltage() : std::numeric_limits<float>::quiet_NaN();
103 }
105
108
113 bool readPGA(ads11xx::PGA& pga);
120 bool writePGA(const ads11xx::PGA pga);
122
131 virtual bool generalReset();
132
133protected:
134 bool start_periodic_measurement(const uint8_t cfg_value);
135 bool start_periodic_measurement();
136 bool stop_periodic_measurement();
137
138 bool measure_singleshot(ads11xx::Data& data, const uint8_t cfg_value);
139 bool measure_singleshot(ads11xx::Data& data);
140
141 bool read_config(uint8_t& v);
142 bool write_config(const uint8_t v);
143 bool read_measurement(uint8_t v[2]);
144 bool is_data_ready();
154 bool detect_data_ready_flag(bool& goes_low);
155
156 virtual bool read_if_ready_in_periodic(uint8_t v[2]);
157 virtual uint32_t get_interval(const uint8_t /* rate */)
158 {
159 return 0;
160 }
161
162 M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(UnitADS11XX, ads11xx::Data);
163
164protected:
165 std::unique_ptr<m5::container::CircularBuffer<ads11xx::Data>> _data{};
166 ads11xx::PGA _pga{};
167 uint8_t _rate{};
168 float _vdd{2048.f};
169 float _factor{1.0f};
170
171 struct Config {
172 inline uint8_t rate() const
173 {
174 return (value >> 2) & 0x03;
175 }
176 inline ads11xx::PGA pga() const
177 {
178 return static_cast<ads11xx::PGA>(value & 0x03);
179 }
180 inline bool continuous() const
181 {
182 return !(value & (1U << 4));
183 }
184 inline bool single() const
185 {
186 return !continuous();
187 }
188 inline bool st() const
189 {
190 // ADS1100 ST/BSY Continuous: Always true, Single: False if data ready
191 // ADS1110 ST/DRDY Continuous/Single: False if data ready
192 return (value & 0x80);
193 }
194
195 inline void rate(const uint8_t rate)
196 {
197 value = (value & ~(0x03 << 2)) | ((rate & 0x03) << 2);
198 }
199 inline void pga(const ads11xx::PGA pga)
200 {
201 value = (value & ~0x03) | m5::stl::to_underlying(pga);
202 }
203 inline void continuous(const bool enable)
204 {
205 value = (value & ~(1U << 4)) | ((enable ? 0 : 1) << 4);
206 }
207 inline void single(const bool enable)
208 {
209 continuous(!enable);
210 }
211 inline void st(const bool b)
212 {
213 value = (value & ~0x80) | (b ? 0x80 : 0x00);
214 }
215 uint8_t value{};
216 };
217};
218} // namespace unit
219} // namespace m5
220#endif
Base class of ADS1100,ADS1110.
Definition unit_ADS11xx.hpp:70
bool detect_data_ready_flag(bool &goes_low)
Checks whether bit 7 of the configuration register goes to 0 in continuous conversion mode.
Definition unit_ADS11xx.cpp:263
bool writePGA(const ads11xx::PGA pga)
Write the PGA.
Definition unit_ADS11xx.cpp:198
float differentialVoltage() const
Oldest measured differential voltage(mV)
Definition unit_ADS11xx.hpp:100
virtual bool generalReset()
General reset.
Definition unit_ADS11xx.cpp:213
UnitADS11XX(const uint8_t addr=DEFAULT_ADDRESS)
Constructor.
Definition unit_ADS11xx.hpp:76
virtual bool begin() override
Begin the unit.
Definition unit_ADS11xx.cpp:42
virtual void update(const bool force=false) override
Update the unit.
Definition unit_ADS11xx.cpp:93
int16_t differentialValue() const
Oldest measured differential value.
Definition unit_ADS11xx.hpp:95
bool readPGA(ads11xx::PGA &pga)
Read the PGA.
Definition unit_ADS11xx.cpp:188
For ADS1100,ADS1110.
Top level namespace of M5Stack.
Unit-related namespace.
Definition unit_ADS11xx.hpp:171
Measurement data group.
Definition unit_ADS11xx.hpp:42
std::array< uint8_t, 2 > raw
Raw.
Definition unit_ADS11xx.hpp:43
float differentialVoltage() const
Gets the differential voltage(mV)
Definition unit_ADS11xx.hpp:55
PGA pga
PGA.
Definition unit_ADS11xx.hpp:45
uint8_t rate
SPS (Value and content depend on derived class)
Definition unit_ADS11xx.hpp:44
static const int32_t min_code_table[4]
Minimum output code for each data rate (index: rate)
Definition unit_ADS11xx.hpp:61
float vdd
VDD(mV)
Definition unit_ADS11xx.hpp:46
int16_t differentialValue() const
Gets the differential value.
Definition unit_ADS11xx.hpp:50
float factor
Correction factor.
Definition unit_ADS11xx.hpp:47
PGA
Programmable Gain Amplifier.
Definition unit_ADS11xx.hpp:31