This commit is contained in:
Tomasz Duda 2024-05-12 18:37:37 +02:00
parent 05b97be419
commit b1f9c57fc9
3 changed files with 19 additions and 6 deletions

View File

@ -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_) {

View File

@ -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"""

View File

@ -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"),