Master-slave communication method and system and storage medium

By building preset communication interfaces and protocols in the serial peripheral interface, using level state synchronization and flow control of communication trigger line and synchronization line, the real-time and reliability problems in SPI communication are solved, and efficient master-slave is achieved machine data transmission.

CN119066010APending Publication Date: 2024-12-03ALLWINNER TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202310646497.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-06-01
Publication Date
2024-12-03

AI Technical Summary

Technical Problem

Serial peripheral interface (SPI) has real-time and data transmission reliability problems in master-slave communication. It is mainly because the data transmission of communication slaves needs to be polled and queried by the host, resulting in limited real-time and high throughput. Data transmission is prone to instability problems during transmission.

Method used

By building a preset communication interface, including a communication trigger line, a communication synchronization line and a preset protocol interface, the level state is set according to the working state of the slave, and the host interruption and synchronization are triggered, thereby real-time and reliability of master-slave communication. . The specific steps include setting the level status of the communication trigger line and the synchronization line, performing communication operations through the preset protocol interface, receiving and sending data in a timely manner, and performing flow control and data verification through the counter to be received and the preset protocol.

Benefits of technology

It effectively improves the real-time nature of master-slave communication and data transmission reliability, alleviates the real-time problems caused by polling and reading and data abnormalities in high-throughput transmission, and ensures the stability and efficiency of communication.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119066010A_ABST
    Figure CN119066010A_ABST
Patent Text Reader

Abstract

The invention discloses a master-slave communication method and system and a storage medium, which are applied to the technical field of communication and can realize master-slave communication with relatively high real-time performance and effectively improve the reliability of data transmission. The method comprises the following steps: constructing a preset communication interface; wherein the preset communication interface comprises a communication trigger line, a communication synchronization line and a preset protocol interface; the communication trigger line is used for triggering the communication host to interrupt and receiving data sent by the communication slave; the communication synchronization line is used for state synchronization of the communication host and the communication slave; setting preset level states of the communication trigger line and the communication synchronization line according to the working state of the communication slave; the communication host determines the working state of the communication slave according to the preset level state; performing corresponding master-slave communication operation through a preset protocol interface according to the working state; wherein the master-slave communication operation comprises sending data to the communication slave through the communication host, or sending data to the communication host through the communication slave.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a master-slave communication method, system and storage medium. Background Art

[0002] Serial Peripheral Interface (SPI) is a high-speed full-duplex communication bus, which is widely used in the communication interface between the main control and the peripherals. In the related art, the serial peripheral interface (SPI) is a master-slave interaction interface, and the transmission of data can only be initiated by the communication host (Master). Usually, the transmission of the communication slave (Slave) data requires the communication host to poll and query and read, which greatly affects the real-time performance of the communication slave actively sending data. In addition, since the transmission of network data requires a higher throughput, this requires SPI to have a higher transmission rate, and SPI usually increases the speed of data transmission by increasing the frequency, which also leads to the problem of unstable data transmission. Therefore, how to achieve real-time and high data transmission reliability of master-slave communication has become a technical problem that needs to be solved urgently. Summary of the invention

[0003] In order to solve at least one of the above technical problems, the present invention proposes a master-slave communication method, system and storage medium, which can achieve high real-time master-slave communication and effectively improve the reliability of data transmission.

[0004] On the one hand, an embodiment of the present invention provides a master-slave communication method, comprising the following steps:

[0005] Constructing a preset communication interface; wherein the communication host and the communication slave communicate through the preset communication interface, and the preset communication interface includes a communication trigger line, a communication synchronization line and a preset protocol interface; the communication trigger line is used to trigger the interruption of the communication host and receive the data sent by the communication slave; the communication synchronization line is used to synchronize the states of the communication host and the communication slave;

[0006] According to the working state of the communication slave, the preset level state of the communication trigger line and the communication synchronization line is set; wherein the working state of the communication slave includes a data receiving state and a data sending state; the communication host determines the working state of the communication slave according to the preset level state;

[0007] According to the working state, corresponding master-slave communication operations are performed through the preset protocol interface; wherein the master-slave communication operations include sending data to the communication slave through the communication host, or sending data to the communication host through the communication slave.

[0008] According to some embodiments of the present invention, setting the preset level states of the communication trigger line and the communication synchronization line according to the working state of the communication slave includes:

[0009] When the communication slave is in the data receiving state, the communication synchronization line is set to a first level state; wherein the communication host determines that the communication slave is in the data receiving state according to the first level state, and sends a first data packet to the communication slave through the preset protocol interface;

[0010] When it is determined that the reception of the first data packet is completed, the communication synchronization line is set to a second level state; wherein the second level state is used to indicate that the communication slave is in the data sending state.

[0011] According to some embodiments of the present invention, setting the preset level states of the communication trigger line and the communication synchronization line according to the working state of the communication slave further includes:

[0012] When the communication slave is in the data sending state, the communication trigger line is set to a third level state; wherein the communication host is triggered by the third level state to perform interrupt processing and receive a second data packet sent by the communication slave; the third level state includes converting the communication trigger line from a first preset level to a second preset level.

[0013] According to some embodiments of the present invention, after performing the step of determining that the communication slave is in a data receiving state according to the first level state, and sending a first data packet to the communication slave through the preset protocol interface, the method further includes:

