ESPHome 2026.3.2
Loading...
Searching...
No Matches
adc_sensor_rp2040.cpp
Go to the documentation of this file.
1#ifdef USE_RP2040
2
3#include "adc_sensor.h"
4#include "esphome/core/log.h"
5
6#ifdef CYW43_USES_VSYS_PIN
7#include "pico/cyw43_arch.h"
8#endif // CYW43_USES_VSYS_PIN
9#include <hardware/adc.h>
10
11// PICO_VSYS_PIN is defined in pico-sdk board headers (e.g. boards/pico2.h),
12// but the Arduino framework's config_autogen.h includes a generic board header
13// that doesn't define it. Provide the standard value (pin 29) as a fallback.
14#ifndef PICO_VSYS_PIN
15#define PICO_VSYS_PIN 29 // NOLINT(cppcoreguidelines-macro-usage)
16#endif
17
18namespace esphome {
19namespace adc {
20
21static const char *const TAG = "adc.rp2040";
22
23void ADCSensor::setup() {
24 static bool initialized = false;
25 if (!initialized) {
26 adc_init();
27 initialized = true;
28 }
29}
30
32 LOG_SENSOR("", "ADC Sensor", this);
33 if (this->is_temperature_) {
34 ESP_LOGCONFIG(TAG, " Pin: Temperature");
35 } else {
36#ifdef USE_ADC_SENSOR_VCC
37 ESP_LOGCONFIG(TAG, " Pin: VCC");
38#else
39 LOG_PIN(" Pin: ", this->pin_);
40#endif // USE_ADC_SENSOR_VCC
41 }
42 ESP_LOGCONFIG(TAG,
43 " Samples: %i\n"
44 " Sampling mode: %s",
45 this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
46 LOG_UPDATE_INTERVAL(this);
47}
48
49float ADCSensor::sample() {
50 uint32_t raw = 0;
51 auto aggr = Aggregator<uint32_t>(this->sampling_mode_);
52
53 if (this->is_temperature_) {
54 adc_set_temp_sensor_enabled(true);
55 delay(1);
56 adc_select_input(4);
57
58 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
59 raw = adc_read();
60 aggr.add_sample(raw);
61 }
62 adc_set_temp_sensor_enabled(false);
63 if (this->output_raw_) {
64 return aggr.aggregate();
65 }
66 return aggr.aggregate() * 3.3f / 4096.0f;
67 }
68
69 uint8_t pin = this->pin_->get_pin();
70#ifdef CYW43_USES_VSYS_PIN
71 if (pin == PICO_VSYS_PIN) {
72 // Measuring VSYS on Raspberry Pico W needs to be wrapped with
73 // `cyw43_thread_enter()`/`cyw43_thread_exit()` as discussed in
74 // https://github.com/raspberrypi/pico-sdk/issues/1222, since Wifi chip and
75 // VSYS ADC both share GPIO29
76 cyw43_thread_enter();
77 }
78#endif // CYW43_USES_VSYS_PIN
79
80 adc_gpio_init(pin);
81 adc_select_input(pin - 26);
82
83 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
84 raw = adc_read();
85 aggr.add_sample(raw);
86 }
87
88#ifdef CYW43_USES_VSYS_PIN
89 if (pin == PICO_VSYS_PIN) {
90 cyw43_thread_exit();
91 }
92#endif // CYW43_USES_VSYS_PIN
93
94 if (this->output_raw_) {
95 return aggr.aggregate();
96 }
97 float coeff = pin == PICO_VSYS_PIN ? 3.0f : 1.0f;
98 return aggr.aggregate() * 3.3f / 4096.0f * coeff;
99}
100
101} // namespace adc
102} // namespace esphome
103
104#endif // USE_RP2040
uint8_t raw[35]
Definition bl0939.h:0
virtual uint8_t get_pin() const =0
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.
InternalGPIOPin * pin_
Definition adc_sensor.h:133
void dump_config() override
Output the configuration details of the ADC sensor for debugging purposes.
SamplingMode sampling_mode_
Definition adc_sensor.h:134
const LogString * sampling_mode_to_str(SamplingMode mode)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void HOT delay(uint32_t ms)
Definition core.cpp:28
static void uint32_t