linux 内核驱动编写流程
1. 查询内核是否安装
dpkg-query -s linux-headers-$(uname -r)
2. 内核头文件目录
/usr/src
3. 增加头文件include
export ARMGCC_DIR="/opt/gcc-arm-none-eabi-10.3-2021.10/"
export IMX6_SDK="/home/jeams/SDK_2.2_MCIM6ULL/devices/MCIMX6Y2/":"/home/jeams/SDK_2.2_MCIM6ULL/devices/MCIMX6Y2/drivers/":"/home/jeams/SDK_2.2_MCIM6ULL/devices/MCIMX6Y2/utilities/":"/home/jeams/SDK_2.2_MCIM6ULL/CORTEXA/Include":"/home/jeams/SDK_2.2_MCIM6ULL/CMSIS/Include"
export LINUX_KERNEL_INCLUDE="/usr/src/linux-headers-6.5.0-45-generic/include/"
export C_INCLUDE_PATH=$C_INCLUDE_PATH:$IMX6_SDK:$LINUX_KERNEL_INCLUDE
## export cds_root="/home/jeams/cadence/installs/SPB172/"
export PATH=$PATH:$ARMGCC_DIR/bin/
export XDG_RUNTIME_DIR=/usr/lib/
export RUNLEVEL=3
4. 驱动代码
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kmod.h>
#include <linux/major.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/tty.h>
/* 1. 确定主设备号 */
static int major = 0;
static struct class *gpioctr_class;
static struct gpio_desc *gpioctr_gpio;
/* 2. 实现对应的 open/read/write 等函数,填入 file_operations 结构体*/
static ssize_t gpio_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offs et)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static ssize_t gpio_drv_write(struct file *file, const char __user *buf, size_t size, loff_ t *offset)
{
int err;
char status;
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_from_user(&status, buf, 1);
gpiod_set_value(gpioctr_gpio, status);
return 1;
}
static int gpio_drv_open(struct inode *node, struct file *file)
{
gpiod_direction_output(gpioctr_gpio, 0);
return 0;
}
static int gpio_drv_close(struct inode *node, struct file *file)
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
return 0;
}
/* 定义自己的 file_operations 结构体*/
static struct file_operations gpioctr_drv = {
.owner = THIS_MODULE,
.open = gpio_drv_open,
.read = gpio_drv_read,
.write = gpio_drv_write,
.release = gpio_drv_close,
};
/* 从 platform_device 获得 GPIO
* 把 file_operations 结构体告诉内核:注册驱动程序
*/
static int chip_demo_gpio_probe(struct platform_device *pdev)
{
/* 设备树中定义有: gpioctr-gpios=<...>; */
gpioctr_gpio = gpiod_get(&pdev->dev, "gpioctr", 0);
if (IS_ERR(gpioctr_gpio))
{
dev_err(&pdev->dev, "Failed to get GPIO for led\n");
return PTR_ERR(gpioctr_gpio);
}
/* 注册 file_operations */
major = register_chrdev(0, "myir_gpioctr", &gpioctr_drv); /* /dev/gpioctr */
gpioctr_class = class_create(THIS_MODULE, "myir_gpioctr_class");
if (IS_ERR(gpioctr_class))
{
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
unregister_chrdev(major, "gpioctr");
gpiod_put(gpioctr_gpio);
return PTR_ERR(gpioctr_class);
}
device_create(gpioctr_class, NULL, MKDEV(major, 0), NULL, "myir_gpioctr%d", 0);
return 0;
}
static int chip_demo_gpio_remove(struct platform_device *pdev)
{
device_destroy(gpioctr_class, MKDEV(major, 0));
class_destroy(gpioctr_class);
unregister_chrdev(major, "myir_gpioctr");
gpiod_put(gpioctr_gpio);
return 0;
}
static const struct of_device_id myir_gpioctr[] = {
{.compatible = "myir,gpioctr"},
{},
};
/* 定义 platform_driver */
static struct platform_driver chip_demo_gpio_driver = {
.probe = chip_demo_gpio_probe,
.remove = chip_demo_gpio_remove,
.driver =
{
.name = "myir_gpioctr",
.of_match_table = myir_gpioctr,
},
};
/* 在入口函数注册 platform_driver */
static int __init gpio_init(void)
{
int err;
err = platform_driver_register(&chip_demo_gpio_driver);
return err;
}
/* 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
* 卸载 platform_driver
*/
static void __exit gpio_exit(void)
{
platform_driver_unregister(&chip_demo_gpio_driver);
}
/* 其他完善:提供设备信息,自动创建设备节点 */
module_init(gpio_init);
module_exit(gpio_exit);
MODULE_LICENSE("GPL");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2023-08-08 nrf52832-mdk
2023-08-08 slui.exe error
2023-08-08 FreeBuds 5