Add my_adc/

This commit is contained in:
hangpersonal committed 2026-10-07 22:36:07 -07:00
1 parent ff59d1588c
commit 1deb818d3e
5 files changed
+145

No files matched your search

+10
View File
@@ -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)
+24
View File
@@ -0,0 +1,24 @@
#include <zephyr/dt-bindings/adc/adc.h>
&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 = <ADC_ACQ_TIME_DEFAULT>;
zephyr,resolution = <12>;
};
};
/ {
aliases {
myadc = &adc1;
};
};
+30
View File
@@ -0,0 +1,30 @@
#include <zephyr/dt-bindings/adc/adc.h>
/* 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 = <ADC_ACQ_TIME_DEFAULT>;
zephyr,resolution = <12>;
};
};
/ {
aliases {
myadc = &adc1;
};
};
+6
View File
@@ -0,0 +1,6 @@
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_ADC=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_CONSOLE=y
CONFIG_PRINTK=y
+75
View File
@@ -0,0 +1,75 @@
#include <inttypes.h>
#include <stdint.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/sys/printk.h>
#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;
}