platform 设备驱动实验

您所在的位置:网站首页 asm设备使用手册 platform 设备驱动实验

platform 设备驱动实验

2023-04-02 21:48| 来源: 网络整理| 查看: 265

        本实验在没有使用设备树的前提下,使用platform平台点亮LED。其中leddevice.c是platform 设备文件,leddriver.c是platform 的驱动文件。

leddevice.c

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* * 寄存器地址定义 */ #define CCM_CCGR1_BASE (0X020C406C) #define SW_MUX_GPIO1_IO03_BASE (0X020E0068) #define SW_PAD_GPIO1_IO03_BASE (0X020E02F4) #define GPIO1_DR_BASE (0X0209C000) #define GPIO1_GDIR_BASE (0X0209C004) #define REGISTER_LENGTH 4 void leddevice_release(struct device *dev){ printk("leddevice release\r\n"); } /* * 设备资源信息,也就是LED0所使用的所有寄存器 */ static struct resource led_resources[] = { [0] = { .start = CCM_CCGR1_BASE, .end = (CCM_CCGR1_BASE + REGISTER_LENGTH - 1), .flags = IORESOURCE_MEM, }, [1] = { .start = SW_MUX_GPIO1_IO03_BASE, .end = (SW_MUX_GPIO1_IO03_BASE + REGISTER_LENGTH - 1), .flags = IORESOURCE_MEM, }, [2] = { .start = SW_PAD_GPIO1_IO03_BASE, .end = (SW_PAD_GPIO1_IO03_BASE + REGISTER_LENGTH - 1), .flags = IORESOURCE_MEM, }, [3] = { .start = GPIO1_DR_BASE, .end = (GPIO1_DR_BASE + REGISTER_LENGTH - 1), .flags = IORESOURCE_MEM, }, [4] = { .start = GPIO1_GDIR_BASE, .end = (GPIO1_GDIR_BASE + REGISTER_LENGTH - 1), .flags = IORESOURCE_MEM, }, }; static struct platform_device leddevice={ .name="imx6ul-led", .id =-1, .dev={ .release=leddevice_release, }, .num_resources=ARRAY_SIZE(led_resources), .resource=led_resources }; static int __init leddevice_init(void){ /*注册platform设备*/ return platform_device_register(&leddevice); } static void __exit leddevice_exit(void){ /*注销platform设备*/ platform_device_unregister(&leddevice); } module_init(leddevice_init);//加载模块 module_exit(leddevice_exit);//注销模块 MODULE_LICENSE("GPL"); MODULE_AUTHOR("liuchuanqiang");

程序说明:  

1、platform 设备结构体变量 leddevice,这里要注意 name 字段为“imx6ul-led”,所

以稍后编写 platform 驱动中的 name 字段也要为“imx6ul-led”,否则设备和驱动匹配失败。

2、num_resources 表示资源数量;resource 表示资源,也就是设备信息

3、platform_device_register(&leddevice);向Linux内核注册platform设备。

4、platform_device_unregister从 Linux内核中卸载platform设备。

leddriver.c

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define LEDDEV_CNT 1 /* 设备号长度 */ #define LEDDEV_NAME "platformled" /* 设备名字 */ #define LEDOFF 0 #define LEDON 1 /* 寄存器名 */ static void __iomem *IMX6U_CCM_CCGR1; static void __iomem *SW_MUX_GPIO1_IO03; static void __iomem *SW_PAD_GPIO1_IO03; static void __iomem *GPIO1_DR; static void __iomem *GPIO1_GDIR; /* leddev设备结构体 */ struct leddev_dev{ dev_t devid; /* 设备号 */ struct cdev cdev; /* cdev */ struct class *class; /* 类 */ struct device *device; /* 设备 */ int major; /* 主设备号 */ }; struct leddev_dev leddev; /* led设备 */ /* * @description : LED打开/关闭 * @param - sta : LEDON(0) 打开LED,LEDOFF(1) 关闭LED * @return : 无 */ void led0_switch(u8 sta) { u32 val = 0; if(sta == LEDON){ val = readl(GPIO1_DR); val &= ~(1 start, ressize[1]); SW_PAD_GPIO1_IO03 = ioremap(ledresource[2]->start, ressize[2]); GPIO1_DR = ioremap(ledresource[3]->start, ressize[3]); GPIO1_GDIR = ioremap(ledresource[4]->start, ressize[4]); val = readl(IMX6U_CCM_CCGR1); val &= ~(3


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3