mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-11-16 01:05:15 +08:00
feat[bsp][stm32]: add Input Capture support for STM32F1 and STM32H7
- Implement RT-Thread Input Capture devices using STM32 HAL TIM1-TIM4, with configurable channels, filters and initial capture polarity. - Add STM32F1 and STM32H7 configuration headers and integrate the driver into Kconfig and SConscript. - Handle APB timer clock multipliers, TIMPRE and 32-bit counter capabilities using HAL feature macros. - Clear pending capture and overcapture flags before restarting a capture channel.
This commit is contained in:
1 parent
89c70abccd
commit
7ac0b44a8d
9 files changed
+746
No files matched your search
@@ -188,3 +188,125 @@ if SOC_SERIES_STM32H7
|
||||
select BSP_USING_BDMA
|
||||
default n
|
||||
endif
|
||||
|
||||
menu "STM32 Input Capture BSP"
|
||||
|
||||
config BSP_USING_INPUT_CAPTURE
|
||||
bool "Enable STM32 timer Input Capture devices"
|
||||
default n
|
||||
select RT_USING_INPUT_CAPTURE
|
||||
help
|
||||
Registers selected STM32 hardware timer Input Capture devices.
|
||||
F1 and H7 have configuration headers; additional series need a
|
||||
matching configuration header before enabling this option.
|
||||
GPIO, clock and interrupt setup are owned by CubeMX HAL IC MSP.
|
||||
|
||||
if BSP_USING_INPUT_CAPTURE
|
||||
config BSP_INPUT_CAPTURE_COUNTER_HZ
|
||||
int "Capture timer counter frequency (Hz)"
|
||||
range 100000 1000000
|
||||
default 1000000
|
||||
|
||||
config BSP_USING_INPUT_CAPTURE_TIM1
|
||||
bool "Enable TIM1 Input Capture"
|
||||
default n
|
||||
|
||||
if BSP_USING_INPUT_CAPTURE_TIM1
|
||||
config BSP_INPUT_CAPTURE_TIM1_NAME
|
||||
string "TIM1 Input Capture device name"
|
||||
default "ic1"
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM1_CHANNEL
|
||||
int "TIM1 Input Capture channel (1-4)"
|
||||
range 1 4
|
||||
default 1
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM1_FILTER
|
||||
int "TIM1 Input Capture digital filter (0-15)"
|
||||
range 0 15
|
||||
default 0
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM1_INITIAL_RISING
|
||||
int "TIM1 initial capture edge (1 rising, 0 falling)"
|
||||
range 0 1
|
||||
default 1
|
||||
endif
|
||||
|
||||
config BSP_USING_INPUT_CAPTURE_TIM2
|
||||
bool "Enable TIM2 Input Capture"
|
||||
default n
|
||||
|
||||
if BSP_USING_INPUT_CAPTURE_TIM2
|
||||
config BSP_INPUT_CAPTURE_TIM2_NAME
|
||||
string "TIM2 Input Capture device name"
|
||||
default "ic2"
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM2_CHANNEL
|
||||
int "TIM2 Input Capture channel (1-4)"
|
||||
range 1 4
|
||||
default 1
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM2_FILTER
|
||||
int "TIM2 Input Capture digital filter (0-15)"
|
||||
range 0 15
|
||||
default 0
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM2_INITIAL_RISING
|
||||
int "TIM2 initial capture edge (1 rising, 0 falling)"
|
||||
range 0 1
|
||||
default 1
|
||||
endif
|
||||
|
||||
config BSP_USING_INPUT_CAPTURE_TIM3
|
||||
bool "Enable TIM3 Input Capture"
|
||||
default y
|
||||
|
||||
if BSP_USING_INPUT_CAPTURE_TIM3
|
||||
config BSP_INPUT_CAPTURE_TIM3_NAME
|
||||
string "TIM3 Input Capture device name"
|
||||
default "ic3"
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM3_CHANNEL
|
||||
int "TIM3 Input Capture channel (1-4)"
|
||||
range 1 4
|
||||
default 1
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM3_FILTER
|
||||
int "TIM3 Input Capture digital filter (0-15)"
|
||||
range 0 15
|
||||
default 0
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM3_INITIAL_RISING
|
||||
int "TIM3 initial capture edge (1 rising, 0 falling)"
|
||||
range 0 1
|
||||
default 1
|
||||
endif
|
||||
|
||||
config BSP_USING_INPUT_CAPTURE_TIM4
|
||||
bool "Enable TIM4 Input Capture"
|
||||
default n
|
||||
|
||||
if BSP_USING_INPUT_CAPTURE_TIM4
|
||||
config BSP_INPUT_CAPTURE_TIM4_NAME
|
||||
string "TIM4 Input Capture device name"
|
||||
default "ic4"
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM4_CHANNEL
|
||||
int "TIM4 Input Capture channel (1-4)"
|
||||
range 1 4
|
||||
default 1
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM4_FILTER
|
||||
int "TIM4 Input Capture digital filter (0-15)"
|
||||
range 0 15
|
||||
default 0
|
||||
|
||||
config BSP_INPUT_CAPTURE_TIM4_INITIAL_RISING
|
||||
int "TIM4 initial capture edge (1 rising, 0 falling)"
|
||||
range 0 1
|
||||
default 1
|
||||
endif
|
||||
|
||||
endif
|
||||
|
||||
endmenu
|
||||
@@ -27,6 +27,9 @@ if GetDepend(['RT_USING_SERIAL']):
|
||||
if GetDepend(['BSP_USING_TIM']):
|
||||
src += ['drv_tim.c']
|
||||
|
||||
if GetDepend(['BSP_USING_INPUT_CAPTURE']):
|
||||
src += ['drv_inputcapture.c']
|
||||
|
||||
if GetDepend(['BSP_USING_PWM']):
|
||||
src += ['drv_pwm.c', 'drv_tim.c']
|
||||
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2026-11-02 wdfk-prog first version
|
||||
*/
|
||||
#ifndef __INPUTCAPTURE_CONFIG_H__
|
||||
#define __INPUTCAPTURE_CONFIG_H__
|
||||
|
||||
#include "drv_inputcapture.h"
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM1
|
||||
/** @brief Board-selected TIM1 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM1_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM1_NAME, \
|
||||
.instance = TIM1, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM1_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM1_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM1_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM1 */
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM2
|
||||
/** @brief Board-selected TIM2 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM2_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM2_NAME, \
|
||||
.instance = TIM2, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM2_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM2_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM2_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM2 */
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM3
|
||||
/** @brief Board-selected TIM3 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM3_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM3_NAME, \
|
||||
.instance = TIM3, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM3_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM3_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM3_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM3 */
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM4
|
||||
/** @brief Board-selected TIM4 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM4_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM4_NAME, \
|
||||
.instance = TIM4, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM4_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM4_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM4_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM4 */
|
||||
|
||||
#endif /* __INPUTCAPTURE_CONFIG_H__ */
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2026-11-01 wdfk-prog first version
|
||||
*/
|
||||
#ifndef __INPUTCAPTURE_CONFIG_H__
|
||||
#define __INPUTCAPTURE_CONFIG_H__
|
||||
|
||||
#include "drv_inputcapture.h"
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM1
|
||||
/** @brief Board-selected TIM1 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM1_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM1_NAME, \
|
||||
.instance = TIM1, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM1_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM1_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM1_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM1 */
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM2
|
||||
/** @brief Board-selected TIM2 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM2_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM2_NAME, \
|
||||
.instance = TIM2, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM2_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM2_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM2_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM2 */
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM3
|
||||
/** @brief Board-selected TIM3 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM3_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM3_NAME, \
|
||||
.instance = TIM3, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM3_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM3_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM3_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM3 */
|
||||
|
||||
#ifdef BSP_USING_INPUT_CAPTURE_TIM4
|
||||
/** @brief Board-selected TIM4 Input Capture configuration. */
|
||||
#define INPUTCAPTURE_TIM4_CONFIG \
|
||||
{ \
|
||||
.name = BSP_INPUT_CAPTURE_TIM4_NAME, \
|
||||
.instance = TIM4, \
|
||||
.channel = STM32_CAPTURE_CHANNEL(BSP_INPUT_CAPTURE_TIM4_CHANNEL), \
|
||||
.input_filter = BSP_INPUT_CAPTURE_TIM4_FILTER, \
|
||||
.counter_period = 0xFFFFU, \
|
||||
.initial_edge_rising = BSP_INPUT_CAPTURE_TIM4_INITIAL_RISING, \
|
||||
.hardware_both_edges = RT_FALSE, \
|
||||
}
|
||||
#endif /* BSP_USING_INPUT_CAPTURE_TIM4 */
|
||||
|
||||
#endif /* __INPUTCAPTURE_CONFIG_H__ */
|
||||
@@ -40,6 +40,7 @@ extern "C" {
|
||||
#include "f1/usbd_config.h"
|
||||
#include "f1/i2c_hard_config.h"
|
||||
#include "f1/pulse_encoder_config.h"
|
||||
#include "f1/inputcapture_config.h"
|
||||
#elif defined(SOC_SERIES_STM32F2)
|
||||
#include "f2/dma_config.h"
|
||||
#include "f2/uart_config.h"
|
||||
@@ -142,6 +143,7 @@ extern "C" {
|
||||
#include "h7/pwm_config.h"
|
||||
#include "h7/usbd_config.h"
|
||||
#include "h7/i2c_hard_config.h"
|
||||
#include "h7/inputcapture_config.h"
|
||||
#elif defined(SOC_SERIES_STM32H7RS)
|
||||
#include "h7/dma_config.h"
|
||||
#include "h7/uart_config.h"
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2026-11-01 wdfk-prog first version
|
||||
*/
|
||||
#ifndef __DRV_INPUTCAPTURE_H__
|
||||
#define __DRV_INPUTCAPTURE_H__
|
||||
|
||||
#include <board.h>
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
#include <rthw.h>
|
||||
#include <drv_common.h>
|
||||
#include <drv_config.h>
|
||||
#include <drivers/rt_inputcapture.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Hardware mapping for one STM32 timer Input Capture device.
|
||||
* @note GPIO, peripheral clocks and NVIC setup belong to CubeMX's HAL MSP
|
||||
* initialization, not to this device mapping.
|
||||
*/
|
||||
struct stm32_inputcapture_config
|
||||
{
|
||||
const char *name; /**< Registered RT-Thread device name. */
|
||||
TIM_TypeDef *instance; /**< Physical timer peripheral instance. */
|
||||
rt_uint32_t channel; /**< The only selected HAL TIM_CHANNEL_x. */
|
||||
rt_uint32_t input_filter; /**< HAL input digital filter value. */
|
||||
rt_uint32_t counter_period; /**< Default ARR for 16-bit TIM; 32-bit TIM uses its full range. */
|
||||
rt_bool_t initial_edge_rising; /**< Initial capture edge when the line level is not sampled. */
|
||||
rt_bool_t hardware_both_edges; /**< True only for supported hardware both-edge capture. */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Register enabled STM32 Input Capture timers with RT-Thread.
|
||||
* @return RT_EOK or the first RT-Thread registration error.
|
||||
* @note The board must give each TIM and its IRQ a single owner. If HAL
|
||||
* callback registration is enabled, the board must register this
|
||||
* driver's HAL_TIM_IC_CaptureCallback explicitly.
|
||||
*/
|
||||
int rt_hw_inputcapture_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* __DRV_INPUTCAPTURE_H__ */
|
||||
@@ -0,0 +1,2 @@
|
||||
CONFIG_BSP_USING_INPUT_CAPTURE=y
|
||||
CONFIG_BSP_USING_INPUT_CAPTURE_TIM3=y
|
||||
@@ -0,0 +1,21 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
devices.inputcapture_tim3:
|
||||
kconfig:
|
||||
- CONFIG_BSP_USING_INPUT_CAPTURE=y
|
||||
- CONFIG_BSP_USING_INPUT_CAPTURE_TIM3=y
|
||||
# CI-only: enable the CubeMX HAL TIM module for Input Capture.
|
||||
pre_build: |
|
||||
sed -i 's@^/\*[[:space:]]*#define HAL_TIM_MODULE_ENABLED[[:space:]]*\*/@#define HAL_TIM_MODULE_ENABLED@' board/CubeMX_Config/Inc/stm32h7xx_hal_conf.h
|
||||
grep -Fx '#define HAL_TIM_MODULE_ENABLED' board/CubeMX_Config/Inc/stm32h7xx_hal_conf.h
|
||||
grep -Fx 'CONFIG_RT_USING_INPUT_CAPTURE=y' .config
|
||||
grep -Fx 'CONFIG_BSP_USING_INPUT_CAPTURE=y' .config
|
||||
grep -Fx 'CONFIG_BSP_USING_INPUT_CAPTURE_TIM3=y' .config
|
||||
grep -Fx '#define RT_USING_INPUT_CAPTURE' rtconfig.h
|
||||
grep -Fx '#define BSP_USING_INPUT_CAPTURE' rtconfig.h
|
||||
grep -Fx '#define BSP_USING_INPUT_CAPTURE_TIM3' rtconfig.h
|
||||
! grep -Eq '^#define RT_USING_(HWTIMER|PWM|PULSE_ENCODER)([[:space:]]|$)' rtconfig.h
|
||||
buildcheckresult: drv_inputcapture.o
|
||||
post_build: |
|
||||
test -s rt-thread.elf
|
||||
find build -type f -name drv_inputcapture.o -print -quit | grep -q .
|
||||
Reference in new issue
Block a user