Lao Cha's ARM study notes: chapter-3 (serial port driver analysis)

Publisher:博雅之士Latest update time:2022-05-30 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


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.


Write the picture description here
Write the picture description here

_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.

Write the picture description here

Write the picture description here

(4) Enter s3c24xx_serial_ops and you can find the start_tx function we need.


Write the picture description here

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.


Write the picture description here

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.

Write the picture description here

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.

Write the picture description here

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.

Write the picture description here


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.


Write the picture description here
Write the picture description here


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.


Write the picture description here
Write the picture description here

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.

Write the picture description here

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.

[1] [2]
Keywords:ARM Reference address:Lao Cha's ARM study notes: chapter-3 (serial port driver analysis)

Previous article:MMU configuration and usage
Next article:OK6410 key test C language program

Recommended ReadingLatest update time:2024-11-16 17:46

ARM Cortex-M3 Study Notes (1)
I'm studying ARM Cortex-M3 recently, and I found a book called "An Definitive Guide to The ARM Cortex-M3" which is considered a classic. This series of study notes is actually the reading notes I made while studying this book. Chapter 1 Introduction This chapter mainly introduces how powerful the Cortex-M3 core
[Microcontroller]
ARM Cortex-M3 Study Notes (1)
After 9 years of non-ARM core development, APT Microelectronics launches 64-bit dual-core general-purpose MCU products
On December 17, at the first Dishui Lake China RISC-V Industry Forum, Yuan Yongsheng, director and deputy general manager of Shenzhen APTCHIP Microelectronics Co., Ltd. (APTCHIP), introduced the company's RISC-V-based 64-bit dual-core general-purpose MCU product "APT32F706". Yuan Yongsheng pointed out that APTCHIP h
[Mobile phone portable]
After 9 years of non-ARM core development, APT Microelectronics launches 64-bit dual-core general-purpose MCU products
Masayoshi Son decides to list ARM in New York in 2023
According to news on March 2, Beijing time, British chip design giant ARM, a subsidiary of SoftBank Group, has decided not to issue shares on the London Stock Exchange for the time being, which is a blow to British politicians. Previously, British politicians had been lobbying for ARM to be listed on the domestic exch
[Semiconductor design/manufacturing]
Image sensor driver design based on FPGA
While cars bring convenience to people's lives, they also bring traffic accidents. Among them, speeding is one of the important hidden dangers that cause traffic accidents. According to research, the image sensors used in the road capture system for speeding vehicles are mostly small array devices, generally 1 milli
[Analog Electronics]
Image sensor driver design based on FPGA
Mier's battery management system (BMS) based on ARM embedded core board
Mier Solution: Battery Management System (BMS) based on ARM core board Introduction: The battery array management unit BAU adopts the MYC-YA157C-V3 core board of the Mil ARM architecture. The core board is based on the STM32MP157 processor, Cortex-A7 architecture, supports 1 Gigabit Ethernet, 2
[Power Management]
Mier's battery management system (BMS) based on ARM embedded core board
Character device driver (I)
1. Overview of Linux devices Linux devices can be divided into three categories: character devices, block devices, and network devices. In this article, we mainly talk about character devices. So what is a character device? A character (char) device is a device that is accessed like a byte stream (similar to a file),
[Microcontroller]
Character device driver (I)
Leybold Hi-Tech: Mass production and supply of TFT-Array driver backplane for black and white electronic paper displays
On January 10, Laibao Hi-Tech stated on the investor interaction platform that the company has mass-produced and supplied dual-screen vehicle touch screens and a small amount of triple-screen vehicle touch screens to Tier 1 automobile assembly customers. The final applications include cars of most well-known domestic
[Mobile phone portable]
Leybold Hi-Tech: Mass production and supply of TFT-Array driver backplane for black and white electronic paper displays
Ming-Chi Kuo on the dispute between Arm and Qualcomm: The probability of canceling the authorization is extremely low, and if it happens, both sides will suffer
On October 24, Bloomberg reported yesterday that Arm plans to cancel the license that allows its long-term partner Qualcomm to use Arm intellectual property to design chips. Today, analyst Ming-Chi Kuo wrote, "I think the chances of ARM cancelling Qauclomm's license are extremely low. If it happens, both
[Semiconductor design/manufacturing]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号