• The last:【STM32Cube_07】 Send and receive data using USART (interrupt mode)

For a better reading experience, please go:Mculover666’s personal blog.

This article describes how to use STM32CubeMX to initialize the USART of STM32L431RCT6 and use DMA mode to send and receive data.

1. Preparation

Hardware preparation

  • Development board First needs to prepare a development board, here I prepared the development board of STM32L4 (BearPi) :

Software to prepare

  • Keil-mdk and the corresponding chip package need to be installed in order to compile and download the generated code;
  • Prepare a serial debugging assistant, the one I used hereSerial Port Utility;

Keil MDK and Serial Port Utility installation packages are available at the end of this article.

2. Generate MDK projects

Select the chip model

Open STM32CubeMX, open MCU selector:

Search and select the chip STM32L431RCT6:

Configuring the Clock Source

  • If you choose to use an external high speed clock (HSE), you need to configure RCC in the System Core;
  • If you use the default internal clock (HSI), you can skip this step.

Here I use the external clock:

Configure a serial port

Bear Pai developed onboard ST-link and virtual a serial port, the schematic diagram is as follows:

Here, I switch the switch to at-MCU mode to connect the PC serial port with USART1.

Next, configure USART1:

USART DMA configuration

Knowledge little card — DMA

DMA (Direct Memory Access) is a peripheral of STM32. It features:

Move data directly from memory to peripherals, from peripherals to memory, or from memory to memory without using CPU.

In need of a serial port to send a large amount of data, for example, the CPU needs only a DMA transfer request, then you can go to do other things, the DMA data will be transmitted to the serial port to send, after a DMA transfer will raise the interrupt, CPU if necessary, can be to deal with the interruption, so the CPU’s efficiency is greatly improved?

There are two DMA peripherals in STM32L431RCT6: DMA1 and DMA2. There are seven channels outside each DMA, each channel is independent. There are several key points when configuring DMA:

  • Where does the data come from?
  • Where does the data go?
  • How much data is there?

This is the end of the knowledge card. Do you know anything about the DMA configuration of STM32?

Next we configure DMA to carry the SRAM data directly to the serial peripherals for transmission:

Configuring the Clock Tree

The maximum main frequency of STM32L4 is 80M, so PLL is configured and HCLK = 80Mhz at last:

Build project Settings

Code generation Settings

Finally set to generate a separate initialization file:

The generated code

Click GENERATE CODE to GENERATE MDK-V5 project:

3. Write, compile and download user code in MDK

Define the sending data area

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t dat[] = "Hello, I am Mculover666.\n";
/* USER CODE END 0 */
Copy the code

Initiate a DMA transfer in the main function

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
  HAL_UART_Transmit_DMA(&huart1, (uint8_t*)dat, sizeof(dat));
  /* USER CODE END 2 */

  while (1) {}}Copy the code

The experimental phenomena

After compiling, downloading and running, the experimental phenomena are as follows:

4. Use DMA to receive serial port data

instructions

  • HAL library cannot send and receive data using DMA at the same time.
  • All the steps are the same as when sending. I only show the parts that need to be modified.

Modify serial DMA configuration

Example Add a serial port receive buffer

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t dat[] = "Hello, I am Mculover666.\n";
uint8_t recv_buf[13] = {0};		// Serial port receive buffer
/* USER CODE END 0 */
Copy the code

Modify main function

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
  HAL_UART_Transmit(&huart1, (uint8_t*)dat, sizeof(dat), 0xFFFF);
  HAL_UART_Receive_DMA(&huart1, recv_buf, 13);  // Enable DMA reception
  /* USER CODE END 2 */

  while (1) {}}Copy the code

Add serial port receive interrupt callback function

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 
{ 
	// Send the received data again
	HAL_UART_Transmit(&huart1,recv_buf,13.0xFFFF);
}
/* USER CODE END 4 */
Copy the code

The experimental phenomena

Now that we’ve learned how to configure USART to send and receive data using DMA mode, the next section discusses various ways to implement the printf() function.

For more exciting articles and resources, please pay attention to my wechat official number: “McUlover666”.