【STM32H7S78-DK测评】期待的串口打印来了
串口在嵌入式工程师眼中的地位非常高,高到拿到开发板后搭建好开发环境后必先要实现之。作为一名嵌入式工程师的我,我也要在第一时间实现串口打印用来调试程序。
在之前的开发板上,调试器只负责调试而无串口,而现在的STM32H7S78上板载的ST-Link v3自带有虚拟串口功能,我们本次也是使用这个虚拟串口来实现调试信息的输出打印。
可以看到连接到了PD0引脚和PD1引脚。阅读datasheet后,可以看到ST-Link的VCP连接到了STM32H7RS的uart4外设上。于是,我们通过STM32CubeMX来配置并生成外设驱动代码。
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] UART MSP Initialization
- * This function configures the hardware resources used in this example
- * @param huart: UART handle pointer
- * @retval None
- */
- void HAL_UART_MspInit(UART_HandleTypeDef* huart)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- if(huart->Instance==UART4)
- {
- /* USER CODE BEGIN UART4_MspInit 0 */
- /* USER CODE END UART4_MspInit 0 */
- /** Initializes the peripherals clock
- */
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART234578;
- PeriphClkInit.Usart234578ClockSelection = RCC_USART234578CLKSOURCE_PCLK1;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- /* Peripheral clock enable */
- __HAL_RCC_UART4_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /**UART4 GPIO Configuration
- PD1 ------> UART4_TX
- PD0 ------> UART4_RX
- */
- GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- /* USER CODE BEGIN UART4_MspInit 1 */
- /* USER CODE END UART4_MspInit 1 */
- }
- }
- /**
- * @brief UART MSP De-Initialization
- * This function freeze the hardware resources used in this example
- * @param huart: UART handle pointer
- * @retval None
- */
- void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
- {
- if(huart->Instance==UART4)
- {
- /* USER CODE BEGIN UART4_MspDeInit 0 */
- /* USER CODE END UART4_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_UART4_CLK_DISABLE();
- /**UART4 GPIO Configuration
- PD1 ------> UART4_TX
- PD0 ------> UART4_RX
- */
- HAL_GPIO_DeInit(GPIOD, GPIO_PIN_1|GPIO_PIN_0);
- /* USER CODE BEGIN UART4_MspDeInit 1 */
- /* USER CODE END UART4_MspDeInit 1 */
- }
- }
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- static uint8_t led_state = 0;
- MPU_Config();
- /* USER CODE END 1 */
- /* Enable the CPU Cache */
- /* Enable I-Cache---------------------------------------------------------*/
- SCB_EnableICache();
- /* Enable D-Cache---------------------------------------------------------*/
- SCB_EnableDCache();
- /* MCU Configuration--------------------------------------------------------*/
- /* Update SystemCoreClock variable according to RCC registers values. */
- SystemCoreClockUpdate();
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_UART4_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1) {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- switch (led_state) {
- case 0: {
- HAL_GPIO_WritePin(GPIOO, GPIO_PIN_1, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOO, GPIO_PIN_5, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOM, GPIO_PIN_2, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOM, GPIO_PIN_3, GPIO_PIN_SET);
- led_state = 1;
- HAL_UART_Transmit(&huart4, hello_str, sizeof(hello_str) - 1, 1000);
- break;
- }
- case 1: {
- HAL_GPIO_WritePin(GPIOO, GPIO_PIN_1, GPIO_PIN_SET);
- led_state = 2;
- break;
- }
- case 2: {
- HAL_GPIO_WritePin(GPIOO, GPIO_PIN_5, GPIO_PIN_SET);
- led_state = 3;
- break;
- }
- case 3: {
- HAL_GPIO_WritePin(GPIOM, GPIO_PIN_2, GPIO_PIN_RESET);
- led_state = 4;
- break;
- }
- case 4: {
- HAL_GPIO_WritePin(GPIOM, GPIO_PIN_3, GPIO_PIN_RESET);
- led_state = 0;
- break;
- }
- }
- HAL_Delay(200);
- }
- /* USER CODE END 3 */
- }
之后,代码就没有什么难度了!调用一下HAL_Uart_Transmin()即可。效果如下所示:
至此,我们的基础环境实验环节就结束了。接下来,我们也就开始了更高阶的实验了。
|