SPI-based communication method and computer storage medium
By introducing interrupt signal lines and clock signal mechanisms in SPI communication, the slave nodes allow active initiation of data packets, which solves the problem that only the master device can initiate data packets in existing SPI communication, realizes bidirectional data transmission and correctness, and expands the Application scope.
Patent Information
- Application Number
- CN202410935483.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-12
- Publication Date
- 2024-11-15
AI Technical Summary
In existing SPI communication, only the master device has the ability to initiate data packets, and the slave device cannot initiate data packets actively, which limits the application scope of SPI communication.
By introducing an interrupt signal line in SPI communication, the slave node is allowed to send an interrupt signal to the master node. After the master node responds, the frame header information and frame length information are obtained, the required clock signal is determined, and data transmission is carried out to realize the data packet of the slave node. Initiate function.
It realizes two-way data transmission in SPI communication, expands the application scope of SPI communication, ensures the correctness and integrity of data transmission, and avoids long-term data occupation.
Smart Images

Figure CN118964264A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communications, and in particular to a communication method based on SPI. Background Art
[0002] SPI (Serial Peripheral Interface) communication mechanism is a commonly used serial communication protocol, which is widely used for communication between microcontrollers and various peripheral devices, such as sensors, SD cards and LCD screens. It is a synchronous serial data link standard. SPI plays an important role as a high-speed interface (for example, much higher than ordinary interfaces such as 232, 458, can, i2C, etc.).
[0003] In SPI communication, generally a master device and at least one slave device are required. The master device reads registers or reads and writes data through the SPI interface, such as NOR FLASH and NAND FLASH media of the SPI interface.
[0004] However, in the prior art SPI communication, only the master device has the ability to initiate a data packet, while the slave device does not have the ability to initiate a data packet, thus greatly limiting the application scope of the SPI communication. Summary of the invention
[0005] In view of the above problems in the prior art, the present application provides a communication method based on SPI, which enables the slave node in the SPI communication to also have the ability to initiate data packets, thereby realizing two-way communication of data and expanding the application scope of SPI communication.
[0006] To achieve the above-mentioned purpose, the first aspect of the present application provides a communication method based on SPI, which is applied to a master-slave system, wherein the master-slave system includes a master node and a slave node, and an interrupt signal line is connected between the master node and the slave node; the communication method includes: the master node receives an interrupt signal sent by the slave node through the interrupt signal line, and the interrupt signal is used to instruct the slave node to actively transmit data to the master node; the master node responds to the interrupt signal and sends a first clock signal to the slave node to obtain frame header information and frame length information of the data to be transmitted; the master node determines the second clock signal required for the data to be transmitted based on the frame header information and frame length information, and sends the second clock signal to the slave node, so that the data to be transmitted is transmitted from the slave node to the master node.
[0007] From the above, based on the master-slave system and communication method provided by the present invention, an interrupt signal sent by the slave node is transmitted through the interrupt signal line to inform the master node that the slave node needs to transmit data to the host node, and then the master node obtains the frame header information and frame length information of the data frame to be transmitted by sending a first clock signal to the slave node, so that the master node can know how many bytes of clock signals need to be sent to the slave node to meet the data transmission requirements, and then generate a second clock signal, thereby realizing the transmission of data from the slave node to the master node, and realizing the function of the slave node initiating data transmission, that is, realizing MISO communication.
[0008] As an implementation method of the first aspect, sending the first clock signal to the slave node includes: determining the number of pulses corresponding to the first clock signal according to a predefined frame format; and the master node sending the first clock signal to the slave node according to the number of pulses corresponding to the first clock signal.
[0009] From the above, since the master node does not know the amount of data that the slave node needs to send, the master node determines the number of bytes required to obtain the frame header information and frame length information of the data to be transmitted based on the preset frame format, and then sends the first clock to the slave node according to the number of bytes. When the slave node returns data to the master node, the length of the data to be transmitted can be known.
[0010] As an implementation method of the first aspect, after obtaining the frame header information and frame length information of the data to be transmitted, it also includes: checking the frame header information and frame length information of the data to be transmitted, and if an error occurs in the check, discarding the data and counting the discarding operation; if the check passes, executing the step of determining the second clock signal required for the data to be transmitted.
[0011] From the above, the correctness of frame data transmission can be guaranteed and erroneous data can be eliminated.
[0012] As an implementation method of the first aspect, after the data to be transmitted is transmitted from the slave node to the master node, it also includes: receiving the data to be transmitted; performing CRC check on the received data to be transmitted, and if the check passes, pushing the data to be transmitted into a message queue; if an error occurs in the check, discarding the data and counting the discarding operations.
[0013] From the above, the correctness and integrity of data transmission can be guaranteed.
[0014] As an implementation manner of the first aspect, before sending the first clock signal to the slave node, it also includes: sending a corresponding preamble code to the slave node; wherein different preamble codes represent communications in different directions.
[0015] From the above, the preamble code can be used to distinguish whether it is MOSI communication or MISO communication, so as to avoid the data communication being occupied by the master node or the slave node for a long time.
[0016] As an implementation method of the first aspect, it also includes: after sending the corresponding preamble code to the slave node, executing a first delay time so that the slave node provides the data to be transmitted.
[0017] From the above, by setting the first delay time, time can be reserved for the slave node to prepare data, thereby ensuring the correctness of data transmission.
[0018] As an implementation method of the first aspect, before the master node responds to the interrupt signal, it also includes: after waiting for the communication process being executed by the master node and the slave node to end, the master node responds to the interrupt signal, wherein the communication process being executed by the master node and the slave node includes the communication being executed by the master node to the slave node and the communication being executed by the slave node to the master node.
[0019] Therefore, it is possible to avoid sudden interruption signals from interrupting the existing communication process, making the communication sequence more orderly.
[0020] As an implementation of the first aspect, after the data to be transmitted is transmitted from the slave node to the master node, the method includes: executing a cooling time of a preset duration, during which no communication is performed between the master node and the slave node.
[0021] The second aspect of the present application provides a communication method based on SPI, which is applied to a master-slave system, wherein the master-slave system includes a master node and a slave node, and an interrupt signal line is connected between the master node and the slave node; the communication method includes: checking a cooling flag, if the master node receives an interrupt signal sent by the slave node through the interrupt signal line at a stage when the cooling flag indicates that the master node is not allowed to actively send data to the slave node, the master node executes the communication method described in any one of the first aspects above; if the cooling flag indicates that the master node is allowed to actively send data to the slave node, the master node sends a corresponding preamble code to the slave node to notify the slave node to prepare to receive the data transmitted by the master node; the master node generates data to be transmitted, and generates a third clock signal required for the data to be transmitted; based on the third clock signal, the master node transmits the data to be transmitted to the slave node.
[0022] From the above, since the master node itself has the function of driving the chip select CS and the clock CLK, it can directly send data to the slave node in the stage where the master node is allowed to actively send data to the slave node.
[0023] As an implementation method of the second aspect, after the data to be transmitted is transmitted to the slave node, it includes: starting a cooling mechanism, the cooling mechanism includes: executing a cooling time of a preset duration, setting a cooling flag to indicate that the master node is not allowed to actively send data to the slave node, and during the cooling time, the master node is not allowed to actively send data to the slave node.
[0024] As an implementation method of the second aspect, after executing a cooling time of a preset length, it also includes: modifying the cooling flag so that it changes from indicating that the master node is not allowed to actively send data to the slave node to indicating that the master node is allowed to actively send data to the slave node.
[0025] From the above, by checking the cooling flag and setting a preset duration after each MOSI communication is completed, it is possible to avoid MOSI communication occupying the system for a long time and provide opportunities for MISO communication.
[0026] As an implementation method of the second aspect, it also includes: after the master node sends the corresponding preamble code to the slave node, executing a second delay time so that the master node provides the data to be transmitted.
[0027] From the above, by setting the second delay time, time can be reserved for the master node to prepare data while the slave node is prepared to receive data, thereby ensuring the correctness of data transmission.
[0028] As an implementation manner of the second aspect, the master node transmits the data to be transmitted to the slave node, including: the master node transmits the data to be transmitted to the slave node by direct memory access.
[0029] From the above, the direct memory access method can reduce data transmission time and improve data processing efficiency.
[0030] The third aspect of the present application provides an SPI-based communication device, which includes a receiving module, a response module, and a clock signal sending module, wherein the receiving module is used to receive an interrupt signal sent by the slave node through the interrupt signal line, and the interrupt signal is used to instruct the slave node to actively transmit data to the master node; the response module is used to respond to the interrupt signal and send a first clock signal to the slave node to obtain frame header information and frame length information of the data to be transmitted; the clock signal sending module is used to determine a second clock signal required for the data to be transmitted based on the frame header information and frame length information, and send the second clock signal to the slave node, so that the data to be transmitted is transmitted from the slave node to the master node.
[0031] The beneficial effects of this aspect can be found in the description of the beneficial effects of each part of the first aspect above.
[0032] The fourth aspect of the present application provides a communication device based on SPI, and the communication device based on SPI includes a checking module, a clock signal generating module and a transmission module. The checking module is used for, if the cooling mark indicates that the master node is not allowed to actively send data to the slave node, when the master node receives an interrupt signal sent by the slave node through the interrupt signal line, the master node executes the communication method described in any one of the above-mentioned first aspects; if the cooling mark indicates that the master node is allowed to actively send data to the slave node, the master node sends a corresponding preamble code to the slave node to notify the slave node to prepare to receive the data transmitted by the master node; the clock signal generating module is used to generate the data to be transmitted and generate the third clock signal required for the data to be transmitted; the transmission module is used for, based on the third clock signal, the master node transmits the data to be transmitted to the slave node.
[0033] The beneficial effects of this aspect can be found in the description of the beneficial effects of each part of the first aspect above.
[0034] The fifth aspect of the present application provides a computing device, comprising: at least one processor; and at least one memory, which is connected to the processor and stores program instructions, and when the program instructions are executed by the at least one processor, the at least one processor executes the SPI-based communication method described in any one of the first aspect or the second aspect.
[0035] The beneficial effects of this aspect can also be found in the description of the beneficial effects of each part of the first aspect above.
[0036] A sixth aspect of the present application provides a computer-readable storage medium having program instructions stored thereon, wherein when the program instructions are executed by a computer, the computer executes the method described in any one of the first aspect or the second aspect.
[0037] The beneficial effects of this aspect can also be found in the description of the beneficial effects of each part of the first aspect above. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] The various technical features of the present application and the relationship between them are further explained below with reference to the accompanying drawings. The accompanying drawings are exemplary, and some technical features are not shown in actual proportion. In addition, some drawings may omit technical features that are commonly used in the technical field to which the present application belongs and are not essential for understanding and implementing the present application, or additional technical features that are not essential for understanding and implementing the present application may be shown. In other words, the combination of the various technical features shown in the accompanying drawings is not used to limit the present application. In addition, throughout the present application, the same figure numbers refer to the same content. The specific description of the drawings is as follows:
[0039] Figure 1 A schematic diagram of the structure of a master-slave system provided in an embodiment of the present application;
[0040] Figure 2 A flowchart of a MISO communication method provided in the first embodiment of the present application;
[0041] Figure 3 A flowchart of a MOSI communication method provided in the first embodiment of the present application;
[0042] Figure 4 A flowchart of a MISO communication method provided in the second embodiment of the present application;
[0043] Figure 5 A flowchart of a MOSI communication method provided in the second embodiment of the present application;
[0044] Figure 6 A receiving schematic diagram of a MISO communication device provided in the third embodiment of the present application;
[0045] Figure 7 A receiving schematic diagram of a MOSI communication device provided in the third embodiment of the present application;
[0046] Figure 8 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0047] The technical solution provided by the present application is further described below with reference to the accompanying drawings and examples. It should be understood that the system structure and business scenarios provided in the examples of the present application are mainly to illustrate the possible implementation methods of the technical solution of the present application and should not be interpreted as the only limitation on the technical solution of the present application. It is known to those of ordinary skill in the art that with the evolution of the system structure and the emergence of new business scenarios, the technical solution provided by the present application is also applicable to similar technical problems.
[0048] It should be understood that the embodiments of the present application provide a communication solution based on SPI. Since the principles of solving the problems in these technical solutions are the same or similar, some repetitions may not be repeated in the following specific embodiments, but it should be regarded as that these specific embodiments have been referenced to each other and can be combined with each other.
[0049] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art of the present application. In case of any inconsistency, the meaning described in this specification or the meaning derived from the contents recorded in this specification shall prevail. In addition, the terms used herein are only for the purpose of describing the embodiments of the present application and are not intended to limit the present application.
[0050] The SPI-based communication scheme provided in this application can be applied to the field of data exchange between microcontrollers and peripheral devices, which may include but are not limited to sensor devices, display devices, storage devices, etc.; it can also be applied to network devices, where SPI is used to connect network interfaces, such as Wi-Fi modules or Ethernet controllers, allowing microcontrollers to manage network communications through SPI; it can also be applied to the field of industrial control, where SPI can be used to control and monitor various industrial sensors and actuators, such as temperature control systems and motor controllers, etc.; it can also be applied to the automotive field, where SPI is used to read on-board sensor data or control various electronic devices inside the car, such as dashboards, air conditioning control systems, etc.; it can also be applied to the field of robotics, where SPI may be used to connect and control multiple sensors and actuators to ensure efficient data communication and precise motion control, etc. Through the SPI-based communication scheme provided in this application, bidirectional communication of high-speed protocols can be achieved on traditional SPI bus interfaces.
[0051] It should be understood that the above application scenarios are exemplary descriptions and are not intended to limit the scope of this application.
[0052] The following is a detailed introduction to a communication method based on SPI provided in the first embodiment of the present application. In this embodiment, the communication method is mainly used in a master-slave system.
[0053] In the traditional SPI master-slave communication architecture, the master node and the slave node are generally connected through a four-wire interface (CLK, MOSI, MISO, and CS). Specifically: CLK: clock line, controlled by the master device. MOSI: data line from the master node to the slave node. MISO: data line from the slave node to the master node. CS: slave node selection line, controlled by the master device, used to activate a specific slave node. In this embodiment, Figure 1 As shown, in order to realize SPI bidirectional communication, an interrupt pin GPIO_INT needs to be added on the basis of the traditional four-wire interface. The CPU is the master node of the system, and the FPGA is the slave node of the system. When the FPGA has data that needs to be actively sent to the CPU, an interrupt signal is sent through GPIO_INT to notify the CPU. As an implementation method, the falling edge of the interrupt signal is effective.
[0054] The structure of the master-slave system provided in this embodiment can realize bidirectional communication based on SPI, that is, MISO communication and MOSI communication can be realized, wherein MISO communication refers to a communication mode in which a slave node actively initiates a data packet; MOSI communication refers to a communication mode in which a master node actively initiates a data packet. Next, the implementation process of bidirectional communication is introduced respectively.
[0055] First combine Figure 2The MISO communication is introduced in detail, and the MISO communication includes steps S210-S280:
[0056] S210: The master node receives the communication request of MISO.
[0057] In this step, when the slave node needs to actively send data to the master node, it will first generate a pulse signal (a low pulse in this embodiment) on the interrupt pin GPIO_INT to inform the master node that it needs to send data. At this time, the master node receives the interrupt signal from the slave node through the interrupt pin GPIO_INT, that is, receives the communication request of MISO, and the master node begins to prepare to provide the SPI clock. Among them, the low pulse signal in this embodiment can be a low pulse signal of 200ns.
[0058] S220: The master node sends a preamble code to the slave node.
[0059] In this embodiment, in order to prevent data communication from being occupied by the master node or the slave node for a long time, and to give the slave node a certain amount of data preparation time, a preamble is required before each communication to distinguish whether the communication is MOSI communication or MISO communication. For example, 0x63 represents MISO communication, and 0x9C represents MOSI communication. In this embodiment, the preamble occupies 1 byte. It should be understood that the preamble 0x63 and the preamble 0x9C are both exemplary descriptions, and in other embodiments, other forms of preambles can be used to distinguish MOSI communication from MISO communication.
[0060] In this embodiment, after the master node sends the preamble code 0x63 to the slave node, it informs the slave node that the master node has received the interrupt signal and asks the slave node to prepare the data to be transmitted.
[0061] S230: Execute the first delay time.
[0062] As an implementation manner, the first delay time is set to 1 μs.
[0063] In this step, the first delay time is used to allow the slave node to prepare data to be transmitted.
[0064] It should be noted that step S220 and step S230 are both optional steps, that is, in some embodiments, step S240 may be directly executed without step S220 and step S230.
[0065] S240: The master node sends a clock signal of a corresponding number of bytes to the slave node according to a predefined data frame format, and the master node receives corresponding frame header information and frame length information returned by the slave node.
[0066] Next, a predefined frame format provided by this embodiment is introduced, as shown in Table 1a and Table 1b below. It should be understood that the frame format is not unique and can be redefined in other embodiments.
[0067] Table 1a
[0068] 0xEB90 Frame length (2 bytes) payload CRC16
[0069] Table 1b
[0070]
[0071] Referring to Table 1a and Table 1b above, the frame header of the predefined frame format is a fixed value 0xEB90, which is used to indicate the start of the data frame. The number of bytes of the frame header is 2, and the data type is UINT16 (unsigned 16-bit integer); the frame length field of the predefined frame format indicates the length of the entire data frame, but does not include the frame length field itself and the CRC16 check code. The frame length field allows the receiving device to obtain the length of the data frame to complete the transmission of the entire data frame. The number of bytes of the frame length field is 2, and the data type is UINT16; the payload of the predefined frame format indicates the actual data content, which may include any information. The length N of this part is variable, depending on the amount of data transmitted. The number of bytes of the payload is N, and the data type is UINT8 (unsigned 8-bit integer); the CRC16 of the predefined frame format is an error check code, which is used to check whether an error occurs in the data frame during transmission. It is obtained by calculating the frame header, frame length and content parts of the frame, thereby verifying the integrity and correctness of the data. The number of bytes of the CRC16 is 2, and the data type is UINT16.
[0072] In this step, because the master node does not know the amount of data to be sent by the slave node, therefore, based on the above-mentioned predefined data frame format, the master node first sends a 4-byte clock to the slave node to obtain the frame header information and frame length information of the data frame to be transmitted. Here, the master node sends a 4-byte clock to the slave node as an arbitrary value, for example, 4 bytes of 00 data can be sent. Next, the slave node returns a 4-byte clock to the master node, 2 bytes of which represent the frame header information of the data frame to be transmitted, and another 2 bytes represent the frame length information of the data frame to be transmitted. Based on the 4-byte clock returned by the slave node received, the master node can now know the frame header information and frame length information of the data frame to be transmitted, and according to the frame length information, it can be determined that the clock of how many bytes needs to be provided to the slave node can meet the transmission requirement.
[0073] S250: The master node verifies the received frame header information and frame length information.
[0074] When the master node receives the frame header information and frame length information, it will first check the frame header and frame length to confirm that it is a valid data frame. If either the frame header information or the frame length information is wrong, the data frame will be discarded and counted; if both the frame header information and the frame length information are correct, the next step will be executed.
[0075] It should be noted that step S250 is an optional step, that is, in some embodiments, step S260 may be directly executed without step S250.
[0076] S260: The master node sends the clock of the remaining bytes to the slave node.
[0077] Based on the frame length information returned by the slave node to the master node in step S240, it can be determined how many bytes of clock the master node needs to provide to the slave node to meet the transmission requirements. As an implementation method, the master node can send a clock of any value to the slave node, for example, all bytes send 00 data, and after the slave node receives the clock of the remaining bytes sent by the master node, the slave node returns the pre-prepared data frame to be transmitted to the master node.
[0078] It should be understood that the process of actively sending the data packet from the slave node to the master node has actually been completed by step S260, but in order to ensure the correctness and integrity of data transmission, this embodiment may also include steps S270 and S280.
[0079] S270: The master node performs CRC check on the received data frame.
[0080] If the CRC check passes, step S280 is executed. If an error occurs in the CRC check, it means that at least part of the content of the data frame is erroneous, so the data frame needs to be discarded.
[0081] S280: Push the received data that has passed the CRC check into the message queue and wait for data processing.
[0082] At this point, the process of actively sending data from the slave node to the master node (MISO communication) is completed. It should be understood that steps S270 to S280 are optional steps, that is, in some embodiments, steps S270 to S280 may not be included, and the communication is terminated directly at step S260.
[0083] Based on the method provided in this embodiment, the interrupt signal of the interrupt pin GPIO_INT is used to inform the master node that the slave node needs to transmit data to the host node, and then the master node inquires the slave node for the frame header and frame length information according to the preset data frame format, so that the master node can know how many bytes of clock signals need to be sent to the slave node for data transmission, thereby realizing data transmission from the slave node to the master node and realizing MISO communication.
[0084] Next, combine Figure 3 The MOSI communication is described in detail. The MOSI communication includes steps S310-S360:
[0085] S310: Check cooling mark.
[0086] In this step, when the cooling flag meets the preset conditions, for example, when it is displayed as the second value, the master node is allowed to send data to the slave node (i.e., MOSI is allowed). In this embodiment, when the master node is sending data to the slave node, or the master node has completed sending data to the slave node, but the preset time length (set to 1ms in this embodiment) has not passed, the cooling flag is displayed as the first value, indicating that MOSI communication is not allowed between the master node and the slave node. When the master node has completed sending data to the slave node, after the preset time length has passed, the cooling flag is modified from the first value to the second value, that is, it is displayed as the second value at this time, indicating that MOSI communication is allowed between the master node and the slave node.
[0087] As an implementation manner, the first value of the cooling flag is 0, and the second value of the cooling flag is 1, that is, before each MOSI communication, it is necessary to check whether the cooling flag is 1. When the cooling flag is 1, MOSI communication can be performed.
[0088] S320: The master node sends a preamble code to the slave node.
[0089] In this step, when the cooling flag is the second value (the second value in this embodiment is 1), the master node sends a preamble code to the slave node.
[0090] It should be understood that when the master node sends a preamble code to the slave node, MOSI communication has already started, so no new communication process is allowed in the system at this time. Therefore, the cooling flag needs to be modified to the first value (the first value of this embodiment is 0).
[0091] Referring to the description of step S220 in the above embodiment, the preamble code corresponding to the MOSI communication in this embodiment is 0x9C. Therefore, at this time, the master node sends the preamble code 0x9C to the slave node to inform the slave node that it is ready to receive the data transmitted by the master node.
[0092] It should be understood that step S320 is an optional step.
[0093] S330: Execute the second delay time.
[0094] As an implementation manner, the second delay time is set to 1 μs.
[0095] In this step, the second delay time is used to allow the slave node to prepare for receiving data.
[0096] It should be understood that step S330 is an optional step.
[0097] S340: The master node generates a service data frame.
[0098] S350: The master node sends the service data frame to the slave node through direct memory access (DMA).
[0099] It should be understood that, at this step, the process of sending data from the master node to the slave node has been completed. In order to reserve time for MISO communication, step S360 may also be included.
[0100] S360: Start the cooling process mechanism.
[0101] It should be understood that the cooling mechanism here is that after the master node and the slave node end the MOSI communication, a preset time length is required to be paused for the MISO communication to be executed, so the cooling flag here is still the first value in step S320 (i.e., MOSI communication is prohibited). Under this cooling processing mechanism, MOSI communication is prohibited and only MISO communication is allowed. Therefore, based on this embodiment, the slave node can have the opportunity to initiate MISO communication.
[0102] It should be understood that step S360 is an optional step.
[0103] Based on the MOSI communication provided in this embodiment, since the master node itself has the function of driving the chip select CS and the clock CLK, the communication process can directly send data from the master node to the slave node without going through the complex process of MISO communication.
[0104] From the above, the SPI-based communication method provided in this embodiment can realize both MOSI communication and MISO communication, thereby realizing the bidirectional communication function of SPI.
[0105] The second embodiment of the present application provides a communication method based on SPI, which is applied to the master-slave system provided by the first embodiment. Figure 4-Figure 5 This communication method is introduced in detail.
[0106] like Figure 4 As shown, the SPI communication method includes MISO communication, and the MISO communication includes steps S410 to S430:
[0107] S410: The master node receives an interrupt signal sent by the slave node through an interrupt signal line, where the interrupt signal is used to instruct the slave node to actively transmit data to the master node.
[0108] In this step, the interrupt signal line is a signal line connecting the master node and the slave node, and is used to transmit an interrupt signal, where the interrupt signal represents a MISO communication request.
[0109] In this step, when executing MISO communication, that is, when the slave node needs to actively send data to the master node, a pulse signal will first be generated on the interrupt pin GPIO_INT to inform the master node that it needs to send data. At this time, the master node receives the interrupt signal from the slave node through the interrupt pin GPIO_INT, that is, it receives the MISO communication request.
[0110] S420: The master node responds to the interrupt signal and sends a first clock signal to the slave node to obtain frame header information and frame length information of the data to be transmitted.
[0111] In this step, the master node needs to wait for the communication process being executed to end in response to the interrupt signal to avoid interfering with the communication being executed. The communication process being executed by the master node and the slave node includes the master node actively transmitting data to the slave node and the slave node actively transmitting data to the master node.
[0112] In this step, the master node sends the first clock signal to the slave node, including: first obtaining a predefined data frame format, which can refer to the frame formats shown in Table 1a and Table 1b in step S240 of the first embodiment above, and will not be repeated here.
[0113] Since the master node does not know the amount of data to be sent by the slave node, based on the above-defined data frame format, the master node first sends a 4-byte first clock to the slave node to obtain the frame header information and frame length information of the data frame to be transmitted (wherein the frame header is 2 bytes and the frame length is 2 bytes). Next, the slave node returns a 4-byte clock to the master node, wherein 2 bytes of the returned 4-byte clock represent the frame header information of the data frame to be transmitted, and the other 2 bytes represent the frame length information of the data frame to be transmitted. Based on the received 4-byte clock returned by the slave node, the master node can now obtain the frame header information and frame length information of the data frame to be transmitted.
[0114] S430: The master node determines a second clock signal required for the data to be transmitted according to the frame header information and the frame length information, and sends the second clock signal to the slave node so that the data to be transmitted is transmitted from the slave node to the master node.
[0115] In this step, the number of bytes of the clock signal required to transmit the data to be transmitted can be determined based on the frame length information of the data to be transmitted returned by the slave node, and then a second clock is sent to the slave node according to the determined number of bytes, and the second clock can be an arbitrary value. Based on the second clock sent by the master node, the slave node can transmit the data to be transmitted to the master node, thereby completing the MISO communication.
[0116] It should be understood that between steps S410 and S420 of this embodiment, the master node may include sending a preamble to the slave node and executing a first delay time. In this step, the master node sends a preamble 0x63 to the slave node to indicate MISO communication. After the master node sends the preamble 0x63 to the slave node, the master node is informed that the master node has received the interrupt signal and the slave node is ready to transmit data. Then, after waiting for the first delay time, step S420 is executed. It should be understood that the first delay time here is for the slave node to prepare the data to be transmitted.
[0117] Between steps S420-S430 of this embodiment, a step of verifying the frame header information and the frame length information may also be included. Through this verification, it can be confirmed whether the data frame is a valid data frame. If either the frame header information or the frame length information is erroneous, the data frame is discarded and statistics are performed.
[0118] After step S430 of this embodiment, a step of performing CRC check on the data to be transmitted may also be included. If an error occurs in the CRC check, it means that at least part of the content of the data frame has an error, so the data frame needs to be discarded.
[0119] like Figure 5 As shown, the SPI communication method includes MOSI communication, and the MOSI communication includes steps S510 to S520:
[0120] S510a: Checking the cooling flag, if the cooling flag indicates that the master node is not allowed to actively send data to the slave node, when the master node receives an interrupt signal sent by the slave node through the interrupt signal line, the master node executes the MISO communication provided in the above embodiment;
[0121] S510b: If the cooling flag indicates that the master node is allowed to actively send data to the slave node, the master node sends a corresponding preamble code to the slave node to notify the slave node to prepare to receive the data transmitted by the master node.
[0122] As an implementation method, for example, when the cooling mark is displayed as the second value, the master node is allowed to actively send data to the slave node, that is, MOSI communication is allowed. When the master node is sending data to the slave node, or the master node has completed sending data to the slave node, but the preset time has not passed, the cooling mark is displayed as the first value, indicating that MOSI communication is not allowed between the master node and the slave node. After the master node has completed sending data to the slave node, the cooling mechanism is started, and the cooling mechanism includes: executing a cooling time of a preset time, setting a cooling mark to indicate that the master node is not allowed to actively send data to the slave node, and during the cooling time, the master node is not allowed to actively send data to the slave node. After the cooling time of the preset time is executed, the cooling mark is modified from indicating that the master node is not allowed to actively send data to the slave node to indicating that the master node is allowed to actively send data to the slave node (in this embodiment, the cooling mark is modified from the first value to the second value). At this time, it means that MOSI communication is allowed between the master node and the slave node. In this embodiment, when the cooling mark is the second value, MOSI communication starts: the master node sends a preamble to the slave node.
[0123] It should be understood that when the master node sends the preamble code to the slave node, MOSI communication has already started, so no new communication process is allowed to be performed in the system at this time, so the cooling flag needs to be modified to the first value at this time.
[0124] S520: The master node generates data to be transmitted, and generates a third clock signal required by the data to be transmitted.
[0125] In this step, since the master node itself has the ability to drive the chip select CS and the clock CLK, and the master node knows the length of the data to be sent, the master node can directly generate the data to be transmitted and the required third clock signal.
[0126] It should be understood that between steps S510 and S520, a step of executing a second delay time may also be included so that the master node can prepare the data to be transmitted and the slave node can prepare to receive the data.
[0127] S530: Based on the third clock signal, the master node transmits the data to be transmitted to the slave node.
[0128] As an implementation method, the data to be transmitted can be transmitted to the slave node in a DMA manner, thereby completing MOSI communication.
[0129] In this embodiment, step S530 may be followed by a step of starting a cooling mechanism, where the cooling mechanism includes: executing a cooling time of a preset length, during which the master node is not allowed to send data to the slave node.
[0130] It should be understood that the cooling mechanism here is the preset duration required to pause after the MOSI communication ends for the slave node to initiate MISO communication. Here, the cooling flag is still the first value (i.e., the flag that the master node and the slave node cannot perform MOSI communication).
[0131] Under this cooling process mechanism, MOSI communication is prohibited and only MISO communication is allowed. Therefore, based on this embodiment, the slave node can have the opportunity to initiate MISO communication.
[0132] The third embodiment of the present application provides a communication device 60 based on SPI. It should be understood that: Figure 6 This is only a schematic diagram of a structure of a communication device based on SPI, and the present application does not limit the division of functional modules in the communication device based on SPI. Figure 6 As shown, the device 60 can be logically divided into multiple modules, each module can have different functions, and the function of each module can be implemented by a processor in a computing device reading and executing instructions in a memory. Exemplarily, the device 60 includes a receiving module 610, a response module 620, and a clock signal sending module 630. In one implementation, the SPI-based communication device 60 is used to perform Figure 2 or Figure 4 Any of the contents shown.
[0133] Specifically: the receiving module 610 is used to receive an interrupt signal sent by the slave node through the interrupt signal line, and the interrupt signal is used to instruct the slave node to actively transmit data to the master node. The response module 620 is used to respond to the interrupt signal and send a first clock signal to the slave node to obtain the frame header information and frame length information of the data to be transmitted. The clock signal sending module 630 is used to determine the second clock signal required for the data to be transmitted according to the frame header information and frame length information, and send the second clock signal to the slave node, so that the data to be transmitted is transmitted from the slave node to the master node.
[0134] The specific implementation of each functional module in this embodiment can refer to the introduction of MISO communication in the first embodiment and the second embodiment above, which will not be described in detail in this embodiment.
[0135] The specific implementation of each functional module in this embodiment can refer to the introduction of MISO communication in the first embodiment and the second embodiment above, which will not be described in detail in this embodiment.
[0136] This embodiment provides a second SPI-based communication device 70. It should be understood that: Figure 7This is only a schematic diagram of a structure of the second SPI-based communication device, and the present application does not limit the division of functional modules in the SPI-based communication device. Figure 7 As shown, the device 70 can be logically divided into multiple modules, each module can have different functions, and the function of each module can be implemented by a processor in a computing device reading and executing instructions in a memory. Exemplarily, the device 70 includes a checking module 710, a clock signal generating module 720, and a transmission module 730. In one implementation, the SPI-based communication device 60 is used to perform Figure 3 or Figure 5 Any of the contents shown.
[0137] Specifically: the checking module 710 is used for, if the cooling mark indicates that the master node is not allowed to actively send data to the slave node, when the master node receives the interrupt signal sent by the slave node through the interrupt signal line, the master node executes the MISO communication method described in the above embodiment; if the cooling mark indicates that the master node is allowed to actively send data to the slave node, the MOSI communication described in the above embodiment is executed, specifically: the master node sends the corresponding preamble code to the slave node to notify the slave node to prepare to receive the data transmitted by the master node. The clock signal generating module 720 is used to generate the data to be transmitted and generate the third clock signal required for the data to be transmitted. The transmission module 730 is used for the master node to transmit the data to be transmitted to the slave node based on the third clock signal.
[0138] The specific implementation of each functional module in this embodiment can refer to the introduction of MOSI communication in the first embodiment and the second embodiment above, which will not be described in detail in this embodiment.
[0139] The specific implementation of each functional module in this embodiment can refer to the introduction of MOSI communication in the first embodiment and the second embodiment above, which will not be described in detail in this embodiment.
[0140] Figure 8 900 is a schematic structural diagram of a computing device 900 provided in an embodiment of the present application. The computing device can execute each optional embodiment of the above-mentioned SPI-based communication method. The computing device can be a terminal, or a chip or chip system inside the terminal. Figure 8 As shown, the computing device 900 includes: a processor 910 , a memory 920 , and a communication interface 930 .
[0141] It should be understood that Figure 8 The communication interface 930 in the computing device 900 shown may be used to communicate with other devices, and may specifically include one or more transceiver circuits or interface circuits.
[0142] The processor 910 may be connected to a memory 920. The memory 920 may be used to store the program code and data. Therefore, the memory 920 may be a storage unit inside the processor 910, or an external storage unit independent of the processor 910, or a component including a storage unit inside the processor 910 and an external storage unit independent of the processor 910.
[0143] Optionally, the computing device 900 may further include a bus. The memory 920 and the communication interface 930 may be connected to the processor 910 via the bus. The bus may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 8 A line without an arrow is used to represent the bus, but this does not mean that there is only one bus or one type of bus.
[0144] It should be understood that in the embodiment of the present application, the processor 910 may adopt a central processing unit (CPU). The processor may also be other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. Alternatively, the processor 910 may adopt one or more integrated circuits to execute relevant programs to implement the technical solutions provided in the embodiment of the present application.
[0145] The memory 920 may include a read-only memory and a random access memory, and provides instructions and data to the processor 910. A portion of the processor 910 may also include a nonvolatile random access memory. For example, the processor 910 may also store information on the device type.
[0146] When the computing device 900 is running, the processor 910 executes the computer-executable instructions in the memory 920 to perform any operation step of the above method and any optional embodiment thereof.
[0147] It should be understood that the computing device 900 according to the embodiment of the present application can correspond to the corresponding subjects in the methods according to the embodiments of the present application, and the above-mentioned and other operations and / or functions of each module in the computing device 900 are respectively for implementing the corresponding processes of each method of the present embodiment. For the sake of brevity, they will not be repeated here.
[0148] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0149] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0150] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0151] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0152] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0153] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0154] An embodiment of the present application also provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, it is used to execute the above method, which includes at least one of the solutions described in the above embodiments.
[0155] The computer storage medium of the embodiment of the present application can adopt any combination of one or more computer-readable media. Computer-readable media can be computer-readable signal media or computer-readable storage media. Computer-readable storage media can be, for example, but not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or devices, or any combination of the above. More specific examples (non-exhaustive lists) of computer-readable storage media include: electrical connections with one or more wires, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the above. In this document, computer-readable storage media can be any tangible medium containing or storing programs, which can be used by instruction execution systems, devices or devices or used in combination with them.
[0156] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0157] The program code embodied on the computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
[0158] Computer program code for performing the operation of the present application can be written in one or more programming languages or a combination thereof, including object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as an independent software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (e.g., using an Internet service provider to connect through the Internet).
[0159] In addition, the words "first, second, third, etc." or module A, module B, module C and other similar terms in the specification and claims are only used to distinguish similar objects and do not represent a specific ordering of the objects. It can be understood that the specific order or sequence can be interchanged where permitted so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.
[0160] In the above description, the numbers representing the steps, such as S110, S120, etc., do not necessarily mean that the steps will be executed in this manner. If permitted, the order of the steps can be interchanged or they can be executed simultaneously.
[0161] The term "comprising" as used in the description and claims should not be interpreted as being limited to what is listed thereafter; it does not exclude other elements or steps. Therefore, it should be interpreted as specifying the presence of the features, integers, steps or components mentioned, but does not exclude the presence or addition of one or more other features, integers, steps or components and groups thereof. Therefore, the expression "a device comprising means A and B" should not be limited to a device consisting of components A and B only.
[0162] References to "one embodiment" or "an embodiment" in this specification mean that a particular feature, structure, or characteristic described in conjunction with the embodiment is included in at least one embodiment of the present application. Therefore, the phrases "in one embodiment" or "in an embodiment" appearing in various places in this specification do not necessarily refer to the same embodiment, but may refer to the same embodiment. In addition, in one or more embodiments, the particular features, structures, or characteristics can be combined in any appropriate manner, as would be apparent to one of ordinary skill in the art from this disclosure.
[0163] Note that the above are only preferred embodiments of the present application and the technical principles used. Those skilled in the art will understand that the present application is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present application. Therefore, although the present application is described in more detail through the above embodiments, the present application is not limited to the above embodiments, and may also include more other equivalent embodiments without departing from the concept of the present application, all of which belong to the scope of protection of the present application.
Claims
1. A communication method based on SPI, characterized in that, The communication method is applied to a master-slave system, the master-slave system includes a master node and a slave node, and an interrupt signal line is connected between the master node and the slave node; the communication method includes: The master node receives an interrupt signal sent by the slave node through the interrupt signal line, wherein the interrupt signal is used to instruct the slave node to actively transmit data to the master node; The master node responds to the interrupt signal and sends a first clock signal to the slave node to obtain frame header information and frame length information of the data to be transmitted; The master node determines the second clock signal required for the data to be transmitted according to the frame header information and the frame length information, and sends the second clock signal to the slave node so that the data to be transmitted is transmitted from the slave node to the master node.
2. The method according to claim 1, characterized in that The sending a first clock signal to the slave node comprises: determining the number of pulses corresponding to the first clock signal according to a predefined frame format; The master node sends the first clock signal to the slave node according to the number of pulses corresponding to the first clock signal.
3. The method according to claim 1, characterized in that After obtaining the frame header information and frame length information of the data to be transmitted, the method further includes: The frame header information and frame length information of the data to be transmitted are checked. If an error occurs in the check, a discard operation is performed and statistics are collected on the discard operation; if the check passes, a step of determining a second clock signal required for the data to be transmitted is executed.
4. The method according to claim 1, characterized in that: After the data to be transmitted is transmitted from the slave node to the master node, the method further includes: receiving the data to be transmitted; The received data to be transmitted is subjected to CRC check. If the check passes, the data to be transmitted is pushed into the message queue; if the check fails, the data is discarded and statistics are collected on the discarded data.
5. The method according to claim 1, characterized in that Before sending the first clock signal to the slave node, the method further includes: A corresponding preamble code is sent to the slave node; wherein different preamble codes represent communications in different directions.
6. The method according to claim 5, characterized in that Also includes: After sending the corresponding preamble code to the slave node, a first delay time is performed to allow the slave node to provide the data to be transmitted.
7. The method according to claim 1, characterized in that Before the master node responds to the interrupt signal, the method further includes: After waiting for the communication process being executed by the master node to the slave node to end, and waiting for the communication process being executed by the slave node to the master node to end, the master node responds to the interrupt signal.
8. The method according to claim 1, characterized in that After the data to be transmitted is transmitted from the slave node to the master node, the method comprises: A cooling time of a preset length is executed, during which no communication is performed between the master node and the slave node.
9. A communication method based on SPI, characterized in that: The communication method is applied to a master-slave system, the master-slave system includes a master node and a slave node, and an interrupt signal line is connected between the master node and the slave node; the communication method includes: Checking a cooling flag, if the master node receives an interrupt signal sent by the slave node through the interrupt signal line at a stage where the cooling flag indicates that the master node is not allowed to actively send data to the slave node, the master node executes the communication method according to any one of claims 1 to 8; If the cooling flag indicates that the master node is allowed to actively send data to the slave node, the master node sends a corresponding preamble code to the slave node to notify the slave node to prepare to receive the data transmitted by the master node; The master node generates data to be transmitted, and generates a third clock signal required by the data to be transmitted; Based on the third clock signal, the master node transmits the data to be transmitted to the slave node.
10. The method according to claim 9, characterized in that After transmitting the data to be transmitted to the slave node, the method further comprises: A cooling mechanism is started, the cooling mechanism comprising: executing a cooling time of a preset length, setting a cooling flag to indicate that the master node is not allowed to actively send data to the slave node, and during the cooling time, the master node is not allowed to actively send data to the slave node.
11. The method according to claim 10, characterized in that After the preset cooldown period, it also includes: The cooling flag is modified so that it changes from indicating that the master node is not allowed to actively send data to the slave node to indicating that the master node is allowed to actively send data to the slave node.
12. The method according to claim 9, characterized in that Also includes: After the master node sends the corresponding preamble code to the slave node, a second delay time is performed to allow the master node to provide the data to be transmitted.
13. The method according to claim 9, characterized in that The master node transmits the data to be transmitted to the slave node, including: The master node transmits the data to be transmitted to the slave node by direct memory access.
14. A computer-readable storage medium, characterized in that: Program instructions are stored thereon, and when the program instructions are executed by a computer, the computer is caused to execute the method according to any one of claims 1 to 13.
