Summary of the invention
The technical problem to be solved in the present invention provides a kind of SPI communication interface and utilizes it to guarantee the means of communication of the data integrity of the SPI communication between single-chip microcomputer or between other programmable chips, the current task of avoiding being in the SPI communication programmable chip of slave status is interrupted, also avoid the data of communication transfer that the problems such as dislocation occur, improved the operation efficiency of transfer efficiency and processor.
For achieving the above object, the technical scheme that adopts is: a kind of SPI communication interface of guaranteeing data integrity, main frame and at least one slave, MISO line, MOSI line, SCK line and CS line of comprising the SPI communication, it is characterized in that: every described slave has a Ready line to be connected with described main frame, is used for guaranteeing the data integrity of SPI communication.
Use above-mentioned SPI communication interface, the first is guaranteed the means of communication of data integrity, it is characterized in that, may further comprise the steps:
(1) the slave data acquisition is complete and data are put into SPI send the buff district, when being ready for the SPI communication, drags down the Ready line;
When (2) Host Detection is low to certain slave Ready line, drag down CS line corresponding to this slave;
(3) main frame and this slave carry out the SPI communication;
(4) communication is complete, and main frame is drawn high the CS line, and slave is drawn high the Ready line.
Use above-mentioned SPI communication interface, the second is guaranteed the means of communication of data integrity, it is characterized in that, may further comprise the steps:
(1) the slave data acquisition is complete and data are put into SPI send the buff district, when being ready for the SPI communication, drags down the Ready line;
When (2) Host Detection is low to certain slave Ready line, drag down CS line corresponding to this slave;
(3) this slave and main frame begin the SPI communication, transmit a byte;
(4) judge whether data transmission is complete, end of transmission then goes to step (7), the not complete step (5) that then goes to;
(5) slave is put into the SPI data register with next byte, and the level of the Ready line that overturns;
(6) main frame judges according to the byte number that transmits whether Ready line level is correct, correctly then goes to step (3), the incorrect step (7) that then goes to;
(7) main frame is drawn high the CS line, and slave is drawn high the Ready line, and communication finishes.
Good effect of the present invention is: by set up a Ready line between the main frame of SPI communication and every slave, make slave have initiative to communication, interrupted the impact of current operation when having eliminated slave passive receive communication instruction in the existing SPI communication.Simultaneously because the existence of CS line, the main frame of SPI communication is not lost the control to communication yet, main frame and slave can not interrupt other tasks of oneself because of the passive communication command of accepting, thereby the quality of SPI communication is significantly improved, guarantee the data integrity of SPI communication, also improved the work efficiency of the CPU of the main frame of SPI communication and slave.
Embodiment
A kind of SPI communication interface of guaranteeing data integrity, main frame and at least one slave, MISO line, MOSI line, SCK line and CS line of comprising the SPI communication, it is characterized in that: every described slave has a Ready line to be connected with described main frame, is used for guaranteeing the data integrity of SPI communication.
Use above-mentioned SPI communication interface, the first is guaranteed the means of communication of data integrity, it is characterized in that may further comprise the steps:
(1) the slave data acquisition is complete and data are put into SPI send the buff district, when being ready for the SPI communication, drags down the Ready line;
When (2) Host Detection is low to certain slave Ready line, drag down CS line corresponding to this slave;
(3) main frame and this slave carry out the SPI communication;
(4) communication is complete, and main frame is drawn high the CS line, and slave is drawn high the Ready line.
Use above-mentioned SPI communication interface, the second is guaranteed the means of communication of data integrity, it is characterized in that, may further comprise the steps:
(1) the slave data acquisition is complete and data are put into SPI send the buff district, when being ready for the SPI communication, drags down the Ready line;
When (2) Host Detection is low to certain slave Ready line, drag down CS line corresponding to this slave;
(3) this slave and main frame begin the SPI communication, transmit a byte;
(4) judge whether data transmission is complete, end of transmission then goes to step (7), the not complete step (5) that then goes to;
(5) slave is put into the SPI data register with next byte, and the level of the Ready line that overturns;
(6) main frame judges according to the byte number that transmits whether Ready line level is correct, correctly then goes to step (3), the incorrect step (7) that then goes to;
(7) main frame is drawn high the CS line, and slave is drawn high the Ready line, and communication finishes.
Fig. 1 is the hardware principle block diagram of existing SPI communication, Fig. 2 is the hardware principle block diagram of SPI communication of the present invention, comparison diagram 1 and Fig. 2 are as seen, SPI communication of the present invention has been Duoed a Ready line than existing SPI communication at hardware, the signal of this line is exported by slave, be input to main frame, that slave is to the control line of whole SPI communication, this line makes main frame in whole SPI communication initiative arranged, therefore, the SPI communication command that no longer sent by main frame such as the task of the current execution of slave such as data collection interrupts.
Fig. 3 is the byte sequential chart of the SPI means of communication of the present invention, by finding out in the sequential chart, only the CS line just can drag down after the Ready line drags down, and only in the situation that Ready and CS line all be low CLK just can be effective, thereby data just can be effective, the just existence of Ready line and CS line, just make main frame and the slave both sides of SPI communication that ownership has been arranged, main frame and slave can not interrupt other tasks of oneself because of the passive communication command of accepting, thereby can guarantee data and other data integrities and the true and accurate of SPI communication.
Sequential chart when the present invention has two kinds of communication mode: Fig. 4 to be the first means of communication multibyte communication of the present invention, the sequential chart when Fig. 5 is the second means of communication multibyte communication of the present invention.The something in common of two kinds of communication modes is, slave has the Ready control line, all communication is had control; Difference is that the first pattern Ready line only overturns once, and beginning drags down before the communication, and communication is complete draws high, and is applicable to the demanding situation of communication speed, and main frame, slave can be with rapid data transmission means such as DMA; And the second pattern is overturn once at each byte end of transmission Ready line, in this case, main frame needed to judge the level of Ready line before each the byte transmission except first byte, thereby more can guarantee the correctness of communication, but the defective of this pattern is, main frame all needs to judge the state of Ready line at every turn because each slave all will overturn the Ready line, thereby has reduced the communication speed of SPI, and can not use the high speed means of communication such as DMA.So can come choice for use the first pattern or the second pattern according to actual conditions in use.
The process flow diagram of slave when Fig. 6 is the first means of communication multibyte communication of the present invention, the process flow diagram of main frame when Fig. 7 is the first means of communication multibyte communication of the present invention.As shown in the figure, when the slave DSR, drag down the Ready line; When Host Detection is low to certain slave Ready line, drag down CS line corresponding to this slave; Main frame and this slave carry out the SPI communication; Communication is complete, and main frame is drawn high the CS line, and slave is drawn high the Ready line.
The process flow diagram of slave when Fig. 8 is the second means of communication multibyte communication of the present invention, the process flow diagram of main frame when Fig. 9 is the second means of communication multibyte communication of the present invention.As shown in the figure:
(1) the slave data acquisition is complete and data are put into SPI send the buff district, when being ready for the SPI communication, drags down the Ready line;
When (2) Host Detection is low to certain slave Ready line, drag down CS line corresponding to this slave;
(3) this slave and main frame begin the SPI communication, transmit a byte;
(4) judge whether data transmission is complete, end of transmission then goes to step (7), the not complete step (5) that then goes to;
(5) slave is put into the SPI data register with next byte, and the level of the Ready line that overturns;
(6) main frame judges according to the byte number that transmits whether Ready line level is correct, correctly then goes to step (3), the incorrect step (7) that then goes to;
(7) main frame is drawn high the CS line, and slave is drawn high the Ready line, and communication finishes.
Process flow diagram by above-mentioned two kinds of communication modes can be found out, before each communication begins, all to operate the Ready line, this is that slave can select when to begin communication, and main frame is because the existence of CS line, do not lose the control to communication yet, like this slave can data acquisition or other tasks finish control afterwards Ready line notice main frame can communication, finish and control afterwards the CS line and begin communication and main frame also can be chosen in other tasks, the control that all has of main frame and slave makes the communication controllability that becomes very strong like this, other operations of main frame and slave can not interrupted, guaranteed the data integrity that is designed into of other operations.
Experimental verification:
Test for performance of the present invention, compare with existing SPI communication modes, both hardware circuits are identical, all adopting main frame is that LPC2136, two slaves are STM32F103, main frame carries out multiple communication simultaneously, and slave carries out the meter of pulsed quantity simultaneously to be measured frequently, and measurement range is 1-10000Hz.
Experimental results show that, when adopting traditional SPI means of communication, slave is because the passive communication command of accepting, when pulsed frequency is higher or pulsed frequency lower the time, the frequently operation meeting of the meter of slave is interrupted by the SPI communication, thereby causes result of calculation to follow the signal source of input inconsistent.When adopting the SPI means of communication of the present invention, slave is chosen in to calculate and drags down the Ready line after complete and begin communication, and through observing for a long time, the result of calculation of slave is consistent with the signal of input, and in measurement range, error is ± 1Hz.
By experimental result as can be known, the present invention can guarantee the correctness of other operations of SPI communication two party, can guarantee that the SPI communication two party carries out the complete of the data that relate to when other calculate.