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