Add a handshake signal.
(In addition, data for checking may be added to the data itself to improve the reliability of communication, but this is an auxiliary method.
Basically, generation of differences in timing should be suppressed.)
To transmit data, it is usually necessary to check whether the other party is ready.
However, such a function is not provided to serial communication signals themselves (except the serial bus), and therefore, a signal (called a handshake signal) must be separately prepared.
[Example of control signals]
For this purpose, the following signals are used by modems.
RTS (Request to Send)
CTS (Clear to Send)
The RTS signal is asserted when data is to be transmitted, notifying the receiver that the transmitter wishes to transmit data.
The receiver, when it is ready for reception, asserts the CTS signal, notifying the transmitter that it may transmit data.
The sequence at this time is illustrated below.
These handshake signals used by modems cannot be used for delicate control of communication as it is.
Therefore, the definition of the signals is changed for local communication between microcontrollers, as follows.
RTS:The master outputs a low level when it wishes to transmit data.
The slave judges the falling edge to be a request for transmission, and the rising edge as the end of transmission.
CTS:The slave outputs a falling edge when it is ready for reception.
(The slave outputs a high level when it has received data once, and outputs a falling edge when it has been ready for receiving the next data.)
The master transmits data of one character when it detects the falling edge of this signal.
The above connection is made to process these signals.
The master outputs the RTS signal by using a port, and the slave monitors this signal by using an edge detection interrupt.
Because the RTS signal uses both the rising and falling edges, the edge detection interrupt and port are used to read the status of the port.
The CTS signal uses only the falling edge.
Therefore, the signal output by the slave through port is monitored by the master by using a falling edge detection interrupt.
[Transmission processing procedure using control signals]
The above processing procedure is explained as follows.
It is assumed that the ports of both the master and slave are high in the default status.
(1) The master sends the RTS signal (falling) to the slave through port before transmitting data, and waits for permission for transmission from the slave.
(2) The slave checks the transmission request from the master by generating the edge detection interrupt and checking the level of the RTS signal, and prepares for reception (starting reception) (even if the slave starts reception, actual transfer is not executed unless the master sends SCK (serial clock)).
(3) The slave sends the falling edge of the CTS signal to the master, notifying the master that the slave is ready for reception, by outputting a low level to the port when it has started reception.
(4) The master transmits data when it has detected the falling edge of the CTS signal from the slave.
(5) The slave receives data from the master.
(6) The master waits for the transmission end interrupt.
When transmission is completed, the master judges whether it should transmit the next data.
If the next data is present, the master returns to waiting for the falling of the CTS signal in (3).
(7) The slave outputs a high level to the port, raises the CTS signal, and processes the received data when it has completed reception.
(8) The master, when it has completed transmission, raises the RTS signal, notifying the slave that the master has completed transmission.
(9) The slave processes the received data, and completes transfer when it has confirmed that the RTS signal from the master has risen, by the edge detection interrupt and level checking.
If the RTS signal does not rise, the slave returns to the next reception.
In this example, the RTS signal is used.
If the slave is always ready for reception and there is no problem (however, the actual communication has not yet been executed), the RTS signal is not necessary.
Transmission by the slave can also be realized by similar processing.
In this case, the RTS signal means a request for data from the slave, and the CTS signal means that the slave is ready for transmitting the data.