[STM32 uses the HAL library to implement DMA mode serial port to send and receive data, logic simulator, and baud rate calculation]

Write the directory title here

  • 1. Introduction to DMA
    • 1. DMA four transmission paths
    • 2. Core parameters
    • 3. DMA channel resources in STM32
    • 4. DMA workflow
      • (1) Kernel workflow without DMA
    • (4) DMA transfer mode
    • (5) Arbiter and priority
    • (6) DMA interrupt
  • Create CubeMX project
    • main code
  • Effect demonstration! [Insert picture description here](https://img-blog.csdnimg.cn/82410eb5a84b4e3a92f52a9d0fd6cc76.png)
  • Logic simulator observes waveforms and calculates baud rate

Foreword
Environment configuration
1. Chip: STM32F103C8T6
2. STM32CubeMX software
3. IDE: MDK-Keil software
This blog covers:
1. Working principle of DMA
2. STM32CubeMX creates DMA routine
3. HAL library timer DMA function library

1. Introduction to 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.
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 data from peripheral A to be copied to peripheral B, you only need to provide a data path for the two peripherals and copy the data directly from A to B without going through CPU processing.
Summary: The role of DMA is to solve the problem of excessive consumption of CPU resources by transferring large amounts of data. With DMA, the CPU can focus more on more practical operations – calculation, control, etc.

1. Four transmission paths of DMA

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

2. Core parameters

  • 1 Source address of data
  • 2 Destination address of data transfer location
  • 3. How much data is transferred?
  • 4 Transmission mode for how many transmissions to make

3. DMA channel resources in STM32

For large-capacity STM32 chips, there are two DMA controllers, DMA1 has 7 channels and DMA2 has 5 channels.
Each channel can be configured with the addresses of some peripherals.

4. DMA workflow

(1) Kernel workflow without DMA

1. If there is no DMA, the CPU must use the kernel as a transfer station when transmitting data. For example, the data collected by the ADC must be transferred to SRAM.
Enter the kernel
The core coordinates with the bus matrix through DCode to obtain the data collected by the peripheral ADC stored in the AHB

Out of the kernel
The kernel then stores the data into the memory SRAM through DCode through bus matrix coordination.


###(2) Workflow with DMA (no kernel involvement required)
1. During DMA transmission, the peripheral device sends a request to the DMA controller, and the DMA controller receives the request and triggers DMA work.
2. The DMA controller obtains the data collected by the ADC from the AHB peripheral and stores it in the DMA channel
3. The DMA bus of the DMA controller coordinates with the bus matrix. AHB is used to store the data collected by the peripheral ADC into the SRAM through the DMA channel. The entire transmission process does not require the participation of the kernel at all, that is, the participation of the CPU is not required.

(4) DMA transfer mode

DMA transfer method
Method 1: DMA_Mode_Normal, normal mode,

When a DMA data transfer is completed, stop the DMA transfer, that is, only transfer once
?
Method 2: DMA_Mode_Circular, cyclic transmission mode

When the transmission ends, the hardware will automatically reload the transmission data amount register for the next round of data transmission. That is, multiple transmission mode

(5) Arbiter and priority

The role of the arbiter is to determine the priority of each DMA transfer

The arbiter initiates peripheral/memory access based on the priority of the channel request.

Software: The priority of each channel can be set in the DMA_CCRx register, with 4 levels:
highest priority
high priority
medium priority
low priority;
Hardware: If 2 requests have the same software priority, the lower numbered channel has higher priority than the higher numbered channel. For example: if the software priorities are the same, channel 2 has priority over channel 4.

(6) DMA interrupt

Each DMA channel can generate interrupts when the DMA transfer is halfway through, when the transfer is completed, and when there is a transfer error. For application flexibility, these interrupts are enabled by setting different bits in the register.

Create CubeMX project

Use external crystal oscillator
](https://img-blog.csdnimg.cn/6304a3d915f34d29b50ee029954d0918. png)\

NVIC configuration turns on UART1 interrupt

Open DMA channel

Clock tree configuration

Main code

//Change the flag bit in IRQH after the reception is completed
void USART1_IRQHandler(void)
{
uint32_t tmp_flag = 0;
uint32_t temp;
tmp_flag =__HAL_UART_GET_FLAG( & amp;huart1,UART_FLAG_IDLE); //Get the IDLE flag
if((tmp_flag != RESET))//idle flag is set
{
__HAL_UART_CLEAR_IDLEFLAG( & amp;huart1);//Clear the flag bit
//temp = huart1.Instance->SR; //Clear the status register SR. Reading the SR register can realize the function of clearing the SR register.
//temp = huart1.Instance->DR; //Read the data in the data register
//These two sentences are equivalent to the above sentence
HAL_UART_DMAStop( & amp;huart1); //
temp = __HAL_DMA_GET_COUNTER( & amp;hdma_usart1_rx); // Get the number of untransmitted data in DMA
//temp = hdma_usart1_rx.Instance->NDTR;//Read the NDTR register to obtain the number of untransmitted data in the DMA,
//This sentence is equivalent to the above sentence
rx_len = BUFFER_SIZE - temp; //Subtract the total count from the number of untransmitted data to get the number of data that has been received
recv_end_flag = 1; // Accept completion flag position 1
}
  HAL_UART_IRQHandler( & amp;huart1);

}
//Process data in the main loop, clear the array, and prepare to receive again
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration------------------------------------------------- ----------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
if(recv_end_flag == 1) //Receive completion flag
{

if(strcmp(rx_buffer, STOP_DATA)==0){
SendFlag=0;
HAL_UART_Transmit_DMA( & amp;huart1, "Stop-------", 10);
}
\t\t\t
//When the input command is the value in START_DATA, send a prompt and change the flag
else if(strcmp(rx_buffer, START_DATA)==0){
SendFlag=1;
HAL_UART_Transmit_DMA( & amp;huart1, "Start------", 10);
}
\t\t\t
//When the input command does not exist, send a prompt and change the flag
else {
SendFlag=0;
HAL_UART_Transmit_DMA( & amp;huart1, (uint8_t *)rx_buffer, strlen(rx_buffer));
}
//DMA_Usart_Send(rx_buffer, rx_len);
rx_len = 0; //Clear count
recv_end_flag = 0;//Clear the reception end flag bit
memset(rx_buffer,0,rx_len);
  }
if(SendFlag)
HAL_UART_Transmit_DMA( & amp;huart1, (uint8_t *) & amp;message, strlen(message));
HAL_Delay(1000);
HAL_UART_Receive_DMA( & amp;huart1,rx_buffer,BUFFER_SIZE);//Re-open DMA reception
}

Effect demonstration

Logic simulator observes waveforms and calculates baud rate





Conversion unit
87.097*(110^-6)
Calculate the time of one bit of data: one start bit, eight data bits, and one stop bit
87.097(1*10^-6)/10
1/0.0000087097

For a version that does not use DMA to transfer data, please go to this article of mine.
https://blog.csdn.net/Xkccsdn147/article/details/134031161

This article refers to blogger [Z Xiaoxuan]
https://blog.csdn.net/as480133937/article/details/104827639