0 工具准备
STM32MP13xx参考手册
1 定时器内核时钟频率计算方法
1.1 定时器分组
STM32MP135的定时器按照时钟源不同分成了三组,如下:
APB1:
APB2:
APB6:
1.2 定时器内核时钟频率计算方法
APB1DIV是APB1的分频系数,APB2DIV、APB6DIV同理。TIMG1PRE是第1组定时器的预分频选择,TIMG2PRE、TIMG3PRE同理。相关寄存器如下:
第1组定时器预分频选择寄存器:
第2组定时器预分频选择寄存器:
第3组定时器预分频选择寄存器:
APB1分频系数寄存器:
APB2分频系数寄存器:
APB6分频系数寄存器:
2 定时器内核时钟频率计算操作
以下代码打印stm32MP135所有定时器的内核时钟频率等信息:
/*** @brief 获取定时器内核时钟频率** @param clock_source_freq 时钟源频率* @param bus_div 总线分频值* @param timer_pre 定时器预分频值* @return float 定时器内核时钟频率*/
float get_timer_kernel_clock_freq(u32 clock_source_freq, u32 bus_div, u32 timer_pre)
{float timer_kernel_clock_freq = 0.0f;switch (bus_div){case 0:timer_kernel_clock_freq = clock_source_freq;break;case 1:timer_kernel_clock_freq = clock_source_freq * 2;break;case 2:case 3:case 4:if (timer_pre == 0){timer_kernel_clock_freq = clock_source_freq * 2;}else if (timer_pre == 1){timer_kernel_clock_freq = clock_source_freq * 4;}default:break;}return timer_kernel_clock_freq;
}int clk(void)
{float timer_kernel_clock_freq;RCC_PeriphCLKInitTypeDef PeriphClkInit;HAL_RCCEx_GetPeriphCLKConfig(&PeriphClkInit);printf("> Group 1 timer [Clock source : APB1]\r\n");printf(" APB1 clock : %0.3f MHz\r\n", (float)HAL_RCC_GetPCLK1Freq() / 1000000.0f);printf(" APB1 clock divider : %lu\r\n", RCC->APB1DIVR & 0x7);printf(" Group 1 prescaler : %lu\r\n", RCC->TIMG1PRER & 0x1);timer_kernel_clock_freq = get_timer_kernel_clock_freq(HAL_RCC_GetPCLK1Freq(), RCC->APB1DIVR & 0x7, RCC->TIMG1PRER & 0x1) / 1000000.0f;printf(" TIM2 TIM3 TIM4 TIM5 TIM6 TIM7 kernel clock freq : %0.3f MHz\r\n", timer_kernel_clock_freq);printf("> Group 2 timer [Clock source : APB2]\r\n");printf(" APB2 clock : %0.3f MHz\r\n", (float)HAL_RCC_GetPCLK2Freq() / 1000000.0f);printf(" APB2 clock divider : %lu\r\n", RCC->APB2DIVR & 0x7);printf(" Group 3 prescaler : %lu\r\n", RCC->TIMG2PRER & 0x1);timer_kernel_clock_freq = get_timer_kernel_clock_freq(HAL_RCC_GetPCLK2Freq(), RCC->APB2DIVR & 0x7, RCC->TIMG2PRER & 0x1) / 1000000.0f;printf(" TIM1 TIM8 clock freq : %0.3f MHz\r\n", timer_kernel_clock_freq);printf("> Group 3 timer [Clock source : APB6]\r\n");printf(" APB6 clock : %0.3f MHz\r\n", (float)HAL_RCC_GetPCLK6Freq() / 1000000.0f);printf(" APB6 clock divider : %lu\r\n", RCC->APB6DIVR & 0x7);printf(" Group 3 prescaler : %lu\r\n", RCC->TIMG3PRER & 0x1);timer_kernel_clock_freq = get_timer_kernel_clock_freq(HAL_RCC_GetPCLK6Freq(), RCC->APB6DIVR & 0x7, RCC->TIMG3PRER & 0x1) / 1000000.0f;printf(" TIM12 TIM13 TIM14 TIM15 TIM16 TIM17 kernel clock freq : %0.3f MHz\r\n", timer_kernel_clock_freq);return 0;
}
打印结果如下: