• 驱动 10月23日 作业


    通过字符设备驱动的分步实现编写LED驱动,另外实现设备文件和设备的绑定

    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 <string.h>
    7. #include <stdlib.h>
    8. #include <sys/ioctl.h>
    9. #include "head.h"
    10. int main(int argc, char const *argv[])
    11. {
    12. char buf[128];
    13. int a;
    14. char b[10];
    15. //char str[100] = "/dev/mycdev0";
    16. // printf("0(led1) 1(led2) 2(led3)\n");
    17. while (1)
    18. {
    19. strcpy(buf, "/dev/mychrdev");
    20. printf("0(led1) 1(led2) 2(led3)\n");
    21. scanf("%s", b);
    22. strcat(buf, b);
    23. //printf("%s\n", str);
    24. int fd = open(buf, O_RDWR);
    25. printf("%d\n", fd);
    26. if (fd < 0)
    27. {
    28. printf("打开设备文件失败\n");
    29. exit(-1);
    30. }
    31. printf("打开设备文件成功\n");
    32. //从终端读取
    33. printf("请输入要实现的功能\n");
    34. printf("0(关灯)1(开灯)\n");
    35. printf("请输入> ");
    36. scanf("%d", &a);
    37. switch (a)
    38. {
    39. case 1:
    40. ioctl(fd, LED_ON);
    41. break;
    42. case 0:
    43. ioctl(fd, LED_OFF);
    44. break;
    45. }
    46. //write(fd, buf, sizeof(buf));//将数据传递给内核
    47. // memset(buf, 0, sizeof(buf));//清空数组
    48. // read(fd, buf, sizeof(buf));//将内核空间数据传递到用户
    49. // printf("buf:%s\n", buf);
    50. close(fd);
    51. bzero(buf, sizeof(buf));
    52. }
    53. return 0;
    54. }

    mycdev.c

    1. #include <linux/init.h>
    2. #include <linux/module.h>
    3. #include <linux/fs.h>
    4. #include <linux/uaccess.h>
    5. #include <linux/io.h>
    6. #include <linux/device.h>
    7. #include <linux/cdev.h>
    8. #include <linux/slab.h>
    9. #include "head.h"
    10. struct cdev *cdev;
    11. unsigned int major = 0;
    12. unsigned int minor = 0;
    13. struct class *cls;
    14. dev_t devno;
    15. struct device *dev;
    16. // 定义三个灯指针指向映射后的虚拟内存
    17. gpio_t *vir_led1;
    18. gpio_t *vir_led2;
    19. gpio_t *vir_led3;
    20. unsigned int *vir_rcc;
    21. // 封装操作方法
    22. int mycdev_open(struct inode *inode, struct file *file)
    23. {
    24. int min = MINOR(inode->i_rdev); //获取打开的文件的次设备号
    25. file->private_data = (void *)min;
    26. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    27. return 0;
    28. }
    29. long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    30. {
    31. int min = (int)file->private_data; //获取到文件的次设备号
    32. switch (min)
    33. {
    34. case 0: //操作LED1
    35. switch (cmd)
    36. {
    37. case LED_ON: //开灯
    38. vir_led1->ODR |= (0x1 << 10);
    39. break;
    40. case LED_OFF: //关灯
    41. vir_led1->ODR &= (~(0x1 << 10));
    42. break;
    43. }
    44. break;
    45. case 1: //操作LED2
    46. switch (cmd)
    47. {
    48. case LED_ON: //开灯
    49. vir_led2->ODR |= (0x1 << 10);
    50. break;
    51. case LED_OFF: //关灯
    52. vir_led2->ODR &= (~(0x1 << 10));
    53. break;
    54. }
    55. break;
    56. case 2: //操作LED3
    57. switch (cmd)
    58. {
    59. case LED_ON: //开灯
    60. vir_led3->ODR |= (0x1 << 8);
    61. break;
    62. case LED_OFF: //关灯
    63. vir_led3->ODR &= (~(0x1 << 8));
    64. break;
    65. }
    66. break;
    67. }
    68. return 0;
    69. }
    70. int mycdev_close(struct inode *inode, struct file *file)
    71. {
    72. printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    73. return 0;
    74. }
    75. // 定义操作方法结构体对象
    76. struct file_operations fops = {
    77. .open = mycdev_open,
    78. .read = mycdev_read,
    79. .write = mycdev_write,
    80. .unlocked_ioctl = mycdev_ioctl,
    81. .release = mycdev_close,
    82. };
    83. int all_led_init(void)
    84. {
    85. // 进行寄存器的地址映射
    86. vir_led1 = ioremap(PHY_LED1_ADDR, sizeof(gpio_t));
    87. if (vir_led1 == NULL)
    88. {
    89. printk("物理内存地址映射失败 %d\n", __LINE__);
    90. return -ENOMEM;
    91. }
    92. vir_led2 = ioremap(PHY_LED2_ADDR, sizeof(gpio_t));
    93. if (vir_led1 == NULL)
    94. {
    95. printk("物理内存地址映射失败 %d\n", __LINE__);
    96. return -ENOMEM;
    97. }
    98. vir_led3 = ioremap(PHY_LED3_ADDR, sizeof(gpio_t));
    99. if (vir_led1 == NULL)
    100. {
    101. printk("物理内存地址映射失败 %d\n", __LINE__);
    102. return -ENOMEM;
    103. }
    104. vir_rcc = ioremap(PHY_RCC_ADDR, 4);
    105. if (vir_rcc == NULL)
    106. {
    107. printk("物理内存地址映射失败 %d\n", __LINE__);
    108. return -ENOMEM;
    109. }
    110. printk("物理地址映射成功\n");
    111. (*vir_rcc) |= (0x3 << 4); // GPIOE、F控制器时钟使能
    112. // LED1寄存器初始化
    113. vir_led1->MODER &= (~(0x3 << 20)); // MODER[21:20] -> 20
    114. vir_led1->MODER |= (0x1 << 20); // MODER[21:20] -> 01
    115. vir_led1->ODR &= (~(0x1 << 10)); // 默认关灯
    116. // LED2寄存器初始化
    117. vir_led2->MODER &= (~(0x3 << 20)); // MODER[21:20] -> 20
    118. vir_led2->MODER |= (0x1 << 20); // MODER[21:20] -> 01
    119. vir_led2->ODR &= (~(0x1 << 10)); // 默认关灯
    120. // LED3寄存器初始化
    121. vir_led3->MODER &= (~(0x3 << 16)); // MODER[21:20] -> 20
    122. vir_led3->MODER |= (0x1 << 16); // MODER[21:20] -> 01
    123. vir_led3->ODR &= (~(0x1 << 8)); // 默认关灯
    124. printk("寄存器初始化成功\n");
    125. return 0;
    126. }
    127. // 入口函数,安装内核模块时执行
    128. static int __init mycdev_init(void)
    129. {
    130. int ret, i;
    131. //1.申请一个对象空间cdev_alloc
    132. cdev = cdev_alloc();
    133. if (cdev == NULL)
    134. {
    135. printk("申请字符设备驱动对象失败 %d \n", __LINE__);
    136. ret = -EFAULT;
    137. goto out1;
    138. }
    139. printk("字符设备驱动对象申请成功\n");
    140. //2.初始化对象cdev_init
    141. cdev_init(cdev, &fops);
    142. //3.申请设备号 register_chrdev_region()/alloc_chrdev_region()
    143. if (major == 0) //动态申请
    144. {
    145. ret = alloc_chrdev_region(&devno, minor, 3, "mychrdev");
    146. if (ret)
    147. {
    148. printk("动态申请设备号失败 %d\n", __LINE__);
    149. goto out2;
    150. }
    151. major = MAJOR(devno); //根据设备号获取主设备号
    152. minor = MINOR(devno); //根据设备号获取次设备号
    153. }
    154. else
    155. {
    156. ret = register_chrdev_region(MKDEV(major, minor), 3, "mychrdev");
    157. if (ret)
    158. {
    159. printk("静态指定设备号失败\n");
    160. goto out2;
    161. }
    162. }
    163. printk("设备号申请成功\n");
    164. //4.注册驱动对象 cdev_add
    165. ret = cdev_add(cdev, MKDEV(major, minor), 3);
    166. if (ret != 0)
    167. {
    168. printk("注册字符设备驱动对象失败 %d\n", __LINE__);
    169. goto out3;
    170. }
    171. printk("注册字符设备驱动对象成功\n");
    172. //5.向上提交目录 class_create
    173. cls = class_create(THIS_MODULE, "mychrdev");
    174. if (IS_ERR(cls))
    175. {
    176. printk("向上提交目录失败 %d\n", __LINE__);
    177. goto out4;
    178. }
    179. printk("向上提交目录成功\n");
    180. //6.向上提交设备节点信息 device_create
    181. for (i = 0; i < 3; i++)
    182. {
    183. dev = device_create(cls, NULL, MKDEV(major, i), NULL, "mychrdev%d", i);
    184. if (IS_ERR(dev))
    185. {
    186. printk("向上提交设备节点失败 %d\n", __LINE__);
    187. goto out5;
    188. }
    189. }
    190. printk("向上提交设备节点成功\n");
    191. all_led_init();
    192. return 0;
    193. out5:
    194. //将提交成功的节点信息释放
    195. for(--i; i >= 0; i--)
    196. {
    197. device_destroy(cls, MKDEV(major, i));
    198. }
    199. //销毁目录
    200. class_destroy(cls);
    201. out4:
    202. cdev_del(cdev);
    203. out3:
    204. unregister_chrdev_region(MKDEV(major, minor), 3);
    205. out2:
    206. kfree(cdev);
    207. out1:
    208. return ret;
    209. }
    210. // 出口函数,卸载内核模块时执行
    211. static void __exit mycdev_exit(void)
    212. {
    213. //取消地址映射
    214. iounmap(vir_led1);
    215. iounmap(vir_led2);
    216. iounmap(vir_rcc);
    217. //1.销毁设备节点信息
    218. int i;
    219. for (i = 0; i < 3; i++)
    220. {
    221. device_destroy(cls, MKDEV(major, i));
    222. }
    223. //2.销毁目录
    224. class_destroy(cls);
    225. //3.注销字符设备驱动对象
    226. cdev_del(cdev);
    227. //4.释放设备号
    228. unregister_chrdev_region(MKDEV(major, minor), 3);
    229. //5.释放申请到的字符设备驱动对象空间
    230. kfree(cdev);
    231. }
    232. // 用于声明入口函数
    233. module_init(mycdev_init);
    234. // 用于声明出口函数
    235. module_exit(mycdev_exit);
    236. // 声明当前内核模块遵循GPL协议
    237. MODULE_LICENSE("GPL");

  • 相关阅读:
    基于划分的聚类分析——K-means(机器学习)
    java-net-php-python-ssm高校学生学业分析及预警系统查重PPT计算机毕业设计程序
    irun和vcs工具,检测TB环境零延时无限循环以及zero-delay组合逻辑
    python执行cmd命令——控制电脑连接wifi——程序打包
    Vue3最佳实践 第六章 Pinia,Vuex与axios,VueUse 3(VueUse )
    有效管理token,充分发挥ChatGPT的能力
    ffplay使用dxva2实现硬解渲染
    基于SpringBoot+MyBatis 五子棋双人对战
    mysql忘记密码 -linux -centos
    40道JAVA经典算法面试题(答案)
  • 原文地址:https://blog.csdn.net/yuyanshang/article/details/133997854