• GPIO子系统编写LED驱动


    text.c

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. int main(int argc, char const *argv[])
    8. {
    9. char buf[128]={0};
    10. int fd=open("/dev/myleds",O_RDWR);
    11. if(fd<0)
    12. {
    13. printf("打开设备文件失败\n");
    14. exit(-1);
    15. }
    16. //在终端输入数据
    17. while(1)
    18. {
    19. printf("请输入:0(led1关灯)1(led1开灯)2(led2开灯)3(led2关灯)4(led3开灯)5(led3开灯)>>>");
    20. fgets(buf,sizeof(buf),stdin);
    21. buf[strlen(buf)-1]='\0';
    22. write(fd,buf,sizeof(buf));
    23. }
    24. close(fd);
    25. return 0;
    26. }

    驱动代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. /* myleds{
    11. led1=<&gpioe 10 0>;
    12. led2=<&gpiof 10 0>;
    13. led3=<&gpioe 8 0>;
    14. };*/
    15. int major;
    16. int gpiono;
    17. int gpiono2;
    18. int gpiono3;
    19. char kbuf[128] = {0};
    20. struct class *cls;
    21. struct device *dev;
    22. struct device_node *dnode;
    23. int mycdev_open(struct inode *inode,struct file *file)
    24. {
    25. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    26. return 0;
    27. }
    28. ssize_t mycdev_read(struct file *file, char *ubuf,size_t size, loff_t *iof)
    29. {
    30. int ret;
    31. //将进程2写到内核的数据传递给进程1
    32. if(size>sizeof(kbuf))
    33. size=sizeof(kbuf);
    34. ret=copy_to_user(ubuf,kbuf,size);
    35. if(ret)
    36. {
    37. printk("copy to user failed\n");
    38. return -EIO;
    39. }
    40. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    41. return 0;
    42. }
    43. ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *off)
    44. {
    45. int ret;
    46. if(size>sizeof(kbuf))
    47. size=sizeof(kbuf);
    48. ret=copy_from_user(kbuf,ubuf,size);
    49. if(ret)
    50. {
    51. printk("copy from user failed\n");
    52. return -EIO;
    53. }
    54. if(kbuf[0]=='1')
    55. {
    56. //开灯
    57. gpio_set_value(gpiono,1);
    58. }
    59. else if(kbuf[0]=='0')
    60. {
    61. //关灯
    62. gpio_set_value(gpiono,0);
    63. }
    64. else if(kbuf[0]=='2')
    65. {
    66. gpio_set_value(gpiono2,1);
    67. }
    68. else if(kbuf[0]=='3')
    69. {
    70. //关灯
    71. gpio_set_value(gpiono2,0);
    72. }
    73. else if(kbuf[0]=='4')
    74. {
    75. gpio_set_value(gpiono3,1);
    76. }
    77. else if(kbuf[0]=='5')
    78. {
    79. gpio_set_value(gpiono3,0);
    80. }
    81. return 0;
    82. }
    83. int mycdev_close(struct inode *inode,struct file *file)
    84. {
    85. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    86. return 0;
    87. }
    88. //定义操作方法结构变量并初始化
    89. struct file_operations fops=
    90. {
    91. .open=mycdev_open,
    92. .read=mycdev_read,
    93. .write=mycdev_write,
    94. .release=mycdev_close,
    95. };
    96. //入口函数
    97. static int __init mycdev_init(void)
    98. {
    99. int ret;
    100. //进行字符设备驱动的注册
    101. major=register_chrdev(0,"myleds",&fops);
    102. if(major<0)
    103. {
    104. printk("字符设备驱动注册失败\n");
    105. return major;
    106. }
    107. printk("字符设备驱动注册成功,major=%d\n",major);
    108. //向上提交目lu
    109. cls=class_create(THIS_MODULE,"myleds");
    110. if(IS_ERR(cls))
    111. {
    112. printk("向上提交目錄失敗\n");
    113. return -PTR_ERR(cls);
    114. }
    115. printk("向上提交目录成功\n");
    116. //向上提交设备节点信息
    117. dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myleds");
    118. if(IS_ERR(dev))
    119. {
    120. printk("向上提交设备节点失败\n");
    121. return -PTR_ERR(dev);
    122. }
    123. printk("向上提交设备节点成功\n");
    124. return 0;
    125. // 解析设备树节点
    126. dnode = of_find_node_by_name(NULL, "myleds");
    127. if (dnode == NULL)
    128. {
    129. printk("解析设备树节点失败\n");
    130. return -ENOMEM;
    131. }
    132. printk("解析设备树节点成功\n");
    133. // 根据设备树节点解析出gpio编号--led1
    134. gpiono = of_get_named_gpio(dnode, "led1", 0);
    135. if (gpiono < 0)
    136. {
    137. printk("gpio解析设备号失败\n");
    138. return -EIO;
    139. }
    140. // 申请gpio编号
    141. ret = gpio_request(gpiono, NULL);
    142. if (ret)
    143. {
    144. printk("GPIO编号申请失败\n");
    145. return -EIO;
    146. }
    147. printk("申请gpio编号成功%d\n", gpiono);
    148. gpiono2 = of_get_named_gpio(dnode, "led2", 0);
    149. if (gpiono2 < 0)
    150. {
    151. printk("gpio2解析设备号失败\n");
    152. return -EIO;
    153. }
    154. ret=gpio_request(gpiono2,NULL);
    155. if(ret)
    156. {
    157. printk("gpio2编号申请失败\n");
    158. return ret;
    159. }
    160. gpiono3 = of_get_named_gpio(dnode, "led3", 0);
    161. if (gpiono3 < 0)
    162. {
    163. printk("gpio3解析设备号失败\n");
    164. return -EIO;
    165. }
    166. ret=gpio_request(gpiono3,NULL);
    167. if(ret)
    168. {
    169. printk("gpio3编号申请失败\n");
    170. return ret;
    171. }
    172. printk("gpio3编号申请成功\n");
    173. // 设置管脚为输出
    174. gpio_direction_output(gpiono, 0);
    175. gpio_direction_output(gpiono2, 0);
    176. gpio_direction_output(gpiono3, 0);
    177. // 开灯
    178. //gpio_set_value(gpiono, 1);
    179. return 0;
    180. }
    181. static void __exit mycdev_exit(void)
    182. {
    183. // 灭灯
    184. gpio_set_value(gpiono, 0);
    185. gpio_set_value(gpiono2, 0);
    186. gpio_set_value(gpiono3, 0);
    187. // 释放gpio编号
    188. gpio_free(gpiono);
    189. gpio_free(gpiono2);
    190. gpio_free(gpiono3);
    191. //销毁设备节点信息
    192. device_destroy(cls,MKDEV(major,0));
    193. //销毁目录空间
    194. class_destroy(cls);
    195. //字符设备驱动的注销
    196. unregister_chrdev(major,"myleds");
    197. }
    198. module_init(mycdev_init);
    199. module_exit(mycdev_exit);
    200. MODULE_LICENSE("GPL");
    201. linux@linux:~/qudong/day2/

  • 相关阅读:
    肖sir__设计测试用例方法之因果图07_(黑盒测试)
    【云原生】SpringCloud-Spring Boot Starter使用测试
    鸿蒙 游戏来了 鸿蒙版 五子棋来了 我不允许你不会
    springboot Logback 不同环境,配置不同的日志输出路径
    争议不断的AI绘画,如今成为了顶流?
    如何在lnmp中实现PHP多版本共存
    USB Audio Class (UAC)音频解读规范
    Flameshot源码及分析3 —— gui函数解析
    Google单元测试sample分析(一)
    window屏幕录制
  • 原文地址:https://blog.csdn.net/weixin_57603834/article/details/134087905