Merge pull request #5095 from charlown/master

support ch32f10x pwm driver.
This commit is contained in:
Bernard Xiong
2021-09-27 13:49:45 +08:00
committed by GitHub
6 changed files with 572 additions and 31 deletions
@@ -32,6 +32,9 @@ if GetDepend('SOC_ARM_SERIES_CH32F103'):
if GetDepend(['RT_USING_HWTIMER', 'BSP_USING_HWTIMER']):
src += ['drv_hwtimer_ch32f10x.c']
if GetDepend(['RT_USING_PWM', 'BSP_USING_PWM']):
src += ['drv_pwm_ch32f10x.c']
src += ['drv_common.c']
path = [cwd]
@@ -73,17 +73,16 @@ static void ch32f1_hwtimer_init(struct rt_hwtimer_device *device, rt_uint32_t st
if (state)
{
ch32f1_hwtimer_clock_init(hwtimer_dev->periph);
ch32f1_tim_clock_init(hwtimer_dev->periph);
hwtimer_info = ch32f1_hwtimer_info_config_get(hwtimer_dev->periph);
clk = ch32f1_hwtimer_clock_get(hwtimer_dev->periph);
clk = ch32f1_tim_clock_get(hwtimer_dev->periph);
prescaler_value = (rt_uint16_t)(clk / hwtimer_info->minfreq) - 1;
/*
* set interrupt callback one or each time need total time =
* (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
* (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
*/
TIM_TimeBaseInitType.TIM_Period = hwtimer_info->maxcnt - 1;
@@ -132,8 +131,7 @@ static rt_err_t ch32f1_hwtimer_start(struct rt_hwtimer_device *device, rt_uint32
hwtimer_dev = (struct hwtimer_device *)device;
/*
* interrupt callback one or each time need total time =
* (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
* (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
*/
TIM_SetCounter(hwtimer_dev->periph, 0);
@@ -197,15 +195,14 @@ static rt_err_t ch32f1_hwtimer_control(struct rt_hwtimer_device *device, rt_uint
rt_uint16_t prescaler_value = 0;
/*
*set interrupt callback one or each time need total time =
* (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
* (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
*/
if (arg != RT_NULL)
{
freq = *((rt_uint32_t *)arg);
clk = ch32f1_hwtimer_clock_get(hwtimer_dev->periph);
clk = ch32f1_tim_clock_get(hwtimer_dev->periph);
prescaler_value = (rt_uint16_t)(clk / freq) - 1;
@@ -369,4 +366,3 @@ void TIM4_IRQHandler(void)
#endif
#endif /* BSP_USING_HWTIMER */
File diff suppressed because it is too large Load Diff
+16 -16
View File
@@ -116,18 +116,18 @@ config BSP_USING_TIM
if BSP_USING_TIM1_PWM
config BSP_USING_TIM1_PWM_CH1
bool "using TIM1 channel 1 as pwm"
bool "using TIM1 channel 1"
default n
config BSP_USING_TIM1_PWM_CH2
bool "using TIM1 channel 2 as pwm"
bool "using TIM1 channel 2"
default n
config BSP_USING_TIM1_PWM_CH3
bool "using TIM1 channel 3 as pwm"
bool "using TIM1 channel 3"
config BSP_USING_TIM1_PWM_CH4
bool "using TIM1 channel 4 as pwm"
bool "using TIM1 channel 4"
endif
@@ -154,18 +154,18 @@ config BSP_USING_TIM
if BSP_USING_TIM2_PWM
config BSP_USING_TIM2_PWM_CH1
bool "using TIM2 channel 1 as pwm"
bool "using TIM2 channel 1"
default n
config BSP_USING_TIM2_PWM_CH2
bool "using TIM2 channel 2 as pwm"
bool "using TIM2 channel 2"
default n
config BSP_USING_TIM2_PWM_CH3
bool "using TIM2 channel 3 as pwm"
bool "using TIM2 channel 3"
config BSP_USING_TIM2_PWM_CH4
bool "using TIM2 channel 4 as pwm"
bool "using TIM2 channel 4"
endif
@@ -192,18 +192,18 @@ config BSP_USING_TIM
if BSP_USING_TIM3_PWM
config BSP_USING_TIM3_PWM_CH1
bool "using TIM3 channel 1 as pwm"
bool "using TIM3 channel 1"
default n
config BSP_USING_TIM3_PWM_CH2
bool "using TIM3 channel 2 as pwm"
bool "using TIM3 channel 2"
default n
config BSP_USING_TIM3_PWM_CH3
bool "using TIM3 channel 3 as pwm"
bool "using TIM3 channel 3"
config BSP_USING_TIM3_PWM_CH4
bool "using TIM3 channel 4 as pwm"
bool "using TIM3 channel 4"
endif
@@ -230,18 +230,18 @@ config BSP_USING_TIM
if BSP_USING_TIM4_PWM
config BSP_USING_TIM4_PWM_CH1
bool "using TIM4 channel 1 as pwm"
bool "using TIM4 channel 1"
default n
config BSP_USING_TIM4_PWM_CH2
bool "using TIM4 channel 2 as pwm"
bool "using TIM4 channel 2"
default n
config BSP_USING_TIM4_PWM_CH3
bool "using TIM4 channel 3 as pwm"
bool "using TIM4 channel 3"
config BSP_USING_TIM4_PWM_CH4
bool "using TIM4 channel 4 as pwm"
bool "using TIM4 channel 4"
endif
+144 -2
View File
@@ -200,7 +200,7 @@ void ch32f1_i2c_config(I2C_TypeDef *i2cx)
}
}
void ch32f1_hwtimer_clock_init(TIM_TypeDef *timx)
void ch32f1_tim_clock_init(TIM_TypeDef *timx)
{
if (timx == TIM1)
{
@@ -223,7 +223,7 @@ void ch32f1_hwtimer_clock_init(TIM_TypeDef *timx)
}
}
rt_uint32_t ch32f1_hwtimer_clock_get(TIM_TypeDef *timx)
rt_uint32_t ch32f1_tim_clock_get(TIM_TypeDef *timx)
{
RCC_ClocksTypeDef RCC_Clocks;
@@ -292,3 +292,145 @@ struct rt_hwtimer_info *ch32f1_hwtimer_info_config_get(TIM_TypeDef *timx)
return info;
}
void ch32f1_pwm_io_init(TIM_TypeDef *timx, rt_uint8_t channel)
{
GPIO_InitTypeDef GPIO_InitStructure;
if (timx == TIM1)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
if (channel == TIM_Channel_1)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_2)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_3)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_4)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
}
if (timx == TIM2)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
if (channel == TIM_Channel_1)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_2)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_3)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_4)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
}
if (timx == TIM3)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
if (channel == TIM_Channel_1)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_2)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
if (channel == TIM_Channel_3)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
if (channel == TIM_Channel_4)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
}
if (timx == TIM4)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
if (channel == TIM_Channel_1)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
if (channel == TIM_Channel_2)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
if (channel == TIM_Channel_3)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
if (channel == TIM_Channel_4)
{
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
}
}
+3 -3
View File
@@ -50,10 +50,10 @@ void ch32f1_spi_clock_and_io_init(SPI_TypeDef* spix);
rt_uint32_t ch32f1_spi_clock_get(SPI_TypeDef* spix);
void ch32f1_i2c_clock_and_io_init(I2C_TypeDef* i2cx);
void ch32f1_i2c_config(I2C_TypeDef* i2cx);
void ch32f1_hwtimer_clock_init(TIM_TypeDef *timx);
rt_uint32_t ch32f1_hwtimer_clock_get(TIM_TypeDef *timx);
void ch32f1_tim_clock_init(TIM_TypeDef *timx);
rt_uint32_t ch32f1_tim_clock_get(TIM_TypeDef *timx);
struct rt_hwtimer_info* ch32f1_hwtimer_info_config_get(TIM_TypeDef *timx);
void ch32f1_pwm_io_init(TIM_TypeDef *timx, rt_uint8_t channel);