我正在做 UART 回环测试,校验是否能在接收缓冲区收到发送出去的数据。使用 Nucleo-U575ZI 开发板,采用 USART2,波特率 57600。TX 引脚与 RX 引脚配置如下:
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
我采用中断方式接收,在中断服务函数中逐字节接收数据。程序进入接收回调前,会先进入错误回调函数,错误码为 4,之后才进入接收回调,但是缓冲区读出数值全是 0。
如果 TX、RX 引脚都配置为推挽复用,回环功能正常,缓冲区可以正确读到发送的数据。但采用上面这种配置时,接收缓冲区收不到有效数据。
|
|