[Embedded Basics] Serial port interrupt communication VS serial port DMA communication

Table of contents

Table of contents

foreword

1. Serial communication

1. Communication method

 2. Communication rate

3. Three working modes of serial communication 

2. Serial port interrupt communication

1. Serial port interrupt characteristics

2. CubeMX configuration initialization serial interrupt related peripherals

3. Serial port interrupt program analysis

 4. Experimental results

3. Serial DMA communication

1. About DMA

2. CubeMX configures and initializes the peripherals of DMA serial communication

 3. DMA program analysis

4. Experimental effect display

4. References

V. Summary


foreword

Comparing the different methods of serial port communication, appreciate the difference between serial port interrupt communication and DMA serial port communication

1. Serial communication

1. Communication method

Synchronous serial: Data transmission is based on a data block (a group of characters). In a data block, there is no interval between characters. The sending and receiving parties rely on independent clock lines for signal synchronization. Suitable for bulk data transfer.

Asynchronous serial: Data transmission is based on a single character, the gap between characters is arbitrary, and the duration of each bit within a character is the same. The sending and receiving parties do not have a special clock signal, but rely on the character format and communication rate agreed in advance to complete the communication.

 2. Communication rate

· Baud rate calculation

Tx / Rx baud rate = fCK/(16* USARTDIV )

·  Here fCK is the clock for peripherals (PCLK1 is used for USART2, 3, 4, 5, PCLK2 is used for USART1) 
  USARTDIV is an unsigned fixed-point number. The value of these 12 bits is set in the USART_BRR register.

 Commonly used baud rates are: 9600, 19200, 38400, 57600 and 115200

·  The baud rate is 115200, which means that 115200 bits are transmitted per second, and the duration of each bit of data on the data line is Tbit = 1/115200 ≈ 8.68us.

3. Three working modes of serial communication 

polling method         The CPU constantly detects the status flag of the serial port to judge the situation of data sending and receiving. Features: The program design is simple, but the CPU cannot perform other tasks during the detection of the flag bit, and the CPU utilization rate is low.
interrupt mode         After enabling the interrupt, apply for an interrupt after receiving one byte of data or sending one byte, and complete the follow-up processing in the ISR. During data transmission and reception, the CPU can perform other tasks, and the CPU utilization rate is high.
DMA mode         Set relevant parameters during initialization, and after starting DMA transfer, the data transfer process does not require CPU intervention. After the transmission is completed, a DMA interrupt is generated, and the CPU performs subsequent processing. The transmission efficiency is the highest.

2. Serial port interrupt communication

1. Serial port interrupt characteristics

1. When sending data, put one byte of data into the data register DR; when receiving data, store the content of DR in the user storage area; 2. The
interrupt method does not need to wait for the data transmission process, only needs to send and receive each byte of data After the completion, the interrupt is triggered by the interrupt flag bit, and a new one-word data is placed in the interrupt service program or the received
one-byte data is read;
3. When the amount of transmitted data is large and the communication baud rate is high ( greater than 38400), if the interrupt method is used, the CPU will be interrupted every time a byte of data is sent and received, causing the CPU to be unable to process other affairs. Therefore, it is recommended to use the DMA method when the batch data is transmitted and the communication baud rate is high.

2. CubeMX configuration initialization serial interrupt related peripherals

 · RCC clock configuration

Select high-speed external crystal oscillator (HSE) as system clock input

Configure Clock Tree

· USART1 configuration enable

 · Serial port interrupt enable

 · Configuration process file export

3. Serial port interrupt program analysis

· USART1 initialization configuration function

 · Definition of related flag bits and sending and receiving string variables

 The main function realizes sending the target string to the host computer on the PC side

 · Serial port interrupt service function and callback function

 Serial port interrupt callback function, the serial port interrupt service functions we want to achieve are all written in the callback function

 · Detailed engineering code portal: USART_TI · Fantasy

 4. Experimental results

The burning plug-in used in this experiment is: FlyMcu.exe

The serial transceiver assistant used in this experiment is: XCOM V2.3

