6#include "hal/nrf_saadc.h"
11static const char *
const TAG =
"adc.zephyr";
14 if (!adc_is_ready_dt(this->
channel_)) {
15 ESP_LOGE(TAG,
"ADC controller device %s not ready", this->
channel_->dev->name);
19 auto err = adc_channel_setup_dt(this->
channel_);
21 ESP_LOGE(TAG,
"Could not setup channel %s (%d)", this->
channel_->dev->name, err);
26#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
27static const LogString *gain_to_str(
enum adc_gain
gain) {
30 return LOG_STR(
"1/6");
32 return LOG_STR(
"1/5");
34 return LOG_STR(
"1/4");
36 return LOG_STR(
"1/3");
38 return LOG_STR(
"2/5");
40 return LOG_STR(
"1/2");
42 return LOG_STR(
"2/3");
44 return LOG_STR(
"4/5");
68 return LOG_STR(
"128");
70 return LOG_STR(
"undefined gain");
73static const LogString *reference_to_str(
enum adc_reference reference) {
76 return LOG_STR(
"VDD");
78 return LOG_STR(
"VDD/2");
80 return LOG_STR(
"VDD/3");
82 return LOG_STR(
"VDD/4");
83 case ADC_REF_INTERNAL:
84 return LOG_STR(
"INTERNAL");
85 case ADC_REF_EXTERNAL0:
86 return LOG_STR(
"External, input 0");
87 case ADC_REF_EXTERNAL1:
88 return LOG_STR(
"External, input 1");
90 return LOG_STR(
"undefined reference");
93static const LogString *input_to_str(uint8_t input) {
95 case NRF_SAADC_INPUT_AIN0:
96 return LOG_STR(
"AIN0");
97 case NRF_SAADC_INPUT_AIN1:
98 return LOG_STR(
"AIN1");
99 case NRF_SAADC_INPUT_AIN2:
100 return LOG_STR(
"AIN2");
101 case NRF_SAADC_INPUT_AIN3:
102 return LOG_STR(
"AIN3");
103 case NRF_SAADC_INPUT_AIN4:
104 return LOG_STR(
"AIN4");
105 case NRF_SAADC_INPUT_AIN5:
106 return LOG_STR(
"AIN5");
107 case NRF_SAADC_INPUT_AIN6:
108 return LOG_STR(
"AIN6");
109 case NRF_SAADC_INPUT_AIN7:
110 return LOG_STR(
"AIN7");
111 case NRF_SAADC_INPUT_VDD:
112 return LOG_STR(
"VDD");
113 case NRF_SAADC_INPUT_VDDHDIV5:
114 return LOG_STR(
"VDDHDIV5");
116 return LOG_STR(
"undefined input");
121 LOG_SENSOR(
"",
"ADC Sensor",
this);
122 LOG_PIN(
" Pin: ", this->
pin_);
123#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
130 this->
channel_->dev->name, this->channel_->channel_id, this->channel_->vref_mv, this->channel_->resolution,
131 this->channel_->oversampling);
136 " acquisition_time: %d\n"
138 LOG_STR_ARG(gain_to_str(this->
channel_->channel_cfg.gain)),
139 LOG_STR_ARG(reference_to_str(this->
channel_->channel_cfg.reference)),
140 this->channel_->channel_cfg.acquisition_time, YESNO(this->
channel_->channel_cfg.differential));
141 if (this->
channel_->channel_cfg.differential) {
145 LOG_STR_ARG(input_to_str(this->
channel_->channel_cfg.input_positive)),
146 LOG_STR_ARG(input_to_str(this->
channel_->channel_cfg.input_negative)));
148 ESP_LOGV(TAG,
" Positive: %s", LOG_STR_ARG(input_to_str(this->
channel_->channel_cfg.input_positive)));
152 LOG_UPDATE_INTERVAL(
this);
160 struct adc_sequence sequence = {
163 .buffer_size =
sizeof(buf),
167 err = adc_sequence_init_dt(this->
channel_, &sequence);
169 ESP_LOGE(TAG,
"Could sequence init %s (%d)", this->
channel_->dev->name, err);
173 err = adc_read(this->
channel_->dev, &sequence);
175 ESP_LOGE(TAG,
"Could not read %s (%d)", this->
channel_->dev->name, err);
179 val_raw = (int32_t) buf;
180 if (!this->
channel_->channel_cfg.differential) {
186 aggr.add_sample(val_raw);
189 int32_t val_mv = aggr.aggregate();
195 err = adc_raw_to_millivolts_dt(this->
channel_, &val_mv);
198 ESP_LOGE(TAG,
"Value in mV not available %s (%d)", this->
channel_->dev->name, err);
202 return val_mv / 1000.0f;
float sample() override
Perform a single ADC sampling operation and return the measured value.
void setup() override
Set up the ADC sensor by initializing hardware and calibration parameters.
void dump_config() override
Output the configuration details of the ADC sensor for debugging purposes.
SamplingMode sampling_mode_
Providing packet encoding functions for exchanging data with a remote host.