赞
踩
此博客仅记录Linux学习路上的问题,以及总结,以供复习。
欢迎留下更有用的知识,或者学习方法,感谢!
比较容易理解,和单片机直接操作寄存器一样。
其中device中指定要用到的资源,resources数组中有引脚,名字等
- static struct platform_device board_A_led_dev = {
- .name = "100ask_led",
- .num_resources = ARRAY_SIZE(resources),
- .resource = resources,
- .dev = {
- .release = led_dev_release,
- },
- };
dirver是更低一层的驱动,里面的名字要与device中匹配,probe函数在匹配完成后会被调用,多个设备会调用多次,remove函数在卸载时被调用。
- static struct platform_driver chip_demo_gpio_driver = {
- .probe = chip_demo_gpio_probe,
- .remove = chip_demo_gpio_remove,
- .driver = {
- .name = "100ask_led",
- },
- };
在此基础上写好各自驱动的入口出口函数。
使用设备树之后,相当于不用我们自己去写资源,而是写一个有关资源的dts文件,然后内核去里面取出设备资源,然后driver再去匹配,只要编写dirver就可以。
- static struct platform_driver chip_demo_gpio_driver = {
- .probe = chip_demo_gpio_probe,
- .remove = chip_demo_gpio_remove,
- .driver = {
- .name = "100ask_led",
- .of_match_table = ask100_leds, //想要支持设备树的话,加上这个
- },
- };
这里使用设备树要加上match_table,告诉内核。probe和remove函数和1.2一样,匹配之后自动调用。
dts文件中只要加上设备,格式有点像json字符的格式,首先要写好compatible,这个参数是和driver中的of_match_table对应的,值要相同,匹配的时候首先就去匹配这个值。
把课后作业的点灯加到了最底层,直接操作硬件。
/sys/bus/platform/driver 这个目录是im6ull看注册进内核的driver,与别的芯片有区别
-
- #include <linux/module.h>
-
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/miscdevice.h>
- #include <linux/kernel.h>
- #include <linux/major.h>
- #include <linux/mutex.h>
- #include <linux/proc_fs.h>
- #include <linux/seq_file.h>
- #include <linux/stat.h>
- #include <linux/init.h>
- #include <linux/device.h>
- #include <linux/tty.h>
- #include <linux/kmod.h>
- #include <linux/gfp.h>
- #include <linux/platform_device.h>
-
- #include "led_resource.h"
-
-
- static void led_dev_release(struct device *dev)
- {
- }
-
- static struct resource resources[] = {
- {
- .start = GROUP_PIN(5,3),
- .flags = RESOURCE_IRQ,
- .name = "100ask_led_pin",
- },
- {
- .start = GROUP_PIN(5,8),
- .flags = IORESOURCE_IRQ,
- .name = "100ask_led_pin",
- },
- };
-
-
- static struct platform_device board_A_led_dev = {
- .name = "100ask_led",
- .num_resources = ARRAY_SIZE(resources),
- .resource = resources,
- .dev = {
- .release = led_dev_release,
- },
- };
-
- static int __init led_dev_init(void)
- {
- int err;
-
- err = platform_device_register(&board_A_led_dev);
-
- return 0;
- }
-
- static void __exit led_dev_exit(void)
- {
- platform_device_unregister(&board_A_led_dev);
- }
-
- module_init(led_dev_init);
- module_exit(led_dev_exit);
-
- MODULE_LICENSE("GPL");
-
- #include <linux/module.h>
-
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/miscdevice.h>
- #include <linux/kernel.h>
- #include <linux/major.h>
- #include <linux/mutex.h>
- #include <linux/proc_fs.h>
- #include <linux/seq_file.h>
- #include <linux/stat.h>
- #include <linux/init.h>
- #include <linux/device.h>
- #include <linux/tty.h>
- #include <linux/kmod.h>
- #include <linux/gfp.h>
- #include <linux/platform_device.h>
- #include <linux/of.h>
- #include <asm/io.h>
- #include "led_opr.h"
- #include "leddrv.h"
- #include "led_resource.h"
-
- static int g_ledpins[100];
- static int g_ledcnt = 0;
-
- static volatile unsigned int *CCM_CCGR1; //0x020C4000 + 0x6C
- static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3; //0x02290000 + 0x14
- static volatile unsigned int *GPIO5_GDIR;
- static volatile unsigned int *GPIO5_DR;
-
- static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */
- {
- //printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
- int val;
- printk("init gpio: group %d, pin %d\n", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
- switch(GROUP(g_ledpins[which]))
- {
- case 5:
- {
- printk("init pin of group 5 ...\n");
- if(!CCM_CCGR1)//虚拟地址映射,没映射ccm是空指针,会进入
- {
- CCM_CCGR1 = ioremap(0x020C4000 + 0x6C,4); //0x020C4000 + 0x6C
- IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x02290000 + 0x14,4); //0x02290000 + 0x14
- GPIO5_GDIR = ioremap(0x020AC004,4);
- GPIO5_DR = ioremap(0x020AC000,4);
- }
- //使能gpio
- *CCM_CCGR1 |= (3<<30);
- //设置引脚为gpio 先清0 再去设置位0101
- val = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 ;
- val&= ~(0xf);
- val|= (5);
- *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = val;
-
- //设置哪一个组输入输出
- *GPIO5_GDIR |= (1<<3);
- break;
- }
- case 1:
- {
- printk("init pin of group 1 ...\n");
- break;
- }
- case 2:
- {
- printk("init pin of group 2 ...\n");
- break;
- }
- case 3:
- {
- printk("init pin of group 3 ...\n");
- break;
- }
- }
-
- return 0;
- }
-
- static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
- {
- //printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
- printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
-
- switch(GROUP(g_ledpins[which]))
- {
- case 5:
- {
- printk("set pin of group 0 ...\n");
- if(status == 0) //输出高电平
- *GPIO5_DR |= (1<<3);
- else if (status == 1)//输出低电平
- {
- *GPIO5_DR &= ~(1<<3);
- }
- break;
- }
- case 1:
- {
- printk("set pin of group 1 ...\n");
- break;
- }
- case 2:
- {
- printk("set pin of group 2 ...\n");
- break;
- }
- case 3:
- {
- printk("set pin of group 3 ...\n");
- break;
- }
- }
-
- return 0;
- }
-
- static struct led_operations board_demo_led_opr = {
- .init = board_demo_led_init,
- .ctl = board_demo_led_ctl,
- };
-
- struct led_operations *get_board_led_opr(void)
- {
- return &board_demo_led_opr;
- }
-
- static int chip_demo_gpio_probe(struct platform_device *pdev)
- {
- struct device_node *np;
- int err = 0;
- int led_pin;
-
- np = pdev->dev.of_node;
- if (!np)//节点不存在
- return -1;
-
- err = of_property_read_u32(np, "pin", &led_pin);//从节点np中读取pin属性,保存在led_pin中
-
- g_ledpins[g_ledcnt] = led_pin;
- led_class_create_device(g_ledcnt);
- g_ledcnt++;
-
- return 0;
-
- }
-
- static int chip_demo_gpio_remove(struct platform_device *pdev)
- {
- int i = 0;
- int err;
- struct device_node *np;
- int led_pin;
-
- np = pdev->dev.of_node;
- if (!np)
- return -1;
-
- err = of_property_read_u32(np, "pin", &led_pin);
-
- for (i = 0; i < g_ledcnt; i++)
- {
- if (g_ledpins[i] == led_pin)
- {
- led_class_destroy_device(i);
- g_ledpins[i] = -1;//销毁之后,讲pin设置为-1
- break;
- };
- }
-
- for (i = 0; i < g_ledcnt; i++)
- {
- if (g_ledpins[i] != -1)
- break;
- }
-
- if (i == g_ledcnt)
- g_ledcnt = 0;//每一项都是-1(每一个都销毁了) 重新将索引值设为0
-
- return 0;
- }
-
- static const struct of_device_id ask100_leds[] = {
- { .compatible = "100as,leddrv" },
- { },
- };
-
- static struct platform_driver chip_demo_gpio_driver = {
- .probe = chip_demo_gpio_probe,
- .remove = chip_demo_gpio_remove,
- .driver = {
- .name = "100ask_led",
- .of_match_table = ask100_leds, //想要支持设备树的话,加上这个
- },
- };
-
- static int __init chip_demo_gpio_drv_init(void)
- {
- int err;
-
- err = platform_driver_register(&chip_demo_gpio_driver);
- register_led_operations(&board_demo_led_opr);
-
- return 0;
- }
-
- static void __exit lchip_demo_gpio_drv_exit(void)
- {
- platform_driver_unregister(&chip_demo_gpio_driver);
- }
-
- module_init(chip_demo_gpio_drv_init);
- module_exit(lchip_demo_gpio_drv_exit);
-
- MODULE_LICENSE("GPL");
-
- #define GROUP_PIN(g,p) ((g<<16) | (p))
-
- / {
- 100ask_led@0 {
- compatible = "100as,leddrv";
- pin = <GROUP_PIN(5, 3)>;//这里只要跟文件中对的上就行,实际还是操作硬件的
- };
-
- 100ask_led@1 {
- compatible = "100as,leddrv";
- pin = <GROUP_PIN(5, 8)>;
- };
-
- };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。