Skip to content

Commit b571cd8

Browse files
authored
Merge pull request #1714 from xeonxu/fix_pwm
[STM32 BSP]Fix PWM channel calculate argorithm.
2 parents 68edd04 + 250de62 commit b571cd8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

bsp/stm32f4xx-HAL/drivers/drv_pwm.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,21 @@ static struct rt_pwm_ops drv_ops =
3838

3939
static rt_err_t drv_pwm_enable(TIM_HandleTypeDef * htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
4040
{
41-
rt_uint32_t channel = 0x04 * configuration->channel;
41+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
4242
if(!enable)
4343
{
4444
HAL_TIM_PWM_Stop(htim, channel);
4545
}
46-
HAL_TIM_PWM_Start(htim, channel);
46+
else
47+
{
48+
HAL_TIM_PWM_Start(htim, channel);
49+
}
4750
return RT_EOK;
4851
}
4952

5053
static rt_err_t drv_pwm_get(TIM_HandleTypeDef * htim, struct rt_pwm_configuration *configuration)
5154
{
52-
rt_uint32_t channel = 0x04 * configuration->channel;
55+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
5356
rt_uint32_t tim_clock;
5457
#if (RT_HSE_HCLK > 100000000UL)//100M
5558
if(htim->Instance == TIM1 && htim->Instance == TIM8)
@@ -81,7 +84,7 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef * htim, struct rt_pwm_configuratio
8184
{
8285
rt_uint32_t period, pulse;
8386
rt_uint32_t tim_clock, psc;
84-
rt_uint32_t channel = 0x04 * configuration->channel;
87+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
8588
#if (RT_HSE_HCLK > 100000000UL)//100M
8689
if(htim->Instance == TIM1 && htim->Instance == TIM8)
8790
{

0 commit comments

Comments
 (0)