diff --git a/software/apps/adc_test/boards/weact_stm32g431_core.overlay b/software/apps/adc_test/boards/weact_stm32g431_core.overlay new file mode 100644 index 0000000..ee86360 --- /dev/null +++ b/software/apps/adc_test/boards/weact_stm32g431_core.overlay @@ -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 = ; + zephyr,resolution = <12>; + zephyr,vref-mv = <3300>; + }; +}; + +&pinctrl { + adc1_in1_pa0: adc1_in1_pa0 { + pinmux = ; + }; +}; diff --git a/software/apps/adc_test/prj.conf b/software/apps/adc_test/prj.conf new file mode 100644 index 0000000..57e2bb1 --- /dev/null +++ b/software/apps/adc_test/prj.conf @@ -0,0 +1,3 @@ +CONFIG_ADC=y +CONFIG_ADC_STM32=y +CONFIG_LOG=y diff --git a/software/apps/adc_test/src/main.c b/software/apps/adc_test/src/main.c new file mode 100644 index 0000000..bd57c7c --- /dev/null +++ b/software/apps/adc_test/src/main.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024 Your Name + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +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; +} \ No newline at end of file