[0014] When it is determined that the waiting time is greater than the preset time, the first data packet is resent to the communication slave through the preset protocol interface.

[0015] According to some embodiments of the present invention, the method further comprises:

[0016] Sending a communication mode conversion instruction to the communication slave through the communication host;

[0017] When it is determined that the communication host receives the first response data packet of the communication slave, the communication host sends several communication mode confirmation packets to the communication slave; wherein the communication slave sends a second response data packet to the communication host according to the received communication mode confirmation packet;

[0018] The communication mode conversion is determined to be completed based on the sending and receiving relationship between the second response data packet and the plurality of communication mode confirmation packets.

[0019] According to some embodiments of the present invention, the method further comprises:

[0020] Constructing a waiting-to-receive counter; wherein the waiting-to-receive counter is set in the communication host;

[0021] When a preset communication operation is detected, a preset counting operation is performed on the to-be-received counter to obtain a count value;

[0022] When it is determined that the count value is not zero, a preset host operation is performed by the communication host; wherein the preset host operation includes at least one of actively sending data or receiving a data packet;

[0023] Wherein, when a preset communication operation is detected, performing a preset counting operation on the to-be-received counter to obtain a count value includes:

[0024] When the communication host receives an active data transmission task or receives a change pulse of the communication trigger line, the waiting-to-receive counter is incremented by one;

[0025] Alternatively, when the communication host completes the active data transmission task or receives a valid data packet, the to-be-received counter is decremented by one.

[0026] According to some embodiments of the present invention, before executing the step of performing corresponding master-slave communication operations through the preset protocol interface according to the working state, the method further includes:

[0027] Construct a preset communication protocol; wherein the preset communication protocol includes a check parameter, a message type parameter, a data length parameter, a data offset parameter and a data body; the message type parameter includes a sequence number parameter and a classification parameter; the data length parameter includes the data length of the current data packet and the data length of the next data packet.

[0028] On the other hand, an embodiment of the present invention further provides a master-slave communication system, including:

[0029] A construction module is used to construct a preset communication interface; wherein the communication host and the communication slave communicate through the preset communication interface, and the preset communication interface includes a communication trigger line, a communication synchronization line and a preset protocol interface; the communication trigger line is used to trigger the communication host to interrupt and receive data sent by the communication slave; the communication synchronization line is used to synchronize the states of the communication host and the communication slave;

[0030] A level setting module, used for setting the preset level state of the communication trigger line and the communication synchronization line according to the working state of the communication slave; wherein the working state of the communication slave includes a data receiving state and a data sending state; and the communication host determines the working state of the communication slave according to the preset level state;

[0031] A communication module is used to perform corresponding master-slave communication operations through the preset protocol interface according to the working state; wherein the master-slave communication operation includes sending data to the communication slave through the communication host, or sending data to the communication host through the communication slave.

[0032] On the other hand, an embodiment of the present invention further provides a master-slave communication system, including:

[0033] at least one processor;

[0034] at least one memory for storing at least one program;

[0035] When the at least one program is executed by the at least one processor, the at least one processor implements the master-slave communication method as described in the above embodiment.

[0036] On the other hand, an embodiment of the present invention further provides a computer storage medium, in which a program executable by a processor is stored. When the program executable by the processor is executed by the processor, it is used to implement the master-slave communication method as described in the above embodiment.

[0037] A master-slave communication method according to an embodiment of the present invention has at least the following beneficial effects: the embodiment of the present invention first constructs a preset communication interface to communicate between a communication host and a communication slave through the preset communication interface. Accordingly, the preset communication interface in the embodiment of the present invention includes a communication trigger line, a communication synchronization line and a preset protocol interface. The embodiment of the present invention sets the preset level state of the communication trigger line and the communication synchronization line, so that the communication host can determine the working state of the communication slave and realize state synchronization. Among them, the embodiment of the present invention triggers the interruption of the communication host through the set communication trigger line, so that the communication host can receive the data sent by the communication slave in time, which alleviates the communication real-time problem caused by the communication host periodically polling and reading the data sent by the communication slave. At the same time, the embodiment of the present invention sets the level state of the communication synchronization line so that the communication host and the communication slave can realize state synchronization and flow control, thereby alleviating the data abnormality problem caused by the high-throughput data transmission process, and effectively improving the reliability of communication. Further, the embodiment of the present invention performs corresponding master-slave communication operations through the preset protocol interface according to the working state, such as sending data from the communication host to the communication slave, or sending data from the communication slave to the communication host, thereby realizing master-slave communication with higher real-time performance and effectively improving the reliability of data transmission. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] Figure 1 is a flow chart of a master-slave communication method provided by an embodiment of the present invention;

[0039] Figure 2 It is a schematic diagram of a preset communication interface for master-slave communication provided by an embodiment of the present invention;

[0040] Figure 3 It is a principle block diagram of a master-slave communication system provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0041] The embodiments described in the embodiments of this application should not be regarded as limitations of this application. All other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0042] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it can be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.

[0043] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.

[0044] Before introducing the embodiments of the present application, the relevant terms involved in the present application are first explained.

[0045] Serial Peripheral Interface (SPI): is a high-speed, full-duplex, synchronous communication bus. SPI works in master-slave mode, which usually has one master device and one or more slave devices, requiring at least 4 wires, but 3 wires can also work for unidirectional transmission.

[0046] As a high-speed full-duplex communication bus, the serial peripheral interface is widely used in the communication interface between the master and the peripherals. In the related art, the serial peripheral interface (SPI) is an interface for master-slave interaction. The data transmission can only be initiated by the communication host (Master). Usually, the communication slave (Slave) data transmission requires the communication host to poll and query and read, which greatly affects the real-time performance of the communication slave actively sending data. In addition, since the transmission of network data requires a higher throughput, this requires SPI to have a higher transmission rate, and SPI usually increases the speed of data transmission by increasing the frequency, which also leads to the problem of unstable data transmission. Therefore, how to achieve real-time and high data transmission reliability of master-slave communication has become a technical problem that needs to be solved urgently.

[0047] An embodiment of the present invention provides a master-slave communication method, system and storage medium, which can realize master-slave communication with high real-time performance and effectively improve the reliability of data transmission. Figure 1 The method of the embodiment of the present invention includes but is not limited to step S110, step S120 and step S130.

[0048] Specifically, the method application process of the embodiment of the present invention includes but is not limited to the following steps:

[0049] S110: Constructing a preset communication interface. The communication host and the communication slave communicate through the preset communication interface, which includes a communication trigger line, a communication synchronization line, and a preset protocol interface. The communication trigger line is used to trigger the communication host interrupt and receive data sent by the communication slave. The communication synchronization line is used to synchronize the status of the communication host and the communication slave.

[0050] S120: According to the working state of the communication slave, the preset level state of the communication trigger line and the communication synchronization line is set. The working state of the communication slave includes a data receiving state and a data sending state. The communication host determines the working state of the communication slave according to the preset level state.

[0051] S130: Perform corresponding master-slave communication operations through a preset protocol interface according to the working state, wherein the master-slave communication operations include sending data from the communication host to the communication slave, or sending data from the communication slave to the communication host.

[0052] During the working process of this specific embodiment, the embodiment of the present invention first constructs a preset communication interface. Specifically, the preset communication interface in the embodiment of the present invention includes a communication trigger line, a communication synchronization line and a preset protocol interface. Accordingly, the embodiment of the present invention communicates between the communication host and the communication slave through the preset communication interface, that is, the communication host and the communication slave are connected for communication through the preset communication interface. At the same time, in the embodiment of the present invention, the interrupt service of the communication host is triggered by the communication trigger line in the preset communication interface to receive the data sent by the communication slave. It is easy to understand that the communication slave in the embodiment of the present invention triggers the interrupt service of the communication host through the level change or level pulse of the communication trigger line, so that the communication host can receive the data of the communication slave in time, which alleviates the problem of real-time data transmission caused by the need for the communication host to periodically poll to query and read the data sent by the communication slave. In addition, the embodiment of the present invention synchronizes the status of the communication host and the communication slave through the communication synchronization line. For example, when the communication host queries the level status of the communication synchronization line to determine the working status of the communication slave, such as data reception status or data transmission status, it can achieve status synchronization and flow control between the communication host and the communication slave, effectively alleviating the problem of data packet loss and data anomalies caused by lack of flow control during high-throughput data transmission, and improving the reliability of master-slave communication.

[0053] For example, refer to Figure 2 In some embodiments of the present invention, the preset protocol interface can be a serial peripheral interface (SPI). In the embodiment of the present invention, two communication lines, namely a communication trigger line (Data_IRQ) ​​and a communication synchronization line (Data_RW), are added on the basis of the 4 lines used for standard SPI communication, such as SO, SI, SCLK and CS in the figure. Among them, for the communication host, the communication synchronization line is connected to the input pin, and the pin (IO) level change of the communication synchronization line can be detected. At the same time, the communication trigger line is also connected to the input pin, but in addition to being able to detect the pin level change, the communication host in the embodiment of the present invention can enter the interrupt service (immediate response processing) for the pin level change connected to the communication trigger line. Correspondingly, for the communication slave, the communication trigger line and the communication synchronization line are both connected to the output pin, and the level change of the communication trigger line and the communication synchronization line is controlled by the communication slave.

[0054] Next, the embodiment of the present invention sets the preset level state of the communication trigger line and the communication synchronization line according to the working state of the communication slave. Specifically, the working state of the communication slave in the embodiment of the present invention includes a data receiving state and a data sending state. Correspondingly, the communication host in the embodiment of the present invention determines the working state of the communication slave according to the preset level state. Further, the embodiment of the present invention performs corresponding master-slave communication operations through a preset protocol interface according to the working state. Specifically, the master-slave communication operation in the embodiment of the present invention includes sending data to the communication slave through the communication host, or sending data to the communication host through the communication slave. Exemplarily, when the communication host detects a level change of the communication trigger line, it is determined that the communication slave actively initiates data transmission, the communication slave is in a data sending state, the communication host enters interrupt processing, and receives the data sent by the communication slave. Alternatively, when the communication host needs to send data, after determining that the communication slave is in a data receiving state through the level state of the communication synchronization line, the communication host sends corresponding data to the communication slave. The embodiment of the present invention triggers the interruption of the communication host through the communication trigger line, thereby effectively alleviating the problem that the real-time performance of communication cannot be guaranteed due to the periodic polling and reading of data by the communication host, and performs high-throughput flow control and state synchronization between the communication host and the communication slave through the communication synchronization line, thereby effectively alleviating the problems of data packet loss and data anomaly, and improving the reliability of master-slave communication.

[0055] In some embodiments of the present invention, the preset level states of the communication trigger line and the communication synchronization line are set according to the working state of the communication slave, including but not limited to:

[0056] When the communication slave is in the data receiving state, the communication synchronization line is set to the first level state. The communication host determines that the communication slave is in the data receiving state according to the first level state, and sends a first data packet to the communication slave through a preset communication protocol interface.

[0057] When it is determined that the reception of the first data packet is completed, the communication synchronization line is set to a second level state, wherein the second level state is used to indicate that the communication slave is in a data sending state.

[0058] In this specific embodiment, when the communication slave is in a data receiving state, the embodiment of the present invention sets the communication synchronization line to a first level state to indicate to the communication host that it is in a data receiving state. Accordingly, in the embodiment of the present invention, the communication host first checks the level state of the communication synchronization line before sending data, and when it is determined that the level state of the communication synchronization line is the first level state, the data to be sent, i.e., the first data packet, is sent to the slave through the preset communication protocol interface. Anyway, when the level state of the communication synchronization line is not the first level state, it enters the waiting state. Then, after receiving the first data packet sent by the communication host, the embodiment of the present invention sets the level state of the communication synchronization line to a second level state. Specifically, the second level state in the embodiment of the present invention indicates that the communication slave is in a data sending state. It is easy to understand that after receiving the first data packet, the communication slave needs to respond to the communication host, so the embodiment of the present invention converts the level state of the communication synchronization line from the first level state to the second level state to indicate to the communication host that the communication slave enters the data sending state. Correspondingly, after the communication host completes sending the first data packet, it determines whether it needs to receive data by detecting the level state of the communication synchronization line. When the level state is detected to be the second level state, the data is received, otherwise it enters the waiting state, thereby realizing the flow control and state synchronization of the communication host and the communication slave.

[0059] Exemplarily, in an embodiment of the present invention, when the communication host actively sends data to the communication slave, the communication host first detects the level state of the communication synchronization line. When it is determined that the level state of the communication synchronization line is a high level, that is, a first level state, it is determined that the communication slave is in a data receiving state, and the communication host sends the data to be sent to the communication slave, that is, a first data packet. On the contrary, when the detected level state of the communication synchronization line is not a high level, the communication host enters a waiting state, waiting for the communication slave to output the level state of the communication synchronization line as a high level, that is, waiting for the communication slave to enter a data receiving state. Further, after the communication host completes sending the first data packet, the level state of the communication synchronization line is detected again. When it is detected that the level state of the communication synchronization line is a low level, that is, a second level state, the data sent by the communication slave is received. On the contrary, it enters a waiting state. Correspondingly, when the communication slave is in an idle state, the communication slave sets the level state of the communication synchronization line to a high level to indicate to the communication host that it is in a data receiving state. Next, when the communication slave receives the data and completes the data reception processing, it sets the level state of the communication synchronization line to a low level, and then waits for the clock to send data to the communication host, such as sending a response data packet.

[0060] In some embodiments of the present invention, the preset level states of the communication trigger line and the communication synchronization line are set according to the working state of the communication slave, including but not limited to:

[0061] When the communication slave is in a data transmission state, the communication trigger line is set to a third level state. The third level state triggers the communication host to perform interrupt processing and receive a second data packet sent by the communication slave. The third level state includes converting the communication trigger line from a first preset level to a second preset level.

[0062] In this specific embodiment, when the communication slave is in a data transmission state, the embodiment of the present invention sets the communication trigger line to a third level state. Specifically, the embodiment of the present invention triggers the communication host to enter the interrupt service through the set third level state to receive the second data packet sent by the communication slave. Wherein, the third level state in the embodiment of the present invention includes converting the level state of the communication trigger line from a first preset level to a second preset level, such as pulling the level state of the communication trigger line from a low level to a high level, or generating a rising edge pulse through the communication trigger line. Exemplarily, in the embodiment of the present invention, when the communication slave requests to initiate data transmission, the communication slave first controls the communication trigger line to convert from a low level to a high level, that is, the communication trigger line is set to the third level state. Then, the communication slave starts to send the second data packet until the data transmission is completed, and the communication slave pulls down the level state of the communication trigger line. Correspondingly, when the communication trigger line is converted from a low level to a high level, the interrupt of the communication host is triggered, and the communication host enters the interrupt processing to receive the second data packet sent by the communication slave.

[0063] In some embodiments of the present invention, after executing the step of determining that the communication slave is in a data receiving state according to the first level state, and sending a first data packet to the communication slave through a preset communication protocol interface, the master-slave communication method provided by the embodiment of the present invention further includes but is not limited to:

[0064] When it is determined that the waiting time is greater than the preset time, the first data packet is resent to the communication slave through the preset communication protocol interface.

