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

A brief introduction to the concepts of the tty subsystem architecture

In the Linux system, the terminal is a type of character device, which includes many types. Usually tty is used to refer to various types of terminal devices.


1 Serial terminal (/dev/ttyS*)

The serial terminal is a terminal device connected to the computer serial port. Linux treats each serial port as a character device. The device names corresponding to these serial ports are /dev/ttySAC0; /dev/ttySAC1...


2 Console terminal (/dev/console)

In Linux systems, the output device of a computer is usually called a console terminal (Console), which specifically refers to the device to which printk information is output. /dev/console is a virtual device that needs to be mapped to a real tty. For example, the kernel startup parameter "console=ttySAC0" maps the console to serial port 0.


3 Virtual terminals (/dev/tty*)

When a user logs in, a virtual terminal is used. Using the Ctcl+Alt+[F1—F6] key combination, we can switch to tty1, tty2, tty3, etc. tty1–tty6 are called virtual terminals, and tty0 is an alias for the currently used virtual terminal.


The Linux tty subsystem includes: tty core, tty line discipline and tty driver. The tty core is an abstraction of the entire tty device, providing a unified interface for users, the tty line discipline is the formatting of transmission data, and the tty driver is the hardware driver for the tty device.


Write the picture description here

Serial port driver analysis initialization

To analyze the serial port driver, you must first remember two files: sumsung.c and s3c6400. These two files are very important and are located in .../drivers/tty/serial.


First, when the user uses the write function to send data through the serial port, write will find the corresponding pointer in file_operation through the system call. The tty_fops structure is the corresponding file_operations, in which tty_write responds to the system call.

Write the picture description here

Next, we enter the tty_write function and find the line discipline ops in the tty_write function. We need to find the line discipline here. The tty_ldisc_N_TTY structure is the structure we need to find. In this structure, we can see n_tty_write, which is the function that tty_write needs to call.

Write the picture description here

Next, let's go into the n_tty_write function and see that n_tty_write will call ops. Ops is actually a tty_operations. Here we need to know a structure uart_ops defined by tty_operations. This structure is the ops we want, which contains the uart_write function.

Write the picture description here

Write the picture description here

So far, the call relationship of write has been listed at the tty level. Simply put, when the user executes the write system call, the first thing to do is to find tty_write, then find n_tty_write, and finally find the uart_write function. Next, we will see how to make system calls in the driver and mainly learn some important data structures.


In the uart_write function, the uart_state data structure is used first, and the other is uart_port. We can see that uart_port is obtained from state.

Write the picture description here

In port, we can also get a data structure uart_ops. In this data structure, there are many function pointers, which can be used to operate the hardware.

Write the picture description here

In addition to these three data structures, there is another important data structure uart_driver. We can go back and look at the uart_write function. The first line tty->driver_data shows that state is obtained from driver_data. So where does driver_data come from? We can open the uart_open function to find out. This function also gives us the following information: driver_data is obtained from state, and state is obtained from driver through the uart_get function. In this way, we can roughly sort out the relationship between the four important data structures: uart_driver, uart_state, uart_port, and uart_ops.

Write the picture description here

Several important data structures are listed below. Next, we will understand these structures in depth and start analyzing initialization.


UART driver structure: struct uart_driver

A uart_driver represents a serial port driver

• UART port structure: struct uart_port

One uart_port corresponds to one serial port (one driver can contain multiple serial ports)

• UART related operation function structure: struct uart_ops

uart_ops contains function pointers to implement related operations on the serial port hardware.

The above three constitute the main body of the serial port driver


• UART status structure: struct uart_state

• UART information structure: struct uart_info


1 Open Samsung.c. During the initialization process, the uart_register_driver function is used to register the serial port driver. The following s3c24xx_uart_drv is a uart_driver, representing a serial port driver, so the first task is to register the serial port driver.

Write the picture description here
Write the picture description here

In s3c6400.c, we can see that the s3c24xx_serial_init function is used. If we continue to follow this function, we will see that the platform_driver_register function is used, which reminds us of the registration of the platform bus driver in the previous blog.

Write the picture description here
Write the picture description here

2 In the platform bus chapter, we know that when the driver and the device are compared, once a match is found, the probe function will be called. The same is true here. Let's focus on analyzing the probe function here. The first task is to take out the corresponding uart_port structure - &s3c24xx_serial_ports[probe_index], and then initialize the uart_port - s3c24xx_serial_init_port.

Write the picture description here


Continue to analyze how the initialization work is carried out. In the s3c24xx_serial_init_port function, there are several codes that we need to master. First, the platform_get_resource function obtains the base address of the device, then uses the statically allocated virtual address S3C_VA_UART, and finally uses the platform_get_irq function to obtain the corresponding interrupt number.

Write the picture description here

Here we also need to pay attention to a function s3c24xx_serial_resetport, which is mainly used to reset fifos. Entering this function, we focus on the register UFCON that resets the fifo. In this register, by looking up the chip manual, we can see that the first and second bits are set to 1 at the same time to reset the FIFO. Similarly, we can find in the kernel that the first and second bits of the UFCON register are set to 1.

Write the picture description here
Write the picture description here
Write the picture description here
Write the picture description here

Next, let's return to the probe function. There are several functions that need to be understood:

uart_add_one_port is used to add a port.

platform_set_drvdata is used to put the port into driver_data in dev.

device_create_file is used to create a property file, through which you can see the relevant serial port information.

The s3c24xx_serial_cpufreq_register function is related to dynamic frequency adjustment.


Here we just need to understand the relevant steps in the initialization function. It may be difficult to understand such a complex calling relationship for the first time, but it doesn't matter, otherwise embedded systems are so simple that anyone can learn them casually and it will be useless. The following is a mind map for this section of analysis.

