linux字符驱动学习实践1(简单控制LED灯)

这两天跟着一个视频照着写了一个驱动,算不上原创,但觉得还是要记录下来......

硬件环境:FL2440开发板

软件环境:linux2.6.38,飞凌提供的文件系统(不太满意,以后会自己编译一个)

以下是源码,主要用来控制LED的亮和暗,

版本一:创建一个设备名,主设备号自动生成,次设备号为0,并自动创建设备节点(由于文件系统原因,热插拔驱动支持的不是很好每次装载完驱动要用mdev -s扫描)

#include <linux/init.h>

#include <linux/module.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/delay.h>

#include <asm/uaccess.h>

#include<linux/device.h>

#include <asm/irq.h>

#include <asm/io.h>

#include <linux/cdev.h>

#include <asm/io.h>

MODULE_LICENSE("GPL");

//struct cdev first_dev;

static struct class *firstdrv_class;

//static struct class_device	*firstdrv_class_dev;

volatile unsigned long *gpbcon = NULL;

volatile unsigned long *gpbdat = NULL;

static int first_drv_open(struct inode *inode, struct file *file)

{

	printk("Open device OK!\n");

	/*GPB 5 6 8 10为输出*/

	*gpbcon &= ~((0x3 << (5*2)) | (0x3 << (6*2)) | (0x3 << (8*2)) | (0x3 << (10*2)));

	*gpbcon |= (0x1 << (5*2) | 0x1 << (6*2) | 0x1 << (8*2) | 0x1 << (10*2));

	return 0;

}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)

{

	int val;

	

	//开启LED灯

	copy_from_user(&val, buf, 1);

	if(val == 0)

	{

		*gpbdat &= ~(0x1 << 5 | 0x1 << 6
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值