USART communication

3. Serial DMA communication

1. About DMA

· Basic concepts of direct memory access (DMA)

Used for high-speed data transfer between peripherals and memory and between memory and memory. The initialization and start of the DMA transfer process is completed by the CPU, and the transfer process is executed by the DMA controller without CPU participation, thereby saving CPU resources and improving utilization.

· Features of DMA controller

1. The STM32F411 microcontroller has two DMA controllers: DMA1 and DMA2, each controller has 8 data streams, and each data stream can be mapped to 8 channels (or requests) 2. Each DMA controller is used to
manage Memory access requests from one or more peripherals, and the priority of each DMA request is coordinated through the bus arbiter
3. The data stream (stream) is the data path used to connect the transfer source and transfer target, and each data stream can be configured For different transmission sources and transmission targets, these transmission sources and transmission targets are called channels (Channel)
4. It has a 16-byte FIFO. After the FIFO function is enabled, the source data is sent to the FIFO first, and then sent to the target address after reaching the trigger threshold of the FIFO

· DMA data transmission mode

normal mode         After the transfer ends (that is, the amount of data to be transferred reaches zero), no more DMA operations will be generated. If a new DMA transfer is started, the DMA transfer needs to be restarted while the DMA channel is closed.
cycle mode         Can be used to handle ring buffers and continuous data streams (e.g. ADC scan mode). When the cycle mode is activated, when each round of transmission ends, the amount of data to be transmitted will be automatically loaded with the set initial value, and continue to respond to DMA requests.

2. CubeMX configures and initializes the peripherals of DMA serial communication

· RCC clock configuration

 Select high-speed external crystal oscillator (HSE) as system clock input

 The clock tree is consistent with the above serial port interrupt clock configuration

· USART1 initial configuration

Follow the steps below to set USART1 to asynchronous communication, with BAUT rate of 115200, 1 stop bit and no parity bit

 enable interrupt

 · DMA Configuration

DMA four transfer directions:

  • Peripheral  To Memory
  • Memory to Peripheral  Memory To Peripheral
  • Memory to Memory  Memory To Memory
  • Peripheral To  Peripheral

DMA transfer direction, working mode and other related configurations

Src Memory represents the peripheral address register

Function: Set whether the peripheral address is unchanged or incremented when transferring data. If it is set to increment, then the address will add Data Width bytes in the next transmission,

Dst Memory represents the memory address register

Function: Set whether the memory address is incremented when transferring data. If it is set to increment, then the address will add Data Width bytes in the next transmission

We send the data from the memory to the peripheral through DMA, and continuously store the data into the sending data register (USARTx_TDR) of the fixed peripheral address serial port. So the address of the peripheral is not incremented.

The internal memory stores the data to be sent, so the address pointer must be incremented to ensure that the data is sent out in sequence

· Configuration project export 

 3. DMA program analysis

· DMA initialization configuration function

· DMA interrupt service function entry 

 

 The main function realizes sending the string to the PC host computer through DMA

array of characters to send

 Send string sentbuf via DMA

 · Detailed engineering code portal: DMA_USART · Fantasy

4. Experimental effect display

The burning plug-in used in this experiment is: FlyMcu.exe

The serial transceiver assistant used in this experiment is: XCOM V2.3

  

DMA serial port

4. References

[STM32] HAL library STM32CubeMX tutorial eleven --- DMA (serial port DMA sending and receiving)

V. Summary

        The serial port communication is realized through the serial port interrupt method and the DMA direct storage access method, which once again deepens the understanding of the basic knowledge of the serial port communication, and realizes the different serial port communication methods, and greatly improves the processing through the interrupt method. The computing efficiency of the chip will only take the processing interrupt function when the interrupt is triggered, and continue to execute the main program at other times; the DMA method is more efficient, it directly transfers data from the memory to the peripheral or back from the peripheral to the memory , this process does not require CPU participation at all, which greatly reduces the burden on the CPU and improves the processing speed of the CPU. use.

Guess you like

Origin blog.csdn.net/qq_52791446/article/details/127449558