NUCLEO-WBA55CG使用的是Arm® Cortex – M33内核,运行频率最高可达100 MHz,能够支持高速串口通信到4.5Mbps,这次就体验一下串口极速狂飙。
一、测试代码
在官方的示例库中,提供了串口超级终端通信的代码:
可以在此代码的基础上,进行修改来做测试。
代码中,定义了接收缓存大小为10,我们直接修改为256:
/* Size of Reception buffer */
#define RXBUFFERSIZE 256
然后修改一下对应的提示信息定义:
/* Buffer used for transmission */
uint8_t aTxStartMessage[] = "\n\r ****UART-Hyperterminal communication based on IT ****\n\r Enter 256 characters using keyboard :\n\r";
uint8_t aTxReplyMessage[] = "Receive:";
uint8_t aTxEndMessage[] = "\n\r Example Finished\n\r";
上面添加了一行收到信息后,回传信息的头部aTxReplyMessage。
在原有代码中,loop循环之前,做了数据收发的测试,进过适当修改,仅保留发送开始信息的不分:
/* USER CODE BEGIN 2 */
/*##-1- Start the transmission process #####################################*/
/* While the UART in reception process, user can transmit data through
"aTxBuffer" buffer */
if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxStartMessage, TXSTARTMESSAGESIZE)!= HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
#if 0
/*##-2- Put UART peripheral in reception process ###########################*/
/* Any data received will be stored in "aRxBuffer" buffer : the number max of
data received is 10 */
if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
/* Transfer error in reception process */
Error_Handler();
}
/*##-3- Wait for the end of the transfer ###################################*/
/* Before starting a new communication transfer, you need to check the current
state of the peripheral; if it's busy you need to wait for the end of current
transfer before starting a new one.
For simplicity reasons, this example is just waiting till the end of the
transfer, but application may perform other tasks while transfer operation
is ongoing. */
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
{
}
/*##-4- Send the received Buffer ###########################################*/
if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
/*##-5- Wait for the end of the transfer ###################################*/
/* Before starting a new communication transfer, you need to check the current
state of the peripheral; if it's busy you need to wait for the end of current
transfer before starting a new one.
For simplicity reasons, this example is just waiting till the end of the
transfer, but application may perform other tasks while transfer operation
is ongoing. */
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
{
}
/*##-6- Send the End Message ###############################################*/
if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxEndMessage, TXENDMESSAGESIZE)!= HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
/*##-7- Wait for the end of the transfer ###################################*/
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
{
}
/* Turn on LD1 if test passes then enter infinite loop */
BSP_LED_On(LD1);
#endif
/* USER CODE END 2 */
然后在loop循环中,接收数据,并回复收到的数据:
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
BSP_LED_Off(LD1);
/*##-2- Put UART peripheral in reception process ###########################*/
/* Any data received will be stored in "aRxBuffer" buffer : the number max of
data received is 10 */
if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
/* Transfer error in reception process */
Error_Handler();
}
/*##-3- Wait for the end of the transfer ###################################*/
/* Before starting a new communication transfer, you need to check the current
state of the peripheral; if it's busy you need to wait for the end of current
transfer before starting a new one.
For simplicity reasons, this example is just waiting till the end of the
transfer, but application may perform other tasks while transfer operation
is ongoing. */
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
{
}
BSP_LED_Toggle(LD1);
/*##-4- Send the received Head ###########################################*/
if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aTxReplyMessage, sizeof(aTxReplyMessage))!= HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
BSP_LED_Toggle(LD1);
/*##-7- Wait for the end of the transfer ###################################*/
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
{
}
BSP_LED_Toggle(LD1);
/*##-4- Send the received Buffer ###########################################*/
if(HAL_UART_Transmit_IT(&huart1, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK)
{
/* Transfer error in transmission process */
Error_Handler();
}
BSP_LED_Toggle(LD1);
/*##-5- Wait for the end of the transfer ###################################*/
/* Before starting a new communication transfer, you need to check the current
state of the peripheral; if it's busy you need to wait for the end of current
transfer before starting a new one.
For simplicity reasons, this example is just waiting till the end of the
transfer, but application may perform other tasks while transfer operation
is ongoing. */
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY)
{
}
BSP_LED_Toggle(LD1);
/* Turn on LD1 if test passes then enter infinite loop */
BSP_LED_On(LD1);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
为了体验串口极速狂飙,还需要修改串口初始化的定义:
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 4500000;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_ODD;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
把BaudRate给直接拉满,设置到4500000。
修改完成后,编译一下,确保没有问题:
然后烧录到开发板:
二、串口通信测试
因为需要测试高速通信,所以数据线一定要用上好的数据线。
打开串口工具,配置好参数:
然后发送256字节数据过去测试一下:
然后,打开自动发送设置,间隔时间设置为100ms:
让数据飕飕飕的发送:
回传的数据也是在飕飕飕的接收:
从测试结果来看,4.5Mbps串口通信的传输过程非常的稳定。
这次先简单的使用串口工具测试,后面有机会,再做具体协议的通讯测试,并检验高速通信情况下数据的准确性。
|