0 工具准备
STM32CubeMX v6.11.1
STM32CubeIDE v1.15
STM32CubeProgrammer v2.16.0
STM32MP13xx参考手册
STM32MP13勘误手册
STM32MP135AD数据手册
正点原子stm32MP135开发板
1 确认时钟源
本例使用的时钟源均由外部晶振提供,分别是24MHz的HSE、32.768KHz的LSE。原理图如下:
STM32MP135AD数据手册对HSE、LSE的描述如下:
LSE:
HSE:
2 使用STM32CubeMX生成RCC初始化代码
2.1 使能HSE、LSE
2.2 修改RCC时钟树
本例以HSE倍频后的PLL1P作为MPU的主频为999MHz,如果使用HSI的话可以设置到正好1GHz。为了保证时钟源的精度,这里牺牲1MHz频率选择HSE。
按照上图指示完成步骤1、2,然后在3框内输入999即可自动得出合适的配置。
其余的时钟我们暂时没用到,配置如下:
2.3 生成初始化代码
在方框内输入工程名,然后点击生成代码即可。
3 修改STM32CubeMX生成的RCC初始化代码
3.1 初始化前去初始化RCC
STM32CubeMX生成的RCC初始化代码并不能直接使用,参考官方初始化RCC操作,我们需要在初始化RCC前去初始化RCC,也就是在RCC初始化前加上HAL_RCC_DeInit()。最后还要为MPU各总线选择时钟源及分频系数。完整代码如下:
void SystemClock_Config(void)
{
#if !defined(USE_DDR)HAL_RCC_DeInit();RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStructure;/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = 16;RCC_OscInitStruct.HSIDivValue = RCC_HSI_DIV1;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLL12SOURCE_HSE;RCC_OscInitStruct.PLL.PLLM = 2;RCC_OscInitStruct.PLL.PLLN = 83;RCC_OscInitStruct.PLL.PLLP = 1;RCC_OscInitStruct.PLL.PLLQ = 2;RCC_OscInitStruct.PLL.PLLR = 2;RCC_OscInitStruct.PLL.PLLFRACV = 2048;RCC_OscInitStruct.PLL.PLLMODE = RCC_PLL_FRACTIONAL;RCC_OscInitStruct.PLL2.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL2.PLLSource = RCC_PLL12SOURCE_HSE;RCC_OscInitStruct.PLL2.PLLM = 2;RCC_OscInitStruct.PLL2.PLLN = 62;RCC_OscInitStruct.PLL2.PLLP = 3;RCC_OscInitStruct.PLL2.PLLQ = 2;RCC_OscInitStruct.PLL2.PLLR = 2;RCC_OscInitStruct.PLL2.PLLFRACV = 4096;RCC_OscInitStruct.PLL2.PLLMODE = RCC_PLL_FRACTIONAL;RCC_OscInitStruct.PLL3.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL3.PLLSource = RCC_PLL3SOURCE_HSE;RCC_OscInitStruct.PLL3.PLLM = 2;RCC_OscInitStruct.PLL3.PLLN = 50;RCC_OscInitStruct.PLL3.PLLP = 3;RCC_OscInitStruct.PLL3.PLLQ = 2;RCC_OscInitStruct.PLL3.PLLR = 2;RCC_OscInitStruct.PLL3.PLLRGE = RCC_PLL3IFRANGE_1;RCC_OscInitStruct.PLL3.PLLFRACV = 0;RCC_OscInitStruct.PLL3.PLLMODE = RCC_PLL_INTEGER;RCC_OscInitStruct.PLL4.PLLState = RCC_PLL_NONE;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/* Select PLLx as MPU, AXI and MCU clock sources */RCC_ClkInitStructure.ClockType = (RCC_CLOCKTYPE_MPU | RCC_CLOCKTYPE_ACLK |RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK4 |RCC_CLOCKTYPE_PCLK5 | RCC_CLOCKTYPE_PCLK1 |RCC_CLOCKTYPE_PCLK6 |RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_PCLK3);RCC_ClkInitStructure.MPUInit.MPU_Clock = RCC_MPUSOURCE_PLL1;RCC_ClkInitStructure.MPUInit.MPU_Div = RCC_MPU_DIV2;RCC_ClkInitStructure.AXISSInit.AXI_Clock = RCC_AXISSOURCE_PLL2;RCC_ClkInitStructure.AXISSInit.AXI_Div = RCC_AXI_DIV1;RCC_ClkInitStructure.MLAHBInit.MLAHB_Clock = RCC_MLAHBSSOURCE_PLL3;RCC_ClkInitStructure.MLAHBInit.MLAHB_Div = RCC_MLAHB_DIV1;RCC_ClkInitStructure.APB1_Div = RCC_APB1_DIV2;RCC_ClkInitStructure.APB2_Div = RCC_APB2_DIV2;RCC_ClkInitStructure.APB3_Div = RCC_APB3_DIV2;RCC_ClkInitStructure.APB4_Div = RCC_APB4_DIV2;RCC_ClkInitStructure.APB5_Div = RCC_APB5_DIV4;RCC_ClkInitStructure.APB6_Div = RCC_APB6_DIV2;if (HAL_RCC_ClockConfig(&RCC_ClkInitStructure) != HAL_OK){/* HAL RCC configuration error */Error_Handler();}
#endif
}
这里有个USE_DDR的宏定义,如果用户程序是在DDR中运行的,则不能再初始化时钟避免DDR异常。
3.2 添加一个LED闪烁观察是否初始化成功
RCC初始化如果异常则会进入Error_Handler函数,这是一个死循环:
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}
本例在RCC初始化后加上一个LED闪烁死循环,如果LED能够闪烁起来则说明RCC初始化成功。相关代码如下:
static void MX_GPIO_Init(void)
{GPIO_InitTypeDef GPIO_InitStruct = {0};/* USER CODE BEGIN MX_GPIO_Init_1 *//* USER CODE END MX_GPIO_Init_1 *//* GPIO Ports Clock Enable */__HAL_RCC_GPIOI_CLK_ENABLE();__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOH_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(GPIOI, GPIO_PIN_3, GPIO_PIN_RESET);/*Configure GPIO pin : PI3 */GPIO_InitStruct.Pin = GPIO_PIN_3;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);/* USER CODE BEGIN MX_GPIO_Init_2 *//* USER CODE END MX_GPIO_Init_2 */}
3.3 添加总线时钟频率打印语句查看总线时钟频率是否设置正确
相关代码如下:
imx_printf("CPU Name : stm32MP135DAE7\r\n");imx_printf("ACLK Freq : %lu.%lu MHz\r\n", HAL_RCC_GetACLKFreq() / 1000000, HAL_RCC_GetACLKFreq() % 1000000);imx_printf("HCLK5 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetHCLK5Freq() / 1000000, HAL_RCC_GetHCLK5Freq() % 1000000);imx_printf("HCLK6 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetHCLK6Freq() / 1000000, HAL_RCC_GetHCLK6Freq() % 1000000);imx_printf("PCLK4 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK4Freq() / 1000000, HAL_RCC_GetPCLK4Freq() % 1000000);imx_printf("PCLK5 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK5Freq() / 1000000, HAL_RCC_GetPCLK5Freq() % 1000000);imx_printf("MPUSS Freq : %lu.%lu MHz\r\n", HAL_RCC_GetMPUSSFreq() / 1000000, HAL_RCC_GetMPUSSFreq() % 1000000);imx_printf("MLAHB Freq : %lu.%lu MHz\r\n", HAL_RCC_GetMLAHBFreq() / 1000000, HAL_RCC_GetMLAHBFreq() % 1000000);imx_printf("HCLK Freq : %lu.%lu MHz\r\n", HAL_RCC_GetHCLK1Freq() / 1000000, HAL_RCC_GetHCLK1Freq() % 1000000);imx_printf("PCLK1 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK1Freq() / 1000000, HAL_RCC_GetPCLK1Freq() % 1000000);imx_printf("PCLK2 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK2Freq() / 1000000, HAL_RCC_GetPCLK2Freq() % 1000000);imx_printf("PCLK3 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK3Freq() / 1000000, HAL_RCC_GetPCLK3Freq() % 1000000);imx_printf("PCLK6 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK6Freq() / 1000000, HAL_RCC_GetPCLK6Freq() % 1000000);
主函数完整代码如下:
/*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_UART4_Init();/* USER CODE BEGIN 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){HAL_Delay(5000);HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_3);imx_printf("CPU Name : stm32MP135DAE7\r\n");imx_printf("ACLK Freq : %lu.%lu MHz\r\n", HAL_RCC_GetACLKFreq() / 1000000, HAL_RCC_GetACLKFreq() % 1000000);imx_printf("HCLK5 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetHCLK5Freq() / 1000000, HAL_RCC_GetHCLK5Freq() % 1000000);imx_printf("HCLK6 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetHCLK6Freq() / 1000000, HAL_RCC_GetHCLK6Freq() % 1000000);imx_printf("PCLK4 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK4Freq() / 1000000, HAL_RCC_GetPCLK4Freq() % 1000000);imx_printf("PCLK5 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK5Freq() / 1000000, HAL_RCC_GetPCLK5Freq() % 1000000);imx_printf("MPUSS Freq : %lu.%lu MHz\r\n", HAL_RCC_GetMPUSSFreq() / 1000000, HAL_RCC_GetMPUSSFreq() % 1000000);imx_printf("MLAHB Freq : %lu.%lu MHz\r\n", HAL_RCC_GetMLAHBFreq() / 1000000, HAL_RCC_GetMLAHBFreq() % 1000000);imx_printf("HCLK Freq : %lu.%lu MHz\r\n", HAL_RCC_GetHCLK1Freq() / 1000000, HAL_RCC_GetHCLK1Freq() % 1000000);imx_printf("PCLK1 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK1Freq() / 1000000, HAL_RCC_GetPCLK1Freq() % 1000000);imx_printf("PCLK2 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK2Freq() / 1000000, HAL_RCC_GetPCLK2Freq() % 1000000);imx_printf("PCLK3 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK3Freq() / 1000000, HAL_RCC_GetPCLK3Freq() % 1000000);imx_printf("PCLK6 Freq : %lu.%lu MHz\r\n", HAL_RCC_GetPCLK6Freq() / 1000000, HAL_RCC_GetPCLK6Freq() % 1000000);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}
4 烧录测试
这里有一个小技巧,如果我们的程序是在SYSRAM中运行的,不需要使用调试工具,直接将板子设置为USB/UART启动,然后将我们的用户程序直接烧写到SYSRAM中即可看到实验现象。下图就是本例使用的烧录配置:
只需要下载用户程序(记得加上stm32头标识)即可。
实验现象:
可以看到LED灯闪烁了起来,说明我们已经将主频设置成功,完成了RCC初始化。打印的总线时钟频率如下:
以上总线频率和我们在stm32CubeMX内设置的一样。详见以下图片: