Skip to content

Commit

Permalink
fix(adc): Add mutex to oneshot adc public APIs (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
finger563 authored Oct 23, 2024
1 parent f1cbaf3 commit 58a8c25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
20 changes: 10 additions & 10 deletions components/adc/include/continuous_adc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,11 @@ class ContinuousAdc : public espp::BaseComponent {

#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
logger_.info("calibration scheme version is {}", "Curve Fitting");
adc_cali_curve_fitting_config_t cali_config = {
.unit_id = unit,
.atten = atten,
.bitwidth = bitwidth,
};
adc_cali_curve_fitting_config_t cali_config;
memset(&cali_config, 0, sizeof(cali_config));
cali_config.unit_id = unit;
cali_config.atten = atten;
cali_config.bitwidth = bitwidth;
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
if (ret == ESP_OK) {
calibrated = true;
Expand All @@ -357,11 +357,11 @@ class ContinuousAdc : public espp::BaseComponent {
// cppcheck-suppress knownConditionTrueFalse
if (!calibrated) {
logger_.info("calibration scheme version is {}", "Line Fitting");
adc_cali_line_fitting_config_t cali_config = {
.unit_id = unit,
.atten = atten,
.bitwidth = bitwidth,
};
adc_cali_line_fitting_config_t cali_config;
memset(&cali_config, 0, sizeof(cali_config));
cali_config.unit_id = unit;
cali_config.atten = atten;
cali_config.bitwidth = bitwidth;
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
if (ret == ESP_OK) {
calibrated = true;
Expand Down
5 changes: 5 additions & 0 deletions components/adc/include/oneshot_adc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <atomic>
#include <mutex>
#include <optional>
#include <unordered_map>

Expand Down Expand Up @@ -76,6 +77,7 @@ class OneshotAdc : public BaseComponent {
std::vector<int> read_all_mv() {
std::vector<int> values;
values.reserve(configs_.size());
std::lock_guard<std::recursive_mutex> lock(mutex_);
for (const auto &config : configs_) {
int raw = 0;
auto err = adc_oneshot_read(adc_handle_, config.channel, &raw);
Expand All @@ -96,6 +98,7 @@ class OneshotAdc : public BaseComponent {
* it was configured).
*/
std::optional<int> read_raw(const AdcConfig &config) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (std::find(configs_.begin(), configs_.end(), config) != configs_.end()) {
int raw;
auto err = adc_oneshot_read(adc_handle_, config.channel, &raw);
Expand All @@ -120,6 +123,7 @@ class OneshotAdc : public BaseComponent {
* (if it was configured).
*/
std::optional<int> read_mv(const AdcConfig &config) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
auto maybe_raw = read_raw(config);
if (maybe_raw.has_value()) {
return raw_to_mv(maybe_raw.value());
Expand Down Expand Up @@ -205,6 +209,7 @@ class OneshotAdc : public BaseComponent {
}
}

std::recursive_mutex mutex_;
std::vector<AdcConfig> configs_;
adc_oneshot_unit_handle_t adc_handle_;
adc_cali_handle_t adc_cali_handle_;
Expand Down

0 comments on commit 58a8c25

Please sign in to comment.