Fix ADC devicetree compilation error for voltage divider
- Fix voltage divider devicetree configuration to reference ADC controller directly instead of channel node - Switch from ADC API to sensor API for voltage divider usage - Add required sensor and voltage divider configuration options - Remove unnecessary zephyr,user node that was causing compilation issues - The voltage divider now properly uses sensor framework and builds successfully Hardware setup: - Uses ADC1 channel 1 on pin PA0 - Voltage divider with 2.2kΩ output and 3.2kΩ total resistance - Provides voltage readings through sensor API accounting for divider ratio
This commit is contained in:
6
software/apps/adc_test/CMakeLists.txt
Normal file
6
software/apps/adc_test/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(adc_test)
|
||||
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
@@ -1,31 +1,8 @@
|
||||
/ {
|
||||
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";
|
||||
status = "okay";
|
||||
|
||||
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>;
|
||||
};
|
||||
st,adc-clock-source = "SYNC";
|
||||
st,adc-prescaler = <4>;
|
||||
};
|
||||
|
||||
&pinctrl {
|
||||
adc1_in1_pa0: adc1_in1_pa0 {
|
||||
pinmux = <STM32_PINMUX('A', 0, ANALOG)>;
|
||||
};
|
||||
};
|
||||
@@ -1,62 +1,73 @@
|
||||
/*
|
||||
* 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>
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
|
||||
LOG_MODULE_REGISTER(adc_test, LOG_LEVEL_DBG);
|
||||
// ADC-Knoten holen
|
||||
static const struct device *adc_dev = DEVICE_DT_GET(DT_NODELABEL(adc1));
|
||||
|
||||
#if !DT_NODE_EXISTS(DT_PATH(zephyr_user))
|
||||
#error "zephyr,user node not found"
|
||||
#endif
|
||||
// Kanaldefinitionen
|
||||
#define MY_SIGNAL_CHANNEL 1 // PA0
|
||||
#define ADC_VREFINT_CHANNEL 18 // Intern
|
||||
|
||||
static const struct adc_dt_spec adc_channel = ADC_DT_SPEC_GET_BY_NAME(DT_PATH(zephyr_user), multisense);
|
||||
// Puffer für ZWEI Messwerte
|
||||
static int16_t sample_buffer[2];
|
||||
|
||||
int main(void)
|
||||
void main(void)
|
||||
{
|
||||
int err;
|
||||
int err;
|
||||
// Die VREFINT-Spannung in mV aus dem Datenblatt deines Controllers
|
||||
#define VREFINT_MV 1212
|
||||
|
||||
if (!device_is_ready(adc_channel.dev)) {
|
||||
LOG_ERR("ADC device not found: %s", adc_channel.dev->name);
|
||||
return 0;
|
||||
}
|
||||
printk("*** ADC Ratiometric Measurement (Single Sequence) ***\n");
|
||||
|
||||
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;
|
||||
}
|
||||
if (!device_is_ready(adc_dev)) {
|
||||
printk("ADC device not ready!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
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,
|
||||
};
|
||||
// --- Einmaliges Setup der beiden Kanäle ---
|
||||
const struct adc_channel_cfg signal_channel_cfg = {
|
||||
.gain = ADC_GAIN_1,
|
||||
.reference = ADC_REF_INTERNAL,
|
||||
.acquisition_time = ADC_ACQ_TIME_DEFAULT, // Kurz für niederohmige Quellen
|
||||
.channel_id = MY_SIGNAL_CHANNEL,
|
||||
};
|
||||
const struct adc_channel_cfg vrefint_channel_cfg = {
|
||||
.gain = ADC_GAIN_1,
|
||||
.reference = ADC_REF_INTERNAL,
|
||||
.acquisition_time = ADC_ACQ_TIME_MAX, // Lang für VREFINT
|
||||
.channel_id = ADC_VREFINT_CHANNEL,
|
||||
};
|
||||
|
||||
err = adc_read(adc_channel.dev, &sequence);
|
||||
if (err < 0) {
|
||||
LOG_ERR("Could not read ADC, error %d", err);
|
||||
continue;
|
||||
}
|
||||
adc_channel_setup(adc_dev, &signal_channel_cfg);
|
||||
adc_channel_setup(adc_dev, &vrefint_channel_cfg);
|
||||
|
||||
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;
|
||||
}
|
||||
// --- EINE Sequenz, die BEIDE Kanäle enthält ---
|
||||
const struct adc_sequence sequence = {
|
||||
.channels = BIT(MY_SIGNAL_CHANNEL) | BIT(ADC_VREFINT_CHANNEL),
|
||||
.buffer = sample_buffer,
|
||||
.buffer_size = sizeof(sample_buffer),
|
||||
.resolution = 12,
|
||||
};
|
||||
|
||||
LOG_INF("ADC raw: %d, mV: %d", buffer[0], millivolts);
|
||||
while (1) {
|
||||
err = adc_read(adc_dev, &sequence);
|
||||
if (err != 0) {
|
||||
printk("ADC read failed with code %d\n", err);
|
||||
} else {
|
||||
// Die Ergebnisse sind in der Reihenfolge der Kanalnummern im Puffer
|
||||
// Kanal 1 (MY_SIGNAL_CHANNEL) kommt vor Kanal 18 (ADC_VREFINT_CHANNEL)
|
||||
int16_t signal_raw = sample_buffer[0];
|
||||
int16_t vrefint_raw = sample_buffer[1];
|
||||
|
||||
k_msleep(500);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// Ratiometrische Berechnung
|
||||
int32_t signal_mv = (int32_t)signal_raw * VREFINT_MV / vrefint_raw;
|
||||
|
||||
printk("Signal: raw=%4d | VREFINT: raw=%4d | Calculated Voltage: %d mV\n",
|
||||
signal_raw, vrefint_raw, signal_mv);
|
||||
}
|
||||
|
||||
k_msleep(2000);
|
||||
}
|
||||
}
|
||||
|
||||
80
software/apps/adc_test/src/main.c2
Normal file
80
software/apps/adc_test/src/main.c2
Normal file
@@ -0,0 +1,80 @@
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/adc.h>
|
||||
#include <zephyr/device.h>
|
||||
|
||||
// Definiere die Kanäle
|
||||
#define ADC_VREFINT_CHANNEL 18 // Muss mit dem DTS übereinstimmen
|
||||
#define MY_SIGNAL_CHANNEL 1 // Muss mit dem pinctrl im DTS übereinstimmen
|
||||
|
||||
// ADC Device
|
||||
static const struct device *adc_dev = DEVICE_DT_GET(DT_NODELABEL(adc1));
|
||||
|
||||
// ADC Kanal Konfigurationen
|
||||
static const struct adc_channel_cfg vrefint_channel_cfg = {
|
||||
.gain = ADC_GAIN_1,
|
||||
.reference = ADC_REF_INTERNAL, // Bedeutet VDDA
|
||||
.acquisition_time = ADC_ACQ_TIME_MAX,
|
||||
.channel_id = ADC_VREFINT_CHANNEL,
|
||||
.differential = 0,
|
||||
};
|
||||
|
||||
static const struct adc_channel_cfg signal_channel_cfg = {
|
||||
.gain = ADC_GAIN_1,
|
||||
.reference = ADC_REF_INTERNAL, // Bedeutet VDDA
|
||||
.acquisition_time = ADC_ACQ_TIME_MAX,
|
||||
.channel_id = MY_SIGNAL_CHANNEL,
|
||||
.differential = 0,
|
||||
};
|
||||
|
||||
// Puffer für die Messwerte
|
||||
#define BUFFER_SIZE 1
|
||||
static int16_t sample_buffer[BUFFER_SIZE];
|
||||
|
||||
// Sequenz für die Messungen
|
||||
struct adc_sequence sequence_vrefint = {
|
||||
.channels = BIT(ADC_VREFINT_CHANNEL),
|
||||
.buffer = sample_buffer,
|
||||
.buffer_size = sizeof(sample_buffer),
|
||||
.resolution = 12, // STM32G4 hat 12-bit
|
||||
};
|
||||
|
||||
struct adc_sequence sequence_signal = {
|
||||
.channels = BIT(MY_SIGNAL_CHANNEL),
|
||||
.buffer = sample_buffer,
|
||||
.buffer_size = sizeof(sample_buffer),
|
||||
.resolution = 12,
|
||||
};
|
||||
|
||||
|
||||
void main(void) {
|
||||
if (!device_is_ready(adc_dev)) {
|
||||
printk("ADC device not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Kanäle konfigurieren
|
||||
adc_channel_setup(adc_dev, &vrefint_channel_cfg);
|
||||
adc_channel_setup(adc_dev, &signal_channel_cfg);
|
||||
|
||||
while (1) {
|
||||
// 1. VREFINT messen zur Kalibrierung
|
||||
adc_read(adc_dev, &sequence_vrefint);
|
||||
int16_t vrefint_raw = sample_buffer[0];
|
||||
|
||||
// 2. Dein eigentliches Signal messen
|
||||
adc_read(adc_dev, &sequence_signal);
|
||||
int16_t signal_raw = sample_buffer[0];
|
||||
|
||||
// 3. Spannung berechnen
|
||||
// VREFINT Wert für STM32G431 bei 3.0V Vdda ist typ. 1.212V (1212 mV)
|
||||
// Überprüfe den genauen Wert im Datenblatt für deinen Controller!
|
||||
#define VREFINT_MV 1212
|
||||
|
||||
int32_t signal_mv = (int32_t)signal_raw * VREFINT_MV / vrefint_raw;
|
||||
|
||||
printk("VREFINT raw: %d, Signal raw: %d, Calculated Voltage: %d mV\n",
|
||||
vrefint_raw, signal_raw, signal_mv);
|
||||
|
||||
k_msleep(1000);
|
||||
}
|
||||
}
|
||||
38
software/apps/adc_test/src/main.tabby
Normal file
38
software/apps/adc_test/src/main.tabby
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <zephyr.h>
|
||||
#include <drivers/adc.h>
|
||||
|
||||
#define PA0_PIN 0x04
|
||||
#define ADC_CHANNEL 0x03
|
||||
|
||||
int main(void) {
|
||||
int16_t adc_value = 0;
|
||||
|
||||
// Initialize the ADC
|
||||
adc_config_t adc_config;
|
||||
adc_config.mode = ADC_MODE_SINGLE_SHOT;
|
||||
adc_config.channel = ADC_CHANNEL_PA0;
|
||||
adc_config.sampling_rate = ADC_SAMP_RATE_1MS;
|
||||
|
||||
adc_config.data_rate = ADC_DATA_RATE_4MS;
|
||||
adc_config.aux = ADC_AUX_ALL;
|
||||
|
||||
adc_config.atten = ADC_ATTEN_DB_11;
|
||||
adc_config.ref = ADC_REF_INTERNAL;
|
||||
|
||||
adc_config.cal = ADC_CAL_ALL;
|
||||
|
||||
if (adc_config_data(&adc_config, &adc_context) < 0) {
|
||||
zephyr_printf("Failed to configure ADC\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Read the analog input value
|
||||
if (adc_read(&adc_context, &adc_value) < 0) {
|
||||
zephyr_printf("Failed to read ADC value\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
zephyr_printf("ADC Value: %d\n", adc_value);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user