[0065] In a specific embodiment, after the communication host sends the first data packet to the communication slave in the embodiment of the present invention, the communication host waits for the response data packet returned by the communication slave. Specifically, data retransmission in the master-slave communication process is necessary, because the first data transmission may fail due to electronic interference and other reasons, and the second or more transmissions can be successful. The present invention resends the first data packet to the communication slave through the preset communication protocol interface by judging whether the waiting time is greater than the preset time. Exemplarily, in the embodiment of the present invention, when the waiting time is greater than 60 seconds, 10 data retransmissions are performed. Accordingly, if the preset time is too short, it may cause erroneous retransmission due to data delay, and conversely, if the preset time is too long, it will affect the communication efficiency because it takes too long to retransmit. At the same time, it is also necessary to set the retransmission timeout, because the communication slave may not receive data, and retransmission wastes resources all the time. Therefore, when the preset retransmission time is exceeded and no response is received, retransmission is no longer performed.

[0066] In some embodiments of the present invention, the master-slave communication method provided by the embodiment of the present invention also includes but is not limited to:

[0067] The communication mode switching instruction is sent to the communication slave through the communication master.

[0068] When it is determined that the communication host receives the first response data packet of the communication slave, the communication host sends several communication mode confirmation packets to the communication slave, wherein the communication slave sends a second response data packet to the communication host according to the received communication mode confirmation packet.

[0069] The communication mode conversion is determined to be complete based on the sending and receiving relationship between the second response data packet and the plurality of communication mode confirmation packets.

[0070] In this specific embodiment, the embodiment of the present invention can switch between half-duplex and full-duplex working modes. Among them, in the embodiment of the present invention, the communication host and the communication slave communicate by default in the half-duplex working mode, and the communication mode can be switched during the communication process. Specifically, when it is necessary to switch the communication mode, the embodiment of the present invention first sends a communication mode switching instruction to the communication slave through the communication host, and waits for the response of the communication slave. Then, after the communication slave in the embodiment of the present invention receives the communication mode switching instruction of the communication host, it switches the communication mode, such as switching from half-duplex mode to full-duplex mode, and sends a first response data packet to the communication host. Further, when the embodiment of the present invention determines that the communication host has received the first response data packet, the communication host sends several communication mode confirmation packets to the communication slave to confirm that the communication mode conversion is successful. Correspondingly, after the communication slave receives the communication mode confirmation packet, it feeds back the corresponding response data, i.e., the second response data packet, to the communication host, and the embodiment of the present invention confirms that the communication mode conversion is successful through the second response data packet and the sending and receiving relationship of several communication mode confirmation packets. Exemplarily, after the communication host and the communication slave switch the communication mode to the full-duplex mode through the communication mode instruction and the first response data packet, the embodiment of the present invention continuously sends 5 communication mode confirmation packets to the communication slave through the communication host. When, in addition to the first communication mode confirmation packet, the subsequent 4 consecutive communication mode confirmation packets can receive the response of the previous communication mode confirmation packet while sending, it is considered that the full-duplex switching is successful. It is easy to understand that, because according to the half-duplex protocol, at the second clock, the communication slave is only responsible for sending the response and has no receiving ability, so the third clock should be missing. The embodiment of the present invention continuously sends 5 packets of data to improve the fault tolerance rate, and in some embodiments of the present invention, continuously sending 3 packets of data can also make relevant judgments. Correspondingly, when the switching failure is confirmed by the transceiver relationship of the second response data packet and several communication mode confirmation packets, it is switched to the half-duplex working mode again, and the step of sending the communication mode conversion instruction to the communication slave through the communication host is returned to be switched again. It is easy to understand that in some embodiments of the present invention, the communication mode switching process can also be initiated by the communication slave to the communication master, and its steps are similar to the above process and will not be repeated here.

[0071] It should be noted that in some embodiments of the present invention, in the full-duplex working mode, when the communication host actively initiates data transmission, the embodiment of the present invention first detects the level state of the communication synchronization line. When it is determined that the communication synchronization line is in a first level state, such as a high level state, the communication host sends a related data packet to the communication slave. On the contrary, when it is detected that the level state of the communication synchronization line is not in the first level state, such as a low level state, the communication host enters waiting. Then, after the transmission is completed, the communication host detects whether relevant response data has been received. When the communication host receives the data, the relevant data is processed, otherwise, it returns to the state of preparing data transmission or reception. Correspondingly, in the full-duplex working mode, when the communication slave is in an idle state, the embodiment of the present invention sets the level state of the communication synchronization line to a high level, that is, a first level state, through the communication slave to indicate to the communication host that it is in a data receiving state. After receiving the data sent by the communication host, the embodiment of the present invention pulls down the level of the communication synchronization line and performs related data processing. When it is determined that the data processing of the communication slave is completed, the embodiment of the present invention pulls up the level of the communication synchronization line, that is, resets it to a high level, and waits for the clock to send the response data. In addition, when the communication slave actively sends data, the embodiment of the present invention controls the communication trigger line through the communication slave to generate a rising edge pulse to trigger the interrupt service of the communication host to receive data.

