From 1d4139ab5357dce6648554f2b7243bdd6fda6ec9 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 23 Aug 2019 14:08:02 +0800 Subject: [PATCH 1/4] [bsp][stm32] add drv_encoder.c --- .../HAL_Drivers/config/f4/encoder_config.h | 64 +++++++ bsp/stm32/libraries/HAL_Drivers/drv_config.h | 1 + bsp/stm32/libraries/HAL_Drivers/drv_encoder.c | 181 ++++++++++++++++++ bsp/stm32/libraries/STM32F4xx_HAL/SConscript | 2 +- 4 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h create mode 100644 bsp/stm32/libraries/HAL_Drivers/drv_encoder.c diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h new file mode 100644 index 0000000000..80d515955f --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/config/f4/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 __ENCODER_CONFIG_H__ +#define __ENCODER_CONFIG_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef BSP_USING_ENCODER1 +#ifndef ENCODER1_CONFIG +#define ENCODER1_CONFIG \ + { \ + .tim_handler.Instance = TIM1, \ + .name = "encoder1" \ + } +#endif /* ENCODER1_CONFIG */ +#endif /* BSP_USING_ENCODER1 */ + +#ifdef BSP_USING_ENCODER2 +#ifndef ENCODER2_CONFIG +#define ENCODER2_CONFIG \ + { \ + .tim_handler.Instance = TIM2, \ + .name = "encoder2" \ + } +#endif /* ENCODER2_CONFIG */ +#endif /* BSP_USING_ENCODER2 */ + +#ifdef BSP_USING_ENCODER3 +#ifndef ENCODER3_CONFIG +#define ENCODER3_CONFIG \ + { \ + .tim_handler.Instance = TIM3, \ + .name = "encoder3" \ + } +#endif /* ENCODER3_CONFIG */ +#endif /* BSP_USING_ENCODER3 */ + +#ifdef BSP_USING_ENCODER4 +#ifndef ENCODER4_CONFIG +#define ENCODER4_CONFIG \ + { \ + .tim_handler.Instance = TIM4, \ + .name = "encoder4" \ + } +#endif /* ENCODER4_CONFIG */ +#endif /* BSP_USING_ENCODER4 */ + +#ifdef __cplusplus +} +#endif + +#endif /* __ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_config.h b/bsp/stm32/libraries/HAL_Drivers/drv_config.h index 639c1151a6..7eb4b5d657 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_config.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_config.h @@ -52,6 +52,7 @@ extern "C" { #include "f4/tim_config.h" #include "f4/sdio_config.h" #include "f4/pwm_config.h" +#include "f4/encoder_config.h" #elif defined(SOC_SERIES_STM32F7) #include "f7/dma_config.h" #include "f7/uart_config.h" diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c new file mode 100644 index 0000000000..692bc019ba --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c @@ -0,0 +1,181 @@ +/* + * @Description: In User Settings Edit + * @Author: your name + * @Date: 2019-08-09 15:25:34 + * @LastEditTime: 2019-08-09 17:29:27 + * @LastEditors: Please set LastEditors + */ +/* + * 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_ENCODER + +//#define DRV_DEBUG +#define LOG_TAG "drv.encoder" +#include + +#if !defined(BSP_USING_ENCODER1) && !defined(BSP_USING_ENCODER2) && !defined(BSP_USING_ENCODER3) \ + && !defined(BSP_USING_ENCODER4) && !defined(BSP_USING_ENCODER5) && !defined(BSP_USING_ENCODER6) && !defined(BSP_USING_LPENCODER1) + #error "Please define at least one BSP_USING_ENCODERx" + /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ +#endif + +enum +{ +#ifdef BSP_USING_ENCODER1 + ENCODER1_INDEX, +#endif +#ifdef BSP_USING_ENCODER2 + ENCODER2_INDEX, +#endif +#ifdef BSP_USING_ENCODER3 + ENCODER3_INDEX, +#endif +#ifdef BSP_USING_ENCODER4 + ENCODER4_INDEX, +#endif +#ifdef BSP_USING_ENCODER5 + ENCODER5_INDEX, +#endif +#ifdef BSP_USING_ENCODER6 + ENCODER6_INDEX, +#endif +}; + +struct stm32_encoder_device +{ + struct rt_encoder_device encoder; + TIM_HandleTypeDef tim_handler; + char *name; +}; + +static struct stm32_encoder_device stm32_encoder_obj[] = +{ +#ifdef BSP_USING_ENCODER1 + ENCODER1_CONFIG, +#endif +#ifdef BSP_USING_ENCODER2 + ENCODER2_CONFIG, +#endif +#ifdef BSP_USING_ENCODER3 + ENCODER3_CONFIG, +#endif +#ifdef BSP_USING_ENCODER4 + ENCODER4_CONFIG, +#endif +#ifdef BSP_USING_ENCODER5 + ENCODER5_CONFIG, +#endif +#ifdef BSP_USING_ENCODER6 + ENCODER6_CONFIG, +#endif +}; + +rt_err_t encoder_init(struct rt_encoder_device *encoder) +{ + TIM_Encoder_InitTypeDef sConfig; + TIM_MasterConfigTypeDef sMasterConfig; + TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)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) + { + return -RT_ERROR; + } + + sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; + sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; + + if (HAL_TIMEx_MasterConfigSynchronization(tim_handler, &sMasterConfig)) + { + return -RT_ERROR; + } + + return RT_EOK; +} + +rt_int32_t encoder_get_count(struct rt_encoder_device *encoder) +{ + TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; + return (rt_int16_t)__HAL_TIM_GET_COUNTER(tim_handler); +} + +rt_err_t encoder_control(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args) +{ + rt_err_t result; + TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; + + result = RT_EOK; + + switch (cmd) + { + case ENCODER_CMD_ENABLE: + HAL_TIM_Encoder_Start(tim_handler, TIM_CHANNEL_ALL); + break; + case ENCODER_CMD_DISABLE: + HAL_TIM_Encoder_Stop(tim_handler, TIM_CHANNEL_ALL); + break; + case ENCODER_CMD_CLEAR_COUNT: + __HAL_TIM_SET_COUNTER(tim_handler, 0); + break; + default: + result = -RT_ENOSYS; + break; + } + + return result; +} + +static const struct rt_encoder_ops _ops = +{ + .init = encoder_init, + .get_count = encoder_get_count, + .control = encoder_control, +}; + +int hw_encoder_init(void) +{ + int i; + int result; + + result = RT_EOK; + for (i = 0; i < sizeof(stm32_encoder_obj) / sizeof(stm32_encoder_obj[0]); i++) + { + stm32_encoder_obj[i].encoder.type = AB_PHASE_ENCODER; + stm32_encoder_obj[i].encoder.ops = &_ops; + + if (rt_device_encoder_register(&stm32_encoder_obj[i].encoder, stm32_encoder_obj[i].name, &stm32_encoder_obj[i].tim_handler) != RT_EOK) + { + LOG_E("%s register failed", stm32_encoder_obj[i].name); + result = -RT_ERROR; + } + } + + return result; +} +INIT_BOARD_EXPORT(hw_encoder_init); + +#endif diff --git a/bsp/stm32/libraries/STM32F4xx_HAL/SConscript b/bsp/stm32/libraries/STM32F4xx_HAL/SConscript index 714d49ba02..7bf1638169 100644 --- a/bsp/stm32/libraries/STM32F4xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32F4xx_HAL/SConscript @@ -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_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'] From 94d7bdd0109651c09102a9a043071aad4b294ab1 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 23 Aug 2019 16:50:59 +0800 Subject: [PATCH 2/4] [bsp][stm32] add drv_encoder.c to stm32f407-atk-explorer --- bsp/stm32/libraries/HAL_Drivers/SConscript | 3 + bsp/stm32/libraries/HAL_Drivers/drv_encoder.c | 2 + .../board/CubeMX_Config/.mxproject | 5 +- .../board/CubeMX_Config/CubeMX_Config.ioc | 67 ++++++++++-------- .../CubeMX_Config/Inc/stm32f4xx_hal_conf.h | 2 +- .../board/CubeMX_Config/Src/main.c | 51 ++++++++++++++ .../CubeMX_Config/Src/stm32f4xx_hal_msp.c | 68 +++++++++++++++++++ .../stm32f407-atk-explorer/board/Kconfig | 12 ++++ 8 files changed, 179 insertions(+), 31 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/SConscript b/bsp/stm32/libraries/HAL_Drivers/SConscript index 0cb64a650e..cb675a16d3 100644 --- a/bsp/stm32/libraries/HAL_Drivers/SConscript +++ b/bsp/stm32/libraries/HAL_Drivers/SConscript @@ -85,6 +85,9 @@ if GetDepend(['BSP_USING_SDIO']): if GetDepend(['BSP_USING_USBD_FS']): src += ['drv_usbd_fs.c'] +if GetDepend(['BSP_USING_ENCODER']): + src += ['drv_encoder.c'] + src += ['drv_common.c'] path = [cwd] diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c index 692bc019ba..d896f23983 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c @@ -103,6 +103,7 @@ rt_err_t encoder_init(struct rt_encoder_device *encoder) if (HAL_TIM_Encoder_Init(tim_handler, &sConfig) != HAL_OK) { + LOG_E("encoder init failed"); return -RT_ERROR; } @@ -111,6 +112,7 @@ rt_err_t encoder_init(struct rt_encoder_device *encoder) if (HAL_TIMEx_MasterConfigSynchronization(tim_handler, &sMasterConfig)) { + LOG_E("TIMx master config failed"); return -RT_ERROR; } diff --git a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/.mxproject b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/.mxproject index 7e119922f0..031ff82f9a 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/.mxproject +++ b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/.mxproject @@ -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; diff --git a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/CubeMX_Config.ioc b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/CubeMX_Config.ioc index 770acec332..fcc30e407e 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/CubeMX_Config.ioc +++ b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/CubeMX_Config.ioc @@ -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 diff --git a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Inc/stm32f4xx_hal_conf.h b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Inc/stm32f4xx_hal_conf.h index 37853623c2..9b15113c2c 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Inc/stm32f4xx_hal_conf.h +++ b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Inc/stm32f4xx_hal_conf.h @@ -5,7 +5,7 @@ ****************************************************************************** * @attention * - *

