You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2022/06/28 02:35:51 UTC
[incubator-nuttx] 03/03: boards/stm32f4discovery: add setup of pwm capture device
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/incubator-nuttx.git
commit 00e8e4fa28d957a257c94784235887e282220e01
Author: zouboan <ff...@feedforward.com.cn>
AuthorDate: Sun Jun 26 20:54:57 2022 +0800
boards/stm32f4discovery: add setup of pwm capture device
drivers/timers/capture.c: add support of pwm capture driver
---
boards/arm/stm32/stm32f4discovery/include/board.h | 9 ++
boards/arm/stm32/stm32f4discovery/src/Make.defs | 4 +
.../arm/stm32/stm32f4discovery/src/stm32_bringup.c | 11 +++
.../arm/stm32/stm32f4discovery/src/stm32_capture.c | 109 +++++++++++++++++++++
.../stm32/stm32f4discovery/src/stm32f4discovery.h | 21 ++++
drivers/timers/capture.c | 1 -
6 files changed, 154 insertions(+), 1 deletion(-)
diff --git a/boards/arm/stm32/stm32f4discovery/include/board.h b/boards/arm/stm32/stm32f4discovery/include/board.h
index 6732108415..6f2698a1f2 100644
--- a/boards/arm/stm32/stm32f4discovery/include/board.h
+++ b/boards/arm/stm32/stm32f4discovery/include/board.h
@@ -314,6 +314,15 @@
#define GPIO_TIM4_CH2OUT GPIO_TIM4_CH2OUT_2
+/* Capture
+ *
+ * The STM32F4 Discovery has no real on-board pwm capture devices, but the
+ * board can be configured to capture pwm using TIM3 CH2 PB5.
+ */
+
+#define GPIO_TIM3_CH2IN GPIO_TIM3_CH2IN_2
+#define GPIO_TIM3_CH1IN GPIO_TIM3_CH2IN_2
+
/* RGB LED
*
* R = TIM1 CH1 on PE9 | G = TIM2 CH2 on PA1 | B = TIM3 CH3 on PB0
diff --git a/boards/arm/stm32/stm32f4discovery/src/Make.defs b/boards/arm/stm32/stm32f4discovery/src/Make.defs
index 89fb34f154..1793630ca3 100644
--- a/boards/arm/stm32/stm32f4discovery/src/Make.defs
+++ b/boards/arm/stm32/stm32f4discovery/src/Make.defs
@@ -92,6 +92,10 @@ ifeq ($(CONFIG_PWM),y)
CSRCS += stm32_pwm.c
endif
+ifeq ($(CONFIG_CAPTURE),y)
+CSRCS += stm32_capture.c
+endif
+
ifeq ($(CONFIG_BOARDCTL),y)
CSRCS += stm32_appinit.c
ifeq ($(CONFIG_BOARDCTL_RESET),y)
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
index 17f0e51383..9a4340b421 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_bringup.c
@@ -347,6 +347,17 @@ int stm32_bringup(void)
}
#endif
+#ifdef CONFIG_CAPTURE
+ /* Initialize Capture and register the Capture driver. */
+
+ ret = stm32_capture_setup("/dev/capture0");
+ if (ret < 0)
+ {
+ syslog(LOG_ERR, "ERROR: stm32_capture_setup failed: %d\n", ret);
+ return ret;
+ }
+#endif
+
#ifdef CONFIG_STM32_CAN_CHARDRIVER
/* Initialize CAN and register the CAN driver. */
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32_capture.c b/boards/arm/stm32/stm32f4discovery/src/stm32_capture.c
new file mode 100644
index 0000000000..811bdabbe0
--- /dev/null
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32_capture.c
@@ -0,0 +1,109 @@
+/****************************************************************************
+ * boards/arm/stm32/stm32f4discovery/src/stm32_capture.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <debug.h>
+#include <nuttx/timers/capture.h>
+#include <arch/board/board.h>
+
+#include "chip.h"
+
+#include "stm32.h"
+#include "stm32_capture.h"
+#include "arm_internal.h"
+
+#include "stm32f4discovery.h"
+
+#if defined(CONFIG_CAPTURE)
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Capture
+ *
+ * The stm32f4discovery has no real on-board pwm capture devices, but the
+ * board can be configured to capture pwm using TIM3 CH2 PB5.
+ */
+
+#define HAVE_CAPTURE 1
+
+#ifndef CONFIG_CAPTURE
+# undef HAVE_CAPTURE
+#endif
+
+#ifndef CONFIG_STM32_TIM3
+# undef HAVE_CAPTURE
+#endif
+
+#ifndef CONFIG_STM32_TIM3_CAP
+# undef HAVE_CAPTURE
+#endif
+
+#if !defined(CONFIG_STM32_TIM3_CHANNEL) || CONFIG_STM32_TIM3_CHANNEL != STM32F4DISCOVERY_CAPTURECHANNEL
+# undef HAVE_CAPTURE
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_capture_setup
+ *
+ * Description:
+ * Initialize and register the pwm capture driver.
+ *
+ * Input parameters:
+ * devpath - The full path to the driver to register. E.g., "/dev/capture0"
+ *
+ * Returned Value:
+ * Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int stm32_capture_setup(FAR const char *devpath)
+{
+#ifdef HAVE_CAPTURE
+ struct cap_lowerhalf_s *capture;
+ int ret;
+
+ capture = stm32_cap_initialize(STM32F4DISCOVERY_CAPTURETIMER);
+
+ /* Then register the pwm capture sensor */
+
+ ret = cap_register(devpath, capture);
+ if (ret < 0)
+ {
+ mtrerr("ERROR: Error registering capture\n");
+ }
+
+ return ret;
+#else
+ return -ENODEV;
+#endif
+}
+
+#endif /* CONFIG_CAPTURE */
diff --git a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
index 8b8e8d308e..15953dd4a6 100644
--- a/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
+++ b/boards/arm/stm32/stm32f4discovery/src/stm32f4discovery.h
@@ -243,6 +243,15 @@
#define STM32F4DISCOVERY_PWMTIMER 4
#define STM32F4DISCOVERY_PWMCHANNEL 2
+/* Capture
+ *
+ * The STM32F4 Discovery has no real on-board pwm capture devices, but the
+ * board can be configured to capture pwm using TIM3 CH2 PB5.
+ */
+
+#define STM32F4DISCOVERY_CAPTURETIMER 3
+#define STM32F4DISCOVERY_CAPTURECHANNEL 2
+
/* SPI chip selects */
#define GPIO_CS_MEMS (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|\
@@ -555,6 +564,18 @@ int stm32_usbhost_initialize(void);
int stm32_pwm_setup(void);
#endif
+/****************************************************************************
+ * Name: stm32_capture_setup
+ *
+ * Description:
+ * Initialize pwm capture support
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_CAPTURE
+int stm32_capture_setup(FAR const char *devpath);
+#endif
+
/****************************************************************************
* Name: stm32_can_setup
*
diff --git a/drivers/timers/capture.c b/drivers/timers/capture.c
index 9537f72a06..379a5eaed9 100644
--- a/drivers/timers/capture.c
+++ b/drivers/timers/capture.c
@@ -358,7 +358,6 @@ int cap_register(FAR const char *devpath, FAR struct cap_lowerhalf_s *lower)
kmm_zalloc(sizeof(struct cap_upperhalf_s));
if (!upper)
{
- snprintf("ERROR: Allocation failed\n");
return -ENOMEM;
}