UART_TwoBoards_ComDMA
在固件库 Projects\STM32F030R8-Nucleo\Examples\UART\UART_TwoBoards_ComDMA 里面的这一个例程:
这个例程是 UART 串口使用 DMA 的方式传输数据,在编译后下载到开发板后看到板载 LD2 快速闪烁,按下使用者按键 B1 后 LD2 熄灭,嗯,然后呢?
main 回圈进入后等待按下使用者按键 B1,此时 LD2 快闪,按键按下后 LD2 熄灭。
/* Configure User push-button in Interrupt mode */
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
/* Wait for User push-button press before starting the Communication.
In the meantime, LED2 is blinking */
while(UserButtonStatus == 0)
{
/* Toggle LED2*/
BSP_LED_Toggle(LED2);
HAL_Delay(100);
}
BSP_LED_Off(LED2);
接下来设定 USART,例程中使用了 USART1:
/*##-1- Configure the UART peripheral ######################################*/
/* Put the USART peripheral in the Asynchronous mode (UART Mode) */
/* UART configured as follows:
- Word Length = 8 Bits
- Stop Bit = One Stop bit
- Parity = None
- BaudRate = 9600 baud
- Hardware flow control disabled (RTS and CTS signals) */
UartHandle.Instance = USARTx;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if(HAL_UART_DeInit(&UartHandle) != HAL_OK)
{
Error_Handler();
}
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
Error_Handler();
}
串口使用 DMA 方式接收数据:
/*##-2- Program the Reception process #####################################*/
if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
Error_Handler();
}
串口使用 DMA 方式发送数据,并启动发送:
/*##-3- Start the transmission process #####################################*/
/* While the UART in reception process, user can transmit data through
"aTxBuffer" buffer */
if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
{
Error_Handler();
}
等待发送结束,并 RESET UartReady 状态:
/*##-4- Wait for the end of the transfer ###################################*/
while (UartReady != SET)
{
}
/* Reset transmission flag */
UartReady = RESET;
接下来判断串口是否有数据接收?
/*##-5- Wait for the end of the transfer ###################################*/
while (UartReady != SET)
{
}
/* Reset transmission flag */
UartReady = RESET;
在串口发送结束的回调函数会设置 UartReady 为 SET 状态:
/**
* @brief Tx Transfer completed callback
* @param UartHandle: UART handle.
* @note This example shows a simple way to report end of DMA Tx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: trasfer complete*/
UartReady = SET;
}
在串口接收完成的回调函数也会设置 UartReady 为 SET 状态:
/**
* @brief Rx Transfer completed callback
* @param UartHandle: UART handle
* @note This example shows a simple way to report end of DMA Rx transfer, and
* you can add your own implementation.
* @retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
/* Set transmission flag: trasfer complete*/
UartReady = SET;
}
因此假如没有接收到串口数据的话程序就会停在第5阶段,也就是 LD2 为熄灭状态。假如串口有接收到数据的话,接下来会去判断接收到的数据跟发送的数据是否相同?假如相同的话会点亮 LD2:
/*##-6- Compare the sent and received buffers ##############################*/
if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
{
Error_Handler();
}
/* Turn on LED2 if test passes then enter infinite loop */
BSP_LED_On(LED2);
/* Infinite loop */
while (1)
{
}
USART1_TX 在 PA9 管脚,USART1_RX 在 PA10 管脚,我将 2 支管脚连接在一起后重新运行程序,在按下使用者按键 B1 后 LD2 就亮起了。
我在 PA9 管脚接上一个 USB 转串口的转换板,在电脑上察看串口发送了那些数据:
|