Disclosure of Invention
In view of this, the embodiment of the application provides a communication mode configuration method and device for an SPI protocol master-slave device, which improves the configuration speed.
In a first aspect, an embodiment of the present application provides a method for configuring a communication mode of an SPI protocol master-slave device, where the method includes:
If the configuration mode of the slave device is selected to be hardware configuration, the master device pulls up the chip select signal;
generating a command stream according to a main equipment communication mode configured by software, sending the command stream to a slave equipment by the main equipment through an MOSI line, generating a clock signal through a clock pin so that the slave equipment can configure according to the command stream, and sending a completion mark to the main equipment through a MISO line after the configuration is completed;
The master device finishes generating the clock signal and pulling down the chip select signal according to the received completion flag so as to communicate with the slave device.
Optionally, before the selecting the configuration mode of the slave device to be the hardware configuration, the master device pulls up the chip select signal, the method further includes: software configures a communication mode of the main equipment;
the generating a command stream according to the software configured communication mode of the main device includes:
Identifying a communication mode of the main equipment configured by the software to obtain an identification result;
and generating a command stream according to the identification result.
Optionally, the command stream includes a configuration for polarity, phase, transmit/accept, and full/half duplex.
Optionally, before the pulling down the chip select signal, the method further includes: the master device and the slave device send out numbers through rising edges and numbers through falling edges of clock signals.
Optionally, the method further comprises:
If the configuration mode of the slave device is selected as software configuration, the configuration of the slave device is carried out according to the standard SPI protocol.
Alternatively, the completion flag is 2' b10.
In a second aspect, the present application further provides a communication mode configuration apparatus of an SPI protocol master-slave device, the apparatus comprising: the device comprises a selection module, a judgment module and a command stream generation module;
The selecting module is used for pulling up the chip select signal by the master device if the configuration mode of the slave device is hardware configuration;
The command stream generating module is used for generating a command stream according to a communication mode of a main device configured by software, the main device sends the command stream to a slave device through an MOSI line, and generates a clock signal through a clock pin so that the slave device configures according to the command stream, and sends a completion mark to the main device through a MISO line after the configuration is completed;
And the receiving module is used for ending generating the clock signal and pulling down the chip selection signal by the master device according to the received completion mark so as to communicate with the slave device.
Optionally, the selecting module is further configured to perform configuration of the slave device according to a standard SPI protocol if the slave device configuration mode is selected to be software configuration.
Optionally, the device further comprises an identification module;
the identification module is used for identifying a communication mode of the main equipment configured by software, obtaining an identification result and sending the identification result to the command stream generation module;
The command stream generating module is further configured to receive the identification result.
Optionally, the command stream includes a configuration for polarity, phase, transmit/accept, and full/half duplex.
The embodiment of the application provides a communication mode configuration method and device of SPI protocol master-slave equipment. If the configuration mode of the slave device is hardware configuration, the master device pulls up a chip selection signal sent to the slave device through a chip selection pin; then, generating a command stream according to a main device communication mode configured by software, sending the command stream to a slave device by the main device through an MOSI line, generating a clock signal through a clock pin so that the slave device configures according to the command stream, and sending a completion mark to the main device through an MISO line after the configuration is completed; the master device finishes generating the clock signal and pulling down the chip select signal according to the received completion flag so as to communicate with the slave device. Furthermore, the slave device to be connected in communication is controlled through the chip selection pin and the chip selection signal wire of the master device, the time sequence of data between the master device and the slave device is controlled through the clock signal of the clock wire, the accuracy and the synchronism of the data are ensured, a command stream is generated according to the communication mode of the master device, the command stream is transmitted through the MOSI wire, and the communication mode configuration result of the slave device is fed back through the MISO wire, so that the configuration of the communication mode of the slave device is completed based on the hardware configuration pin or the connection wire between the master device and the slave device, the data transmission is reliable and the transmission speed is high, and the configuration speed of the communication mode of the slave device is improved.
Detailed Description
The standard SPI (Serial Peripheral Interface) protocol requires software to configure communication modes of the two parties in advance when the master and slave devices communicate, and communication errors can be caused if the communication modes configured by the two parties are not corresponding. The software configuration mode adopted at present has slower configuration speed.
Based on the above problems, the communication mode configuration method and device for the SPI protocol master-slave device control the corresponding slave device through the chip selection pin and the chip selection signal line of the master device, realize the time sequence of data between the master device and the slave device through the clock signal of the clock line, ensure the accuracy and the synchronism of the data, and send command streams to configure the communication mode of the slave device through the MOSI line and the MISO line.
It will be apparent that the described embodiments are only some, but not all, embodiments of the application. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
Fig. 1 is a flow chart of a communication mode configuration method of an SPI protocol master-slave device provided by an embodiment of the present application, referring to fig. 1, a master device may be correspondingly connected to at least one slave device, and when the master device configures a communication mode for a certain slave device, the method includes:
s101, if the configuration mode of the slave device is selected to be hardware configuration, the master device pulls up the chip select signal.
For example, when the slave device configuration mode can also select the software mode to configure the slave device, if the slave device configuration mode is selected to be the software configuration, the software configuration is performed on the slave device according to the standard SPI protocol.
Specifically, the master device pulls up the chip select signal refers to pulling up the chip select signal sent to the slave device by the master device through the chip select pin. A chip selection signal line is connected between the chip selection pin of the master device and the chip selection pin of the slave device.
S102, generating a command stream according to a main device communication mode configured by software, transmitting the command stream to a slave device by the main device through an MOSI line, generating a clock signal through a clock pin so that the slave device configures according to the command stream, and transmitting a completion mark to the main device through a MISO line after the configuration is completed.
Specifically, the MOSI line between the master device and the slave device is used for the master device to send signals to the slave device, and the MISO line is used for the slave device to send signals to the master device.
In particular, a clock signal is a periodic signal whose rising and falling edges can be used to synchronize and drive operations in a circuit. The clock pins are typically provided by a master device (e.g., a microprocessor or clock generator) and may be connected to clock inputs of various slave devices (e.g., registers, counters, memory, etc.). By means of the clock signal provided by the clock pin, the individual components in the overall circuit can operate and data transfer in coordination.
And S103, the master device finishes generating a clock signal and pulling down a chip select signal according to the received completion mark so as to communicate with the slave device.
The slave device sends a completion flag to the master device through the MISO line, and when the master device receives the completion flag, the master device indicates that the communication mode of the slave device is configured, and the master device and the slave device can communicate.
According to the steps S101-S103, the present application can control the corresponding slave device through the chip selection pin and the chip selection signal line of the master device (pull up the chip selection signal, the master device can perform configuration of the communication mode for the slave device, after completing the configuration, pull down the chip selection signal, the master device and the slave device can perform communication), realize the timing sequence of the data between the master device and the slave device through the clock signal of the clock line, ensure the accuracy and the synchronism of the data, and send command streams to configure the communication mode of the slave device through the MOSI line and the MISO line, thus, the communication mode of the slave device can be completed based on the hardware configuration pin or the connection line between the master device and the slave device, the data transmission is reliable and the transmission speed is fast, and the configuration speed of the communication mode of the slave device is improved.
In the embodiment of the present application, step S101 described in fig. 1 may further include, before the master device pulls up the chip select signal if the slave device configuration mode is selected to be a hardware configuration: the software configures the master device communication mode. Specifically, the configuration of the communication mode is performed on the master device according to the SPI protocol.
Correspondingly, in step S101, a command stream is generated according to a main device communication mode configured by software, and a specific implementation manner may be:
Identifying a communication mode of the main equipment configured by the software to obtain an identification result;
and generating a command stream according to the identification result.
In one possible implementation, the command stream includes a configuration for polarity, phase, transmit/accept, and full/half duplex.
In another possible implementation, the master device and slave device count down by a rising edge of a clock signal, and a falling edge count, before the pull-down chip select signal.
Specifically, the rising edge number refers to that when the rising edge of the clock signal arrives, the data of the transmission signal is transmitted to the receiving party so that the receiving party performs data acquisition. The number of falling edge acquisition means that when the falling edge of the clock signal arrives, the receiving party acquires the transmitted data and stores or carries out subsequent processing. The rising edge count and the falling edge count can ensure that data is correctly transmitted and collected at the stable moment of a clock signal. The rising and falling edges of the clock signal provide an explicit time reference enabling the sender and receiver to perform data transmission and acquisition at synchronized moments.
For example, referring to a timing diagram of one hardware configuration shown in fig. 2, when the chip select signal is pulled up, the master device may sequentially transmit a plurality of configuration data (for example, may correspond to a polarity of transmission at a rising edge of a 3 rd period, a transmission/reception at a rising edge of a 4 th period, a transmission full/half duplex at a rising edge of a 5th period, and a transmission/reception at a rising edge of a6 th period) in a command stream at a rising edge of a plurality of periods (for example, may correspond to a polarity of transmission at a falling edge of a 3 rd period, a transmission phase of transmission at a falling edge of a 4 th period, a transmission/reception at a falling edge of a 5th period, and a transmission full/half duplex at a falling edge of a6 th period) in succession in a clock signal through the MOSI line, so that the slave device receives the configuration data transmitted at the rising edge from the falling edge corresponding to each rising edge in turn (specifically, may correspond to the transmission polarity at the falling edge of the 3 rd period, for example), and performs the corresponding configuration. Then, when the slave device completes the configuration of the communication mode, the slave device transmits a completion flag to the master device through the MISO line (for example, see fig. 2, after completing the configuration of the command stream in the 6 th cycle, the slave device transmits a completion signal to the master device through the MISO line in the 8 th cycle). After receiving the completion flag through the MISO line, the master device pulls down the chip select signal sent to the slave device through the chip select pin. The master device and the slave device can perform data interaction communication.
Illustratively, the completion flag may be 2' b10.
Specifically, 2'b10 is a binary representation in which 2' b represents a value represented using two bits (bits) and 10 represents a value of a binary number. In 2' b10, the leftmost bit represents the most significant bit and the rightmost bit represents the least significant bit. Thus, 2' b10 represents a binary number, with the highest order being 1 and the lowest order being 0.
The communication mode method of the master-slave device of the SPI protocol provided by the embodiment of the application is described above, and an exemplary description is made below of the communication mode method of the master-slave device of the SPI protocol in combination with a specific application scenario.
Referring to fig. 3, a schematic application flow diagram of a communication mode of an SPI protocol master-slave device is shown. An application method of communication modes of SPI protocol master-slave equipment comprises the following steps:
s301, configuring a main device communication mode by software.
S302, if the configuration mode of the slave device is selected to be hardware configuration, the master device pulls up the chip select signal.
S303, identifying the communication mode of the main equipment configured by the software, and obtaining an identification result.
S304, generating a command stream according to the identification result, generating a clock signal and transmitting the command stream to the slave device through the MOSI line.
S305, after the slave device completes configuration according to the command stream, a completion mark is sent to the master device through the MISO line.
Specifically, referring to a schematic diagram of hardware configuration of an SPI master device and a slave device and command flow thereof shown in fig. 4, when a chip select signal is invalid, the master device needs to configure a communication mode for the slave device, and steps S302 to S305 described above may be performed, a master device generates a command flow according to the master device communication mode configured by software, then the master device generates a clock signal through a clock pin (SCLK), and sends the command flow to the slave device through a MOSI line, and the slave device sends a completion flag to the master device through a MISO line after receiving and configuring the command flow.
S306, if the configuration mode of the slave equipment is selected to be software configuration, completing the configuration of the slave equipment according to a standard SPI protocol;
S307, pull down the chip select signal, the master device and the slave device may communicate.
Specifically, referring to fig. 5, which shows a hardware configuration and communication schematic diagram of an SPI master device and a slave device, after the configuration of the communication mode of the SPI master device and the slave device is completed, the master device sends a clock signal when a chip select signal is valid, and then data can be transmitted between the master device and the slave device through a MISO line and a MOSI line.
As can be seen from the above steps S301 to S307, the master device of the present application may adopt a hardware configuration mode to configure the communication mode of the slave device (steps S302 to S305), or may adopt a software configuration mode to configure the communication mode of the slave device through a protocol (step S306).
The embodiments of the present application provide some specific implementation manners of a communication mode configuration method of an SPI protocol master-slave device, and based on this, the present application also provides a corresponding device. The apparatus provided by the embodiment of the present application will be described in terms of functional modularization.
Referring to fig. 6, a schematic structural diagram of a communication mode configuration device of an SPI protocol master-slave device, the communication mode configuration device of an SPI protocol master-slave device includes: a selection module 601, a command stream generation module 602, and a reception module 603;
The selecting module 601 is configured to pull up the chip select signal by the master device if the slave device configuration mode is selected to be hardware configuration;
the command stream generating module 602 is configured to generate a command stream according to a communication mode of a master device configured by software, where the master device sends the command stream to a slave device through a MOSI line, and generates a clock signal through a clock pin, so that the slave device configures according to the command stream, and sends a completion flag to the master device through a MISO line after the configuration is completed;
and the receiving module 603 is configured to end generating a clock signal and pulling down a chip select signal by the master device according to the received completion flag, so as to communicate with the slave device.
According to the above-mentioned communication mode configuration device of the SPI protocol master-slave device, it is known that the selection module 601 determines whether to select the software configuration or the hardware configuration for the slave device, then the command stream generating module 602 generates a command stream for configuring the communication mode for the slave device according to the communication mode of the master device, the master device generates a clock signal and sends the command stream to the slave device, the slave device completes the configuration of the communication mode according to the command stream, and the receiving module 603 informs the master device, so that the communication configuration of the master device to the slave device is completed rapidly and reliably based on the hardware structures such as the clock, the MOSI line, the MISO line, and the like.
In another possible implementation manner, the selection module 601 is further configured to perform the configuration of the slave device according to the standard SPI protocol if the slave device configuration mode is selected to be a software configuration.
Referring to fig. 7, the identifying module 604 is configured to identify a communication mode of a master device configured by software, obtain an identification result, and send the identification result to the command stream generating module.
The communication mode of the master device is identified through the identification module, so that the subsequent command stream generating module 602 correspondingly generates a command stream for configuring the communication mode for the slave device according to the configured communication mode of the master device, the slave device needs to establish communication connection with the master device, and the communication modes of the two parties need to be corresponding, so that communication errors are avoided.
Further, the command stream generating module 602 is further configured to receive the identification result, thereby generating a command stream according to the identification result (the host device communication mode configured by the software).
The command stream includes configurations for polarity, phase, transmit/accept, and full/half duplex.
Further, before the pulling down the chip select signal, the method further includes: the command stream generating module 602, the selecting module 601 and the slave device send out numbers through rising edges and falling edges of clocks.
Alternatively, the completion flag may be 2' b10.
The embodiment of the application also provides corresponding equipment and a computer storage medium, which are used for realizing the scheme provided by the embodiment of the application.
The device comprises a memory and a processor, wherein the memory is used for storing instructions or codes, and the processor is used for executing the instructions or codes so that the device executes the communication mode configuration method of the SPI protocol master-slave device.
The computer storage medium stores codes, and when the codes are executed, the equipment running the codes realizes the communication mode configuration method of the SPI protocol master-slave equipment.
The "first" and "second" in the names of "first", "second" (where present) and the like in the embodiments of the present application are used for name identification only, and do not represent the first and second in sequence.
From the above description of embodiments, it will be apparent to those skilled in the art that all or part of the steps of the above described example methods may be implemented in software plus general hardware platforms. Based on such understanding, the technical solution of the present application may be embodied in the form of a software product, which may be stored in a storage medium, such as a read-only memory (ROM)/RAM, a magnetic disk, an optical disk, etc., and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network communication device such as a router) to perform the method according to the embodiments or some parts of the embodiments of the present application.
In this specification, each embodiment is described in a progressive manner, and identical and similar parts of each embodiment are all referred to each other, and each embodiment mainly describes differences from other embodiments. In particular, for the device embodiments, since they are substantially similar to the method embodiments, the description is relatively simple, and reference is made to the description of the method embodiments for relevant points. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those of ordinary skill in the art will understand and implement the present invention without undue burden.
The foregoing description of the exemplary embodiments of the application is merely illustrative of the application and is not intended to limit the scope of the application.