TTY data sending call relationship
(1) To send data, the user must first write the write function. The kernel must respond to the user program's write, so there must be a file_operations. In the previous section, we have learned how to find this file_operations in the driver program—tty_fops. In this structure, we can find the tty_write function, which is the write function that responds to the user program. If you forget, you can recall it in the first section.
(2) The implementation of tty_write mainly depends on the n_tty_write function of the line discipline (n_tty_write comes from the tty_ldisc_N-TTY structure). If you forget, you can recall it in the first section.
(3) n_tty_write will find the file_operations of tty. In fact, the file_operations of tty is uart_ops, and uart_ops has the uart_write function.
uart_write will call the uart_start function, and then uart_start will find the _uart_start function.
_uart_start will call start_tx. The source of start_tx is the ops in uart_port. When we enter the Samsung.c file, we can directly find the ops in uart_port, which is s3c24xx_serial_ops.
(4) Enter s3c24xx_serial_ops and you can find the start_tx function we need.
The above is a process in which the user's write function finds the s3c24xx_serial_start_tx function in the driver. It can also be said to be a small summary of the analysis in the first section. Next, let's take a look at how the s3c24xx_serial_start_tx function is implemented.
Let's first look at how the s3c24xx_serial_start_tx function operates. In this short function, there is no code to operate the register to send data. The reason is that the enable_irq function completes the work of sending data, not start_tx. start_tx activates the interrupt, and the interrupt handler sends data.
How does the interrupt handler get the data to send? Here we need to know the concept of circular buffer, which is very important. The circular buffer is an area that stores the data carried by the write system call itself when the user executes the write system call to find the s3c24xx_serial_start_tx function in the driver. Simply put, it stores the data in write. When the serial port sends data, the write data stored in the circular buffer will be taken away. Specifically, the data is sent to the circular buffer in the uart_write function during the process of writex finding the start_tx function. In this function, the circular buffer is the circ structure, which is saved in the xmit member. The code pointed by the arrow indicates that the data in buf is put into the circular buffer.
Analysis of serial port sending function
Here we mainly analyze the serial port send interrupt handler s3c24xx_serial_tx_chars.
In this function, the main analysis is on the parts pointed by the three arrows below.
Here we analyze in the order of the arrows from top to bottom.
1 If there is data to be sent, the data will be placed in the UTXH register, and then the status will be cleared to indicate that there is no data at this time.
2 When the circular buffer is empty or data sending is not allowed, stop sending. Because the previous analysis shows that sending data is to turn on the interrupt enable_irq, then stopping sending data can be thought of by turning off the interrupt.
3 Use the while loop to send data. The loop condition is that the loop buffer is not empty and the amount of data to be sent is less than 256, count is greater than 0, and count is at most 256. A maximum of 256 characters can be sent in one interrupt. In this while loop:
Register UFSTAT is used to receive and transmit FIFO. When the transmit FIFO is full, the transmission is terminated.
When the FIFO is not full, data comes from the circular buffer, valid data is taken from the tail of the circular buffer, and then written to UTXH.
Adjust the position of the circular buffer.
After sending the data, there are two more things to do. The first is to calculate the amount of data in the circular buffer. When the amount of data is less than 256, a uart_write_wakeup must be done. Because there is not enough data to send at this time, the application must be notified to wake up the process in the application that attempts to send data.
The last one is that when the circular buffer is empty, the serial port sending function should be turned off.
Summary: The above is the analysis of the serial port sending function s3c24xx_serial_tx_chars. In this way, we can roughly understand how to find the driver's start_tx after the user program executes the write function, and then how to send the data in the user program's own buf to the serial port. Here is a mind map.
Serial port driver analysis-data reception
tty data reception
(1) Here we continue to analyze the way tty data is sent. When the user makes a read system call to receive data, the kernel responds to the user program's read by looking for the corresponding file_operations—tty_fops. In this structure, we can find the tty_read function, which serves as the read function that responds to the user program.
(2) The implementation of tty_read mainly depends on the n_tty_read function of the line discipline (n_tty_read comes from the tty_ldisc_N-TTY structure). Let's take a look at the n_tty_read function first. The following two pictures are from the n_tty_read function.
This function mainly does the following tasks
1 Set blocking state TASK_INTERRUPTIBLE
2 Determine whether there is data to read. If there is no data, schedule_timeout will be performed to schedule the blocking to take effect.
3 If there is data to read, it will be read from read_buf (copy_from_read_buf). This read_buf is where the serial port driver stores data. APP reads data from read_buf
Serial port receiving data
We have learned how to send data using the s3c24xx_serial_tx_chars function. Now, we can imagine that receiving data is done using the s3c24xx_serial_rx_chars function. Here we will analyze how the s3c24xx_serial_rx_chars function works. The following two figures show the complete contents of the s3c24xx_serial_rx_chars function.
The steps of the above function can be summarized as follows:
Read the UFCON register
Read the UFSTAT register
In the s3c24xx_serial_rx_fifocnt function, fifosize is the amount of data received in the fifo. If the amount of data received is 0, the interrupt is exited.
Read the UERSTAT register
· Get the received character in the URXH register
· Perform flow control processing (UPF_CONS_FLOW)
Record the specific error type according to the value of the UERSTAT register
If sysrq is received, special processing will be performed (uart_handle_sysrq_char)
Send the received character to the serial port driver (uart_insert_char)
Send the data in the serial port driver to read_buf (tty_flip_buffer_push)
Summarize
In this section, we have explained in detail the process of initializing the serial port program, opening the device, and reading and writing operations. We mainly analyze how the system call finds the response function in the driver, such as how write finds the start_tx function, and how the start_tx function is executed in the driver. The analysis process is quite painful, nesting one by one, and it takes a long time to learn the driver. In the next section, we will try to write a serial port driver.
Previous article:MMU configuration and usage
Next article:OK6410 key test C language program
Recommended ReadingLatest update time:2024-11-16 17:46
- Popular Resources
- Popular amplifiers
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- Modern Product Design Guide
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- What are the differences and connections between compilers and integrated development environments?
- [RISC-V MCU CH32V103 Review] ---Advanced Wiki---Practical debugging methods
- LLC resonant converter classic literature or books, personal test feedback!
- How to use current transformer to measure three-phase electricity?
- When AD is routed, are there grid lines? Are there solid lines?
- Overview of Short Range Wireless Communication Technology
- Embedded development system
- I heard that RISC-V is a big hit now?
- Can someone help me convert the PCB format (allegro->altium)?
- Simulation and MCU source code of sawtooth wave generated by DAC0832