当前位置:网站首页>Embedded driver design
Embedded driver design
2022-06-12 12:49:00 【Salted fish brother】
1. Basic principle of embedded driver
The task of the device driver is to control the hardware of the device to complete the specified I/O operation , So in device management, the driver deals directly with the device hardware . The driver contains the code to perform various operations on the device , Under the control of the operating system ,CPU Through the implementation of the driver to achieve the processing and operation of the underlying hardware of the device Linux The main functions of the device driver are : Initialize the device ; Start or stop the operation of the equipment ; Transfer the data on the device to the memory ; Transfer data from memory to the device ; Check the equipment status .
Linux The kernel divides drivers into three categories : Character device 、 Block device 、 Network device driver , There are different access methods for different devices .
* Character device
Character device (char device ) And ordinary file system : Ordinary file systems can be read back and forth / Write , Most character devices are just data channels , Can only be read sequentially / Write . On character device Linux The simplest device , Can be accessed like a file . The application uses standard system calls to open , Read , Write and close , It's exactly like this device is a normal file . When initializing the character device , Its device driver to Linux registration , And add a... In the character device vector table device_struct Data structure entries , The primary device identifier of this device ( for example , about tty The primary device identifier of the device is 4 ) Used as the index of this vector table . The primary device identifier of a device is fixed .chrdevs The vector table maintains the registered character device files .
* Block device
Block device (block device ) Is the material basis of the file system , It also supports file like access . This mechanism of providing the correct file system operation group for open block special files is very similar to that of character devices .Linux use blkdevs The vector table maintains the registered block device files . It's like chrdevs Like a vector table , Use the master device number of the device as the index . Unlike character devices , Block devices for classification ,SCSI Is one of them , and IDE It's another kind of .
* Network device driver
For every network interface , All use one device Data structure representation of . Usually , A network device is a physical device , Such as Ethernet card , But software can also be used as a network device , Typically, a loopback device (loopback ) . When the kernel starts , The system registers the existing network devices through the network device driver . The device uses a standard network supporting mechanism to forward the received data to the corresponding network layer . For more detailed information about network device drivers, please check the relevant materials .
System call is the interface between operating system kernel and application program , The driver is the interface between the operating system kernel and the machine hardware . Device drivers have direct access to hardware code , A system call must be provided for the application . So that the application can access the device . The driver calls are consistent , Adopt unified interface ( In data structure file _operations in ). Applications can use devices as easily as they can read and write ordinary files , Use the same open ( ),close( ) ,read ( ) ,write ( ) etc. , It really has nothing to do with the equipment .
Usually Linux The driver interface is divided into the following four layers :
(1) Interface between application process and kernel ;
(2) Interface between kernel and file system ;
(3) Interface between file system and device driver ;
(4) Interface between device driver and hardware device .
Each driver has a file_operations Data structure of , Contains a series of function pointers , It can point to the interface developed by itself, such as open ( ) etc. . There are two tables in the kernel , A device driver for characters , A driver for block devices . These two tables are used to store points to file_operations Pointer to data structure , The address of the driver's internal function is stored in this structure .
Linux The system distinguishes different equipment by equipment number . The equipment number consists of two parts : Main equipment number and secondary equipment number . The master device number indicates which device drivers correspond to , This correspondence is fixed and exists as part of the kernel resource . It should be noted that , The same master device number can correspond to two different device drivers , One can be a character device and the other can be a block device .
The secondary device number distinguishes an independent device under the control of a device driver . such as , Of the same type USB The device can have several in the system , They are distinguished by secondary equipment , The device driver can only correspond to one . stay /proc/ devices The main equipment number of the active equipment in the system is listed in , The so-called active state means that the device driver corresponding to the device has been loaded by the system kernel .
The equipment entry point can also be understood as “ Device file handle ”, The entry point of a device is the same as the normal file system on the disk , You can delete (rm ) , Move (mv ) And copy (cp ) etc. .
We can use... In the file system mknod Command to create a device entry point or through a system call mknof To create . Device drivers and hardware that create a device entry point in the file system and do not represent a response are ready , It only represents part of the communication with the device driver .
Here is an example of creating a character device entry point :
#mknod /dev/testChar c 100 0
among c Represents a character device , If you want to create a block device, use b Instead of c . Parameters 100 Represents the main equipment number of the equipment ,0 Represents the secondary equipment number of the equipment .
For the existing Linux operating system ,/dev Directories are essential , This directory contains all Linux A character device known to the system , Block devices and network devices .
The method of operating a character device is very simple . Opening a character device is like opening a text file , Just read / Write “ file ” The operation of is actually the process of operating the equipment , You can use normal file operation commands cat Or the shell reset syntax implements the data exchange with the device .
stay Linux The driver can be loaded dynamically or statically . Static loading is to compile the driver directly into the kernel , After the system starts, you can directly call . The disadvantage of static loading is that debugging is troublesome , Every time you change a place, you have to recompile and download the kernel , Low efficiency . Dynamic loading takes advantage of Linux Of module characteristic , You can use... After the system starts insmod Command the driver (.o file ) Add to , Use... When you don't need it rmmod Command to uninstall . Dynamic loading is generally used on desktop computers . In embedded products, dynamic loading can be used to debug , After debugging, compile it into the kernel .
The device driver first needs to call the entry function when loading init_module ( ) , This function completes the initialization of the device driver , For example, register setting , A series of work such as structural value-added . One of the most important tasks is to register the device with the kernel , Set... For characters Standby call register_chrdev ( ) To complete the registration , For block devices, you need to call register_blkdev ( ) To complete the registration . After successful registration , The equipment has obtained the master equipment number assigned by the system , Customized secondary equipment number , And establish an association with the file system . The device is uninstalled , Resources that need to be recycled for response , Reset the response register value of the device and unregister the device from the system . The system call part is the operation process of the device , such as open , read ,write , ioctl Wait for the operation .
2.LED Driver example
Write the actual driver , You must understand the relevant hardware resources , For example, the registers used , Physical address , Interrupt, etc , ad locum ,LED It's a very simple example , It uses the following hardware resources .
Used on the development board 4 individual LED Hardware resources
LED | Corresponding IO |
LED1 | GPK4 |
LED2 | GPK5 |
LED3 | GPK6 |
LED4 | GPK7 |
The driver leds.c
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
//#include <mach/regs -gpio.h>
#include <mach/hardware.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>
#include <mach/map.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>
#include <plat/gpio -cfg.h>
#include <mach/gpio-bank -e.h>
#include <mach/gpio-bank -k.h>
#define DEVICE_NAME “leds”
static long sbc2440_leds_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
switch(cmd) {
unsigned tmp;
case 0:
case 1:
if (arg > 4) {
return -EINVAL;
}
tmp = readl(S3C64XX_GPKDAT);
tmp &= ~(1 << (4 + arg));
tmp |= ( (!cmd) << (4 + arg) );
writel(tmp, S3C64XX_GPKDAT);
return 0;
default:
return -EINVAL;
}
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = sbc2440_leds_ioctl,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init dev_init(void)
{
int ret;
{ unsigned tmp;
tmp = readl(S3C64XX_GPKCON);
tmp = (tmp & ~(0xffffU<<16))|(0x1111U
writel(tmp, S3C64XX_GPKCON);
tmp = readl(S3C64XX_GPKDAT);
tmp |= (0xF << 4);
writel(tmp, S3C64XX_GPKDAT);
}
ret = misc_register(&misc);
printk (DEVICE_NAME“\ tinitialized\ n“);
return ret; }
static void __exit dev_exit(void)
{ misc_deregister(&misc); }
module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE(“GPL“);
MODULE_AUTHOR(“FriendlyARM Inc.“);
compile led The driver , Then download it to the development board and run it .LED Reference to the test procedure is as follows :

边栏推荐
- Jacobian matrix IK of manipulator
- Introduction, installation and use of core JS
- 442个作者100页论文!谷歌耗时2年发布大模型新基准BIG-Bench | 开源
- [VIM] VIM plug-in youcompleteme configuration file
- The 4th Zhejiang CTF preliminary contest web pppop
- ITK multiresolution image itk:: recursivemultiresolutionpyramidimagefilter
- 二叉树(构造篇)
- 2021-11-16
- Improve pipeline efficiency: you need to know how to identify the main obstacles in ci/cd pipeline
- 快速下载谷歌云盘大文件的5种方法
猜你喜欢

NewOJ Week 10题解

VS2019 设置 CTRL+/ 为注释和取消注释快捷键

The 4th Zhejiang CTF preliminary contest web pppop

In depth anatomy of C language - key words & supplementary contents

Deep analysis of advanced pointer -- advanced chapter of C language

MySQL 分区表介绍与测试

Summary of knowledge points of ES6, ES7, es8, es9, ES10, es11 and ES12 (interview)

Buu question brushing record - 5

数组——双指针技巧秒杀七道数组题目

Binary tree (program)
随机推荐
关于派文的问题
功能标记是什么?一文了解它的作用,以及它的最佳实践
Improve pipeline efficiency: you need to know how to identify the main obstacles in ci/cd pipeline
Binary tree (thoughts)
分享PDF高清版,系列篇
Downloading and using SWI Prolog
Typescript and abstract classes
【vim】vim插件YouCompleteMe配置文件
Advanced chapter of C language -- ten thousand words explanation pointer and qsort function
Image comparison function after registration itk:: checkerboardimagefilter
Five ways to quickly download large files from Google cloud disk
路由信息的来源
Microsoft Word tutorial, how to insert a header or footer in word?
A "murder case" caused by ES setting operation
Part of the fourth Zhejiang CTF finals
Typeof and instanceof, how to simulate the implementation of an instanceof? Is there a general detection data type?
二叉树(纲领篇)
Introduction and test of MySQL partition table
JS how to get the values of multiple objects in an array
wx. Login and wx Getuserprofile simultaneous use problem