SPI-based data transmission method, chip, controller, and storage medium
Patent Information
- Application Number
- MYPI2025004991
- Authority / Receiving Office
- MY · MY
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-02-28
- Filing Date
- 2023-08-23
- Publication Date
- 2024-08-28
AI Technical Summary
In existing SPI communication, the master device and the slave device need to perform the exchange process of valid data blocks and invalid data blocks twice, resulting in low data transmission efficiency.
In the SPI communication system, after receiving the trigger signal sent by the second device, preparing the data block (including valid data block or invalid data block), and completing the sending and receiving of the data block during a data interaction process, the master-slave side is realized. Simultaneous transmission of data.
It improves data transmission efficiency, reduces the number of data exchanges, simplifies timing control, and avoids bus conflicts.
Smart Images

Figure PI2025004991_DRAWING_001 
Figure PI2025004991_DRAWING_002 
Figure PI2025004991_DRAWING_003
Abstract
Description
SPI-based data transmission method, chip, controller and storage medium
[0001] CROSS-REFERENCE TO RELATED APPLICATIONS
[0002] This disclosure claims priority to Chinese patent application number 202310224153.3, filed on February 28, 2023, entitled “SPI-based data transmission method, chip, controller and storage medium,” the entire contents of which are incorporated by reference into this disclosure. Technical Field
[0003] The present disclosure relates to the field of communication technology, and in particular to an SPI-based data transmission method, chip, controller, and storage medium. Background Art
[0004] In the related art, when an SPI slave device (Slave device) based on the SPI (Serial Peripheral Interface) protocol exchanges data with an SPI master device (Master device), first, the master device receives a trigger signal from the slave device, and the master device prepares an invalid data block to exchange with the valid data block of the slave device; secondly, the slave device receives a trigger signal from the master device, and the slave device prepares an invalid data block to exchange with the valid data block of the master device.
[0005] It can be seen that in the related art, the master device and the slave device need to perform two exchanges of valid data blocks and invalid data blocks, and the data transmission efficiency needs to be improved.
[0006] Public content
[0007] The present disclosure aims to solve at least one of the technical problems in the related art to a certain extent. To this end, the present disclosure provides a data transmission method, chip, controller and storage medium based on SPI.
[0008] The present disclosure provides a data transmission method based on SPI, which is applied to a first device of an SPI communication system, the method comprising:
[0009] receiving a first side trigger signal sent by a second device in the communication system based on a second valid data block that needs to be exchanged;
[0010] preparing a first side data block based on the first side trigger signal, wherein the first side data block includes a valid data block or an invalid data block;
[0011] During a data interaction process, the first side data block is sent to the second device, and a second side data block corresponding to the second valid data block and sent by the second device is received.
[0012] In one embodiment, the preparing the first-side data block based on the first-side trigger signal includes:
[0013] In response to the first-side trigger signal, determining whether the first device has a first valid data block that needs to be exchanged;
[0014] If the first valid data block exists, preparing the first valid data block as the first side data block;
[0015] If the first valid data block does not exist, an invalid data block is prepared as the first side data block.
[0016] In one embodiment, the first device has a first sending buffer and a second sending buffer; and preparing the first valid data block as the first side data block includes:
[0017] The first valid data block is written from the first sending buffer area to the second sending buffer area to prepare the first side data block.
[0018] In one embodiment, preparing the invalid data block as the first-side data block includes:
[0019] An invalid data block of a preset length is written into the second sending buffer area to prepare the first side data block.
[0020] In one embodiment, determining whether the first device has a first valid data block that needs to be exchanged includes:
[0021] Determining whether there is a first valid data block in the first sending buffer;
[0022] If the first sending buffer contains a first valid data block, determining that the first device contains a first valid data block that needs to be exchanged;
[0023] If the first sending buffer does not contain the first valid data block, it is determined that the first device does not contain the first valid data block that needs to be exchanged.
[0024] In one embodiment, before writing the first valid data block from the first sending buffer area to the second sending buffer area, the method also includes: obtaining a message to be sent; encapsulating the message into multiple data frames; writing the multiple data frames into a data block; and caching the data block into the first sending buffer area.
[0025] In one embodiment, the first device further has a first receiving buffer and a second receiving buffer; the receiving of the second side data block corresponding to the second valid data block sent by the second device includes: receiving the second side data block to the first receiving buffer; and writing the second side data block in the first receiving buffer to the second receiving buffer.
[0026] In one embodiment, the method further includes: clearing the second sending buffer and the first receiving buffer when the second-side data block is completely written into the second receiving buffer.
[0027] In one embodiment, the first device is an SPI slave device, and the second device is an SPI master device;
[0028] After preparing the first side data block based on the first side trigger signal, the method further includes: sending a second side trigger signal to the second device, so that the second device prepares the second valid data block as the second side data block.
[0029] In one embodiment, when it is determined that the reception of the second side data block is completed, the method further includes: subtracting one from the current number of receptions of the first side trigger signal; when it is determined that the first side trigger signal is received, the method further includes: adding one to the current number of receptions of the first side trigger signal; preparing the first side data block based on the first side trigger signal includes: preparing the first side data block when the current number of receptions of the first side trigger signal is greater than 0.
[0030] In one embodiment, the SPI slave device is configured with a first thread and a second thread; the first thread is used to receive the first side trigger signal, prepare the first side data block, send the first side data block, and receive the second side data block; the second thread is used to prepare the valid data block as a slave side data block when the SPI slave device has a valid data block that needs to be exchanged, and after preparing the slave side data block, send a master side trigger signal to the SPI master device to enable the SPI master device to prepare the master side data block; wherein, the first thread and the second thread are controlled by a mutex lock.
[0031] In one embodiment, the first device is an SPI master device, the second device is an SPI slave device, and the second device has a third send buffer area and a fourth send buffer area; the first side trigger signal is sent by the second device after writing the second valid data block from the third send buffer area to the fourth send buffer area when the second device has a second valid data block that needs to be exchanged.
[0032] The present disclosure provides an SPI-based data transmission method, which is applied to an SPI communication system. The SPI communication system includes a first device and a second device. The method includes: the second device sends a first side trigger signal based on a second valid data block that needs to be exchanged; the second device prepares the second valid data block as a second side data block; the first device receives the first side trigger signal sent by the second device and, based on the first side trigger signal, prepares a first side data block, where the first side data block includes a valid data block or an invalid data block; the first device sends the first side data block to the second device; and the second device sends the second side data block to the first device.
[0033] In one embodiment, the first device is an SPI master device, the second device is an SPI slave device, and the second device has a third sending buffer area and a fourth sending buffer area; the second device prepares the second valid data block as a second side data block, including: the second device writes the second valid data block from the third sending buffer area to the fourth sending buffer area to prepare the second side data block; the second device sends a first side trigger signal based on the second valid data block to be exchanged, including: the second device sends the first side trigger signal after having the second valid data block to be exchanged and writing the second valid data block to the fourth sending buffer area.
[0034] The present disclosure provides a chip, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the method described in any one of the above embodiments is implemented.
[0035] The present disclosure provides a computer-readable storage medium storing computer instructions or programs. When the computer instructions or programs are executed on a computer, the computer is enabled to execute the method described in any one of the above embodiments.
[0036] The present disclosure provides a controller, comprising a first chip and a second chip; wherein the first chip and the second chip are connected via an SPI bus; the second chip is configured to send a first side trigger signal based on a second valid data block that needs to be exchanged; the second chip is further configured to prepare the second valid data block as a second side data block; the first chip is configured to receive the first side trigger signal sent by the second chip, and based on the first side trigger signal, prepare a first side data block, the first side data block including a valid data block or an invalid data block; the first chip is further configured to send the first side data block to the second chip; the second chip is further configured to send the second side data block to the first chip.
[0037] The SPI-based data transmission method proposed in this disclosure receives a first-side trigger signal sent by a second device in the communication system based on a second valid data block to be exchanged; and based on the first-side trigger signal, prepares a first-side data block. Thus, during a single data exchange, the first-side data block is sent to the second device, and a second-side data block corresponding to the second valid data block is received from the second device. This allows the exchange of valid data blocks between the first and second devices to be completed through a single data exchange, enabling simultaneous transmission of master-side and slave-side data, thereby improving data transmission efficiency.
[0038] Additional aspects and advantages of the present disclosure will be given in part in the following description and in part will be obvious from the following description, or will be learned through practice of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] The above and / or additional aspects and advantages of the present disclosure will become apparent and readily understood from the description of the embodiments in conjunction with the following drawings, in which:
[0040] FIG1a is a schematic diagram of a standard four-wire SPI according to an embodiment of the specification;
[0041] FIG1b is a schematic diagram of a five-wire SPI according to an embodiment of the specification;
[0042] FIG1c is a schematic diagram of a six-wire SPI according to an embodiment of the specification;
[0043] FIG1d is a schematic diagram of bottom-layer hard-wire connections provided according to an embodiment of the specification;
[0044] FIG1e is a schematic diagram of a SOC-side communication framework provided according to an embodiment of the specification;
[0045] FIG1f is a schematic diagram of an MCU-side communication framework provided according to an embodiment of the specification;
[0046] FIG1g is a schematic diagram of a framework of a slave device provided according to an embodiment of the specification;
[0047] FIG1h is a schematic diagram of a framework of a main device provided according to an embodiment of the specification;
[0048] FIG2 a is a schematic flow chart of a data transmission method based on SPI according to an embodiment of the specification;
[0049] FIG2 b is a schematic diagram of interaction between a master device and a slave device according to an embodiment of the specification;
[0050] FIG2c is a schematic diagram of interaction between a master device and a slave device according to an embodiment of the specification;
[0051] FIG3 a is a flow chart of a data transmission method based on SPI according to an embodiment of the specification;
[0052] FIG3 b is a flow chart of a data transmission method based on SPI provided in accordance with an embodiment of the specification;
[0053] FIG4 a is a flow chart of a data transmission method based on SPI according to an embodiment of the specification;
[0054] FIG4 b is a flow chart of a TO_SLAVE pulse signal reception interrupt processing process provided in accordance with an embodiment of the specification;
[0055] FIG4c is a flow chart showing a process of a thread in a scenario where the Master side actively triggers a data block exchange according to an embodiment of the specification;
[0056] FIG4 d is a flow chart showing a process of actively triggering a thread for a data block exchange scenario on the slave side according to an embodiment of the specification;
[0057] FIG5 is a flow chart of a data transmission method based on SPI according to an embodiment of the specification;
[0058] FIG6 is a flow chart of a data transmission method based on SPI provided according to an embodiment of the specification. DETAILED DESCRIPTION
[0059] The following describes in detail embodiments of the present disclosure, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended only to explain the present disclosure and are not to be construed as limiting the present disclosure.
[0060] In a multi-domain centralized automotive electrical and electronic architecture, the MCU (Microcontroller Unit) handles software functions requiring real-time performance and safety, while the SoC (System on Chip) handles software functions requiring high computing performance. The MCU and SoC can be rationally allocated and backed up with redundancy to balance these requirements. Ethernet is typically used for communication between the MCU and SoC to meet high bandwidth requirements. For security reasons, other communication methods are also used between the MCU and SoC. Typically, the underlying hardware between the MCU and SoC communicates directly via various methods, such as serial ports and SPI.
[0061] In the related technologies, some solutions realize active transmission on one side of the SOC side or the MCU side based on the SPI bus; some solutions realize active transmission on both the SOC side and the MCU side; but the real-time performance is not high enough, or the communication reliability of both ends of the communication is not considered, or there are strong restrictions on the length of the communication data, or it is impossible to meet the scenario where the SOC side and the MCU side exchange valid data at the same time, or it is necessary to occupy too many hardware resources (such as pins) to control the data transmission process, or the timing control of the underlying data transmission is too complicated.
[0062] Please refer to Figure 1a, which shows a schematic diagram of a standard four-wire SPI system. Figure 1a shows two nodes (NODE1 and NODE2), where NODE1 functions as the master device (MASTER) and NODE2 functions as the slave device (SLAVE). Four-wire SPI cannot achieve real-time master-slave data exchange. Master-slave data exchange is typically performed through SPI master-side polling. While this method consumes less hardware resources (pins), it suffers from poor real-time performance. MOSI is the master-out, slave-in signal line; MISO is the master-in, slave-out signal line; CLK is the signal line that transmits the clock signal; and CS is the signal line that transmits the chip select signal.
[0063] Refer to Figure 1b, which shows a schematic diagram of a five-wire SPI system. Five-wire SPI generally adds a control line from the slave device to the master device. When the master needs to actively transmit, it can initiate a transmission using the standard four lines: CS, CLK, MOSI, and MISO. However, this does not allow for quick and accurate notification of the slave's initiation time. When the slave needs to actively transmit, it can notify the master using the S_CTRL control line, but the master may be preparing to initiate data transmission at that moment. Therefore, five-wire SPI is superior to four-wire SPI in terms of bidirectional transmission efficiency. While it only adds one control line and consumes minimal resources (pins), it does require complex logic to control bus conflicts caused by bidirectional transmission, which can also affect transmission efficiency.
[0064] Refer to Figure 1c, which shows a schematic diagram of a six-wire SPI system. Six-wire SPI typically adds a control line from the slave side to the master side and a control line from the master side to the slave side. There are two common approaches to achieving bidirectional real-time transmission with six-wire SPI.
[0065] The first approach is: when the MASTER side wants to send data, the MASTER side notifies the SLAVE side through M_CTL that it is ready. After the SLAVE side prepares the data, the SLAVE side notifies the MASTER side through S_CTL that it is ready, and then the MASTER side initiates data transmission; when the SLAVE side wants to send data, the SLAVE side notifies the MASTER side through S_CTL that it is ready. After the MASTER side prepares the data, the MASTER side notifies the SLAVE side through M_CTL that it is ready, and then the MASTER side initiates data transmission; when the MASTER side and the SLAVE side need to send at the same time, priority control and the state machines on both the MASTER side and the SLAVE side are needed to ensure that one side is transmitted first to prevent bus conflicts.
[0066] The second approach is: when the master side wants to send data, the master side notifies the slave side of its readiness through M_CTL, and the master side initiates the data transmission; when the slave side wants to send data, the slave side notifies the master side of its readiness through S_CTL, and the master side initiates the data transmission. When the master and slave sides need to send data at the same time, priority control and state machine control on both the master and slave sides are also required to ensure that one side is transmitted first to prevent bus conflicts.
[0067] The above two methods have higher real-time performance and higher transmission efficiency than the five-wire SPI. However, in the scenario of bidirectional simultaneous transmission, some additional logic control is required to prevent bus conflicts, and full-duplex real-time transmission cannot be fully achieved.
[0068] In addition, there are some solutions for seven-wire and eight-wire SPI in the related technology. On the one hand, more control states will be added, which will cause the control logic to be more complex and increase the probability of error; on the other hand, the consumption of hardware resources (pins) will be increased.
[0069] Therefore, six-wire SPI is a solution that balances resource consumption and bidirectional real-time transmission. However, for bidirectional real-time transmission scenarios, solutions in related technologies require the addition of complex bus conflict control logic to ensure that one side prioritizes transmission. As mentioned in the background, the master and slave sides need to perform the exchange process of valid data blocks and invalid data blocks twice, which shows that related technologies do not achieve bidirectional real-time transmission of valid data blocks.
[0070] Based on this, the implementation methods of this specification optimize the underlying communication timing, ensuring that valid data blocks on the master and slave sides are completed in a single data exchange process, enabling real-time data transmission between the master and slave sides and improving data transmission efficiency. Please refer to Figure 1d, which shows a schematic diagram of the underlying hard-wired connection provided by the implementation methods of this specification. It should be noted that the pin descriptions of the hard-wired connections involved in Figure 1d are shown in Table 1.
[0071] Table 1: Hard-wire connection pin description
[0072] Furthermore, the embodiments of this specification also provide a SPI-based communication framework between the SOC and the MCU. Please refer to Figure 1e, which shows a schematic diagram of the SOC-side communication framework provided for the embodiments of this specification. The SOC-side communication framework is divided into four layers: physical layer, driver layer, communication layer, and application layer. The communication layer is divided into three modules: data link function module, transmission function module, and interface call module. The functions implemented by each layer on the SOC side are shown in Table 2.
[0073] Table 2: Function description of each layer on the SOC side
[0074] Please refer to Figure 1f, which shows the MCU-side communication framework provided for embodiments of this specification. The MCU-side communication framework is divided into four layers: physical layer, driver layer, communication layer, and application layer. The communication layer is divided into the data link function module and the transmission function module, from bottom to top. The functions implemented by each layer on the MCU are shown in Table 3.
[0075] Table 3: Function description of each layer on the MCU side
[0076] Through the communication framework in the implementation mode of this specification, a more clear architectural layering of the communication framework is achieved. Based on the underlying communication timing, the communication framework in the implementation mode of this specification meets the requirements of rapid portability. It should be noted that in the implementation mode of this specification, the SOC side is used as the SPI slave side, and the MCU side is used as the SPI master side. While achieving two-way communication between the SOC side and the MCU side, the diverse communication function requirements of the SOC side and the MCU side are met.
[0077] The embodiments of this specification provide a scenario example of an SPI-based data transmission method. Please refer to Figures 1g and 1h. Figure 1g shows a schematic diagram of the framework of a slave device, and Figure 1h shows a schematic diagram of the framework of a master device. In this scenario example, the slave side can use an SOC, and the master side can use an MCU. The communication layer mainly implements the data link module and the transmission module. For the SOC side, since it uses the Linux operating system, it is also necessary to implement an interface call module to complete the data transmission between user space and kernel space.
[0078] In this scenario example, the Master side actively triggers the exchange sequence interaction process. The following example illustrates the exchange process of valid data blocks between the Master side and the Slave side, including (1) to (6):
[0079] (1) On the Master side, the application data message is sent to the data message sending linked list buffer of the transmission module of the communication layer through the application data sending routine of the application layer. The application data message is encapsulated by the encapsulation routine of the transmission module to obtain several valid data blocks, and the valid data block A is written into the data block sending FIFO buffer of the data link module. The data sending trigger routine triggers the sending of the TO_SLAVE signal, sending the TO_SLAVE signal to the Slave side. The Slave side receives the TO_SLAVE signal and wakes up the transmission thread 1.
[0080] (2) On the Slave side, the valid data block B in the data block send FIFO buffer is written to the send buffer (DMA) of the driver layer through transmission thread 1; after the Slave side completes the preparation of the valid data block B, the Slave side sends the TO_MASTER signal to the Master side through transmission thread 1.
[0081] (3) On the Master side, the TO_MASTER signal is received through the data exchange routine. The data exchange routine writes the valid data block A in the data link module's data block send FIFO buffer into the driver layer's send buffer (DMA). The valid data block A is sent to the Slave side through the Master side's driver interface.
[0082] (4) On the slave side, the valid data block A is written into the slave side's receive buffer (DMA) through the slave side's driver interface. Through transmission thread 1, the valid data block A is written from the slave side's driver layer's receive buffer (DMA) to the data block receive FIFO buffer of the slave side's data link module. The valid data block A is parsed by the data link module's parsing thread, and the parsed data message is written into the data message receive linked list buffer of the slave side's transmission module. The data message is then written into the application data buffer through the transmission module's receive thread, interface call module, and application data receive thread, and provided to the application data processing thread for processing.
[0083] (5) While the Slave side is receiving the valid data block A, the Slave side sends the valid data block B to the Master side through the Slave side's driver interface.
[0084] (6) On the Master side, the valid data block B is written into the Master side's receive buffer (DMA) through the Master side's driver interface. Through the transmission completion interrupt routine, the valid data block B is written from the Master side's driver layer's receive buffer (DMA) to the Master side's data block receive FIFO buffer. The valid data block B is parsed by the data link module's parsing thread, and the parsed data message is written into the Master side's transmission module's data message receive linked list buffer. The data message is then written into the application data buffer through the transmission module's receive routine and application data receive routine, and provided to the application data processing thread for processing.
[0085] In this scenario example, the slave side actively triggers the exchange timing interaction process. The following example illustrates the exchange process of valid data blocks between the slave side and the master side, including (1) to (5):
[0086] (1) On the slave side, the application data sending thread of the application layer sends the application data message to the data message sending linked list buffer of the transmission module of the communication layer. The application data message is encapsulated by the encapsulation thread of the transmission module to obtain several valid data blocks, and the valid data block X is written into the data block sending FIFO buffer of the data link module. The valid data block X in the data block sending FIFO buffer is written to the sending buffer (DMA) of the driver layer through the transmission thread 2; the sending of the TO_MASTER signal is triggered by the transmission thread 2, and the TO_MASTER signal is sent to the Master side. The Master side receives the TO_MASTER signal and wakes up the data exchange routine.
[0087] (2) On the Master side, the data exchange routine writes the valid data block Y in the data link module's data block send FIFO buffer into the driver layer's send buffer (DMA). The valid data block Y is sent to the Slave side through the Master side's driver interface.
[0088] (3) On the Slave side, the valid data block Y is received by transmission thread 2 to the receiving buffer (DMA) of the driver layer on the Slave side, and at the same time, the valid data block X of the sending buffer (DMA) of the driver layer on the Slave side is sent to the Master side through the driver interface on the Slave side.
[0089] (4) On the Master side, the valid data block X is written into the Master side's receive buffer (DMA) through the Master side's driver interface. Through the transmission completion interrupt routine, the valid data block X is written from the Master side's driver layer's receive buffer (DMA) to the Master side's data link module's data block receive FIFO buffer. The valid data block X is parsed by the data link module's parsing thread, and the parsed data message is written into the Master side's transmission module's data message receive linked list buffer. The data message is then written into the application data buffer through the transmission module's receive routine and application data receive routine, and provided to the application data processing thread for processing.
[0090] (5) On the slave side, the valid data block Y is written from the slave side driver layer's receive buffer (DMA) to the slave side data link module's data block receive FIFO buffer via transmission thread 2. The valid data block Y is parsed by the data link module's parsing thread, and the parsed data message is written to the slave side transmission module's data message receive linked list buffer. The data message is then written to the application data buffer via the transmission module's receive thread, interface call module, and application data receive thread, and then provided to the application data processing thread for processing.
[0091] It should be noted that SPI stands for Serial Peripheral Interface. It is a high-speed, full-duplex, synchronous communication bus that can be used for inter-chip communication. MCU stands for Microcontroller Unit. In automotive domain controllers, MCUs are generally used to implement functions that require real-time performance and safety. SOC stands for System on Chip. In automotive domain controllers, SoCs are generally used to implement functions that require high computing and communication performance.
[0092] In this scenario example, the data message refers to the application layer data cached in the transmission module. The data message cached in the transmission module is encapsulated according to the frame format, which is a data frame. The response information is encapsulated according to the frame format, which is a response frame. The fixed data used to fill the remaining space of the data block is encapsulated according to the frame format, which is a padding frame. In this scenario example, the driver layer data exchange is fixed-length, and writing the data frame, response frame, and padding frame into a fixed-length storage space can form a data block. It should be noted that the data message is a concept of the transmission module, and the data frame, response frame, padding frame, and data block are concepts of the data link module.
[0093] In this example scenario, the data link module's transmit and receive buffers are used to cache 1280-byte data blocks for both sending and receiving. Each block buffer corresponds to a receive and send buffer. The block buffer is a first-in, first-out circular queue with a queue depth of 12, capable of storing twelve 1280-byte blocks simultaneously. The driver layer's transmit and receive buffers exchange data with the data link module's transmit and receive buffers.
[0094] In this scenario example, on the one hand, a new SPI underlying communication timing control method is implemented based on the two newly added GPIO pins and the driver layer code logic. The timing control method has simple logic and can avoid conflicts caused by simultaneous transmission triggering on the host side and the slave side. On the other hand, a set of inter-chip communication frameworks on the SOC side and the MCU side are implemented based on the underlying SPI communication, which can provide segmented transmission services for longer data. Moreover, this framework has the characteristics of clear layering, simple interface, and good portability and scalability.
[0095] The embodiments of this specification provide a data transmission method based on SPI, which is applied to a first device of an SPI communication system. Referring to FIG2a , the data transmission method based on SPI may include:
[0096] S210: Receive a first side trigger signal sent by a second device in the communication system based on a second valid data block that needs to be exchanged.
[0097] S220 : Prepare a first-side data block based on a first-side trigger signal, where the first-side data block includes a valid data block or an invalid data block.
[0098] S230: During a data interaction process, send a first side data block to a second device, and receive a second side data block corresponding to a second valid data block sent by the second device.
[0099] Wherein, the communication system includes a first device and a second device. The first device serves as a side that passively receives data. Specifically, the second device has a second valid data block that needs to be exchanged, and the second device sends a first side trigger signal to the first device based on the second valid data block. The first device receives the first side trigger signal and prepares the first side data block based on the first side trigger signal. After the first device prepares the first side data block, the first device and the second device begin to jointly execute a data interaction process. During a data interaction process, the first device sends the first side data block to the second device, and at the same time receives the second side data block corresponding to the second valid data block sent by the second device. It should be noted that when the first device has a valid data block that needs to be exchanged, the valid data block to be exchanged is prepared as the first side data block. Therefore, during a data interaction process, data exchange of valid first side data blocks and valid second side data blocks is realized between the first device and the second device. The invalid data block is prepared as the first-side data block only when the first device has no valid data block to be exchanged. It can be seen that in this embodiment, in a data interaction process, data is exchanged between the first device and the second device using the invalid data block and the valid second-side data block only when the first device has no valid data block to be exchanged.
[0100] In this embodiment, the SPI communication system includes a master device and a slave device. The SPI communication system corresponds to three scenarios. They are respectively a scenario in which the master device (Master side) actively triggers data block exchange, a scenario in which the slave device (Slave side) actively triggers data block exchange, and a scenario in which the Master side and the Slave side simultaneously trigger data block exchange. The following describes the data transmission process based on SPI for different scenarios. The first device in the embodiment of this specification can be understood as a side that passively receives data. For example, in the scenario in which the Master side actively triggers data block exchange and the scenario in which the Master side and the Slave side simultaneously trigger data block exchange, the first device can be regarded as a slave device. In the scenario in which the Slave side actively triggers data block exchange, the first device can be regarded as a master device.
[0101] Refer to Figure 2b. For the scenario where the master actively triggers data block exchange, the second device can be considered the master device and the first device can be considered the slave device. Specifically, when the master device has a second valid data block that needs to be exchanged, the master device sends a first-side trigger signal to the slave device, and the slave device receives the first-side trigger signal. Based on the first-side trigger signal, the slave device prepares a first-side data block for data exchange. It should be noted that when the slave device has a valid data block that needs to be exchanged, the valid data block to be exchanged can be prepared as the first-side data block. When the slave device does not have a valid data block to exchange, an invalid data block can be prepared as the first-side data block. It can be seen that the first-side data block includes a valid data block or an invalid data block. After completing the first-side data block, during a data exchange process, the slave device sends the first-side data block to the master device and receives the second-side data block corresponding to the second valid data block sent by the master device. It can be seen that during a data exchange process, the exchange of valid data blocks between the master and slave devices is achieved, reducing the number of data exchanges between the master and slave devices and improving data transmission efficiency. It should be noted that, for the scenario where the Master side actively triggers data block exchange, the first-side trigger signal can be regarded as a TO_SLAVE pulse signal.
[0102] Please refer to Figure 2c. For the scenario where the slave side actively triggers data block exchange, the first device can be considered as the master device and the second device can be considered as the slave device. Specifically, the slave device prepares a second valid data block that needs to be exchanged. The slave device sends a first-side trigger signal to the master device, and the master device receives the first-side trigger signal. Based on the first-side trigger signal, the master device prepares a first-side data block for data exchange. It should be noted that if the master device has a valid data block that needs to be exchanged, the valid data block that needs to be exchanged can be prepared as the first-side data block. If the master device does not have a valid data block that needs to be exchanged, an invalid data block can be prepared as the first-side data block. It can be seen that the first-side data block includes a valid data block or an invalid data block. After completing the first-side data block, during a data exchange process, the master device sends the first-side data block to the slave device and receives the second-side data block corresponding to the second valid data block sent by the slave device. It can be seen that during a data exchange process, the exchange of valid data blocks between the master and slave devices is achieved, reducing the number of data exchanges between the master and slave devices and improving data transmission efficiency. It should be noted that, for the scenario where the SLAVE side actively triggers data block exchange, the first-side trigger signal can be regarded as a TO_MASTER pulse signal.
[0103] For the scenario where the master side and the slave side trigger data block exchange at the same time, the scenario where the master side and the slave side trigger data block exchange at the same time is the same as the scenario where the master side actively triggers data block exchange. The data interaction process and timing are the same and will not be repeated here.
[0104] In the above-mentioned SPI-based data transmission method, data exchange can be completed between the first device and the second device through one data interaction, thereby realizing simultaneous transmission of master-side data and slave-side data. The timing control logic is simple and there is no bus conflict problem.
[0105] In some embodiments, preparing the first side data block based on the first side trigger signal may include: in response to the first side trigger signal, determining whether the first device has a first valid data block that needs to be exchanged; if there is a first valid data block, preparing the first valid data block as the first side data block; if there is no first valid data block, preparing an invalid data block as the first side data block.
[0106] Specifically, when the first device receives a first-side trigger signal sent by the second device, the first device determines whether there is data in the send buffer. If there is data, it indicates that the first device has a first valid data block to be exchanged. If there is no data, it indicates that the first device does not have a first valid data block to be exchanged. If the first device has a first valid data block, the first device prepares the first valid data block as the first-side data block. If the first device does not have a first valid data block, the first device prepares an invalid data block as the first-side data block.
[0107] Please refer to Figure 3a. For the scenario where the Master side actively triggers the data block exchange, the second device can be regarded as the master device and the first device can be regarded as the slave device. The first-side trigger signal can be regarded as a TO_SLAVE pulse signal. Specifically, in the scenario where the Master side is idle and there are data blocks to be sent, the slave device receives the TO_SLAVE pulse signal sent by the master device. The slave device responds to the TO_SLAVE pulse signal to determine whether the slave device has the first valid data block that needs to be exchanged; if the slave device has the first valid data block, the slave device prepares the first valid data block as the first-side data block; if the slave device does not have the first valid data block, the slave device prepares the invalid data block as the first-side data block. It should be noted that,
[0108] Referring to Figure 3b, in a scenario where the slave side actively triggers data block exchange, the first device can be considered the master device, the second device can be considered the slave device, and the first-side trigger signal can be considered the TO_MASTER pulse signal. Specifically, the master device receives the TO_MASTER pulse signal sent by the slave device. In response to the TO_MASTER pulse signal, the master device determines whether it has the first valid data block to be exchanged. If the master device has the first valid data block, the master device prepares the first valid data block as the first-side data block. If the master device does not have the first valid data block, the master device prepares the invalid data block as the first-side data block.
[0109] In the above embodiment, when the first device has valid data blocks to exchange, the valid data blocks to be exchanged are prepared as first-side data blocks. Therefore, during a single data exchange process, the first and second devices exchange valid first-side data blocks with valid second-side data blocks. Only when the first device has no valid data blocks to exchange, data is exchanged between the first and second devices using invalid data blocks and valid second-side data blocks. This simplifies the data transmission process and improves data transmission efficiency.
[0110] In some embodiments, the first device has a first sending buffer and a second sending buffer; preparing the first valid data block as a first side data block includes: writing the first valid data block from the first sending buffer to the second sending buffer to prepare the first side data block.
[0111] Specifically, the first device has a first sending buffer and a second sending buffer. A first valid data block is stored in the first sending buffer of the first device. In response to a first-side trigger signal, the first device writes the first valid data block in the first sending buffer into the second sending buffer to prepare a first-side data block for sending to the second device.
[0112] In scenarios where the master actively triggers data block exchange, the second device can be considered the master device, and the first device can be considered the slave device. The first-side trigger signal can be considered the TO_SLAVE pulse signal. The slave device has a driver layer and a data link module. The data link module has a first send buffer, and the driver layer has a second send buffer. The first send buffer of the data link module has a first valid data block. In response to the TO_SLAVE pulse signal, the slave device writes the first valid data block from the first send buffer of the data link module to the second send buffer of the driver layer, preparing the first-side data block for transmission to the master device.
[0113] In the scenario where the slave side actively triggers data block exchange, the first device can be considered as the master device and the second device can be considered as the slave device. The first-side trigger signal can be considered as the TO_MASTER pulse signal. Specifically, the master device has a driver layer and a data link module. The data link module has a first send buffer area, and the driver layer has a second send buffer area. The first send buffer area of the data link module has a first valid data block. In response to the TO_MASTER pulse signal, the master device writes the first valid data block from the first send buffer area of the data link module to the second send buffer area of the driver layer, preparing the first-side data block for transmission to the slave device.
[0114] In the above embodiment, when the first device has a valid data block that needs to be exchanged, the valid data block that needs to be exchanged is prepared as a first-side data block. Therefore, in a data interaction process, data exchange of the valid first-side data block and the valid second-side data block is realized between the first device and the second device.
[0115] In some implementations, preparing the invalid data block as the first side data block includes: writing the invalid data block of a preset length into the second sending buffer to prepare the first side data block.
[0116] Specifically, the first device and the second device can exchange data blocks of a preset length. Since there is no data in the first send buffer of the first device, the data block in the second send buffer can be padded with custom invalid characters according to the preset length, that is, the invalid data block of the preset length can be written to the second send buffer. The first device prepares the first-side data block using the invalid data block of the preset length.
[0117] In scenarios where the master actively triggers data block exchange, the second device can be considered the master device, and the first device can be considered the slave device. The first-side trigger signal can be considered the TO_SLAVE pulse signal. The slave device has a driver layer and a data link module. The data link module has a first send buffer, and the driver layer has a second send buffer. If the first send buffer of the data link module does not contain the first valid data block, in response to the TO_SLAVE pulse signal, the slave device writes an invalid data block of a preset length to the second send buffer, preparing the first-side data block for transmission to the master device.
[0118] In the scenario where the slave side actively triggers data block exchange, the first device can be considered the master device and the second device can be considered the slave device. The first-side trigger signal can be considered the TO_MASTER pulse signal. Specifically, the master device has a driver layer and a data link module. The data link module has a first send buffer area, and the driver layer has a second send buffer area. The first send buffer area of the data link module does not have a first valid data block. In response to the TO_MASTER pulse signal, the master device writes an invalid data block of a preset length to the second send buffer area to prepare for sending the first-side data block to the slave device.
[0119] In the above embodiment, only when the first device has no valid data blocks to be exchanged, data is exchanged between the first device and the second device using invalid data blocks and valid second-side data blocks, thereby simplifying the data transmission process and improving data transmission efficiency.
[0120] In some embodiments, determining whether the first device has a first valid data block that needs to be exchanged includes: determining whether the first sending buffer area has the first valid data block; if the first sending buffer area has the first valid data block, determining that the first device has the first valid data block that needs to be exchanged; if the first sending buffer area does not have the first valid data block, determining that the first device does not have the first valid data block that needs to be exchanged.
[0121] Specifically, the first device has a first send buffer and a second send buffer. When the first device receives a first-side trigger signal sent by the second device, the first device determines whether there is data in the first send buffer. If there is data, it indicates that the first device has a first valid data block to be exchanged. If not, it indicates that the first device does not have a first valid data block to be exchanged.
[0122] In the scenario where the Master side actively triggers data block exchange, the second device can be considered the Master device and the first device can be considered the Slave device. The first-side trigger signal can be considered the TO_SLAVE pulse signal. The Slave device has a data link module, which has a first transmit buffer. In response to the TO_SLAVE pulse signal, the Slave device determines whether the first transmit buffer of the data link module has a first valid data block. If the first transmit buffer of the data link module has a first valid data block, the Slave device determines that the Slave device has a first valid data block that needs to be exchanged. If the first transmit buffer of the data link module does not have a first valid data block, the Slave device determines that the Slave device does not have a first valid data block that needs to be exchanged.
[0123] In the scenario where the slave side actively triggers data block exchange, the first device can be regarded as the master device and the second device can be regarded as the slave device. The first-side trigger signal can be regarded as a TO_MASTER pulse signal. Specifically, the master device has a data link module, and the data link module has a first send buffer. In response to the TO_MASTER pulse signal, the master device determines whether the first send buffer of the data link module has a first valid data block. If the first send buffer of the data link module has a first valid data block, it is determined that the master device has a first valid data block that needs to be exchanged. If the first send buffer of the data link module does not have a first valid data block, it is determined that the master device does not have a first valid data block that needs to be exchanged.
[0124] In some implementations, before writing the first valid data block from the first sending buffer to the second sending buffer, referring to FIG. 4a , the method may further include the following steps:
[0125] S410: Obtain a message to be sent.
[0126] S420: Encapsulate the message into multiple data frames.
[0127] S430: Write multiple data frames into a data block.
[0128] S440: Cache the data block into a first sending buffer area.
[0129] The communication layer of the first device includes a data link module and a transmission module. The transmission module has a data message transmission linked list buffer, and the data link module has a first transmission buffer. Data messages from the application layer are transmitted to the data link layer. The data message transmission linked list buffer is used to buffer data messages to be transmitted. The data link module encapsulates the data messages to be transmitted into data frames, writes them into data blocks, and writes the data blocks into the first transmission buffer of the data link module.
[0130] For example, the driver layer can complete the exchange of 1280-byte data blocks at a time. The size of the driver layer's transceiver buffer can be 1280 bytes. This 1280-byte data block will first be filled with message frames (data frames or response frames) until the 1280 bytes are just filled. When the 1280 bytes cannot be filled, invalid frames or invalid data will be filled according to the remaining space. The distribution scenarios of the driver layer's transceiver data are shown in the following table:
[0131] In scenarios where the master actively triggers data block exchange, the second device can be considered the master device, and the first device can be considered the slave device. Specifically, the slave device's communication layer includes a data link module and a transmission module. The transmission module of the slave device has a data message transmission linked list buffer. The slave device's data link module has a first transmission buffer. The slave device encapsulates the data message to be transmitted into a data frame, writes the data block, and writes the data block to the first transmission buffer of the slave device's data link module.
[0132] In scenarios where the slave side actively triggers data block exchange, the first device can be considered the master device, and the second device can be considered the slave device. Specifically, the master device's communication layer includes a data link module and a transmission module. The master device's transmission module has a data message transmission list buffer. The master device's data link module has a first transmission buffer. The master device encapsulates the data message to be transmitted into a data frame, writes the data block, and writes the data block to the first transmission buffer of the master device's data link module.
[0133] In some embodiments, the first device also has a first receiving buffer and a second receiving buffer; receiving a second side data block corresponding to a second valid data block sent by the second device may include: receiving the second side data block to the first receiving buffer; writing the second side data block in the first receiving buffer to the second receiving buffer.
[0134] Specifically, the first device has a first receive buffer and a second receive buffer. The second device sends a second side data block to the first device, the first device receives the second side data block and writes it into the first receive buffer of the first device, and the first device writes the second side data block in the first receive buffer into the second receive buffer.
[0135] In scenarios where the master actively triggers data block exchange, the second device can be considered the master, and the first device can be considered the slave. The slave device has a driver layer and a data link module. The data link module has a second receive buffer, and the driver layer has a first receive buffer. The master device sends a second-side data block to the slave device. The slave device receives the second-side data block into the driver layer's first receive buffer and then writes the second-side data block from the driver layer's first receive buffer to the data link module's second receive buffer.
[0136] In scenarios where the slave side actively triggers data block exchange, the first device can be considered the master device, and the second device can be considered the slave device. The master device has a driver layer and a data link module. The data link module has a second receive buffer, and the driver layer has a first receive buffer. The slave device sends a second-side data block to the master device. The master device receives the second-side data block in the driver layer's first receive buffer and then writes the second-side data block from the driver layer's first receive buffer to the data link module's second receive buffer.
[0137] In some implementations, the method may further include: clearing the second sending buffer and the first receiving buffer when the second-side data block is completely written into the second receiving buffer.
[0138] Specifically, the first device sends a first-side data block to the second device, and simultaneously, the first device receives a second-side data block sent by the second device. The first device writes the second-side data block into the second receive buffer of the first device. If the second-side data block is written into the second receive buffer, it indicates that the first device has completed receiving the second-side data block sent by the second device, i.e., the data block exchange between the first and second devices has been completed, and the first receive buffer and the second send buffer can be cleared.
[0139] In the scenario where the master side actively triggers data block exchange, the second device can be considered the master device and the first device can be considered the slave device. The slave device has a driver layer and a data link module. The data link module has a first send buffer and a second receive buffer, and the driver layer has the second send buffer and a first receive buffer. The first send buffer of the data link module has a first valid data block. In response to the TO_SLAVE pulse signal, the slave device writes the first valid data block from the first send buffer of the data link module to the second send buffer of the driver layer, and then sends the first side data block of the second send buffer to the master device. At the same time, the slave device receives the second side data block sent by the master device, receives the second side data block to the first receive buffer of the driver layer, and writes the second side data block from the first receive buffer of the driver layer to the second receive buffer of the data link module. When the second side data block is written to the second receive buffer, the first side data block in the second send buffer and the second side data block in the first receive buffer are cleared.
[0140] In the scenario where the slave side actively triggers data block exchange, the first device can be regarded as the master device and the second device can be regarded as the slave device. The master device has a driver layer and a data link module. The data link module has a first send buffer and a second receive buffer. The driver layer has the second send buffer and a first receive buffer. The first send buffer of the data link module has a first valid data block. In response to the TO_MASTER pulse signal, the master device writes the first valid data block from the first send buffer of the data link module to the second send buffer of the driver layer, and the master device sends the first side data block of the second send buffer to the slave device. At the same time, the master device receives the second side data block sent by the slave device, receives the second side data block to the first receive buffer of the driver layer, and writes the second side data block from the first receive buffer of the driver layer to the second receive buffer of the data link module. When the second side data block is completely written to the second receive buffer, the first side data block in the second send buffer and the second side data block in the first receive buffer are cleared.
[0141] In some embodiments, the first device is an SPI slave device and the second device is an SPI master device. After preparing the first side data block based on the first side trigger signal, the method may further include: sending a second side trigger signal to the second device to cause the second device to prepare the second valid data block as the second side data block.
[0142] Specifically, when the second device has a second valid data block that needs to be exchanged, the second device sends a first side trigger signal to the first device, and the first device receives the first side trigger signal. The first device prepares the first side data block for data exchange based on the first side trigger signal. After completing the preparation of the first side data block, the first device sends a second side trigger signal to the second device, and the second device prepares the second valid data block as the second side data block based on the second side trigger signal.
[0143] For example, when the SPI master device has a second valid data block that needs to be exchanged, the SPI master device sends a TO_SLAVE pulse signal to the SPI slave device, and the SPI slave device receives the TO_SLAVE pulse signal. The SPI slave device prepares a first-side data block for data exchange based on the TO_SLAVE pulse signal. After completing the preparation of the first-side data block, the SPI slave device sends a TO_MASTER pulse signal to the SPI master device, and the SPI master device prepares the second valid data block as a second-side data block based on the TO_MASTER pulse signal.
[0144] In some embodiments, when it is determined that the reception of the second-side data block is completed, the method may further include: reducing the current number of reception times of the first-side trigger signal by one.
[0145] If it is determined that the first side trigger signal has been received, the method may further include: incrementing a current reception count of the first side trigger signal by 1. Accordingly, preparing the first side data block based on the first side trigger signal may include: preparing the first side data block if the current reception count of the first side trigger signal is greater than 0.
[0146] Specifically, the first device records the number of times the first-side trigger signal is received. When the first device completes receiving the second-side data block, the data exchange state of the first device can be released, so that the first device enters the data preparation state for receiving the data block, and the current number of times the first-side trigger signal is received is reduced by one.
[0147] When the first device determines that it has received the first-side trigger signal, to reduce conflicts on the bus, the current number of times the first-side trigger signal has been received is incremented by one. The current number of times the first-side trigger signal has been received indicates the data exchange state that the first device is in. Since the current number of times the first-side trigger signal has been received is greater than 0, indicating that the first-side trigger signal has been received, a first-side data block can be prepared.
[0148] For example, the slave can execute an interrupt handling process after the data block exchange is complete. See Figure 4b, which illustrates the TO_SLAVE pulse signal reception interrupt handling process. This process specifically includes recording the number of master-triggered data block exchanges received by the slave and waking up a dedicated slave thread dedicated to handling master-triggered data block exchanges.
[0149] Exemplarily, the Slave side has a thread for the scenario in which the Master side actively triggers the data block exchange, that is, in the scenario in which the Master side actively triggers, the thread is responsible for exchanging data blocks with the Master side. Please refer to Figure 4c, which shows the processing flow of the thread for the scenario in which the Master side actively triggers the data block exchange. Specifically, it includes: when there is data on the Slave side, taking a fixed-length data block from the communication layer data link module and writing it into the driver layer send buffer; when there is no data on the Slave side, writing a fixed-length invalid data block into the driver layer send buffer; waiting for the physical layer to complete the exchange of the fixed-length data block; processing and recording the number of Master-triggered data block exchanges received by the Slave side (that is, the number needs to be reduced by one to complete one exchange).
[0150] Exemplarily, the Slave side has a thread for the scenario in which the Slave side actively triggers data block exchange. That is, in the scenario in which the Slave side actively triggers the exchange, this thread is responsible for exchanging data blocks with the Master side. Please refer to Figure 4d, which shows the processing flow of the thread for the scenario in which the Slave side actively triggers data block exchange. Specifically, it includes: when there is data on the Slave side, taking a fixed-length data block from the communication layer data link module and writing it to the driver layer send buffer; when there is no data on the Slave side, writing a fixed-length invalid data block to the driver layer send buffer; and waiting for the physical layer to complete the exchange of fixed-length data blocks.
[0151] In some embodiments, an SPI slave device is configured with a first thread and a second thread. The first thread is configured to receive a first-side trigger signal, prepare a first-side data block, send the first-side data block, and receive a second-side data block. The second thread is configured to, when the SPI slave device has a valid data block to be exchanged, prepare the valid data block as a slave-side data block and, after preparing the slave-side data block, send a master-side trigger signal to the SPI master device to cause the SPI master device to prepare a master-side data block. The first and second threads are controlled by a mutex lock.
[0152] The SPI slave device is configured with a first thread for processing the data block exchange process actively triggered by the Master side and a second thread for processing the data block exchange process actively triggered by the Slave side. In the data block exchange process actively triggered by the Master side, the SPI slave device receives a first-side trigger signal, prepares a first-side data block, sends a first-side data block, and receives a second-side data block through the first thread. In the data block exchange process actively triggered by the Slave side, when the SPI slave device has a valid data block that needs to be exchanged, the SPI slave device prepares the valid data block as a slave-side data block through the second thread. After preparing the slave-side data block, a master-side trigger signal is sent to the SPI master device through the second thread, so that the SPI master device prepares the master-side data block based on the master-side trigger signal. The first thread and the second thread are controlled by adding a mutex lock to ensure that only one thread can participate in the data block exchange at a time.
[0153] In some embodiments, the first device is an SPI master device, the second device is an SPI slave device, and the second device has a third transmit buffer and a fourth transmit buffer. The first-side trigger signal is sent by the second device after writing the second valid data block from the third transmit buffer to the fourth transmit buffer when the second device has a second valid data block to be exchanged.
[0154] In the scenario where the slave side actively triggers data block exchange, the second device is an SPI slave device and the first device is an SPI master device. When the SPI slave device completes data preparation, the SPI slave device sends a first-side trigger signal to the SPI master device. Specifically, the second device has a third send buffer area and a fourth send buffer area. It is determined whether there is a second valid data block in the third send buffer area of the second device. If so, it indicates that the second device has a second valid data block that needs to be exchanged. Furthermore, the second device writes the second valid data block in the third send buffer area to the fourth send buffer area. At this time, the second device has completed data preparation and can send a first-side trigger signal to the first device.
[0155] For example, for the scenario where the slave side actively triggers the data block exchange, the first device can be regarded as the master device and the second device can be regarded as the slave device. The master device has a driver layer and a data link module, the data link module has a third sending buffer area, and the driver layer has a fourth sending buffer area. The third sending buffer area of the data link module has a second valid data block. In response to the TO_SLAVE pulse signal, the master device writes the second valid data block from the third sending buffer area of the data link module to the fourth sending buffer area of the driver layer, and the master device sends the second side data block of the fourth sending buffer area to the slave device. At the same time, the master device receives the first side data block sent by the slave device, the master device receives the first side data block to the third receiving buffer area of the driver layer, and writes the first side data block from the third receiving buffer area of the driver layer to the fourth receiving buffer area of the data link module. When the second side data block is completed from being written to the fourth receiving buffer area, the second side data block in the fourth sending buffer area and the first side data block in the third receiving buffer area are cleared.
[0156] The embodiments of this specification provide an SPI data transmission method, which is applied to an SPI communication system, wherein the SPI communication system includes a first device and a second device. Referring to FIG5 , the method may include the following steps:
[0157] S510: The second device sends a first-side trigger signal based on a second valid data block that needs to be exchanged.
[0158] S520: The second device prepares a second valid data block as a second side data block.
[0159] S530: The first device receives a first side trigger signal sent by the second device, and prepares a first side data block based on the first side trigger signal, where the first side data block includes a valid data block or an invalid data block.
[0160] S540: The first device sends the first side data block to the second device.
[0161] S550: The second device sends the second side data block to the first device.
[0162] In some embodiments, the first device is an SPI master device, the second device is an SPI slave device, and the second device has a third transmit buffer area and a fourth transmit buffer area. Referring to FIG. 6 , the second device prepares the second valid data block as the second side data block, which may include:
[0163] S610: The second device writes a second valid data block from the third sending buffer area to the fourth sending buffer area to prepare a second-side data block.
[0164] The second device sends a first-side trigger signal based on a second valid data block to be exchanged, including:
[0165] S620: After the second device has a second valid data block that needs to be exchanged and writes the second valid data block into the fourth sending buffer area, the second device sends a first-side trigger signal.
[0166] The embodiments of this specification provide a system including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the method in any of the above embodiments is implemented.
[0167] An embodiment of this specification provides a computer-readable storage medium, which stores computer instructions or programs. When the computer instructions or programs are executed on a computer, the computer executes the method in any of the above embodiments.
[0168] An embodiment of the present specification provides a controller including a first chip and a second chip; wherein the first chip and the second chip are connected via an SPI bus.
[0169] The second chip is configured to send a first side trigger signal based on a second valid data block that needs to be exchanged.
[0170] The second chip is further configured to prepare a second valid data block as a second side data block.
[0171] The first chip is configured to receive a first side trigger signal sent by the second chip, and prepare a first side data block based on the first side trigger signal, where the first side data block includes a valid data block or an invalid data block.
[0172] The first chip is further configured to send the first side data block to the second chip.
[0173] The second chip is further configured to send the second side data block to the first chip.
[0174] In some embodiments, with reference to the Linux network communication framework, the layering is clearer and more specific. When the underlying hardware changes, only the relevant code of the physical layer and the driver layer needs to be adapted, and the upper communication layer does not need to be modified too much. The upper application can choose reliable transmission or unreliable transmission according to the actual situation. In the scenario of reliable transmission, a message response mechanism is introduced. Only messages that have been normally responded to can be removed from the transmission module buffer, otherwise a certain number of retransmission attempts will be required. The upper application can configure multiple priority transmission channels according to the actual situation. The transmission module takes out the messages from the transmission channel in order according to the priority level and sends them in sequence.
[0175] Exemplarily, the communication layer mainly implements the data link module and the transmission module. Since the SOC side uses the Linux operating system, it is also necessary to implement the interface call module to complete the data transmission between the user space and the kernel space. The data link module mainly provides data block caching, data block and frame encapsulation and parsing, frame checksum calculation and inspection, and response frame reply. The data link module's transceiver buffer is used to cache 1280-byte data blocks for sending and receiving. In this solution, there is a data block buffer for both receiving and sending. The data block buffer is a first-in-first-out circular queue with a queue depth tentatively set at 12, which can store 12 1280-byte data blocks at the same time. The driver layer's transceiver buffer will exchange data with the (data link module) transceiver buffer. The data link module encapsulates the data message in the transmission module and the response information in the response information cache list into data frames and response frames respectively, writes them into the data block, fills the remaining space, and then writes the entire data block into the data link module's send buffer.
[0176] For example, regarding data block and frame parsing, the data link module extracts the data block from the receive buffer, parses the data frame and response frame in the data block, discards the padding frame, and then writes the data in the data frame to the transmission module's buffer. The transmission module responds to the parsed response frame (deleting successfully sent messages or retransmitting failed messages).
[0177] The data link module frame format is illustrated as follows. In this example, there are three types of frames: data frame, response frame, and padding frame. The data frame adds 6 bytes of information before the data payload to identify the application layer service to which the data belongs and information related to data segmentation.
[0178] The following table shows how to calculate the CRC value of the data link module message when encapsulating the frame message. The calculation and filling method of the frame header and payload CRC of the data frame, response frame, and padding frame are as follows:
[0179] When parsing a frame message, it is necessary to check the CRC value of the corresponding frame message. The CRC value check process of the frame header and payload of the data frame, response frame, and padding frame is shown in the following table:
[0180] The data link module replies to the response frame as an example. When receiving a data frame message, if the data frame is a frame that requires reliable transmission, then after parsing and processing, whether successful or failed, a response frame needs to be replied to the sender (unreliable transmission data frame message does not require a response frame).
[0181] The transmission module provides reliable and real-time data message transmission services to the application layer and data link module. The transmission module sends data messages to the corresponding priority channel according to the message priority and adds a retransmission mechanism for data messages transmitted on the reliable channel.
[0182] To illustrate the transmission mode, in this solution, the priority 0 channel is reserved for transmitting acknowledgment frames and filler frames. Priority 1 through priority 31 channels are reserved for service data and are used to transmit data packets. Two transmission modes are designed based on the reliability of data packet transmission: reliable and unreliable. The is_reliable field configures which transmission mode is used for priority 1 through priority 31 channels. In unreliable transmission mode, as soon as the transmission module encapsulates the data packet and writes it to the data link module buffer, the data packet is considered successfully transmitted and removed from the corresponding priority channel in the transmission module. In reliable transmission mode, after the transmission module writes the data packet to the data link module buffer, the data packet is not removed from the corresponding priority channel in the transmission module. Further processing must wait for a response frame (ACK / NACK frame) from the receiving end. If an ACK frame is received from the receiving end, the receiving end is considered to have successfully received the data packet and the data packet is removed from the corresponding priority channel in the transmission module. If a NACK frame is received from the receiver, it is assumed that an error occurred when the receiver received the data message. The sender needs to resend the data message. After a certain number of resends, the data message is removed from the corresponding priority channel of the transmission module. If the sender does not receive any response frames from the receiver, it resends the data message every transmission cycle. After a certain number of resends, the data message is removed from the corresponding priority channel of the transmission module. The priority and seq num fields in the response frame can be used to find the corresponding data message in the corresponding priority channel.
[0183] To illustrate priority-based transmission, the transmission module provides 31 data packet transmission channels, ranging from priority 1 to priority 31. Each channel has a corresponding priority. Priorities range from 1 to 31, decreasing in order. The transmission module prioritizes data packets cached in high-priority transmission channels. Data packets with high real-time requirements can be transmitted through high-priority channels.
[0184] As an example, the interface call module is used on the SOC side, which uses the Linux operating system, and requires an additional interface call module to implement data transmission between user space and kernel space. This technical solution uses NetLink communication to implement an interface for sending data from user space to kernel space, and an interface for sending data from kernel space to user space, meeting these data transmission requirements.
[0185] As an example, the application layer implements the segmentation function on both the SOC and MCU sides. This allows for the segmentation and reassembly of longer data. When sending longer data, the data is segmented according to the maximum transmission length, and the segmentation-related fields are filled in. Four fields are required: the segmentation flag (FRAG FLAG), the message number (MSG ID), the number of segments (FRAG CNT), and the segment number (FRAG ID). When receiving segmented data, the application layer caches the segmented data and uses these four fields to confirm whether the segments corresponding to the complete message have been received and whether the complete data can be transmitted to the upper-layer service.
[0186] It should be noted that the logic and / or steps represented in the flowcharts or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing the logical functions, and can be embodied in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus, or device (e.g., a computer-based system, a system including a processor, or other system that can fetch and execute instructions from an instruction execution system, apparatus, or device). For purposes of this specification, a "computer-readable medium" can be any device that can contain, store, communicate, propagate, or transport a program for use by, or in conjunction with, an instruction execution system, apparatus, or device. More specific examples (non-exhaustive list) of computer-readable media include the following: an electrical connection with one or more wires (electronic device), a portable computer disk cartridge (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and programmable read-only memory (EPROM or flash memory), fiber optic devices, and portable compact disc read-only memory (CDROM). Furthermore, the computer-readable medium may even be paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium and then editing, interpreting or processing it in another suitable manner if necessary, and then storing it in a computer memory.
[0187] It should be understood that various parts of the present disclosure can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiments, multiple steps or methods can be implemented using software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented using hardware, as in another embodiment, any one of the following technologies known in the art or a combination thereof can be used to implement: a discrete logic circuit having a logic gate circuit for implementing a logic function on a data signal, an application-specific integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0188] Throughout this specification, reference to terms such as "one embodiment," "some embodiments," "examples," "specific examples," or "some examples" means that a specific feature, structure, material, or characteristic described in conjunction with that embodiment or example is included in at least one embodiment or example of the present disclosure. In this specification, schematic representations of the above terms do not necessarily refer to the same embodiment or example. Furthermore, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples.
[0189] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features being referred to. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one such feature. Throughout the present disclosure, "plurality" means at least two, such as two, three, etc., unless otherwise specifically defined.
[0190] In this disclosure, unless otherwise expressly specified or limited, terms such as "installed," "connected," "connect," and "fixed" should be understood in a broad sense. For example, they may refer to fixed connections, detachable connections, or integration; mechanical connections or electrical connections; direct connections or indirect connections through an intermediate medium; and internal communication between two components or interaction between two components, unless otherwise expressly limited. Those skilled in the art will understand the specific meanings of the above terms in this disclosure based on specific circumstances.
[0191] Although the embodiments of the present disclosure have been shown and described above, it is understood that the above embodiments are illustrative and are not to be construed as limitations on the present disclosure. A person skilled in the art may change, modify, replace and vary the above embodiments within the scope of the present disclosure.
Claims
1. A data transmission method based on SPI, applied to a first device of an SPI communication system, the method comprising: Receiving a first side trigger signal sent by a second device in the communication system based on a second valid data block that needs to be exchanged; Based on the first side trigger signal, prepare a first side data block, wherein the first side data block includes a valid data block or an invalid data block; During a data interaction process, the first side data block is sent to the second device, and a second side data block corresponding to the second valid data block and sent by the second device is received.
2. The method according to claim 1, wherein: The preparing the first side data block based on the first side trigger signal includes: In response to the first side trigger signal, determining whether the first device has a first valid data block that needs to be exchanged; If the first valid data block exists, preparing the first valid data block as the first side data block; If the first valid data block does not exist, an invalid data block is prepared as the first side data block.
3. The method according to claim 2, wherein: The first device has a first sending buffer area and a second sending buffer area; and preparing the first valid data block as the first side data block includes: The first valid data block is written from the first sending buffer area to the second sending buffer area to prepare the first side data block.
4. The method according to claim 3, wherein: The step of preparing the invalid data block as the first side data block comprises: An invalid data block of a preset length is written into the second sending buffer area to prepare the first side data block.
5. The method according to claim 3, wherein: The determining whether the first device has a first valid data block that needs to be exchanged includes: Determining whether there is a first valid data block in the first sending buffer; If the first sending buffer has a first valid data block, determining that the first device has a first valid data block that needs to be exchanged; If the first sending buffer does not contain the first valid data block, it is determined that the first device does not contain the first valid data block that needs to be exchanged.
6. The method according to claim 3, wherein: Before writing the first valid data block from the first sending buffer area to the second sending buffer area, the method further includes: Get the message to be sent; Encapsulating the message into multiple data frames; Writing the plurality of data frames into a data block; The data block is cached in the first sending buffer area.
7. The method according to claim 3, wherein: The first device also has a first receiving buffer area and a second receiving buffer area; The receiving a second side data block corresponding to the second valid data block sent by the second device includes: receiving the second-side data block to the first receiving buffer; The second side data block in the first receiving buffer area is written into the second receiving buffer area.
8. The method according to claim 7, wherein: The method further comprises: When the second-side data block is completely written into the second receiving buffer area, the second sending buffer area and the first receiving buffer area are cleared.
9. The method according to any one of claims 1 to 8, wherein: The first device is an SPI slave device, and the second device is an SPI master device; After preparing the first side data block based on the first side trigger signal, the method further includes: A second side trigger signal is sent to the second device, so that the second device prepares the second valid data block as the second side data block.
10. The method according to claim 9, wherein: In the case where it is determined that the receiving of the second-side data block is completed, the method further includes: Subtract one from the current number of times the trigger signal on the first side is received; In the case where it is determined that the first side trigger signal is received, the method further includes: Increasing the current number of receptions of the first side trigger signal by one; The preparing the first side data block based on the first side trigger signal includes: When the current number of receptions of the first-side trigger signal is greater than 0, a first-side data block is prepared.
11. The method according to claim 9, wherein: The SPI slave device is configured with a first thread and a second thread; The first thread is used to receive the first side trigger signal, prepare the first side data block, send the first side data block and receive the second side data block; The second thread is used for preparing the valid data block as a slave-side data block when the SPI slave device has a valid data block that needs to be exchanged, and sending a master-side trigger signal to the SPI master device after preparing the slave-side data block, so that the SPI master device prepares the master-side data block; The first thread and the second thread are controlled by a mutex lock.
12. The method according to any one of claims 1 to 8, wherein: The first device is an SPI master device, the second device is an SPI slave device, and the second device has a third sending buffer area and a fourth sending buffer area; the first side trigger signal is sent by the second device after writing the second valid data block from the third sending buffer area to the fourth sending buffer area when the second device has a second valid data block that needs to be exchanged.
13. A data transmission method based on SPI, applied to an SPI communication system, the SPI communication system comprising a first device and a second device, the method comprising: The second device sends a first side trigger signal based on a second valid data block that needs to be exchanged; The second device prepares the second valid data block as a second side data block; The first device receives a first side trigger signal sent by the second device, and prepares a first side data block based on the first side trigger signal, wherein the first side data block includes a valid data block or an invalid data block; The first device sends the first side data block to the second device; The second device sends the second side data block to the first device.
14. The method according to claim 13, wherein: The first device is an SPI master device, the second device is an SPI slave device, and the second device has a third sending buffer area and a fourth sending buffer area; The second device prepares the second valid data block as a second side data block, including: The second device writes the second valid data block from the third sending buffer area to the fourth sending buffer area to prepare the second side data block; The second device sends a first-side trigger signal based on a second valid data block to be exchanged, including: The second device sends the first side trigger signal after having a second valid data block to be exchanged and writing the second valid data block into the fourth sending buffer area.
15. A chip comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 12 when executing the computer program.
16. A computer-readable storage medium storing computer instructions or programs, which, when executed on a computer, enable the computer to execute the method according to any one of claims 1 to 12.
17. A controller, comprising a first chip and a second chip; wherein: The first chip is connected to the second chip via an SPI bus; The second chip is configured to send a first side trigger signal based on a second valid data block that needs to be exchanged; The second chip is further configured to prepare the second valid data block as a second side data block; The first chip is configured to receive the first side trigger signal sent by the second chip, and prepare a first side data block based on the first side trigger signal, wherein the first side data block includes a valid data block or an invalid data block; The first chip is further configured to send the first side data block to the second chip; The second chip is further configured to send the second side data block to the first chip.