[0072] It should be noted that, in some embodiments of the present invention, when the communication mode between the communication host and the communication slave is full-duplex mode, when the communication host in the embodiment of the present invention does not receive the response data of the communication slave, when the level state of the communication synchronization line is detected to be the first level state, such as the communication synchronization line is at a high level, it can be determined that the communication slave is in a data receiving state, and at this time, the communication host directly initiates the data transmission of the next packet. Specifically, in the traditional master-slave communication process, when the communication host initiates data, after receiving the response of the communication slave, the next packet of data is sent, so that the communication host can ensure that the data is correctly received by the communication slave by the master-slave response. Correspondingly, SPI supports full-duplex communication, that is, the communication host can receive the response of the previous packet of data while sending data, but it can also determine that the communication slave is in a receiving state and can receive data by receiving the response, and in the embodiment of the present invention, the communication host determines that the communication slave is in a data receiving state through the communication synchronization line, so the communication host can directly start sending the next packet of data when the response is not received, saving the time of waiting for the response and effectively improving the communication efficiency. In addition, in the embodiment of the present invention, when the communication slave cannot receive data and needs to resend the data, the data sent out in the embodiment of the present invention is not discarded immediately, and the data is resent when no response is received.

[0073] In some embodiments of the present invention, the master-slave communication method provided by the embodiment of the present invention also includes but is not limited to:

[0074] A waiting-to-receive counter is constructed, wherein the waiting-to-receive counter is set in the communication host.

[0075] When a preset communication operation is detected, a preset counting operation is performed on the to-be-received counter to obtain a count value.

[0076] When it is determined that the count value is not zero, a preset host operation is performed by the communication host, wherein the preset host operation includes at least one of actively sending data or receiving a data packet.

[0077] When a preset communication operation is detected, a preset counting operation is performed on the receiving counter to obtain a counting value, including but not limited to:

[0078] When the communication host receives an active data transmission task or receives a change pulse of the communication trigger line, the waiting-to-receive counter is incremented by one.

[0079] Alternatively, when the communication host completes the task of actively sending data or receives a valid data packet, the counter to be received is decremented by one.

[0080] In this specific embodiment, the embodiment of the present invention improves the data transmission efficiency and code efficiency by constructing a counter to be received. Specifically, the embodiment of the present invention first constructs a counter to be received or a spin lock inside the host. When a preset communication operation is detected, the embodiment of the present invention performs a preset counting operation on the counter to be received, thereby obtaining the corresponding count value of the counter to be received. Among them, when the communication host receives an active data transmission task or receives a change pulse of the communication trigger line in the embodiment of the present invention, the embodiment of the present invention adds one to the counter to be received. For example, when the communication host needs to actively send data or receives a rising edge pulse generated by the communication trigger line, the count value of the counter to be received is added by one to obtain a new count value. Correspondingly, when the communication host completes the active data transmission task or receives a valid data packet, the embodiment of the present invention subtracts one from the counter to be received. For example, when the communication host has data to send, after the data transmission is completed, the embodiment of the present invention subtracts one from the count value of the counter to be received. In addition, when the communication host has no data to send, it actively receives a packet of data. When the received data is a valid data packet, the embodiment of the present invention subtracts one from the count value of the counter to be received. Further, when it is determined that the count value is not zero, the embodiment of the present invention performs a preset host operation through the communication host. Specifically, the preset host operation in the embodiment of the present invention includes at least one of actively sending data or receiving a data packet. In the embodiment of the present invention, the data processing thread detects the count value of the counter to be received. When the count value is not 0, the embodiment of the present invention first detects whether the communication host has data to send. When it is determined that the communication host has data to send, the communication host actively sends the data and reduces the corresponding count value by one. Then, when the communication host has no data to send, the communication host actively receives a packet of data and parses the data packet. When it is determined that the data packet is a valid data packet, the count value is reduced by one. It is easy to understand that the embodiment of the present invention can effectively improve the data transmission efficiency and code efficiency by setting the counter to be received. For example, when the communication host and the communication slave need to send 2 packets of data to each other at the same time. The communication host only needs to know that it needs to initiate 4 clocks, instead of the thread preparing to send data to monitor whether the SPI is idle. For the communication slave, there is no need to pay attention to the state of the communication host, only need to pull up the communication trigger line twice, and then only need to pay attention to the clock of the communication host. It should be noted that the count value in the embodiment of the present invention will not be lower than 0. If the count of the to-be-received counter is not zero, but the communication host has no data to send and invalid data is received, the count number is directly cleared to zero. Invalid data in the embodiment of the present invention refers to data that cannot be parsed when received.

[0081] In some embodiments of the present invention, before executing the step of performing corresponding master-slave communication operations through a preset communication protocol interface according to the working state, the master-slave communication method provided by the embodiment of the present invention further includes but is not limited to:

[0082] Construct a preset communication protocol. The preset communication protocol includes a check parameter, a message type parameter, a data length parameter, a data offset parameter, and a data body. The message type parameter includes a sequence number parameter and a classification parameter. The data length parameter includes the data length of the current data packet and the data length of the next data packet.

