feat(adc_test): use devicetree for adc configuration
- Use named adc channel 'multisense' from devicetree - Enable adc calibration
This commit is contained in:
parent
a77298b3a6
commit
2cc258e8e2
|
|
@ -0,0 +1,31 @@
|
|||
/ {
|
||||
zephyr,user {
|
||||
io-channels = <&adc1 1>;
|
||||
io-channel-names = "multisense";
|
||||
};
|
||||
};
|
||||
|
||||
&adc1 {
|
||||
#address-cells = <1>;
|
||||
#size-cells = <0>;
|
||||
status = "okay";
|
||||
st,adc-clock-source = "SYNC";
|
||||
st,adc-prescaler = <4>;
|
||||
pinctrl-0 = <&adc1_in1_pa0>;
|
||||
pinctrl-names = "default";
|
||||
|
||||
channel@1 {
|
||||
reg = <1>;
|
||||
zephyr,gain = "ADC_GAIN_1";
|
||||
zephyr,reference = "ADC_REF_INTERNAL";
|
||||
zephyr,acquisition-time = <ADC_ACQ_TIME_MAX>;
|
||||
zephyr,resolution = <12>;
|
||||
zephyr,vref-mv = <3300>;
|
||||
};
|
||||
};
|
||||
|
||||
&pinctrl {
|
||||
adc1_in1_pa0: adc1_in1_pa0 {
|
||||
pinmux = <STM32_PINMUX('A', 0, ANALOG)>;
|
||||
};
|
||||
};
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
CONFIG_ADC=y
|
||||
CONFIG_ADC_STM32=y
|
||||
CONFIG_LOG=y
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2024 Your Name
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/adc.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_REGISTER(adc_test, LOG_LEVEL_DBG);
|
||||
|
||||
#if !DT_NODE_EXISTS(DT_PATH(zephyr_user))
|
||||
#error "zephyr,user node not found"
|
||||
#endif
|
||||
|
||||
static const struct adc_dt_spec adc_channel = ADC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), multisense);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(adc_channel.dev)) {
|
||||
LOG_ERR("ADC device not found: %s", adc_channel.dev->name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = adc_channel_setup_dt(&adc_channel);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Could not setup channel #%d, error %d", adc_channel.channel_id, err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
int16_t buffer[1];
|
||||
struct adc_sequence sequence = {
|
||||
.channels = BIT(adc_channel.channel_id),
|
||||
.buffer = buffer,
|
||||
.buffer_size = sizeof(buffer),
|
||||
.resolution = adc_channel.resolution,
|
||||
.calibrate = true,
|
||||
};
|
||||
|
||||
err = adc_read(adc_channel.dev, &sequence);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Could not read ADC, error %d", err);
|
||||
continue;
|
||||
}
|
||||
|
||||
int32_t millivolts = buffer[0];
|
||||
err = adc_raw_to_millivolts_dt(&adc_channel, &millivolts);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Could not convert to millivolts (%d)", err);
|
||||
continue;
|
||||
}
|
||||
|
||||
LOG_INF("ADC raw: %d, mV: %d", buffer[0], millivolts);
|
||||
|
||||
k_msleep(500);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue