Data forwarding method oriented to small memory platform and based on DMA and dynamic memory allocation
By adopting data forwarding methods based on DMA and dynamic memory allocation on the small memory MCU platform, dynamically switching buffers are solved, and the problem of low data forwarding efficiency on the small memory platform is achieved, and efficient data transmission and compatibility are achieved.
Patent Information
- Application Number
- CN202411644844.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-18
- Publication Date
- 2025-03-11
AI Technical Summary
When the small memory platform of the embedded platform MCU is forwarding, the DMA-based transmission method occupies a large memory, slow transmission speed, and is not efficient.
The data forwarding method based on DMA and dynamic memory allocation for small memory platforms is adopted. By opening the sending buffer and receiving buffer in the MCU of the data forwarding device, and periodically determining the satisfaction of the buffer, dynamically switching the use of the buffer, realizing the DMA transmission and reception of data.
It effectively reduces the memory usage of the small memory MCU platform when forwarding data between different devices, improves the speed and efficiency of data transmission, and realizes compatibility with different communication interfaces and protocols.
Smart Images

Figure CN119597466A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the technical field of data transmission, and in particular to a data forwarding method based on DMA and dynamic memory allocation for a small memory platform. Background Art
[0002] With the rapid development of electronic devices today, the number of embedded platforms is increasing, and there are inevitably differences in communication interfaces and communication protocols between different devices. In order to achieve fast data transmission within and between devices and improve CPU processing efficiency, data transmission based on DMA (Direct Memory Access) has become a good choice. DMA allows hardware devices of different speeds (peripheral to memory, memory to peripheral, memory to memory, peripheral to peripheral) to communicate without relying on CPU interrupt load, thereby saving CPU resources. DMA also has efficient operations that general CPUs do not have, which can improve system throughput.
[0003] However, embedded platform chips generally have the characteristic of small MCU memory. Relying solely on DMA-based data transmission will still occupy a large amount of memory, and the transmission speed is slow and the transmission efficiency is not high. Summary of the invention
[0004] In view of this, an embodiment of the present application proposes a data forwarding method based on DMA and dynamic memory allocation for a small memory platform, aiming to reduce the memory occupancy of a small memory MCU platform for data forwarding between different devices and improve the speed and efficiency of data transmission.
[0005] In order to achieve the above-mentioned purpose, an embodiment of the present application proposes a data forwarding method based on DMA and dynamic memory allocation for a small memory platform, which is applied to a data forwarding device developed based on a small memory MCU platform, and the method includes the following steps: setting the data transceiver mode of the data forwarding device to a DMA transceiver mode; wherein the data forwarding device is used to receive data sent by an upper-level device and forward the data to a lower-level device; opening a sending buffer and a receiving buffer in the MCU of the data forwarding device; receiving data sent by the upper-level device, periodically determining whether the receiving buffer is full, if the receiving buffer is full, switching the sending buffer to the receiving buffer, switching the receiving buffer to the sending buffer, and starting to forward data to the lower-level device; periodically determining whether the sending buffer has been sent and whether the receiving buffer is full; if the sending buffer has been sent and the receiving buffer is full, switching the sending buffer to the receiving buffer, switching the receiving buffer to the sending buffer, until all data sent by the upper-level device is received, and all received data is forwarded to the lower-level device.
[0006] In order to achieve the above-mentioned purpose, the embodiment of the present application also proposes a data forwarding system based on DMA and dynamic memory allocation for a small memory platform, the system comprising: an upper-level device, a data forwarding device and a lower-level device, the data forwarding device is composed of a mode setting module, a buffer management module, a transceiver execution module and a transceiver determination module, and the data forwarding device is developed based on a small memory MCU platform; an upper-level device, used to send data to the data forwarding device; a mode setting module, used to set the data transceiver mode of the data forwarding device to a DMA transceiver mode; a buffer management module, used to open a sending buffer and a receiving buffer in the MCU of the data forwarding device, and manage the sending buffer and the receiving buffer; a transceiver execution module, used to receive the data sent by the upper-level device after the receiving buffer has been opened data; a transceiver determination module, used to periodically determine whether the receiving buffer is full. When the receiving buffer is full, the buffer management module is notified to switch the sending buffer to the receiving buffer, and the receiving buffer is switched to the sending buffer, and the transceiver execution module is notified to start forwarding data to the next-level device; the transceiver determination module is also used to periodically determine whether the sending buffer has been sent and the receiving buffer is full after the transceiver execution module starts forwarding data to the next-level device. When the sending buffer has been sent and the receiving buffer is full, the buffer management module is notified to switch the sending buffer to the receiving buffer again, and the receiving buffer is switched to the sending buffer, until the data forwarding device receives all the data sent by the upper-level device and forwards all the received data to the next-level device.
[0007] In order to achieve the above objectives, an embodiment of the present application also proposes a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it can implement a data forwarding method based on DMA and dynamic memory allocation for a small memory platform as described above.
[0008] The embodiment of the present application proposes a data forwarding method based on DMA and dynamic memory allocation for a small memory platform. The data reception and forwarding are both implemented based on DMA, which effectively reduces the CPU occupancy in the data transmission and reception process and improves the chip processing efficiency. In addition to DMA technology, the present application also performs dynamic memory allocation, and opens up a sending buffer and a receiving buffer in the MCU of the data forwarding device. The size of the sending buffer and the receiving buffer can be freely adjusted dynamically as needed, thereby adjusting the memory occupancy, improving data forwarding efficiency, and also being able to achieve compatibility with different communication interfaces and communication protocols. When starting to receive data sent by the upper-level device, periodically determine whether the receiving buffer is full. If the receiving buffer is full, switch the sending buffer to the receiving buffer, switch the receiving buffer to the sending buffer, and start forwarding data to the next-level device. Then, it periodically determines whether the sending buffer has been sent out and whether the receiving buffer is full. If the sending buffer has been sent out and the receiving buffer is full, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer until all data sent by the upper-level device is received, and all received data is forwarded to the lower-level device to ensure that data can be sent and received at the same time, further improving the efficiency of data sending and receiving.
[0009] In some optional embodiments, after periodically determining whether the sending buffer has been sent out and whether the receiving buffer is full, the method further includes: if the sending buffer has been sent out but the receiving buffer is not full, suspending data forwarding to the next level device, waiting for the receiving buffer to be full, then switching the sending buffer to the receiving buffer, switching the receiving buffer to the sending buffer, and resuming data forwarding to the next level device. During the data sending and receiving process, forwarding can be suspended, and receiving requires filling the receiving buffer or receiving all the data to be received at one time. Such a design effectively ensures the correctness and integrity of the data.
[0010] In some optional embodiments, after periodically determining whether the sending buffer has been sent out and whether the receiving buffer is full, it also includes: if the receiving buffer is full but the sending buffer is not full, a new receiving buffer is opened, and the new receiving buffer receives the data sent by the upper-level device until the sending buffer is full, then the sending buffer is released, the receiving buffer that is filled first is switched to the sending buffer, and the data is forwarded to the next-level device. In the case of multiple receiving buffers, after the data stored in the sending buffer is forwarded, the sending buffer needs to be released, and the receiving buffer that is filled first is switched to the sending buffer, so as to reduce memory usage as much as possible.
[0011] In some optional embodiments, after the earliest full receiving buffer is switched to the sending buffer and the data is forwarded to the next level device, the method further includes: periodically determining whether there are idle receiving buffers whose existence time exceeds the preset idle time threshold, and if there are idle receiving buffers whose existence time exceeds the preset idle time threshold, releasing the idle receiving buffers whose existence time exceeds the preset idle time threshold. There can be multiple receiving buffers, and in the process of releasing and scheduling, there may be idle receiving buffers. At this time, it is necessary to periodically determine whether there are idle receiving buffers whose existence time exceeds the preset idle time threshold, and clean up these idle receiving buffers in time, so as to better reduce memory usage.
[0012] In some optional embodiments, if the sending buffer has been sent out and the receiving buffer is full, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer until all data sent by the upper-level device is received, and all received data is forwarded to the next-level device, including: if the sending buffer has been sent out and the receiving buffer is full, it is detected whether the number of existing receiving buffers is greater than 1; if the number of existing receiving buffers is greater than 1, the sending buffer is released, and the receiving buffer that is filled the earliest is switched to the sending buffer until all data sent by the upper-level device is received, and all received data is forwarded to the next-level device; if the number of existing receiving buffers is equal to 1, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer until all data sent by the upper-level device is received, and all received data is forwarded to the next-level device.
[0013] In some optional embodiments, the data forwarding device always ensures that there is at least one receiving buffer and one sending buffer in the entire process of data receiving and sending processing.
[0014] In some optional embodiments, a sending buffer and a receiving buffer are opened in the MCU of the data forwarding device, including: according to a preset memory size, a sending buffer and a receiving buffer are opened in the free memory in the MCU of the data forwarding device, and a communication protocol is set with the upper-level device and the lower-level device, and a frame header, frame tail and check byte are agreed upon with the upper-level device and the lower-level device; wherein the optional communication protocols include RS-232 protocol, RS-485 protocol, RS-42 protocol, UART protocol, SPI protocol, I2C protocol and Modbus protocol.
[0015] In some optional embodiments, when the data forwarding device receives data sent by the upper-level device, after receiving each frame of data, it verifies the correctness of the data based on the frame header, frame tail and check byte agreed with the upper-level device. If the verification is successful, it continues to receive the next frame of data. If the verification fails, it re-receives the current frame of data. When forwarding data to the lower-level device, after sending each frame of data, it waits for the verification result of the current frame of data reported by the lower-level device. If the verification is successful, it continues to send the next frame of data. If the verification fails, it re-sends the current frame of data. The lower-level device verifies the correctness of the data based on the frame header, frame tail and check byte agreed with the data forwarding device. The design of data verification can ensure the correctness of data transmission, thereby ensuring the normal operation of downstream applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The drawings herein are incorporated into the specification and constitute a part of the specification of the present application, showing embodiments consistent with the present application and, together with the specification, are used to explain the principles of the present application. Obviously, the drawings described below are only some embodiments of the present application, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0017] Figure 1 It is a flow chart of a data forwarding method based on DMA and dynamic memory allocation for a small memory platform provided in one embodiment of the present application;
[0018] Figure 2 It is a structural schematic diagram of a data forwarding system based on DMA and dynamic memory allocation for a small memory platform provided in another embodiment of the present application;
[0019] Figure 3 It is a structural diagram of a data forwarding device provided in another embodiment of the present application. DETAILED DESCRIPTION
[0020] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the embodiments of the present application will be described in detail below in conjunction with the accompanying drawings. However, it will be appreciated by those skilled in the art that in the present application, many technical details are proposed in order to enable the reader to better understand the present application. However, even without these technical details and various changes and modifications based on the following embodiments, the technical scheme claimed in the present application can also be implemented. The division of the following embodiments is for the convenience of description, and the specific implementation of the present application should not be construed as any limitation, and the various embodiments can be combined and referenced with each other under the premise of no contradiction.
[0021] An embodiment of the present application proposes a data forwarding method based on DMA and dynamic memory allocation for a small memory platform, which is applied to a data forwarding device developed based on a small memory MCU platform. The implementation details of the data forwarding method based on DMA and dynamic memory allocation for a small memory platform proposed in this embodiment are specifically described below. The following content is only the relevant implementation details provided for the convenience of understanding, and is not necessary for the implementation of this solution.
[0022] The specific process of the data forwarding method based on DMA and dynamic memory allocation for a small memory platform proposed in this embodiment can be as follows: Figure 1 As shown, including:
[0023] Step 11, setting the data transceiving mode of the data forwarding device to the DMA transceiving mode, wherein the data forwarding device is used to receive data sent by the upper-level device and forward the data to the lower-level device.
[0024] In the specific implementation, first of all, it is necessary to set the data transceiver mode of the data forwarding device to the DMA transceiver mode. The entire data transceiver is implemented based on DMA, which effectively reduces the CPU occupancy in the data transceiver process and improves the chip processing efficiency. The task of the data forwarding device is to receive the data sent by the upper-level device and forward the data to the lower-level device. After determining the upper-level device and the lower-level device, it is also necessary to determine the communication protocol and communication interface. The optional communication protocols include RS-232 protocol, RS-485 protocol, RS-42 protocol, UART protocol, SPI protocol, I2C protocol and Modbus protocol, etc. Correspondingly, the optional interface also includes the communication interface corresponding to the above communication protocols.
[0025] In one example, the data forwarding device also needs to agree on the frame header, frame trailer and check byte with the upper-level device and the lower-level device to facilitate data verification.
[0026] Step 12: Create a sending buffer and a receiving buffer in the MCU of the data forwarding device.
[0027] In the specific implementation, after completing the setting of the data transmission and reception mode, it is necessary to open a sending buffer and a receiving buffer in the free memory in the MCU of the data forwarding device according to the preset memory size. The preset memory size can be set by technical personnel in this field according to actual needs.
[0028] Step 13, receiving data sent by the upper-level device, periodically determining whether the receiving buffer is full, if the receiving buffer is full, switching the sending buffer to the receiving buffer, switching the receiving buffer to the sending buffer, and starting to forward data to the next-level device.
[0029] In the specific implementation, after the sending buffer and receiving buffer are opened, the data forwarding device can enter the data sending and receiving process, start to receive the data sent by the upper-level device, and periodically determine whether the receiving buffer is full. If the receiving buffer is full, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer, and the data is forwarded to the next-level device. When the data is first received, the sending buffer is idle. When the receiving buffer is full, the two can be flipped and switched. The idle sending buffer becomes the receiving buffer to continue to receive the data sent by the upper-level device, and the original receiving buffer becomes the sending buffer to start forwarding data to the next-level device.
[0030] It is worth noting that during the entire process of data transmission and reception, the data forwarding device always needs to ensure that there is at least one receiving buffer and one sending buffer, so as to ensure that data transmission and reception can be carried out simultaneously.
[0031] Step 14, periodically determine whether the sending buffer has been completely sent and whether the receiving buffer is full.
[0032] Step 15, if the sending buffer has been sent out and the receiving buffer is full, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer until all data sent by the upper-level device is received and all received data is forwarded to the lower-level device.
[0033] In a specific implementation, after the data forwarding device enters the data sending and receiving parallel stage, it needs to periodically determine whether the sending buffer has been sent out and whether the receiving buffer is full, thereby deriving four determination results.
[0034] The first determination result is that the sending buffer has not been fully sent and the receiving buffer has not been fully received, then the data forwarding device will not perform any operation and will continue to maintain the status quo of the sending buffer and the receiving buffer.
[0035] The second judgment result is that the sending buffer has been sent out and the receiving buffer is full. At this time, the two need to be flipped, that is, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer, and then data sending and receiving continue until all data sent by the upper-level device is received, and all received data is forwarded to the next-level device.
[0036] The third judgment result is that the sending buffer has been sent out, but the receiving buffer is not full. At this time, the data forwarding device needs to suspend forwarding data to the next level device, wait for the receiving buffer to be full, and then switch the sending buffer to the receiving buffer, and switch the receiving buffer to the sending buffer, and resume forwarding data to the next level device. In other words, during the data sending and receiving process, forwarding can be suspended, and receiving needs to fill the receiving buffer or receive all the data to be received at once. This design effectively ensures the correctness and integrity of the data.
[0037] The fourth judgment result is that the receiving buffer is full, but the sending buffer is not fully sent. The sending buffer cannot be flipped to the receiving buffer. At this time, the data forwarding device needs to open a new receiving buffer, and the new receiving buffer receives the data sent by the upper-level device until the sending buffer is fully sent. Then the sending buffer is released, the receiving buffer that was first full is switched to the sending buffer, and the data is continued to be forwarded to the next-level device. This situation makes the data forwarding device have multiple receiving buffers. In the case of multiple receiving buffers, the data forwarding device needs to release the sending buffer after the data stored in the sending buffer is forwarded, and switch the receiving buffer that was first full to the sending buffer, so as to minimize memory usage.
[0038] It is worth noting that the fourth judgment result makes the data forwarding device have multiple receiving buffers. Then, in the whole process of data transmission and reception, there may be multiple receiving buffers under the first judgment result. Therefore, the data forwarding device needs to detect whether the number of existing receiving buffers is greater than 1 when the sending buffer has been sent and the receiving buffer is full. If the number of existing receiving buffers is greater than 1, the sending buffer is released, and the receiving buffer that is filled first is switched to the sending buffer until all data sent by the upper-level device is received, and all received data is forwarded to the lower-level device. If the number of existing receiving buffers is equal to 1, the sending buffer is switched to the receiving buffer, and the receiving buffer is switched to the sending buffer until all data sent by the upper-level device is received, and all received data is forwarded to the lower-level device.
[0039] In addition, the data forwarding device also needs to periodically determine whether there are idle receiving buffers whose existence time exceeds the preset idle time threshold. If there are idle receiving buffers whose existence time exceeds the preset idle time threshold, the idle receiving buffers whose existence time exceeds the preset idle time threshold are released, wherein the preset idle time threshold can be set by a technician in this field according to actual needs. Since there can be multiple receiving buffers, there may be idle receiving buffers during the release and scheduling process. At this time, it is necessary to periodically determine whether there are idle receiving buffers whose existence time exceeds the preset idle time threshold, and clean up these idle receiving buffers in time, so as to better reduce memory usage.
[0040] In one example, when a data forwarding device receives data sent by an upper-level device, it needs to verify the correctness of the data based on the frame header, frame trailer, and check bytes agreed with the upper-level device after receiving each frame of data. If the verification passes, it continues to receive the next frame of data. If the verification fails, it re-receives the current frame of data. Correspondingly, when a data forwarding device forwards data to a lower-level device, it waits for the verification result of the current frame of data reported by the lower-level device after sending each frame of data. If the verification passes, it continues to send the next frame of data. If the verification fails, it re-sends the current frame of data, wherein the lower-level device verifies the correctness of the data based on the frame header, frame trailer, and check bytes agreed with the data forwarding device. The design of data verification can ensure the correctness of data transmission, thereby ensuring the normal operation of downstream applications.
[0041] In this embodiment, the reception and forwarding of data are both based on DMA, which effectively reduces the CPU occupancy in the data transmission and reception process and improves the chip processing efficiency. In addition to DMA technology, this application also performs dynamic memory allocation, and opens a sending buffer and a receiving buffer in the MCU of the data forwarding device. The size of the sending buffer and the receiving buffer can be freely adjusted dynamically as needed, thereby adjusting the memory occupancy, improving the data forwarding efficiency, and also being able to achieve compatibility with different communication interfaces and communication protocols. When starting to receive the data sent by the upper-level device, periodically determine whether the receiving buffer is full. If the receiving buffer is full, switch the sending buffer to the receiving buffer, switch the receiving buffer to the sending buffer, and start forwarding data to the next-level device. Then periodically determine whether the sending buffer is sent and whether the receiving buffer is full. If the sending buffer is sent and the receiving buffer is full, switch the sending buffer to the receiving buffer, and switch the receiving buffer to the sending buffer until all the data sent by the upper-level device is received, and all the received data are forwarded to the next-level device, thereby ensuring that data transmission and reception can be carried out simultaneously, further improving the data transmission and reception efficiency.
[0042] The step division of the above methods is only for clear description. When implemented, they can be combined into one step or some steps can be split and decomposed into multiple steps. As long as they include the same logical relationship, they are all within the scope of protection of this application; adding insignificant modifications to the algorithm or process or introducing insignificant designs without changing the core design of the algorithm and process are all within the scope of protection of this application.
[0043] Another embodiment of the present application proposes a data forwarding system based on DMA and dynamic memory allocation for a small memory platform. The details of the data forwarding system based on DMA and dynamic memory allocation for a small memory platform proposed in this embodiment are specifically described below. The following content is only the implementation details provided for the convenience of understanding and is not necessary for the implementation of this example. Figure 2 Schematic diagram of a data forwarding system based on DMA and dynamic memory allocation for a small memory platform proposed in this embodiment, including: an upper-level device 20, a data forwarding device 30 and a lower-level device 40. The structure of the data forwarding device 30 can be as follows Figure 3 As shown, the data forwarding device 30 is composed of a mode setting module 31, a buffer management module 32, a transceiver execution module 33 and a transceiver determination module 34, and the data forwarding device 30 is developed based on a small memory MCU platform.
[0044] The upper-level device 20 is used to send data to the data forwarding device 30 .
[0045] The mode setting module 31 is used to set the data transceiving mode of the data forwarding device 30 to the DMA transceiving mode.
[0046] The buffer management module 32 is used to create a sending buffer and a receiving buffer in the MCU of the data forwarding device 30, and manage the sending buffer and the receiving buffer.
[0047] The transceiver execution module 33 is used to receive data sent by the upper-level device 20 after the receiving buffer is opened.
[0048] The transceiver determination module 34 is used to periodically determine whether the receiving buffer is full. When the receiving buffer is full, it notifies the buffer management module 32 to switch the sending buffer to the receiving buffer, and switches the receiving buffer to the sending buffer, and at the same time notifies the transceiver execution module 33 to start forwarding data to the next level device 40.
[0049] The sending and receiving determination module 34 is also used to periodically determine whether the sending buffer has been sent and whether the receiving buffer is full after the sending and receiving execution module 33 starts forwarding data to the next-level device 40. When the sending buffer has been sent and the receiving buffer is full, it notifies the buffer management module 32 to switch the sending buffer to the receiving buffer and the receiving buffer to the sending buffer until the data forwarding device 30 receives all the data sent by the upper-level device and forwards all the received data to the next-level device 40.
[0050] It is not difficult to find that this embodiment is a system embodiment corresponding to the above-mentioned method embodiments, and this embodiment can be implemented in conjunction with the above-mentioned method embodiments. The relevant technical details and technical effects mentioned in the above-mentioned embodiments are still valid in this embodiment, and in order to reduce repetition, they are not repeated here. Accordingly, the relevant technical details mentioned in this embodiment can also be applied in the above-mentioned embodiments.
[0051] It is worth mentioning that all modules involved in this embodiment are logic modules. In practical applications, a logic unit can be a physical unit, a part of a physical unit, or a combination of multiple physical units. In addition, in order to highlight the innovative part of this application, this embodiment does not introduce units that are not closely related to solving the technical problems proposed by this application, but this does not mean that there are no other units in this embodiment.
[0052] Another embodiment of the present application relates to a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it can implement a data forwarding method based on DMA and dynamic memory allocation for a small memory platform as described in the above method embodiments.
[0053] That is, those skilled in the art can understand that all or part of the steps in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a program, and the program is stored in a storage medium, including a number of instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, ROM (Read-Only Memory), RAM (Random Access Memory), disk or optical disk and other media that can store program codes.
[0054] Those skilled in the art will appreciate that the above embodiments are specific embodiments for implementing the present application, and in actual applications, various changes may be made thereto in form and detail without departing from the spirit and scope of the present application.
Claims
1. A data forwarding method based on DMA and dynamic memory allocation for a small memory platform, applied to a data forwarding device developed based on a small memory MCU platform, characterized in that: The method comprises: Set the data transceiver mode of the data forwarding device to the DMA transceiver mode; wherein the data forwarding device is used to receive data sent by the upper-level device and forward the data to the lower-level device; A sending buffer and a receiving buffer are opened in the MCU of the data forwarding device; Receive data sent by the upper-level device, periodically determine whether the receiving buffer is full, and if the receiving buffer is full, switch the sending buffer to the receiving buffer, and switch the receiving buffer to the sending buffer, and start forwarding data to the next-level device; Periodically determine whether the send buffer has been sent and the receive buffer is full; If the send buffer has been sent out and the receive buffer is full, the send buffer is switched to the receive buffer, and the receive buffer is switched to the send buffer until all data sent by the upper-level device is received and all received data is forwarded to the lower-level device.
2. The method according to claim 1, characterized in that After periodically determining whether the sending buffer has been completely sent and whether the receiving buffer is full, the method further includes: If the send buffer has been sent out, but the receive buffer is not full, data forwarding to the next-level device is suspended. After the receive buffer is full, the send buffer is switched to the receive buffer, and the receive buffer is switched to the send buffer, and data forwarding to the next-level device is resumed.
3. The method according to claim 2, characterized in that After periodically determining whether the sending buffer has been completely sent and whether the receiving buffer is full, the method further includes: If the receive buffer is full but the send buffer is not full, a new receive buffer is opened to receive the data sent by the upper-level device. After the send buffer is full, the send buffer is released and the receive buffer that was filled first is switched to the send buffer, and data is forwarded to the next-level device.
4. The method according to claim 3, characterized in that After releasing the sending buffer, switching the earliest full receiving buffer to the sending buffer, and continuing to forward data to the next level device, the method further includes: It is periodically determined whether there is an idle receiving buffer whose existence time exceeds a preset idle time threshold. If there is an idle receiving buffer whose existence time exceeds the preset idle time threshold, the idle receiving buffer whose existence time exceeds the preset idle time threshold is released.
5. The method according to claim 3, characterized in that: If the send buffer has been used up and the receive buffer is full, the send buffer is switched to the receive buffer, and the receive buffer is switched to the send buffer, until all data sent by the upper-level device is received and all received data is forwarded to the lower-level device, including: If the send buffer has been sent out and the receive buffer is full, check whether the number of existing receive buffers is greater than 1; If the number of existing receive buffers is greater than 1, the send buffer is released and the earliest full receive buffer is switched to the send buffer until all data sent by the upper-level device is received and all received data is forwarded to the lower-level device; If the number of existing receive buffers is equal to 1, the send buffer is switched to the receive buffer, and the receive buffer is switched to the send buffer until all data sent by the upper-level device is received, and all received data is forwarded to the lower-level device.
6. The method according to any one of claims 1 to 5, characterized in that During the entire process of data transmission and reception, the data forwarding device always ensures that there is at least one receiving buffer and one sending buffer.
7. The method according to any one of claims 1 to 5, characterized in that A sending buffer and a receiving buffer are opened in the MCU of the data forwarding device, including: According to the preset memory size, a sending buffer and a receiving buffer are opened in the free memory of the MCU of the data forwarding device, and the communication protocol between the upper-level device and the lower-level device is set, and the frame header, frame tail and check byte are agreed with the upper-level device and the lower-level device; Among them, the optional communication protocols include RS-232 protocol, RS-485 protocol, RS-42 protocol, UART protocol, SPI protocol, I2C protocol and Modbus protocol.
8. The method according to claim 7, characterized in that When receiving data sent by the upper-level device, the data forwarding device verifies the correctness of each frame of data based on the frame header, frame tail and check byte agreed with the upper-level device. If the verification passes, it continues to receive the next frame of data. If the verification fails, it re-receives the current frame of data. When forwarding data to the next-level device, after sending each frame of data, wait for the verification result of the current frame of data reported by the next-level device. If the verification passes, continue to send the next frame of data. If the verification fails, resend the current frame of data. Among them, the next-level device verifies the correctness of the data based on the frame header, frame tail and check byte agreed with the data forwarding device.
9. A data forwarding system based on DMA and dynamic memory allocation for small memory platforms, characterized in that: include: The upper level equipment, data forwarding equipment and lower level equipment, the data forwarding equipment is composed of mode setting module, buffer management module, transceiver execution module and transceiver judgment module, and the data forwarding equipment is developed based on a small memory MCU platform; The upper-level device is used to send data to the data forwarding device; A mode setting module, used to set the data transceiver mode of the data forwarding device to a DMA transceiver mode; The buffer management module is used to open a sending buffer and a receiving buffer in the MCU of the data forwarding device and manage the sending buffer and the receiving buffer; The transceiver execution module is used to receive data sent by the upper-level device after the receiving buffer has been opened; The transceiver determination module is used to periodically determine whether the receiving buffer is full. If the receiving buffer is full, the buffer management module is notified to switch the sending buffer to the receiving buffer and the receiving buffer to the sending buffer, and the transceiver execution module is notified to start forwarding data to the next level device. The sending and receiving determination module is also used to periodically determine whether the sending buffer has been sent and whether the receiving buffer is full after the sending and receiving execution module starts forwarding data to the next-level device. When the sending buffer has been sent and the receiving buffer is full, the buffer management module is notified to switch the sending buffer to the receiving buffer, and the receiving buffer to the sending buffer, until the data forwarding device receives all the data sent by the upper-level device and forwards all the received data to the next-level device.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the data forwarding method based on DMA and dynamic memory allocation for a small memory platform as claimed in any one of claims 1 to 8 can be implemented.