65.设备树下platform_device和platform_driver

在设备树根节点下添加这个节点

复制代码
/{
    quan {
        #address-cells = <1>;
        #size-cells = <1>;
        compatible = "simple-bus";
        myLed{
            compatible = "my devicetree";
            reg = <0xFDD60000 0x00000004>;
        };
    };
};

driver.c

cpp 复制代码
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
// 平台设备的初始化函数
static int my_platform_probe(struct platform_device *pdev)
{
    printk(KERN_INFO "my_platform_probe: Probing platform device\n");
 
    // 添加设备特定的操作
    // ...
 
    return 0;
}
 
// 平台设备的移除函数
static void my_platform_remove(struct platform_device *pdev)
{
    printk(KERN_INFO "my_platform_remove: Removing platform device\n");
 
    // 清理设备特定的操作
    // ...
 
}
 
 
const struct of_device_id of_match_table_id[]  = {
    {.compatible="my devicetree"},
};
 
// 定义平台驱动结构体
static struct platform_driver my_platform_driver = {
    .probe = my_platform_probe,
    .remove = my_platform_remove,
    .driver = {
        .name = "my_platform_device",
        .owner = THIS_MODULE,
        .of_match_table =  of_match_table_id,
    },
};
 
// 模块初始化函数
static int __init my_platform_driver_init(void)
{
    int ret;
 
    // 注册平台驱动
    ret = platform_driver_register(&my_platform_driver);
    if (ret) {
        printk(KERN_ERR "Failed to register platform driver\n");
        return ret;
    }
 
    printk(KERN_INFO "my_platform_driver: Platform driver initialized\n");
 
    return 0;
}
 
// 模块退出函数
static void __exit my_platform_driver_exit(void)
{
    // 注销平台驱动
    platform_driver_unregister(&my_platform_driver);
 
    printk(KERN_INFO "my_platform_driver: Platform driver exited\n");
}
 
module_init(my_platform_driver_init);
module_exit(my_platform_driver_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("quan");

makefile

cpp 复制代码
obj-m += devicetree.o
KDIR:=/home/linux/samba-mount/linux-kernel/linux-6.17.5
PWD?=$(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules
	echo $(PWD)
clean:
	rm -rf *.ko *.o *.mod *.mod.o *.mod.c *.symvers *.order

install:
	cp  *.ko ../../linux-kernel/linux-6.17.5/kmodules

编译及开发板测试

相关推荐
sukalot10 小时前
windows 驱动实例分析系列: libusb驱动分析-应用层源码篇(三)
windows·驱动开发
智购科技无人售货机工厂15 小时前
2026自动售货机防拆机物理安全设计:从安全螺丝到结构互锁的工程实践~YH
android·网络·驱动开发·python·单片机·安全·云原生
Jaixln_HRF3 天前
率能SS8837T 单通道1.8A/12V H桥电机驱动芯片,低功耗睡眠模式120nA,用于摄像机/玩具/机器人
驱动开发·嵌入式硬件·机器人·硬件工程
bingcheby3 天前
PS通过AXI总线控制PL的LED灯实验之四---编程下载调试
驱动开发·fpga开发·iar
xu_wenming3 天前
嵌入式软件架构中的6种解耦艺术
c语言·驱动开发·嵌入式硬件·架构
DevHub3 天前
CANopen 教程:STM32 实现 CANopen 从站,3 个回调替代手写对象字典
c语言·驱动开发·stm32·单片机·嵌入式硬件·mcu
Jaixln_HRF4 天前
率能SS6216 单通道1.4A/7.2V直流电机驱动芯片,支持正反转/停止/刹车,用于玩具/智能锁/车载夹
驱动开发·嵌入式硬件·硬件工程
sukalot4 天前
windows 驱动实例分析系列: wireguard-nt驱动分析-driver篇(一)
windows·驱动开发
沫璃染墨4 天前
《从零入门Linux系统篇(二十九):文件篇·二——深入文件描述符:从文件描述符表到重定向,再到Shell实现》
linux·运维·服务器·c++·驱动开发·系统架构
Jaixln_HRF4 天前
率能SS6635E 单通道4.5A/30V直流电机驱动芯片,高压大电流,用于电子锁/玩具/机器人
驱动开发·嵌入式硬件·机器人·硬件工程