© COPYRIGHT(c) 2018 STMicroelectronics

+ *

© COPYRIGHT(c) 2019 STMicroelectronics

* * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: diff --git a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/main.c b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/main.c index 2577da8b41..ef64d82dc1 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/main.c +++ b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/main.c @@ -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 diff --git a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/stm32f4xx_hal_msp.c b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/stm32f4xx_hal_msp.c index 258ea04acc..90a333827e 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/stm32f4xx_hal_msp.c +++ b/bsp/stm32/stm32f407-atk-explorer/board/CubeMX_Config/Src/stm32f4xx_hal_msp.c @@ -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 diff --git a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig index 4ee6fdd657..1f0de499ed 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig +++ b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig @@ -293,6 +293,18 @@ menu "On-chip Peripheral Drivers" select RT_USING_SDIO select RT_USING_DFS default n + + menuconfig BSP_USING_ENCODER + bool "Enable Encoder" + default n + select RT_USING_ENCODER + if BSP_USING_ENCODER + config BSP_USING_ENCODER4 + bool "Enable Encoder4" + default n + + endif + source "../libraries/HAL_Drivers/Kconfig" endmenu From 05b1dff0946c846926b4159b52b3e45156a5f7fc Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 23 Aug 2019 17:16:51 +0800 Subject: [PATCH 3/4] [bsp][stm32] add drv_encoder.c to stm32f103-atk-warshipv3 --- .../HAL_Drivers/config/f1/encoder_config.h | 64 ++++++++++++++++++ bsp/stm32/libraries/HAL_Drivers/drv_config.h | 1 + bsp/stm32/libraries/HAL_Drivers/drv_encoder.c | 7 -- bsp/stm32/libraries/STM32F1xx_HAL/SConscript | 2 +- .../board/CubeMX_Config/.mxproject | 5 +- .../board/CubeMX_Config/CubeMX_Config.ioc | 29 ++++++--- .../CubeMX_Config/Inc/stm32f1xx_hal_conf.h | 2 +- .../board/CubeMX_Config/Src/main.c | 53 +++++++++++++++ .../CubeMX_Config/Src/stm32f1xx_hal_msp.c | 65 +++++++++++++++++++ .../stm32f103-atk-warshipv3/board/Kconfig | 13 +++- 10 files changed, 220 insertions(+), 21 deletions(-) create mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h new file mode 100644 index 0000000000..80d515955f --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/config/f1/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 __ENCODER_CONFIG_H__ +#define __ENCODER_CONFIG_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef BSP_USING_ENCODER1 +#ifndef ENCODER1_CONFIG +#define ENCODER1_CONFIG \ + { \ + .tim_handler.Instance = TIM1, \ + .name = "encoder1" \ + } +#endif /* ENCODER1_CONFIG */ +#endif /* BSP_USING_ENCODER1 */ + +#ifdef BSP_USING_ENCODER2 +#ifndef ENCODER2_CONFIG +#define ENCODER2_CONFIG \ + { \ + .tim_handler.Instance = TIM2, \ + .name = "encoder2" \ + } +#endif /* ENCODER2_CONFIG */ +#endif /* BSP_USING_ENCODER2 */ + +#ifdef BSP_USING_ENCODER3 +#ifndef ENCODER3_CONFIG +#define ENCODER3_CONFIG \ + { \ + .tim_handler.Instance = TIM3, \ + .name = "encoder3" \ + } +#endif /* ENCODER3_CONFIG */ +#endif /* BSP_USING_ENCODER3 */ + +#ifdef BSP_USING_ENCODER4 +#ifndef ENCODER4_CONFIG +#define ENCODER4_CONFIG \ + { \ + .tim_handler.Instance = TIM4, \ + .name = "encoder4" \ + } +#endif /* ENCODER4_CONFIG */ +#endif /* BSP_USING_ENCODER4 */ + +#ifdef __cplusplus +} +#endif + +#endif /* __ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_config.h b/bsp/stm32/libraries/HAL_Drivers/drv_config.h index 7eb4b5d657..9f5194d960 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_config.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_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/encoder_config.h" #elif defined(SOC_SERIES_STM32F2) #include "f2/dma_config.h" #include "f2/uart_config.h" diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c index d896f23983..ca42c151fa 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c @@ -1,10 +1,3 @@ -/* - * @Description: In User Settings Edit - * @Author: your name - * @Date: 2019-08-09 15:25:34 - * @LastEditTime: 2019-08-09 17:29:27 - * @LastEditors: Please set LastEditors - */ /* * Copyright (c) 2006-2018, RT-Thread Development Team * diff --git a/bsp/stm32/libraries/STM32F1xx_HAL/SConscript b/bsp/stm32/libraries/STM32F1xx_HAL/SConscript index 403698f9f8..8d554cac85 100644 --- a/bsp/stm32/libraries/STM32F1xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32F1xx_HAL/SConscript @@ -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_ENCODER']): src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c'] src += ['STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c'] diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/.mxproject b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/.mxproject index bab08a26bd..3701927b37 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/.mxproject +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/.mxproject @@ -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; diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/CubeMX_Config.ioc b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/CubeMX_Config.ioc index 0482a4927a..905e7b4364 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/CubeMX_Config.ioc +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/CubeMX_Config.ioc @@ -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 diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Inc/stm32f1xx_hal_conf.h b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Inc/stm32f1xx_hal_conf.h index 9ab2b09148..a6ed331c5f 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Inc/stm32f1xx_hal_conf.h +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Inc/stm32f1xx_hal_conf.h @@ -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 */ diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/main.c b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/main.c index c183908d36..2e51a42f8e 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/main.c +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/main.c @@ -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 diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/stm32f1xx_hal_msp.c b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/stm32f1xx_hal_msp.c index bd70146cfe..0422f4b858 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/stm32f1xx_hal_msp.c +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/CubeMX_Config/Src/stm32f1xx_hal_msp.c @@ -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 diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig b/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig index 8cbacb4bab..8f34aca198 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig @@ -200,7 +200,18 @@ menu "On-chip Peripheral Drivers" bool "Enable Watchdog Timer" select RT_USING_WDT default n - + + menuconfig BSP_USING_ENCODER + bool "Enable Encoder" + default n + select RT_USING_ENCODER + if BSP_USING_ENCODER + config BSP_USING_ENCODER3 + bool "Enable Encoder3" + default n + + endif + source "../libraries/HAL_Drivers/Kconfig" endmenu From b76d3dac8a92b36993169e5d38a6960b3210af04 Mon Sep 17 00:00:00 2001 From: tangweikang Date: Fri, 30 Aug 2019 15:38:05 +0800 Subject: [PATCH 4/4] [components][drivers] change name: encoder -> pulse_encoder --- bsp/stm32/libraries/HAL_Drivers/SConscript | 4 +- .../HAL_Drivers/config/f1/encoder_config.h | 64 ------- .../config/f1/pulse_encoder_config.h | 64 +++++++ .../HAL_Drivers/config/f4/encoder_config.h | 64 ------- .../config/f4/pulse_encoder_config.h | 64 +++++++ bsp/stm32/libraries/HAL_Drivers/drv_config.h | 4 +- bsp/stm32/libraries/HAL_Drivers/drv_encoder.c | 176 ------------------ .../libraries/HAL_Drivers/drv_pulse_encoder.c | 176 ++++++++++++++++++ bsp/stm32/libraries/STM32F1xx_HAL/SConscript | 2 +- bsp/stm32/libraries/STM32F4xx_HAL/SConscript | 2 +- .../stm32f103-atk-warshipv3/board/Kconfig | 12 +- .../stm32f407-atk-explorer/board/Kconfig | 12 +- components/drivers/Kconfig | 4 +- components/drivers/include/drivers/encoder.h | 58 ------ .../drivers/include/drivers/pulse_encoder.h | 58 ++++++ components/drivers/include/rtdevice.h | 4 +- components/drivers/misc/SConscript | 4 +- components/drivers/misc/encoder.c | 136 -------------- components/drivers/misc/pulse_encoder.c | 136 ++++++++++++++ 19 files changed, 522 insertions(+), 522 deletions(-) delete mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h create mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h delete mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h create mode 100644 bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h delete mode 100644 bsp/stm32/libraries/HAL_Drivers/drv_encoder.c create mode 100644 bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c delete mode 100644 components/drivers/include/drivers/encoder.h create mode 100644 components/drivers/include/drivers/pulse_encoder.h delete mode 100644 components/drivers/misc/encoder.c create mode 100644 components/drivers/misc/pulse_encoder.c diff --git a/bsp/stm32/libraries/HAL_Drivers/SConscript b/bsp/stm32/libraries/HAL_Drivers/SConscript index cb675a16d3..1d37075fcd 100644 --- a/bsp/stm32/libraries/HAL_Drivers/SConscript +++ b/bsp/stm32/libraries/HAL_Drivers/SConscript @@ -85,8 +85,8 @@ if GetDepend(['BSP_USING_SDIO']): if GetDepend(['BSP_USING_USBD_FS']): src += ['drv_usbd_fs.c'] -if GetDepend(['BSP_USING_ENCODER']): - src += ['drv_encoder.c'] +if GetDepend(['BSP_USING_PULSE_ENCODER']): + src += ['drv_pulse_encoder.c'] src += ['drv_common.c'] diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h deleted file mode 100644 index 80d515955f..0000000000 --- a/bsp/stm32/libraries/HAL_Drivers/config/f1/encoder_config.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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 __ENCODER_CONFIG_H__ -#define __ENCODER_CONFIG_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef BSP_USING_ENCODER1 -#ifndef ENCODER1_CONFIG -#define ENCODER1_CONFIG \ - { \ - .tim_handler.Instance = TIM1, \ - .name = "encoder1" \ - } -#endif /* ENCODER1_CONFIG */ -#endif /* BSP_USING_ENCODER1 */ - -#ifdef BSP_USING_ENCODER2 -#ifndef ENCODER2_CONFIG -#define ENCODER2_CONFIG \ - { \ - .tim_handler.Instance = TIM2, \ - .name = "encoder2" \ - } -#endif /* ENCODER2_CONFIG */ -#endif /* BSP_USING_ENCODER2 */ - -#ifdef BSP_USING_ENCODER3 -#ifndef ENCODER3_CONFIG -#define ENCODER3_CONFIG \ - { \ - .tim_handler.Instance = TIM3, \ - .name = "encoder3" \ - } -#endif /* ENCODER3_CONFIG */ -#endif /* BSP_USING_ENCODER3 */ - -#ifdef BSP_USING_ENCODER4 -#ifndef ENCODER4_CONFIG -#define ENCODER4_CONFIG \ - { \ - .tim_handler.Instance = TIM4, \ - .name = "encoder4" \ - } -#endif /* ENCODER4_CONFIG */ -#endif /* BSP_USING_ENCODER4 */ - -#ifdef __cplusplus -} -#endif - -#endif /* __ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f1/pulse_encoder_config.h new file mode 100644 index 0000000000..4621240c98 --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/config/f1/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 + +#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__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h deleted file mode 100644 index 80d515955f..0000000000 --- a/bsp/stm32/libraries/HAL_Drivers/config/f4/encoder_config.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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 __ENCODER_CONFIG_H__ -#define __ENCODER_CONFIG_H__ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef BSP_USING_ENCODER1 -#ifndef ENCODER1_CONFIG -#define ENCODER1_CONFIG \ - { \ - .tim_handler.Instance = TIM1, \ - .name = "encoder1" \ - } -#endif /* ENCODER1_CONFIG */ -#endif /* BSP_USING_ENCODER1 */ - -#ifdef BSP_USING_ENCODER2 -#ifndef ENCODER2_CONFIG -#define ENCODER2_CONFIG \ - { \ - .tim_handler.Instance = TIM2, \ - .name = "encoder2" \ - } -#endif /* ENCODER2_CONFIG */ -#endif /* BSP_USING_ENCODER2 */ - -#ifdef BSP_USING_ENCODER3 -#ifndef ENCODER3_CONFIG -#define ENCODER3_CONFIG \ - { \ - .tim_handler.Instance = TIM3, \ - .name = "encoder3" \ - } -#endif /* ENCODER3_CONFIG */ -#endif /* BSP_USING_ENCODER3 */ - -#ifdef BSP_USING_ENCODER4 -#ifndef ENCODER4_CONFIG -#define ENCODER4_CONFIG \ - { \ - .tim_handler.Instance = TIM4, \ - .name = "encoder4" \ - } -#endif /* ENCODER4_CONFIG */ -#endif /* BSP_USING_ENCODER4 */ - -#ifdef __cplusplus -} -#endif - -#endif /* __ENCODER_CONFIG_H__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h b/bsp/stm32/libraries/HAL_Drivers/config/f4/pulse_encoder_config.h new file mode 100644 index 0000000000..4621240c98 --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/config/f4/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 + +#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__ */ diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_config.h b/bsp/stm32/libraries/HAL_Drivers/drv_config.h index 9f5194d960..fa4555fe50 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_config.h +++ b/bsp/stm32/libraries/HAL_Drivers/drv_config.h @@ -34,7 +34,7 @@ extern "C" { #include "f1/sdio_config.h" #include "f1/pwm_config.h" #include "f1/usbd_fs_config.h" -#include "f1/encoder_config.h" +#include "f1/pulse_encoder_config.h" #elif defined(SOC_SERIES_STM32F2) #include "f2/dma_config.h" #include "f2/uart_config.h" @@ -53,7 +53,7 @@ extern "C" { #include "f4/tim_config.h" #include "f4/sdio_config.h" #include "f4/pwm_config.h" -#include "f4/encoder_config.h" +#include "f4/pulse_encoder_config.h" #elif defined(SOC_SERIES_STM32F7) #include "f7/dma_config.h" #include "f7/uart_config.h" diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c deleted file mode 100644 index ca42c151fa..0000000000 --- a/bsp/stm32/libraries/HAL_Drivers/drv_encoder.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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_ENCODER - -//#define DRV_DEBUG -#define LOG_TAG "drv.encoder" -#include - -#if !defined(BSP_USING_ENCODER1) && !defined(BSP_USING_ENCODER2) && !defined(BSP_USING_ENCODER3) \ - && !defined(BSP_USING_ENCODER4) && !defined(BSP_USING_ENCODER5) && !defined(BSP_USING_ENCODER6) && !defined(BSP_USING_LPENCODER1) - #error "Please define at least one BSP_USING_ENCODERx" - /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */ -#endif - -enum -{ -#ifdef BSP_USING_ENCODER1 - ENCODER1_INDEX, -#endif -#ifdef BSP_USING_ENCODER2 - ENCODER2_INDEX, -#endif -#ifdef BSP_USING_ENCODER3 - ENCODER3_INDEX, -#endif -#ifdef BSP_USING_ENCODER4 - ENCODER4_INDEX, -#endif -#ifdef BSP_USING_ENCODER5 - ENCODER5_INDEX, -#endif -#ifdef BSP_USING_ENCODER6 - ENCODER6_INDEX, -#endif -}; - -struct stm32_encoder_device -{ - struct rt_encoder_device encoder; - TIM_HandleTypeDef tim_handler; - char *name; -}; - -static struct stm32_encoder_device stm32_encoder_obj[] = -{ -#ifdef BSP_USING_ENCODER1 - ENCODER1_CONFIG, -#endif -#ifdef BSP_USING_ENCODER2 - ENCODER2_CONFIG, -#endif -#ifdef BSP_USING_ENCODER3 - ENCODER3_CONFIG, -#endif -#ifdef BSP_USING_ENCODER4 - ENCODER4_CONFIG, -#endif -#ifdef BSP_USING_ENCODER5 - ENCODER5_CONFIG, -#endif -#ifdef BSP_USING_ENCODER6 - ENCODER6_CONFIG, -#endif -}; - -rt_err_t encoder_init(struct rt_encoder_device *encoder) -{ - TIM_Encoder_InitTypeDef sConfig; - TIM_MasterConfigTypeDef sMasterConfig; - TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)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("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 encoder_get_count(struct rt_encoder_device *encoder) -{ - TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; - return (rt_int16_t)__HAL_TIM_GET_COUNTER(tim_handler); -} - -rt_err_t encoder_control(struct rt_encoder_device *encoder, rt_uint32_t cmd, void *args) -{ - rt_err_t result; - TIM_HandleTypeDef *tim_handler = (TIM_HandleTypeDef *)encoder->parent.user_data; - - result = RT_EOK; - - switch (cmd) - { - case ENCODER_CMD_ENABLE: - HAL_TIM_Encoder_Start(tim_handler, TIM_CHANNEL_ALL); - break; - case ENCODER_CMD_DISABLE: - HAL_TIM_Encoder_Stop(tim_handler, TIM_CHANNEL_ALL); - break; - case ENCODER_CMD_CLEAR_COUNT: - __HAL_TIM_SET_COUNTER(tim_handler, 0); - break; - default: - result = -RT_ENOSYS; - break; - } - - return result; -} - -static const struct rt_encoder_ops _ops = -{ - .init = encoder_init, - .get_count = encoder_get_count, - .control = encoder_control, -}; - -int hw_encoder_init(void) -{ - int i; - int result; - - result = RT_EOK; - for (i = 0; i < sizeof(stm32_encoder_obj) / sizeof(stm32_encoder_obj[0]); i++) - { - stm32_encoder_obj[i].encoder.type = AB_PHASE_ENCODER; - stm32_encoder_obj[i].encoder.ops = &_ops; - - if (rt_device_encoder_register(&stm32_encoder_obj[i].encoder, stm32_encoder_obj[i].name, &stm32_encoder_obj[i].tim_handler) != RT_EOK) - { - LOG_E("%s register failed", stm32_encoder_obj[i].name); - result = -RT_ERROR; - } - } - - return result; -} -INIT_BOARD_EXPORT(hw_encoder_init); - -#endif diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c b/bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c new file mode 100644 index 0000000000..432b8b2a05 --- /dev/null +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pulse_encoder.c @@ -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 + +#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 diff --git a/bsp/stm32/libraries/STM32F1xx_HAL/SConscript b/bsp/stm32/libraries/STM32F1xx_HAL/SConscript index 8d554cac85..f629479376 100644 --- a/bsp/stm32/libraries/STM32F1xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32F1xx_HAL/SConscript @@ -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']) or GetDepend(['RT_USING_ENCODER']): +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'] diff --git a/bsp/stm32/libraries/STM32F4xx_HAL/SConscript b/bsp/stm32/libraries/STM32F4xx_HAL/SConscript index 7bf1638169..87c000ae18 100644 --- a/bsp/stm32/libraries/STM32F4xx_HAL/SConscript +++ b/bsp/stm32/libraries/STM32F4xx_HAL/SConscript @@ -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']) or GetDepend(['RT_USING_ENCODER']): +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'] diff --git a/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig b/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig index 8f34aca198..e02250d243 100644 --- a/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig +++ b/bsp/stm32/stm32f103-atk-warshipv3/board/Kconfig @@ -201,13 +201,13 @@ menu "On-chip Peripheral Drivers" select RT_USING_WDT default n - menuconfig BSP_USING_ENCODER - bool "Enable Encoder" + menuconfig BSP_USING_PULSE_ENCODER + bool "Enable Pulse Encoder" default n - select RT_USING_ENCODER - if BSP_USING_ENCODER - config BSP_USING_ENCODER3 - bool "Enable Encoder3" + select RT_USING_PULSE_ENCODER + if BSP_USING_PULSE_ENCODER + config BSP_USING_PULSE_ENCODER3 + bool "Enable Pulse Encoder3" default n endif diff --git a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig index 1f0de499ed..2a235f0550 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/Kconfig +++ b/bsp/stm32/stm32f407-atk-explorer/board/Kconfig @@ -294,13 +294,13 @@ menu "On-chip Peripheral Drivers" select RT_USING_DFS default n - menuconfig BSP_USING_ENCODER - bool "Enable Encoder" + menuconfig BSP_USING_PULSE_ENCODER + bool "Enable Pulse Encoder" default n - select RT_USING_ENCODER - if BSP_USING_ENCODER - config BSP_USING_ENCODER4 - bool "Enable Encoder4" + select RT_USING_PULSE_ENCODER + if BSP_USING_PULSE_ENCODER + config BSP_USING_PULSE_ENCODER4 + bool "Enable Pulse Encoder4" default n endif diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig index ff30fa6162..ea928f452d 100755 --- a/components/drivers/Kconfig +++ b/components/drivers/Kconfig @@ -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 diff --git a/components/drivers/include/drivers/encoder.h b/components/drivers/include/drivers/encoder.h deleted file mode 100644 index f338f9e845..0000000000 --- a/components/drivers/include/drivers/encoder.h +++ /dev/null @@ -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 -#include - -#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__ */ diff --git a/components/drivers/include/drivers/pulse_encoder.h b/components/drivers/include/drivers/pulse_encoder.h new file mode 100644 index 0000000000..0c7df04cd6 --- /dev/null +++ b/components/drivers/include/drivers/pulse_encoder.h @@ -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 +#include + +#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__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index bb302ac2af..623c085430 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.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 diff --git a/components/drivers/misc/SConscript b/components/drivers/misc/SConscript index f0d177f761..8a9fdb5eb9 100644 --- a/components/drivers/misc/SConscript +++ b/components/drivers/misc/SConscript @@ -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'] diff --git a/components/drivers/misc/encoder.c b/components/drivers/misc/encoder.c deleted file mode 100644 index e9e1da0f40..0000000000 --- a/components/drivers/misc/encoder.c +++ /dev/null @@ -1,136 +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 -#include - -static rt_err_t rt_encoder_init(struct rt_device *dev) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->init) - { - return encoder->ops->init(encoder); - } - else - { - return -RT_ENOSYS; - } -} - -static rt_err_t rt_encoder_open(struct rt_device *dev, rt_uint16_t oflag) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->control) - { - return encoder->ops->control(encoder, ENCODER_CMD_ENABLE, RT_NULL); - } - else - { - return -RT_ENOSYS; - } -} - -static rt_err_t rt_encoder_close(struct rt_device *dev) -{ - struct rt_encoder_device *encoder; - - encoder = (struct rt_encoder_device *)dev; - if (encoder->ops->control) - { - return encoder->ops->control(encoder, ENCODER_CMD_DISABLE, RT_NULL); - } - else - { - return -RT_ENOSYS; - } -} - -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); - - 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); -} diff --git a/components/drivers/misc/pulse_encoder.c b/components/drivers/misc/pulse_encoder.c new file mode 100644 index 0000000000..3145c24675 --- /dev/null +++ b/components/drivers/misc/pulse_encoder.c @@ -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 +#include + +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); +}