当前位置:   article > 正文

韦东山imx6ull,设备树电亮LED灯,课后作业笔记_imx6ull韦东山led点灯

imx6ull韦东山led点灯

此博客仅记录Linux学习路上的问题,以及总结,以供复习。

欢迎留下更有用的知识,或者学习方法,感谢!

1、总结3种写驱动程序的方法

1.1 资源和驱动放在同一文件下

比较容易理解,和单片机直接操作寄存器一样。

1.2 使用platform_device和platform_driver来完成,分离思想

其中device中指定要用到的资源,resources数组中有引脚,名字等

  1. static struct platform_device board_A_led_dev = {
  2. .name = "100ask_led",
  3. .num_resources = ARRAY_SIZE(resources),
  4. .resource = resources,
  5. .dev = {
  6. .release = led_dev_release,
  7. },
  8. };

dirver是更低一层的驱动,里面的名字要与device中匹配,probe函数在匹配完成后会被调用,多个设备会调用多次,remove函数在卸载时被调用。

  1. static struct platform_driver chip_demo_gpio_driver = {
  2. .probe = chip_demo_gpio_probe,
  3. .remove = chip_demo_gpio_remove,
  4. .driver = {
  5. .name = "100ask_led",
  6. },
  7. };

在此基础上写好各自驱动的入口出口函数。

1.3 使用设备树

使用设备树之后,相当于不用我们自己去写资源,而是写一个有关资源的dts文件,然后内核去里面取出设备资源,然后driver再去匹配,只要编写dirver就可以。

  1. static struct platform_driver chip_demo_gpio_driver = {
  2. .probe = chip_demo_gpio_probe,
  3. .remove = chip_demo_gpio_remove,
  4. .driver = {
  5. .name = "100ask_led",
  6. .of_match_table = ask100_leds, //想要支持设备树的话,加上这个
  7. },
  8. };

这里使用设备树要加上match_table,告诉内核。probe和remove函数和1.2一样,匹配之后自动调用。

dts文件中只要加上设备,格式有点像json字符的格式,首先要写好compatible,这个参数是和driver中的of_match_table对应的,值要相同,匹配的时候首先就去匹配这个值。

2、主要代码

  把课后作业的点灯加到了最底层,直接操作硬件。

  /sys/bus/platform/driver  这个目录是im6ull看注册进内核的driver,与别的芯片有区别

board.c

  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/errno.h>
  4. #include <linux/miscdevice.h>
  5. #include <linux/kernel.h>
  6. #include <linux/major.h>
  7. #include <linux/mutex.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/stat.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/tty.h>
  14. #include <linux/kmod.h>
  15. #include <linux/gfp.h>
  16. #include <linux/platform_device.h>
  17. #include "led_resource.h"
  18. static void led_dev_release(struct device *dev)
  19. {
  20. }
  21. static struct resource resources[] = {
  22. {
  23. .start = GROUP_PIN(5,3),
  24. .flags = RESOURCE_IRQ,
  25. .name = "100ask_led_pin",
  26. },
  27. {
  28. .start = GROUP_PIN(5,8),
  29. .flags = IORESOURCE_IRQ,
  30. .name = "100ask_led_pin",
  31. },
  32. };
  33. static struct platform_device board_A_led_dev = {
  34. .name = "100ask_led",
  35. .num_resources = ARRAY_SIZE(resources),
  36. .resource = resources,
  37. .dev = {
  38. .release = led_dev_release,
  39. },
  40. };
  41. static int __init led_dev_init(void)
  42. {
  43. int err;
  44. err = platform_device_register(&board_A_led_dev);
  45. return 0;
  46. }
  47. static void __exit led_dev_exit(void)
  48. {
  49. platform_device_unregister(&board_A_led_dev);
  50. }
  51. module_init(led_dev_init);
  52. module_exit(led_dev_exit);
  53. MODULE_LICENSE("GPL");

