Data communication method based on hardware double-buffer DMA
Through linked list management buffer and semaphore management, the problems of excessive resource occupation and low timeliness in traditional DMA double buffers during high data transmission are solved, dynamic adjustment of buffer size and parallel data processing are realized, and buffering is avoided Area overflow improves the system's resource utilization and data processing efficiency.
Patent Information
- Application Number
- CN202411023226.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-29
- Publication Date
- 2024-11-12
AI Technical Summary
Traditional DMA double buffers will cause excessive resource usage when transmitting high data volumes, low timeliness, large data processing delays, and buffer overflow problems when the transmission speed is faster than the processing speed.
The buffer is managed by linked lists, allowing the number of buffers to be unlimited. The size of each buffer can be freely defined, and the buffer size is dynamically adjusted by DMA through one-way ring linked list structure and semaphore management.
It improves the timeliness of data processing and resource utilization, reduces the time of single data processing, and avoids buffer overflow, realizing dynamic adjustment of DMA and parallel data processing.
Smart Images

Figure CN118939587A_ABST
Abstract
Description
Technical Field
[0001] The present invention is applied to the technical field of data communication processing, and particularly relates to a data communication method based on hardware double buffer DMA. Background Art
[0002] The tasks and communication data interfaces undertaken by the embedded system MCU mainly include GPIO, UART, SPI, I2C, PWM, ADC, and DAC; these interfaces and communication protocols are used for the MCU to communicate with external devices or sensors, thereby completing various tasks and data exchanges of the embedded system.
[0003] When the CPU is communicating, it needs to save the peripheral data to RAM first, and then further process the data, which will occupy CPU resources. DMA, literally meaning "direct memory access", directly reads and writes memory without CPU intervention. The CPU can process data while handing over the data reading work to DMA, which can greatly improve the communication efficiency of data.
[0004] Although the traditional DMA double buffering data can solve the problem of data loss, when the amount of data is large, it takes a long time to transfer data when the DMA is interrupted, which will occupy more processor resources, making it difficult to meet the data communication requirements of system resources shortage and numerous tasks. Double buffer DMA adds a memory buffer pointer on the basis of general DMA, which can automatically switch the memory pointer when a DMA transfer is completed. Its classic usage is as follows: Usually after starting a DMA transfer, DMA will write data to the first buffer M0. After a period of time, the data in buffer M0 is full, and MDA will switch the buffer and continue to transfer data to the next buffer M1. While M1 is transmitting data, an interrupt is triggered to tell the CPU to process the data in M0. While M0 is transmitting data, the CPU processes the data in M1, and the transmission is alternating in this way. For example, the Chinese patent with publication number CN109542811A discloses a data communication processing method. In the classic usage of the above DMA double buffer, it is assumed that the DMA transmission speed is Vt, the CPU data processing speed is Vp, and the total size of the buffer is M. It has the following three problems: 1. Every time the buffer M0 is full, an interrupt is generated and the CPU is notified to process the data. The data processing delay is ,For some applications with high latency requirements, the timeliness is too low.
[0005] 2. Every time the CPU is notified to process data, The data needs to be processed, and the single data processing time is , a large amount of data to be processed instantly will affect the CPU's execution of other tasks.
[0006] 3. When Vt>Vp, buffer overflow will occur, and the overflow time is: .
[0007] Therefore, it is necessary to provide a data communication method based on hardware double buffer DMA, which uses linked lists to manage buffers, has unlimited number of buffers, can freely define the size of each buffer, and can realize the function of dynamically adjusting the buffer size without pausing DMA. Summary of the invention
[0008] The technical problem to be solved by the present invention is to overcome the shortcomings of the prior art and provide a data communication method based on hardware double-buffered DMA, which uses a linked list to manage buffers, has no limit on the number of buffers, can freely define the size of each buffer, and realizes the function of dynamically adjusting the buffer size without pausing DMA.
[0009] The technical solution adopted by the present invention is: the present invention includes peripherals, a DMA controller, a buffer manager, and a central processing unit; the DMA controller includes a first register, a second register, and a DMA memory operation pointer; the buffer manager includes a buffer chain list, and the buffer chain list includes a full buffer semaphore and an empty buffer semaphore. The method includes the following steps: S1. Initialize the buffer list and semaphore, initialize the starting pointer of each buffer, the buffer size information, and initialize the full buffer semaphore to 0. Assuming that a total of N buffers are used, the empty buffer semaphore needs to be initialized to N; S2, initializing the DMA controller, setting the first register as the starting pointer of the first buffer, and setting the DMA memory operation pointer to M0; S3. Register the interrupt processing function and start the data processing thread. The thread will obtain a full buffer semaphore. When the acquisition is successful, the corresponding data will be processed. S4, obtaining an empty buffer semaphore, the empty buffer semaphore becomes N-1, starting DMA transmission, and the DMA controller starts reading data from the peripheral device into the buffer; S5. When the data read by the DMA controller is larger than half of the current buffer size, a 'DMA transfer half interrupt' is generated. The interrupt signal notifies the CPU to execute a 'DMA transfer half interrupt processing function'. In the interrupt processing function, an empty buffer semaphore is obtained. After the acquisition is successful, the first register and the second register are set to the starting address of the buffer. If the acquisition of the empty buffer semaphore fails, it indicates that the buffer overflows, and the corresponding error processing function is executed according to the specific situation. S6. When the data read by the DMA controller is larger than the current buffer size, the DMA controller will automatically load the values of the first register and the second register into the DMA memory operation pointer, and continue to start the next DMA transfer. At the same time, a 'DMA transfer completion interrupt' will be generated. The interrupt signal will notify the CPU to execute the 'DMA transfer completion interrupt processing function', in which a full buffer semaphore will be released; S7. Since the data processing thread has been initialized in steps S1-S6, the operation of releasing the full buffer semaphore in step S6 wakes up the thread and successfully obtains the semaphore. The thread will process the data in the corresponding buffer. The entire system is in a state of parallel operation of data processing and data communication. When the data processing is completed, an empty buffer semaphore will be released. S8. Execute steps S5-S7 in a loop until the communication is completed.
[0010] As can be seen from the above scheme, the present application uses a linked list to manage buffers. The addresses of each buffer can be discontinuous, and can even be different storage media. The number of buffers is not limited, and the size of each buffer can be freely defined. The linked list adopts a one-way circular linked list structure. In addition to the parameters of an ordinary linked list, the linked list also adds a status variable to indicate whether the buffer is in an empty state, a full state, or a filled state. It can easily insert an empty buffer after an empty buffer without affecting the operation of DMA, that is, it has the function of dynamically adjusting the buffer size without pausing DMA.
[0011] A preferred solution is that the peripherals include serial port devices, SPI, USB devices, and analog-to-digital converters.
[0012] A preferred solution is to automatically load the value of the memory address register into the DMA memory operation pointer each time a DMA transfer is completed.
[0013] A preferred solution is that the buffer linked list is used to manage all buffers, N buffers can be freely defined, and all buffers are linked together using the buffer linked list, the buffer linked list contains information such as the size and starting address of each buffer, and two semaphores, the full buffer semaphore is used to indicate how many buffers are currently full, and the empty buffer semaphore is used to indicate how many buffers are currently empty.
[0014] A preferred solution is that the central processing unit includes a CPU, which has three functions: one is to process the data moved from DMA to RAM; the second is to execute the 'DMA transfer halfway interrupt processing function' when the DMA controller generates a 'DMA transfer halfway interrupt'; and the third is to execute the 'DMA transfer completion interrupt processing function' when the DMA controller generates a 'DMA transfer completion interrupt'. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 It is a structural principle diagram of the present invention; Figure 2 is a process flow chart of the present invention; Figure 3 It is a buffer zone linked list structure diagram of the present invention. DETAILED DESCRIPTION
[0016] like Figures 1 to 3 As shown, in this embodiment, the present invention includes a peripheral 1, a DMA controller 2, a buffer manager 3, and a central processing unit 4; the DMA controller 2 includes a first register, a second register, and a DMA memory operation pointer, the buffer manager 3 includes a buffer linked list, the buffer linked list includes a full buffer semaphore and an empty buffer semaphore, and the method includes the following steps: S1. Initialize the buffer list and semaphore, initialize the starting pointer of each buffer, the buffer size information, and initialize the full buffer semaphore to 0. Assume that a total of N buffers are used and N is an integer greater than 2, so it is necessary to initialize the empty buffer semaphore to N; S2, initializing the DMA controller 2, setting the first register as the starting pointer of the first buffer zone, and setting the DMA memory operation pointer to M0; S3. Register the interrupt processing function and start the data processing thread. The thread will obtain a full buffer semaphore. When the acquisition is successful, the corresponding data will be processed. S4, obtaining an empty buffer semaphore, the empty buffer semaphore becomes N-1, and DMA transmission starts, and the DMA controller 2 starts to read data from the peripheral 1 into the buffer; S5. When the data read by the DMA controller 2 is greater than half of the current buffer size, a 'DMA transfer half interrupt' will be generated. The interrupt signal will notify the CPU to execute the 'DMA transfer half interrupt processing function'. In the interrupt processing function, an empty buffer semaphore will be obtained. After successful acquisition, the first register and the second register will be set to the starting address of the buffer. If the empty buffer semaphore fails to be obtained, it means that the buffer overflows, and the corresponding error processing function is executed according to the specific situation. S6. When the data read by the DMA controller 2 is larger than the current buffer size, the DMA controller 2 will automatically load the values of the first register and the second register into the DMA memory operation pointer, and continue to start the next DMA transfer. At the same time, a 'DMA transfer completion interrupt' will be generated. The interrupt signal will notify the CPU to execute the 'DMA transfer completion interrupt processing function', in which a full buffer semaphore will be released; S7. Since the data processing thread has been initialized in steps S1-S6, the operation of releasing the full buffer semaphore in step S6 wakes up the thread and successfully obtains the semaphore. The thread will process the data in the corresponding buffer. The entire system is in a state of parallel operation of data processing and data communication. When the data processing is completed, an empty buffer semaphore will be released. S8. Execute steps S5-S7 in a loop until the communication is completed.
[0017] like Figures 1 to 3 As shown, in this embodiment, the peripheral device 1 includes a serial port device, an SPI, a USB device, and an analog-to-digital converter.
[0018] like Figures 1 to 3 As shown, in this embodiment, the value of the memory address register is automatically loaded into the DMA memory operation pointer each time a DMA transfer is completed.
[0019] like Figures 1 to 3 As shown, in this embodiment, the buffer linked list is used to manage all buffers, N buffers can be freely defined, and all buffers are linked together by the buffer linked list, the buffer linked list contains information such as the size and starting address of each buffer, and two semaphores, the full buffer semaphore is used to indicate how many buffers are currently full, and the empty buffer semaphore is used to indicate how many buffers are currently empty.
[0020] like Figures 1 to 3 As shown, in this embodiment, the central processing unit 4 includes a CPU, which has three functions: one is to process the data transferred from DMA to RAM; the second is to execute the 'DMA transfer halfway interrupt processing function' when the DMA controller 2 generates a 'DMA transfer halfway interrupt'; the third is to execute the 'DMA transfer completion interrupt processing function' when the DMA controller 2 generates a 'DMA transfer completion interrupt'.
[0021] In this embodiment, compared with a data communication processing method with publication number CN109542811A, the present application has the following technical effects: 1. Use linked lists to manage buffers. The addresses of each buffer can be discontinuous or even different storage media. The number of buffers is not limited. The size of each buffer can be freely defined, and the buffer size can be dynamically adjusted without pausing DMA. 2. When the number of buffers is defined as N, the data processing delay is , which is superior to the usage described in the referenced documents; 3. Single data processing time is , which is superior to the usage described in the referenced documents; 4. Overflow time is , which is superior to the usage described in the referenced documents.
[0022] Although the embodiments of the present invention are described with practical solutions, they do not constitute limitations on the meaning of the present invention. For those skilled in the art, it is obvious to modify the implementation scheme and combine it with other solutions based on this description.
Claims
1. A data communication method based on hardware double buffer DMA, comprising a peripheral device (1), a DMA controller (2), a buffer manager (3), and a central processing unit (4); characterized in that: The DMA controller (2) comprises a first register, a second register and a DMA memory operation pointer, the buffer manager (3) comprises a buffer linked list, the buffer linked list comprises a full buffer semaphore and an empty buffer semaphore, and the method comprises the following steps: S1. Initialize the buffer list and semaphore, initialize the starting pointer of each buffer, the buffer size information, and initialize the full buffer semaphore to 0. Assuming that a total of N buffers are used (N is an integer greater than 2), the empty buffer semaphore needs to be initialized to N; S2, initializing the DMA controller (2), setting the first register as the start pointer of the first buffer, and setting the DMA memory operation pointer to M0; S3. Register the interrupt processing function and start the data processing thread. The thread will obtain a full buffer semaphore. When the acquisition is successful, the corresponding data will be processed. S4, obtaining an empty buffer semaphore, the empty buffer semaphore becomes N-1, DMA transmission starts, and the DMA controller (2) starts to read data from the peripheral device (1) into the buffer; S5. When the data read by the DMA controller (2) is greater than half of the current buffer size, a 'DMA transfer half-over interrupt' is generated. The interrupt signal notifies the CPU to execute a 'DMA transfer half-over interrupt processing function'. In the interrupt processing function, an empty buffer semaphore is obtained. After the acquisition is successful, the first register and the second register are set to the starting address of the buffer. If the acquisition of the empty buffer semaphore fails, it indicates that the buffer overflows, and the corresponding error processing function is executed according to the specific situation. S6. When the data read by the DMA controller (2) is larger than the current buffer size, the DMA controller (2) automatically loads the values of the first register and the second register into the DMA memory operation pointer, and continues to start the next DMA transfer. At the same time, a 'DMA transfer completion interrupt' is generated. The interrupt signal notifies the CPU to execute a 'DMA transfer completion interrupt processing function', in which a full buffer semaphore is released; S7. Since the data processing thread has been initialized in steps S1-S6, the operation of releasing the full buffer semaphore in step S6 wakes up the thread and successfully obtains the semaphore. The thread will process the data in the corresponding buffer. The entire system is in a state of parallel operation of data processing and data communication. When the data processing is completed, an empty buffer semaphore will be released. S8. Execute steps S5-S7 in a loop until the communication is completed.
2. The data communication method based on hardware double buffer DMA according to claim 1, characterized in that: The peripherals (1) include serial port devices, SPI, USB devices, and analog-to-digital converters.
3. The data communication method based on hardware double buffer DMA according to claim 1, characterized in that: The value of the memory address register is automatically loaded into the DMA memory operation pointer each time a DMA transfer is completed.
4. The data communication method based on hardware double buffer DMA according to claim 1, characterized in that: The buffer linked list is used to manage all buffers. N buffers can be freely defined and all buffers are linked together using the buffer linked list. The buffer linked list contains information such as the size and starting address of each buffer, and two semaphores. The full buffer semaphore is used to indicate how many buffers are currently full, and the empty buffer semaphore is used to indicate how many buffers are currently empty.
5. The data communication method based on hardware double buffer DMA according to claim 1, characterized in that: The central processing unit (4) includes a CPU, which has three functions: first, to process data transferred from DMA to RAM; second, to execute a 'DMA transfer halfway interrupt processing function' when the DMA controller (2) generates a 'DMA transfer halfway interrupt'; and third, to execute a 'DMA transfer completion interrupt processing function' when the DMA controller (2) generates a 'DMA transfer completion interrupt'.
Citation Information
Cited By
ADC uninterrupted sampling method based on DMA
CN119921771A
A DMA-based uninterrupted sampling method for ADC
CN119921771B
