Linux驱动开发(二)---驱动与设备的分离设计

前言

《Linux驱动开发(一)—环境搭建与hello world》
继续宣传一下韦老师的视频

70天30节Linux驱动开发快速入门系列课程【实战教学、技术讨论、直播答疑】

在这里插入图片描述

分离设计

参考韦东山老师的代码。
那开始的话,我们写的程序,如果要驱动一个LED,可能会写成下面的样子,


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/mutex.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <asm/io.h>

static int major;
static struct class *led_class;

static ssize_t led_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
   
   
	char val;
	/* copy_from_user : get data from app */
	copy_from_user(&val, buf, 1);

	/* to set gpio register: out 1/0 */
	if (val)
	{
   
   
		/* set gpio to let led on */
	}
	else
	{
   
   

		/* set gpio to let led off */
	}
	return 1;
}

static int led_open(struct inode *inode, struct file *filp)
{
   
   
	/* enable gpio
	 * configure pin as gpio
	 * configure gpio as output 
	 */
	return 0;
}

static struct file_operations led_fops = {
   
   
	.owner		= THIS_MODULE,
	.write		= led_write,
	.open		= led_open,
};

/* 入口函数 */
static int __init led_init(void)
{
   
   
	major = register_chrdev(0, "leddev", &led_fops);
	led_class = class_create(THIS_MODULE, "myled");
	device_create(led_class, NULL, MKDEV(major, 0), NULL, "myled"); /* /dev/myled */
	return 0;
}

static void __exit led_exit(void)
{
   
   
	device_destroy(led_class, MKDEV(
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胖哥王老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值