Merge pull request #2995 from balanceTWK/drv_encoder
[components][drivers] change name: encoder -> pulse_encoder
This commit is contained in:
commit
ab78203e2a
@ -85,6 +85,9 @@ if GetDepend(['BSP_USING_SDIO']):
|
||||
if GetDepend(['BSP_USING_USBD_FS']):
|
||||
src += ['drv_usbd_fs.c']
|
||||
|
||||
if GetDepend(['BSP_USING_PULSE_ENCODER']):
|
||||
src += ['drv_pulse_encoder.c']
|
||||
|
||||
src += ['drv_common.c']
|
||||
|
||||
path = [cwd]
|
||||
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-23 balanceTWK first version
|
||||
*/
|
||||
|
||||
#ifndef __PULSE_ENCODER_CONFIG_H__
|
||||
#define __PULSE_ENCODER_CONFIG_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER1
|
||||
#ifndef PULSE_ENCODER1_CONFIG
|
||||
#define PULSE_ENCODER1_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM1, \
|
||||
.name = "pulse1" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER1_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER1 */
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER2
|
||||
#ifndef PULSE_ENCODER2_CONFIG
|
||||
#define PULSE_ENCODER2_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM2, \
|
||||
.name = "pulse2" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER2_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER2 */
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER3
|
||||
#ifndef PULSE_ENCODER3_CONFIG
|
||||
#define PULSE_ENCODER3_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM3, \
|
||||
.name = "pulse3" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER3_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER3 */
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER4
|
||||
#ifndef PULSE_ENCODER4_CONFIG
|
||||
#define PULSE_ENCODER4_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM4, \
|
||||
.name = "pulse4" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER4_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER4 */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PULSE_ENCODER_CONFIG_H__ */
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-23 balanceTWK first version
|
||||
*/
|
||||
|
||||
#ifndef __PULSE_ENCODER_CONFIG_H__
|
||||
#define __PULSE_ENCODER_CONFIG_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER1
|
||||
#ifndef PULSE_ENCODER1_CONFIG
|
||||
#define PULSE_ENCODER1_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM1, \
|
||||
.name = "pulse1" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER1_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER1 */
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER2
|
||||
#ifndef PULSE_ENCODER2_CONFIG
|
||||
#define PULSE_ENCODER2_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM2, \
|
||||
.name = "pulse2" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER2_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER2 */
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER3
|
||||
#ifndef PULSE_ENCODER3_CONFIG
|
||||
#define PULSE_ENCODER3_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM3, \
|
||||
.name = "pulse3" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER3_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER3 */
|
||||
|
||||
#ifdef BSP_USING_PULSE_ENCODER4
|
||||
#ifndef PULSE_ENCODER4_CONFIG
|
||||
#define PULSE_ENCODER4_CONFIG \
|
||||
{ \
|
||||
.tim_handler.Instance = TIM4, \
|
||||
.name = "pulse4" \
|
||||
}
|
||||
#endif /* PULSE_ENCODER4_CONFIG */
|
||||
#endif /* BSP_USING_PULSE_ENCODER4 */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PULSE_ENCODER_CONFIG_H__ */
|
@ -34,6 +34,7 @@ extern "C" {
|
||||
#include "f1/sdio_config.h"
|
||||
#include "f1/pwm_config.h"
|
||||
#include "f1/usbd_fs_config.h"
|
||||
#include "f1/pulse_encoder_config.h"
|
||||
#elif defined(SOC_SERIES_STM32F2)
|
||||
#include "f2/dma_config.h"
|
||||
#include "f2/uart_config.h"
|
||||
@ -52,6 +53,7 @@ extern "C" {
|
||||
#include "f4/tim_config.h"
|
||||
#include "f4/sdio_config.h"
|
||||
#include "f4/pwm_config.h"
|
||||
#include "f4/pulse_encoder_config.h"
|
||||
#elif defined(SOC_SERIES_STM32F7)
|
||||
#include "f7/dma_config.h"
|
||||
#include "f7/uart_config.h"
|
||||
|
176
bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c
Normal file
176
bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c
Normal file
@ -0,0 +1,176 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-23 balanceTWK first version
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
#include "drv_config.h"
|
||||
#ifdef RT_USING_PULSE_ENCODER
|
||||
|
||||
//#define DRV_DEBUG
|
||||
#define LOG_TAG "drv.pulse_encoder"
|
||||
#include <drv_log.h>
|
||||
|
||||
#if !defined(BSP_USING_PULSE_ENCODER1) && !defined(BSP_USING_PULSE_ENCODER2) && !defined(BSP_USING_PULSE_ENCODER3) \
|
||||
&& !defined(BSP_USING_PULSE_ENCODER4) && !defined(BSP_USING_PULSE_ENCODER5) && !defined(BSP_USING_PULSE_ENCODER6)
|
||||
#error "Please define at least one BSP_USING_PULSE_ENCODERx"
|
||||
/* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
|
||||
#endif
|
||||
|
||||
enum
|
||||
{
|
||||
#ifdef BSP_USING_PULSE_ENCODER1
|
||||
PULSE_ENCODER1_INDEX,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER2
|
||||
PULSE_ENCODER2_INDEX,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER3
|
||||
PULSE_ENCODER3_INDEX,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER4
|
||||
PULSE_ENCODER4_INDEX,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER5
|
||||
PULSE_ENCODER5_INDEX,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER6
|
||||
PULSE_ENCODER6_INDEX,
|
||||
#endif
|
||||
};
|
||||
|
||||
struct stm32_pulse_encoder_device
|
||||
{
|
||||
struct rt_pulse_encoder_device pulse_encoder;
|
||||
TIM_HandleTypeDef tim_handler;
|
||||
char *name;
|
||||
};
|
||||
|
||||
static struct stm32_pulse_encoder_device stm32_pulse_encoder_obj[] =
|
||||
{
|
||||
#ifdef BSP_USING_PULSE_ENCODER1
|
||||
PULSE_ENCODER1_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER2
|
||||
PULSE_ENCODER2_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER3
|
||||
PULSE_ENCODER3_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER4
|
||||
PULSE_ENCODER4_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER5
|
||||
PULSE_ENCODER5_CONFIG,
|
||||
#endif
|
||||
#ifdef BSP_USING_PULSE_ENCODER6
|
||||
PULSE_ENCODER6_CONFIG,
|
||||
#endif
|
||||
};
|
||||
|
||||
rt_err_t pulse_encoder_init(struct rt_pulse_encoder_device *pulse_encoder)
|
||||
{
|
||||
TIM_Encoder_InitTypeDef sConfig;
|
||||
TIM_MasterConfigTypeDef sMasterConfig;
|
||||
TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data;
|
||||
|
||||
tim_handler->Init.Prescaler = 0;
|
||||
tim_handler->Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
tim_handler->Init.Period = 0xffff;
|
||||
tim_handler->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
|
||||
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC1Filter = 3;
|
||||
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC2Filter = 3;
|
||||
|
||||
if (HAL_TIM_Encoder_Init(tim_handler, &sConfig) != HAL_OK)
|
||||
{
|
||||
LOG_E("pulse_encoder init failed");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(tim_handler, &sMasterConfig))
|
||||
{
|
||||
LOG_E("TIMx master config failed");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_int32_t pulse_encoder_get_count(struct rt_pulse_encoder_device *pulse_encoder)
|
||||
{
|
||||
TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data;
|
||||
return (rt_int16_t)__HAL_TIM_GET_COUNTER(tim_handler);
|
||||
}
|
||||
|
||||
rt_err_t pulse_encoder_control(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args)
|
||||
{
|
||||
rt_err_t result;
|
||||
TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)pulse_encoder->parent.user_data;
|
||||
|
||||
result = RT_EOK;
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case PULSE_ENCODER_CMD_ENABLE:
|
||||
HAL_TIM_Encoder_Start(tim_handler, TIM_CHANNEL_ALL);
|
||||
break;
|
||||
case PULSE_ENCODER_CMD_DISABLE:
|
||||
HAL_TIM_Encoder_Stop(tim_handler, TIM_CHANNEL_ALL);
|
||||
break;
|
||||
case PULSE_ENCODER_CMD_CLEAR_COUNT:
|
||||
__HAL_TIM_SET_COUNTER(tim_handler, 0);
|
||||
break;
|
||||
default:
|
||||
result = -RT_ENOSYS;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static const struct rt_pulse_encoder_ops _ops =
|
||||
{
|
||||
.init = pulse_encoder_init,
|
||||
.get_count = pulse_encoder_get_count,
|
||||
.control = pulse_encoder_control,
|
||||
};
|
||||
|
||||
int hw_pulse_encoder_init(void)
|
||||
{
|
||||
int i;
|
||||
int result;
|
||||
|
||||
result = RT_EOK;
|
||||
for (i = 0; i < sizeof(stm32_pulse_encoder_obj) / sizeof(stm32_pulse_encoder_obj[0]); i++)
|
||||
{
|
||||
stm32_pulse_encoder_obj[i].pulse_encoder.type = AB_PHASE_PULSE_ENCODER;
|
||||
stm32_pulse_encoder_obj[i].pulse_encoder.ops = &_ops;
|
||||
|
||||
if (rt_device_pulse_encoder_register(&stm32_pulse_encoder_obj[i].pulse_encoder, stm32_pulse_encoder_obj[i].name, &stm32_pulse_encoder_obj[i].tim_handler) != RT_EOK)
|
||||
{
|
||||
LOG_E("%s register failed", stm32_pulse_encoder_obj[i].name);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
INIT_BOARD_EXPORT(hw_pulse_encoder_init);
|
||||
|
||||
#endif
|
@ -44,7 +44,7 @@ if GetDepend(['RT_USING_USB_HOST']) or GetDepend(['RT_USING_USB_DEVICE']):
|
||||
if GetDepend(['RT_USING_CAN']):
|
||||
src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_can.c']
|
||||
|
||||
if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']):
|
||||
if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_PULSE_ENCODER']):
|
||||
src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c']
|
||||
src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c']
|
||||
|
||||
|
@ -46,7 +46,7 @@ if GetDepend(['RT_USING_USB_HOST']) or GetDepend(['RT_USING_USB_DEVICE']):
|
||||
if GetDepend(['RT_USING_CAN']):
|
||||
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c']
|
||||
|
||||
if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']):
|
||||
if GetDepend(['RT_USING_HWTIMER']) or GetDepend(['RT_USING_PWM']) or GetDepend(['RT_USING_PULSE_ENCODER']):
|
||||
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c']
|
||||
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c']
|
||||
src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_lptim.c']
|
||||
|
@ -1,7 +1,7 @@
|
||||
[PreviousGenFiles]
|
||||
HeaderPath=G:/Wondfo/RT-Thread/Official information/rt-thread-master/bsp/1stm32/stm32f103-atk-warshipV3/board/CubeMX_Config/Inc
|
||||
HeaderPath=E:/workspace/rt-thread/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Inc
|
||||
HeaderFiles=stm32f1xx_it.h;stm32f1xx_hal_conf.h;main.h;
|
||||
SourcePath=G:/Wondfo/RT-Thread/Official information/rt-thread-master/bsp/1stm32/stm32f103-atk-warshipV3/board/CubeMX_Config/Src
|
||||
SourcePath=E:/workspace/rt-thread/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src
|
||||
SourceFiles=stm32f1xx_it.c;stm32f1xx_hal_msp.c;main.c;
|
||||
|
||||
[PreviousLibFiles]
|
||||
@ -10,4 +10,5 @@ LibFiles=Drivers/STM32F1xx_HAL_Driver/Inc/stm32f1xx_hal_tim.h;Drivers/STM32F1xx_
|
||||
[PreviousUsedKeilFiles]
|
||||
SourceFiles=..\Src\main.c;..\Src\stm32f1xx_it.c;..\Src\stm32f1xx_hal_msp.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;../\Src/system_stm32f1xx.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_cortex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c;../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c;../\Src/system_stm32f1xx.c;../Drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates/system_stm32f1xx.c;null;
|
||||
HeaderPath=..\Drivers\STM32F1xx_HAL_Driver\Inc;..\Drivers\STM32F1xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32F1xx\Include;..\Drivers\CMSIS\Include;..\Inc;
|
||||
CDefines=USE_HAL_DRIVER;STM32F103xE;USE_HAL_DRIVER;STM32F103xE;
|
||||
|
||||
|
@ -5,19 +5,22 @@ Mcu.Family=STM32F1
|
||||
Mcu.IP0=NVIC
|
||||
Mcu.IP1=RCC
|
||||
Mcu.IP2=SYS
|
||||
Mcu.IP3=USART1
|
||||
Mcu.IPNb=4
|
||||
Mcu.IP3=TIM3
|
||||
Mcu.IP4=USART1
|
||||
Mcu.IPNb=5
|
||||
Mcu.Name=STM32F103Z(C-D-E)Tx
|
||||
Mcu.Package=LQFP144
|
||||
Mcu.Pin0=PC14-OSC32_IN
|
||||
Mcu.Pin1=PC15-OSC32_OUT
|
||||
Mcu.Pin2=OSC_IN
|
||||
Mcu.Pin3=OSC_OUT
|
||||
Mcu.Pin4=PA9
|
||||
Mcu.Pin5=PA10
|
||||
Mcu.Pin6=VP_SYS_VS_ND
|
||||
Mcu.Pin7=VP_SYS_VS_Systick
|
||||
Mcu.PinsNb=8
|
||||
Mcu.Pin4=PC6
|
||||
Mcu.Pin5=PC7
|
||||
Mcu.Pin6=PA9
|
||||
Mcu.Pin7=PA10
|
||||
Mcu.Pin8=VP_SYS_VS_ND
|
||||
Mcu.Pin9=VP_SYS_VS_Systick
|
||||
Mcu.PinsNb=10
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32F103ZETx
|
||||
@ -45,6 +48,10 @@ PC14-OSC32_IN.Mode=LSE-External-Oscillator
|
||||
PC14-OSC32_IN.Signal=RCC_OSC32_IN
|
||||
PC15-OSC32_OUT.Mode=LSE-External-Oscillator
|
||||
PC15-OSC32_OUT.Signal=RCC_OSC32_OUT
|
||||
PC6.Locked=true
|
||||
PC6.Signal=S_TIM3_CH1
|
||||
PC7.Locked=true
|
||||
PC7.Signal=S_TIM3_CH2
|
||||
PCC.Checker=false
|
||||
PCC.Line=STM32F103
|
||||
PCC.MCU=STM32F103Z(C-D-E)Tx
|
||||
@ -54,7 +61,7 @@ PCC.Series=STM32F1
|
||||
PCC.Temperature=25
|
||||
PCC.Vdd=3.3
|
||||
PinOutPanel.RotationAngle=0
|
||||
ProjectManager.AskForMigrate=true
|
||||
ProjectManager.AskForMigrate=false
|
||||
ProjectManager.BackupPrevious=false
|
||||
ProjectManager.CompilerOptimize=6
|
||||
ProjectManager.ComputerToolchain=false
|
||||
@ -80,7 +87,7 @@ ProjectManager.StackSize=0x400
|
||||
ProjectManager.TargetToolchain=MDK-ARM V5
|
||||
ProjectManager.ToolChainLocation=
|
||||
ProjectManager.UnderRoot=false
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_TIM8_Init-TIM8-false-HAL-true
|
||||
RCC.ADCFreqValue=36000000
|
||||
RCC.AHBFreq_Value=72000000
|
||||
RCC.APB1CLKDivider=RCC_HCLK_DIV2
|
||||
@ -107,6 +114,10 @@ RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK
|
||||
RCC.TimSysFreq_Value=72000000
|
||||
RCC.USBFreq_Value=72000000
|
||||
RCC.VCOOutput2Freq_Value=8000000
|
||||
SH.S_TIM3_CH1.0=TIM3_CH1,Encoder_Interface
|
||||
SH.S_TIM3_CH1.ConfNb=1
|
||||
SH.S_TIM3_CH2.0=TIM3_CH2,Encoder_Interface
|
||||
SH.S_TIM3_CH2.ConfNb=1
|
||||
USART1.IPParameters=VirtualMode
|
||||
USART1.VirtualMode=VM_ASYNC
|
||||
VP_SYS_VS_ND.Mode=No_Debug
|
||||
|
@ -78,7 +78,7 @@
|
||||
/*#define HAL_SMARTCARD_MODULE_ENABLED */
|
||||
/*#define HAL_SPI_MODULE_ENABLED */
|
||||
/*#define HAL_SRAM_MODULE_ENABLED */
|
||||
/*#define HAL_TIM_MODULE_ENABLED */
|
||||
#define HAL_TIM_MODULE_ENABLED
|
||||
#define HAL_UART_MODULE_ENABLED
|
||||
/*#define HAL_USART_MODULE_ENABLED */
|
||||
/*#define HAL_WWDG_MODULE_ENABLED */
|
||||
|
@ -42,6 +42,8 @@
|
||||
/* USER CODE END PM */
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
TIM_HandleTypeDef htim3;
|
||||
|
||||
UART_HandleTypeDef huart1;
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
@ -52,6 +54,7 @@ UART_HandleTypeDef huart1;
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
static void MX_USART1_UART_Init(void);
|
||||
static void MX_TIM3_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
@ -90,6 +93,7 @@ int main(void)
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_USART1_UART_Init();
|
||||
MX_TIM3_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@ -142,6 +146,55 @@ void SystemClock_Config(void)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM3 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM3_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM3_Init 0 */
|
||||
|
||||
/* USER CODE END TIM3_Init 0 */
|
||||
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM3_Init 1 */
|
||||
|
||||
/* USER CODE END TIM3_Init 1 */
|
||||
htim3.Instance = TIM3;
|
||||
htim3.Init.Prescaler = 0;
|
||||
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim3.Init.Period = 0;
|
||||
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC1Filter = 0;
|
||||
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC2Filter = 0;
|
||||
if (HAL_TIM_Encoder_Init(&htim3, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM3_Init 2 */
|
||||
|
||||
/* USER CODE END TIM3_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USART1 Initialization Function
|
||||
* @param None
|
||||
|
@ -81,6 +81,71 @@ void HAL_MspInit(void)
|
||||
/* USER CODE END MspInit 1 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM_Encoder MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param htim_encoder: TIM_Encoder handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* htim_encoder)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(htim_encoder->Instance==TIM3)
|
||||
{
|
||||
/* USER CODE BEGIN TIM3_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM3_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_TIM3_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
/**TIM3 GPIO Configuration
|
||||
PC6 ------> TIM3_CH1
|
||||
PC7 ------> TIM3_CH2
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
__HAL_AFIO_REMAP_TIM3_ENABLE();
|
||||
|
||||
/* USER CODE BEGIN TIM3_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM3_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM_Encoder MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param htim_encoder: TIM_Encoder handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* htim_encoder)
|
||||
{
|
||||
if(htim_encoder->Instance==TIM3)
|
||||
{
|
||||
/* USER CODE BEGIN TIM3_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM3_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM3_CLK_DISABLE();
|
||||
|
||||
/**TIM3 GPIO Configuration
|
||||
PC6 ------> TIM3_CH1
|
||||
PC7 ------> TIM3_CH2
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6|GPIO_PIN_7);
|
||||
|
||||
/* USER CODE BEGIN TIM3_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM3_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief UART MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
|
@ -201,6 +201,17 @@ menu "On-chip Peripheral Drivers"
|
||||
select RT_USING_WDT
|
||||
default n
|
||||
|
||||
menuconfig BSP_USING_PULSE_ENCODER
|
||||
bool "Enable Pulse Encoder"
|
||||
default n
|
||||
select RT_USING_PULSE_ENCODER
|
||||
if BSP_USING_PULSE_ENCODER
|
||||
config BSP_USING_PULSE_ENCODER3
|
||||
bool "Enable Pulse Encoder3"
|
||||
default n
|
||||
|
||||
endif
|
||||
|
||||
source "../libraries/HAL_Drivers/Kconfig"
|
||||
|
||||
endmenu
|
||||
|
@ -1,7 +1,7 @@
|
||||
[PreviousGenFiles]
|
||||
HeaderPath=E:/workspace/BspFramework/rt-thread/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Inc
|
||||
HeaderPath=E:/workspace/rt-thread/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Inc
|
||||
HeaderFiles=stm32f4xx_it.h;stm32f4xx_hal_conf.h;main.h;
|
||||
SourcePath=E:/workspace/BspFramework/rt-thread/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src
|
||||
SourcePath=E:/workspace/rt-thread/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src
|
||||
SourceFiles=stm32f4xx_it.c;stm32f4xx_hal_msp.c;main.c;
|
||||
|
||||
[PreviousLibFiles]
|
||||
@ -10,4 +10,5 @@ LibFiles=Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_adc.h;Drivers/STM32F4xx_
|
||||
[PreviousUsedKeilFiles]
|
||||
SourceFiles=..\Src\main.c;..\Src\stm32f4xx_it.c;..\Src\stm32f4xx_hal_msp.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;../\Src/system_stm32f4xx.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_eth.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_iwdg.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rtc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_sdmmc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_sd.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_spi.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;../Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;../\Src/system_stm32f4xx.c;../Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;null;
|
||||
HeaderPath=..\Drivers\STM32F4xx_HAL_Driver\Inc;..\Drivers\STM32F4xx_HAL_Driver\Inc\Legacy;..\Drivers\CMSIS\Device\ST\STM32F4xx\Include;..\Drivers\CMSIS\Include;..\Inc;
|
||||
CDefines=USE_HAL_DRIVER;STM32F407xx;USE_HAL_DRIVER;STM32F407xx;
|
||||
|
||||
|
@ -13,11 +13,12 @@ Mcu.Family=STM32F4
|
||||
Mcu.IP0=ADC1
|
||||
Mcu.IP1=ETH
|
||||
Mcu.IP10=TIM2
|
||||
Mcu.IP11=TIM11
|
||||
Mcu.IP12=TIM13
|
||||
Mcu.IP13=TIM14
|
||||
Mcu.IP14=USART1
|
||||
Mcu.IP15=USART3
|
||||
Mcu.IP11=TIM4
|
||||
Mcu.IP12=TIM11
|
||||
Mcu.IP13=TIM13
|
||||
Mcu.IP14=TIM14
|
||||
Mcu.IP15=USART1
|
||||
Mcu.IP16=USART3
|
||||
Mcu.IP2=IWDG
|
||||
Mcu.IP3=NVIC
|
||||
Mcu.IP4=RCC
|
||||
@ -26,7 +27,7 @@ Mcu.IP6=SDIO
|
||||
Mcu.IP7=SPI1
|
||||
Mcu.IP8=SPI2
|
||||
Mcu.IP9=SYS
|
||||
Mcu.IPNb=16
|
||||
Mcu.IPNb=17
|
||||
Mcu.Name=STM32F407Z(E-G)Tx
|
||||
Mcu.Package=LQFP144
|
||||
Mcu.Pin0=PC14-OSC32_IN
|
||||
@ -56,37 +57,39 @@ Mcu.Pin3=PH1-OSC_OUT
|
||||
Mcu.Pin30=PB3
|
||||
Mcu.Pin31=PB4
|
||||
Mcu.Pin32=PB5
|
||||
Mcu.Pin33=VP_IWDG_VS_IWDG
|
||||
Mcu.Pin34=VP_RTC_VS_RTC_Activate
|
||||
Mcu.Pin35=VP_SYS_VS_Systick
|
||||
Mcu.Pin36=VP_TIM2_VS_ClockSourceINT
|
||||
Mcu.Pin37=VP_TIM11_VS_ClockSourceINT
|
||||
Mcu.Pin38=VP_TIM13_VS_ClockSourceINT
|
||||
Mcu.Pin39=VP_TIM14_VS_ClockSourceINT
|
||||
Mcu.Pin33=PB6
|
||||
Mcu.Pin34=PB7
|
||||
Mcu.Pin35=VP_IWDG_VS_IWDG
|
||||
Mcu.Pin36=VP_RTC_VS_RTC_Activate
|
||||
Mcu.Pin37=VP_SYS_VS_Systick
|
||||
Mcu.Pin38=VP_TIM2_VS_ClockSourceINT
|
||||
Mcu.Pin39=VP_TIM11_VS_ClockSourceINT
|
||||
Mcu.Pin4=PC1
|
||||
Mcu.Pin40=VP_TIM13_VS_ClockSourceINT
|
||||
Mcu.Pin41=VP_TIM14_VS_ClockSourceINT
|
||||
Mcu.Pin5=PC2
|
||||
Mcu.Pin6=PC3
|
||||
Mcu.Pin7=PA1
|
||||
Mcu.Pin8=PA2
|
||||
Mcu.Pin9=PA3
|
||||
Mcu.PinsNb=40
|
||||
Mcu.PinsNb=42
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32F407ZGTx
|
||||
MxCube.Version=5.0.0
|
||||
MxDb.Version=DB.5.0.0
|
||||
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
|
||||
NVIC.SPI1_IRQn=true\:0\:0\:false\:false\:true\:true
|
||||
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true
|
||||
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false
|
||||
NVIC.SPI1_IRQn=true\:0\:0\:false\:false\:true\:true\:true
|
||||
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false\:true
|
||||
NVIC.USART1_IRQn=true\:0\:0\:false\:false\:true\:true\:true
|
||||
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
|
||||
PA1.Mode=RMII
|
||||
PA1.Signal=ETH_REF_CLK
|
||||
PA10.Mode=Asynchronous
|
||||
@ -119,6 +122,10 @@ PB4.Signal=SPI1_MISO
|
||||
PB5.Locked=true
|
||||
PB5.Mode=Full_Duplex_Master
|
||||
PB5.Signal=SPI1_MOSI
|
||||
PB6.Locked=true
|
||||
PB6.Signal=S_TIM4_CH1
|
||||
PB7.Locked=true
|
||||
PB7.Signal=S_TIM4_CH2
|
||||
PC1.Mode=RMII
|
||||
PC1.Signal=ETH_MDC
|
||||
PC10.Mode=SD_4_bits_Wide_bus
|
||||
@ -167,7 +174,7 @@ PH0-OSC_IN.Signal=RCC_OSC_IN
|
||||
PH1-OSC_OUT.Mode=HSE-External-Oscillator
|
||||
PH1-OSC_OUT.Signal=RCC_OSC_OUT
|
||||
PinOutPanel.RotationAngle=0
|
||||
ProjectManager.AskForMigrate=true
|
||||
ProjectManager.AskForMigrate=false
|
||||
ProjectManager.BackupPrevious=false
|
||||
ProjectManager.CompilerOptimize=6
|
||||
ProjectManager.ComputerToolchain=false
|
||||
@ -181,7 +188,7 @@ ProjectManager.FreePins=false
|
||||
ProjectManager.HalAssertFull=false
|
||||
ProjectManager.HeapSize=0x200
|
||||
ProjectManager.KeepUserCode=true
|
||||
ProjectManager.LastFirmware=false
|
||||
ProjectManager.LastFirmware=true
|
||||
ProjectManager.LibraryCopy=0
|
||||
ProjectManager.MainLocation=Src
|
||||
ProjectManager.NoMain=false
|
||||
@ -193,7 +200,7 @@ ProjectManager.StackSize=0x400
|
||||
ProjectManager.TargetToolchain=MDK-ARM V5
|
||||
ProjectManager.ToolChainLocation=
|
||||
ProjectManager.UnderRoot=false
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_SPI1_Init-SPI1-false-HAL-true,5-MX_ETH_Init-ETH-false-HAL-true,6-MX_USART3_UART_Init-USART3-false-HAL-true,7-MX_ADC1_Init-ADC1-false-HAL-true,8-MX_RTC_Init-RTC-false-HAL-true,9-MX_IWDG_Init-IWDG-false-HAL-true,10-MX_TIM14_Init-TIM14-false-HAL-true,11-MX_TIM13_Init-TIM13-false-HAL-true,12-MX_TIM11_Init-TIM11-false-HAL-true,13-MX_SDIO_SD_Init-SDIO-false-HAL-true,14-MX_TIM2_Init-TIM2-false-HAL-true,15-MX_SPI2_Init-SPI2-false-HAL-true
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART1_UART_Init-USART1-false-HAL-true,4-MX_SPI1_Init-SPI1-false-HAL-true,5-MX_ETH_Init-ETH-false-HAL-true,6-MX_USART3_UART_Init-USART3-false-HAL-true,7-MX_ADC1_Init-ADC1-false-HAL-true,8-MX_RTC_Init-RTC-false-HAL-true,9-MX_IWDG_Init-IWDG-false-HAL-true,10-MX_TIM14_Init-TIM14-false-HAL-true,11-MX_TIM13_Init-TIM13-false-HAL-true,12-MX_TIM11_Init-TIM11-false-HAL-true,13-MX_SDIO_SD_Init-SDIO-false-HAL-true,14-MX_TIM2_Init-TIM2-false-HAL-true,15-MX_SPI2_Init-SPI2-false-HAL-true,16-MX_TIM4_Init-TIM4-false-HAL-true
|
||||
RCC.48MHZClocksFreq_Value=48000000
|
||||
RCC.AHBFreq_Value=168000000
|
||||
RCC.APB1CLKDivider=RCC_HCLK_DIV4
|
||||
@ -233,6 +240,10 @@ SH.ADCx_IN5.0=ADC1_IN5,IN5
|
||||
SH.ADCx_IN5.ConfNb=1
|
||||
SH.S_TIM2_CH4.0=TIM2_CH4,PWM Generation4 CH4
|
||||
SH.S_TIM2_CH4.ConfNb=1
|
||||
SH.S_TIM4_CH1.0=TIM4_CH1,Encoder_Interface
|
||||
SH.S_TIM4_CH1.ConfNb=1
|
||||
SH.S_TIM4_CH2.0=TIM4_CH2,Encoder_Interface
|
||||
SH.S_TIM4_CH2.ConfNb=1
|
||||
SPI1.CalculateBaudRate=42.0 MBits/s
|
||||
SPI1.Direction=SPI_DIRECTION_2LINES
|
||||
SPI1.IPParameters=VirtualType,Mode,Direction,CalculateBaudRate
|
||||
|
@ -5,7 +5,7 @@
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
|
||||
* <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
|
@ -76,6 +76,7 @@ SPI_HandleTypeDef hspi1;
|
||||
SPI_HandleTypeDef hspi2;
|
||||
|
||||
TIM_HandleTypeDef htim2;
|
||||
TIM_HandleTypeDef htim4;
|
||||
TIM_HandleTypeDef htim11;
|
||||
TIM_HandleTypeDef htim13;
|
||||
TIM_HandleTypeDef htim14;
|
||||
@ -104,6 +105,7 @@ static void MX_TIM11_Init(void);
|
||||
static void MX_SDIO_SD_Init(void);
|
||||
static void MX_TIM2_Init(void);
|
||||
static void MX_SPI2_Init(void);
|
||||
static void MX_TIM4_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
|
||||
@ -155,6 +157,7 @@ int main(void)
|
||||
MX_SDIO_SD_Init();
|
||||
MX_TIM2_Init();
|
||||
MX_SPI2_Init();
|
||||
MX_TIM4_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@ -551,6 +554,54 @@ static void MX_TIM2_Init(void)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM4 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM4_Init(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN TIM4_Init 0 */
|
||||
|
||||
/* USER CODE END TIM4_Init 0 */
|
||||
|
||||
TIM_Encoder_InitTypeDef sConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM4_Init 1 */
|
||||
|
||||
/* USER CODE END TIM4_Init 1 */
|
||||
htim4.Instance = TIM4;
|
||||
htim4.Init.Prescaler = 0;
|
||||
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim4.Init.Period = 0;
|
||||
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
sConfig.EncoderMode = TIM_ENCODERMODE_TI1;
|
||||
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC1Filter = 0;
|
||||
sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
|
||||
sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
|
||||
sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
|
||||
sConfig.IC2Filter = 0;
|
||||
if (HAL_TIM_Encoder_Init(&htim4, &sConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM4_Init 2 */
|
||||
|
||||
/* USER CODE END TIM4_Init 2 */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM11 Initialization Function
|
||||
* @param None
|
||||
|
@ -585,6 +585,43 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM_Encoder MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param htim_encoder: TIM_Encoder handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
void HAL_TIM_Encoder_MspInit(TIM_HandleTypeDef* htim_encoder)
|
||||
{
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
if(htim_encoder->Instance==TIM4)
|
||||
{
|
||||
/* USER CODE BEGIN TIM4_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM4_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_TIM4_CLK_ENABLE();
|
||||
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**TIM4 GPIO Configuration
|
||||
PB6 ------> TIM4_CH1
|
||||
PB7 ------> TIM4_CH2
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* USER CODE BEGIN TIM4_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM4_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
|
||||
{
|
||||
|
||||
@ -669,6 +706,37 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM_Encoder MSP De-Initialization
|
||||
* This function freeze the hardware resources used in this example
|
||||
* @param htim_encoder: TIM_Encoder handle pointer
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
void HAL_TIM_Encoder_MspDeInit(TIM_HandleTypeDef* htim_encoder)
|
||||
{
|
||||
|
||||
if(htim_encoder->Instance==TIM4)
|
||||
{
|
||||
/* USER CODE BEGIN TIM4_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM4_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM4_CLK_DISABLE();
|
||||
|
||||
/**TIM4 GPIO Configuration
|
||||
PB6 ------> TIM4_CH1
|
||||
PB7 ------> TIM4_CH2
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);
|
||||
|
||||
/* USER CODE BEGIN TIM4_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM4_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief UART MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
|
@ -293,6 +293,18 @@ menu "On-chip Peripheral Drivers"
|
||||
select RT_USING_SDIO
|
||||
select RT_USING_DFS
|
||||
default n
|
||||
|
||||
menuconfig BSP_USING_PULSE_ENCODER
|
||||
bool "Enable Pulse Encoder"
|
||||
default n
|
||||
select RT_USING_PULSE_ENCODER
|
||||
if BSP_USING_PULSE_ENCODER
|
||||
config BSP_USING_PULSE_ENCODER4
|
||||
bool "Enable Pulse Encoder4"
|
||||
default n
|
||||
|
||||
endif
|
||||
|
||||
source "../libraries/HAL_Drivers/Kconfig"
|
||||
|
||||
endmenu
|
||||
|
@ -434,8 +434,8 @@ menuconfig RT_USING_HWCRYPTO
|
||||
endif
|
||||
endif
|
||||
|
||||
config RT_USING_ENCODER
|
||||
bool "Using ENCODER device drivers"
|
||||
config RT_USING_PULSE_ENCODER
|
||||
bool "Using PULSE ENCODER device drivers"
|
||||
default n
|
||||
|
||||
config RT_USING_INPUT_CAPTURE
|
||||
|
@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-08 balanceTWK the first version
|
||||
*/
|
||||
|
||||
#ifndef __ENCODER_H__
|
||||
#define __ENCODER_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* encoder control command */
|
||||
#define ENCODER_CMD_GET_TYPE (128 + 0) /* get a encoder type information */
|
||||
#define ENCODER_CMD_ENABLE (128 + 1) /* enable encoder */
|
||||
#define ENCODER_CMD_DISABLE (128 + 2) /* disable encoder */
|
||||
#define ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear encoder count */
|
||||
|
||||
/* encoder type */
|
||||
enum rt_encoder_type
|
||||
{
|
||||
UNKNOWN_ENCODER_TYPE = 0x00, /* Unknown encoder type */
|
||||
SINGLE_PHASE_ENCODER, /* single phase encoder */
|
||||
AB_PHASE_ENCODER /* two phase encoder */
|
||||
};
|
||||
|
||||
struct rt_encoder_device;
|
||||
|
||||
struct rt_encoder_ops
|
||||
{
|
||||
rt_err_t (*init)(struct rt_encoder_device *encoder);
|
||||
rt_int32_t (*get_count)(struct rt_encoder_device *encoder);
|
||||
rt_err_t (*clear_count)(struct rt_encoder_device *encoder);
|
||||
rt_err_t (*control)(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args);
|
||||
};
|
||||
|
||||
struct rt_encoder_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct rt_encoder_ops *ops;
|
||||
enum rt_encoder_type type;
|
||||
};
|
||||
|
||||
rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __ENCODER_H__ */
|
58
components/drivers/include/drivers/pulse_encoder.h
Normal file
58
components/drivers/include/drivers/pulse_encoder.h
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-08 balanceTWK the first version
|
||||
*/
|
||||
|
||||
#ifndef __PULSE_ENCODER_H__
|
||||
#define __PULSE_ENCODER_H__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* pulse_encoder control command */
|
||||
#define PULSE_ENCODER_CMD_GET_TYPE (128 + 0) /* get a pulse_encoder type information */
|
||||
#define PULSE_ENCODER_CMD_ENABLE (128 + 1) /* enable pulse_encoder */
|
||||
#define PULSE_ENCODER_CMD_DISABLE (128 + 2) /* disable pulse_encoder */
|
||||
#define PULSE_ENCODER_CMD_CLEAR_COUNT (128 + 3) /* clear pulse_encoder count */
|
||||
|
||||
/* pulse_encoder type */
|
||||
enum rt_pulse_encoder_type
|
||||
{
|
||||
UNKNOWN_PULSE_ENCODER_TYPE = 0x00, /* Unknown pulse_encoder type */
|
||||
SINGLE_PHASE_PULSE_ENCODER, /* single phase pulse_encoder */
|
||||
AB_PHASE_PULSE_ENCODER /* two phase pulse_encoder */
|
||||
};
|
||||
|
||||
struct rt_pulse_encoder_device;
|
||||
|
||||
struct rt_pulse_encoder_ops
|
||||
{
|
||||
rt_err_t (*init)(struct rt_pulse_encoder_device *pulse_encoder);
|
||||
rt_int32_t (*get_count)(struct rt_pulse_encoder_device *pulse_encoder);
|
||||
rt_err_t (*clear_count)(struct rt_pulse_encoder_device *pulse_encoder);
|
||||
rt_err_t (*control)(struct rt_pulse_encoder_device *pulse_encoder, rt_uint32_t cmd, void *args);
|
||||
};
|
||||
|
||||
struct rt_pulse_encoder_device
|
||||
{
|
||||
struct rt_device parent;
|
||||
const struct rt_pulse_encoder_ops *ops;
|
||||
enum rt_pulse_encoder_type type;
|
||||
};
|
||||
|
||||
rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __PULSE_ENCODER_H__ */
|
@ -126,8 +126,8 @@ extern "C" {
|
||||
#include "drivers/crypto.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_ENCODER
|
||||
#include "drivers/encoder.h"
|
||||
#ifdef RT_USING_PULSE_ENCODER
|
||||
#include "drivers/pulse_encoder.h"
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_INPUT_CAPTURE
|
||||
|
@ -14,8 +14,8 @@ if GetDepend(['RT_USING_ADC']):
|
||||
if GetDepend(['RT_USING_PWM']):
|
||||
src = src + ['rt_drv_pwm.c']
|
||||
|
||||
if GetDepend(['RT_USING_ENCODER']):
|
||||
src = src + ['encoder.c']
|
||||
if GetDepend(['RT_USING_PULSE_ENCODER']):
|
||||
src = src + ['pulse_encoder.c']
|
||||
|
||||
if GetDepend(['RT_USING_INPUT_CAPTURE']):
|
||||
src = src + ['rt_inputcapture.c']
|
||||
|
@ -1,137 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-08 balanceTWK the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
static rt_err_t rt_encoder_init(struct rt_device *dev)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_encoder_device *encoder;
|
||||
|
||||
ret = RT_EOK;
|
||||
encoder = (struct rt_encoder_device *)dev;
|
||||
if (encoder->ops->init)
|
||||
{
|
||||
ret = encoder->ops->init(encoder);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_encoder_device *encoder;
|
||||
|
||||
ret = RT_EOK;
|
||||
encoder = (struct rt_encoder_device *)dev;
|
||||
if (encoder->ops->control)
|
||||
{
|
||||
ret = encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_err_t rt_encoder_close(struct rt_device *dev)
|
||||
{
|
||||
rt_err_t ret;
|
||||
struct rt_encoder_device *encoder;
|
||||
|
||||
ret = RT_EOK;
|
||||
encoder = (struct rt_encoder_device *)dev;
|
||||
if (encoder->ops->control)
|
||||
{
|
||||
ret = encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_size_t rt_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||
{
|
||||
struct rt_encoder_device *encoder;
|
||||
|
||||
encoder = (struct rt_encoder_device *)dev;
|
||||
if (encoder->ops->get_count)
|
||||
{
|
||||
*(rt_int32_t *)buffer = encoder->ops->get_count(encoder);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static rt_err_t rt_encoder_control(struct rt_device *dev, int cmd, void *args)
|
||||
{
|
||||
rt_err_t result;
|
||||
struct rt_encoder_device *encoder;
|
||||
|
||||
result = RT_EOK;
|
||||
encoder = (struct rt_encoder_device *)dev;
|
||||
switch (cmd)
|
||||
{
|
||||
case ENCODER_CMD_CLEAR_COUNT:
|
||||
result = encoder->ops->clear_count(encoder);
|
||||
break;
|
||||
case ENCODER_CMD_GET_TYPE:
|
||||
*(enum rt_encoder_type *)args = encoder->type;
|
||||
break;
|
||||
case ENCODER_CMD_ENABLE:
|
||||
case ENCODER_CMD_DISABLE:
|
||||
result = encoder->ops->control(encoder, cmd, args);
|
||||
break;
|
||||
default:
|
||||
result = -RT_ENOSYS;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops encoder_ops =
|
||||
{
|
||||
rt_encoder_init,
|
||||
rt_encoder_open,
|
||||
rt_encoder_close,
|
||||
rt_encoder_read,
|
||||
RT_NULL,
|
||||
rt_encoder_control
|
||||
};
|
||||
#endif
|
||||
|
||||
rt_err_t rt_device_encoder_register(struct rt_encoder_device *encoder, const char *name, void *user_data)
|
||||
{
|
||||
struct rt_device *device;
|
||||
|
||||
RT_ASSERT(encoder != RT_NULL);
|
||||
RT_ASSERT(encoder->ops != RT_NULL);
|
||||
RT_ASSERT(encoder->ops->get_count != RT_NULL);
|
||||
|
||||
device = &(encoder->parent);
|
||||
|
||||
device->type = RT_Device_Class_Miscellaneous;
|
||||
device->rx_indicate = RT_NULL;
|
||||
device->tx_complete = RT_NULL;
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
device->ops = &encoder_ops;
|
||||
#else
|
||||
device->init = rt_encoder_init;
|
||||
device->open = rt_encoder_open;
|
||||
device->close = rt_encoder_close;
|
||||
device->read = rt_encoder_read;
|
||||
device->write = RT_NULL;
|
||||
device->control = rt_encoder_control;
|
||||
#endif
|
||||
device->user_data = user_data;
|
||||
|
||||
return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE);
|
||||
}
|
136
components/drivers/misc/pulse_encoder.c
Normal file
136
components/drivers/misc/pulse_encoder.c
Normal file
@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-08-08 balanceTWK the first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
static rt_err_t rt_pulse_encoder_init(struct rt_device *dev)
|
||||
{
|
||||
struct rt_pulse_encoder_device *pulse_encoder;
|
||||
|
||||
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
|
||||
if (pulse_encoder->ops->init)
|
||||
{
|
||||
return pulse_encoder->ops->init(pulse_encoder);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t rt_pulse_encoder_open(struct rt_device *dev, rt_uint16_t oflag)
|
||||
{
|
||||
struct rt_pulse_encoder_device *pulse_encoder;
|
||||
|
||||
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
|
||||
if (pulse_encoder->ops->control)
|
||||
{
|
||||
return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_ENABLE, RT_NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
}
|
||||
|
||||
static rt_err_t rt_pulse_encoder_close(struct rt_device *dev)
|
||||
{
|
||||
struct rt_pulse_encoder_device *pulse_encoder;
|
||||
|
||||
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
|
||||
if (pulse_encoder->ops->control)
|
||||
{
|
||||
return pulse_encoder->ops->control(pulse_encoder, PULSE_ENCODER_CMD_DISABLE, RT_NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
}
|
||||
|
||||
static rt_size_t rt_pulse_encoder_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||
{
|
||||
struct rt_pulse_encoder_device *pulse_encoder;
|
||||
|
||||
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
|
||||
if (pulse_encoder->ops->get_count)
|
||||
{
|
||||
*(rt_int32_t *)buffer = pulse_encoder->ops->get_count(pulse_encoder);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static rt_err_t rt_pulse_encoder_control(struct rt_device *dev, int cmd, void *args)
|
||||
{
|
||||
rt_err_t result;
|
||||
struct rt_pulse_encoder_device *pulse_encoder;
|
||||
|
||||
result = RT_EOK;
|
||||
pulse_encoder = (struct rt_pulse_encoder_device *)dev;
|
||||
switch (cmd)
|
||||
{
|
||||
case PULSE_ENCODER_CMD_CLEAR_COUNT:
|
||||
result = pulse_encoder->ops->clear_count(pulse_encoder);
|
||||
break;
|
||||
case PULSE_ENCODER_CMD_GET_TYPE:
|
||||
*(enum rt_pulse_encoder_type *)args = pulse_encoder->type;
|
||||
break;
|
||||
case PULSE_ENCODER_CMD_ENABLE:
|
||||
case PULSE_ENCODER_CMD_DISABLE:
|
||||
result = pulse_encoder->ops->control(pulse_encoder, cmd, args);
|
||||
break;
|
||||
default:
|
||||
result = -RT_ENOSYS;
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops pulse_encoder_ops =
|
||||
{
|
||||
rt_pulse_encoder_init,
|
||||
rt_pulse_encoder_open,
|
||||
rt_pulse_encoder_close,
|
||||
rt_pulse_encoder_read,
|
||||
RT_NULL,
|
||||
rt_pulse_encoder_control
|
||||
};
|
||||
#endif
|
||||
|
||||
rt_err_t rt_device_pulse_encoder_register(struct rt_pulse_encoder_device *pulse_encoder, const char *name, void *user_data)
|
||||
{
|
||||
struct rt_device *device;
|
||||
|
||||
RT_ASSERT(pulse_encoder != RT_NULL);
|
||||
RT_ASSERT(pulse_encoder->ops != RT_NULL);
|
||||
|
||||
device = &(pulse_encoder->parent);
|
||||
|
||||
device->type = RT_Device_Class_Miscellaneous;
|
||||
device->rx_indicate = RT_NULL;
|
||||
device->tx_complete = RT_NULL;
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
device->ops = &pulse_encoder_ops;
|
||||
#else
|
||||
device->init = rt_pulse_encoder_init;
|
||||
device->open = rt_pulse_encoder_open;
|
||||
device->close = rt_pulse_encoder_close;
|
||||
device->read = rt_pulse_encoder_read;
|
||||
device->write = RT_NULL;
|
||||
device->control = rt_pulse_encoder_control;
|
||||
#endif
|
||||
device->user_data = user_data;
|
||||
|
||||
return rt_device_register(device, name, RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_STANDALONE);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user