diff --git a/apps/my_adc/CMakeLists.txt b/apps/my_adc/CMakeLists.txt new file mode 100644 index 0000000..c536643 --- /dev/null +++ b/apps/my_adc/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.20.0) + +# Locate the Zephyr build system as a CMake package +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +# Project name +project(my_adc) + +# Add source files belong to this application +target_sources(app PRIVATE src/main.c) diff --git a/apps/my_adc/boards/nucleo_f767zi.overlay b/apps/my_adc/boards/nucleo_f767zi.overlay new file mode 100644 index 0000000..8fff871 --- /dev/null +++ b/apps/my_adc/boards/nucleo_f767zi.overlay @@ -0,0 +1,24 @@ +#include + +&adc1 { + pinctrl-0 = <&adc1_in3_pa3>; + pinctrl-names = "default"; + status = "okay"; + st,adc-prescaler = <4>; + #address-cells = <1>; + #size-cells = <0>; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <12>; + }; +}; + +/ { + aliases { + myadc = &adc1; + }; +}; diff --git a/apps/my_adc/boards/stm32f4_disco.overlay b/apps/my_adc/boards/stm32f4_disco.overlay new file mode 100644 index 0000000..09a0bbe --- /dev/null +++ b/apps/my_adc/boards/stm32f4_disco.overlay @@ -0,0 +1,30 @@ +#include + +/* PA3 is used by ADC1 channel 3, so keep USART2 as a TX-only console. +&usart2 { + pinctrl-0 = <&usart2_tx_pa2>; + pinctrl-names = "default"; +};*/ + +&adc1 { + pinctrl-0 = <&adc1_in3_pa3>; + pinctrl-names = "default"; + status = "okay"; + st,adc-prescaler = <4>; + #address-cells = <1>; + #size-cells = <0>; + + channel@3 { + reg = <3>; + zephyr,gain = "ADC_GAIN_1"; + zephyr,reference = "ADC_REF_INTERNAL"; + zephyr,acquisition-time = ; + zephyr,resolution = <12>; + }; +}; + +/ { + aliases { + myadc = &adc1; + }; +}; diff --git a/apps/my_adc/prj.conf b/apps/my_adc/prj.conf new file mode 100644 index 0000000..cb21cb7 --- /dev/null +++ b/apps/my_adc/prj.conf @@ -0,0 +1,6 @@ +CONFIG_GPIO=y +CONFIG_SERIAL=y +CONFIG_ADC=y +CONFIG_UART_INTERRUPT_DRIVEN=y +CONFIG_CONSOLE=y +CONFIG_PRINTK=y \ No newline at end of file diff --git a/apps/my_adc/src/main.c b/apps/my_adc/src/main.c new file mode 100644 index 0000000..7355576 --- /dev/null +++ b/apps/my_adc/src/main.c @@ -0,0 +1,75 @@ +#include +#include + +#include +#include +#include +#include + +#define MY_ADC_NODE DT_ALIAS(myadc) +#define MY_ADC_CH_NODE DT_CHILD(MY_ADC_NODE, channel_3) +#define MY_ADC_CHANNEL_ID DT_REG_ADDR(MY_ADC_CH_NODE) + +static const struct device *const adc_dev = + DEVICE_DT_GET(MY_ADC_NODE); + +static const struct adc_channel_cfg ch_cfg = + ADC_CHANNEL_CFG_DT(MY_ADC_CH_NODE); + +static uint16_t sample; + +static struct adc_sequence seq = { + .channels = BIT(MY_ADC_CHANNEL_ID), + .buffer = &sample, + .buffer_size = sizeof(sample), + .resolution = DT_PROP_OR(MY_ADC_CH_NODE, zephyr_resolution, 12), +}; + +int main(void) { + + int err; + + if (!device_is_ready(adc_dev)) { + printk("ADC device not ready\n"); + return 0; + } + + err = adc_channel_setup(adc_dev, &ch_cfg); + if (err < 0) { + printk("Could not setup ADC channel (%d)\n", err); + return 0; + } + + printk("Reading ADC1 channel 3 on PA3 (Arduino A0)\n"); + + while (1) { + int32_t val_mv; + sample = 0; + + err = adc_read(adc_dev, &seq); + if (err < 0) { + printk("Could not read ADC (%d)\n", err); + k_sleep(K_MSEC(500)); + continue; + } + + if (ch_cfg.differential) { + val_mv = (int32_t)((int16_t)sample); + } else { + val_mv = (int32_t)sample; + } + + printk("ADC raw: %" PRId32, val_mv); + err = adc_raw_to_millivolts(adc_ref_internal(adc_dev), ch_cfg.gain, + seq.resolution, &val_mv); + if (err < 0) { + printk(" (voltage conversion not available)\n"); + } else { + printk(" Voltage: %" PRId32 " mV\n", val_mv); + } + + k_sleep(K_MSEC(500)); + } + + return 0; +}