最简单的点亮LED灯驱动程序

本文详细介绍了Linux驱动程序的实现方式,包括模块初始化、设备创建、打开、关闭及中断处理等关键步骤,并提供了相应的测试程序以验证驱动功能。

一,驱动程序:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>

#define GPBCONADDR 0x56000010  //GPBCON物理地址
#define GPBDATADDR 0x56000014

static struct class *firstdev_class;
static struct device *firstdev_class_dev;

volatile unsigned long *gpbcon;
volatile unsigned long *gpbdat;	


static int wzc_driver_open(struct inode *inode, struct file *file)
{
	printk("wzc open!\n");
	gpbcon = (volatile unsigned long *)ioremap(GPBCONADDR,16); //ioremap实现物理地址到虚拟地址的转换
	gpbdat = gpbcon+1;
	*gpbcon &= ~((3<<5*2) | (3<<6*2) | (3<<7*2) | (3<<8*2));//使IO口功能为输出
	*gpbcon |= ((1<<5*2) | (1<<6*2) | (1<<7*2) | (1<<8*2));
	return 0;
}

static int  wzc_driver_write(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
	int val;
	copy_from_user(&val, buff, 4);//copy_from_user实现从用户空间读取参数到内核空间
	printk("wzc write!\n");
	if(val ==1)
	{
		*gpbdat &= ~(0x0f<<5);
	}
	else if(val == 0)
	{
		*gpbdat |= (0x0f<<5);
	}
	return 0;
}

static struct file_operations wzc_fp = 
{
	.owner = THIS_MODULE,
	.open = wzc_driver_open,
	.write = wzc_driver_write
};


int major;
static int wzc_driver_init(void)
{
	printk("wzc init2212!\n");
	major = register_chrdev(0,"wzc_driver",&wzc_fp);	//注册字符驱动程序,0代表系统自动分配
	firstdev_class = class_create(THIS_MODULE, "firstdev");
	firstdev_class_dev = device_create(firstdev_class, NULL, MKDEV(major, 0), NULL, "firstdev");
	return 0;
}



static void wzc_driver_exit(void)
{
	unregister_chrdev(major,"wzc_driver");
	device_unregister(firstdev_class_dev);
	class_destroy(firstdev_class);
}


module_init(wzc_driver_init);
module_exit(wzc_driver_exit);
MODULE_LICENSE("GPL");

二,测试程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main(int argc, char** argv)
{
	int fd;
	int val;
	if(argc != 2)
	{
		printf("usage:\n");
		printf("%s <on|off>\n",argv[0]);
		return 0;
	}
	fd = open("/dev/firstdev",O_RDWR);
	if(fd<0)
	{
		printf("can't open file!\n");
		return 0;
	}
	if(strcmp(argv[1],"on") == 0)
	{
		val = 1;
		write(fd,&val,4);
	}
	else if(strcmp(argv[1],"off") == 0)
	{
		val = 0;
		write(fd,&val,4);
	}
	else
	{
		printf("usage:\n");
		printf("%s <on|off>\n",argv[0]);
		return 0;
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值