ESPHome 2026.5.0
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::adc {
19
20static const char *const TAG = "adc.rp2040";
21
22void ADCSensor::setup() {
23 static bool initialized = false;
24 if (!initialized) {
25 adc_init();
26 initialized = true;
27 }
28}
29
31 LOG_SENSOR("", "ADC Sensor", this);
32 if (this->is_temperature_) {
33 ESP_LOGCONFIG(TAG, " Pin: Temperature");
34 } else {
35#ifdef USE_ADC_SENSOR_VCC
36 ESP_LOGCONFIG(TAG, " Pin: VCC");
37#else
38 LOG_PIN(" Pin: ", this->pin_);
39#endif // USE_ADC_SENSOR_VCC
40 }
41 ESP_LOGCONFIG(TAG,
42 " Samples: %i\n"
43 " Sampling mode: %s",
44 this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
45 LOG_UPDATE_INTERVAL(this);
46}
47
48float ADCSensor::sample() {
49 uint32_t raw = 0;
50 auto aggr = Aggregator<uint32_t>(this->sampling_mode_);
51
52 if (this->is_temperature_) {
53 adc_set_temp_sensor_enabled(true);
54 delay(1);
55 adc_select_input(4);
56
57 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
58 raw = adc_read();
59 aggr.add_sample(raw);
60 }
61 adc_set_temp_sensor_enabled(false);
62 if (this->output_raw_) {
63 return aggr.aggregate();
64 }
65 return aggr.aggregate() * 3.3f / 4096.0f;
66 }
67
68 uint8_t pin = this->pin_->get_pin();
69#ifdef CYW43_USES_VSYS_PIN
70 if (pin == PICO_VSYS_PIN) {
71 // Measuring VSYS on Raspberry Pico W needs to be wrapped with
72 // `cyw43_thread_enter()`/`cyw43_thread_exit()` as discussed in
73 // https://github.com/raspberrypi/pico-sdk/issues/1222, since Wifi chip and
74 // VSYS ADC both share GPIO29
75 cyw43_thread_enter();
76 }
77#endif // CYW43_USES_VSYS_PIN
78
79 adc_gpio_init(pin);
80 adc_select_input(pin - 26);
81
82 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
83 raw = adc_read();
84 aggr.add_sample(raw);
85 }
86
87#ifdef CYW43_USES_VSYS_PIN
88 if (pin == PICO_VSYS_PIN) {
89 cyw43_thread_exit();
90 }
91#endif // CYW43_USES_VSYS_PIN
92
93 if (this->output_raw_) {
94 return aggr.aggregate();
95 }
96 float coeff = pin == PICO_VSYS_PIN ? 3.0f : 1.0f;
97 return aggr.aggregate() * 3.3f / 4096.0f * coeff;
98}
99
100} // namespace esphome::adc
101
102#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)
void HOT delay(uint32_t ms)
Definition hal.cpp:82
static void uint32_t