Data transmission method and system based on SPI communication, medium and terminal
By carrying the data length to be transmitted in the packet header information in the SPI communication, the possible no-load problem in SPI communication between MCUs is solved, which improves transmission efficiency and avoids performance waste and transmission abnormalities.
Patent Information
- Application Number
- CN202510093931.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-21
- Publication Date
- 2025-05-06
AI Technical Summary
In SPI communication between multiple MCUs, it is impossible to predict how much data the peer needs to be transmitted, resulting in no-load during transmission, wasting transmission bandwidth and system performance.
By carrying the length information of the remaining data to be transmitted in the header information of the data packet, the data receiver can judge the transmission status in real time and control the length of each transmitted data to prevent buffer overflow or transmission congestion.
It effectively improves data transmission efficiency, reduces invalid data transmission, releases more MCU performance, and avoids buffer overflow or transmission congestion.
Smart Images

Figure CN119945989A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of SPI communication, and in particular to a data transmission method, system, medium and terminal based on SPI communication. Background Art
[0002] In the SPI (Serial Peripheral Interface) communication between multiple MCUs (Microcontroller Units), since it is impossible to predict how much data the other end needs to transmit, and the maximum length of a single transmission cannot exceed the size supported by the FIFO of the hardware register, both parties generally set the data transmission length based on the size supported by the FIFO of the hardware register, and perform data transmission at the driver layer with the pre-set data transmission length, that is, fixed-length data transmission is performed at the driver layer, which is convenient for both parties to perform data parsing, and then merge the data into a complete message for processing after parsing. However, in the scenario of small data transmission, it is very likely that there will be no load, that is, a bunch of empty data is transmitted, which wastes both transmission bandwidth and system performance. Summary of the invention
[0003] The embodiment of the present invention provides a data transmission method, system, medium and terminal based on SPI communication, which utilizes the header information of a data packet to carry the length information of the remaining data to be transmitted, so that a data receiving end can judge the transmission status in real time and control the length of each transmission data to prevent abnormalities such as buffer overflow or transmission congestion.
[0004] In order to solve the above technical problems, an embodiment of the present invention provides a data transmission method based on SPI communication, comprising:
[0005] In response to the data transmission signal, based on the data transmission signal, the data to be transmitted is acquired, and a first terminal and a second terminal for SPI communication are determined;
[0006] If there is no data being transmitted between the current first terminal and the current second terminal, the data to be transmitted is taken as the first data, and the first data is iteratively processed. During each iterative processing, a first data packet to be transmitted is generated based on the current first data and in accordance with a preset transmission length rule, and the first data packet to be transmitted is included in the first header information and the second data. Then, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is taken as the first data, until the current first header information is not greater than zero, and then the iteration is terminated;
[0007] In which, in each iteration, the current first header information is the difference between the length of the current first data and the current second data, and if the first terminal has third data, the current first data is composed of the current second data and the current third data; if the first terminal does not have third data, the current first data is used as the current second data.
[0008] In the implementation of the embodiment of the present invention, a data transmission signal is responded to, and based on the data transmission signal, data to be transmitted is obtained, and a first terminal and a second terminal for SPI communication are determined. Based only on the data transmission signal, the first terminal as the data transmission party and the second terminal as the data receiving party can be quickly determined. The transmission logic between the terminals is consistent, and there is no master-slave data transmission relationship. If there is no data being transmitted between the current first terminal and the current second terminal, the data to be transmitted is used as the first data, and the first data is iteratively processed. During each iterative processing, a first data packet to be transmitted including first header information and second data is generated based on the current first data in accordance with a preset transmission length rule. In each iteration, the current first header information is the difference in length between the current first data and the current second data. By The first header information is added to the header of the first data packet to be transmitted by the terminal, and the first header information is the difference between the length of the current first data and the length of the second data, that is, the first header information carries information about how much data is waiting to be transmitted by the first terminal, which can realize the dynamic change of the transmission data length, thereby effectively improving the data transmission efficiency, reducing invalid data transmission, and releasing more MCU performance. Then, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has the third data, the current third data is used as the first data until the current first header information is not greater than zero, then the iteration is terminated, and the transmission status is judged by the first header information. If the first header information is greater than zero, it means that the first terminal still has data to be transmitted, and the iteration should continue to transmit the remaining data, otherwise the transmission is completed. In addition, by setting the transmission length rule, the larger data to be transmitted is divided into smaller data packets, and the amount of data transmitted each time can be controlled to avoid buffer overflow or transmission congestion caused by excessive data volume. If a data packet fails to be transmitted, only the data packet needs to be retransmitted, rather than the entire data set, which helps to manage the buffer and reduce the impact of transmission errors.
[0009] As a preferred solution, the first data is iteratively processed, and each time the iterative processing is performed, a first data packet to be transmitted including first header information and second data is generated based on the current first data in accordance with a preset transmission length rule, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, until the current first header information is not greater than zero, and then the iteration is terminated, specifically:
[0010] According to a preset initial transmission length, based on the current first data, a first data packet to be transmitted is generated, which includes the first header information and the second data, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that when the second terminal receives the current first data packet to be transmitted, the current first header information is obtained based on the current first data packet to be transmitted, and whether the current first header information is greater than zero; wherein the length of the current second data is equal to the preset initial transmission length;
[0011] If so, it is determined that the first terminal has the third data, and the current third data is used as the first data, and then the first data is iteratively processed. In each iterative processing, the length of the data part of the current first data packet to be transmitted is set according to the preset transmission length rule, and then the current first data is filled into the data part of the current first data packet to be transmitted. When the data filling is completed or the data part of the current first data packet to be transmitted is filled, a first data packet to be transmitted containing the first header information and the second data is generated, and then the SPI communication channel between the first terminal and the second terminal is called to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has the third data, the current third data is used as the first data, until the current first header information is not greater than zero, and then the iteration is terminated;
[0012] If not, it is determined that the first terminal does not have the third data, and the data to be transmitted has been transmitted from the first terminal to the second terminal.
[0013] The preferred scheme for implementing the embodiment of the present invention is to generate a first data packet to be transmitted containing first header information and second data based on the current first data according to the preset initial transmission length, wherein the length of the current second data is equal to the preset initial transmission length, and then transmit the current first data packet to be transmitted from the first terminal to the second terminal. By the above means, using the preset initial transmission length, the transmission process can be quickly started, the handshake and setting time can be reduced, the efficiency of the initial transmission can be improved, and a smaller length can be set for data transmission according to actual needs to minimize the probability of the first data packet to be transmitted being empty during the first transmission. In addition, by calling the SPI communication channel between the first terminal and the second terminal for transmission, the high-speed characteristics and low pin count advantages of the SPI interface can be fully utilized, which is suitable for device communication in embedded systems.
[0014] As a preferred solution, the length of the data portion of the current first data packet to be transmitted is set according to a preset transmission length rule, and then the current first data is filled into the data portion of the current first data packet to be transmitted, and when the data filling is completed or the data portion of the current first data packet to be transmitted is filled, a first data packet to be transmitted containing the first header information and the second data is generated, specifically:
[0015] Using the current first data as the first header information corresponding to the transmission period at the current moment;
[0016] Obtaining first header information and second header information corresponding to the previous transmission cycle, and comparing the first header information corresponding to the previous transmission cycle and the second header information corresponding to the previous transmission cycle with the maximum depth of the FIFO; wherein, in each transmission cycle, when the first terminal receives the second data packet to be transmitted transmitted by the second terminal, obtaining the length of the remaining data to be transmitted of the second terminal based on the current second data packet to be transmitted as the second header information corresponding to the current transmission cycle;
[0017] If the first header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO or the second header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO, the length of the data part of the current first data packet to be transmitted is set equal to the maximum depth of the FIFO, and then the current first data is filled into the data part of the current first data packet to be transmitted, and when the data part of the current first data packet to be transmitted is filled, the data in the current first data that is not filled into the data part of the current first data packet to be transmitted is used as the third data, and then the length of the current third data is used as the first header information of the current first data packet to be transmitted, so as to generate a first data packet to be transmitted containing the first header information and the second data.
[0018] A preferred scheme for implementing the embodiment of the present invention takes into account the maximum depth of the FIFO. If the first header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO or the second header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO, the length of the data portion of the current first data packet to be transmitted is set equal to the maximum depth of the FIFO, and then the current first data is filled into the data portion of the current first data packet to be transmitted. This can effectively control the amount of data transmitted in a single time, ensure that the data transmitted each time does not exceed the capacity of the FIFO, prevent the data packet from being too large and causing the receiving end buffer to overflow, thereby avoiding data loss.
[0019] As a preferred solution, after comparing the first header information corresponding to the previous transmission cycle and the second header information corresponding to the previous transmission cycle with the maximum depth of the FIFO, the method further includes:
[0020] If the first header information corresponding to the previous transmission cycle is greater than the second header information corresponding to the previous transmission cycle and the first header information corresponding to the previous transmission cycle is less than the maximum depth of the FIFO, the length of the data portion of the current first data packet to be transmitted is set equal to the first header information corresponding to the previous transmission cycle, and then the current first data is filled into the data portion of the current first data packet to be transmitted, and when the data filling is completed or the data portion of the current first data packet to be transmitted is filled, the first header information of the current first data packet to be transmitted is set to zero to generate a first data packet to be transmitted containing the first header information and the second data;
[0021] If the second header information corresponding to the previous transmission cycle is greater than the first header information corresponding to the previous transmission cycle and the second header information corresponding to the previous transmission cycle is less than the maximum depth of the FIFO, the length of the data part of the current first data packet to be transmitted is set equal to the second header information corresponding to the previous transmission cycle, and then the current first data is filled into the data part of the current first data packet to be transmitted, and at the end of the data filling, the first header information of the current first data packet to be transmitted is set to zero to generate a first data packet to be transmitted containing the first header information and the second data.
[0022] A preferred scheme for implementing the embodiment of the present invention dynamically adjusts the length of the data portion of the current first data packet to be transmitted by comparing the first header information corresponding to the previous transmission cycle, the second header information corresponding to the previous transmission cycle and the maximum depth of the FIFO to ensure that the size of each first data packet to be transmitted matches the actual transmission requirements of both ends.
[0023] As a preferred solution, the data transmission method based on SPI communication further includes:
[0024] If data is being transmitted between the current first terminal and the current second terminal, before transmitting the current first data packet to be transmitted from the first terminal to the second terminal, updating the current first data based on the data to be transmitted;
[0025] Based on the length of the updated first data, the first header information in the current first data packet to be transmitted is updated, and then the current first data packet to be transmitted is iteratively processed. In each iterative processing, the SPI communication channel between the first terminal and the second terminal is called to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated. Until the current first header information is not greater than zero, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends.
[0026] In the preferred solution of the embodiment of the present invention, if data is being transmitted between the current first terminal and the current second terminal, the current first data is dynamically updated based on the data to be transmitted corresponding to the data transmission signal, and the first header information in the current first data packet to be transmitted is dynamically updated according to the length of the updated first data, so that the remaining amount of data to be transmitted from the first terminal to the second terminal can be accurately fed back to the second terminal, which helps the second terminal as the data receiving end to more accurately judge the real-time transmission status. In addition, if data is being transmitted between the first terminal and the second terminal, the current first data can still be updated based on the data to be transmitted, without waiting for the current transmission to be completed before processing the new data. This parallel processing capability can significantly improve data transmission efficiency and reduce waiting time.
[0027] As a preferred solution, based on the length of the updated first data, the first header information in the current first data packet to be transmitted is updated, and then the current first data packet to be transmitted is iteratively processed. During each iterative processing, the SPI communication channel between the first terminal and the second terminal is called to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated until the current first header information is not greater than zero, then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends, specifically:
[0028] Based on the updated length of the first data, the first header information in the current first data packet to be transmitted is updated;
[0029] The SPI communication channel between the first terminal and the second terminal is called, and the first data packet to be transmitted with the header information updated is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information and the current second data when receiving the current first data packet to be transmitted, and then iteratively processes the current first data. In each iterative processing, according to a preset transmission length rule, based on the current first data, a first data packet to be transmitted including the first header information and the second data is generated, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, until the current first header information is not greater than zero, and then the iteration is terminated.
[0030] A preferred solution for implementing an embodiment of the present invention is to update the first header information in the current first data packet to be transmitted based on the length of the updated first data, and then call the SPI communication channel between the first terminal and the second terminal to transmit the first data packet to be transmitted with the updated header information from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted, and then iteratively process the current first data, generate a new data packet according to the current first data in each iterative process, and transmit it through the SPI communication channel, so that the data transmission process is more flexible and can adapt to different data volumes and transmission requirements.
[0031] In order to solve the same technical problem, an embodiment of the present invention further provides a data transmission system based on SPI communication, comprising:
[0032] A data acquisition module, used for responding to a data transmission signal, acquiring data to be transmitted based on the data transmission signal, and determining a first terminal and a second terminal for SPI communication;
[0033] A data transmission module, used for, if there is no data being transmitted between the current first terminal and the current second terminal, taking the data to be transmitted as the first data, and iteratively processing the first data, generating a first data packet to be transmitted including first header information and second data based on the current first data in accordance with a preset transmission length rule in each iterative processing, and then transmitting the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted, and if the first terminal has third data at this time, taking the current third data as the first data, until the current first header information is not greater than zero, then the iteration is terminated; wherein, in each iteration, the current first header information is the difference between the length of the current first data and the length of the current second data, and if the first terminal has third data, the current first data is composed of the current second data and the current third data, and if the first terminal does not have third data, the current first data is taken as the current second data.
[0034] As a preferred solution, the data transmission system based on SPI communication further includes:
[0035] The second transmission module is used for updating the current first data packet to be transmitted based on the data to be transmitted before transmitting the current first data packet to be transmitted from the first terminal to the second terminal if data is being transmitted between the current first terminal and the current second terminal; updating the first header information in the current first data packet to be transmitted based on the length of the updated first data, and then iteratively processing the current first data packet to be transmitted, calling the SPI communication channel between the first terminal and the second terminal in each iterative processing, transmitting the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted, and if the first terminal has third data at this time, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated to be transmitted, until the current first header information is not greater than zero, then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends.
[0036] In order to solve the same technical problem, the present invention also provides a computer-readable storage medium, which includes a stored computer program; wherein, when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the data transmission method based on SPI communication.
[0037] In order to solve the same technical problem, the present invention also provides a terminal, including a processor, a memory and a computer program stored in the memory; wherein the computer program can be executed by the processor to implement the data transmission method based on SPI communication. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] Figure 1 : A flow chart of a data transmission method based on SPI communication provided in Embodiment 1 of the present invention;
[0039] Figure 2 : A structural diagram of a data transmission system based on SPI communication provided in Example 1 of the present invention. DETAILED DESCRIPTION
[0040] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0041] Embodiment one:
[0042] Please refer to Figure 1 , a data transmission method based on SPI communication provided by an embodiment of the present invention, the method includes steps S1 to S2, each step is specifically as follows:
[0043] Step S1, responding to a data transmission signal, acquiring data Message to be transmitted based on the data transmission signal, and determining a first terminal and a second terminal for SPI communication.
[0044] Step S2, if there is no data being transmitted between the current first terminal and the current second terminal, the data to be transmitted is taken as the first data, and the first data is iteratively processed. During each iterative processing, according to the preset transmission length rule, based on the current first data, a first data packet to be transmitted is generated including the first header information Header and the second data Data, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information Header when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is taken as the first data, until the current first header information Header is not greater than zero, and the iteration is terminated.
[0045] Among them, in each iteration, the current first header information Header is the difference between the length of the current first data and the length of the second data, and if the first terminal has third data, the current first data is composed of the current second data Data and the current third data, indicating that in this iteration, only part of the current first data - the current second data Data, is transmitted from the first terminal to the second terminal. If the first terminal does not have the third data, the current first data is used as the current second data Data, indicating that in this iteration, the current first data is completely transmitted from the first terminal to the second terminal.
[0046] As a preferred solution, step S2 includes step S21 to step S24, and each step is specifically as follows:
[0047] Step S21, if there is no data being transmitted between the current first terminal and the current second terminal, the data Message to be transmitted is used as the first data, and according to the preset initial transmission length, based on the current first data, a first data packet to be transmitted is generated including the first header information Header and the second data Data.
[0048] The length of the current second data Data is equal to the preset initial transmission length.
[0049] In this embodiment, in response to a data transmission signal, when data is transmitted from the first terminal to the second terminal for the first time, a smaller initial transmission length can be pre-set, wherein the initial transmission length is set to cover most scenarios and have a high block utilization rate, so that small data packets can be exchanged between the first terminal and the second terminal, so that the remaining length to be transmitted by the other end can be obtained while transmitting valid data, so as to avoid wasting transmission bandwidth and improve the effective utilization of transmission bandwidth. Among them, small data packet interaction refers to the process of frequent data exchange with a small amount of data in network communication, and in each data exchange process, the amount of data transmitted is not large, but the exchange frequency may be high.
[0050] Step S22, calling the SPI communication channel between the first terminal and the second terminal, transmitting the current first data packet to be transmitted from the first terminal to the second terminal, so that when the second terminal receives the current first data packet to be transmitted, it obtains the current first header information Header based on the current first data packet to be transmitted, and determines whether the current first header information Header is greater than zero; if so, execute step S23; if not, execute step S24.
[0051] Step S23, determine whether the first terminal has the third data, and use the current third data as the first data, and then iteratively process the first data. In each iterative process, set the length of the data part of the current first data packet to be transmitted according to the preset transmission length rule, and then fill the current first data into the data part of the current first data packet to be transmitted. When the data filling is completed or the data part of the current first data packet to be transmitted is completed, generate a first data packet to be transmitted including the first header information Header and the second data Data, and then call the SPI communication channel between the first terminal and the second terminal to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information Header when receiving the current first data packet to be transmitted. At this time, if the first terminal has the third data, use the current third data as the first data, until the current first header information Header is not greater than zero, then it is determined that all the data to be transmitted have been transmitted from the first terminal to the second terminal, and the iteration ends.
[0052] As a preferred solution, the process of "setting the length of the data portion of the current first data packet to be transmitted according to the preset transmission length rule, and then filling the current first data into the data portion of the current first data packet to be transmitted, and generating a first data packet to be transmitted including the first header information Header and the second data Data when the data filling is completed or the data portion of the current first data packet to be transmitted is completed" in step S23 includes steps S231 to S233, and each step is specifically as follows:
[0053] Step S231: Use the current first data as the first header information corresponding to the transmission period at the current moment.
[0054] Step S232, obtaining the first header information and the second header information corresponding to the previous transmission cycle, and comparing the first header information and the second header information corresponding to the previous transmission cycle with the maximum depth of the FIFO.
[0055] Among them, in each transmission cycle, when the first terminal receives the second data packet to be transmitted transmitted by the second terminal, the length of the remaining data to be transmitted of the second terminal is obtained based on the current second data packet to be transmitted as the second header information corresponding to the current transmission cycle.
[0056] In this embodiment, the SPI communication between the first terminal and the second terminal is bidirectional and simultaneously transmitted. In the first data packet to be transmitted from the first terminal to the second terminal, the length of the remaining data to be transmitted of the first terminal, i.e., the third data, is carried, and in the second data packet to be transmitted from the second terminal to the first terminal, the length of the remaining data to be transmitted of the second terminal is carried. Among them, the transmission logic between the terminals is consistent. Therefore, in order to transmit a certain data from the second terminal to the first terminal, the data can be analogized to the first data in this embodiment, and the second terminal in this process can be analogized to the first terminal in this embodiment, and the first terminal in this process can be analogized to the second terminal in this embodiment, and then the process of transmitting the data to be transmitted from the first terminal to the second terminal in this embodiment is referred to, and the process of transmitting the data from the second terminal to the first terminal is realized.
[0057] Step S233, if the first header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO or the second header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO, the length of the data portion of the current first data packet to be transmitted is set equal to the maximum depth of the FIFO, and then the current first data is filled into the data portion of the current first data packet to be transmitted, and when the data portion of the current first data packet to be transmitted is filled, the data in the current first data that is not filled into the data portion of the current first data packet to be transmitted is used as the third data, and then the length of the current third data is used as the first header information of the current first data packet to be transmitted, so as to generate a first data packet to be transmitted containing the first header information and the second data.
[0058] It should be noted that if the first header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO or the second header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO, it indicates that the length of the data part of the current first data packet to be transmitted is set equal to the maximum depth of the FIFO, so that the length of the data part of the first data packet to be transmitted that has completed data filling will not exceed the maximum depth of the FIFO, thereby avoiding the abnormal situation of buffer overflow.
[0059] As a preferred solution, after executing step S232, step S234 and step S235 are further included, and each step is specifically as follows:
[0060] Step S234, if the first header information corresponding to the previous transmission cycle is greater than the second header information corresponding to the previous transmission cycle and the first header information corresponding to the previous transmission cycle is less than the maximum depth of the FIFO, then the length of the data portion of the current first data packet to be transmitted is set equal to the first header information corresponding to the previous transmission cycle, and then the current first data is filled into the data portion of the current first data packet to be transmitted, and when the data filling is completed or the data portion of the current first data packet to be transmitted is filled, the first header information of the current first data packet to be transmitted is set to zero to generate a first data packet to be transmitted containing the first header information and the second data.
[0061] Step S235, if the second header information corresponding to the previous transmission cycle is greater than the first header information corresponding to the previous transmission cycle and the second header information corresponding to the previous transmission cycle is less than the maximum depth of the FIFO, then the length of the data portion of the current first data packet to be transmitted is set equal to the second header information corresponding to the previous transmission cycle, and then the current first data is filled into the data portion of the current first data packet to be transmitted, and at the end of the data filling, the first header information of the current first data packet to be transmitted is set to zero to generate a first data packet to be transmitted containing the first header information and the second data.
[0062] Step S24: determining that the first terminal does not have the third data, and that all the data to be transmitted have been transmitted from the first terminal to the second terminal.
[0063] As a preferred solution, an embodiment of the present invention provides a data transmission method based on SPI communication, which further includes steps S3 to S4, and each step is specifically as follows:
[0064] Step S3: If data is being transmitted between the current first terminal and the current second terminal, before the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, the current first data is updated based on the data to be transmitted.
[0065] Step S4, based on the length of the updated first data, update the first header information Header in the current first data packet to be transmitted, and then iterate the current first data packet to be transmitted, call the SPI communication channel between the first terminal and the second terminal in each iterative processing, and transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information Header when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information Header and the second data Data is generated, until the current first header information Header is not greater than zero, then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends.
[0066] As a preferred solution, step S4 includes step S41 to step S42, and each step is specifically as follows:
[0067] Step S41: updating the first header information in the current first data packet to be transmitted based on the updated length of the first data.
[0068] Step S42, calling the SPI communication channel between the first terminal and the second terminal, transmitting the first data packet to be transmitted with the header information updated from the first terminal to the second terminal, so that the second terminal obtains the current first header information Header and the current second data Data when receiving the current first data packet to be transmitted, and then iteratively processes the current first data. In each iterative processing, according to a preset transmission length rule, based on the current first data, a first data packet to be transmitted including the first header information Header and the second data Data is generated, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information Header when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, until the current first header information Header is not greater than zero, and then the iteration ends.
[0069] In this embodiment, the specific implementation process of "generating a first data packet to be transmitted including the first header information Header and the second data Data based on the current first data according to the preset transmission length rule" in step S42 can refer to the process of steps S231 to S233 in this embodiment.
[0070] Please refer to Figure 2 , is a structural diagram of a data transmission system based on SPI communication provided by an embodiment of the present invention, the system includes a data acquisition module M1 and a first transmission module M2, and the specific details of each module are as follows:
[0071] The data acquisition module M1 is used to respond to the data transmission signal, acquire the data to be transmitted based on the data transmission signal, and determine the first terminal and the second terminal for SPI communication;
[0072] The data transmission module M2 is used for, if there is no data being transmitted between the current first terminal and the current second terminal, taking the data to be transmitted as the first data, and iteratively processing the first data. In each iterative processing, according to the preset transmission length rule, based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has the third data, the current third data is taken as the first data, until the current first header information is not greater than zero, then the iteration is terminated; wherein, in each iteration, the current first header information is the difference between the length of the current first data and the length of the current second data, and if the first terminal has the third data, the current first data is composed of the current second data and the current third data, and if the first terminal does not have the third data, the current first data is taken as the current second data.
[0073] As a preferred option, please refer to Figure 2 , a data transmission system based on SPI communication provided by an embodiment of the present invention further includes a second transmission module M3, which is specifically as follows:
[0074] The second transmission module M3 is used for updating the current first data based on the data to be transmitted before transmitting the current first data packet to be transmitted from the first terminal to the second terminal if data is being transmitted between the current first terminal and the current second terminal; updating the first header information in the current first data packet to be transmitted based on the length of the updated first data, and then iteratively processing the current first data packet to be transmitted, calling the SPI communication channel between the first terminal and the second terminal in each iterative processing, transmitting the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted, and if the first terminal has the third data at this time, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated to be transmitted, until the current first header information is not greater than zero, then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends.
[0075] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0076] An embodiment of the present invention further provides a computer-readable storage medium, the computer-readable storage medium including a stored computer program; wherein, when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute a data transmission method based on SPI communication as described in Embodiment 1.
[0077] An embodiment of the present invention further provides a terminal, comprising a processor, a memory, and a computer program stored in the memory; wherein the computer program can be executed by the processor to implement a data transmission method based on SPI communication described in Embodiment 1.
[0078] Preferably, the computer program can be divided into one or more modules / units (such as computer program, computer program), one or more modules / units are stored in the memory and executed by the processor to complete the present invention. One or more modules / units can be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program in the terminal.
[0079] The processor can be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor, or the processor can also be any conventional processor. The processor is the control center of the terminal, and various parts of the terminal are connected using various interfaces and lines.
[0080] The memory mainly includes a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function, etc., and the data storage area can store related data, etc. In addition, the memory can be a high-speed random access memory, or a non-volatile memory, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, and a flash card (Flash Card), etc., or the memory can also be other volatile solid-state storage devices.
[0081] It should be noted that the above-mentioned terminal may include, but is not limited to, a processor and a memory. Those skilled in the art will understand that the above-mentioned terminal is merely an example and does not constitute a limitation on the terminal. It may include more or fewer components, or a combination of certain components, or different components.
[0082] Compared with the prior art, the embodiments of the present invention have the following beneficial effects:
[0083] The present invention provides a data transmission method, system, medium and terminal based on SPI communication. Based on a data transmission signal, data to be transmitted is obtained, and a first terminal and a second terminal for SPI communication are determined. Only based on the data transmission signal, the first terminal as a data transmission party and the second terminal as a data receiving party can be quickly determined. The transmission logic between the terminals is consistent, and there is no master-slave data transmission relationship. If there is no data being transmitted between the current first terminal and the current second terminal, the data to be transmitted is used as the first data, and the first data is iteratively processed. During each iterative processing, according to a preset transmission length rule, a first data packet to be transmitted containing first header information and second data is generated based on the current first data. In each iteration, the current first header information is the difference in length between the current first data and the current second data. By The first header information is added to the header of the first data packet to be transmitted from the terminal to the second terminal, and the first header information is the difference between the length of the current first data and the length of the second data, that is, the first header information carries information about how much data the first terminal has waiting to be transmitted, which can realize the dynamic change of the transmission data length, thereby effectively improving the data transmission efficiency, reducing invalid data transmission, and releasing more MCU performance. Then, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has the third data, the current third data is used as the first data until the current first header information is not greater than zero, then the iteration is terminated, and the transmission status is judged by the first header information. If the first header information is greater than zero, it means that the first terminal still has data to be transmitted, and the iteration should continue to transmit the remaining data, otherwise the transmission is completed. In addition, by setting the transmission length rule, the larger data to be transmitted is divided into smaller data packets, and the amount of data transmitted each time can be controlled to avoid buffer overflow or transmission congestion caused by excessive data volume. If a data packet fails to be transmitted, only the data packet needs to be retransmitted, rather than the entire data set, which helps to manage the buffer and reduce the impact of transmission errors.
[0084] The specific embodiments described above further illustrate the purpose, technical solutions and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. It is particularly pointed out that for those skilled in the art, any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the scope of protection of the present invention.
Claims
1. A data transmission method based on SPI communication, characterized in that: include: In response to the data transmission signal, based on the data transmission signal, obtain the data to be transmitted, and determine the first terminal and the second terminal for SPI communication; If there is no data being transmitted between the current first terminal and the current second terminal, the data to be transmitted is taken as the first data, and the first data is iteratively processed. During each iterative processing, a first data packet to be transmitted is generated based on the current first data and in accordance with a preset transmission length rule, and the first data packet to be transmitted is included in the first header information and the second data. Then, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is taken as the first data, until the current first header information is not greater than zero, and then the iteration is terminated; In which, in each iteration, the current first header information is the difference between the length of the current first data and the current second data, and if the first terminal has third data, the current first data is composed of the current second data and the current third data; if the first terminal does not have third data, the current first data is used as the current second data.
2. A data transmission method based on SPI communication as claimed in claim 1, characterized in that, The first data is iteratively processed, and each time the iterative processing is performed, a first data packet to be transmitted including first header information and second data is generated based on the current first data according to a preset transmission length rule, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, until the current first header information is not greater than zero, and then the iteration is terminated, specifically: According to a preset initial transmission length, based on the current first data, a first data packet to be transmitted is generated, which includes the first header information and the second data, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that when the second terminal receives the current first data packet to be transmitted, the current first header information is obtained based on the current first data packet to be transmitted, and whether the current first header information is greater than zero; wherein the length of the current second data is equal to the preset initial transmission length; If so, it is determined that the first terminal has the third data, and the current third data is used as the first data, and then the first data is iteratively processed. In each iterative processing, the length of the data part of the current first data packet to be transmitted is set according to the preset transmission length rule, and then the current first data is filled into the data part of the current first data packet to be transmitted. When the data filling is completed or the data part of the current first data packet to be transmitted is filled, a first data packet to be transmitted containing the first header information and the second data is generated, and then the SPI communication channel between the first terminal and the second terminal is called to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has the third data, the current third data is used as the first data, until the current first header information is not greater than zero, and then the iteration is terminated; If not, it is determined that the first terminal does not have the third data, and the data to be transmitted has been transmitted from the first terminal to the second terminal.
3. A data transmission method based on SPI communication as claimed in claim 2, characterized in that, The method of setting the length of the data portion of the current first data packet to be transmitted according to the preset transmission length rule, then filling the current first data into the data portion of the current first data packet to be transmitted, and generating the first data packet to be transmitted including the first header information and the second data when the data filling is completed or the data portion of the current first data packet to be transmitted is completed, is specifically as follows: Using the current first data as the first header information corresponding to the transmission period at the current moment; Obtaining first header information and second header information corresponding to the previous transmission cycle, and comparing the first header information corresponding to the previous transmission cycle and the second header information corresponding to the previous transmission cycle with the maximum depth of the FIFO; wherein, in each transmission cycle, when the first terminal receives the second data packet to be transmitted transmitted by the second terminal, obtaining the length of the remaining data to be transmitted of the second terminal based on the current second data packet to be transmitted as the second header information corresponding to the current transmission cycle; If the first header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO or the second header information corresponding to the previous transmission cycle is greater than the maximum depth of the FIFO, the length of the data part of the current first data packet to be transmitted is set equal to the maximum depth of the FIFO, and then the current first data is filled into the data part of the current first data packet to be transmitted, and when the data part of the current first data packet to be transmitted is filled, the data in the current first data that is not filled into the data part of the current first data packet to be transmitted is used as the third data, and then the length of the current third data is used as the first header information of the current first data packet to be transmitted, so as to generate a first data packet to be transmitted containing the first header information and the second data.
4. A data transmission method based on SPI communication as claimed in claim 3, characterized in that: After comparing the first header information corresponding to the last transmission cycle and the second header information corresponding to the last transmission cycle with the maximum depth of the FIFO, the method further includes: If the first header information corresponding to the previous transmission cycle is greater than the second header information corresponding to the previous transmission cycle and the first header information corresponding to the previous transmission cycle is less than the maximum depth of the FIFO, the length of the data portion of the current first data packet to be transmitted is set equal to the first header information corresponding to the previous transmission cycle, and then the current first data is filled into the data portion of the current first data packet to be transmitted, and when the data filling is completed or the data portion of the current first data packet to be transmitted is filled, the first header information of the current first data packet to be transmitted is set to zero to generate a first data packet to be transmitted containing the first header information and the second data; If the second header information corresponding to the previous transmission cycle is greater than the first header information corresponding to the previous transmission cycle and the second header information corresponding to the previous transmission cycle is less than the maximum depth of the FIFO, the length of the data part of the current first data packet to be transmitted is set equal to the second header information corresponding to the previous transmission cycle, and then the current first data is filled into the data part of the current first data packet to be transmitted, and at the end of the data filling, the first header information of the current first data packet to be transmitted is set to zero to generate a first data packet to be transmitted containing the first header information and the second data.
5. A data transmission method based on SPI communication as claimed in claim 1, characterized in that, Also includes: If data is being transmitted between the current first terminal and the current second terminal, before transmitting the current first data packet to be transmitted from the first terminal to the second terminal, updating the current first data based on the data to be transmitted; Based on the length of the updated first data, the first header information in the current first data packet to be transmitted is updated, and then the current first data packet to be transmitted is iteratively processed. In each iterative processing, the SPI communication channel between the first terminal and the second terminal is called to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated. Until the current first header information is not greater than zero, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends.
6. A data transmission method based on SPI communication as claimed in claim 5, characterized in that: Based on the length of the updated first data, the first header information in the current first data packet to be transmitted is updated, and then the current first data packet to be transmitted is iteratively processed. In each iterative processing, the SPI communication channel between the first terminal and the second terminal is called to transmit the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted including the first header information and the second data is generated, until the current first header information is not greater than zero, the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration is terminated, specifically: Based on the updated length of the first data, the first header information in the current first data packet to be transmitted is updated; The SPI communication channel between the first terminal and the second terminal is called, and the first data packet to be transmitted with the header information updated is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information and the current second data when receiving the current first data packet to be transmitted, and then iteratively processes the current first data. In each iterative processing, according to a preset transmission length rule, based on the current first data, a first data packet to be transmitted including the first header information and the second data is generated, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, until the current first header information is not greater than zero, and then the iteration is terminated.
7. A data transmission system based on SPI communication, characterized in that: include: A data acquisition module, used to respond to the data transmission signal, acquire the data to be transmitted based on the data transmission signal, and determine the first terminal and the second terminal for SPI communication; The first transmission module is used to, if there is no data being transmitted between the current first terminal and the current second terminal, use the data to be transmitted as the first data, and iteratively process the first data. In each iterative process, according to a preset transmission length rule, based on the current first data, a first data packet to be transmitted containing first header information and second data is generated, and then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted. At this time, if the first terminal has third data, the current third data is used as the first data, until the current first header information is not greater than zero, then the iteration is terminated; wherein, in each iteration, the current first header information is the difference between the length of the current first data and the length of the current second data, and if the first terminal has third data, the current first data is composed of the current second data and the current third data, and if the first terminal does not have third data, the current first data is used as the current second data.
8. A data transmission system based on SPI communication as claimed in claim 7, characterized in that: Also includes: The second transmission module is used for updating the current first data packet to be transmitted based on the data to be transmitted before transmitting the current first data packet to be transmitted from the first terminal to the second terminal if data is being transmitted between the current first terminal and the current second terminal; updating the first header information in the current first data packet to be transmitted based on the length of the updated first data, and then iteratively processing the current first data packet to be transmitted, calling the SPI communication channel between the first terminal and the second terminal in each iterative processing, transmitting the current first data packet to be transmitted from the first terminal to the second terminal, so that the second terminal obtains the current first header information when receiving the current first data packet to be transmitted, and if the first terminal has third data at this time, the current third data is used as the first data, and based on the current first data, a first data packet to be transmitted containing the first header information and the second data is generated to be transmitted, until the current first header information is not greater than zero, then the current first data packet to be transmitted is transmitted from the first terminal to the second terminal, and the iteration ends.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored computer program; wherein, when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute a data transmission method based on SPI communication as described in any one of claims 1 to 6.
10. A terminal, characterized in that: It comprises a processor, a memory and a computer program stored in the memory; wherein the computer program can be executed by the processor to implement a data transmission method based on SPI communication as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Flow control method, device and equipment for serial port data transmission and storage medium
CN114138700A
Data communication method and device, electronic device and storage medium
CN117667813A
Data transmission method and device, electronic equipment and storage medium
CN117692111A
Data transmission method and device, equipment and storage medium
CN118869387A
Data transmission method, device and computer readable storage medium
US20210014342A1