Linux下驱动程序的操作(杂)

本文详细介绍了一个简单的嵌入式Linux字符设备驱动开发过程,包括使用Makefile进行编译、利用insmod和rmmod命令加载及卸载驱动、通过ioremap映射外设寄存器等关键技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1、 # insmod xx_drv.ko //加载该驱动模块

# lsmod //列出加载的模块

#rmmod xx_drv.ko //卸载该驱动模块


2、Makefile://KERN_DIR是编译的kernel的Makefile路径

KERN_DIR =/home/xu/tool-kernel/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules 
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += forth_drv.o


#arm-linux-gcc -o drvtest drvtest.c //交叉编译测试程序,./drvtest可执行



###########################################################################################

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>


static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev; //建一个“xyz”索引自由分配的驱动,MKDEV(major, 0)为主从设备号

volatile unsigned long *gpbcon = NULL;
volatile unsigned long *gpbdat = NULL;

int major;
static int first_drv_init(void)
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核,0设备号指内核自由分配设备号
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16);//将io寄存器地址直接映射给内核里的变量
gpbdat = gpbcon + 1;
return 0;
}

static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载
class_device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
iounmap(gpbcon);//取消映射
}

static int first_drv_open(struct inode *inode, struct file *file)
{
return 0;
}


static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
//copy_from_user(&val, buf, count); //把用户空间buf的值传到驱动里变量val,count长度
//copy_to_user(buf, &val, count);//相反
return 0;
}

static struct file_operations first_drv_fops = { //字符设备
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,     
    .write = first_drv_write,    
    

    .release =  third_drv_close,
};


module_init(first_drv_init);//入口
module_exit(first_drv_exit);//出口
MODULE_LICENSE("GPL");

#############################

request_irq(IRQ_EINT8,  buttons_irq, IRQT_BOTHEDGE, "K1", &pins_desc[0]);//按键中断初始化,和pins_desc[0]变量关联

free_irq(IRQ_EINT8,  &pins_desc[0]);//关掉按键中断


static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//申明一个button_waitq等待队列

wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */

wait_event_interruptible(button_waitq, ev_press);//如果button_waitq唤醒了且ev_press=1则不休眠,否则休眠












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值