• 驱动开发-按键中断


    编写LED灯的驱动,使用GPIO子系统,里面添加按键的中断处理

    1.应用程序发送指令控制LED亮灭

    2.按键1 按下,led1电位反转 按键2按下,led2电位反转 按键3 按下,led3电位反转

    功能函数

    1. #include<stdlib.h>
    2. #include<stdio.h>
    3. #include <sys/types.h>
    4. #include <sys/stat.h>
    5. #include <fcntl.h>
    6. #include<unistd.h>
    7. #include<string.h>
    8. #include<sys/ioctl.h>
    9. //功能码
    10. #define LED_ON _IOW('l',1,int)
    11. #define LED_OFF _IOW('l',0,int)
    12. int main(int argc, char const *argv[])
    13. {
    14. char buf[128] = {0};
    15. int a,b;
    16. int fd;
    17. while (1)
    18. {
    19. printf("请输入要控制的灯:0(led1) 1(led2) 2(led3)\n");
    20. scanf("%d",&a);
    21. if(a == 0)
    22. {
    23. fd = open("/dev/myled0", O_RDWR);
    24. if (fd < 0)
    25. {
    26. printf("打开设备文件失败\n");
    27. exit(-1);
    28. }
    29. }
    30. else if(a == 1)
    31. {
    32. fd = open("/dev/myled1", O_RDWR);
    33. if (fd < 0)
    34. {
    35. printf("打开设备文件失败\n");
    36. exit(-1);
    37. }
    38. }
    39. else if(a == 2)
    40. {
    41. int fd = open("/dev/myled2", O_RDWR);
    42. if (fd < 0)
    43. {
    44. printf("打开设备文件失败\n");
    45. exit(-1);
    46. }
    47. }
    48. printf("请输入控制命令:0(关闭) 1(开灯)>");
    49. scanf("%d",&b);
    50. switch(b)
    51. {
    52. case 1:
    53. ioctl(fd,LED_ON);
    54. break;
    55. case 0:
    56. ioctl(fd,LED_OFF);
    57. break;
    58. }
    59. close(fd);
    60. }
    61. return 0;
    62. }

    驱动代码

    1. #include <linux/of_irq.h>
    2. #include <linux/interrupt.h>
    3. #include <linux/of_gpio.h>
    4. #include <linux/gpio.h>
    5. #include <linux/init.h>
    6. #include <linux/module.h>
    7. #include<linux/fs.h>
    8. #include<linux/io.h>
    9. #include<linux/device.h>
    10. //功能码
    11. #define LED_ON _IOW('l',1,int)
    12. #define LED_OFF _IOW('l',0,int)
    13. unsigned int major;//定义一个变量保存主设备号
    14. char kbuf[128]={0};
    15. struct class *cls;
    16. struct device*device;
    17. struct device_node *dev_led;
    18. struct device_node *dev;
    19. unsigned int irqno1,irqno2,irqno3;
    20. struct gpio_desc *gpiono1;
    21. struct gpio_desc *gpiono2;
    22. struct gpio_desc *gpiono3;
    23. /*
    24. myirq{
    25. interrupt-parent=<&gpiof>;//引用中断父节点
    26. interrupts=<9 0>,<7 0>,<8 0>;//声明和中断父节点的关系 9表示索引号,0表示默认设置
    27. };
    28. */
    29. //中断处理函数
    30. irqreturn_t myirq_handler1(int irq,void *dev)
    31. {
    32. gpiod_set_value(gpiono1, !gpiod_get_value(gpiono1)); // LED1
    33. return IRQ_HANDLED;
    34. }
    35. irqreturn_t myirq_handler2(int irq,void *dev)
    36. {
    37. gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2)); // LED2
    38. return IRQ_HANDLED;
    39. }
    40. irqreturn_t myirq_handler3(int irq,void *dev)
    41. {
    42. gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3)); // LED3
    43. return IRQ_HANDLED;
    44. }
    45. //封装操作方法
    46. int mycdev_open(struct inode *inode, struct file *file)
    47. {
    48. int a = inode->i_rdev;//获取当前设备文件对应的设备号
    49. file->private_data=(void*)MINOR(a);//将次设备号保存到当前文件的file结构中
    50. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    51. return 0;
    52. }
    53. long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    54. {
    55. unsigned int a=(unsigned int)file->private_data;
    56. switch(a)
    57. {
    58. case 0:
    59. if(cmd == LED_ON)
    60. gpiod_set_value(gpiono1,1);
    61. else
    62. gpiod_set_value(gpiono1,0);
    63. break;
    64. case 1:
    65. if(cmd == LED_ON)
    66. gpiod_set_value(gpiono2,1);
    67. else
    68. gpiod_set_value(gpiono2,0);
    69. break;
    70. case 2:
    71. if(cmd == LED_ON)
    72. gpiod_set_value(gpiono3,1);
    73. else
    74. gpiod_set_value(gpiono3,0);
    75. break;
    76. }
    77. return 0;
    78. }
    79. int mycdev_close(struct inode *inode, struct file *file)
    80. {
    81. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    82. return 0;
    83. }
    84. //定义一个操作方法结构体变量并且初始化
    85. struct file_operations fops={
    86. .open=mycdev_open,
    87. .release=mycdev_close,
    88. .unlocked_ioctl = mycdev_ioctl,
    89. };
    90. static int __init mycdev_init(void)
    91. {
    92. int i;
    93. // 字符设备驱动注册
    94. major = register_chrdev(0, "mychrdev", &fops);
    95. if (major < 0)
    96. {
    97. printk("字符设备驱动注册失败\n");
    98. return major;
    99. }
    100. printk("字符设备驱动注册成功:major=%d\n", major);
    101. // 向上提交目录
    102. cls = class_create(THIS_MODULE, "myled");
    103. if (IS_ERR(cls))
    104. {
    105. printk("向上提交目录失败\n");
    106. return -PTR_ERR(cls);
    107. }
    108. printk("向上提交目录信息成功\n");
    109. // 向上提交设备节点信息
    110. for (i = 0; i < 3; i++)
    111. {
    112. device = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
    113. if (IS_ERR(device))
    114. {
    115. printk("向上提交设备节点信息失败\n");
    116. return -PTR_ERR(device);
    117. }
    118. }
    119. printk("向上提交设备节点成功\n");
    120. int ret1,ret2,ret3;
    121. //解析按键的设备树节点
    122. dev = of_find_node_by_path("/myirq");
    123. if(dev==NULL)
    124. {
    125. printk("解析设备树节点失败\n");
    126. return -EFAULT;
    127. }
    128. printk("解析设备树节点成功\n");
    129. //根据设备树节点的路径解析设备树信息
    130. dev_led = of_find_node_by_path("/leds");
    131. if(dev_led==NULL)
    132. {
    133. printk("解析设备树信息失败\n");
    134. return -EFAULT;
    135. }
    136. printk("解析设备树信息成功\n");
    137. //根据设备树节点解析出软中断号
    138. irqno1 = irq_of_parse_and_map(dev,0);//按键1索引号为0
    139. irqno2 = irq_of_parse_and_map(dev,1);//按键2索引号为1
    140. irqno3 = irq_of_parse_and_map(dev,2);//按键3索引号为2
    141. if(!irqno1|!irqno1|!irqno2)
    142. {
    143. printk("解析软中断号失败\n");
    144. return -ENXIO;
    145. }
    146. printk("解析软中断号成功 irqno=%d %d %d\n",irqno1,irqno2,irqno3);
    147. //注册中断
    148. ret1 = request_irq(irqno1,myirq_handler1,IRQF_TRIGGER_FALLING,"key1",NULL);
    149. ret2 = request_irq(irqno2,myirq_handler2,IRQF_TRIGGER_FALLING,"key2",NULL);
    150. ret3 = request_irq(irqno3,myirq_handler3,IRQF_TRIGGER_FALLING,"key3",NULL);
    151. if(ret1|ret2|ret3)
    152. {
    153. printk("注册中断失败\n");
    154. return -EFAULT;
    155. }
    156. printk("注册中断成功\n");
    157. //根据解析到的设备树信息解析出led的gpio编号
    158. // 申请gpio_desc对象并设置输出为低电平
    159. gpiono1 = gpiod_get_from_of_node(dev_led, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);
    160. if (IS_ERR(gpiono1))
    161. {
    162. printk("申请gpio1对象失败\n");
    163. return -PTR_ERR(gpiono1);
    164. }
    165. printk("申请gpio1对象成功\n");
    166. gpiono2 = gpiod_get_from_of_node(dev_led, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);
    167. if (IS_ERR(gpiono2))
    168. {
    169. printk("申请gpio2对象失败\n");
    170. return -PTR_ERR(gpiono2);
    171. }
    172. printk("申请gpio2对象成功\n");
    173. gpiono3 = gpiod_get_from_of_node(dev_led, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);
    174. if (IS_ERR(gpiono3))
    175. {
    176. printk("申请gpio对象失败\n");
    177. return -PTR_ERR(gpiono3);
    178. }
    179. printk("申请gpio3对象成功\n");
    180. return 0;
    181. }
    182. static void __exit mycdev_exit(void)
    183. {
    184. //销毁设备节点信息
    185. int i;
    186. for(i=0;i<3;i++)
    187. {
    188. device_destroy(cls,MKDEV(major,i));
    189. }
    190. //销毁目录信息
    191. class_destroy(cls);
    192. //注销字符设备驱动
    193. unregister_chrdev(major,"mychrdev");
    194. //注销中断
    195. free_irq(irqno1,NULL);
    196. free_irq(irqno2,NULL);
    197. free_irq(irqno3,NULL);
    198. // 灭灯
    199. gpiod_set_value(gpiono1, 0);
    200. // 释放gpio编号
    201. gpiod_put(gpiono1);
    202. gpiod_set_value(gpiono2, 0);
    203. gpiod_put(gpiono2);
    204. gpiod_set_value(gpiono3, 0);
    205. gpiod_put(gpiono3);
    206. }
    207. module_init(mycdev_init);
    208. module_exit(mycdev_exit);
    209. MODULE_LICENSE("GPL");
  • 相关阅读:
    TCPIP网络编程基础知识
    解决OpenSSL加入到在Visual Studio 2019中编译返回LNK2019错误
    介绍HTTP
    简单学习OSM(OpenStreetMap)文件格式的最基础结构
    FIX - 克隆虚拟机NAT模式网络不通、不稳定、vMnet8网络故障、网卡冲突、ssh连接慢
    2022年高教社杯数学建模竞赛A题 B题 C题 D题 E题思路
    python刷题笔记1(42例题)
    LaTeX:在标题section中添加脚注footnote
    计算机毕业设计Java大连环保公益网(源码+系统+mysql数据库+lw文档)
    C++11特性——类型推导
  • 原文地址:https://blog.csdn.net/qq_45418048/article/details/132054205