08_第一个相对完整的驱动实践编写
1.01_Linux最简单驱动-helloworld2.02_Linux下编译驱动模块实践3.03_make menuconfig图形化配置4.04_Linux下把驱动编译进内核5.05_杂项设备驱动6.06_应用层和内核层实现数据交互7.07_Linux物理地址到虚拟地址映射
8.08_第一个相对完整的驱动实践编写
9.09_驱动模块传参数10.10_申请字符类设备号11.11_注册字符类设备12.12_自动创建设备节点13.13_Platform 设备驱动14.14_Linux 设备树15.15_pinctl和gpio子系统16.16_ioctl接口17.17_Linux中断18.18_内核定时器19.19_输入子系统20.20_Linux I2C 驱动21.21_FT5X06触摸驱动实验第一个相对完整的驱动实践编写
需求:
1.使用杂项设备完成一个蜂鸣器的驱动。
2.完成一个上层测试应用。
应用要求:在上层应用中传入参数1为打开蜂鸣器,传入参数0为 关闭蜂鸣器。
buzzer.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#define GPIO5_DR 0x020AC000 //蜂鸣器物理地址
unsigned int *vir_gpio5_dr; //蜂鸣器虚拟地址
int misc_open(struct inode *inode, struct file *file)
{
printk("hello misc_open\n");
return 0;
}
int misc_release(struct inode *inode, struct file *file)
{
printk("hello mise_release bye bye\n");
return 0;
}
ssize_t misc_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff_t)
{
char kbuf[64] = "heheh";
if (copy_to_user(ubuf, kbuf, strlen(kbuf) + 1) != 0)
{
printk("copy_to_user error\n");
return -1;
}
printk("hello misc_read bye bye\n");
return 0;
}
ssize_t misc_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff_t)
{
char kbuf[64] = {0};
if (copy_from_user(kbuf, ubuf, size) != 0)
{
printk("copy_from_user error\n");
return -1;
}
printk("hello misc_write bye bye\n");
if(kbuf[0] == 1)
{
*vir_gpio5_dr |= 0x02;
}else if(kbuf[0] == 0)//关闭蜂鸣器
{
*vir_gpio5_dr &= ~0x02;
}
return 0;
}
/* 文件操作集 */
struct file_operations misc_fops = {
.owner = THIS_MODULE, // 当前模块
.open = misc_open,
.release = misc_release,
.write = misc_write,
.read = misc_read,
};
/* 杂项设备结构体 */
struct miscdevice misc_dev = {
.minor = MISC_DYNAMIC_MINOR, // 动态分配次设备号
.name = "hello_mise", // 设备节点的名字
.fops = &misc_fops // 文件操作集
};
static int mise_init(void)
{
int ret;
ret = misc_register(&misc_dev); // 注册杂项设备
if (ret < 0)
{
printk("misc register is error!\n");
return -1;
}
printk("mise register is ok!\n");
vir_gpio5_dr = ioremap(GPIO5_DR, 4); //将物理地址转化为虚拟地址
if(vir_gpio5_dr == NULL)
{
printk("GPIO5_DR is error!\n");
return -EBUSY;
}
printk("GPIO5_DR is ok!\n");
return 0;
}
static void mise_exit(void)
{
misc_deregister(&misc_dev); // 注销杂项设备
printk("misc gooodbye!\n");
iounmap(vir_gpio5_dr); //卸载杂项设备
}
module_init(mise_init);
module_exit(mise_exit);
MODULE_LICENSE("GPL");
app.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int fd;
char buf[64] = {0};
fd = open("/dev/hello_mise", O_RDWR);
if (fd < 0)
{
perror("open error\n");
return fd;
}
buf[0] = atoi(argv[1]);
write(fd, buf, strlen(buf)+1);
close(fd);
return 0;
}
Makefile
obj-m+=buzzer.o
KDIR:=/home/mzx/imx6ull/linux-imx-rel_imx_4.1.15_2.1.0_ga
PWD?=$(shell pwd)
all:
make -C $(KDIR) M=$(PWD) modules
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!