[STM32H7] 【STM32H7S78-DK测评】3、串口通信测试

[复制链接]
 楼主| sujingliang 发表于 2024-9-7 20:47 | 显示全部楼层 |阅读模式
<
本帖最后由 sujingliang 于 2024-9-7 21:05 编辑

目的
实现板载调试串口通讯,实现字符串打印输出“Hello 21ic, Hello STM32H7S78-DK!"

一、硬件准备
STLINK虚拟串口连接到PD0、PD1,对应UART4

stlink1.png
STM32CubeMX中对UART4配置
mx1.png

二、TouchGFX配置
增加一个Screen3,添加几个部件
touchgfx1.png
增加一个Ineraction
trigger:Button clicked
Choose clicked Soucre:buttonUartSend
Action:Call new virtual fuciton
Funciton Name: uartSend
实现按下buttonUartSend按键,触发uartSend函数。
GFX3_1.png

三、Uart4初始化及printf重定向
STM32CubeMX配置UART4后,生成如下代码:
  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url] UART4 Initialization Function
  3.   * @param None
  4.   * @retval None
  5.   */
  6. static void MX_UART4_Init(void)
  7. {

  8.   /* USER CODE BEGIN UART4_Init 0 */

  9.   /* USER CODE END UART4_Init 0 */

  10.   /* USER CODE BEGIN UART4_Init 1 */

  11.   /* USER CODE END UART4_Init 1 */
  12.   huart4.Instance = UART4;
  13.   huart4.Init.BaudRate = 115200;
  14.   huart4.Init.WordLength = UART_WORDLENGTH_8B;
  15.   huart4.Init.StopBits = UART_STOPBITS_1;
  16.   huart4.Init.Parity = UART_PARITY_NONE;
  17.   huart4.Init.Mode = UART_MODE_TX_RX;
  18.   huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  19.   huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  20.   huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  21.   huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  22.   huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  23.   if (HAL_UART_Init(&huart4) != HAL_OK)
  24.   {
  25.     Error_Handler();
  26.   }
  27.   if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  28.   {
  29.     Error_Handler();
  30.   }
  31.   if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  32.   {
  33.     Error_Handler();
  34.   }
  35.   if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
  36.   {
  37.     Error_Handler();
  38.   }
  39.   /* USER CODE BEGIN UART4_Init 2 */

  40.   /* USER CODE END UART4_Init 2 */

  41. }
添加printf重定向:
  1. #if defined(__ICCARM__)
  2. __ATTRIBUTES size_t __write(int, const unsigned char *, size_t);
  3. #endif /* __ICCARM__ */

  4. #if defined(__ICCARM__)
  5. /* New definition from EWARM V9, compatible with EWARM8 */
  6. int iar_fputc(int ch);
  7. #define PUTCHAR_PROTOTYPE int iar_fputc(int ch)
  8. #elif defined ( __CC_ARM ) || defined(__ARMCC_VERSION)
  9. /* ARM Compiler 5/6*/
  10. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  11. #elif defined(__GNUC__)
  12. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  13. #endif /* __ICCARM__ */

  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Retargets the C library printf function to the USART.
  3.   * @param  None
  4.   * @retval None
  5.   */
  6. PUTCHAR_PROTOTYPE
  7. {
  8.   /* Place your implementation of fputc here */
  9.   /* e.g. write a character to the USART3 and Loop until the end of transmission */
  10.   HAL_UART_Transmit(&huart4, (uint8_t *)&ch, 1, 0xFFFF);

  11.   return ch;
  12. }

  13. int fgetc(FILE *f)
  14. {
  15.         int ch;
  16.         /* 等待串口输入数据 */
  17.         while (__HAL_UART_GET_FLAG(&huart4, UART_FLAG_RXNE) == RESET);
  18.         HAL_UART_Receive(&huart4, (uint8_t *)&ch, 1, 0xFFFF);
  19.         return (ch);
  20. }

  21. #if defined(__ICCARM__)
  22. size_t __write(int file, unsigned char const *ptr, size_t len)
  23. {
  24.   size_t idx;
  25.   unsigned char const *pdata = ptr;

  26.   for (idx = 0; idx < len; idx++)
  27.   {
  28.     iar_fputc((int)*pdata);
  29.     pdata++;
  30.   }
  31.   return len;
  32. }
  33. #endif /* __ICCARM__ */


如果用gcc,需要确保makefile中包括syscalls.c。当然还要有
        $(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_uart.c
        $(Drivers_path)/STM32H7RSxx_HAL_Driver/Src/stm32h7rsxx_hal_uart_ex.c

四、MVP架构实现通过TouchGFX驱动串口发送信息

TouchGFX串口.png

1、Model.cpp中增加
  1. void Model::uart4SendTest()
  2. {
  3.         printf("Hello 21ic, Hello STM32H7S78-DK!\r\n");
  4. }


2、Screen3View中增加
  1. void Screen3View::uartSend()
  2. {
  3.         presenter->uart4SendTest();
  4. }

3、Screen3Presenter中增加
  1. void Screen3Presenter::uart4SendTest()
  2. {
  3.         model->uart4SendTest();
  4. }


五、效果

微信图片_20240907204139.jpg

TouchGFX串口1.png

按下“UartSend”,串口会打印出信息。

tutieshi_480x270_6s.gif
Amazingxixixi 发表于 2024-10-31 16:04 | 显示全部楼层
用Hall库的话做Printf就比较麻烦,学写了
suncat0504 发表于 2024-10-31 17:08 | 显示全部楼层
使用工具和库函数,开发方便多了
地瓜patch 发表于 2024-10-31 18:05 来自手机 | 显示全部楼层
这图形化工具简化设计很多工作量
铁血丹心LLLL 发表于 2024-12-29 00:23 | 显示全部楼层
使用 ST-LINK 虚拟串口连接到 UART4 的引脚:PD0 (TX) 和 PD1 (RX)。
Stahan 发表于 2024-12-29 18:12 来自手机 | 显示全部楼层
现在的开发方便多了
yangjiaxu 发表于 2024-12-31 11:41 | 显示全部楼层
这个显示很不错啊,H7的话感觉做显示确实可以
申小林一号 发表于 2024-12-31 15:25 | 显示全部楼层
感谢分享,学习一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

146

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部

84

主题

146

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部