驱动模板编程总结
2019-04-13 16:48发布
生成海报
驱动模板
- xxx.c –> 应用层驱动封装函数
- xxx.h –> 应用层驱动封装函数头文件
- test.c –> 测试驱动所有的应用层代码
- xxxDrv.c –> 内核层驱动实现文件
- xxxDrv.h –> 内核层驱动实现文件函数头文件
- Makefile –> 编译管理
- 一个外设的驱动程序模板一般都需要这几个文件
具体展开讲:
static int __init key_init(void)
{
int ret;
printk("key_init
");
devNo = MKDEV(251, 2);
ret = register_chrdev_region(devNo, 1, "key");
if (ret < 0) {
printk("Register char device faikey, ret = %d
", ret);
goto err;
}
cdev_init(&keyDev, &key_ops);
keyDev.owner = THIS_MODULE;
ret = cdev_add(&keyDev, devNo, 1);
if (ret < 0) {
printk("Add dev faikey
");
unregister_chrdev_region(devNo, 1);
goto err;
}
printk("Hello device init success
");
s3c2410_gpio_cfgpin(S3C2410_GPF(KEY0), S3C2410_GPIO_INPUT);
s3c2410_gpio_cfgpin(S3C2410_GPF(KEY1), S3C2410_GPIO_INPUT);
s3c2410_gpio_cfgpin(S3C2410_GPF(KEY2), S3C2410_GPIO_INPUT);
s3c2410_gpio_cfgpin(S3C2410_GPF(KEY3), S3C2410_GPIO_INPUT);
err:
return 0;
}
static void __exit key_exit(void)
{
printk("key_exit
");
cdev_del(&keyDev);
unregister_chrdev_region(devNo, 1);
}
static ssize_t key_read (struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
return size;
}
static ssize_t key_write (struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
return 0;
}
static int key_open(struct inode *inode, struct file *filp)
{
printk("Open driver
");
return 0;
}
static int key_release (struct inode *inode, struct file *filp)
{
printk("Close driver
");
return 0;
}
static long key_ioctl (struct file *filp, unsigned int cmd, unsigned long param)
{
printk("ioctl driver
");
return 0;
}
static struct file_operations key_ops = {
.owner = THIS_MODULE,
.read = key_read,
.write = key_write,
.open = key_open,
.release = key_release,
.llseek = key_lseek,
.unlocked_ioctl = key_ioctl,
};
module_init(key_init);
module_exit(key_exit);
int keyInit(void)
{
int ret;
keyFd = open("/dev/key", O_RDWR, 0666);
if (keyFd < 0) {
printf("Open key device faikey
");
return -1;
}
return 0;
}
void keyDeInit(void)
{
close(keyFd);
}
int main(void)
{
}
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