STM32+UART serial port+DMA transceiver

Table of contents

1. Cubemax terminal configuration

1.1 Initial configuration

1.2 GPIO configuration

 1.3 UART configuration

1.3.1 Serial port basic configuration

1.3.2 DMA configuration

2. Keil code design

2.1 Initial configuration

2.2 DMA receiving initialization configuration

2.3 DMA send configuration

 2.4 Receive callback function setting

2.5 Callback function content code writing

2.5.1 Receive callback function

2.5.2 Send callback function

2.6 Callback function content code optimization


1. Cubemax terminal configuration

1.1 Initial configuration

First, perform basic configuration through STM32cubemax:

 cubemax basic configuration

1.2 GPIO configuration

Use a small LED light (active high) to observe the effect, and configure the GPIO of the PB0 pin, as shown in the figure below:

 1.3 UART configuration

1.3.1 Serial port basic configuration

1.3.2 DMA configuration

First configure the RX, as shown in the figure below:

 

Then configure TX (the default is fine), as shown in the figure below:

2. Keil code design

2.1 Initial configuration

First, set the sending and receiving array above the main function, as shown in the figure below:

uint8_t tx[] = "TX ok";
uint8_t Rx[2]; 

2.2 DMA receiving initialization configuration

 HAL_UART_Receive_DMA(&huart1,Rx,sizeof(Rx));

2.3 DMA send configuration

HAL_UART_Transmit_DMA(&huart1,tx,sizeof(tx));
HAL_Delay(1000);

 2.4 Receive callback function setting

First find the HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) function, as shown in the figure below:

Then copy the function above the main function as shown below:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(huart);
  /* NOTE: This function should not be modified, when the callback is needed,
           the HAL_UART_RxCpltCallback could be implemented in the user file
   */
}

2.5 Callback function content code writing

2.5.1 Receive callback function

//received data LED light level flip

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart1.Instance == USART1)
	{
		HAL_GPIO_TogglePin(LED_GPIO_Port,LED_Pin);
	}
}

2.5.2 Send callback function

Change the R in the receiving callback function to T to realize the sending callback function. As shown below:

 When data is sent, the callback function works, as shown in the figure below:


uint8_t tx2[] = "TX_IT ok";

//发送回调函数
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart1.Instance == USART1)
	{
		HAL_UART_Transmit_DMA(&huart1,tx,sizeof(tx2));
	}
}

2.6 Callback function content code optimization

When the delay is added to the callback function, the program will be stuck, so the NVIC needs to be configured, as shown in the figure below:

 

 

Guess you like

Origin blog.csdn.net/weixin_44597885/article/details/130897647