diff options
| author | wdfk-prog <[email protected]> | 2026-11-17 08:59:07 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-11-17 08:59:07 +0800 |
| commit | 0dffe47813444cc8b0d26a10b019e1a629a40a96 (patch) | |
| tree | ea3d9606ea345cbf871a7754beac586510a16cd6 | |
| parent | 7c974b59fdc258afbdb19c378f250bcd95511537 (diff) | |
feat[STM32][inputcapture]: Support multiple capture channels and paired-edge modes (#12052)
* feat[STM32][inputcapture]: Support multiple capture channels and paired-edge modes
- Support independent EDGE capture devices on multiple channels of the same physical timer.
- Add exclusive PWM Input and dual-edge capture modes using paired TIM channels.
- Share HAL timer handles while maintaining independent capture device states and interrupt dispatch.
- Extend Kconfig selections and F1/H7 configuration headers for per-channel capture settings.
* style: format code with clang-format
5 files changed, 1535 insertions, 280 deletions
diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/Kconfig b/bsp/stm32/libraries/HAL_Drivers/drivers/Kconfig index 2987e112c7..0b6eac65d5 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/Kconfig +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/Kconfig @@ -196,10 +196,9 @@ menu "STM32 Input Capture BSP" 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. + Register independently selected timer capture channels, or one + exclusive hardware PWM Input or dual-edge device per enabled TIM. + Configure capture pins, peripheral clocks and IRQ in the board HAL MSP. if BSP_USING_INPUT_CAPTURE config BSP_INPUT_CAPTURE_COUNTER_HZ @@ -212,24 +211,38 @@ menu "STM32 Input Capture BSP" 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 + config BSP_INPUT_CAPTURE_TIM1_PWM_INPUT + int "TIM1 PWM Input (0 = off, 1 = on)" + range 0 1 default 0 + help + PWM Input occupies the entire timer and registers one + logical capture device. Set 0 to select dual-edge or + independent EDGE capture instead. - config BSP_INPUT_CAPTURE_TIM1_INITIAL_RISING - int "TIM1 initial capture edge (1 rising, 0 falling)" - range 0 1 - default 1 + config BSP_INPUT_CAPTURE_TIM1_DUAL_EDGE + bool "TIM1 paired dual-edge capture (CH1/CH2)" + depends on BSP_INPUT_CAPTURE_TIM1_PWM_INPUT = 0 + default n + help + Capture rising and falling edges of the same TI1/TI2 input + with CH1/CH2 and register one logical device. This mode + reserves the entire timer and does not use slave reset. + + if BSP_INPUT_CAPTURE_TIM1_PWM_INPUT = 0 && !BSP_INPUT_CAPTURE_TIM1_DUAL_EDGE + config BSP_INPUT_CAPTURE_TIM1_CH1 + bool "TIM1 CH1 independent EDGE capture" + default y + config BSP_INPUT_CAPTURE_TIM1_CH2 + bool "TIM1 CH2 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM1_CH3 + bool "TIM1 CH3 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM1_CH4 + bool "TIM1 CH4 independent EDGE capture" + default n + endif endif config BSP_USING_INPUT_CAPTURE_TIM2 @@ -237,24 +250,38 @@ menu "STM32 Input Capture BSP" 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 + config BSP_INPUT_CAPTURE_TIM2_PWM_INPUT + int "TIM2 PWM Input (0 = off, 1 = on)" + range 0 1 default 0 + help + PWM Input occupies the entire timer and registers one + logical capture device. Set 0 to select dual-edge or + independent EDGE capture instead. - config BSP_INPUT_CAPTURE_TIM2_INITIAL_RISING - int "TIM2 initial capture edge (1 rising, 0 falling)" - range 0 1 - default 1 + config BSP_INPUT_CAPTURE_TIM2_DUAL_EDGE + bool "TIM2 paired dual-edge capture (CH1/CH2)" + depends on BSP_INPUT_CAPTURE_TIM2_PWM_INPUT = 0 + default n + help + Capture rising and falling edges of the same TI1/TI2 input + with CH1/CH2 and register one logical device. This mode + reserves the entire timer and does not use slave reset. + + if BSP_INPUT_CAPTURE_TIM2_PWM_INPUT = 0 && !BSP_INPUT_CAPTURE_TIM2_DUAL_EDGE + config BSP_INPUT_CAPTURE_TIM2_CH1 + bool "TIM2 CH1 independent EDGE capture" + default y + config BSP_INPUT_CAPTURE_TIM2_CH2 + bool "TIM2 CH2 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM2_CH3 + bool "TIM2 CH3 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM2_CH4 + bool "TIM2 CH4 independent EDGE capture" + default n + endif endif config BSP_USING_INPUT_CAPTURE_TIM3 @@ -262,24 +289,38 @@ menu "STM32 Input Capture BSP" 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 + config BSP_INPUT_CAPTURE_TIM3_PWM_INPUT + int "TIM3 PWM Input (0 = off, 1 = on)" + range 0 1 default 0 + help + PWM Input occupies the entire timer and registers one + logical capture device. Set 0 to select dual-edge or + independent EDGE capture instead. - config BSP_INPUT_CAPTURE_TIM3_INITIAL_RISING - int "TIM3 initial capture edge (1 rising, 0 falling)" - range 0 1 - default 1 + config BSP_INPUT_CAPTURE_TIM3_DUAL_EDGE + bool "TIM3 paired dual-edge capture (CH1/CH2)" + depends on BSP_INPUT_CAPTURE_TIM3_PWM_INPUT = 0 + default n + help + Capture rising and falling edges of the same TI1/TI2 input + with CH1/CH2 and register one logical device. This mode + reserves the entire timer and does not use slave reset. + + if BSP_INPUT_CAPTURE_TIM3_PWM_INPUT = 0 && !BSP_INPUT_CAPTURE_TIM3_DUAL_EDGE + config BSP_INPUT_CAPTURE_TIM3_CH1 + bool "TIM3 CH1 independent EDGE capture" + default y + config BSP_INPUT_CAPTURE_TIM3_CH2 + bool "TIM3 CH2 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM3_CH3 + bool "TIM3 CH3 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM3_CH4 + bool "TIM3 CH4 independent EDGE capture" + default n + endif endif config BSP_USING_INPUT_CAPTURE_TIM4 @@ -287,26 +328,54 @@ menu "STM32 Input Capture BSP" 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 + config BSP_INPUT_CAPTURE_TIM4_PWM_INPUT + int "TIM4 PWM Input (0 = off, 1 = on)" + range 0 1 default 0 + help + PWM Input occupies the entire timer and registers one + logical capture device. Set 0 to select dual-edge or + independent EDGE capture instead. - config BSP_INPUT_CAPTURE_TIM4_INITIAL_RISING - int "TIM4 initial capture edge (1 rising, 0 falling)" - range 0 1 - default 1 + config BSP_INPUT_CAPTURE_TIM4_DUAL_EDGE + bool "TIM4 paired dual-edge capture (CH1/CH2)" + depends on BSP_INPUT_CAPTURE_TIM4_PWM_INPUT = 0 + default n + help + Capture rising and falling edges of the same TI1/TI2 input + with CH1/CH2 and register one logical device. This mode + reserves the entire timer and does not use slave reset. + + if BSP_INPUT_CAPTURE_TIM4_PWM_INPUT = 0 && !BSP_INPUT_CAPTURE_TIM4_DUAL_EDGE + config BSP_INPUT_CAPTURE_TIM4_CH1 + bool "TIM4 CH1 independent EDGE capture" + default y + config BSP_INPUT_CAPTURE_TIM4_CH2 + bool "TIM4 CH2 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM4_CH3 + bool "TIM4 CH3 independent EDGE capture" + default n + config BSP_INPUT_CAPTURE_TIM4_CH4 + bool "TIM4 CH4 independent EDGE capture" + default n + endif endif + config BSP_USING_INPUT_CAPTURE_DUAL_EDGE + bool + default y if BSP_INPUT_CAPTURE_TIM1_DUAL_EDGE + default y if BSP_INPUT_CAPTURE_TIM2_DUAL_EDGE + default y if BSP_INPUT_CAPTURE_TIM3_DUAL_EDGE + default y if BSP_INPUT_CAPTURE_TIM4_DUAL_EDGE + + config BSP_USING_INPUT_CAPTURE_PWM_INPUT + bool + default y if BSP_INPUT_CAPTURE_TIM1_PWM_INPUT = 1 + default y if BSP_INPUT_CAPTURE_TIM2_PWM_INPUT = 1 + default y if BSP_INPUT_CAPTURE_TIM3_PWM_INPUT = 1 + default y if BSP_INPUT_CAPTURE_TIM4_PWM_INPUT = 1 + endif -endmenu +endmenu
\ No newline at end of file diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/inputcapture_config.h b/bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/inputcapture_config.h index 65e34effcb..bbcd7d1b84 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/inputcapture_config.h +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/config/f1/inputcapture_config.h @@ -3,67 +3,345 @@ * * Change Logs: * Date Author Notes - * 2026-11-02 wdfk-prog first version + * 2026-11-01 wdfk-prog first version + * 2026-11-10 wdfk-prog describe individual capture channels and exclusive PWM Input */ #ifndef __INPUTCAPTURE_CONFIG_H__ #define __INPUTCAPTURE_CONFIG_H__ -#include "drv_inputcapture.h" +/* The configurations below describe board hardware, not Kconfig user choices. + * Edit pin-related capture polarity, filter, ARR and name for this board here. + * Every logical device on the same physical TIM shares PSC, ARR and CNT. + * PWM_INPUT uses the direct input channel plus its indirect paired channel. + * DUAL_EDGE pairs the same input without slave reset or interval batching. + */ #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, \ +/** @brief TIM1 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH1_CONFIG \ + { \ + .name = "ic1", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM1 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH2_CONFIG \ + { \ + .name = "ic1_2", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM1 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH3_CONFIG \ + { \ + .name = "ic1_3", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM1 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH4_CONFIG \ + { \ + .name = "ic1_4", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM1 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM1_PWM_CONFIG \ + { \ + .name = "ic1", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM1_PWM_INPUT, \ + } +/** @brief TIM1 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM1_DUAL_CONFIG \ + { \ + .name = "ic1", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #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, \ +/** @brief TIM2 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH1_CONFIG \ + { \ + .name = "ic2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM2 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH2_CONFIG \ + { \ + .name = "ic2_2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM2 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH3_CONFIG \ + { \ + .name = "ic2_3", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM2 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH4_CONFIG \ + { \ + .name = "ic2_4", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM2 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM2_PWM_CONFIG \ + { \ + .name = "ic2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM2_PWM_INPUT, \ + } +/** @brief TIM2 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM2_DUAL_CONFIG \ + { \ + .name = "ic2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #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, \ +/** @brief TIM3 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH1_CONFIG \ + { \ + .name = "ic3_1", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM3 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH2_CONFIG \ + { \ + .name = "ic0", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM3 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH3_CONFIG \ + { \ + .name = "ic3_3", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM3 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH4_CONFIG \ + { \ + .name = "ic3_4", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM3 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM3_PWM_CONFIG \ + { \ + .name = "ic0", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM3_PWM_INPUT, \ + } +/** @brief TIM3 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM3_DUAL_CONFIG \ + { \ + .name = "ic0", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #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, \ +/** @brief TIM4 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH1_CONFIG \ + { \ + .name = "ic4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM4 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH2_CONFIG \ + { \ + .name = "ic4_2", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM4 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH3_CONFIG \ + { \ + .name = "ic4_3", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM4 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH4_CONFIG \ + { \ + .name = "ic4_4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM4 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM4_PWM_CONFIG \ + { \ + .name = "ic4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM4_PWM_INPUT, \ + } +/** @brief TIM4 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM4_DUAL_CONFIG \ + { \ + .name = "ic4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ #endif /* __INPUTCAPTURE_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/inputcapture_config.h b/bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/inputcapture_config.h index d2621b93d6..433f2fbe37 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/inputcapture_config.h +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/config/h7/inputcapture_config.h @@ -4,66 +4,344 @@ * Change Logs: * Date Author Notes * 2026-11-01 wdfk-prog first version + * 2026-11-10 wdfk-prog describe individual capture channels and exclusive PWM Input */ #ifndef __INPUTCAPTURE_CONFIG_H__ #define __INPUTCAPTURE_CONFIG_H__ -#include "drv_inputcapture.h" +/* The configurations below describe board hardware, not Kconfig user choices. + * Edit pin-related capture polarity, filter, ARR and name for this board here. + * Every logical device on the same physical TIM shares PSC, ARR and CNT. + * PWM_INPUT uses the direct input channel plus its indirect paired channel. + * DUAL_EDGE pairs the same input without slave reset or interval batching. + */ #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, \ +/** @brief TIM1 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH1_CONFIG \ + { \ + .name = "ic1", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM1 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH2_CONFIG \ + { \ + .name = "ic1_2", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM1 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH3_CONFIG \ + { \ + .name = "ic1_3", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM1 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM1_CH4_CONFIG \ + { \ + .name = "ic1_4", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM1 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM1_PWM_CONFIG \ + { \ + .name = "ic1", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM1_PWM_INPUT, \ + } +/** @brief TIM1 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM1_DUAL_CONFIG \ + { \ + .name = "ic1", \ + .instance = TIM1, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #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, \ +/** @brief TIM2 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH1_CONFIG \ + { \ + .name = "ic2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM2 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH2_CONFIG \ + { \ + .name = "ic2_2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM2 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH3_CONFIG \ + { \ + .name = "ic2_3", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM2 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM2_CH4_CONFIG \ + { \ + .name = "ic2_4", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM2 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM2_PWM_CONFIG \ + { \ + .name = "ic2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM2_PWM_INPUT, \ + } +/** @brief TIM2 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM2_DUAL_CONFIG \ + { \ + .name = "ic2", \ + .instance = TIM2, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #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, \ +/** @brief TIM3 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH1_CONFIG \ + { \ + .name = "ic3_1", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM3 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH2_CONFIG \ + { \ + .name = "ic0", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM3 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH3_CONFIG \ + { \ + .name = "ic3_3", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM3 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM3_CH4_CONFIG \ + { \ + .name = "ic3_4", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM3 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM3_PWM_CONFIG \ + { \ + .name = "ic0", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_FALSE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM3_PWM_INPUT, \ + } +/** @brief TIM3 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM3_DUAL_CONFIG \ + { \ + .name = "ic0", \ + .instance = TIM3, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 9U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #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, \ +/** @brief TIM4 CH1 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH1_CONFIG \ + { \ + .name = "ic4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ } + +/** @brief TIM4 CH2 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH2_CONFIG \ + { \ + .name = "ic4_2", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_2, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM4 CH3 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH3_CONFIG \ + { \ + .name = "ic4_3", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_3, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM4 CH4 independent input-capture configuration. */ +#define INPUTCAPTURE_TIM4_CH4_CONFIG \ + { \ + .name = "ic4_4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_4, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + } + +/** @brief TIM4 exclusive PWM Input configuration (CH1+CH2 pair). */ +#define INPUTCAPTURE_TIM4_PWM_CONFIG \ + { \ + .name = "ic4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = BSP_INPUT_CAPTURE_TIM4_PWM_INPUT, \ + } +/** @brief TIM4 exclusive dual-edge capture (CH1/CH2 on one TI input). */ +#define INPUTCAPTURE_TIM4_DUAL_CONFIG \ + { \ + .name = "ic4", \ + .instance = TIM4, \ + .channel = TIM_CHANNEL_1, \ + .input_filter = 0U, \ + .counter_period = 0xFFFFU, \ + .initial_edge_rising = RT_TRUE, \ + .hardware_both_edges = RT_FALSE, \ + .pwm_input = RT_FALSE, \ + .dual_edge = RT_TRUE, \ + .dual_partner_filter = 0U, \ + } + #endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ #endif /* __INPUTCAPTURE_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.c b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.c index e9e992bcc5..47b8cd6ea4 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.c +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.c @@ -4,6 +4,7 @@ * Change Logs: * Date Author Notes * 2026-11-01 wdfk-prog first version + * 2026-11-10 wdfk-prog support independent capture channels and exclusive PWM Input */ #include <board.h> #include <rtdevice.h> @@ -56,7 +57,10 @@ #define STM32_INPUTCAPTURE_HAL_HAS_BOTHEDGE_POLARITY 0 #endif /* defined(TIM_INPUTCHANNELPOLARITY_BOTHEDGE) */ -/** @brief Stable indices for selected physical timer instances. */ +#define LOG_TAG "drv.inputcapture" +#include <drv_log.h> + +/** @brief Stable indices of enabled physical timer instances. */ enum stm32_capture_index { #ifdef BSP_USING_INPUT_CAPTURE_TIM1 @@ -70,42 +74,216 @@ enum stm32_capture_index #endif /* BSP_USING_INPUT_CAPTURE_TIM3 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM4 STM32_CAPTURE_TIM4_INDEX, -#endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ - STM32_CAPTURE_DEVICE_COUNT +#endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ + STM32_CAPTURE_TIMER_COUNT /**< Number of enabled physical timers. */ }; -/** @brief Board-selected physical timer capture configuration table. */ +/** @brief Timer-handle storage shared by all capture channels of one physical TIM. */ +static TIM_HandleTypeDef stm32_capture_timers[STM32_CAPTURE_TIMER_COUNT]; + +/** @brief Fixed configurations of independent EDGE, PWM Input and dual-edge devices. */ static const struct stm32_inputcapture_config stm32_capture_configs[] = { #ifdef BSP_USING_INPUT_CAPTURE_TIM1 - INPUTCAPTURE_TIM1_CONFIG, +#if BSP_INPUT_CAPTURE_TIM1_PWM_INPUT == 1 + INPUTCAPTURE_TIM1_PWM_CONFIG, +#elif defined(BSP_INPUT_CAPTURE_TIM1_DUAL_EDGE) + INPUTCAPTURE_TIM1_DUAL_CONFIG, +#else +#ifdef BSP_INPUT_CAPTURE_TIM1_CH1 + INPUTCAPTURE_TIM1_CH1_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM1_CH1 */ +#ifdef BSP_INPUT_CAPTURE_TIM1_CH2 + INPUTCAPTURE_TIM1_CH2_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM1_CH2 */ +#ifdef BSP_INPUT_CAPTURE_TIM1_CH3 + INPUTCAPTURE_TIM1_CH3_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM1_CH3 */ +#ifdef BSP_INPUT_CAPTURE_TIM1_CH4 + INPUTCAPTURE_TIM1_CH4_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM1_CH4 */ +#endif /* BSP_INPUT_CAPTURE_TIM1_PWM_INPUT */ #endif /* BSP_USING_INPUT_CAPTURE_TIM1 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM2 - INPUTCAPTURE_TIM2_CONFIG, +#if BSP_INPUT_CAPTURE_TIM2_PWM_INPUT == 1 + INPUTCAPTURE_TIM2_PWM_CONFIG, +#elif defined(BSP_INPUT_CAPTURE_TIM2_DUAL_EDGE) + INPUTCAPTURE_TIM2_DUAL_CONFIG, +#else +#ifdef BSP_INPUT_CAPTURE_TIM2_CH1 + INPUTCAPTURE_TIM2_CH1_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM2_CH1 */ +#ifdef BSP_INPUT_CAPTURE_TIM2_CH2 + INPUTCAPTURE_TIM2_CH2_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM2_CH2 */ +#ifdef BSP_INPUT_CAPTURE_TIM2_CH3 + INPUTCAPTURE_TIM2_CH3_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM2_CH3 */ +#ifdef BSP_INPUT_CAPTURE_TIM2_CH4 + INPUTCAPTURE_TIM2_CH4_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM2_CH4 */ +#endif /* BSP_INPUT_CAPTURE_TIM2_PWM_INPUT */ #endif /* BSP_USING_INPUT_CAPTURE_TIM2 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM3 - INPUTCAPTURE_TIM3_CONFIG, +#if BSP_INPUT_CAPTURE_TIM3_PWM_INPUT == 1 + INPUTCAPTURE_TIM3_PWM_CONFIG, +#elif defined(BSP_INPUT_CAPTURE_TIM3_DUAL_EDGE) + INPUTCAPTURE_TIM3_DUAL_CONFIG, +#else +#ifdef BSP_INPUT_CAPTURE_TIM3_CH1 + INPUTCAPTURE_TIM3_CH1_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM3_CH1 */ +#ifdef BSP_INPUT_CAPTURE_TIM3_CH2 + INPUTCAPTURE_TIM3_CH2_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM3_CH2 */ +#ifdef BSP_INPUT_CAPTURE_TIM3_CH3 + INPUTCAPTURE_TIM3_CH3_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM3_CH3 */ +#ifdef BSP_INPUT_CAPTURE_TIM3_CH4 + INPUTCAPTURE_TIM3_CH4_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM3_CH4 */ +#endif /* BSP_INPUT_CAPTURE_TIM3_PWM_INPUT */ #endif /* BSP_USING_INPUT_CAPTURE_TIM3 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM4 - INPUTCAPTURE_TIM4_CONFIG, +#if BSP_INPUT_CAPTURE_TIM4_PWM_INPUT == 1 + INPUTCAPTURE_TIM4_PWM_CONFIG, +#elif defined(BSP_INPUT_CAPTURE_TIM4_DUAL_EDGE) + INPUTCAPTURE_TIM4_DUAL_CONFIG, +#else +#ifdef BSP_INPUT_CAPTURE_TIM4_CH1 + INPUTCAPTURE_TIM4_CH1_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM4_CH1 */ +#ifdef BSP_INPUT_CAPTURE_TIM4_CH2 + INPUTCAPTURE_TIM4_CH2_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM4_CH2 */ +#ifdef BSP_INPUT_CAPTURE_TIM4_CH3 + INPUTCAPTURE_TIM4_CH3_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM4_CH3 */ +#ifdef BSP_INPUT_CAPTURE_TIM4_CH4 + INPUTCAPTURE_TIM4_CH4_CONFIG, +#endif /* BSP_INPUT_CAPTURE_TIM4_CH4 */ +#endif /* BSP_INPUT_CAPTURE_TIM4_PWM_INPUT */ #endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ }; -/** @brief Runtime state owned by one RT-Thread physical Capture device. */ +/** @brief Runtime state of one independent EDGE, PWM Input or dual-edge device. */ struct stm32_capture_device { - struct rt_inputcapture_device parent; /**< RT-Thread Input Capture object. */ - const struct stm32_inputcapture_config *config; /**< Immutable timer configuration. */ - TIM_HandleTypeDef timer; /**< HAL timer handle owned by this driver. */ - rt_uint32_t pulsewidth_us; /**< Last completed edge interval, in us. */ - rt_uint32_t counter_hz; /**< Timer counter frequency, in Hz. */ - rt_uint32_t previous_count; /**< Last captured CCR value. */ - rt_bool_t first_edge; /**< Ignore the first partial interval. */ - rt_bool_t next_edge_rising; /**< Edge direction tracked for each sample. */ - volatile rt_bool_t active; /**< Capture channel is running. */ + struct rt_inputcapture_device parent; /**< RT-Thread Input Capture device and FIFO. */ + const struct stm32_inputcapture_config *config; /**< Immutable physical-channel configuration. */ + TIM_HandleTypeDef *timer; /**< Shared HAL handle of the owning physical TIM. */ + rt_uint32_t pulsewidth_us; /**< Last completed pulse interval, microseconds. */ + rt_uint32_t counter_hz; /**< Timer counter frequency, hertz. */ + rt_uint32_t previous_count; /**< Previous EDGE or dual-edge capture CCR value. */ + rt_bool_t first_edge; /**< Ignore the first incomplete interval after opening. */ + rt_bool_t next_edge_rising; /**< Next expected EDGE or DUAL_EDGE polarity. */ + volatile rt_bool_t active; /**< Capture device has started. */ }; -/** @brief One Capture device state per enabled timer instance. */ -static struct stm32_capture_device stm32_capture_devices[STM32_CAPTURE_DEVICE_COUNT]; +/** @brief Logical capture-device states in the same order as the configuration table. */ +static struct stm32_capture_device + stm32_capture_devices[sizeof(stm32_capture_configs) / sizeof(stm32_capture_configs[0])]; + +/** + * @brief Resolve the shared HAL handle of one physical TIM. + * @param instance Physical TIM instance. + * @return HAL handle or RT_NULL for an unsupported instance. + */ +static TIM_HandleTypeDef *stm32_capture_get_timer(TIM_TypeDef *instance) +{ +#ifdef BSP_USING_INPUT_CAPTURE_TIM1 + if (instance == TIM1) + { + return &stm32_capture_timers[STM32_CAPTURE_TIM1_INDEX]; + } +#endif /* BSP_USING_INPUT_CAPTURE_TIM1 */ +#ifdef BSP_USING_INPUT_CAPTURE_TIM2 + if (instance == TIM2) + { + return &stm32_capture_timers[STM32_CAPTURE_TIM2_INDEX]; + } +#endif /* BSP_USING_INPUT_CAPTURE_TIM2 */ +#ifdef BSP_USING_INPUT_CAPTURE_TIM3 + if (instance == TIM3) + { + return &stm32_capture_timers[STM32_CAPTURE_TIM3_INDEX]; + } +#endif /* BSP_USING_INPUT_CAPTURE_TIM3 */ +#ifdef BSP_USING_INPUT_CAPTURE_TIM4 + if (instance == TIM4) + { + return &stm32_capture_timers[STM32_CAPTURE_TIM4_INDEX]; + } +#endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ + return RT_NULL; +} + +/** + * @brief Find the logical capture device assigned to a HAL channel interrupt. + * @param timer Shared HAL handle containing the physical TIM instance. + * @param channel HAL TIM_CHANNEL_x selector. + * @return Matching logical device or RT_NULL. + */ +static struct stm32_capture_device *stm32_capture_find_device(TIM_HandleTypeDef *timer, rt_uint32_t channel) +{ + rt_size_t i; /* Logical device index. */ + + for (i = 0; i < sizeof(stm32_capture_devices) / sizeof(stm32_capture_devices[0]); i++) + { + if (stm32_capture_devices[i].timer == timer && + (stm32_capture_devices[i].config->channel == channel +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE + || (stm32_capture_devices[i].config->dual_edge && + (stm32_capture_devices[i].config->channel == TIM_CHANNEL_1 ? TIM_CHANNEL_2 : TIM_CHANNEL_1) == channel) +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + )) + { + return &stm32_capture_devices[i]; + } + } + return RT_NULL; +} + +/** + * @brief Convert HAL's active capture channel into the corresponding CCR selector. + * @param channel HAL active input-capture channel. + * @return TIM_CHANNEL_x selector, or an invalid selector if no channel is active. + */ +static rt_uint32_t stm32_capture_channel(HAL_TIM_ActiveChannel channel) +{ + switch (channel) + { + case HAL_TIM_ACTIVE_CHANNEL_1: + return TIM_CHANNEL_1; + case HAL_TIM_ACTIVE_CHANNEL_2: + return TIM_CHANNEL_2; + case HAL_TIM_ACTIVE_CHANNEL_3: + return TIM_CHANNEL_3; + case HAL_TIM_ACTIVE_CHANNEL_4: + return TIM_CHANNEL_4; + default: + return (rt_uint32_t)-1; + } +} + +/** + * @brief Check whether another logical device has started on this physical timer. + * @param timer Shared physical TIM handle. + * @return RT_TRUE if any logical capture device is running. + * @note Callers serialize first open/close operations on the same TIM. + */ +static rt_bool_t stm32_capture_timer_active(TIM_HandleTypeDef *timer) +{ + rt_size_t i; /* Logical device index. */ + + for (i = 0; i < sizeof(stm32_capture_devices) / sizeof(stm32_capture_devices[0]); i++) + { + if (stm32_capture_devices[i].timer == timer && stm32_capture_devices[i].active) + { + return RT_TRUE; + } + } + return RT_FALSE; +} /** * @brief Obtain the effective timer input clock, including APB prescaling. @@ -136,42 +314,444 @@ static rt_uint32_t stm32_inputcapture_timer_clock(TIM_TypeDef *instance) return pclk * 2U; } +#if defined(BSP_USING_INPUT_CAPTURE_PWM_INPUT) || defined(BSP_USING_INPUT_CAPTURE_DUAL_EDGE) /** - * @brief Initialize one timer for hardware input capture with a 1-channel configuration. - * @param inputcapture Device whose hardware timer is initialized. - * @return RT_EOK, or a negative HAL/clock error code. + * @brief Find the indirect channel paired with a direct capture channel. + * @param channel Direct TI input channel (CH1 or CH2). + * @return Partner channel (CH2 or CH1). + */ +static rt_uint32_t stm32_capture_paired_partner(rt_uint32_t channel) +{ + return channel == TIM_CHANNEL_1 ? TIM_CHANNEL_2 : TIM_CHANNEL_1; +} +#endif /* BSP_USING_INPUT_CAPTURE_PWM_INPUT || BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + +#ifdef BSP_USING_INPUT_CAPTURE_PWM_INPUT +/** + * @brief Configure PWM Input's direct/indirect channels and slave-reset mode. + * @param device PWM Input logical device. + * @return RT_EOK on success, a negative RT-Thread error otherwise. + */ +static rt_err_t stm32_capture_pwm_input_init(struct stm32_capture_device *device) +{ + const struct stm32_inputcapture_config *config = device->config; /* Physical TI configuration. */ + TIM_HandleTypeDef *timer = device->timer; /* Physical TIM handle. */ + TIM_IC_InitTypeDef ic = { 0 }; /* Both capture channels. */ + TIM_SlaveConfigTypeDef slave = { 0 }; /* Reset trigger configuration. */ + + ic.ICPolarity = config->initial_edge_rising ? TIM_INPUTCHANNELPOLARITY_RISING : TIM_INPUTCHANNELPOLARITY_FALLING; + ic.ICSelection = TIM_ICSELECTION_DIRECTTI; + ic.ICPrescaler = TIM_ICPSC_DIV1; + ic.ICFilter = config->input_filter; + if (HAL_TIM_IC_ConfigChannel(timer, &ic, config->channel) != HAL_OK) + { + LOG_E("%s PWM direct channel configuration failed", config->name); + return -RT_ERROR; + } + ic.ICSelection = TIM_ICSELECTION_INDIRECTTI; + ic.ICPolarity = config->initial_edge_rising ? TIM_INPUTCHANNELPOLARITY_FALLING : TIM_INPUTCHANNELPOLARITY_RISING; + if (HAL_TIM_IC_ConfigChannel(timer, &ic, stm32_capture_paired_partner(config->channel)) != HAL_OK) + { + LOG_E("%s PWM indirect channel configuration failed", config->name); + return -RT_ERROR; + } + slave.SlaveMode = TIM_SLAVEMODE_RESET; + slave.InputTrigger = config->channel == TIM_CHANNEL_1 ? TIM_TS_TI1FP1 : TIM_TS_TI2FP2; + slave.TriggerPolarity = config->initial_edge_rising ? TIM_TRIGGERPOLARITY_RISING : TIM_TRIGGERPOLARITY_FALLING; + slave.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1; + slave.TriggerFilter = config->input_filter; + if (HAL_TIM_SlaveConfigSynchro(timer, &slave) != HAL_OK) + { + LOG_E("%s PWM reset trigger configuration failed", config->name); + return -RT_ERROR; + } + /* With URS=1, only CNT overflow sets UIF; the normal slave reset does not. */ + __HAL_TIM_URS_ENABLE(timer); + return RT_EOK; +} + +/** + * @brief Start PWM Input's indirect latch and direct-channel capture interrupt. + * @param device PWM Input logical device. + * @return HAL_OK on success, HAL error code otherwise. + */ +static HAL_StatusTypeDef stm32_capture_pwm_input_open(struct stm32_capture_device *device) +{ + TIM_HandleTypeDef *timer = device->timer; /* Physical TIM handle. */ + rt_uint32_t partner = stm32_capture_paired_partner(device->config->channel); /* Indirect channel. */ + HAL_StatusTypeDef status; /* Channel start result. */ + + status = HAL_TIM_IC_Start(timer, partner); + if (status != HAL_OK) + { + return status; + } + /* Let the indirect channel latch without IRQ; only the reset edge interrupts. */ + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC1OF | TIM_FLAG_CC2OF | + TIM_FLAG_UPDATE); + status = HAL_TIM_IC_Start_IT(timer, device->config->channel); + if (status != HAL_OK) + { + HAL_TIM_IC_Stop(timer, partner); + } + return status; +} + +/** + * @brief Stop both PWM Input channels while keeping a failed close retryable. + * @param device PWM Input logical device. + * @return HAL_OK on success, HAL error code otherwise. + */ +static HAL_StatusTypeDef stm32_capture_pwm_input_close(struct stm32_capture_device *device) +{ + TIM_HandleTypeDef *timer = device->timer; /* Physical TIM handle. */ + HAL_StatusTypeDef status; /* Channel stop result. */ + + status = HAL_TIM_IC_Stop_IT(timer, device->config->channel); + if (status != HAL_OK) + { + return status; + } + status = HAL_TIM_IC_Stop(timer, stm32_capture_paired_partner(device->config->channel)); + if (status != HAL_OK) + { + HAL_TIM_IC_Start_IT(timer, device->config->channel); + } + return status; +} + +/** + * @brief Convert one complete PWM Input period into high/low pulse records. + * @param device PWM Input logical device whose reset-edge capture completed. + * @note Indirect captures have no IRQ, avoiding cross-channel callback ordering. + */ +static void stm32_capture_pwm_input_sample(struct stm32_capture_device *device) +{ + const struct stm32_inputcapture_config *config = device->config; /* Direct TI and reset edge. */ + TIM_HandleTypeDef *timer = device->timer; /* Physical timer. */ + rt_uint32_t partner = stm32_capture_paired_partner(config->channel); /* Indirect CCR. */ + rt_uint32_t partner_flag = partner == TIM_CHANNEL_1 ? TIM_FLAG_CC1 : TIM_FLAG_CC2; /* Pending capture. */ + rt_uint32_t partner_overflow = partner == TIM_CHANNEL_1 ? TIM_FLAG_CC1OF : TIM_FLAG_CC2OF; /* Indirect overrun. */ + rt_uint32_t direct_overflow = config->channel == TIM_CHANNEL_1 ? TIM_FLAG_CC1OF : TIM_FLAG_CC2OF; /* Reset overrun. */ + rt_uint32_t flags = timer->Instance->SR; /* Pending capture, overrun and update flags. */ + rt_uint32_t period = HAL_TIM_ReadCapturedValue(timer, config->channel); /* Reset-to-reset interval. */ + rt_uint32_t segment = HAL_TIM_ReadCapturedValue(timer, partner); /* First part of the period. */ + /* With URS set, a latched UIF means this period crossed ARR and the CCR values are truncated. */ + rt_bool_t valid = !device->first_edge && + (flags & TIM_FLAG_UPDATE) == 0U && + (flags & partner_flag) != 0U && + (flags & partner_overflow) == 0U && + (flags & direct_overflow) == 0U && + period != 0U && segment < period; /* Reject initial/overcaptured or incomplete periods. */ + + device->first_edge = RT_FALSE; + /* Snapshot SR before CCR reads; do not clear CCxIF again or lose a newer capture. */ + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_UPDATE | partner_overflow | direct_overflow); + if (!valid) + { + return; + } + /* A rising reset edge yields high then low; falling yields low then high. */ + device->pulsewidth_us = + (rt_uint32_t)(((rt_uint64_t)segment * 1000000ULL + device->counter_hz / 2U) / device->counter_hz); + rt_hw_inputcapture_isr(&device->parent, config->initial_edge_rising); + device->pulsewidth_us = + (rt_uint32_t)(((rt_uint64_t)(period - segment) * 1000000ULL + device->counter_hz / 2U) / + device->counter_hz); + rt_hw_inputcapture_isr(&device->parent, !config->initial_edge_rising); +} +#endif /* BSP_USING_INPUT_CAPTURE_PWM_INPUT */ + +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE +/** + * @brief Configure two capture channels on one TI input without slave reset. + * @param device Exclusive dual-edge capture device. + * @return RT_EOK or an RT-Thread hardware configuration error. + */ +static rt_err_t stm32_capture_dual_edge_init(struct stm32_capture_device *device) +{ + const struct stm32_inputcapture_config *config = device->config; /* Direct channel and polarity. */ + TIM_HandleTypeDef *timer = device->timer; /* Shared physical TIM. */ + TIM_IC_InitTypeDef ic = { 0 }; /* Direct/indirect capture setup. */ + + ic.ICPolarity = config->initial_edge_rising ? TIM_INPUTCHANNELPOLARITY_RISING : TIM_INPUTCHANNELPOLARITY_FALLING; + ic.ICSelection = TIM_ICSELECTION_DIRECTTI; + ic.ICPrescaler = TIM_ICPSC_DIV1; + ic.ICFilter = config->input_filter; + if (HAL_TIM_IC_ConfigChannel(timer, &ic, config->channel) != HAL_OK) + { + LOG_E("%s dual-edge direct channel configuration failed", config->name); + return -RT_ERROR; + } + /* The paired channel timestamps the opposite edge of this same physical TI pin. */ + ic.ICSelection = TIM_ICSELECTION_INDIRECTTI; + ic.ICPolarity = config->initial_edge_rising ? TIM_INPUTCHANNELPOLARITY_FALLING : TIM_INPUTCHANNELPOLARITY_RISING; + ic.ICFilter = config->dual_partner_filter; + if (HAL_TIM_IC_ConfigChannel(timer, &ic, stm32_capture_paired_partner(config->channel)) != HAL_OK) + { + LOG_E("%s dual-edge indirect channel configuration failed", config->name); + return -RT_ERROR; + } + return RT_EOK; +} + +/** + * @brief Start both dual-edge capture interrupts and undo the first on failure. + * @param device Exclusive dual-edge capture device. + * @return HAL_OK or a HAL channel-start failure. + */ +static HAL_StatusTypeDef stm32_capture_dual_edge_open(struct stm32_capture_device *device) +{ + TIM_HandleTypeDef *timer = device->timer; /* Shared physical TIM. */ + rt_uint32_t partner = stm32_capture_paired_partner(device->config->channel); /* Indirect channel. */ + HAL_StatusTypeDef status; /* Channel start/stop result. */ + + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC1 | TIM_FLAG_CC2 | TIM_FLAG_CC1OF | TIM_FLAG_CC2OF); + status = HAL_TIM_IC_Start_IT(timer, partner); + if (status != HAL_OK) + { + return status; + } + status = HAL_TIM_IC_Start_IT(timer, device->config->channel); + if (status != HAL_OK) + { + if (HAL_TIM_IC_Stop_IT(timer, partner) != HAL_OK) + { + LOG_E("%s dual-edge start rollback failed", device->config->name); + } + } + return status; +} + +/** + * @brief Stop both dual-edge channels; restore the first if the second fails. + * @param device Exclusive dual-edge capture device. + * @return HAL_OK on success, HAL error code otherwise. + */ +static HAL_StatusTypeDef stm32_capture_dual_edge_close(struct stm32_capture_device *device) +{ + TIM_HandleTypeDef *timer = device->timer; /* Shared physical TIM. */ + HAL_StatusTypeDef status; /* Channel stop result. */ + + status = HAL_TIM_IC_Stop_IT(timer, device->config->channel); + if (status != HAL_OK) + { + return status; + } + status = HAL_TIM_IC_Stop_IT(timer, stm32_capture_paired_partner(device->config->channel)); + if (status != HAL_OK) + { + if (HAL_TIM_IC_Start_IT(timer, device->config->channel) != HAL_OK) + { + LOG_E("%s dual-edge close rollback failed", device->config->name); + } + } + return status; +} + +/** + * @brief Report one already-latched dual-edge timestamp in chronological order. + * @param device Exclusive paired-channel Input Capture device. + * @param channel Physical channel that latched the edge. + * @param captured CCR timestamp read by the IRQ dispatcher. + * @note CCR differences are modulo ARR+1: intervals at least one full wrap are ambiguous. + */ +static void stm32_capture_dual_edge_sample(struct stm32_capture_device *device, + rt_uint32_t channel, rt_uint32_t captured) +{ + TIM_HandleTypeDef *timer = device->timer; /* Shared free-running TIM counter. */ + rt_uint32_t delta; /* Elapsed ticks modulo timer ARR+1. */ + rt_bool_t was_high; /* Level ending at the captured edge. */ + + if (!device->first_edge) + { + if (captured >= device->previous_count) + { + delta = captured - device->previous_count; + } + else + { + delta = (timer->Init.Period - device->previous_count + 1U) + captured; + } + if (delta != 0U) + { + /* A rising edge closes LOW; a falling edge closes HIGH, regardless of channel order. */ + was_high = (channel == device->config->channel) ? !device->config->initial_edge_rising + : device->config->initial_edge_rising; + device->pulsewidth_us = + (rt_uint32_t)(((rt_uint64_t)delta * 1000000ULL + device->counter_hz / 2U) / + device->counter_hz); + rt_hw_inputcapture_isr(&device->parent, was_high); + } + } + device->previous_count = captured; + device->first_edge = RT_FALSE; + /* A valid next edge on the same TI input must have the opposite polarity. */ + device->next_edge_rising = (channel == device->config->channel) ? !device->config->initial_edge_rising + : device->config->initial_edge_rising; +} + +/** + * @brief Consume paired captures and identify whether DUAL_EDGE owns this timer IRQ. + * @param timer Shared physical TIM handle for the incoming interrupt. + * @return RT_TRUE if DUAL_EDGE handled the IRQ, including inactive or discarded events; + * RT_FALSE if the IRQ must be dispatched through the normal HAL handler. + * @note Reading a CCR acknowledges CCxIF, so HAL must not redispatch a handled dual-edge IRQ. + */ +static rt_bool_t stm32_capture_dual_edge_handle_irq(TIM_HandleTypeDef *timer) +{ + struct stm32_capture_device *device = stm32_capture_find_device(timer, TIM_CHANNEL_1); /* Paired TI owner. */ + + if (device != RT_NULL && device->config->dual_edge) + { + if (device->active) + { + rt_uint32_t flags = timer->Instance->SR; /* Pending edge/overcapture snapshot. */ + rt_uint32_t pending = flags & (TIM_FLAG_CC1 | TIM_FLAG_CC2); /* Captures to consume. */ + rt_uint32_t cc1 = 0U; /* CH1 timestamp, valid only when CC1IF was set. */ + rt_uint32_t cc2 = 0U; /* CH2 timestamp, valid only when CC2IF was set. */ + rt_uint32_t overcapture; /* A lost capture makes both intervals untrustworthy. */ + rt_uint32_t delta1; /* CH1 elapsed ticks from the previous accepted edge. */ + rt_uint32_t delta2; /* CH2 elapsed ticks from the previous accepted edge. */ + rt_uint32_t expected; /* Channel of the next physical polarity after the last accepted edge. */ + + /* Reading an input CCR clears CCxIF. Do not clear it again after the read. */ + if (pending & TIM_FLAG_CC1) + { + cc1 = HAL_TIM_ReadCapturedValue(timer, TIM_CHANNEL_1); + } + if (pending & TIM_FLAG_CC2) + { + cc2 = HAL_TIM_ReadCapturedValue(timer, TIM_CHANNEL_2); + } + overcapture = (flags | timer->Instance->SR) & (TIM_FLAG_CC1OF | TIM_FLAG_CC2OF); + if (overcapture) + { + __HAL_TIM_CLEAR_FLAG(timer, overcapture); + device->first_edge = RT_TRUE; + return RT_TRUE; + } + + if (pending == (TIM_FLAG_CC1 | TIM_FLAG_CC2)) + { + /* HAL serves CC1 before CC2, which is not necessarily physical edge order. + * The previous timestamp can order both events only within one counter wrap. */ + if (device->first_edge) + { + return RT_TRUE; /* Initial paired events have no known chronological anchor. */ + } + delta1 = (cc1 >= device->previous_count) ? cc1 - device->previous_count + : (timer->Init.Period - device->previous_count + 1U) + cc1; + delta2 = (cc2 >= device->previous_count) ? cc2 - device->previous_count + : (timer->Init.Period - device->previous_count + 1U) + cc2; + expected = (device->next_edge_rising == device->config->initial_edge_rising) + ? device->config->channel + : stm32_capture_paired_partner(device->config->channel); + if (delta1 == 0U || delta2 == 0U || delta1 == delta2 || + (delta1 < delta2 ? TIM_CHANNEL_1 : TIM_CHANNEL_2) != expected) + { + /* Lost or ambiguous ordering must not produce a wrapped, false pulse. */ + device->first_edge = RT_TRUE; + return RT_TRUE; + } + if (delta1 < delta2) + { + stm32_capture_dual_edge_sample(device, TIM_CHANNEL_1, cc1); + stm32_capture_dual_edge_sample(device, TIM_CHANNEL_2, cc2); + } + else + { + stm32_capture_dual_edge_sample(device, TIM_CHANNEL_2, cc2); + stm32_capture_dual_edge_sample(device, TIM_CHANNEL_1, cc1); + } + } + /* A single CCxIF is normal in DUAL_EDGE. Unlike legacy EDGE mode, + * both polarities remain armed instead of switching the active edge. + * As in the legacy driver, a missed opposite edge cannot be recovered: + * a later same-polarity capture may report an incorrect long interval. + * No polarity-alternation check is applied to single pending events. */ + else if (pending & TIM_FLAG_CC1) + { + stm32_capture_dual_edge_sample(device, TIM_CHANNEL_1, cc1); + } + else if (pending & TIM_FLAG_CC2) + { + stm32_capture_dual_edge_sample(device, TIM_CHANNEL_2, cc2); + } + } + return RT_TRUE; /* DUAL_EDGE owns both capture IRQs; HAL must not dispatch them again. */ + } + return RT_FALSE; +} +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + +/** + * @brief Dispatch DUAL_EDGE captures in timestamp order, otherwise use HAL's normal TIM IRQ path. + * @param timer Shared physical TIM handle. + */ +static void stm32_capture_handle_irq(TIM_HandleTypeDef *timer) +{ +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE + if (stm32_capture_dual_edge_handle_irq(timer)) + { + return; + } +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + HAL_TIM_IRQHandler(timer); +} + +/** + * @brief Initialize the shared physical TIM and this device's capture channel. + * @param inputcapture RT-Thread capture device to initialize. + * @return RT_EOK on success, negative RT-Thread error otherwise. */ static rt_err_t stm32_capture_init(struct rt_inputcapture_device *inputcapture) { struct stm32_capture_device *device = - rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Capture state. */ + rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Logical capture state. */ const struct stm32_inputcapture_config *config = device->config; /* Board mapping. */ - TIM_HandleTypeDef *timer = &device->timer; /* Private HAL TIM handle. */ - TIM_IC_InitTypeDef ic = { 0 }; /* Single direct-TI input channel setup. */ - rt_uint32_t clock = stm32_inputcapture_timer_clock(config->instance); /* Effective TIM clock. */ + TIM_HandleTypeDef *timer = device->timer; /* Shared HAL timer handle. */ + TIM_IC_InitTypeDef ic = { 0 }; /* EDGE channel settings. */ + rt_uint32_t clock = stm32_inputcapture_timer_clock(config->instance); /* Physical TIM clock. */ if (clock < BSP_INPUT_CAPTURE_COUNTER_HZ || clock % BSP_INPUT_CAPTURE_COUNTER_HZ != 0U) { + LOG_E("%s unsupported TIM clock %u for %u Hz", config->name, clock, BSP_INPUT_CAPTURE_COUNTER_HZ); return -RT_EINVAL; } - timer->Instance = config->instance; - timer->Init.Prescaler = clock / BSP_INPUT_CAPTURE_COUNTER_HZ - 1U; - timer->Init.CounterMode = TIM_COUNTERMODE_UP; - timer->Init.Period = config->counter_period; - /* Use the full hardware range only when this specific TIM has a 32-bit counter. */ - if (STM32_INPUTCAPTURE_TIM_HAS_32BIT_COUNTER(config->instance)) + /* The first device initializes shared PSC/ARR and CubeMX IC MSP only once. */ + if (timer->State == HAL_TIM_STATE_RESET) { - timer->Init.Period = 0xFFFFFFFFU; + timer->Instance = config->instance; + timer->Init.Prescaler = clock / BSP_INPUT_CAPTURE_COUNTER_HZ - 1U; + timer->Init.CounterMode = TIM_COUNTERMODE_UP; + timer->Init.Period = config->counter_period; + /* Use the full hardware range only when this specific TIM has a 32-bit counter. */ + if (STM32_INPUTCAPTURE_TIM_HAS_32BIT_COUNTER(config->instance)) + { + timer->Init.Period = 0xFFFFFFFFU; + } + timer->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + timer->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_IC_Init(timer) != HAL_OK) + { + LOG_E("%s timer HAL initialization failed", config->name); + return -RT_ERROR; + } } - timer->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; - timer->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; device->counter_hz = BSP_INPUT_CAPTURE_COUNTER_HZ; - - /* HAL_TIM_IC_Init invokes the HAL_TIM_IC_MspInit provided by CubeMX. */ - if (HAL_TIM_IC_Init(timer) != HAL_OK) +#ifdef BSP_USING_INPUT_CAPTURE_PWM_INPUT + if (config->pwm_input) { - return -RT_ERROR; + return stm32_capture_pwm_input_init(device); } +#endif /* BSP_USING_INPUT_CAPTURE_PWM_INPUT */ +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE + if (config->dual_edge) + { + return stm32_capture_dual_edge_init(device); + } +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ ic.ICPolarity = config->initial_edge_rising ? TIM_INPUTCHANNELPOLARITY_RISING : TIM_INPUTCHANNELPOLARITY_FALLING; if (config->hardware_both_edges) @@ -179,7 +759,6 @@ static rt_err_t stm32_capture_init(struct rt_inputcapture_device *inputcapture) #if STM32_INPUTCAPTURE_HAL_HAS_BOTHEDGE_POLARITY ic.ICPolarity = STM32_INPUTCAPTURE_HAL_BOTHEDGE_POLARITY; #else - HAL_TIM_IC_DeInit(timer); return -RT_ENOSYS; #endif /* STM32_INPUTCAPTURE_HAL_HAS_BOTHEDGE_POLARITY */ } @@ -188,23 +767,24 @@ static rt_err_t stm32_capture_init(struct rt_inputcapture_device *inputcapture) ic.ICFilter = config->input_filter; if (HAL_TIM_IC_ConfigChannel(timer, &ic, config->channel) != HAL_OK) { - HAL_TIM_IC_DeInit(timer); + LOG_E("%s EDGE channel configuration failed", config->name); return -RT_ERROR; } return RT_EOK; } /** - * @brief Start the selected Capture channel after RT-Thread creates its FIFO. - * @param inputcapture RT-Thread Input Capture device to open. + * @brief Start capture on this logical device without resetting a running timer. + * @param inputcapture Device to open. * @return RT_EOK or a negative FIFO/HAL error code. */ static rt_err_t stm32_capture_open(struct rt_inputcapture_device *inputcapture) { struct stm32_capture_device *device = - rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Capture state. */ - const struct stm32_inputcapture_config *config = device->config; /* Board mapping. */ - TIM_HandleTypeDef *timer = &device->timer; /* Configured timer. */ + rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Logical capture device. */ + const struct stm32_inputcapture_config *config = device->config; /* Capture config. */ + TIM_HandleTypeDef *timer = device->timer; /* Shared timer. */ + HAL_StatusTypeDef status; /* Capture start result. */ if (!inputcapture->ringbuff) { @@ -214,75 +794,117 @@ static rt_err_t stm32_capture_open(struct rt_inputcapture_device *inputcapture) device->previous_count = 0U; device->pulsewidth_us = 0U; device->next_edge_rising = config->initial_edge_rising; - __HAL_TIM_SET_COUNTER(timer, 0U); - if (!config->hardware_both_edges) + /* CNT is shared; only the first active logical device may reset it. */ + if (!stm32_capture_timer_active(timer)) { - __HAL_TIM_SET_CAPTUREPOLARITY(timer, config->channel, - device->next_edge_rising ? TIM_INPUTCHANNELPOLARITY_RISING : TIM_INPUTCHANNELPOLARITY_FALLING); + __HAL_TIM_SET_COUNTER(timer, 0U); + } + /* HAL can dispatch CC IRQ before Start_IT returns; publish the ISR gate first. */ + device->active = RT_TRUE; +#ifdef BSP_USING_INPUT_CAPTURE_PWM_INPUT + if (config->pwm_input) + { + status = stm32_capture_pwm_input_open(device); } - /* Clear stale capture and overcapture flags before enabling this channel's interrupt. */ - switch (config->channel) + else +#endif /* BSP_USING_INPUT_CAPTURE_PWM_INPUT */ +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE + if (config->dual_edge) { - case TIM_CHANNEL_1: - __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC1 | TIM_FLAG_CC1OF); - break; - case TIM_CHANNEL_2: - __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC2 | TIM_FLAG_CC2OF); - break; - case TIM_CHANNEL_3: - __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC3 | TIM_FLAG_CC3OF); - break; - case TIM_CHANNEL_4: - __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC4 | TIM_FLAG_CC4OF); - break; + status = stm32_capture_dual_edge_open(device); } - device->active = RT_TRUE; - if (HAL_TIM_IC_Start_IT(timer, config->channel) != HAL_OK) + else +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + { + if (!config->hardware_both_edges) + { + __HAL_TIM_SET_CAPTUREPOLARITY(timer, config->channel, + device->next_edge_rising ? TIM_INPUTCHANNELPOLARITY_RISING : TIM_INPUTCHANNELPOLARITY_FALLING); + } + /* Clear stale capture and overcapture flags for this EDGE channel only. */ + switch (config->channel) + { + case TIM_CHANNEL_1: + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC1 | TIM_FLAG_CC1OF); + break; + case TIM_CHANNEL_2: + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC2 | TIM_FLAG_CC2OF); + break; + case TIM_CHANNEL_3: + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC3 | TIM_FLAG_CC3OF); + break; + case TIM_CHANNEL_4: + __HAL_TIM_CLEAR_FLAG(timer, TIM_FLAG_CC4 | TIM_FLAG_CC4OF); + break; + } + status = HAL_TIM_IC_Start_IT(timer, config->channel); + } + if (status != HAL_OK) { device->active = RT_FALSE; + LOG_E("%s capture start failed", config->name); return -RT_ERROR; } return RT_EOK; } /** - * @brief Stop the Capture channel without modifying CubeMX-owned NVIC state. - * @param inputcapture RT-Thread device to stop. - * @return RT_EOK or a negative HAL stop error. + * @brief Stop only this logical device's channels, retaining the shared TIM. + * @param inputcapture Device to stop. + * @return RT_EOK or a negative HAL error code. */ static rt_err_t stm32_capture_close(struct rt_inputcapture_device *inputcapture) { struct stm32_capture_device *device = - rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Capture state. */ + rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Logical capture device. */ + HAL_StatusTypeDef status; /* HAL channel stop result. */ + if (!device->active) { return RT_EOK; } - if (HAL_TIM_IC_Stop_IT(&device->timer, device->config->channel) != HAL_OK) +#ifdef BSP_USING_INPUT_CAPTURE_PWM_INPUT + if (device->config->pwm_input) { + status = stm32_capture_pwm_input_close(device); + } + else +#endif /* BSP_USING_INPUT_CAPTURE_PWM_INPUT */ +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE + if (device->config->dual_edge) + { + status = stm32_capture_dual_edge_close(device); + } + else +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + { + status = HAL_TIM_IC_Stop_IT(device->timer, device->config->channel); + } + if (status != HAL_OK) + { + LOG_E("%s capture stop failed", device->config->name); return -RT_ERROR; } - /* Only clear the ISR gate after HAL confirms the capture channel has stopped. */ device->active = RT_FALSE; return RT_EOK; } /** - * @brief Return the most recently recorded pulse duration to RT-Thread's FIFO. - * @param inputcapture Source Capture device. - * @param pulsewidth_us Output pulse duration in microseconds. + * @brief Return the last recorded pulse duration to RT-Thread's FIFO. + * @param inputcapture Source capture device. + * @param pulsewidth_us Output duration, microseconds. * @return RT_EOK. */ static rt_err_t stm32_capture_get_pulsewidth(struct rt_inputcapture_device *inputcapture, rt_uint32_t *pulsewidth_us) { struct stm32_capture_device *device = - rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Capture state. */ + rt_container_of(inputcapture, struct stm32_capture_device, parent); /* Logical capture device. */ *pulsewidth_us = device->pulsewidth_us; return RT_EOK; } -/** @brief RT-Thread Input Capture HAL backend operations. */ +/** @brief RT-Thread Input Capture backend operations. */ static const struct rt_inputcapture_ops stm32_capture_ops = { .init = stm32_capture_init, .open = stm32_capture_open, @@ -291,28 +913,37 @@ static const struct rt_inputcapture_ops stm32_capture_ops = { }; /** - * @brief Push a complete interval from the only configured capture channel. - * @param timer HAL timer for which Input Capture completed. - * @note The IRQ caller must pass a driver-owned timer handle and the selected - * capture channel. Handle/channel validation is intentionally omitted - * from this hot path; a foreign callback can cause invalid memory access. - * @note Software polarity switching can miss short edges; hardware both-edge - * sampling assumes every transition is observed in order. Intervals - * longer than one counter period cannot be reconstructed without overflow tracking. + * @brief Route a physical TIM capture interrupt to its owning logical device. + * @param timer Shared HAL timer handle reporting the captured CCR channel. */ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *timer) { - struct stm32_capture_device *device = - rt_container_of(timer, struct stm32_capture_device, timer); /* Owner of the HAL TIM handle. */ - const struct stm32_inputcapture_config *config = device->config; /* Selected channel mapping. */ - rt_uint32_t captured; /* Hardware CCR value at the selected edge. */ - rt_uint32_t delta; /* Timer ticks elapsed, permitting one wrap. */ - rt_bool_t was_high; /* Logic level of the interval just ended. */ + rt_uint32_t channel = stm32_capture_channel(timer->Channel); /* Selected HAL CCR channel. */ + struct stm32_capture_device *device = stm32_capture_find_device(timer, channel); /* Logical owner. */ + const struct stm32_inputcapture_config *config; /* Board channel configuration. */ + rt_uint32_t captured; /* Timestamp captured by hardware. */ + rt_uint32_t delta; /* Ticks since the preceding captured edge. */ + rt_bool_t was_high; /* Level of the completed interval. */ - if (!device->active) + if (device == RT_NULL || !device->active) { return; } + config = device->config; +#ifdef BSP_USING_INPUT_CAPTURE_PWM_INPUT + if (config->pwm_input) + { + stm32_capture_pwm_input_sample(device); + return; + } +#endif /* BSP_USING_INPUT_CAPTURE_PWM_INPUT */ +#ifdef BSP_USING_INPUT_CAPTURE_DUAL_EDGE + if (config->dual_edge) + { + return; /* Paired channels are consumed together by the TIM IRQ dispatcher. */ + } +#endif /* BSP_USING_INPUT_CAPTURE_DUAL_EDGE */ + captured = HAL_TIM_ReadCapturedValue(timer, config->channel); was_high = device->next_edge_rising ? RT_FALSE : RT_TRUE; device->next_edge_rising = !device->next_edge_rising; @@ -340,66 +971,70 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *timer) } /** - * @brief Register every timer selected in the board's Input Capture configuration. - * @return RT_EOK or the first registration failure. + * @brief Register one RT-Thread device per EDGE channel or per exclusive paired-input TIM. + * @return RT_EOK or a configuration/registration failure. */ int rt_hw_inputcapture_init(void) { - rt_size_t i; /* Current physical timer. */ - rt_err_t status; /* Device registration status. */ + rt_size_t i; /* Current logical capture device. */ + rt_err_t status; /* Registration result. */ - for (i = 0; i < STM32_CAPTURE_DEVICE_COUNT; ++i) + for (i = 0; i < sizeof(stm32_capture_devices) / sizeof(stm32_capture_devices[0]); i++) { stm32_capture_devices[i].config = &stm32_capture_configs[i]; + stm32_capture_devices[i].timer = stm32_capture_get_timer(stm32_capture_configs[i].instance); stm32_capture_devices[i].parent.ops = &stm32_capture_ops; status = rt_device_inputcapture_register(&stm32_capture_devices[i].parent, stm32_capture_configs[i].name, &stm32_capture_devices[i]); if (status != RT_EOK) { + LOG_E("%s registration failed: %d", stm32_capture_configs[i].name, status); return status; } + LOG_D("%s registered", stm32_capture_configs[i].name); } return RT_EOK; } INIT_DEVICE_EXPORT(rt_hw_inputcapture_init); #ifdef BSP_USING_INPUT_CAPTURE_TIM1 -/** @brief Dispatch TIM1 Capture IRQ within one RT-Thread interrupt boundary. */ +/** @brief Dispatch TIM1 input-capture IRQ under the RT-Thread ISR boundary. */ void TIM1_CC_IRQHandler(void) { rt_interrupt_enter(); - HAL_TIM_IRQHandler(&stm32_capture_devices[STM32_CAPTURE_TIM1_INDEX].timer); + stm32_capture_handle_irq(&stm32_capture_timers[STM32_CAPTURE_TIM1_INDEX]); rt_interrupt_leave(); } #endif /* BSP_USING_INPUT_CAPTURE_TIM1 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM2 -/** @brief Dispatch TIM2 Capture IRQ within one RT-Thread interrupt boundary. */ +/** @brief Dispatch TIM2 input-capture IRQ under the RT-Thread ISR boundary. */ void TIM2_IRQHandler(void) { rt_interrupt_enter(); - HAL_TIM_IRQHandler(&stm32_capture_devices[STM32_CAPTURE_TIM2_INDEX].timer); + stm32_capture_handle_irq(&stm32_capture_timers[STM32_CAPTURE_TIM2_INDEX]); rt_interrupt_leave(); } #endif /* BSP_USING_INPUT_CAPTURE_TIM2 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM3 -/** @brief Dispatch TIM3 Capture IRQ within one RT-Thread interrupt boundary. */ +/** @brief Dispatch TIM3 input-capture IRQ under the RT-Thread ISR boundary. */ void TIM3_IRQHandler(void) { rt_interrupt_enter(); - HAL_TIM_IRQHandler(&stm32_capture_devices[STM32_CAPTURE_TIM3_INDEX].timer); + stm32_capture_handle_irq(&stm32_capture_timers[STM32_CAPTURE_TIM3_INDEX]); rt_interrupt_leave(); } #endif /* BSP_USING_INPUT_CAPTURE_TIM3 */ #ifdef BSP_USING_INPUT_CAPTURE_TIM4 -/** @brief Dispatch TIM4 Capture IRQ within one RT-Thread interrupt boundary. */ +/** @brief Dispatch TIM4 input-capture IRQ under the RT-Thread ISR boundary. */ void TIM4_IRQHandler(void) { rt_interrupt_enter(); - HAL_TIM_IRQHandler(&stm32_capture_devices[STM32_CAPTURE_TIM4_INDEX].timer); + stm32_capture_handle_irq(&stm32_capture_timers[STM32_CAPTURE_TIM4_INDEX]); rt_interrupt_leave(); } #endif /* BSP_USING_INPUT_CAPTURE_TIM4 */ + diff --git a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.h b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.h index 499f7fb278..964326a319 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.h +++ b/bsp/stm32/libraries/HAL_Drivers/drivers/drv_inputcapture.h @@ -9,11 +9,6 @@ #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 @@ -22,28 +17,28 @@ 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. - */ + * @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. */ + const char *name; /**< Registered RT-Thread device name. */ + TIM_TypeDef *instance; /**< Physical timer peripheral instance. */ + rt_uint32_t channel; /**< Selected direct-TI HAL TIM_CHANNEL_x. */ + rt_uint32_t input_filter; /**< HAL input digital filter value. */ + rt_uint32_t counter_period; /**< Configured timer auto-reload value. */ + 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. */ + rt_bool_t pwm_input; /**< PWM Input uses CH1/CH2 and reserves the entire physical TIM. */ + rt_bool_t dual_edge; /**< Pair CH1/CH2 for independent edge timestamps on one TI input. */ + rt_uint32_t dual_partner_filter; /**< Digital filter for the indirect channel in dual-edge mode. */ }; /** - * @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. - */ + * @brief Register enabled STM32 Input Capture timers with RT-Thread. + * @return RT_EOK or the first RT-Thread registration error. + */ int rt_hw_inputcapture_init(void); #ifdef __cplusplus |