Write the picture description here

Serial port driver analysis-open device

Based on the analysis in the previous section, we can roughly understand the working steps of driver initialization. In this section, we will mainly analyze how the serial port driver opens the device.

First open Samsung.c and enter the initialization of the serial port driver registration program. There is a function uart_register_driver.

Write the picture description here

Entering uart_register_driver, you can see that a function tty_register_driver is called.

Write the picture description here

Entering the tty_register_driver function, we can see that cdev_init is used in the serial port registration function, which means that the serial port is also a character device, and tty_fops is actually the file_operations of the serial port device file.

Write the picture description here

Continue to open tty_fops, you will find various device methods, open, read, write, etc., so we can find the response entry of the open system call.

Write the picture description here

What should tty_open do next? Here we just need to know that tty_open calls the uart_open function in uart_ops.


Write the picture description here


Next, let's take a look at the uart-open function. After opening this function, we find that this function continues to call the uart_startup function.


Write the picture description here

We continue to open the uart_startup function, and we can find the following line of code. Now we call the startup of the serial port driver function set. uport is of uart_port type, representing a serial port, which contains a function operation set. (What is the operation function set? Don't worry, keep reading.) So where can we find the startup of the serial port driver function set? Let's try to find it in the driver program.

Write the picture description here

In the driver Samsung.c, the port is obtained through the s3c24xx_serial_ports array in the probe function.


Write the picture description here

Open s3c24xx_serial_ports, you can see that each port represents a serial port. In the port, there is a structure s3c24xx_serial_ops, which is the set of operation functions.

Write the picture description here


In the s3c24xx_serial_ops structure, we can find the startup function.


Write the picture description here

To summarize the above

1 When the user uses the open system call, a file_operations will be found, tty_fops acts as this file_operations, and then the tty_open function will be found in tty_fops.

2 In uart_ops there is a function uart_open, which is called by tty_open. In uart_open function, uart_startup function is called.

3. Get the operation function set s3c24xx_serial_ops through the array s3c24xx_serial_ports in the driver, and then find the startup function.


Next, let’s analyze what the startup does.

Here we mainly look at the following four lines of code


rx_enabled(port)=1; /*Enable serial port receiving function*/


ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0, s3c24xx_serial_portname(port), ourport); /*Register interrupt handler for data reception*/


tx_enabled(port) = 1; /*Enable serial port sending function*/


ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0, s3c24xx_serial_portname(port), ourport); /*Register interrupt handler for data transmission*/

Write the picture description here

Serial port driver analysis-data transmission

In this section, our analysis is carried out in two steps. The first step is to analyze the call relationship of tty data sending, and the second step is to analyze the serial port sending function. Let's take a look at the first step first.

[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 14:59

Common digital tube chip TM1637 driver based on STM32
I won’t go into details about the IIC protocol here; Use analog IIC port definition B14 B15 #define AnalonSDA_Pin GPIO_PIN_12 #define AnalonSDA_GPIO_Port GPIOB #define AnalonSCL_Pin GPIO_PIN_13 #define AnalonSCL_GPIO_Port GPIOB IO configured as output   /*Configure GPIO pin Output Level */   HAL_GPIO_WritePin(
[Microcontroller]
MAX3669 Low-Power 622Mbps Laser Driver
The MAX3669 is a complete, +3.3 V laser driver with automatic power control (APC) circuitry for SDH/SONET applications up to 622 Mbps. It accepts differential PECL inputs, provides bias and modulation currents, and operates over the -40°C to +85°C temperature range. The APC feedback loop is combined to maintain cons
[Analog Electronics]
MAX3669 Low-Power 622Mbps Laser Driver
Design of campus LED bulletin board system based on ARM
In recent years, LED electronic display screens have attracted increasing attention as a high-tech product. They can display text, graphics and image information in real time or in a loop. They have many advantages, such as rich display modes, strong viewing, convenient display content modification, high brightness, s
[Power Management]
Design of campus LED bulletin board system based on ARM
Circuit design of ultrasonic monitoring and early warning system based on ARM single chip
With the development of informatization, intelligence and networking, embedded system technology has gained a broad space for development, and the field of industrial control is also undergoing a huge change. Real-time embedded software and hardware technology based on 32-bit high-end processors will be applied in eve
[Power Management]
Circuit design of ultrasonic monitoring and early warning system based on ARM single chip
80x126 pixel LCD driving technology based on PCF8811 design
This article introduces the main features and advantages of PCF8811, block diagram, and several typical application block diagrams. PCF8811 is a low-power 80x126 pixel CMOS LCD controller from NXP, with 80 rows and 128 columns of output, display data RAM 80x128 bits, 8-bit parallel interface, 3/4-way SPI and hig
[Microcontroller]
80x126 pixel LCD driving technology based on PCF8811 design
51 single chip microcomputer series driving buzzer to sound
Name: 51 single chip microcomputer drives buzzer to sound  Platform: Keil 4, Ly-51S learning board  Content: Simulate alarm sounds, such as the beeping of an alarm clock  -------------------------------------------------- ---*/       #include reg52.h       sbit SPK = P1^2;       void delay_2us(unsigned char t) {     
[Microcontroller]
Simulate 8(COM)*8(SEG) LCD driver on EM78Pxx
;/*===================================================== ; Simulate 8(COM)*8(SEG) LCD driver on EM78Pxx                              ;                                                                              ;                                                                              ;I. LCD scanning princi
[Microcontroller]
AC drive LED cannot avoid the essential constant current control technology analysis
1. AC LED is not a fundamental change in the device. In other words, there actually does not exist an LED chip that works with an alternating electric field mechanism. The AC LED that is now available is a device with a special arrangement of internal chipsets. It is merely a change in the internal structure of the
[Power Management]
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号