[STM32WBA] 【NUCLEO-WBA55CG测评】串口极速狂飙4.5Mbps

[复制链接]
2104|4
 楼主| HonestQiao 发表于 2024-12-22 12:26 | 显示全部楼层 |阅读模式
NUCLEO-WBA55CG使用的是Arm® Cortex – M33​内核,运行频率最高可达100 MHz,能够支持高速串口通信到4.5Mbps,这次就体验一下串口极速狂飙。
一、测试代码
在官方的示例库中,提供了串口超级终端通信的代码:
816167678f198f2de.png
可以在此代码的基础上,进行修改来做测试。

代码中,定义了接收缓存大小为10,我们直接修改为256:
  1. /* Size of Reception buffer */
  2. #define RXBUFFERSIZE                      256
然后修改一下对应的提示信息定义:
  1. /* Buffer used for transmission */
  2. uint8_t aTxStartMessage[] = "\n\r ****UART-Hyperterminal communication based on IT ****\n\r Enter 256 characters using keyboard :\n\r";
  3. uint8_t aTxReplyMessage[] = "Receive:";
  4. uint8_t aTxEndMessage[] = "\n\r Example Finished\n\r";
上面添加了一行收到信息后,回传信息的头部aTxReplyMessage。

在原有代码中,loop循环之前,做了数据收发的测试,进过适当修改,仅保留发送开始信息的不分:
  1.   /* USER CODE BEGIN 2 */

  2.   /*##-1- Start the transmission process #####################################*/
  3.   /* While the UART in reception process, user can transmit data through
  4.      "aTxBuffer" buffer */
  5.   if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxStartMessage, TXSTARTMESSAGESIZE)!= HAL_OK)
  6.   {
  7.     /* Transfer error in transmission process */
  8.     Error_Handler();
  9.   }
  10. #if 0
  11.   /*##-2- Put UART peripheral in reception process ###########################*/
  12.   /* Any data received will be stored in "aRxBuffer" buffer : the number max of
  13.      data received is 10 */
  14.   if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  15.   {
  16.     /* Transfer error in reception process */
  17.     Error_Handler();
  18.   }

  19.   /*##-3- Wait for the end of the transfer ###################################*/
  20.   /*  Before starting a new communication transfer, you need to check the current
  21.       state of the peripheral; if it's busy you need to wait for the end of current
  22.       transfer before starting a new one.
  23.       For simplicity reasons, this example is just waiting till the end of the
  24.       transfer, but application may perform other tasks while transfer operation
  25.       is ongoing. */
  26.   while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  27.   {
  28.   }

  29.   /*##-4- Send the received Buffer ###########################################*/
  30.   if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK)
  31.   {
  32.     /* Transfer error in transmission process */
  33.     Error_Handler();
  34.   }

  35.   /*##-5- Wait for the end of the transfer ###################################*/
  36.   /*  Before starting a new communication transfer, you need to check the current
  37.       state of the peripheral; if it's busy you need to wait for the end of current
  38.       transfer before starting a new one.
  39.       For simplicity reasons, this example is just waiting till the end of the
  40.       transfer, but application may perform other tasks while transfer operation
  41.       is ongoing. */
  42.   while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  43.   {
  44.   }

  45.   /*##-6- Send the End Message ###############################################*/
  46.   if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxEndMessage, TXENDMESSAGESIZE)!= HAL_OK)
  47.   {
  48.     /* Transfer error in transmission process */
  49.     Error_Handler();
  50.   }

  51.   /*##-7- Wait for the end of the transfer ###################################*/
  52.   while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  53.   {
  54.   }

  55.   /* Turn on LD1 if test passes then enter infinite loop */
  56.   BSP_LED_On(LD1);
  57. #endif
  58.   /* USER CODE END 2 */