[0083] In this specific embodiment, before the corresponding master-slave communication operation is performed through the preset communication protocol interface, the embodiment of the present invention first constructs a preset communication protocol. Specifically, the preset communication protocol constructed in the embodiment of the present invention includes a check parameter, a message type parameter, a data length parameter, a data offset parameter and a data body. Among them, the message type parameter in the embodiment of the present invention includes a sequence number parameter and a classification parameter. The sequence number parameter in the embodiment of the present invention is used by the data sender to cumulatively number each data packet so that the data receiver can calculate the expected sequence number of the next data packet according to the sequence number parameter in the previous data packet. When the sequence number parameter in the received data packet does not match the expected sequence number, it indicates that the data is lost and the data sender needs to retransmit the data. It should be noted that in the embodiment of the present invention, retransmission is achieved by defining a protection area (protect). In the embodiment of the present invention, the data that has been sent will not be discarded, but saved in the protect area. The size of the protect area is variable. When the area data is full, the earliest saved data is discarded. Once retransmission occurs, the embodiment of the present invention will search whether there is a reserved sequence number parameter (seq) in the protect area. If so, it will be retransmitted, otherwise the reply data has been lost. Correspondingly, in the embodiment of the present invention, the highest bit of the sequence number parameter (seq) is 1, which means that the seq is the retransmitted data, and the highest bit of the sequence number parameter (seq) is 0, which means that it is the data transmitted for the first time. At the same time, the embodiment of the present invention classifies the actual data packet through the classification parameter, and different data packet categories have different priorities. For example, in a network transmission scenario, when the classification parameter type is 0x00, it represents network data, and the classification parameter type is 0x01, it represents user-defined data, and the 0x01 data type will be transmitted first. In addition, the data length parameter in the embodiment of the present invention includes the data length of the current data packet and the data length of the next data packet. In the embodiment of the present invention, the data length means the length of the data (payload). In particular, in addition to the length of the current data, the data length in the embodiment of the present invention may include the length of the data to be sent next time. Further, in the embodiment of the present invention, the data offset parameter (offset) represents the starting position of the data (payload). For example, the general data starting position is bit64. In particular, some chips require that the data length be a multiple of a specific number, such as a multiple of 4 or a multiple of 16 when receiving and sending data. For these situations, the embodiments of the present invention set an offset address to adjust the starting address of the data, and the data offset parameter indicates the starting address of the data. At the same time, the received data is verified by the verification parameter in the embodiments of the present invention. The verification in the embodiments of the present invention means that the message type, data length, data offset, and data are calculated according to a certain algorithm to obtain a numerical value. For example, the encryption algorithm used in the embodiments of the present invention is CRC-16.

[0084] It should be noted that, in some embodiments of the present invention, it may be considered not to use the communication synchronization line (Data_RW) and the communication trigger line (Data_IRQ), and instead use a single line and infrared NEC protocol. Accordingly, in the embodiments of the present invention, the state of the communication slave can also be represented by a high level, a low level, or a pulse. In addition, in some embodiments of the present invention, the preset protocol interface can also be a high-speed protocol such as USB.

[0085] An embodiment of the present invention further provides a master-slave communication system, comprising:

[0086] A construction module is used to construct a preset communication interface. The communication host and the communication slave communicate through the preset communication interface, and the preset communication interface includes a communication trigger line, a communication synchronization line, and a preset communication protocol interface. The communication trigger line is used to trigger the communication host interrupt and receive data sent by the communication slave. The communication synchronization line is used to synchronize the status of the communication host and the communication slave.

[0087] The level setting module is used to set the preset level state of the communication trigger line and the communication synchronization line according to the working state of the communication slave. The working state of the communication slave includes a data receiving state and a data sending state. The communication host determines the working state of the communication slave according to the preset level state.

[0088] The communication module is used to perform corresponding master-slave communication operations through a preset communication protocol interface according to the working state. The master-slave communication operation includes sending data from the communication host to the communication slave, or sending data from the communication slave to the communication host.

[0089] Reference Figure 3 , an embodiment of the present invention further provides a master-slave communication system, comprising:

[0090] At least one processor 210 .

[0091] At least one memory 220 is used to store at least one program.

[0092] When at least one program is executed by at least one processor 210, the at least one processor 210 implements the master-slave communication method described in the above embodiment.

[0093] An embodiment of the present invention further provides a computer-readable storage medium, which stores computer-executable instructions. The computer-executable instructions are executed by one or more control processors, for example, to execute the steps described in the above embodiment.

[0094] It will be appreciated by those skilled in the art that all or some of the steps and systems in the disclosed method above may be implemented as software, firmware, hardware and appropriate combinations thereof. Some physical components or all physical components may be implemented as software executed by a processor, such as a central processing unit, a digital signal processor or a microprocessor, or may be implemented as hardware, or may be implemented as an integrated circuit, such as an application specific integrated circuit. Such software may be distributed on a computer-readable medium, which may include a computer storage medium (or a non-transitory medium) and a communication medium (or a temporary medium). As known to those skilled in the art, the term computer storage medium includes volatile and non-volatile, removable and non-removable media implemented in any method or technology for storing information (such as computer-readable instructions, data structures, program modules or other data). Computer storage media include, but are not limited to, RAM, ROM, EEPROM, flash memory or other memory technologies, CD-ROM, digital versatile disks (DVD) or other optical disk storage, magnetic cassettes, magnetic tapes, disk storage or other magnetic storage devices, or any other medium that may be used to store desired information and may be accessed by a computer. Furthermore, it is well known to those skilled in the art that communication media typically embodies computer readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism, and may include any information delivery media.

