55 : unit_(unit),
56 num_channels_(num_channels),
57 attenuation_(attenuation),
58 bitwidth_(ResolveBitwidth(bitwidth)),
59 reference_voltage_(reference_voltage),
60 max_raw_((1U << static_cast<uint8_t>(bitwidth_)) - 1U)
61{
62 ASSERT(channels != nullptr);
63 ASSERT(num_channels_ > 0U);
64 ASSERT(reference_voltage_ > 0.0f);
65 ASSERT(max_raw_ != 0U);
66 ASSERT(num_channels_ <= SOC_ADC_MAX_CHANNEL_NUM);
67 if ((channels == nullptr) || (num_channels_ == 0U) || (reference_voltage_ <= 0.0f) ||
68 (max_raw_ == 0U) || (num_channels_ > SOC_ADC_MAX_CHANNEL_NUM))
69 {
70 return;
71 }
72
73 channels_ = new Channel[num_channels_];
74 channel_ids_ = new adc_channel_t[num_channels_];
75 channel_ready_ = new bool[num_channels_];
76 latest_values_ = new float[num_channels_];
77 latest_raw_ = new uint16_t[num_channels_];
78
79 for (uint8_t i = 0; i < SOC_ADC_MAX_CHANNEL_NUM; ++i)
80 {
81 channel_idx_map_[i] = INVALID_CHANNEL_IDX;
82 cali_handles_[i] = nullptr;
83 }
84
85 for (uint8_t i = 0; i < num_channels_; ++i)
86 {
87 ASSERT(IsValidChannel(channels[i]));
88 const uint8_t ch = static_cast<uint8_t>(channels[i]);
89 ASSERT(channel_idx_map_[ch] == INVALID_CHANNEL_IDX);
90 if (!IsValidChannel(channels[i]) || (channel_idx_map_[ch] != INVALID_CHANNEL_IDX))
91 {
92 return;
93 }
94
95 channels_[i] = Channel(this, i, ch);
96 channel_ids_[i] = channels[i];
97 channel_idx_map_[ch] = i;
98 channel_ready_[i] = false;
99 latest_values_[i] = 0.0f;
100 latest_raw_[i] = 0U;
101 }
102
103#if defined(CONFIG_IDF_TARGET_ESP32) && CONFIG_IDF_TARGET_ESP32 && \
104 defined(CONFIG_ESP_WIFI_ENABLED) && CONFIG_ESP_WIFI_ENABLED
105 if (unit_ == ADC_UNIT_2)
106 {
107 wifi_mode_t wifi_mode = WIFI_MODE_NULL;
108 const esp_err_t wifi_mode_err = esp_wifi_get_mode(&wifi_mode);
109 const bool wifi_active = (wifi_mode_err == ESP_OK) && (wifi_mode != WIFI_MODE_NULL);
110 ASSERT(!wifi_active);
111 if (wifi_active)
112 {
113 return;
114 }
115 }
116#endif
117
118 (void)InitCalibration();
119
120 const ContinuousInitResult cont_ans = InitContinuous(freq, dma_buf_size);
121 if (cont_ans == ContinuousInitResult::STARTED)
122 {
123 valid_ = true;
124 return;
125 }
126
127 if (cont_ans == ContinuousInitResult::FAILED)
128 {
129 ASSERT(false);
130 return;
131 }
132
133 const bool oneshot_ok = InitOneshot();
134 ASSERT(oneshot_ok);
135 if (!oneshot_ok)
136 {
137 return;
138 }
139
140 valid_ = true;
141}