boards/stm32f4discovery: add setup of pwm capture device

drivers/timers/capture.c: add support of pwm capture driver
This commit is contained in:
zouboan 2022-06-26 20:54:57 +08:00 committed by Xiang Xiao
parent b41929522c
commit 00e8e4fa28
6 changed files with 154 additions and 1 deletions

View File

@ -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

View File

@ -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)

View File

@ -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. */

View File

@ -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 */

View File

@ -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
*

View File

@ -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;
}