Interrupt mode and DMA mode complete serial communication

Article directory

    • 1. Basic introduction to DMA
    • 2. Complete tasks by interrupting
        • 1. Use STM32CubeMX to create a new project
        • 2. Code writing
        • 3. Hardware implementation
    • 3. Complete the task using DMA method
        • 1. Use STM32CubeMX to create a new project
        • 2. Code writing
        • 3. Demonstration of running results
    • 4. Reference materials

1. Basic introduction to DMA

What is DMA (basic definition of DMA)
DMA, the full name is Direct Memory Access, which is direct memory access.
DMA transfer copies data from one address space to another, providing high-speed data transfer between peripherals and memory or between memory and memory.

We know that the CPU has many functions such as data transfer, calculation, and control program transfer. The core of system operation is the CPU.
The CPU is processing a large number of transactions all the time, but some things are not that important, such as data copying and data storage. If we take out this part of the CPU resources, let the CPU handle other complex computing transactions. , can it make better use of CPU resources?

Therefore: transferring data (especially transferring large amounts of data) does not require CPU involvement. For example, if you want the data of peripheral A to be copied to peripheral B, you only need to provide a data path for the two peripherals, and the data can be copied directly from A to B without going through the processing of the CPU.

DMA definition:
DMA is used to provide high-speed data transfer between peripherals and memory or between memory and memory. Data can be moved quickly through DMA without CPU intervention. This saves CPU resources for other operations.
DMA transmission method
The function of DMA is to realize the direct transmission of data, and eliminates the traditional data transmission that requires the participation of CPU registers. It mainly involves four situations of data transmission, but they are essentially the same, all from memory. A certain area of the memory is transferred to another area of the memory (the data register of the peripheral is essentially a storage unit of the memory). The data transmission in the four cases is as follows:

  • Peripheral to memory
  • memory to peripheral
  • memory to memory
  • Peripheral to memory

2. Complete the task by interrupting

The STM32 system continuously sends “hello windows!” to the host computer (win10); when the host computer sends the character “stop” to stm32, stm32 pauses sending “hello windows!”; after sending a character “start”, stm32 continues to send;

1. Create a new project using STM32CubeMX
  • Set up RCC
  • Set USART1, select asynchronous communication, default parameters and enable the serial port
  • Set the file name and path and create the project
2. Code writing
  • Programming ideas

Set the flag bit and use the strEqual function to determine whether the string received by the interrupt is start;
Set the initial string to start in the main function, so that the flag bit is 1 when no interrupt is triggered at the beginning, and the program can run normally;
After the interrupt is triggered, the string is set to the stop flag position 0, and STM32 pauses sending.

  • HAL_UART_Transmit_IT
    HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_int *data, uint16_t Size)
    /*huart: Which serial port to use for communication
    data: an address, which contains the data to be sent, usually an array
    Size: Number of data sent*/
    

Use this function to enable the send interrupt. When the send register is empty, the interrupt is triggered. The data to be sent is sent to the send register and sent. Turn off the interrupt after the transmission is completed. In this experiment, we can treat it as an ordinary sending function.

  • strEqual function

    uint8_t flag=0;
    void strEqual(char rcData[5],char rcData1[5]){<!-- -->
      for(uint8_t i=0;i<5;i + + ){<!-- -->
         if(rcData[i]!=rcData1[i])
      flag=0;
         else
          flag=1;
       }
    }
    
  • Add in main function

     char rcData[5] = "start";
       while (1)
    {<!-- -->
      //Enable receive interrupt
      HAL_UART_Receive_IT( & amp;huart1,(uint8_t*)rcData,5);
      strEqual(rcData,"start");
      if(flag==1)//Receive*
      {<!-- -->
      uint8_t hello[20]="hello windows\\
    ";
      HAL_UART_Transmit_IT( & amp;huart1,hello,20);
      HAL_Delay(500);
      }
      else if(flag==0)//Receive#
      {<!-- -->}
       }
    
3. Hardware implementation

Burning software: FLYMCU
Serial communication software: XCOM

  • When burning the program, you need to change the jumper connection method of the STM32 minimum system board: change the jumper connection method of BOOTO from 0–>1, select the program to be burned in FLYMCU, then click to start programming, and then press the minimum Press the reset button on the system board to complete programming. After the programming is completed, you need to change the connection method of the jumper cap back to 0.
  • Run result demonstration

3. Complete the task using DMA

1. Create a new project using STM32CubeMX
  • Set up RCC
  • Set USART1, select asynchronous communication, default parameters and enable the serial port
  • Add two channels
  • Create project
2. Code writing
  • HAL_UART_Receive_DMA
    HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart,uint8_t *pData,uint16_t Size);
    

Among them, the parameter huart is a pointer of type UART_HandleTypeDef, indicating the UART serial port to be configured; the parameter pData is a pointer of type uint8_t, indicating the buffer area for DMA receiving data, and the parameter Size is a variable of type uint16_t, indicating the length of data received by DMA.

  • HAL_UART_Transmit_DMA
    HAL_StatusTypeDef
    HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart,uint8_t *pData,uint16_t Size)
    

This function is to send a certain amount of data in DMA mode. The parameter huart is the address of the serial port handle, the parameter pData is the first address of the data to be sent, and the parameter Size is the number of data to be sent.

  • strEqual function

    uint8_t flag=0;
    void strEqual(char rcData[5],char rcData1[5]){<!-- -->
      for(uint8_t i=0;i<5;i + + ){<!-- -->
         if(rcData[i]!=rcData1[i])
      flag=0;
         else
          flag=1;
       }
    }
    
  • Add in main function

     char rcData[5] = "start";
    uint8_t hello[20]="hello world\\
    ";
    while (1)
    {<!-- -->
      /* USER CODE END WHILE */
       HAL_UART_Receive_DMA( & amp;huart1,(uint8_t*)rcData,5);
      strEqual(rcData,"start");
      if(flag==1)//Receive*
      {<!-- -->
      HAL_UART_Transmit_DMA( & amp;huart1,(uint8_t*)hello,20);
      HAL_Delay(500);
      }
      \t
      else if(flag==0)//Receive#
    }
    
3. Demonstration of running results

4. Reference materials

[STM32] DMA principle, super detailed explanation of steps, understand DMA in one article
Stm32 HAL library USART (send + receive) all uses DMA form