In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains the "Linux tty architecture and UART driver knowledge points", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what are the Linux tty architecture and UART driver knowledge points" bar!
First, module hardware learning
1.1. Uart introduction
Universal Asynchronous Transceiver (Universal Asynchronous Receiver/Transmitter), commonly known as UART, is an asynchronous transceiver that is part of computer hardware. The data to be transmitted is converted between serial communication and parallel communication.
As a chip that converts parallel input signals into serial output signals, UART is usually integrated into other communication interfaces.
UART is a universal serial data bus for asynchronous communication. The bus communicates in both directions and can realize full-duplex transmission and reception. In embedded devices, UART is used for communication between host and auxiliary devices, such as communication between car sound and external AP, communication with PC machine, including communication with monitoring debugger and other devices, such as EEPOM.
1.1.1. Communication protocol
As one of the asynchronous serial communication protocols, UART works by transmitting each character of the transmitted data bit by bit. The significance of each of you is as follows:
Start bit: first send a logical "0" signal to indicate the beginning of the transmission character.
Data bit: immediately after the start bit. The number of data bits can be 5, 6, 7, 8, etc., forming a character. ASCII code is usually used. The transmission starts from the lowest level and is located by the clock.
Parity bit: when this bit is added to the data bit, the number of digits of "1" should be even (even) or odd (odd), so as to verify the correctness of data transmission.
Stop bit: it is the end flag of a character data. Can be 1-bit, 1.5-bit, 2-bit high level.
Because the data is timed on the transmission line, and each device has its own clock, there is likely to be a small out-of-sync between the two devices in communication. Therefore, the stop bit not only indicates the end of the transmission, but also provides the computer with the opportunity to correct clock synchronization. The more bits applicable to stop bits, the greater the tolerance of different clock synchronization, but the slower the data transfer rate.
Free bit: in a logical "1" state, indicating that there is no data transfer on the current line.
Uart transmits data as shown in figure 2-1:
1.1.2. Baud rate
The baud rate is an indicator of the data transmission rate. Represents the number of symbols transmitted per second (symbol). The amount of information (bits) represented by a symbol is related to the order of the symbol. For example, if 256th-order symbols are used for transmission, each 8bit represents a symbol, and the data transfer rate is 120characters per second, then the baud rate is 120baud and the bit rate is 120*8=960bit/s. The concepts of the two are easy to get wrong.
The reception and transmission of UART are sent and received at the same baud rate. The clock frequency generated by the baud rate generator is not the baud rate clock frequency, but 16 times of the baud rate clock frequency. The purpose is to accurately sample during reception to extract asynchronous serial data. According to the given crystal oscillator clock and the required baud rate, the baud rate frequency division count can be calculated.
1.1.3. working principle
The process of sending data: idle state, the line is at high potential; when receiving the transmission data instruction, pull down the time T of one data bit of the line, then the data bit is sent sequentially from low bit to high bit, after the data is sent, then send parity bit and stop bit (stop bit is high potential), and a frame of data transmission ends.
The process of receiving data: idle state, the line is at high potential; when the falling edge of the line is detected (the line potential changes from high potential to low potential), it shows that the line has data transmission, and the data is received from low to high according to the agreed baud rate. after the data is received, the parity check bit is received and compared, and if it is correct, the subsequent equipment is informed to prepare to receive the data or to store it in the cache.
Because UART is an asynchronous transfer, there is no synchronous clock transmission. In order to ensure the correctness of data transmission, UART uses a clock with 16 times data baud rate for sampling. Each data has 16 clock samples, take the middle sampling value to ensure that the sampling will not slip code or error code.
Generally speaking, the data bit of a UART frame is 8, so that even if each data has a clock error, the receiver can sample the data correctly.
The time sequence of UART receiving data is as follows: when the falling edge of data is detected, it indicates that there is data transmission on the line, then the counter CNT begins to count, when the counter is 8, the value of sampling is "0" indicates the start bit; when the counter is 24: 161 / 8, the value of sampling is bit0 data; when the value of counter is 40 / 162 / 8, the value of sampling is bit1 data; and so on, the next six data are sampled. If the parity bit is required, when the value of the counter is 152-169-8, the sampled value is parity; when the counter is 168-1610-8, the sampled value of "1" indicates the stop bit, and one frame of data is sent and received.
1.1.4. RS232 and RS485
UART: generally speaking, UART refers to a serial communication protocol that specifies the data frame format, baud rate, and so on.
RS232 and RS485: are two different electrical protocols, that is, regulations on electrical and physical properties, which act on the transmission path of data and do not include the processing of data.
The corresponding physical devices are RS232 or RS485 driver chips, which drive the voltage signals transmitted by CPU through UART into RS232 or RS485 level logic.
The RS232 uses an effective level of 3-15V, while UART, because there is no specification for electrical characteristics, directly uses the level used by the CPU, that is, the TTL level (between 0 and 3.3V).
More specifically, the electrical characteristics also determine the connection mode of the line, such as RS232, which stipulates that the data is represented by the level, so the line is single-line, and two lines can achieve the purpose of full-duplex; RS485 uses differential levels to represent data, so two lines must be used to achieve the basic requirements of data transmission, and four lines must be used to achieve full-duplex.
The difference between RS232 and RS485 (1) Anti-interference
RS485 interface is a combination of balanced driver and differential receiver, which has the ability to suppress common mode interference and strong anti-noise interference.
The RS232 interface uses a signal line and a signal return line to form the form of ground-to-ground transmission, which is easy to produce common-mode interference, so the anti-noise interference is weak. (2) Transmission distance
The maximum transmission distance of the RS485 interface is 1200 meters (at 9600bps), which can actually reach 3000 meters.
The transmission distance of RS232 is limited, and the maximum transmission distance is 50 meters. In fact, it can only use about 15 meters. (3) Communication ability
The RS485 interface can connect up to 128transceivers on the bus, that is, it has the ability of multiple stations, and such users can use a single RS485 interface to set up a device network conveniently.
RS232 only allows one-to-one communication. (4) Transmission rate
RS232 transmission rate is low, in asynchronous transmission, the baud rate is 20Kbps.
The highest data transmission rate of RS485 is 10Mbps. (5) signal line
RS485 full duplex: uart-tx 1 wire, becomes RS485- Amax B2 line; uart-rx 1 line, becomes RS485- xUnix y 2 line
RS485 half-duplex: combine full-duplex Ahambo B; XUnip Y for time-sharing multiplexing.
RS232 only allows one-to-one communications (6) Electrical level values
Logic "1" is expressed by the voltage difference between two lines is + (2-6) V, and logic "0" is represented by the voltage difference between two lines is-(2-6) V.
In RS232, the voltage of any signal is negative logic relation. That is, logic "1"-5-15V; logic "0", + 5cm 15V, noise tolerance is 2V. That is to say, the receiver is required to recognize the signal as low as + 3V as logic "0" and the signal as high as-3V as logic "1".
The signal level of the RS232 interface is high, which is easy to damage the chip of the interface circuit, and because it is not compatible with the TTL level, the level conversion circuit can be used to connect with the TTL circuit.
If the signal level of the RS485 interface is lower than that of the RS232, it is not easy to damage the chip of the interface circuit, and the level is compatible with the TTL level, so it is convenient to connect with the TTL circuit.
1.1.5. Flow control
When data is transmitted through two serial ports, there is often the phenomenon of data loss, or the processing speed of the two computers is different, such as the communication between the desktop and the single-chip microcomputer, the data buffer at the receiving end is full, and the data that continues to be sent will be lost. Flow control can solve this problem. When the data is no longer processed by the receiver, it sends a "no longer receive" signal, and the sender stops sending. Do not send data until you receive a "can continue to send" signal.
Therefore, flow control can control the process of data transmission and prevent data loss. Two kinds of flow control commonly used in PC are hardware flow control (including RTS/CTS, DTR/CTS, etc.) and software flow control XON/XOFF (continue / stop).
There are two kinds of hardware flow control: RTS/CTS flow control and DTR/DSR flow control.
DTR- data Terminal Readiness (Data Terminal Ready) is low efficient. When low, it indicates that the device itself is ready. This signal is output to the peer device, and the peer device is used to determine whether to communicate with the device.
The DSR- data device ready (Data Set Ready) is low efficient, and this signal is provided by the peer device to which the device is connected. When it is low, the device can communicate with the device side.
RTS-request to send (data) (Request To Send) is inefficient, this signal is set by this device when it needs to send data to the peer device. When low, it indicates that the device has data to be sent to the peer device. Whether the end-to-end device can receive the transmitted data of its own party, it will be answered by CTS signal.
CTS-receive and send (request) (Clear To Send) is inefficient, and whether the peer device can receive the data sent by this party is determined by the CTS. If the CTS is low, it means that the peer is ready to receive the data sent by the local end.
Based on RTS/CTS flow control analysis, analyze the host send / receive process:
Physical connection
The RTS (output signal) of the host and the CTS (input signal) connected to the slave. The host is the CTS (input signal), which is connected to the RTS (input signal) of the slave.
1. The sending process of the host: the host queries the CTS pin signal of the host, which is connected to the RTS signal of the slave and is controlled by the slave. If the host CTS signal is valid (low), it means that the receiving FIFO of the slave is not full, and the slave can receive it. At this time, the host can send data to the slave, and always query whether the CTS signal is valid during the sending process. When the host queries that the CTS is invalid, the sending is aborted. When will the host's CTS signal be invalid? When the slave receives the data sent by the host, if the FIFO of the receiver module of the slave is full, the slave RTS will be invalid, that is, the CTS signal of the host is invalid. When the host query CTS is invalid, the host sends abort.
two。 Host reception mode: if the host receives FIFO is not full, then make the host RTS signal valid (low), that is, the slave CTS signal is valid. At this point, if the slave is to be sent, the CTS signal of the slave will be queried before it is sent, and if it is valid, the transmission will begin. And in the process of transmission, the valid status of the slave CTS signal should be queried all the time, and if it is invalid, the transmission will be terminated. Whether it is effective or not is determined by the RTS signal of the host. If the host FIFO is full, the host RTS signal is invalid, that is, the slave CTS signal is invalid, and the host reception is aborted.
Due to the limitation of cable, hardware flow control is not used in ordinary control communication, but software flow control is used.
Software flow control is generally realized through XON/XOFF. The common method is to stop sending data immediately after sending XOFF characters to the data sender when the amount of data in the input buffer of the receiver exceeds the set high level.
When the amount of data in the input buffer of the receiver is lower than the set low bit, the XON character (decimal 17 or Control-Q) is sent to the data sender, and the sender begins to send data immediately after receiving the XON character.
Generally, it can be found from the source program of the equipment that the sender sends data as soon as it receives the XON character. Generally, you can find out what bytes are sent from the source program of the device.
It should be noted that if binary data is transmitted, flag characters may also appear in the data stream and cause misoperation, which is a defect of software flow control, while hardware flow control will not have such a problem.
II. Linux serial framework
In the Linux system, the terminal is a character device, it has many types, usually use tty (Teletype) to abbreviate various types of terminal devices.
For embedded systems, the most commonly used is Uart (Universal Asynchronous Receiver/Transmitter), serial port, which is referred to as port in daily life.
2.1. TTY driver framework
2.1.1. TTY concept
A serial port terminal is a terminal device connected by a computer serial port. Linux treats each serial port as a character device. The device name corresponding to these serial ports is / dev/ttySAC*
In Linux systems, the output device of a computer is usually called a console terminal, which specifically refers to the output of printk information to the device. / dev/console is a virtual device that needs to be mapped to a real tty, such as mapping console to serial port 0 through the kernel startup parameter "console=ttySCA0".
When a user logs in, a virtual terminal is used. When using the Ctcl+ alt [F1-F6] key combination, we can switch to tty1, tty2, tty3 and so on. Tty* is called a virtual terminal, and tty0 is an alias for the virtual terminal currently in use.
2.1.2. TTY architecture analysis
The entire tty architecture is roughly shown in figure 3.1. it can be divided into two layers, one layer is the lower layer of our serial port driver layer, which is in direct contact with the hardware, we need to fill a struct uart_ops structure, and the other layer is the upper tty layer, including the tty core and line rules, each of which has an Ops structure, and the user space is accessed through a character device node registered by tty.
Figure 3.1tty architecture diagram
As shown in figure 3.2, the process of sending data to a tty device is as follows: the tty core obtains the data to be sent to a tty device from a user, and the tty core passes the data to the tty line discipline driver, which then passes the data to the tty driver, which converts the data into a format that can be sent to the hardware.
The process of receiving data is as follows: the data received from the tty hardware is handed up to the tty driver, then into the tty line discipline driver, and then into the tty core, where it is obtained by a user.
Figure 3.2 data sending and receiving flow of tty device
2.2. Key data structure
2.2.1. Struct uart_driver
Uart_driver contains information such as serial device name, serial driver name, primary and secondary device number, serial console (optional), and encapsulates tty_driver (the underlying serial driver does not need to care about tty_driver).
Struct uart_driver {struct module * owner; / * owns the module of the uart_driver, which is usually the THIS_MODULE*/ const char * driver_name; / * driver serial port name. The serial device name is based on the driver name * / const char * dev_name; / * serial device name * / int major. / * Primary device number * / int minor; / * Secondary device number * / int nr; / * the number of serial ports supported by the uart_driver * / struct console * cons; / * its corresponding console, if the uart_driver supports serial console, * otherwise it is NULL*/ / * * these are private; the low level driver should not * touch these They should be initialised to NULL * / struct uart_state * state; / * lower layer, window driver layer * / struct tty_driver * tty_driver; / * tty related * /
2.2.2. Struct console
The structure that must be registered in order to print the console
Struct console {char name [16]; void (* write) (struct console *, const char *, unsigined); int (* read) (struct console *, char *, unsigned); struct tty_driver * (struct console *, int*); void (* unblank) (void); int (* setup) (struct console *, char *); int (* early_setup) (void); short flags; short index / * it is used to specify which uart port (line in the corresponding uart_port) is used for the console. If it is-1, the first uart port*/ int cflag; void * data; struct console * next;} will be selected automatically.
2.2.3. Struct uart_state
Each uart port corresponds to a uart_state, which connects the uart_port to the corresponding circ_buf. Uart_state has two members that are used by the underlying serial port drivers: xmit and port.
When the user space program sends data through the serial port, the upper driver saves the user data in xmit;, and the serial port sends the interrupt handling function to get the user data through xmit and send them out. The serial port receiving interrupt handling function needs to transfer the received data to the line protocol layer through port.
Struct uart_state {struct tty_port port; enum uart_pm_state pm_state; struct circ_buf xmit; struct uart_port * uart_port; / * corresponds to a serial port device * /}
2.2.4. Struct uart_port
Uart_port is used to describe the information of serial port such as memory address, FIFO size, port type, serial clock and so on. In fact, a uart_port implementation corresponds to a serial device.
Struct uart_port {spinlock_t lock; / * port lock * / unsigned long iobase; / * in/out [bwl] * / unsigned char _ iomem * membase; / * read/write [bwl] * / unsigned int (* serial_in) (struct uart_port *, int) Void (* serial_out) (struct uart_port *, int, int); void (* set_termios) (struct uart_port *, struct ktermios * new, struct ktermios * old) Int (* handle_irq) (struct uart_port *); void (* pm) (struct uart_port *, unsigned int state, unsigned int old); void (* handle_break) (struct uart_port *); unsigned int irq / * irq number * / unsigned long irqflags; / * irqflags * / unsigned int uartclk; / * base uart clock * / unsigned int fifosize; / * tx fifosize * / unsigned char x_char / * xon/xoff char * / unsigned char regshift; / * reg offset shift * / unsigned char iotype; / * io access style * / unsigned char unused1 # define UPIO_PORT (0) # define UPIO_HUB6 (1) # define UPIO_MEM (2) # define UPIO_MEM32 (3) # define UPIO_AU (4) / * Au1x00 and RT288x type IO * / # define UPIO_TSI (5) / * Tsi108/109 type IO * / unsigned int read_status_mask / * driver specific * / unsigned int ignore_status_mask; / * driver specific * / struct uart_state * state; / * pointer to parent state * / struct uart_icount icount; / * statistics * / struct console * cons / * struct console, if any * / # if defined (CONFIG_SERIAL_CORE_CONSOLE) | | defined (SUPPORT_SYSRQ) unsigned long sysrq; / * sysrq timeout * / # endif upf_t flags; # define UPF_FOURPORT ((_ _ force upf_t) (1 temp_ col1]) }} else {printf ("read temp colour failed\ n"); return-1;} free (temp); close (fd); tcsetattr (fd, TCSANOW, & oldtio); / / restore the previous setting return 0 } Thank you for your reading. The above is the content of "what are the knowledge points of Linux's tty architecture and UART drivers?" after the study of this article, I believe you have a deeper understanding of what are the knowledge points of Linux's tty architecture and UART drivers, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.