This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit a4b17a2d14cd023d5d468a31b9b11cb9bd7b16b6 Author: Côme VINCENT <44554692+com...@users.noreply.github.com> AuthorDate: Mon Aug 11 12:35:55 2025 -0400 arch/stm32h7: Fix timer capture This patch fixes an incorrect call to stm32_cap_initialize() in stm32_bringup.c: the call was made without the channel parameter. Instead of adding the channel in the call, the channel is selected by stm32_cap_gpio() (first available channel). This patch also fixes incorrect driver registration in drivers/timers/capture.c: the driver was registered with the wrong name (/dev/cap -> /dev/capture). Also added more error checking in cap_register_multiple(). Signed-off-by: Côme VINCENT <44554692+com...@users.noreply.github.com> --- arch/arm/src/stm32h7/stm32_capture.c | 34 +++++++++++++++++++++++--- arch/arm/src/stm32h7/stm32_capture.h | 6 ++--- arch/arm/src/stm32h7/stm32_capture_lowerhalf.c | 2 +- drivers/timers/capture.c | 23 ++++++++++++++--- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/arch/arm/src/stm32h7/stm32_capture.c b/arch/arm/src/stm32h7/stm32_capture.c index 3b740c38160..1cc71acd0ca 100644 --- a/arch/arm/src/stm32h7/stm32_capture.c +++ b/arch/arm/src/stm32h7/stm32_capture.c @@ -217,6 +217,32 @@ static inline uint32_t stm32_cap_gpio(const struct stm32_cap_priv_s *priv, int channel) { + if (channel < 0) /* Special case: return first available channel */ + { + uint32_t gpio; + + /* Try counter channel first */ + + gpio = stm32_cap_gpio(priv, STM32_CAP_CHANNEL_COUNTER); + if (gpio) + { + return gpio; + } + + /* Then try channels 1..4 */ + + for (int ch = 1; ch <= 4; ch++) + { + gpio = stm32_cap_gpio(priv, ch); + if (gpio) + { + return gpio; + } + } + + return 0; /* None found */ + } + switch (priv->base) { #ifdef CONFIG_STM32H7_TIM1_CAP @@ -1578,7 +1604,7 @@ static inline const struct stm32_cap_priv_s * stm32_cap_get_priv(int timer) * Public Function - Initialization ****************************************************************************/ -struct stm32_cap_dev_s *stm32_cap_init(int timer, uint8_t channel) +struct stm32_cap_dev_s *stm32_cap_init(int timer) { const struct stm32_cap_priv_s *priv = stm32_cap_get_priv(timer); uint32_t gpio; @@ -1587,7 +1613,7 @@ struct stm32_cap_dev_s *stm32_cap_init(int timer, uint8_t channel) { stm32_cap_set_rcc(priv, true); - gpio = stm32_cap_gpio(priv, channel); + gpio = stm32_cap_gpio(priv, -1); if (gpio) { stm32_configgpio(gpio); @@ -1601,7 +1627,7 @@ struct stm32_cap_dev_s *stm32_cap_init(int timer, uint8_t channel) return (struct stm32_cap_dev_s *)priv; } -int stm32_cap_deinit(struct stm32_cap_dev_s * dev, uint8_t channel) +int stm32_cap_deinit(struct stm32_cap_dev_s * dev) { const struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; uint32_t gpio; @@ -1612,7 +1638,7 @@ int stm32_cap_deinit(struct stm32_cap_dev_s * dev, uint8_t channel) stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); - gpio = stm32_cap_gpio(priv, channel); + gpio = stm32_cap_gpio(priv, -1); if (gpio) { stm32_unconfiggpio(gpio); diff --git a/arch/arm/src/stm32h7/stm32_capture.h b/arch/arm/src/stm32h7/stm32_capture.h index 954cd911a25..89d09b5c5f4 100644 --- a/arch/arm/src/stm32h7/stm32_capture.h +++ b/arch/arm/src/stm32h7/stm32_capture.h @@ -204,11 +204,11 @@ struct stm32_cap_ops_s /* Power-up timer and get its structure */ -struct stm32_cap_dev_s *stm32_cap_init(int timer, uint8_t channel); +struct stm32_cap_dev_s *stm32_cap_init(int timer); /* Power-down timer, mark it as unused */ -int stm32_cap_deinit(struct stm32_cap_dev_s *dev, uint8_t channel); +int stm32_cap_deinit(struct stm32_cap_dev_s *dev); /**************************************************************************** * Name: stm32_cap_initialize @@ -228,7 +228,7 @@ int stm32_cap_deinit(struct stm32_cap_dev_s *dev, uint8_t channel); ****************************************************************************/ #ifdef CONFIG_CAPTURE -struct cap_lowerhalf_s *stm32_cap_initialize(int timer, uint8_t channel); +struct cap_lowerhalf_s *stm32_cap_initialize(int timer); #endif #undef EXTERN diff --git a/arch/arm/src/stm32h7/stm32_capture_lowerhalf.c b/arch/arm/src/stm32h7/stm32_capture_lowerhalf.c index 84bbeef1331..11b855dd4fc 100644 --- a/arch/arm/src/stm32h7/stm32_capture_lowerhalf.c +++ b/arch/arm/src/stm32h7/stm32_capture_lowerhalf.c @@ -576,7 +576,7 @@ struct cap_lowerhalf_s *stm32_cap_initialize(int timer) /* Initialize the elements of lower half state structure */ lower->started = false; - lower->cap = stm32_cap_init(timer, lower->channel); + lower->cap = stm32_cap_init(timer); if (lower->cap == NULL) { diff --git a/drivers/timers/capture.c b/drivers/timers/capture.c index 7fae3d31fcd..a03b84e7495 100644 --- a/drivers/timers/capture.c +++ b/drivers/timers/capture.c @@ -472,17 +472,34 @@ int cap_register_multiple(FAR const char *devpath, FAR struct cap_lowerhalf_s **lower, int n) { - char fullpath[16]; + char fullpath[32]; int ret; - if (n < 1) + if (!devpath || !lower || n < 1) { return -EINVAL; } + size_t devlen = strlen(devpath); + if (devlen == 0 || devlen > sizeof(fullpath) - 2) + { + return -ENAMETOOLONG; + } + for (int i = 0; i < n; i++) { - snprintf(fullpath, sizeof(fullpath), "%s%d", devpath, i); + int written = snprintf(fullpath, sizeof(fullpath), "%s%d", devpath, i); + + if (written < 0) + { + return -EIO; + } + + if ((size_t)written >= sizeof(fullpath)) + { + return -ENAMETOOLONG; + } + ret = cap_register(fullpath, lower[i]); if (ret < 0) {