1.编译、安装驱动程序
Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码。因此编译、安装驱动程序实质是编译、安装内核模块
memdev.c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
int dev1_registers[5];
int dev2_registers[5];
struct cdev cdev;
dev_t devno;
int mem_open(struct inode *inode, struct file *filp)
{
int num = MINOR(inode->i_rdev);
if (num==0)
filp->private_data = dev1_registers;
else if(num == 1)
filp->private_data = dev2_registers;
else
return -ENODEV;
return 0;
}
int mem_release(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
int *register_addr = filp->private_data;
if (p >= 5*sizeof(int))
return 0;
if (count > 5*sizeof(int) - p)
count = 5*sizeof(int) - p;
if (copy_to_user(buf, register_addr+p, count))
{
ret = -EFAULT;
}
else
{
{
*ppos += count;
ret = count;
}
return ret;
}
static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
{
loff_t newpos;
switch(whence) {
case SEEK_SET:
newpos = offset;
break;
case SEEK_CUR:
newpos = filp->f_pos + offset;
break;
case SEEK_END:
newpos = 5*sizeof(int)-1 + offset;
break;
default:
return -EINVAL;
}
if ((newpos<0) || (newpos>5*sizeof(int)))
return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
static const struct file_operations mem_fops =
{
.llseek = mem_llseek,
.read = mem_read,
.write = mem_write,
.open = mem_open,
.release = mem_release,
};
static int memdev_init(void)
{
cdev_init(&cdev, &mem_fops);
alloc_chrdev_region(&devno, 0, 2, "memdev");
cdev_add(&cdev, devno, 2);
}
static void memdev_exit(void)
{
cdev_del(&cdev);
unregister_chrdev_region(devno, 2);
}
MODULE_LICENSE("GPL");
module_init(memdev_init);
module_exit(memdev_exit);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
Makefile
obj-m := memdev.o
KDIR := /home/topeet/Android/iTop4412_Kernel_3.0
all:
make -C $(KDIR) M=$(PWD) modules
clean:
rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.order
编写Makefile,通过make编译该驱动程序生成.ko文件。下载到开发板中,使用insmod memdev.ko加载,使用lsmod即可查看该驱动程序的存在。
2.字符设备文件
应用程序通过文件名来找到字符设备文件来访问设备驱动程序来控制字符设备。创建字符设备文件的方法一般有两种:
2.1.使用mknod命令:
mknod /dev/文件名 c 主设备号 次设备号
2.2.使用函数在驱动程序中创建
通过cat /proc/devices可看到新增的主设备号249,
此刻使用命令mknod /dev/memdev0 249 0创建设备文件
ls /dev就可以看到新增的设备文件
3.驱动程序的调用
write_mem.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = 0;
int src = 2013;
fd = open("/dev/memdev0",O_RDWR);
write(fd, &src, sizeof(int));
close(fd);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
使用arm-none-linux-gnueabi-gcc write-mem.c -o write-mem -static编译该文件,将文件下载到开发板上,./write-mem执行,
若报错-/bin/sh: ./write-mem:not found,在linux虚拟机上的文件路径下,使用arm-linux-readelf -d write-mem查询应用程序的动态链接库。发现缺少动态链接库
使用-static编译应用程序不会出现以上问题。
执行成功后将2013写入模拟寄存器。
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = 0;
int dst = 0;
fd = open("/dev/memdev0",O_RDWR);
read(fd, &dst, sizeof(int));
printf("dst is %d\n",dst);
close(fd);
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
使用arm-none-linux-gnueabi-gcc read-mem.c -o read-mem -static编译该文件,将文件下载到开发板上,./read-mem执行会读出dst is 2013
4总结:
首先编写虚拟寄存器的驱动程序
创建字符设备文件或者说设备节点,
通过应用程序write-mem写入2013,
通过应用程序read-mem读出2013,