[0095] The above is a specific description of the preferred implementation of the present invention, but the present invention is not limited to the above-mentioned implementation mode. Technical personnel familiar with the field can also make various equivalent deformations or substitutions without violating the spirit of the present invention. These equivalent deformations or substitutions are all included in the scope defined by the claims of the present invention.

Claims

1. A master-slave communication method, characterized in that: The following steps are involved: Constructing a preset communication interface; wherein the communication host and the communication slave communicate through the preset communication interface, and the preset communication interface includes a communication trigger line, a communication synchronization line and a preset protocol interface; the communication trigger line is used to trigger the interruption of the communication host and receive the data sent by the communication slave; the communication synchronization line is used to synchronize the states of the communication host and the communication slave; According to the working state of the communication slave, the preset level state of the communication trigger line and the communication synchronization line is set; wherein the working state of the communication slave includes a data receiving state and a data sending state; the communication host determines the working state of the communication slave according to the preset level state; According to the working state, corresponding master-slave communication operations are performed through the preset protocol interface; wherein the master-slave communication operations include sending data to the communication slave through the communication host, or sending data to the communication host through the communication slave.

2. The master-slave communication method according to claim 1, characterized in that: The step of setting the preset level states of the communication trigger line and the communication synchronization line according to the working state of the communication slave includes: When the communication slave is in the data receiving state, the communication synchronization line is set to a first level state; wherein the communication host determines that the communication slave is in the data receiving state according to the first level state, and sends a first data packet to the communication slave through the preset protocol interface; When it is determined that the reception of the first data packet is completed, the communication synchronization line is set to a second level state; wherein the second level state is used to indicate that the communication slave is in the data sending state.

3. The master-slave communication method according to claim 2, characterized in that: The step of setting the preset level states of the communication trigger line and the communication synchronization line according to the working state of the communication slave further comprises: When the communication slave is in the data sending state, the communication trigger line is set to a third level state; wherein the communication host is triggered by the third level state to perform interrupt processing and receive a second data packet sent by the communication slave; the third level state includes converting the communication trigger line from a first preset level to a second preset level.

4. The master-slave communication method according to claim 2, characterized in that: After executing the step of determining that the communication slave is in a data receiving state according to the first level state, and sending a first data packet to the communication slave through the preset protocol interface, the method further includes: When it is determined that the waiting time is greater than the preset time, the first data packet is resent to the communication slave through the preset protocol interface.

5. The master-slave communication method according to claim 1, characterized in that: The method further comprises: Sending a communication mode conversion instruction to the communication slave through the communication host; When it is determined that the communication host receives the first response data packet of the communication slave, the communication host sends several communication mode confirmation packets to the communication slave; wherein the communication slave sends a second response data packet to the communication host according to the received communication mode confirmation packet; The communication mode conversion is determined to be completed based on the sending and receiving relationship between the second response data packet and the plurality of communication mode confirmation packets.

6. The master-slave communication method according to claim 1, characterized in that: The method further comprises: Constructing a waiting-to-receive counter; wherein the waiting-to-receive counter is set in the communication host; When a preset communication operation is detected, a preset counting operation is performed on the to-be-received counter to obtain a count value; When it is determined that the count value is not zero, a preset host operation is performed by the communication host; wherein the preset host operation includes at least one of actively sending data or receiving a data packet; Wherein, when a preset communication operation is detected, performing a preset counting operation on the to-be-received counter to obtain a count value includes: When the communication host receives an active data transmission task or receives a change pulse of the communication trigger line, the waiting-to-receive counter is incremented by one; Alternatively, when the communication host completes the active data transmission task or receives a valid data packet, the to-be-received counter is decremented by one.

7. The master-slave communication method according to claim 1, characterized in that: Before executing the step of performing corresponding master-slave communication operations through the preset protocol interface according to the working state, the method further includes: Construct a preset communication protocol; wherein the preset communication protocol includes a check parameter, a message type parameter, a data length parameter, a data offset parameter and a data body; the message type parameter includes a sequence number parameter and a classification parameter; the data length parameter includes the data length of the current data packet and the data length of the next data packet.

8. A master-slave communication system, characterized in that: include: A construction module is used to construct a preset communication interface; wherein the communication host and the communication slave communicate through the preset communication interface, and the preset communication interface includes a communication trigger line, a communication synchronization line and a preset protocol interface; the communication trigger line is used to trigger the communication host to interrupt and receive data sent by the communication slave; the communication synchronization line is used to synchronize the states of the communication host and the communication slave; A level setting module, used for setting the preset level state of the communication trigger line and the communication synchronization line according to the working state of the communication slave; wherein the working state of the communication slave includes a data receiving state and a data sending state; and the communication host determines the working state of the communication slave according to the preset level state; A communication module is used to perform corresponding master-slave communication operations through the preset protocol interface according to the working state; wherein the master-slave communication operation includes sending data to the communication slave through the communication host, or sending data to the communication host through the communication slave.

9. A master-slave communication system, characterized in that: include: at least one processor; at least one memory for storing at least one program; When the at least one program is executed by the at least one processor, the at least one processor implements the master-slave communication method according to any one of claims 1 to 7.

10. A computer storage medium storing a program executable by a processor, characterized in that: The program executable by the processor is used to implement the master-slave communication method according to any one of claims 1 to 7 when executed by the processor.