chip_demo_gpio.c

  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/errno.h>
  4. #include <linux/miscdevice.h>
  5. #include <linux/kernel.h>
  6. #include <linux/major.h>
  7. #include <linux/mutex.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/stat.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/tty.h>
  14. #include <linux/kmod.h>
  15. #include <linux/gfp.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <asm/io.h>
  19. #include "led_opr.h"
  20. #include "leddrv.h"
  21. #include "led_resource.h"
  22. static int g_ledpins[100];
  23. static int g_ledcnt = 0;
  24. static volatile unsigned int *CCM_CCGR1; //0x020C4000 + 0x6C
  25. static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3; //0x02290000 + 0x14
  26. static volatile unsigned int *GPIO5_GDIR;
  27. static volatile unsigned int *GPIO5_DR;
  28. static int board_demo_led_init (int which) /* 初始化LED, which-哪个LED */
  29. {
  30. //printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
  31. int val;
  32. printk("init gpio: group %d, pin %d\n", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
  33. switch(GROUP(g_ledpins[which]))
  34. {
  35. case 5:
  36. {
  37. printk("init pin of group 5 ...\n");
  38. if(!CCM_CCGR1)//虚拟地址映射,没映射ccm是空指针,会进入
  39. {
  40. CCM_CCGR1 = ioremap(0x020C4000 + 0x6C,4); //0x020C4000 + 0x6C
  41. IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x02290000 + 0x14,4); //0x02290000 + 0x14
  42. GPIO5_GDIR = ioremap(0x020AC004,4);
  43. GPIO5_DR = ioremap(0x020AC000,4);
  44. }
  45. //使能gpio
  46. *CCM_CCGR1 |= (3<<30);
  47. //设置引脚为gpio 先清0 再去设置位0101
  48. val = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 ;
  49. val&= ~(0xf);
  50. val|= (5);
  51. *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = val;
  52. //设置哪一个组输入输出
  53. *GPIO5_GDIR |= (1<<3);
  54. break;
  55. }
  56. case 1:
  57. {
  58. printk("init pin of group 1 ...\n");
  59. break;
  60. }
  61. case 2:
  62. {
  63. printk("init pin of group 2 ...\n");
  64. break;
  65. }
  66. case 3:
  67. {
  68. printk("init pin of group 3 ...\n");
  69. break;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int board_demo_led_ctl (int which, char status) /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
  75. {
  76. //printk("%s %s line %d, led %d, %s\n", __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
  77. printk("set led %s: group %d, pin %d\n", status ? "on" : "off", GROUP(g_ledpins[which]), PIN(g_ledpins[which]));
  78. switch(GROUP(g_ledpins[which]))
  79. {
  80. case 5:
  81. {
  82. printk("set pin of group 0 ...\n");
  83. if(status == 0) //输出高电平
  84. *GPIO5_DR |= (1<<3);
  85. else if (status == 1)//输出低电平
  86. {
  87. *GPIO5_DR &= ~(1<<3);
  88. }
  89. break;
  90. }
  91. case 1:
  92. {
  93. printk("set pin of group 1 ...\n");
  94. break;
  95. }
  96. case 2:
  97. {
  98. printk("set pin of group 2 ...\n");
  99. break;
  100. }
  101. case 3:
  102. {
  103. printk("set pin of group 3 ...\n");
  104. break;
  105. }
  106. }
  107. return 0;
  108. }
  109. static struct led_operations board_demo_led_opr = {
  110. .init = board_demo_led_init,
  111. .ctl = board_demo_led_ctl,
  112. };
  113. struct led_operations *get_board_led_opr(void)
  114. {
  115. return &board_demo_led_opr;
  116. }
  117. static int chip_demo_gpio_probe(struct platform_device *pdev)
  118. {
  119. struct device_node *np;
  120. int err = 0;
  121. int led_pin;
  122. np = pdev->dev.of_node;
  123. if (!np)//节点不存在
  124. return -1;
  125. err = of_property_read_u32(np, "pin", &led_pin);//从节点np中读取pin属性,保存在led_pin中
  126. g_ledpins[g_ledcnt] = led_pin;
  127. led_class_create_device(g_ledcnt);
  128. g_ledcnt++;
  129. return 0;
  130. }
  131. static int chip_demo_gpio_remove(struct platform_device *pdev)
  132. {
  133. int i = 0;
  134. int err;
  135. struct device_node *np;
  136. int led_pin;
  137. np = pdev->dev.of_node;
  138. if (!np)
  139. return -1;
  140. err = of_property_read_u32(np, "pin", &led_pin);
  141. for (i = 0; i < g_ledcnt; i++)
  142. {
  143. if (g_ledpins[i] == led_pin)
  144. {
  145. led_class_destroy_device(i);
  146. g_ledpins[i] = -1;//销毁之后,讲pin设置为-1
  147. break;
  148. };
  149. }
  150. for (i = 0; i < g_ledcnt; i++)
  151. {
  152. if (g_ledpins[i] != -1)
  153. break;
  154. }
  155. if (i == g_ledcnt)
  156. g_ledcnt = 0;//每一项都是-1(每一个都销毁了) 重新将索引值设为0
  157. return 0;
  158. }
  159. static const struct of_device_id ask100_leds[] = {
  160. { .compatible = "100as,leddrv" },
  161. { },
  162. };
  163. static struct platform_driver chip_demo_gpio_driver = {
  164. .probe = chip_demo_gpio_probe,
  165. .remove = chip_demo_gpio_remove,
  166. .driver = {
  167. .name = "100ask_led",
  168. .of_match_table = ask100_leds, //想要支持设备树的话,加上这个
  169. },
  170. };
  171. static int __init chip_demo_gpio_drv_init(void)
  172. {
  173. int err;
  174. err = platform_driver_register(&chip_demo_gpio_driver);
  175. register_led_operations(&board_demo_led_opr);
  176. return 0;
  177. }
  178. static void __exit lchip_demo_gpio_drv_exit(void)
  179. {
  180. platform_driver_unregister(&chip_demo_gpio_driver);
  181. }
  182. module_init(chip_demo_gpio_drv_init);
  183. module_exit(lchip_demo_gpio_drv_exit);
  184. MODULE_LICENSE("GPL");

dts

  1. #define GROUP_PIN(g,p) ((g<<16) | (p))
  2. / {
  3. 100ask_led@0 {
  4. compatible = "100as,leddrv";
  5. pin = <GROUP_PIN(5, 3)>;//这里只要跟文件中对的上就行,实际还是操作硬件的
  6. };
  7. 100ask_led@1 {
  8. compatible = "100as,leddrv";
  9. pin = <GROUP_PIN(5, 8)>;
  10. };
  11. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/688828
推荐阅读
  

闽ICP备14008679号