• 华清远见(上海中心)


    日期:11月24日             

    一、题目

    1. .应用层写程序控制6盏灯的亮灭,要求使用GPIO子系统

    代码实现:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include "device.h"
    8. int main(int argc,const char * argv[])
    9. {
    10. int i;
    11. int fd[6]={0};
    12. char *p[6]={"/dev/myled0","/dev/myled1","/dev/myled2","/dev/myled3","/dev/myled4","/dev/myled5"};
    13. for(i=0;i<6;i++)
    14. {
    15. fd[i] = open(p[i],O_RDWR);
    16. if(-1 == fd[i])
    17. {
    18. printf("open is error\n");
    19. return -1;
    20. }
    21. }
    22. while(1)
    23. {
    24. ioctl(fd[0],LED0_ON);
    25. sleep(1);
    26. ioctl(fd[0],LED0_OFF);
    27. ioctl(fd[1],LED3_ON);
    28. sleep(1);
    29. ioctl(fd[1],LED3_OFF);
    30. ioctl(fd[2],LED5_ON);
    31. sleep(1);
    32. ioctl(fd[2],LED5_OFF);
    33. ioctl(fd[3],LED4_ON);
    34. sleep(1);
    35. ioctl(fd[3],LED4_OFF);
    36. ioctl(fd[4],LED2_ON);
    37. sleep(1);
    38. ioctl(fd[4],LED2_OFF);
    39. ioctl(fd[5],LED1_ON);
    40. sleep(1);
    41. ioctl(fd[5],LED1_OFF);
    42. }
    43. return 0;
    44. }
    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include "device.h"
    13. #define CNAME "myleds"
    14. struct device_node *node;
    15. struct gpio_desc *gpio[6];
    16. int ret,i;
    17. struct cdev *cdev;
    18. dev_t dev;
    19. int minor=0;
    20. int major;
    21. int count=6;
    22. struct device* device;
    23. struct class* cls;
    24. char *p[6]={"myled0","myled1","myled2","myled3","myled4","myled5"};
    25. int myc_open(struct inode *node, struct file *file)
    26. {
    27. return 0;
    28. }
    29. long myc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    30. {
    31. if(cmd == LED0_ON)
    32. {
    33. gpiod_set_value(gpio[0],1);
    34. }else if(cmd == LED0_OFF)
    35. {
    36. gpiod_set_value(gpio[0],0);
    37. }
    38. if(cmd == LED1_ON)
    39. {
    40. gpiod_set_value(gpio[1],1);
    41. }else if(cmd == LED1_OFF)
    42. {
    43. gpiod_set_value(gpio[1],0);
    44. }
    45. if(cmd == LED2_ON)
    46. {
    47. gpiod_set_value(gpio[2],1);
    48. }else if(cmd == LED2_OFF)
    49. {
    50. gpiod_set_value(gpio[2],0);
    51. }
    52. if(cmd == LED3_ON)
    53. {
    54. gpiod_set_value(gpio[3],1);
    55. }else if(cmd == LED3_OFF)
    56. {
    57. gpiod_set_value(gpio[3],0);
    58. }
    59. if(cmd == LED4_ON)
    60. {
    61. gpiod_set_value(gpio[4],1);
    62. }else if(cmd == LED4_OFF)
    63. {
    64. gpiod_set_value(gpio[4],0);
    65. }
    66. if(cmd == LED5_ON)
    67. {
    68. gpiod_set_value(gpio[5],1);
    69. }else if(cmd == LED5_OFF)
    70. {
    71. gpiod_set_value(gpio[5],0);
    72. }
    73. return 0;
    74. }
    75. int myc_close(struct inode *node, struct file *file)
    76. {
    77. return 0;
    78. }
    79. const struct file_operations fops = {
    80. .open = myc_open,
    81. .release = myc_close,
    82. .unlocked_ioctl = myc_ioctl,
    83. };
    84. static int __init mycdev_init(void)
    85. {
    86. //分配字符设备驱动
    87. cdev = cdev_alloc();
    88. if(cdev == NULL)
    89. {
    90. printk("内存分配失败\n");
    91. goto ERR1;
    92. }
    93. printk("内存分配成功\n");
    94. //初始化设备驱动
    95. cdev_init(cdev,&fops);
    96. //动态申请设备号
    97. ret = alloc_chrdev_region(&dev,minor,count,CNAME);
    98. if(ret<0)
    99. {
    100. printk("动态申请设备号失败\n");
    101. goto ERR2;
    102. }
    103. printk("动态申请设备号成功\n");
    104. //注册字符设备驱动
    105. ret = cdev_add(cdev,dev,count);
    106. if(ret<0)
    107. {
    108. printk("注册字符设备驱动失败\n");
    109. goto ERR3;
    110. }
    111. printk("注册字符设备驱动成功\n");
    112. //5.自动创建设备节点
    113. //向上层提交目录信息
    114. cls = class_create(THIS_MODULE,CNAME);
    115. if(IS_ERR(cls))
    116. {
    117. printk("class create is failed\n");
    118. ret = PTR_ERR(cls);
    119. goto ERR4;
    120. }
    121. //向上层提交设备节点
    122. //自动申请
    123. major = MAJOR(dev);
    124. for(i=0;i
    125. {
    126. device = device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
    127. if(IS_ERR(device))
    128. {
    129. printk("device create is error\n");
    130. ret = PTR_ERR(device);
    131. goto ERR5;
    132. }
    133. }
    134. node = of_find_node_by_path("/myleds");
    135. if(node == NULL)
    136. {
    137. printk("通过路径解析设备树节点失败\n");
    138. return -EFAULT;
    139. }
    140. printk("成功解析到设备树节点\n");
    141. for(i=0;i<6;i++)
    142. {
    143. gpio[i] = gpiod_get_from_of_node(node,p[i],0,GPIOD_OUT_LOW,NULL);
    144. if(IS_ERR(gpio[i]))
    145. {
    146. printk("gpio编号获取失败\n");
    147. return PTR_ERR(gpio[i]);
    148. }
    149. printk("gipo[%d]编号获取成功\n",i);
    150. //设置引脚为输出模式
    151. ret = gpiod_direction_output(gpio[i],0);
    152. if(ret<0)
    153. {
    154. printk("输出模式设置成功\n");
    155. return ret;
    156. }
    157. //初始化
    158. gpiod_set_value(gpio[i],0);
    159. }
    160. return 0;
    161. ERR5:
    162. for(--i;i>=0;i--)
    163. {
    164. device_destroy(cls,MKDEV(major,i));
    165. }
    166. class_destroy(cls);
    167. ERR4:
    168. cdev_del(cdev);
    169. ERR3:
    170. unregister_chrdev_region(dev,count);
    171. ERR2:
    172. kfree(cdev);
    173. ERR1:
    174. return -EIO;
    175. }
    176. static void __exit mycdev_exit(void)
    177. {
    178. for(i=0;i<6;i++)
    179. {
    180. gpiod_set_value(gpio[i],0);
    181. //释放
    182. gpiod_put(gpio[i]);
    183. }
    184. for(i=0;i
    185. {
    186. device_destroy(cls,MKDEV(major,i));
    187. }
    188. //2.销毁目录信息
    189. class_destroy(cls);
    190. //3.驱动的注销
    191. cdev_del(cdev);
    192. //4.销毁设备号
    193. unregister_chrdev_region(dev,count);
    194. //5.释放cdev结构体
    195. kfree(cdev);
    196. }
    197. module_init(mycdev_init);
    198. module_exit(mycdev_exit);
    199. MODULE_LICENSE("GPL");
    1. #ifndef __DEVICE_H__
    2. #define __DEVICE_H__
    3. #define LED0_ON _IOW('a',1,int)
    4. #define LED0_OFF _IOW('a',0,int)
    5. #define LED1_ON _IOW('a',1,int)
    6. #define LED1_OFF _IOW('a',0,int)
    7. #define LED2_ON _IOW('a',1,int)
    8. #define LED2_OFF _IOW('a',0,int)
    9. #define LED3_ON _IOW('a',1,int)
    10. #define LED3_OFF _IOW('a',0,int)
    11. #define LED4_ON _IOW('a',1,int)
    12. #define LED4_OFF _IOW('a',0,int)
    13. #define LED5_ON _IOW('a',1,int)
    14. #define LED5_OFF _IOW('a',0,int)
    15. #endif

    现象实现:

     

  • 相关阅读:
    华为浏览器,Chrome的平替,插件无缝连接
    罗素《为什么我不是基督徒》的后果
    Python每日一练 04
    互联网安全面临的全新挑战
    form-create表单生成器简单使用
    wxPython 4.2.0 发布
    卷积层计算量(FLOPS)和参数量的计算
    babili-webpack-plugin编译后的代码v+‘‘===1,会被压缩成字符串v1
    计算机网络数据链路层知识总结
    Leetcode1805-字符串中不同整数的数目
  • 原文地址:https://blog.csdn.net/qq_58538047/article/details/128025248