mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-08-18 11:53:05 +08:00
feat[ADC]: 新增 ADC V2 驱动支持 (#11440)
* feat[ADC V2]: add ADC V2 driver support add ADC V2 core APIs, session state management, and sequence read support add STM32 ADC V2 HAL backend with internal-channel and VREF handling add per-series STM32 ADC V2 default configuration headers wire ADC V2 Kconfig and SConscript integration for components and STM32 BSP drivers keep ADC V2 mutually exclusive with the legacy ADC driver path * feat[ADC V2]: add ADC V2 MSH special channel read support add generic ADC V2 MSH commands for probing, configuring, raw reads, voltage reads, and sequence reads add STM32 ADC V2 backend special commands for VREFINT, temperature sensor, and VBAT reads add STM32 helper APIs for special logical channels, sampling time, resolution, and temperature calculation wire ADC V2 MSH sources through Kconfig and SConscript * feat[ADC V2]: add ADC V2 stream DMA support add ADC V2 stream APIs with latest-frame and FIFO buffering policies wire STM32 ADC V2 to circular DMA stream mode with per-instance Kconfig options extend STM32 ADC config headers and DMA helpers for stream DMA configuration add FinSH stream commands for start, read, cancel, and stop operations handle Cortex-M7 cache-safe DMA buffers for STM32 stream sampling * feat[ADC V2]: add timer trigger support for ADC V2 streams add ADC V2 trigger configuration, validation, and lifecycle coordination add timer update trigger support through clock timer TRGO controls map STM32 ADC V2 timer update triggers to HAL external trigger selector fields add Kconfig, SConscript, and MSH entries for ADC trigger setup and inspection * feat[ADCV2]: add timer and comparator trigger selectors add ADC V2 timer compare and analog comparator trigger configuration support extend clock timer trigger config with compare event and channel fields wire STM32 ADC external trigger selector mappings for TIM update, TIM compare, and COMP output add FinSH trigger_set commands and Kconfig switches for timer and comparator trigger backends * fix[ADC V2]: resolve ADC V1 compatibility Kconfig loop * ci[ADC V2][stm32]: add ADC v2 peripheral config for stm32f407-rt-spark BSP
This commit is contained in:
@@ -5,6 +5,7 @@ rsource "ipc/Kconfig"
|
||||
|
||||
rsource "serial/Kconfig"
|
||||
rsource "can/Kconfig"
|
||||
rsource "adc/Kconfig"
|
||||
rsource "clock_time/Kconfig"
|
||||
rsource "i2c/Kconfig"
|
||||
rsource "phy/Kconfig"
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
menuconfig RT_USING_ADC
|
||||
bool "Using ADC device drivers"
|
||||
default n
|
||||
|
||||
if RT_USING_ADC
|
||||
config RT_USING_ADC_V2
|
||||
bool "Using ADC V2 device drivers"
|
||||
default n
|
||||
help
|
||||
Enable the standalone ADC V2 framework. ADC V2 uses the legacy ADC
|
||||
public namespace, so it cannot be enabled together with ADC V1.
|
||||
|
||||
config RT_USING_ADC_V1
|
||||
bool
|
||||
default y if !RT_USING_ADC_V2
|
||||
|
||||
if RT_USING_DM && RT_USING_ADC_V1
|
||||
osource "$(SOC_DM_ADC_DIR)/Kconfig"
|
||||
endif
|
||||
|
||||
if RT_USING_ADC_V2
|
||||
|
||||
|
||||
config RT_ADC_USING_TRIGGER
|
||||
bool "Enable ADC trigger framework"
|
||||
default n
|
||||
help
|
||||
Enable device-level ADC trigger requests, framework validation, caching,
|
||||
and stream lifecycle coordination. Timer trigger frequency and TRGO
|
||||
start/stop are delegated to the timer trigger backend.
|
||||
|
||||
config RT_ADC_TRIGGER_USING_TIMER
|
||||
bool "Enable ADC timer trigger selector support"
|
||||
depends on RT_ADC_USING_TRIGGER
|
||||
default n
|
||||
help
|
||||
Enable timer update/TRGO and timer compare events as ADC stream
|
||||
trigger sources.
|
||||
|
||||
config RT_ADC_TRIGGER_USING_COMPARE
|
||||
bool "Enable ADC analog comparator trigger selector support"
|
||||
depends on RT_ADC_USING_TRIGGER
|
||||
default n
|
||||
help
|
||||
Enable analog comparator output events as ADC stream trigger sources.
|
||||
|
||||
|
||||
config RT_ADC_USING_STREAM
|
||||
bool "Enable ADC stream framework"
|
||||
default n
|
||||
help
|
||||
Enable long-running ADC stream sessions.
|
||||
|
||||
Disable this option to remove ADC stream runtime state, stream APIs,
|
||||
and stream backend hooks from the ADC V2 framework.
|
||||
|
||||
if RT_ADC_USING_STREAM
|
||||
|
||||
menu "ADC stream buffering policies"
|
||||
|
||||
config RT_ADC_STREAM_USING_LATEST
|
||||
bool "Enable ADC stream latest-frame policy"
|
||||
default y
|
||||
help
|
||||
Enable the latest-frame ADC stream policy.
|
||||
|
||||
This mode is intended for slow-changing or status-like ADC values, such
|
||||
as battery voltage, temperature, potentiometer input, resistor-divider
|
||||
voltage, or board health monitoring.
|
||||
|
||||
The DMA buffer stores exactly one complete ADC scan frame. Old values
|
||||
may be overwritten. This mode does not require DMA half-transfer or
|
||||
transfer-complete data interrupts, and it does not allocate a framework
|
||||
FIFO.
|
||||
|
||||
This option may be enabled together with RT_ADC_STREAM_USING_FIFO. The
|
||||
active policy is selected per stream at runtime.
|
||||
|
||||
config RT_ADC_STREAM_USING_FIFO
|
||||
bool "Enable ADC stream FIFO policy"
|
||||
default n
|
||||
help
|
||||
Enable the FIFO ADC stream policy.
|
||||
|
||||
This mode is intended for continuous and loss-sensitive sampling. The
|
||||
DMA buffer is used as a staging buffer, and completed DMA data events
|
||||
are copied into a ringbuffer for ordered blocking reads.
|
||||
|
||||
FIFO mode requires DMA data events. The default DMA event mode uses both
|
||||
half-transfer and transfer-complete events to reduce read latency. A
|
||||
stream configuration may select full-only events to reduce interrupt
|
||||
frequency, at the cost of higher latency and larger data bursts.
|
||||
|
||||
This option may be enabled together with RT_ADC_STREAM_USING_LATEST. The
|
||||
active policy is selected per stream at runtime.
|
||||
|
||||
comment "At least one ADC stream buffering policy should be enabled"
|
||||
depends on !RT_ADC_STREAM_USING_LATEST && !RT_ADC_STREAM_USING_FIFO
|
||||
|
||||
endmenu
|
||||
|
||||
endif
|
||||
|
||||
config RT_ADC_V2_USING_MSH
|
||||
bool "Enable ADC V2 MSH command"
|
||||
depends on RT_USING_FINSH
|
||||
default y
|
||||
help
|
||||
Enable the ADC V2 MSH command.
|
||||
|
||||
This option exports the "adc" shell command for probing ADC devices,
|
||||
configuring channels, reading raw samples, reading voltage values, and
|
||||
reading one ADC sequence. When RT_ADC_USING_STREAM is enabled, stream
|
||||
subcommands are also available.
|
||||
|
||||
Disable this option to remove the ADC V2 shell command while keeping
|
||||
the ADC V2 framework and driver APIs available.
|
||||
|
||||
endif
|
||||
|
||||
endif
|
||||
@@ -0,0 +1,23 @@
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = []
|
||||
CPPPATH = [cwd + '/../include']
|
||||
group = []
|
||||
|
||||
if GetDepend(['RT_USING_ADC']) and not GetDepend(['RT_USING_ADC_V2']):
|
||||
src += ['adc.c']
|
||||
|
||||
if GetDepend(['RT_USING_ADC_V2']):
|
||||
src += ['adc_v2.c']
|
||||
|
||||
if GetDepend(['RT_USING_ADC_V2', 'RT_ADC_USING_TRIGGER']):
|
||||
src += ['adc_v2_trigger.c']
|
||||
|
||||
if GetDepend(['RT_USING_ADC_V2', 'RT_USING_FINSH', 'RT_ADC_V2_USING_MSH']):
|
||||
src += ['adc_v2_msh.c']
|
||||
|
||||
if len(src):
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_ADC'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2026, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file adc_v2_internal.h
|
||||
* @brief Internal ADC V2 framework declarations.
|
||||
*/
|
||||
|
||||
#ifndef __ADC_V2_INTERNAL_H__
|
||||
#define __ADC_V2_INTERNAL_H__
|
||||
|
||||
#include <rtconfig.h>
|
||||
#include <drivers/adc_v2.h>
|
||||
|
||||
#if defined(RT_ADC_USING_TRIGGER)
|
||||
/**
|
||||
* @brief Initialize the cached ADC trigger state to the default software-started mode.
|
||||
* @param device Pointer to the ADC device object.
|
||||
*/
|
||||
void adc_trigger_init(rt_adc_device_t device);
|
||||
|
||||
/**
|
||||
* @brief Preconfigure the active device-level ADC trigger before hardware configuration.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return RT_EOK on success, otherwise an RT-Thread error code.
|
||||
*/
|
||||
rt_err_t adc_trigger_preconfig(rt_adc_device_t device);
|
||||
|
||||
/**
|
||||
* @brief Get the active device-level ADC trigger configuration.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return Pointer to the cached trigger configuration, or RT_NULL when no hardware trigger is configured.
|
||||
*/
|
||||
const struct rt_adc_trigger_cfg *adc_trigger_active_get(rt_adc_device_t device);
|
||||
|
||||
/**
|
||||
* @brief Trigger-source lifecycle actions used after ADC hardware is armed.
|
||||
*/
|
||||
enum adc_trigger_source_action
|
||||
{
|
||||
ADC_TRIGGER_SOURCE_START = 0, /**< Start the active trigger source. */
|
||||
ADC_TRIGGER_SOURCE_STOP /**< Stop the previously started trigger source. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Control the active trigger source around an armed ADC conversion path.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @param action Trigger-source lifecycle action.
|
||||
* @param started Pointer to the trigger-start state.
|
||||
* @param current_result Transfer result before trigger stop, ignored for start.
|
||||
* @return For start, trigger-source start result; for stop, @p current_result unless trigger stop is the first
|
||||
* observed failure.
|
||||
*/
|
||||
rt_err_t adc_trigger_source_control(rt_adc_device_t device, enum adc_trigger_source_action action,
|
||||
rt_bool_t *started, rt_err_t current_result);
|
||||
|
||||
#endif /* defined(RT_ADC_USING_TRIGGER) */
|
||||
|
||||
#endif /* __ADC_V2_INTERNAL_H__ */
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,411 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2026, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2026-05-20 wdfk-prog add ADC V2 timer trigger framework implementation
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file adc_v2_trigger.c
|
||||
* @brief ADC V2 trigger framework implementation.
|
||||
*/
|
||||
|
||||
#include <rtconfig.h>
|
||||
|
||||
#if defined(RT_USING_ADC_V2) && defined(RT_ADC_USING_TRIGGER)
|
||||
|
||||
#include <rtdevice.h>
|
||||
#include <rtthread.h>
|
||||
#if defined(RT_ADC_TRIGGER_USING_TIMER) && defined(RT_USING_CLOCK_TIMER_TRIGGER)
|
||||
#include <drivers/clock_time.h>
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_TIMER) && defined(RT_USING_CLOCK_TIMER_TRIGGER) */
|
||||
|
||||
#define DBG_TAG "adc.v2.trigger"
|
||||
#define DBG_LVL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
#include "adc_v2_internal.h"
|
||||
|
||||
/**
|
||||
* @brief Validate an ADC hardware trigger configuration at framework level.
|
||||
* @param cfg Pointer to the trigger configuration object.
|
||||
* @return Operation status.
|
||||
*/
|
||||
static rt_err_t adc_validate_trigger_cfg_common(const struct rt_adc_trigger_cfg *cfg)
|
||||
{
|
||||
switch (cfg->type)
|
||||
{
|
||||
case RT_ADC_TRIGGER_TIMER_UPDATE:
|
||||
#if defined(RT_ADC_TRIGGER_USING_TIMER)
|
||||
if ((cfg->event.timer.timer == RT_NULL) || (cfg->event.timer.timer->type != RT_Device_Class_Timer) ||
|
||||
(cfg->event.timer.freq_hz == 0U))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
return RT_EOK;
|
||||
#else
|
||||
return -RT_ENOSYS;
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_TIMER) */
|
||||
|
||||
case RT_ADC_TRIGGER_TIMER_COMPARE:
|
||||
#if defined(RT_ADC_TRIGGER_USING_TIMER)
|
||||
if ((cfg->event.timer.timer == RT_NULL) || (cfg->event.timer.timer->type != RT_Device_Class_Timer) ||
|
||||
(cfg->event.timer.channel == 0U) || (cfg->event.timer.freq_hz == 0U))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
return RT_EOK;
|
||||
#else
|
||||
return -RT_ENOSYS;
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_TIMER) */
|
||||
|
||||
case RT_ADC_TRIGGER_ANALOG_COMPARE:
|
||||
#if defined(RT_ADC_TRIGGER_USING_COMPARE)
|
||||
if ((cfg->event.compare.channel == 0U) || (cfg->event.compare.edge == RT_ADC_TRIGGER_EDGE_NONE))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
return RT_EOK;
|
||||
#else
|
||||
return -RT_ENOSYS;
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_COMPARE) */
|
||||
|
||||
case RT_ADC_TRIGGER_PWM_EDGE:
|
||||
case RT_ADC_TRIGGER_EXTI_EDGE:
|
||||
case RT_ADC_TRIGGER_BACKEND:
|
||||
return -RT_ENOSYS;
|
||||
|
||||
default:
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize the ADC trigger control state to software-started mode.
|
||||
* @param device Pointer to the ADC device object.
|
||||
*/
|
||||
void adc_trigger_init(rt_adc_device_t device)
|
||||
{
|
||||
rt_memset(&device->trigger_ctrl, 0, sizeof(device->trigger_ctrl));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the active device-level ADC trigger configuration.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return Pointer to the cached trigger configuration, or RT_NULL when no hardware trigger is configured.
|
||||
*/
|
||||
const struct rt_adc_trigger_cfg *adc_trigger_active_get(rt_adc_device_t device)
|
||||
{
|
||||
if (device->trigger_ctrl.configured != RT_TRUE)
|
||||
{
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
return &device->trigger_ctrl.cfg;
|
||||
}
|
||||
|
||||
#if defined(RT_ADC_TRIGGER_USING_TIMER)
|
||||
/**
|
||||
* @brief Configure and start a timer-based ADC trigger source.
|
||||
* @param cfg Pointer to the active ADC trigger configuration object.
|
||||
* @return Operation status.
|
||||
*/
|
||||
static rt_err_t adc_trigger_timer_source_start(const struct rt_adc_trigger_cfg *cfg)
|
||||
{
|
||||
#if defined(RT_USING_CLOCK_TIMER_TRIGGER)
|
||||
struct rt_clock_timer_trigger_cfg timer_cfg = {0};
|
||||
rt_err_t result;
|
||||
|
||||
timer_cfg.freq_hz = cfg->event.timer.freq_hz;
|
||||
timer_cfg.channel = cfg->event.timer.channel;
|
||||
|
||||
switch (cfg->type)
|
||||
{
|
||||
case RT_ADC_TRIGGER_TIMER_UPDATE:
|
||||
timer_cfg.event = CLOCK_TIMER_TRIGGER_EVENT_UPDATE;
|
||||
timer_cfg.channel = 0U;
|
||||
break;
|
||||
|
||||
case RT_ADC_TRIGGER_TIMER_COMPARE:
|
||||
timer_cfg.event = CLOCK_TIMER_TRIGGER_EVENT_COMPARE;
|
||||
break;
|
||||
|
||||
default:
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
|
||||
result = rt_device_control(cfg->event.timer.timer, CLOCK_TIMER_CTRL_TRIGGER_CONFIG, &timer_cfg);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
result = rt_device_control(cfg->event.timer.timer, CLOCK_TIMER_CTRL_TRIGGER_START, RT_NULL);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
(void)rt_device_control(cfg->event.timer.timer, CLOCK_TIMER_CTRL_TRIGGER_RELEASE, RT_NULL);
|
||||
return result;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
#else
|
||||
RT_UNUSED(cfg);
|
||||
return -RT_ENOSYS;
|
||||
#endif /* defined(RT_USING_CLOCK_TIMER_TRIGGER) */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stop and release a timer-based ADC trigger source.
|
||||
* @param cfg Pointer to the active ADC trigger configuration object.
|
||||
* @return Operation status.
|
||||
*/
|
||||
static rt_err_t adc_trigger_timer_source_stop(const struct rt_adc_trigger_cfg *cfg)
|
||||
{
|
||||
#if defined(RT_USING_CLOCK_TIMER_TRIGGER)
|
||||
rt_err_t stop_result;
|
||||
rt_err_t release_result;
|
||||
|
||||
if ((cfg->type != RT_ADC_TRIGGER_TIMER_UPDATE) && (cfg->type != RT_ADC_TRIGGER_TIMER_COMPARE))
|
||||
{
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
|
||||
stop_result = rt_device_control(cfg->event.timer.timer, CLOCK_TIMER_CTRL_TRIGGER_STOP, RT_NULL);
|
||||
release_result = rt_device_control(cfg->event.timer.timer, CLOCK_TIMER_CTRL_TRIGGER_RELEASE, RT_NULL);
|
||||
|
||||
return (release_result != RT_EOK) ? release_result : stop_result;
|
||||
#else
|
||||
RT_UNUSED(cfg);
|
||||
return -RT_ENOSYS;
|
||||
#endif /* defined(RT_USING_CLOCK_TIMER_TRIGGER) */
|
||||
}
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_TIMER) */
|
||||
|
||||
/**
|
||||
* @brief Start the active trigger source after the ADC conversion path is armed.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return Operation status.
|
||||
*/
|
||||
static rt_err_t adc_trigger_source_start(rt_adc_device_t device)
|
||||
{
|
||||
const struct rt_adc_trigger_cfg *cfg;
|
||||
|
||||
cfg = adc_trigger_active_get(device);
|
||||
if (cfg == RT_NULL)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
switch (cfg->type)
|
||||
{
|
||||
#if defined(RT_ADC_TRIGGER_USING_TIMER)
|
||||
case RT_ADC_TRIGGER_TIMER_UPDATE:
|
||||
case RT_ADC_TRIGGER_TIMER_COMPARE:
|
||||
return adc_trigger_timer_source_start(cfg);
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_TIMER) */
|
||||
|
||||
default:
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stop the active trigger source before the ADC conversion path is disarmed.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return Operation status.
|
||||
*/
|
||||
static rt_err_t adc_trigger_source_stop(rt_adc_device_t device)
|
||||
{
|
||||
const struct rt_adc_trigger_cfg *cfg;
|
||||
|
||||
cfg = adc_trigger_active_get(device);
|
||||
if (cfg == RT_NULL)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
switch (cfg->type)
|
||||
{
|
||||
#if defined(RT_ADC_TRIGGER_USING_TIMER)
|
||||
case RT_ADC_TRIGGER_TIMER_UPDATE:
|
||||
case RT_ADC_TRIGGER_TIMER_COMPARE:
|
||||
return adc_trigger_timer_source_stop(cfg);
|
||||
#endif /* defined(RT_ADC_TRIGGER_USING_TIMER) */
|
||||
|
||||
default:
|
||||
return RT_EOK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Control the active trigger source around an armed ADC conversion path.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @param action Trigger-source lifecycle action.
|
||||
* @param started Pointer to the trigger-start state.
|
||||
* @param current_result Transfer result before trigger stop, ignored for start.
|
||||
* @return For start, trigger-source start result; for stop, @p current_result unless trigger stop is the first
|
||||
* observed failure.
|
||||
*/
|
||||
rt_err_t adc_trigger_source_control(rt_adc_device_t device, enum adc_trigger_source_action action,
|
||||
rt_bool_t *started, rt_err_t current_result)
|
||||
{
|
||||
rt_err_t stop_result;
|
||||
rt_err_t result;
|
||||
|
||||
switch (action)
|
||||
{
|
||||
case ADC_TRIGGER_SOURCE_START:
|
||||
*started = RT_FALSE;
|
||||
if (adc_trigger_active_get(device) == RT_NULL)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
result = adc_trigger_source_start(device);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
LOG_E("trigger source start failed: device=%s result=%d", device->parent.parent.name, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
*started = RT_TRUE;
|
||||
return RT_EOK;
|
||||
|
||||
case ADC_TRIGGER_SOURCE_STOP:
|
||||
if (*started != RT_TRUE)
|
||||
{
|
||||
return current_result;
|
||||
}
|
||||
|
||||
stop_result = adc_trigger_source_stop(device);
|
||||
if (stop_result != RT_EOK)
|
||||
{
|
||||
LOG_E("trigger source stop failed: device=%s result=%d", device->parent.parent.name, stop_result);
|
||||
if (current_result == RT_EOK)
|
||||
{
|
||||
current_result = stop_result;
|
||||
}
|
||||
}
|
||||
|
||||
*started = RT_FALSE;
|
||||
return current_result;
|
||||
|
||||
default:
|
||||
return (current_result != RT_EOK) ? current_result : -RT_EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Preconfigure the active device-level ADC trigger before hardware configuration.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return RT_EOK on success, otherwise an RT-Thread error code.
|
||||
*
|
||||
* @note A null active trigger still has to be passed to the backend when the
|
||||
* backend provides trigger_prepare(), because some backends cache the
|
||||
* previously prepared hardware trigger selector. Passing RT_NULL asks the
|
||||
* backend to prepare the software-start/default trigger path.
|
||||
*/
|
||||
rt_err_t adc_trigger_preconfig(rt_adc_device_t device)
|
||||
{
|
||||
const struct rt_adc_trigger_cfg *cfg;
|
||||
|
||||
cfg = adc_trigger_active_get(device);
|
||||
|
||||
if ((device->ops == RT_NULL) || (device->ops->core == RT_NULL) || (device->ops->core->trigger_prepare == RT_NULL))
|
||||
{
|
||||
return (cfg == RT_NULL) ? RT_EOK : -RT_ENOSYS;
|
||||
}
|
||||
|
||||
return device->ops->core->trigger_prepare(device, cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set one device-level ADC hardware trigger request.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @param cfg Pointer to the ADC trigger configuration object.
|
||||
* @return Operation status.
|
||||
* @note This function only checks the framework-level trigger structure and
|
||||
* caches the request. It does not write ADC trigger registers immediately.
|
||||
* Backend compatibility is reported when the cached trigger is
|
||||
* preconfigured before a stream hardware configuration.
|
||||
*/
|
||||
rt_err_t rt_adc_trigger_set(rt_adc_device_t device, const struct rt_adc_trigger_cfg *cfg)
|
||||
{
|
||||
struct rt_adc_trigger_cfg trigger_cfg;
|
||||
rt_err_t result;
|
||||
rt_atomic_t old_state;
|
||||
|
||||
if ((device == RT_NULL) || (cfg == RT_NULL))
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
trigger_cfg = *cfg;
|
||||
old_state = (rt_atomic_t)RT_ADC_STATE_IDLE;
|
||||
if (rt_atomic_compare_exchange_strong(&device->state, &old_state, (rt_atomic_t)RT_ADC_STATE_LOCKED) == 0)
|
||||
{
|
||||
return -RT_EBUSY;
|
||||
}
|
||||
|
||||
result = adc_validate_trigger_cfg_common(&trigger_cfg);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
device->trigger_ctrl.cfg = trigger_cfg;
|
||||
device->trigger_ctrl.configured = RT_TRUE;
|
||||
}
|
||||
|
||||
rt_atomic_store(&device->state, (rt_atomic_t)RT_ADC_STATE_IDLE);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
LOG_E("trigger set cache failed: device=%s result=%d", device->parent.parent.name, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear the cached ADC trigger configuration.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return Operation status.
|
||||
* @note The ADC device must be idle so the cached trigger cannot be cleared while an active conversion is using it.
|
||||
*/
|
||||
rt_err_t rt_adc_trigger_clear(rt_adc_device_t device)
|
||||
{
|
||||
rt_atomic_t old_state;
|
||||
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
old_state = (rt_atomic_t)RT_ADC_STATE_IDLE;
|
||||
if (rt_atomic_compare_exchange_strong(&device->state, &old_state, (rt_atomic_t)RT_ADC_STATE_LOCKED) == 0)
|
||||
{
|
||||
return -RT_EBUSY;
|
||||
}
|
||||
|
||||
rt_memset(&device->trigger_ctrl, 0, sizeof(device->trigger_ctrl));
|
||||
rt_atomic_store(&device->state, (rt_atomic_t)RT_ADC_STATE_IDLE);
|
||||
|
||||
LOG_D("trigger cleared: device=%s", device->parent.parent.name);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check whether one ADC device has a cached hardware trigger request.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return RT_TRUE if a hardware trigger request is set, otherwise RT_FALSE.
|
||||
*/
|
||||
rt_bool_t rt_adc_trigger_is_set(rt_adc_device_t device)
|
||||
{
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
return RT_FALSE;
|
||||
}
|
||||
|
||||
return device->trigger_ctrl.configured;
|
||||
}
|
||||
|
||||
#endif /* defined(RT_USING_ADC_V2) && defined(RT_ADC_USING_TRIGGER) */
|
||||
@@ -2,6 +2,17 @@ menuconfig RT_USING_CLOCK_TIME
|
||||
bool "Clock time subsystem"
|
||||
default n
|
||||
|
||||
config RT_USING_CLOCK_TIMER_TRIGGER
|
||||
bool "Enable clock timer hardware trigger output"
|
||||
depends on RT_USING_CLOCK_TIME
|
||||
default n
|
||||
help
|
||||
Enable clock timer hardware trigger output controls. Timer backends
|
||||
may use this to expose update/TRGO trigger events to ADC drivers. The
|
||||
selected timer must be dedicated to trigger output; the framework does
|
||||
not arbitrate shared use with clock-time timeouts, PWM, or other timer
|
||||
consumers.
|
||||
|
||||
if RT_USING_CLOCK_TIME
|
||||
config CLOCK_TIMER_FREQ
|
||||
int "Clock time timer freq"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2026, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2026-05-20 wdfk-prog add ADC V2 trigger declarations
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file adc_v2_trigger.h
|
||||
* @brief ADC V2 trigger framework declarations.
|
||||
*/
|
||||
|
||||
#ifndef __ADC_V2_TRIGGER_H__
|
||||
#define __ADC_V2_TRIGGER_H__
|
||||
|
||||
#include <rtconfig.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#if defined(RT_ADC_USING_TRIGGER)
|
||||
struct rt_adc_device;
|
||||
struct rt_device;
|
||||
|
||||
/**
|
||||
* @brief ADC hardware trigger event type.
|
||||
*
|
||||
* @note A platform backend may support only part of these trigger types.
|
||||
* Unsupported trigger types shall be rejected by the framework or backend
|
||||
* with an error such as -RT_ENOSYS.
|
||||
*/
|
||||
enum rt_adc_trigger_type
|
||||
{
|
||||
RT_ADC_TRIGGER_TIMER_UPDATE = 0, /**< Use a timer update/TRGO event. */
|
||||
RT_ADC_TRIGGER_TIMER_COMPARE, /**< Use a timer compare event. */
|
||||
RT_ADC_TRIGGER_PWM_EDGE, /**< Use a PWM edge event. */
|
||||
RT_ADC_TRIGGER_EXTI_EDGE, /**< Use an external interrupt edge event. */
|
||||
RT_ADC_TRIGGER_ANALOG_COMPARE, /**< Use an analog comparator event. */
|
||||
RT_ADC_TRIGGER_BACKEND, /**< Use a backend-specific trigger event. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief ADC trigger edge selector.
|
||||
*/
|
||||
enum rt_adc_trigger_edge
|
||||
{
|
||||
RT_ADC_TRIGGER_EDGE_NONE = 0, /**< No edge selection is required. */
|
||||
RT_ADC_TRIGGER_EDGE_RISING, /**< Use the rising edge. */
|
||||
RT_ADC_TRIGGER_EDGE_FALLING, /**< Use the falling edge. */
|
||||
RT_ADC_TRIGGER_EDGE_BOTH, /**< Use both rising and falling edges. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Timer-based ADC trigger event.
|
||||
*/
|
||||
struct rt_adc_trigger_timer_event
|
||||
{
|
||||
struct rt_device *timer; /**< Timer device used as trigger source. */
|
||||
rt_uint32_t freq_hz; /**< Timer trigger output frequency in hertz. */
|
||||
rt_uint16_t channel; /**< Timer channel, or 0 for update/TRGO event. */
|
||||
rt_uint16_t reserved; /**< Reserved for future extension. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief PWM-based ADC trigger event.
|
||||
*/
|
||||
struct rt_adc_trigger_pwm_event
|
||||
{
|
||||
struct rt_device *pwm; /**< PWM device used as trigger source. */
|
||||
rt_uint16_t channel; /**< PWM channel. */
|
||||
enum rt_adc_trigger_edge edge; /**< PWM edge used as trigger event. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief External interrupt ADC trigger event.
|
||||
*/
|
||||
struct rt_adc_trigger_exti_event
|
||||
{
|
||||
struct rt_device *pin; /**< Pin or GPIO controller device. */
|
||||
rt_uint16_t line; /**< External interrupt line or pin number. */
|
||||
enum rt_adc_trigger_edge edge; /**< Interrupt edge used as trigger event. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Analog comparator ADC trigger event.
|
||||
*/
|
||||
struct rt_adc_trigger_compare_event
|
||||
{
|
||||
struct rt_device *comparator; /**< Optional comparator device used as trigger source. */
|
||||
rt_uint16_t channel; /**< Comparator channel or instance index. */
|
||||
enum rt_adc_trigger_edge edge; /**< Comparator output edge used as trigger event. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Backend-specific ADC trigger event.
|
||||
*/
|
||||
struct rt_adc_trigger_backend_event
|
||||
{
|
||||
rt_uint32_t selector; /**< Backend-specific trigger selector. */
|
||||
rt_uint32_t edge; /**< Backend-specific edge selector. */
|
||||
const void *data; /**< Optional backend-specific configuration. */
|
||||
rt_size_t data_size; /**< Size of backend-specific configuration. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief ADC trigger configuration.
|
||||
*/
|
||||
struct rt_adc_trigger_cfg
|
||||
{
|
||||
enum rt_adc_trigger_type type; /**< Trigger event type. */
|
||||
rt_uint32_t flags; /**< Common trigger flags, reserved for future extension. */
|
||||
|
||||
union
|
||||
{
|
||||
struct rt_adc_trigger_timer_event timer; /**< Timer trigger event. */
|
||||
struct rt_adc_trigger_pwm_event pwm; /**< PWM trigger event. */
|
||||
struct rt_adc_trigger_exti_event exti; /**< External interrupt trigger event. */
|
||||
struct rt_adc_trigger_compare_event compare; /**< Comparator trigger event. */
|
||||
struct rt_adc_trigger_backend_event backend; /**< Backend-specific trigger event. */
|
||||
} event; /**< Trigger event payload. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief ADC trigger runtime control block.
|
||||
*/
|
||||
struct rt_adc_trigger_ctrl
|
||||
{
|
||||
struct rt_adc_trigger_cfg cfg; /**< Cached device-level hardware trigger configuration. */
|
||||
rt_bool_t configured; /**< Whether cfg contains a cached hardware trigger request. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Set one device-level ADC hardware trigger request.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @param cfg Pointer to the ADC trigger configuration object.
|
||||
* @return Operation status.
|
||||
* @note This function only checks the framework-level trigger structure and
|
||||
* caches the request. It does not write ADC trigger registers immediately.
|
||||
* Backend compatibility is reported when the cached trigger is
|
||||
* preconfigured before a conversion hardware configuration.
|
||||
*/
|
||||
rt_err_t rt_adc_trigger_set(struct rt_adc_device *device, const struct rt_adc_trigger_cfg *cfg);
|
||||
|
||||
/**
|
||||
* @brief Clear the cached device-level ADC hardware trigger request.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return Operation status.
|
||||
* @note This function does not write ADC trigger registers immediately. The
|
||||
* next conversion hardware configuration preconfigures the backend
|
||||
* software/default trigger state.
|
||||
*/
|
||||
rt_err_t rt_adc_trigger_clear(struct rt_adc_device *device);
|
||||
|
||||
/**
|
||||
* @brief Check whether one ADC device has a cached hardware trigger request.
|
||||
* @param device Pointer to the ADC device object.
|
||||
* @return RT_TRUE if a hardware trigger request is set, otherwise RT_FALSE.
|
||||
*/
|
||||
rt_bool_t rt_adc_trigger_is_set(struct rt_adc_device *device);
|
||||
#endif /* defined(RT_ADC_USING_TRIGGER) */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* __ADC_V2_TRIGGER_H__ */
|
||||
@@ -47,9 +47,41 @@ typedef enum
|
||||
CLOCK_TIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01,
|
||||
CLOCK_TIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02,
|
||||
CLOCK_TIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03,
|
||||
CLOCK_TIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04
|
||||
CLOCK_TIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04,
|
||||
#if defined(RT_USING_CLOCK_TIMER_TRIGGER)
|
||||
CLOCK_TIMER_CTRL_TRIGGER_CONFIG = RT_DEVICE_CTRL_BASE(Timer) + 0x05,
|
||||
CLOCK_TIMER_CTRL_TRIGGER_START = RT_DEVICE_CTRL_BASE(Timer) + 0x06,
|
||||
CLOCK_TIMER_CTRL_TRIGGER_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x07,
|
||||
CLOCK_TIMER_CTRL_TRIGGER_RELEASE = RT_DEVICE_CTRL_BASE(Timer) + 0x08
|
||||
#endif /* defined(RT_USING_CLOCK_TIMER_TRIGGER) */
|
||||
} rt_clock_timer_ctrl_t;
|
||||
|
||||
#if defined(RT_USING_CLOCK_TIMER_TRIGGER)
|
||||
/**
|
||||
* @brief Hardware timer trigger output event type.
|
||||
*/
|
||||
enum rt_clock_timer_trigger_event
|
||||
{
|
||||
CLOCK_TIMER_TRIGGER_EVENT_UPDATE = 0, /**< Generate trigger on timer update/overflow event. */
|
||||
CLOCK_TIMER_TRIGGER_EVENT_COMPARE, /**< Generate trigger on timer compare match event. */
|
||||
CLOCK_TIMER_TRIGGER_EVENT_PWM_EDGE, /**< Generate trigger on timer PWM output reference edge. */
|
||||
CLOCK_TIMER_TRIGGER_EVENT_BACKEND /**< Generate trigger from a backend-specific timer event. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Hardware trigger output configuration.
|
||||
* @note This interface does not arbitrate timer ownership. Callers must
|
||||
* provide a timer dedicated to hardware trigger output.
|
||||
*/
|
||||
struct rt_clock_timer_trigger_cfg
|
||||
{
|
||||
rt_uint32_t freq_hz; /**< Trigger output frequency in hertz. */
|
||||
enum rt_clock_timer_trigger_event event; /**< Timer event used as trigger output. */
|
||||
rt_uint16_t channel; /**< Timer compare channel, or 0 for update event. */
|
||||
rt_uint16_t reserved; /**< Reserved for future extension. */
|
||||
};
|
||||
#endif /* defined(RT_USING_CLOCK_TIMER_TRIGGER) */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
CLOCK_TIMER_MODE_ONESHOT = 0x01,
|
||||
|
||||
@@ -272,9 +272,11 @@ extern "C" {
|
||||
#include "drivers/dev_audio.h"
|
||||
#endif /* RT_USING_AUDIO */
|
||||
|
||||
#ifdef RT_USING_ADC
|
||||
#ifdef RT_USING_ADC_V2
|
||||
#include "drivers/adc_v2.h"
|
||||
#elif defined(RT_USING_ADC)
|
||||
#include "drivers/adc.h"
|
||||
#endif /* RT_USING_ADC */
|
||||
#endif /* RT_USING_ADC_V2 */
|
||||
|
||||
#ifdef RT_USING_DAC
|
||||
#include "drivers/dac.h"
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
menuconfig RT_USING_ADC
|
||||
bool "Using ADC device drivers"
|
||||
default n
|
||||
|
||||
if RT_USING_DM && RT_USING_ADC
|
||||
osource "$(SOC_DM_ADC_DIR)/Kconfig"
|
||||
endif
|
||||
|
||||
config RT_USING_DAC
|
||||
bool "Using DAC device drivers"
|
||||
default n
|
||||
|
||||
@@ -5,9 +5,6 @@ src = []
|
||||
CPPPATH = [cwd + '/../include']
|
||||
group = []
|
||||
|
||||
if GetDepend(['RT_USING_ADC']):
|
||||
src = src + ['adc.c']
|
||||
|
||||
if GetDepend(['RT_USING_DAC']):
|
||||
src = src + ['dac.c']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user