淘先锋技术网

首页 1 2 3 4 5 6 7

原文地址

记一次RT-Thread的bug

异常复现

stm32f407
RT-Thread 4.05
通过drv_pwm_set函数设置pwm1的频率,实际值是设定值的2倍

通过排查发现函数tim_clock_get()中,只有TIM9 10 11才会* pclk2_doubler
查询函数pclkx_doubler_get()可知pclk2_doubler是APB2总线的倍频数

if (RCC_ClkInitStruct.APB2CLKDivider != RCC_HCLK_DIV1)
{
     *pclk2_doubler = 2;
}

在这里插入图片描述
而TIM1 和 TIM8也是挂载在APB2总线上,同时APB2上的定时器频率会2倍频
在这里插入图片描述
因此需修改tim_clock_get()源码为

附上tim_clock_get源码,220707目前此问题未修复

static rt_uint64_t tim_clock_get(TIM_HandleTypeDef *htim)
{
    rt_uint32_t pclk1_doubler, pclk2_doubler;
    rt_uint64_t tim_clock;

    pclkx_doubler_get(&pclk1_doubler, &pclk2_doubler);

#if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
    if (htim->Instance == TIM9 || htim->Instance == TIM10 || htim->Instance == TIM11)
#elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32H7)|| defined(SOC_SERIES_STM32F3)
    if (htim->Instance == TIM15 || htim->Instance == TIM16 || htim->Instance == TIM17)
#elif defined(SOC_SERIES_STM32MP1)
    if (htim->Instance == TIM4)
#elif defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)
    if (0)
#endif
    {
#if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0)
        tim_clock = (rt_uint32_t)(HAL_RCC_GetPCLK2Freq() * pclk2_doubler);
#endif
    }
    else
    {
        tim_clock = (rt_uint32_t)(HAL_RCC_GetPCLK1Freq() * pclk1_doubler);
    }

    return tim_clock;
}