Files
Martin JägerandClaude Opus 5.5 82c5d1542d drivers: daq: add data acquisition driver
Add a Zephyr driver for the data acquisition (DAQ), i.e. continuous
sampling of all analog inputs with 1 kHz, low-pass filtering of the
readings and checking them against upper and lower limits. It is based
on the existing implementation in the application.

The driver consists of a common part (filtering, alerts, scaling and
calibration) and backends for the different ADC types:

- STM32 ADCs with a fixed channel sequence (STM32F0, STM32L0 and newly
  supported STM32G0)
- STM32 ADCs with a configurable channel sequence (STM32G4)
- Emulated DAQ for tests on native_sim

Compared to the application implementation, the position of the
readings in the DMA buffer is determined by the driver, so that the
inputs can be specified in any order in the devicetree. The factory
calibration values for the internal reference voltage and temperature
sensor are taken from the STM32 LL headers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 12:16:45 +01:00

343 lines
11 KiB
C

/*
* Copyright (c) The Libre Solar Project Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief DAQ driver for STM32 ADCs with a configurable channel sequence (STM32G4)
*
* The channels can be connected to ADC1 and ADC2. They are converted in the order of the
* devicetree nodes. The reference voltage is generated by the internal reference buffer
* (VREFBUF) with 2.048 V.
*/
#define DT_DRV_COMPAT libresolar_stm32_daq
#include "daq_common.h"
#include "daq_stm32.h"
#include <zephyr/device.h>
#include <zephyr/irq.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <soc.h>
#include <stm32_ll_adc.h>
#include <stm32_ll_bus.h>
#include <stm32_ll_dac.h>
#include <stm32_ll_dma.h>
#include <stm32_ll_system.h>
LOG_MODULE_DECLARE(daq, CONFIG_DAQ_LOG_LEVEL);
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1, "Only one DAQ instance supported");
#define DAQ_NODE DT_DRV_INST(0)
/* priority 2: second-lowest value for STM32L0/F0 */
#define DMA_IRQ_PRIORITY 2
/* reference voltage generated by VREFBUF */
#define VREF_MV 2048
#define ADC_BASE_FN(child) DT_REG_ADDR(DT_IO_CHANNELS_CTLR(child)),
static const uint32_t adc_base[] = { DT_FOREACH_CHILD(DAQ_NODE, ADC_BASE_FN) };
// Depending on the channel, not just a single bit has to be set in the right location. So we need
// a map to get correct bit settings for each channel.
static const uint32_t table_channel[] = {
LL_ADC_CHANNEL_0,
LL_ADC_CHANNEL_1,
LL_ADC_CHANNEL_2,
LL_ADC_CHANNEL_3,
LL_ADC_CHANNEL_4,
LL_ADC_CHANNEL_5,
LL_ADC_CHANNEL_6,
LL_ADC_CHANNEL_7,
LL_ADC_CHANNEL_8,
LL_ADC_CHANNEL_9,
LL_ADC_CHANNEL_10,
LL_ADC_CHANNEL_11,
LL_ADC_CHANNEL_12,
LL_ADC_CHANNEL_13,
LL_ADC_CHANNEL_14,
LL_ADC_CHANNEL_15,
LL_ADC_CHANNEL_TEMPSENSOR_ADC1,
LL_ADC_CHANNEL_VBAT,
LL_ADC_CHANNEL_VREFINT,
};
// Rank and sequence definitions for STM32G4 from adc_stm32.c Zephyr driver
#define RANK(n) LL_ADC_REG_RANK_##n
static const uint32_t table_rank[] = {
RANK(1), RANK(2), RANK(3), RANK(4), RANK(5), RANK(6), RANK(7), RANK(8),
RANK(9), RANK(10), RANK(11), RANK(12), RANK(13), RANK(14), RANK(15), RANK(16),
};
#define SEQ_LEN(n) LL_ADC_REG_SEQ_SCAN_ENABLE_##n##RANKS
static const uint32_t table_seq_len[] = {
LL_ADC_REG_SEQ_SCAN_DISABLE,
SEQ_LEN(2),
SEQ_LEN(3),
SEQ_LEN(4),
SEQ_LEN(5),
SEQ_LEN(6),
SEQ_LEN(7),
SEQ_LEN(8),
SEQ_LEN(9),
SEQ_LEN(10),
SEQ_LEN(11),
SEQ_LEN(12),
SEQ_LEN(13),
SEQ_LEN(14),
SEQ_LEN(15),
SEQ_LEN(16),
};
struct daq_stm32_data
{
struct daq_common_data common;
struct k_timer trigger_timer;
uint8_t num_adc1_ch;
uint8_t num_adc2_ch;
};
DAQ_COMMON_DEFINE(DAQ_NODE);
static const struct daq_common_config daq_stm32_config = {
DAQ_COMMON_CONFIG_INIT(DAQ_NODE),
DAQ_STM32_CAL_CONFIG_INIT(),
};
static struct daq_stm32_data daq_stm32_data = {
.common = { DAQ_COMMON_DATA_INIT(), .vref_mv = VREF_MV },
};
static void vref_setup(void)
{
// output 2.048V at VREF+ pin (also used internally for ADC and DAC)
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
LL_VREFBUF_SetVoltageScaling(LL_VREFBUF_VOLTAGE_SCALE0);
LL_VREFBUF_DisableHIZ();
LL_VREFBUF_Enable();
while (LL_VREFBUF_IsVREFReady() == 0) {
}
}
static void dac_setup(void)
{
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_DAC1);
/* DAC1 at PA4 for bi-directional DC/DC current measurement at 0.5 * VCC */
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_1, LL_DAC_OUTPUT_BUFFER_ENABLE);
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_1);
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_1, 4096 / 2);
/* DAC1 at PA5 for uni-directional PWM and load current measurement at 0.1 * VCC */
LL_DAC_SetOutputBuffer(DAC1, LL_DAC_CHANNEL_2, LL_DAC_OUTPUT_BUFFER_ENABLE);
LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_2);
LL_DAC_ConvertData12RightAligned(DAC1, LL_DAC_CHANNEL_2, 4096 / 10);
}
static void adc_setup(const struct device *dev, ADC_TypeDef *adc)
{
const struct daq_common_config *config = dev->config;
int num_ch = 0;
// ADC clock can be generated from SYSCLK or PLL (async mode) or derived from AHB clock
// (sync mode). For synchronization with a timer, sync mode is preferred.
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_ADC12);
if (LL_ADC_IsEnabled(adc)) {
LL_ADC_Disable(adc);
}
// Use DIV1 only, as DIV2 and DIV4 lead to errors in ADC readings (not sure why...)
LL_ADC_SetCommonClock(__LL_ADC_COMMON_INSTANCE(adc), LL_ADC_CLOCK_SYNC_PCLK_DIV1);
// Prepare for ADC calibration
LL_ADC_DisableDeepPowerDown(adc);
LL_ADC_EnableInternalRegulator(adc);
// Datasheet: wait 20us for regulator to stabilize (taking 100us to be safe)
// LL_ADC_DELAY_INTERNAL_REGUL_STAB_US is erroneously set to 10 in stm32g4xx_ll_adc.h
k_busy_wait(100);
LL_ADC_StartCalibration(adc, LL_ADC_SINGLE_ENDED);
while (LL_ADC_IsCalibrationOnGoing(adc)) {
}
if (LL_ADC_IsActiveFlag_ADRDY(adc)) {
LL_ADC_ClearFlag_ADRDY(adc);
}
// Enable internal reference voltage and temperature
LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(adc),
LL_ADC_PATH_INTERNAL_VREFINT | LL_ADC_PATH_INTERNAL_TEMPSENSOR);
// more complicated sequencer allows to define the sequence independent of the channel number
// (using ranks)
for (int i = 0; i < config->num_channels; i++) {
if (adc_base[i] == (uint32_t)adc) {
uint32_t channel = table_channel[config->adc_channel[i]];
LL_ADC_REG_SetSequencerRanks(adc, table_rank[num_ch], channel);
LL_ADC_SetChannelSamplingTime(adc, channel, LL_ADC_SAMPLINGTIME_247CYCLES_5);
num_ch++;
}
}
LL_ADC_REG_SetSequencerLength(adc, table_seq_len[num_ch - 1]);
LL_ADC_SetDataAlignment(adc, LL_ADC_DATA_ALIGN_LEFT);
LL_ADC_SetResolution(adc, LL_ADC_RESOLUTION_12B);
LL_ADC_REG_SetOverrun(adc, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
// Enable DMA transfer on ADC and circular mode
LL_ADC_REG_SetDMATransfer(adc, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
LL_ADC_Enable(adc);
}
static void process_adc_channels(const struct device *dev, ADC_TypeDef *adc)
{
const struct daq_common_config *config = dev->config;
const struct daq_stm32_data *data = dev->data;
for (int i = 0; i < config->num_channels; i++) {
if (adc_base[i] == (uint32_t)adc) {
daq_common_process(dev, i);
}
}
// ADC2 is sampled after ADC1 (if used)
if (adc == ADC2 || data->num_adc2_ch == 0) {
daq_common_sample_done(dev);
}
}
static void dma1_isr(const void *arg)
{
if ((DMA1->ISR & DMA_ISR_TCIF1) != 0) { // transfer completed on DMA channel 1
process_adc_channels(arg, ADC1);
}
DMA1->IFCR |= 0x0FFFFFFF; // clear all interrupt flags
}
static void dma2_isr(const void *arg)
{
if ((DMA2->ISR & DMA_ISR_TCIF1) != 0) { // transfer completed on DMA channel 1
process_adc_channels(arg, ADC2);
}
DMA2->IFCR |= 0x0FFFFFFF; // clear all interrupt flags
}
// DMA1 is mapped to ADC1 and DMA2 is mapped to ADC2
static void dma_setup(DMA_TypeDef *dma, ADC_TypeDef *adc, volatile uint16_t *buf, uint32_t length)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMAMUX1);
LL_DMA_SetPeriphRequest(dma, LL_DMA_CHANNEL_1,
(dma == DMA1) ? LL_DMAMUX_REQ_ADC1 : LL_DMAMUX_REQ_ADC2);
LL_AHB1_GRP1_EnableClock((dma == DMA1) ? LL_AHB1_GRP1_PERIPH_DMA1 : LL_AHB1_GRP1_PERIPH_DMA2);
LL_DMA_ConfigAddresses(dma, LL_DMA_CHANNEL_1,
LL_ADC_DMA_GetRegAddr(adc, LL_ADC_DMA_REG_REGULAR_DATA), (uint32_t)buf,
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
// Configure the number of DMA transfers (data length in multiples of size per transfer)
LL_DMA_SetDataLength(dma, LL_DMA_CHANNEL_1, length);
LL_DMA_SetMemoryIncMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetMemorySize(dma, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
LL_DMA_SetPeriphSize(dma, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_EnableIT_TE(dma, LL_DMA_CHANNEL_1); // transfer error interrupt
LL_DMA_EnableIT_TC(dma, LL_DMA_CHANNEL_1); // transfer complete interrupt
LL_DMA_SetMode(dma, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
LL_DMA_EnableChannel(dma, LL_DMA_CHANNEL_1);
if (dma == DMA1) {
IRQ_CONNECT(DMA1_Channel1_IRQn, DMA_IRQ_PRIORITY, dma1_isr, DEVICE_DT_INST_GET(0), 0);
irq_enable(DMA1_Channel1_IRQn);
}
else {
IRQ_CONNECT(DMA2_Channel1_IRQn, DMA_IRQ_PRIORITY, dma2_isr, DEVICE_DT_INST_GET(0), 0);
irq_enable(DMA2_Channel1_IRQn);
}
}
static void trigger_conversion(struct k_timer *timer)
{
const struct daq_stm32_data *data = k_timer_user_data_get(timer);
if (data->num_adc1_ch > 0) {
LL_ADC_REG_StartConversion(ADC1);
}
if (data->num_adc2_ch > 0) {
LL_ADC_REG_StartConversion(ADC2);
}
}
static int daq_stm32_init(const struct device *dev)
{
const struct daq_common_config *config = dev->config;
struct daq_stm32_data *data = dev->data;
int err;
// the DMA stores the channels of ADC1 first, followed by the channels of ADC2
for (int i = 0; i < config->num_channels; i++) {
if (config->adc_channel[i] >= ARRAY_SIZE(table_channel)) {
LOG_ERR("Invalid ADC channel %d", config->adc_channel[i]);
return -EINVAL;
}
if (adc_base[i] == (uint32_t)ADC1) {
data->common.channels[i].buf_idx = data->num_adc1_ch++;
}
else if (adc_base[i] == (uint32_t)ADC2) {
data->common.channels[i].buf_idx = data->num_adc2_ch++;
}
else {
LOG_ERR("Only ADC1 and ADC2 are supported");
return -EINVAL;
}
}
for (int i = 0; i < config->num_channels; i++) {
if (adc_base[i] == (uint32_t)ADC2) {
data->common.channels[i].buf_idx += data->num_adc1_ch;
}
}
err = daq_common_init(dev);
if (err != 0) {
return err;
}
vref_setup();
dac_setup();
// configure both ADCs before starting conversions, as they share common settings
if (data->num_adc1_ch > 0) {
adc_setup(dev, ADC1);
}
if (data->num_adc2_ch > 0) {
adc_setup(dev, ADC2);
}
if (data->num_adc1_ch > 0) {
dma_setup(DMA1, ADC1, &data->common.buf[0], data->num_adc1_ch);
LL_ADC_REG_StartConversion(ADC1);
}
if (data->num_adc2_ch > 0) {
dma_setup(DMA2, ADC2, &data->common.buf[data->num_adc1_ch], data->num_adc2_ch);
LL_ADC_REG_StartConversion(ADC2);
}
k_timer_init(&data->trigger_timer, trigger_conversion, NULL);
k_timer_user_data_set(&data->trigger_timer, data);
k_timer_start(&data->trigger_timer, K_MSEC(DAQ_SAMPLE_PERIOD_MS), K_MSEC(DAQ_SAMPLE_PERIOD_MS));
return 0;
}
DEVICE_DT_INST_DEFINE(0, daq_stm32_init, NULL, &daq_stm32_data, &daq_stm32_config, POST_KERNEL,
CONFIG_DAQ_INIT_PRIORITY, NULL);