当前位置:   article > 正文

imx6ull Linux使用设备树配置LED_imx6ull 更换设备树文件

imx6ull 更换设备树文件

我们基于寄存器的方式已经编写了LED驱动,实现点亮/熄灭LED,但是你有没有发现一个问题,就是假设LED修改了一个GPIO,那么需要对应的修改寄存器代码,非常繁琐,而且随着改板次数增加,那么会带来一个灾难性的重复性没有技术含量的重复工作的弊端。

在之前的文档我们已经介绍了LED寄存器实现方式:https://blog.csdn.net/XiaoXiaoPengBo/article/details/128684419?spm=1001.2014.3001.5501

并且我们之前文档也写过了设备树相关的概念内容:

https://blog.csdn.net/XiaoXiaoPengBo/article/details/128734828?spm=1001.2014.3001.5501

那么本节,我们就结合之前的内容把他利用起来!!!我们还是用之前的LED GPIO1_3

一. 修改设备树dts文件

1.修改dts文件内容

在根节点下增加一下内容

  1. wireless_link_led{
  2. #address-cells = <1>;
  3. #size-cells = <1>;
  4. compatible = "wl_led";
  5. status = "ok";
  6. reg = < 0X020C406C 0X04 /* CCM_CCGR1_BASE */
  7. 0X020E0068 0X04 /* SW_MUX_GPIO1_IO03_BASE */
  8. 0X020E02F4 0X04 /* SW_PAD_GPIO1_IO03_BASE */
  9. 0X0209C000 0X04 /* GPIO1_DR_BASE */
  10. 0X0209C004 0X04 >; /* GPIO1_GDIR_BASE */
  11. };

2.编译dts验证

make dtbs

