ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ads131m0x.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <array>
6#include <atomic>
7#include <cstdint>
8
9#include <freertos/FreeRTOS.h>
10#include <freertos/task.h>
11
14#include "esphome/core/hal.h"
16
17namespace esphome::ads131m0x {
18
19static constexpr uint8_t MAX_CHANNELS = 8;
20
26
28struct Snapshot {
29 std::array<float, MAX_CHANNELS> rms{};
30 std::array<float, MAX_CHANNELS> mean{};
33};
34
38class ADS131M0x final : public Component,
39 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
40 spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_8MHZ> {
41 public:
42 void setup() override;
43 void dump_config() override;
44
45 void set_num_channels(uint8_t num_channels) { this->num_channels_ = num_channels; }
46 void set_drdy_pin(InternalGPIOPin *pin) { this->drdy_pin_ = pin; }
47 void set_reset_pin(InternalGPIOPin *pin) { this->reset_pin_ = pin; }
49 void set_osr_code(uint8_t code) { this->osr_code_ = code; }
50 void set_sample_rate(float sample_rate) { this->sample_rate_ = sample_rate; }
51 void set_window_ms(uint32_t window_ms) { this->window_ms_ = window_ms; }
52 void set_gain(uint8_t channel, uint8_t gain_code) { this->gain_codes_[channel] = gain_code; }
53
58 bool is_healthy() const { return this->healthy_.load() != 0; }
59 uint32_t get_window_ms() const { return this->window_ms_; }
61 float get_full_scale(uint8_t channel) const;
62
63 protected:
64 static void task_entry(void *arg);
65 static void drdy_isr(ADS131M0x *self);
66 void task_loop_();
67 void check_health_();
69 void check_rate_(uint32_t elapsed_ms, uint32_t frames);
70
72 const LogString *configure_();
74 bool transfer_frame_(uint16_t command, uint16_t data, uint16_t *response, int32_t *samples);
75 bool read_register_(uint8_t address, uint16_t *value);
76 bool write_register_(uint8_t address, uint16_t value);
77 size_t frame_bytes_() const;
79 void drain_fifo_();
82
85 uint8_t num_channels_{MAX_CHANNELS};
87 uint8_t osr_code_{3};
88 float sample_rate_{4000.0f};
90 std::array<uint8_t, MAX_CHANNELS> gain_codes_{};
91
93 portMUX_TYPE lock_ = portMUX_INITIALIZER_UNLOCKED;
95 uint8_t *frame_buf_{nullptr};
96 size_t transfer_bytes_{0};
97
98 // Sampling task only
100
101 // Written by the sampling task, read by the main loop
102 std::atomic<uint8_t> healthy_{0};
103 std::atomic<uint8_t> configured_{0};
104 std::atomic<uint32_t> frames_{0};
105 std::atomic<uint32_t> crc_errors_{0};
106 std::atomic<uint32_t> device_resets_{0};
107
108 // Main loop only
115 uint8_t rate_checks_{0};
116 bool rate_degraded_{false};
117};
118
119} // namespace esphome::ads131m0x
120
121#endif // USE_ESP32
Helper for FreeRTOS static task management.
Definition static_task.h:18
TI ADS131M02/04/08 simultaneous-sampling 24-bit delta-sigma ADC (https://www.ti.com/lit/ds/symlink/ad...
Definition ads131m0x.h:40
void drain_fifo_()
Reads both FIFO slots back to back, as the datasheet requires after a gap in reading.
const LogString * configure_()
Resets (if a reset pin is set) and configures the device. Returns nullptr on success,...
void set_gain(uint8_t channel, uint8_t gain_code)
Definition ads131m0x.h:52
std::atomic< uint32_t > device_resets_
Definition ads131m0x.h:106
void check_rate_(uint32_t elapsed_ms, uint32_t frames)
Averages the sample rate over several health checks and warns when conversions are being lost.
void set_window_ms(uint32_t window_ms)
Definition ads131m0x.h:51
InternalGPIOPin * reset_pin_
Definition ads131m0x.h:84
static void task_entry(void *arg)
uint32_t get_window_ms() const
Definition ads131m0x.h:59
uint8_t * frame_buf_
DMA-capable, allocated once in setup()
Definition ads131m0x.h:95
void invalidate_snapshot_()
Marks the snapshot as invalid (sequence 0) until a full window has been sampled again.
void set_osr_code(uint8_t code)
Definition ads131m0x.h:49
bool transfer_frame_(uint16_t command, uint16_t data, uint16_t *response, int32_t *samples)
Clocks one full frame. Returns false on an output CRC mismatch. response receives the first word.
std::atomic< uint8_t > healthy_
Definition ads131m0x.h:102
ADS131M0xPowerMode power_mode_
Definition ads131m0x.h:86
InternalGPIOPin * drdy_pin_
Definition ads131m0x.h:83
void set_sample_rate(float sample_rate)
Definition ads131m0x.h:50
size_t transfer_bytes_
Frame length, padded where the DMA requires it.
Definition ads131m0x.h:96
float get_full_scale(uint8_t channel) const
Input full scale of a channel in volts (the input range is +/- this value).
std::atomic< uint32_t > crc_errors_
Definition ads131m0x.h:105
void set_reset_pin(InternalGPIOPin *pin)
Definition ads131m0x.h:47
bool write_register_(uint8_t address, uint16_t value)
static void drdy_isr(ADS131M0x *self)
void set_drdy_pin(InternalGPIOPin *pin)
Definition ads131m0x.h:46
void set_power_mode(ADS131M0xPowerMode power_mode)
Definition ads131m0x.h:48
std::atomic< uint8_t > configured_
Definition ads131m0x.h:103
Snapshot get_snapshot()
Copy of the latest completed window; sequence is 0 until the first window after (re)configuration.
std::array< uint8_t, MAX_CHANNELS > gain_codes_
Definition ads131m0x.h:90
void set_num_channels(uint8_t num_channels)
Definition ads131m0x.h:45
bool read_register_(uint8_t address, uint16_t *value)
bool is_healthy() const
True while the device is configured and delivers samples at the expected rate.
Definition ads131m0x.h:58
std::atomic< uint32_t > frames_
Definition ads131m0x.h:104
The SPIDevice is what components using the SPI will create.
Definition spi.h:444
PowerMode power_mode
Definition msa3xx.h:3
@ ADS131M0X_POWER_MODE_VERY_LOW_POWER
Definition ads131m0x.h:22
@ ADS131M0X_POWER_MODE_HIGH_RESOLUTION
Definition ads131m0x.h:24
@ ADS131M0X_POWER_MODE_LOW_POWER
Definition ads131m0x.h:23
uint32_t value
Definition hal.cpp:59
static void uint32_t
Result of one measurement window, in volts at the ADC input.
Definition ads131m0x.h:28
uint32_t sequence
Incremented with every completed window; 0 = no valid data.
Definition ads131m0x.h:31
std::array< float, MAX_CHANNELS > mean
Mean (DC) value.
Definition ads131m0x.h:30
std::array< float, MAX_CHANNELS > rms
RMS of the AC component (DC offset removed)
Definition ads131m0x.h:29
uint32_t timestamp_ms
millis() at the end of the window
Definition ads131m0x.h:32