• 驱动开发,基于gpio子系统编写LED灯的驱动,亮灭控制


    1.gpio子系统介绍

            一个芯片厂商生产出芯片后会给linux提供一个当前芯片中gpio外设的驱动,我们当前只需要调用对应的厂商驱动即可完成硬件的控制。而linux内核源码中的gpio厂商驱动有很多,这里linux内核对厂商驱动做了一些封装,提供了一系列的API,我们在自己编写的设备驱动中只需要调用这些API即可访问对应的厂商驱动,进而完成GPIO的控制。

     

    2.实现框图

     

     3.代码

    ---gpiod_dri.c---驱动程序
    1. #include <linux/init.h>
    2. #include <linux/module.h>
    3. #include <linux/of.h>
    4. #include <linux/of_gpio.h>
    5. #include <linux/gpio.h>
    6. #include <linux/io.h>
    7. #include <linux/device.h>
    8. #include <linux/uaccess.h>
    9. unsigned int major;
    10. struct device_node *dnode;
    11. struct gpio_desc *gpiono1;
    12. struct gpio_desc *gpiono2;
    13. struct gpio_desc *gpiono3;
    14. struct class *cls;
    15. struct device *dev;
    16. char kbuf[128] = {0};
    17. // 封装操作方法
    18. int mycdev_open(struct inode *inode, struct file *file)
    19. {
    20. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    21. return 0;
    22. }
    23. ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
    24. {
    25. int ret;
    26. ret = copy_to_user(ubuf, kbuf, size);
    27. if (ret)
    28. {
    29. printk("copy_to_ user err\n");
    30. return -EIO;
    31. }
    32. return 0;
    33. }
    34. ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
    35. {
    36. int ret;
    37. if (size > sizeof(kbuf))
    38. size = sizeof(kbuf);
    39. // 从用户拷贝数据
    40. ret = copy_from_user(kbuf, ubuf, size);
    41. if (ret)
    42. {
    43. printk("copy_from_user err\n");
    44. return -EIO;
    45. }
    46. switch (kbuf[0])
    47. {
    48. case '1':
    49. gpiod_set_value(gpiono1,!gpiod_get_value(gpiono1));
    50. break;
    51. case '2':
    52. gpiod_set_value(gpiono2,!gpiod_get_value(gpiono2));
    53. break;
    54. case '3':
    55. gpiod_set_value(gpiono3,!gpiod_get_value(gpiono3));
    56. break;
    57. }
    58. return 0;
    59. }
    60. int mycdev_close(struct inode *inode, struct file *file)
    61. {
    62. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    63. return 0;
    64. }
    65. struct file_operations fops = {
    66. .open = mycdev_open,
    67. .read = mycdev_read,
    68. .write = mycdev_write,
    69. .release = mycdev_close,
    70. };
    71. static int __init mycdev_init(void)
    72. {
    73. // 1字符设备驱动注册
    74. major = register_chrdev(0, "myled", &fops);
    75. if (major < 0)
    76. {
    77. printk("字符设备驱动注册失败\n");
    78. return major;
    79. }
    80. printk("字符设备驱动注册成功:major=%d\n", major);
    81. // 2向上提交目录
    82. cls = class_create(THIS_MODULE, "MYLED");
    83. if (IS_ERR(cls))
    84. {
    85. printk("向上提交目录失败\n");
    86. return -PTR_ERR(cls);
    87. }
    88. printk("向上提交目录成功\n");
    89. // 3向上提交设备节点信息
    90. int i;
    91. for (i = 0; i < 3; i++)
    92. {
    93. dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
    94. if (IS_ERR(dev))
    95. {
    96. printk("向上提交设备节点信息失败\n");
    97. return -PTR_ERR(dev);
    98. }
    99. }
    100. printk("向上提交设备节点信息成功\n");
    101. // 4解析LED的设备树节点
    102. dnode = of_find_node_by_path("/myled");
    103. if (dnode == NULL)
    104. {
    105. printk("解析设备树节点失败\n");
    106. return -1;
    107. }
    108. printk("解析GPIO信息成功\n");
    109. // 5申请gpio对象
    110. gpiono1 = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    111. if (IS_ERR(gpiono1))
    112. {
    113. printk("申请gpio1对象失败\n");
    114. return -ENXIO;
    115. }
    116. gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    117. if (IS_ERR(gpiono2))
    118. {
    119. printk("申请gpio2对象失败\n");
    120. return -ENXIO;
    121. }
    122. gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    123. if (IS_ERR(gpiono3))
    124. {
    125. printk("申请gpio3对象失败\n");
    126. return -ENXIO;
    127. }
    128. printk("申请gpio信息对象成功\n");
    129. return 0;
    130. }
    131. static void __exit mycdev_exit(void)
    132. {
    133. // 灭灯
    134. gpiod_set_value(gpiono1, 0);
    135. gpiod_set_value(gpiono2, 0);
    136. gpiod_set_value(gpiono3, 0);
    137. // 释放GPIO编号
    138. gpiod_put(gpiono1);
    139. gpiod_put(gpiono2);
    140. gpiod_put(gpiono3);
    141. // 销毁设备节点信息
    142. int i;
    143. for (i = 0; i < 3; i++)
    144. {
    145. device_destroy(cls, MKDEV(major, i));
    146. }
    147. // 销毁目录信息
    148. class_destroy(cls);
    149. // 字符设备驱动注销
    150. unregister_chrdev(major, "myled");
    151. }
    152. module_init(mycdev_init);
    153. module_exit(mycdev_exit);
    154. MODULE_LICENSE("GPL");
    ---test.c---应用程序测试程序
    1. #include <stdio.h>
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <fcntl.h>
    5. #include <unistd.h>
    6. #include <stdlib.h>
    7. #include <string.h>
    8. int main(int argc, char const *argv[])
    9. {
    10. int a;
    11. char buf[128] = {0};
    12. int fd = open("/dev/myled0", O_RDWR);
    13. if (fd < 0)
    14. {
    15. printf("设备文件打开失败\n");
    16. exit(-1);
    17. }
    18. while (1)
    19. {
    20. printf("请输入控制的灯:1(LED1) 2(LED2) 3(LED3)\n");
    21. fgets(buf,sizeof(buf),stdin);
    22. buf[strlen(buf)-1] = '\0';
    23. //像设备文件中写
    24. write(fd,buf,sizeof(buf));
    25. }
    26. close(fd);
    27. return 0;
    28. }

     

    4.测试现象 

            输入灯编号,对状态取反;

  • 相关阅读:
    【文件包含漏洞-03】文件包含漏洞的六种利用方式
    电商平台API接口亚马逊国际站获得AMAZON商品详情操作案例
    【译】32位 .NET Framework 项目的 WinForm 设计器选择
    【字符串】有序队列 排序
    Rank-embedded Hashing for Large-scale Image Retrieval
    I2C外设
    k8s之PV、PVC和StorageClass
    游览器找不到服务器上PHP文件的一种原因
    状态模式和策略模式对比
    想做好接口测试,先把这些概念搞清楚了
  • 原文地址:https://blog.csdn.net/weixin_46260677/article/details/132992566