From b1f9c57fc92b8210c347a28fb1807d887a92542a Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Sun, 12 May 2024 18:37:37 +0200 Subject: [PATCH] fix adc --- esphome/components/adc/adc_sensor.cpp | 14 +++++++++++--- esphome/components/adc/sensor.py | 2 ++ esphome/components/zephyr/__init__.py | 9 ++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/esphome/components/adc/adc_sensor.cpp b/esphome/components/adc/adc_sensor.cpp index 18464940f2..1f04a48aea 100644 --- a/esphome/components/adc/adc_sensor.cpp +++ b/esphome/components/adc/adc_sensor.cpp @@ -424,7 +424,7 @@ std::string ADCSensor::unique_id() { return get_mac_address() + "-adc"; } #ifdef USE_ZEPHYR float ADCSensor::sample() { - uint16_t buf; + int16_t buf = 0; struct adc_sequence sequence = { .buffer = &buf, /* buffer size in bytes, not number of samples */ @@ -432,9 +432,13 @@ float ADCSensor::sample() { }; int32_t val_mv; - adc_sequence_init_dt(adc_channel_, &sequence); + auto err = adc_sequence_init_dt(adc_channel_, &sequence); + if (err < 0) { + ESP_LOGE(TAG, "Could sequence init %s (%d)", adc_channel_->dev->name, err); + return 0.0; + } - auto err = adc_read(adc_channel_->dev, &sequence); + err = adc_read(adc_channel_->dev, &sequence); if (err < 0) { ESP_LOGE(TAG, "Could not read %s (%d)", adc_channel_->dev->name, err); return 0.0; @@ -449,6 +453,10 @@ float ADCSensor::sample() { val_mv = (int32_t) ((int16_t) buf); } else { val_mv = (int32_t) buf; + // https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/0ed4d9ffc674ae407be7cacf5696a02f5e789861/cores/nRF5/wiring_analog_nRF52.c#L222 + if (val_mv < 0) { + val_mv = 0; + } } if (output_raw_) { diff --git a/esphome/components/adc/sensor.py b/esphome/components/adc/sensor.py index e5184802da..d215ed07e5 100644 --- a/esphome/components/adc/sensor.py +++ b/esphome/components/adc/sensor.py @@ -104,6 +104,8 @@ async def to_code(config): adc = cg.new_Pvariable(nrf_saadc, rhs) cg.add(var.set_adc_channel(adc)) gain = "ADC_GAIN_1_6" + if config[CONF_PIN][CONF_NUMBER] == "VDDHDIV5": + gain = "ADC_GAIN_1_2" zephyr_add_user("io-channels", f"<&adc {channel_id}>") zephyr_add_overlay( f""" diff --git a/esphome/components/zephyr/__init__.py b/esphome/components/zephyr/__init__.py index a637f1541c..28301a3d0b 100644 --- a/esphome/components/zephyr/__init__.py +++ b/esphome/components/zephyr/__init__.py @@ -138,6 +138,8 @@ def zephyr_add_cdc_acm(config): # prevent device to go to susspend, without this communication stop working in python # there should be a way to solve it zephyr_add_prj_conf("USB_DEVICE_REMOTE_WAKEUP", False) + # prevent logging when buffer is full + zephyr_add_prj_conf("USB_CDC_ACM_LOG_LEVEL_WRN", True) zephyr_add_overlay( """ &zephyr_udc0 { @@ -163,14 +165,15 @@ def copy_files(): write_file_if_changed(CORE.relative_build_path("zephyr/prj.conf"), prj_conf) - zephyr_add_overlay( - f""" + if CORE.data[KEY_ZEPHYR][KEY_USER]: + zephyr_add_overlay( + f""" / {{ zephyr,user {{ {[f"{key} = {', '.join(value)};" for key, value in CORE.data[KEY_ZEPHYR][KEY_USER].items()][0]} }}; }};""" - ) + ) write_file_if_changed( CORE.relative_build_path("zephyr/app.overlay"),