• 驱动DAY8


    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/timer.h>
    7. /* myled
    8. {
    9. led1-gpio=<&gpioe 10 0>;
    10. led2-gpio=<&gpiof 10 0>;
    11. led3-gpio=<&gpioe 8 0>;
    12. };
    13. */
    14. struct device_node *dnode;
    15. struct gpio_desc *gpiono;
    16. struct gpio_desc *gpiono2;
    17. struct gpio_desc *gpiono3;
    18. struct timer_list mytimer;
    19. struct timer_list mytimer2;
    20. void mytimer_function(struct timer_list *timer)
    21. {
    22. gpiod_set_value(gpiono, !gpiod_get_value(gpiono));
    23. gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
    24. gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
    25. mod_timer(timer, jiffies + HZ);
    26. }
    27. void mytimer_function2(struct timer_list *timer2)
    28. {
    29. printk("hello world\n");
    30. mod_timer(timer2, jiffies + HZ*5);
    31. }
    32. static int __init mycdev_init(void)
    33. {
    34. // 解析设备树节点信息
    35. dnode = of_find_node_by_path("/myled");
    36. if (dnode == NULL)
    37. {
    38. printk("解析设备树节点失败\n");
    39. return -ENXIO;
    40. }
    41. // 获取LED1 GPIO编号
    42. gpiono = gpiod_get_from_of_node(dnode, "led1-gpio", 0, GPIOD_OUT_LOW, NULL);
    43. if (IS_ERR(gpiono))
    44. {
    45. printk("申请gpio信息失败\n");
    46. return -PTR_ERR(gpiono);
    47. }
    48. // 灯亮
    49. //gpiod_set_value(gpiono, 1);
    50. // 获取LED2 GPIO编号
    51. gpiono2 = gpiod_get_from_of_node(dnode, "led2-gpio", 0, GPIOD_OUT_LOW, NULL);
    52. if (IS_ERR(gpiono2))
    53. {
    54. printk("申请gpio信息失败\n");
    55. return -PTR_ERR(gpiono2);
    56. }
    57. // 灯亮
    58. //gpiod_set_value(gpiono2, 1);
    59. // 获取LED3 GPIO编号
    60. gpiono3 = gpiod_get_from_of_node(dnode, "led3-gpio", 0, GPIOD_OUT_LOW, NULL);
    61. if (IS_ERR(gpiono3))
    62. {
    63. printk("申请gpio信息失败\n");
    64. return -PTR_ERR(gpiono3);
    65. }
    66. // 灯亮
    67. //gpiod_set_value(gpiono3, 1);
    68. timer_setup(&mytimer, mytimer_function, 0);
    69. mytimer.expires = jiffies + HZ;
    70. add_timer(&mytimer);
    71. timer_setup(&mytimer2, mytimer_function2, 0);
    72. mytimer2.expires = jiffies + 5*HZ;
    73. add_timer(&mytimer2);
    74. return 0;
    75. }
    76. static void __exit mycdev_exit(void)
    77. {
    78. del_timer(&mytimer);
    79. del_timer(&mytimer2);
    80. gpiod_set_value(gpiono, 0);
    81. // 释放GPIO编号
    82. gpiod_put(gpiono);
    83. gpiod_set_value(gpiono2, 0);
    84. // 释放GPIO编号
    85. gpiod_put(gpiono2);
    86. gpiod_set_value(gpiono3, 0);
    87. // 释放GPIO编号
    88. gpiod_put(gpiono3);
    89. }
    90. module_init(mycdev_init);
    91. module_exit(mycdev_exit);
    92. MODULE_LICENSE("GPL");

  • 相关阅读:
    栈(C语言实现)
    再探Handler(下)(Handler核心原理最全解析)
    云计算与边缘计算:有何不同?
    重生奇迹MU玛雅宝石的获取方法
    HarmonyOS应用开发入门(五)
    17-VMware Horizon 2203 虚拟桌面-Win10 手动桌面池浮动(十七)
    HTML+CSS-Day11
    零代码工具推荐---HiFlow
    虚拟内存相关笔记
    集成学习、boosting、bagging、Adaboost、GBDT、随机森林
  • 原文地址:https://blog.csdn.net/weixin_69028524/article/details/134084044