What is drive?
The most popular explanation is “drive hardware to action”
The device driver directly deals with the underlying hardware, reads and writes the device register according to the specific working mode of the hardware device, completes the polling, interrupt processing and DMA communication of the device, maps the physical memory to the virtual memory, and finally enables the communication device to receive and receive data, enables the display device to display text and picture, and enables the storage device to record the file number data
The hardware of computer system mainly consists of CPU, memory and peripheral equipment. However, with the development of IC manufacturing technology, at present, the degree of chip integration is getting higher and higher, and memory and peripheral adapter are often integrated in CPU. ARM processor integrates UART, I2C controller (2 = = Square), USB controller, SDRAM controller, etc
The driver is for memory and peripherals (including memory and peripherals integrated inside the CPU), not for the CPU core. Linux divides memory and peripherals into three basic categories: character device, block device and network device
Devices that must be accessed in serial order, such as touch screen, tape drive, mouse, etc. The device can be accessed in any order and operated in blocks, such as hard disk, floppy disk, etc. Character devices do not go through the system’s fast buffer, while block devices go through the system’s fast buffer. However, there is no obvious boundary between character device and block device. For example, flash device conforms to the characteristics of block device, but we can still access it as a character device
In Linux system, network device is designed for receiving and sending packets, which is not corresponding to the nodes of file system. The communication mode between kernel and network device and between kernel and character device and block device is totally different.
As shown in the above figure, except for network devices, character devices and block devices are mapped to files and directories of the Linux file system. Character devices and block devices can be accessed through the system call interfaces open(), write(), read(), close() and other functions of the file system. Block devices are more complex than character devices, on which a disk / flash file system will be established first, such as fat, ext3, yaffs, JFFS, etc. fat, ext3, yaffs, JFFS regulate the organization of files and directories on storage media
The application program can be programmed by using the system call interface of Linux, or by using the C library function. For the sake of portability, the latter is more recommended. C library function itself is realized through system call interface
In the design of embedded system, LED is usually directly controlled by GPIO of CPU. GPIO generally has two sets of register control, that is, a set of control registers and a set of data registers. The control register can set the working mode of GPIO port as input or output. When the pin is set to output, writing 1 and 0 to the corresponding bit of data register will generate high level and low level on the pin respectively; when the pin is set to input, reading the corresponding bit of data register can obtain the corresponding level signal on the pin.
Hardware foundation of driver design
The classification of processors is shown in the following figure:
Memory can be divided into read-only memory (ROM), flash memory (flash), random access memory (RAM), optical media memory and magnetic media memory
The interface between nor flash and CPU is a typical SRAM like interface, and no additional control circuit is needed. The characteristic of NOR flash is that it can be executed on chip, and the program can run directly in nor. The interface between NAND fast and CPU must be converted by corresponding control circuit, and the signal of NAND flash interface can also be generated through address line or GPIO. NAND flash is accessed in block mode and does not support on-chip execution.
The programming principle of flash is that only 1 can be written as 0, not 0 can be written as 1. Therefore, the corresponding block must be erased before flash programming, and the process of erasing is the process of writing all bits as 1, and all bytes in the block become 1xff
The classification of memory is shown in the following figure:
Schematic analysis: through reading the schematic diagram of the circuit board, obtain the hardware resources used by various memories and peripherals, mainly including chip selection, interrupt and DMA resources used by memory and peripherals control chip. The memory and I / O base address of the chip can be obtained by analyzing the chip selection, and the interrupt number and DMA channel used by the chip can be obtained by analyzing the interrupt and DMA signal. The following table is summarized:
Timing analysis: for the driver engineer or hardware engineer, timing analysis means to make the access between chips meet the requirements of the effective sequence of timing chart signals in the chip manual, the sampling establishment time and the holding time. When the circuit board is not working normally, the problem of accurate positioning timing shall be solved
Kernel and kernel programming
In the aspect of device driver, Linux 2.6 has a great change compared with Linux 2.4, which is mainly manifested in the addition of many new functions (such as memory pool), sysfs file system, the change of kernel module from. O to. Ko, the compilation mode of driver module, the use count of module, the definition of module loading and unloading function, etc
Linux kernel is mainly composed of five subsystems: process scheduling, memory management, virtual file system, network interface and interprocess communication, as shown in the following figure:
In device driver programming, when the requested resource cannot be satisfied, the driver will generally schedule other threads to execute, and make the corresponding process of the driver go to sleep state, until the requested resource is released, it will not be awakened and Memorial Day ready state
In device driven programming, if several concurrent tasks are needed, the kernel thread can be started. The functions to start the kernel thread are: int kernel [thread (int (* FN) (void *), void * Arg, unsigned long flags);
Linux memory management completes the conversion of virtual memory to physical memory for each process. Generally speaking, each process of Linux has 4GB of memory space, 0-3gb of user space, and 3-4gb of kernel space. Kernel space has different processing methods for conventional memory, I / O device memory and high-end memory
Virtual file system hides the details of various hardware and provides a unified interface for all devices. Moreover, it is independent of each specific file system, and it is an abstraction of various file systems. It uses super block to store the information related to the file system, inode to store the physical information of the file, dentry to store the logical information of the file
Linux system can only transfer control from user space to kernel space through system call and hardware interrupt.