然后在loop循环中,接收数据,并回复收到的数据:
  1.   /* Infinite loop */
  2.   /* USER CODE BEGIN WHILE */
  3.   while (1)
  4.   {
  5.           BSP_LED_Off(LD1);

  6.           /*##-2- Put UART peripheral in reception process ###########################*/
  7.           /* Any data received will be stored in "aRxBuffer" buffer : the number max of
  8.              data received is 10 */
  9.           if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
  10.           {
  11.             /* Transfer error in reception process */
  12.             Error_Handler();
  13.           }

  14.           /*##-3- Wait for the end of the transfer ###################################*/
  15.           /*  Before starting a new communication transfer, you need to check the current
  16.               state of the peripheral; if it's busy you need to wait for the end of current
  17.               transfer before starting a new one.
  18.               For simplicity reasons, this example is just waiting till the end of the
  19.               transfer, but application may perform other tasks while transfer operation
  20.               is ongoing. */
  21.           while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  22.           {
  23.           }
  24.           BSP_LED_Toggle(LD1);

  25.           /*##-4- Send the received Head ###########################################*/
  26.           if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxReplyMessage, sizeof(aTxReplyMessage))!= HAL_OK)
  27.           {
  28.             /* Transfer error in transmission process */
  29.             Error_Handler();
  30.           }
  31.           BSP_LED_Toggle(LD1);

  32.           /*##-7- Wait for the end of the transfer ###################################*/
  33.           while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  34.           {
  35.           }
  36.           BSP_LED_Toggle(LD1);

  37.           /*##-4- Send the received Buffer ###########################################*/
  38.           if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK)
  39.           {
  40.             /* Transfer error in transmission process */
  41.             Error_Handler();
  42.           }
  43.           BSP_LED_Toggle(LD1);

  44.           /*##-5- Wait for the end of the transfer ###################################*/
  45.           /*  Before starting a new communication transfer, you need to check the current
  46.               state of the peripheral; if it's busy you need to wait for the end of current
  47.               transfer before starting a new one.
  48.               For simplicity reasons, this example is just waiting till the end of the
  49.               transfer, but application may perform other tasks while transfer operation
  50.               is ongoing. */
  51.           while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
  52.           {
  53.           }
  54.           BSP_LED_Toggle(LD1);

  55.           /* Turn on LD1 if test passes then enter infinite loop */
  56.           BSP_LED_On(LD1);
  57.     /* USER CODE END WHILE */

  58.     /* USER CODE BEGIN 3 */

  59.   }
  60.   /* USER CODE END 3 */

为了体验串口极速狂飙,还需要修改串口初始化的定义:
  1.   /* USER CODE END USART1_Init 1 */
  2.   huart1.Instance = USART1;
  3.   huart1.Init.BaudRate = 4500000;
  4.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  5.   huart1.Init.StopBits = UART_STOPBITS_1;
  6.   huart1.Init.Parity = UART_PARITY_ODD;
  7.   huart1.Init.Mode = UART_MODE_TX_RX;
  8.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  9.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  10.   huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  11.   huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  12.   huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
把BaudRate给直接拉满,设置到4500000。

修改完成后,编译一下,确保没有问题:
8445567679058658da.png

然后烧录到开发板:
64892676791a47c962.png

二、串口通信测试
因为需要测试高速通信,所以数据线一定要用上好的数据线。
627646767925d88f67.png

打开串口工具,配置好参数:
94757676792ba72b85.png

然后发送256字节数据过去测试一下:
33913676793eaf3a79.png
34700676793f7b042f.png

然后,打开自动发送设置,间隔时间设置为100ms:
366196767942d87bc5.png

让数据飕飕飕的发送:
2836167679455bc9f1.png

回传的数据也是在飕飕飕的接收:
249936767947220f6e.png

从测试结果来看,4.5Mbps串口通信的传输过程非常的稳定。

这次先简单的使用串口工具测试,后面有机会,再做具体协议的通讯测试,并检验高速通信情况下数据的准确性。
小明的同学 发表于 2024-12-23 14:08 | 显示全部楼层
什么情况,串口可以这么快?
V853 发表于 2024-12-24 18:11 | 显示全部楼层
4.5Mbps串口通信这对线和环境要求都有点高吧
Amazingxixixi 发表于 2024-12-27 16:12 | 显示全部楼层
过来学习学习
 楼主| HonestQiao 发表于 2025-1-15 08:35 | 显示全部楼层
V853 发表于 2024-12-24 18:11
4.5Mbps串口通信这对线和环境要求都有点高吧

那必须的,需要好线好口好工具
您需要登录后才可以回帖 登录 | 注册

本版积分规则

42

主题

115

帖子

2

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