Inter-device communication method and device based on SPI bus
Technical Field
The invention relates to the technical field of communication, in particular to an inter-device communication method and device based on an SPI bus.
Background
SPI (Serial Peripheral Interface) is a high-speed, full-duplex, synchronous Serial communication bus. SPI is a serial interface protocol that uses 4 signal lines for communication, including master/slave modes, typically a master device and one or more slave devices, where the 4 signal lines are an SCLK signal line (serial clock signal line), an MOSI signal line (master data output, slave data input signal line), a MISO signal line (master data input, slave data output signal line), and a CS signal line (chip select signal line), respectively.
The SPI bus only occupies four wires on the pins of the chip, when the master device is connected with one slave device, the pins of the chip can be saved, and the space of a PCB (printed circuit board) is saved during wiring. However, as the number of slave devices increases, the number of signal lines increases, and each time a slave device is added, a CS signal line needs to be added, which increases the wiring space and increases the cost.
Disclosure of Invention
The invention aims to overcome the defects of the prior art and provides an inter-device communication method and device based on an SPI bus, which reduce the occupied wiring space.
In order to achieve the purpose, the invention provides the following technical scheme: an inter-device communication method based on an SPI bus comprises the following steps:
step S1, sequentially connecting a master device and a plurality of slave devices in series by adopting an SPI bus, wherein the SPI bus comprises an SCLK signal line, an MOSI signal line and a MISO signal line;
step S2, the master device controls each slave device to set a local address;
step S3, the master device reads data from the slave device or writes data into the slave device, and the master device sends an instruction containing address information and data information to all the slave devices;
step S4, the slave device judges whether the address information matches with the local address, if yes, step S5 is executed, otherwise, the slave device analyzes the data information in the instruction and makes the data information in the instruction in a bypass state;
in step S5, the slave device reads data and transmits the data to the master device or writes the data to the slave device.
Preferably, in step S2, the slave device setting the local address includes the steps of:
s201, the master device sends a command for setting the body address to the slave device;
s202, the slave device waits for receiving a non-zero value when receiving the command for setting the local address, takes the non-zero value as the local address, and adds one to the non-zero value and sends the non-zero value to the next-stage slave device.
Preferably, the slave device continuously sends zero to the next slave device when the slave device does not receive the non-zero value.
The invention also discloses a device based on the SPI bus, which comprises a master device and a plurality of slave devices, wherein the master device and the plurality of slave devices are sequentially connected in series through the SPI bus, and the SPI bus only comprises an SCLK signal line, an MOSI signal line and an MISO signal line.
Preferably, each slave device is provided with a local address, and the master device sets the local address by sending a set local address command to the slave device.
Preferably, each slave device is connected to an auxiliary device, each slave device being configured as an SPI bridge between the master device and the auxiliary device.
Preferably, when the SPI bridge function of the slave device is disabled, the slave device transmits the received address information directly to the next-stage slave device.
The invention has the beneficial effects that:
the communication method and the device between the devices based on the SPI bus only adopt an SCLK signal line, an MOSI signal line and an MISO signal line to connect the main device and a plurality of slave devices in series in sequence, and realize the determination of the slave devices and the data reading and writing in a device address addressing mode, simultaneously, the main device can access any slave device in a link in one period through a bypass technology, and can access all the slave devices at the same time through a broadcasting mode, thereby omitting a chip selection signal line, reducing the occupation of wiring space and simultaneously ensuring the access speed.
Drawings
FIG. 1 is a flow chart diagram of the method of inter-device communication of the present invention;
fig. 2 is a block diagram illustrating the structure of the present invention.
Detailed Description
The technical solution of the embodiment of the present invention will be clearly and completely described below with reference to the accompanying drawings of the present invention.
The invention discloses a method and a device for communication among equipment based on an SPI bus, which only adopt an SCLK (Serial clock) signal line, an MOSI (Master Output/Slave Input ) signal line and an MISO (Master Input/Slave Output, Master data Input/Slave Output) signal line to connect the Master equipment and a plurality of Slave equipment in series in sequence, and realize the determination of the Slave equipment and the reading and writing of data through an equipment address addressing mode, thereby omitting a chip selection signal line and further reducing the occupation of wiring space.
As shown in fig. 1, an SPI bus based inter-device communication method includes the following steps:
step S1, sequentially connecting a master device and a plurality of slave devices in series by adopting an SPI bus, wherein the SPI bus comprises an SCLK signal line, an MOSI signal line and a MISO signal line;
specifically, in order to save the wiring space, in the present invention, the SPI bus only occupies three signal lines, namely, an SCLK signal line, an MOSI signal line, and a MISO signal line, on the pin of the Chip, and the master device and the plurality of slave devices are sequentially connected in series through the SCLK signal line, the MOSI signal line, and the MISO signal line, so that a CS (Chip Select) signal line (i.e., a Chip Select signal line) is omitted, and the problem that one CS signal line is correspondingly added for each device added in the prior art is avoided, thereby saving the wiring space and saving the cost. Wherein, the SCLK signal line is a serial clock signal line, and the serial clock signal is generated by the master device; MOSI signal line is the data output of the main equipment, the slave equipment data input signal line; the MISO signal line is the master data input, and the slave data output signal line.
The SPI buses are ultimately connected in series to form the topology shown in fig. 2. The master device and the first slave device, the first slave device and the second slave device, and so on, the N-1 th slave device and the nth slave device are all connected through the SCLK signal line, the MOSI signal line and the MISO signal line.
Step S2, the master device controls each slave device to set a local address;
in particular, since the CS signal line is omitted, when the slave device is determined, the slave device is determined by the device address addressing mode. Each slave device has a device address, hereinafter referred to as a local address, which can be set by the master device.
When a local address is set for each slave device, the master device can set a local address for each slave device through the command of setting the body address, wherein the format of the command of setting the local address is as follows:
0xA0- > n 0x00- >0xMM, MM being address data.
When the slave device receives a command for setting the local address, the slave device waits for receiving a non-zero value, takes the non-zero value as the local address, and adds one to the non-zero value and outputs the non-zero value to the next-stage slave device. When the slave device does not receive the non-zero value, the slave device always outputs zero to the next slave device.
Take the topology shown in fig. 2 as an example. When the master device sends a command for setting a local address, the first slave device first receives the command and waits for receiving a non-zero value, and if the received non-zero value is 2, the non-zero value 2 is used as the local address of the first slave device. Meanwhile, the first slave device adds one to the non-zero value 2 and outputs the value to the second slave device, that is, 3 is output to the second slave device, the second slave device takes the non-zero value 3 as a local address, adds one to the non-zero value 3 and outputs the value to the third slave device, and so on, the non-zero value received by the nth slave device is N +1, and therefore, the local address of the nth slave device is N + 1. In specific implementation, the local address is expressed by a 16-ary number.
Step S3, the master device reads data from the slave device or writes data into the slave device, and the master device sends an instruction containing address information and data information to all the slave devices;
step S4, the slave device judges whether the address information matches with the local address, if yes, step S5 is executed, otherwise, the slave device analyzes the data information in the instruction and makes the data information in the instruction in a bypass state;
in step S5, the slave device reads data and transmits the data to the master device or writes the data to the slave device.
Specifically, when the master device reads data from the slave devices, the master device has determined from which slave device the data is to be read, and the master device generates an instruction containing address information and data information to all the slave devices. As shown in fig. 2, the topology structure adopts a serial connection manner, and in a default case, the slave devices are in a Bypass (Bypass) state, and in one clock cycle, all the slave devices can receive the instruction sent by the master device, and the slave devices determine whether address information in the instruction matches the local address, and if not, continue to analyze data information in the instruction and continue to maintain the Bypass state. When the slave device judges that the address information matches the local address, the slave device does not continue to maintain the bypass state, and the slave device acquires data from the memory and transmits the data to the master device.
Similarly, when the master device writes data to the slave device, the master device has already determined which slave device to read the data to, and the master device generates an instruction containing address information and data information to the slave device. And if the address information in the instruction is not matched with the local address, continuously analyzing the data information in the instruction and continuously keeping the Bypass state. When the slave device judges that the address information matches the local address, the slave device is not continuing to maintain the bypass state, and the slave device writes data into the memory.
In this embodiment, when a slave device is in the bypass state, the instruction received by the slave device is directly transferred to the next slave device.
In specific implementation, the master device may send the following instructions to read data in the slave device:
0xAF- > local address- > register address- > register data, wherein localadress is a local address of slave equipment of data to be read, namely address information; the register address is an address for storing data to be read in the memory; the register data is data to be read, i.e., data information.
Likewise, the master may send a command to write data to the slave as follows:
0xAF- > local address- > register address- > register data, wherein localadress is a local address of slave equipment to be written with data, namely address information; register address is the memory address where the data to be written is stored; the register data is data to be written, i.e., data information.
As shown in fig. 2, the present invention further discloses an SPI bus-based device, which comprises a master device and a plurality of slave devices, wherein the master device and the plurality of slave devices are sequentially connected in series through an SPI bus, and the SPI bus comprises an SCLK signal line, an MOSI signal line, and a MISO signal line.
Specifically, the SPI bus only occupies three signal lines, namely, an SCLK signal line, an MOSI signal line and a MISO signal line, on the pin of the chip, and the master device and the plurality of slave devices are sequentially connected in series through the SCLK signal line, the MOSI signal line and the MISO signal line, thereby omitting a CS signal line, avoiding the problem that one CS signal line is correspondingly added for each device in the prior art, further saving the space for wiring, and saving the cost.
Further, each slave device has a device address, hereinafter referred to as a local address, which can be set by the following command:
0xA0- > n 0x00- >0xMM, MM being address data.
When the slave device receives a command for setting the local address, the slave device waits for receiving a non-zero value, takes the non-zero value as the local address, and adds one to the non-zero value and outputs the non-zero value to the next-stage slave device. When the slave device does not receive the non-zero value, the slave device always outputs zero to the next slave device.
As shown in fig. 2, after the master device issues the command for setting the local address, the first slave device first receives the command, and waits for receiving a non-zero value, and if the received non-zero value is 1, the non-zero value 1 is used as the local address of the first slave device. Meanwhile, the first slave device adds one to the non-zero value 1 and outputs the value to the second slave device, that is, 2 is output to the second slave device, the second slave device takes the non-zero value 2 as a local address, adds one to the non-zero value 2 and outputs the value to the third slave device, and so on, the non-zero value received by the nth slave device is N, and therefore, the local address of the nth slave device is N. In specific implementation, the local address is expressed by a 16-ary number.
Further, the invention carries out the determination of the slave device and the reading or writing of the data in a device address addressing mode. As shown in fig. 2, when the master device reads data from or writes data to the slave device, the master device generates an instruction containing address information and data information and transmits the instruction to the slave device. As shown in fig. 2, the topology structure adopts a serial connection manner, and in a default case, the slave devices are in a Bypass (Bypass) state, and in one clock cycle, all the slave devices can receive the instruction sent by the master device, and the slave devices determine whether address information in the instruction matches the local address, and if not, continue to analyze data information in the instruction and continue to maintain the Bypass state. When the slave device judges that the address information matches the local address, the slave device does not continue to maintain the bypass state, and the slave device performs reading or writing of data.
Further, each slave device may also communicate with an auxiliary device individually, in which case each slave device acts as an SPI bridge between the master device and the auxiliary device. When the SPI bridge function of a certain slave device is not enabled, the slave device does not match the received address information with the local information and directly sends the address information to the next-stage slave device.
In specific implementation, the master device may enable the SPI bridge function of a slave device by the following command:
0xA5- > local address- > open time, wherein the local address is a local address of a slave device of which the SPI bridge function is to be started; open time is the on-time, where the on-time is calculated in bytes. When local address is 0, the SPI bridge function of all slave devices is enabled. When the SPI bridge function of a slave is enabled, the master device may perform data interaction with the auxiliary device to which the slave device is connected.
Therefore, the scope of the present invention should not be limited to the disclosure of the embodiments, but includes various alternatives and modifications without departing from the scope of the present invention, which is defined by the claims of the present patent application.