二. 实现LED基于设备树的驱动

  1. #include <linux/types.h>
  2. #include <linux/kernel.h>
  3. #include <linux/delay.h>
  4. #include <linux/ide.h>
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/errno.h>
  8. #include <linux/gpio.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of.h>
  11. #include <asm/mach/map.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/io.h>
  14. #define LED_MAJOR 200
  15. #define LED_NAME "led"
  16. static struct class *led_class;
  17. struct device_node *led_nd;
  18. static void __iomem *IMX6U_CCM_CCGR1;
  19. static void __iomem *SW_MUX_GPIO1_IO03;
  20. static void __iomem *SW_PAD_GPIO1_IO03;
  21. static void __iomem *GPIO1_DR;
  22. static void __iomem *GPIO1_GDIR;
  23. static int led_open(struct inode *inode, struct file *filp)
  24. {
  25. printk("led_open\r\n");
  26. return 0;
  27. }
  28. static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
  29. {
  30. printk("led_read\r\n");
  31. return 0;
  32. }
  33. static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
  34. {
  35. printk("led_write\r\n");
  36. int retvalue;
  37. u32 val = 0;
  38. unsigned char databuf[1];
  39. unsigned char ledstat;
  40. retvalue = copy_from_user(databuf, buf, cnt);
  41. if(retvalue < 0) {
  42. printk("kernel write failed!\r\n");
  43. return -EFAULT;
  44. }
  45. ledstat = databuf[0];
  46. printk("ledstat:%d\r\n",ledstat);
  47. if(ledstat == 1)
  48. {
  49. val = readl(GPIO1_DR);
  50. val &= ~(1 << 3);
  51. writel(val, GPIO1_DR);
  52. }
  53. else if(ledstat == 0)
  54. {
  55. val = readl(GPIO1_DR);
  56. val|= (1 << 3);
  57. writel(val, GPIO1_DR);
  58. }
  59. return 1;
  60. }
  61. static int led_release(struct inode *inode, struct file *filp)
  62. {
  63. printk("led_release\r\n");
  64. return 0;
  65. }
  66. static struct file_operations led_fops = {
  67. .owner = THIS_MODULE,
  68. .open = led_open,
  69. .read = led_read,
  70. .write = led_write,
  71. .release = led_release,
  72. };
  73. static int __init led_driver_init(void)
  74. {
  75. u8 index;
  76. u32 val = 0;
  77. int ret = 0;
  78. const char *str;
  79. u32 regdata[14];
  80. struct property *proper;
  81. printk("led_driver_init\r\n");
  82. led_nd = of_find_node_by_path("/wireless_link_led");
  83. if(led_nd)
  84. printk("led nd found\r\n");
  85. else
  86. {
  87. printk("led nd is not found\r\n");
  88. return -1;
  89. }
  90. proper = of_find_property(led_nd, "compatible", NULL);
  91. if(proper)
  92. printk("compatible = %s\r\n", (char*)proper->value);
  93. else
  94. printk("compatible property find failed\r\n");
  95. ret = of_property_read_string(led_nd, "status", &str);
  96. printk("of_property_read_string ret:%d\r\n",ret);
  97. if(ret >= 0)
  98. printk("status = %s\r\n",str);
  99. ret = of_property_read_u32_array(led_nd, "reg", regdata, 10);
  100. printk("of_property_read_string ret:%d\r\n",ret);
  101. printk("---------------------------\r\n");
  102. for(index = 0; index < 10; index++)
  103. printk("%#X ", regdata[index]);
  104. printk("\r\n---------------------------\r\n");
  105. IMX6U_CCM_CCGR1 = of_iomap(led_nd, 0);
  106. SW_MUX_GPIO1_IO03 = of_iomap(led_nd, 1);
  107. SW_PAD_GPIO1_IO03 = of_iomap(led_nd, 2);
  108. GPIO1_DR = of_iomap(led_nd, 3);
  109. GPIO1_GDIR = of_iomap(led_nd, 4);
  110. val = readl(IMX6U_CCM_CCGR1);
  111. val &= ~(3 << 26);
  112. val |= (3 << 26);
  113. writel(val, IMX6U_CCM_CCGR1);
  114. writel(5, SW_MUX_GPIO1_IO03);
  115. writel(0x10B0, SW_PAD_GPIO1_IO03);
  116. val = readl(GPIO1_GDIR);
  117. val &= ~(1 << 3);
  118. val |= (1 << 3);
  119. writel(val, GPIO1_GDIR);
  120. val = readl(GPIO1_DR);
  121. val |= (1 << 3);
  122. writel(val, GPIO1_DR);
  123. ret = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);
  124. if(ret < 0){
  125. printk("register chrdev failed!\r\n");
  126. return -EIO;
  127. }
  128. led_class = class_create(THIS_MODULE,"led_class");
  129. device_create(led_class,NULL,MKDEV(LED_MAJOR,0),NULL,"led"); /* /dev/led */
  130. return 0;
  131. }
  132. static void __exit led_driver_exit(void)
  133. {
  134. printk("led_driver_exit\r\n");
  135. iounmap(IMX6U_CCM_CCGR1);
  136. iounmap(SW_MUX_GPIO1_IO03);
  137. iounmap(SW_PAD_GPIO1_IO03);
  138. iounmap(GPIO1_DR);
  139. iounmap(GPIO1_GDIR);
  140. device_destroy(led_class,MKDEV(LED_MAJOR,0));
  141. class_destroy(led_class);
  142. unregister_chrdev(LED_MAJOR, LED_NAME);
  143. }
  144. module_init(led_driver_init);
  145. module_exit(led_driver_exit);
  146. MODULE_LICENSE("GPL");

三. 实现APP验证程序

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdint.h>
  9. int main(int argc, char *argv[])
  10. {
  11. int fd;
  12. int ret;
  13. uint8_t led;
  14. fd = open(argv[1], O_RDWR);
  15. if(!strcmp("led_on",argv[2]))
  16. {
  17. printf("led on\r\n");
  18. led = 1;
  19. write(fd,&led,sizeof(led));
  20. }
  21. if(!strcmp("led_off",argv[2]))
  22. {
  23. led = 0;
  24. printf("led on\r\n");
  25. write(fd,&led,sizeof(led));
  26. }
  27. close(fd);
  28. }

四. 验证

点亮 LED灯 ./test_app /dev/led led_on

熄灭 LED灯 ./test_app /dev/led led_on

五.总结

其实到了这一步虽然不需要配对寄存器地址,但是代码还在编写寄存器代码,也就是说换了一个GPIO还是要修改代码,是的!!!别急,后续我们看了GPIO子系统以及pinctl子系统后相信会有答案!!!

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

闽ICP备14008679号