The relationship between serial port communication and interruption
[Copy link]
Serial communication is divided into two parts: sending and receiving. Sending generally does not require interruption to complete, while receiving generally requires interruption to receive.
The sender can choose to use interrupts or not. The working scenario of using interrupts is: the sender first sets up interrupts and binds an interrupt handler, then the sender throws a frame of data to the transmitter, the transmitter takes a while to send this frame of data, during this time the sender's CPU can do other things, after the transmitter is finished sending, a TXD interrupt will be generated, this interrupt will cause the previously bound interrupt handler to execute, in the interrupt handler the CPU will switch back to continue to send a frame of data to the transmitter, then the CPU switches away; the working scenario of not using interrupts is: the sender disables the TXD interrupt in advance (of course, there is no need to give the corresponding interrupt handler), the sender's CPU sends a frame of data to the transmitter, then the transmitter takes a while to send this frame of data, during this time the CPU waits here (the CPU does not switch to do other things), after the sender finishes sending, the CPU gives it another frame of data to continue sending until all the data is sent. How does the CPU know that the transmitter has finished sending? It turns out that there is a status register. In the status register, there is a bit called the send buffer empty flag. When the transmitter completes the transmission (the send buffer is empty), this flag will be set to 1. The CPU knows whether the transmission has been completed by constantly checking whether this flag is 1 or 0.
The receiver can choose to use interrupts or not. The working scenario of using interrupts is: the receiver first sets up the interrupt and binds an interrupt handler, and then the receiver will spend some time to receive a frame of data from the receiver. During this time, the receiver's CPU can do other things. After the receiver completes receiving the data, an RXD interrupt will be generated. This interrupt will cause the previously bound interrupt handler to execute. In the interrupt handler, the CPU will switch back to read data from the receiver, and then the CPU will switch away; the working scenario of not using interrupts is: the receiver prohibits the RXD interrupt in advance (of course, there is no need to give a corresponding interrupt handler), the receiver will spend some time to receive a frame of data from the receiver, and the CPU will wait here during this time (the CPU does not switch to do other things). After the receiver completes receiving, the CPU will continue to wait until all data is received. How does the CPU know that the receiver has finished receiving? It turns out that there is a status register. In the status register, there is a bit called the receive buffer full flag. When the receiver completes the reception (the receive buffer is full), this flag will be set to 1. The CPU knows whether the reception has been completed by constantly checking whether this flag is 1 or 0.
Because serial communication is asynchronous, asynchronous means that the sender is in control. That is to say, the sender can send whenever he wants, but the receiver can only wait all the time to avoid losing data. Therefore, this difference leads to the sender not needing interrupts, while the receiver has to use interrupt mode.
Clock Design of Serial Communication Interface
Why does serial communication need a clock? Because serial communication requires a fixed baud rate, both the transmitter and the receiver need a clock signal.
Where does the clock signal come from? The source clock signal is provided to the serial port module by the external APB bus (PCLK_PSYS, 66MHz) (this is why we say that the serial port is hung on the APB bus), and then enters the serial port controller and is given to the baud rate generator (essentially a divider), where it is divided and a low-frequency clock is obtained. This clock is used for the transmitter and the receiver.
The setting of the clock in serial communication mainly depends on the register setting. The key points are: register source setting (selecting the source clock for the serial port controller, generally PCLK_PSYS, or SCLK_UART), and two registers of the baud rate generator. The baud rate
generator has two important registers: UBRDIVn and UDIVSLOTn, of which UBRDIVn is the main register for setting the baud rate, and UDIVSLOTn is used for auxiliary setting, the purpose is to calibrate the baud rate.
|