Linux驱动开发入门之编译驱动模块

Linux驱动开发培训系列教程网址https://edu.csdn.net/course/detail/26814

Linux驱动开发入门之编译驱动模块

调试Linux驱动的基本流程是先把驱动编译成模块,通过insmod命令加载到内核进行调试。调试完成,编译进内核即可。下面的代码基于Linux3.2(AM335X)平台上经过验证。

第一步,在任意位置创建目录gao_driver
第二步,在目录gao_driver中创建文件hello.c makefile
其中hello.c内容:

/*******************************************************************************************
*
*******************************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
#include <linux/spi/spi.h>
#include <linux/bcd.h>
#include <linux/miscdevice.h>  

/*******************************************************************************************
*
*******************************************************************************************/
int my_gpio_open(struct inode *inode, struct file *filp)
{
	printk("my_gpio_open \n");
	
	return 0;
}

/*******************************************************************************************
*
*******************************************************************************************/
static ssize_t my_gpio_write(struct file *filp, const char __user *buf,  size_t size, loff_t *ppos)
{
	char gao_buf[100];

	printk("my_gpio_write\n");

	memset(gao_buf, 0, 100);

	/*用户空间->内核空间*/
	if (copy_from_user(gao_buf, buf, size))
	{
		printk("copy_from_user error\n");
	}
	printk("my_gpio_write=%s\n", gao_buf);
	return size;
}

/*******************************************************************************************
*
*******************************************************************************************/
static ssize_t my_gpio_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	char gao_buf[100];

	printk("my_gpio_read\n");

	memset(gao_buf, 0, 100);

	sprintf(gao_buf, "gaoyanli hello world");

	if(copy_to_user(buf, gao_buf, strlen(gao_buf)))
	{
		printk("my_gpio_read error\n");
	}
	return 0;
}

/*******************************************************************************************
*
*******************************************************************************************/
int my_gpio_release(struct inode *inode, struct file *filp)
{
	return 0;
}

/*******************************************************************************************
*
*******************************************************************************************/
struct file_operations my_gpio_fops = 
{
    .owner = THIS_MODULE,

	.open    =  my_gpio_open,
	.write   =  my_gpio_write,
	.read    =  my_gpio_read, 
	.release =  my_gpio_release,
};

//分配初始化miscdevice
static struct miscdevice my_gpio_dev = 
{
    .minor = MISC_DYNAMIC_MINOR,     //自动分配次设备号
    .name = "my_gpio",               //产生设备节点dev/my_gpio
    .fops = &my_gpio_fops
};

/*******************************************************************************************
*
*******************************************************************************************/
int my_gpio_init(void)
{
	printk("---------------------------------------------------------------------my_gpio_init\n");

	//注册混杂设备驱动。产生设备节点/dev/my_gpio
	misc_register(&my_gpio_dev); 

	return 0;
}

/*******************************************************************************************
*
*******************************************************************************************/
void my_gpio_remove(void)
{
	printk("------------------------------------------------------------------my_gpio_remove\n");


	//卸载混杂设备
	misc_deregister(&my_gpio_dev); 
}

/*******************************************************************************************
*
*******************************************************************************************/
module_init(my_gpio_init);
module_exit(my_gpio_remove);

MODULE_AUTHOR("gaoyanli");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:da5304"); 

其中makefile内容:

MOD_FILE=hello

#ifneq ($(KERNELRELEASE),)
	obj-m := $(MOD_FILE).o
#else
	KERNELDIR = /home/forlinx/okxx18-source-android51/kernel
	PWD :=$(shell pwd)
all:	
	$(MAKE) -C $(KERNELDIR)   M=$(PWD) ARCH=arm CROSS_COMPILE=/home/forlinx/okxx18-source-android51/prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi- modules  -Wno-error=format-security

clean:
	rm ./*.o
	rm ./*.ko
	rm ./*.mod.c
	rm ./module*
	rm ./Module*
#endif;

第三步,进入目录gao_driver,编译模块,生成hello.ko

#cd ./gao_driver
#make

第四步,把hello.ko下载到开发板,并加载进内核。

#insmod hello.ko
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gaoyanli1972

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

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

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

打赏作者

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

抵扣说明:

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

余额充值