• 2023/10/30-LED灯驱动开发


     k1.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 "head.h"
    7. char kbuf[128] = {};
    8. unsigned int major;
    9. //定义三个指针指向映射后的虚拟内存
    10. unsigned int *vir_rcc;
    11. unsigned int *vir_moder_led1;
    12. unsigned int *vir_odr_led1;
    13. unsigned int *vir_moder_led2;
    14. unsigned int *vir_odr_led2;
    15. unsigned int *vir_moder_led3;
    16. unsigned int *vir_odr_led3;
    17. // 封装操作方法
    18. int mycdev_open(struct inode *inode, struct file *file)
    19. {
    20. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    21. return 0;
    22. }
    23. ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
    24. {
    25. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    26. int ret=0;
    27. ret = copy_to_user(ubuf,kbuf,size);
    28. if(ret = 0)
    29. {
    30. printk("copy_to_user 内核->用户 filed!\n");
    31. return -EIO;
    32. }
    33. //printk("copy_to_user 内核->用户 success!,ubuf=%s\n",ubuf);
    34. printk("copy_to_user 内核->用户 success!\n");
    35. return 0;
    36. }
    37. ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
    38. {
    39. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    40. int ret=0;
    41. ret = copy_from_user(kbuf,ubuf,size);
    42. if(ret = 0)
    43. {
    44. printk("copy_from_user 用户->内核 filed!\n");
    45. return -EIO;
    46. }
    47. //printk("copy_from_user 用户->内核 success!,ubuf=%s\n",ubuf);
    48. printk("copy_from_user 用户->内核 success!\n");
    49. if(kbuf[0] == '0') //关灯
    50. {
    51. (*vir_odr_led1) &= (~(0x1 << 10));
    52. (*vir_odr_led2) &= (~(0x1 << 10));
    53. (*vir_odr_led3) &= (~(0x1 << 8));
    54. }
    55. else if(kbuf[0] == '1') //开灯
    56. {
    57. (*vir_odr_led1) |= (0x1 << 10);
    58. (*vir_odr_led2) |= (0x1 << 10);
    59. (*vir_odr_led3) |= (0x1 << 8);
    60. }
    61. return 0;
    62. }
    63. int mycdev_close(struct inode *inode, struct file *file)
    64. {
    65. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    66. return 0;
    67. }
    68. //定义操作方法结构体对象
    69. struct file_operations fops={
    70. .open=mycdev_open,
    71. .read=mycdev_read,
    72. .write=mycdev_write,
    73. .release=mycdev_close,
    74. };
    75. static int __init mycdev_init(void)
    76. {
    77. //注册字符设备驱动
    78. major=register_chrdev(0,"mychrdev",&fops);
    79. if (major < 0)
    80. {
    81. printk("字符设备驱动注册失败\n");
    82. return major;
    83. }
    84. printk("注册字符设备驱动成功major=%d\n", major);
    85. //进行寄存器的地址映射
    86. vir_rcc = ioremap(PHY_RCC,4);
    87. if(vir_rcc == NULL)
    88. {
    89. printk("物理内存映射filed! %d\n",__LINE__);
    90. return -EFAULT;
    91. }
    92. vir_moder_led1 = ioremap(PHY_LED1_MODER,4);
    93. if(vir_moder_led1 == NULL)
    94. {
    95. printk("物理内存映射filed! %d\n",__LINE__);
    96. return -EFAULT;
    97. }
    98. vir_odr_led1 = ioremap(PHY_LED1_ODR,4);
    99. if(vir_odr_led1 == NULL)
    100. {
    101. printk("物理内存映射filed! %d\n",__LINE__);
    102. return -EFAULT;
    103. }
    104. vir_moder_led2 = ioremap(PHY_LED2_MODER,4);
    105. if(vir_moder_led2 == NULL)
    106. {
    107. printk("物理内存映射filed! %d\n",__LINE__);
    108. return -EFAULT;
    109. }
    110. vir_odr_led2 = ioremap(PHY_LED2_ODR,4);
    111. if(vir_odr_led2 == NULL)
    112. {
    113. printk("物理内存映射filed! %d\n",__LINE__);
    114. return -EFAULT;
    115. }
    116. vir_moder_led3 = ioremap(PHY_LED3_MODER,4);
    117. if(vir_moder_led3 == NULL)
    118. {
    119. printk("物理内存映射filed! %d\n",__LINE__);
    120. return -EFAULT;
    121. }
    122. vir_odr_led3 = ioremap(PHY_LED3_ODR,4);
    123. if(vir_odr_led3 == NULL)
    124. {
    125. printk("物理内存映射filed! %d\n",__LINE__);
    126. return -EFAULT;
    127. }
    128. printk("物理内存映射成功!\n");
    129. //LED1寄存器初始化
    130. (*vir_rcc) |= (0x3 << 4); //GPIOE控制器时钟使能
    131. (*vir_moder_led1) &= (~(0x3 << 20)); //MODER[21:20] => 00
    132. (*vir_moder_led1) |= (0x1 << 20); //MODER[21:20] => 01
    133. (*vir_odr_led1) &= (~(0x1 << 10)); //默认为关灯
    134. (*vir_moder_led2) &= (~(0x3 << 20)); //MODER[21:20] => 00
    135. (*vir_moder_led2) |= (0x1 << 20); //MODER[21:20] => 01
    136. (*vir_odr_led2) &= (~(0x1 << 10)); //默认为关灯
    137. (*vir_moder_led3) &= (~(0x3 << 16)); //MODER[17:16] => 00
    138. (*vir_moder_led3) |= (0x1 << 16); //MODER[17:16] => 01
    139. (*vir_odr_led3) &= (~(0x1 << 8)); //默认为关灯
    140. return 0;
    141. }
    142. static void __exit mycdev_exit(void)
    143. {
    144. //取消内存映射
    145. iounmap(vir_rcc);
    146. iounmap(vir_moder_led1);
    147. iounmap(vir_odr_led1);
    148. iounmap(vir_moder_led2);
    149. iounmap(vir_odr_led2);
    150. iounmap(vir_moder_led3);
    151. iounmap(vir_odr_led3);
    152. //注销字符设备驱动
    153. unregister_chrdev(major,"mychrdev");
    154. }
    155. module_init(mycdev_init);
    156. module_exit(mycdev_exit);
    157. MODULE_LICENSE("GPL");

    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. int main(int argc,const char * argv[])
    8. {
    9. char buf[128] = {0};
    10. int fd = open("/dev/mychrdev",O_RDWR);
    11. if(fd < 0)
    12. {
    13. printf("文件/dev/mychrdev打开失败\n");
    14. return -1;
    15. }
    16. printf("文件/dev/mychrdev打开成功\n");
    17. while(1)
    18. {
    19. printf("输入要进行的操作=> 0(关灯) <-> 1(开灯) : ");
    20. fgets(buf,sizeof(buf),stdin);
    21. buf[strlen(buf)-1] = '\0';
    22. //User -> 内核
    23. write(fd,buf,sizeof(buf));
    24. }
    25. /*
    26. //User -> 内核
    27. write(fd,buf,sizeof(buf));
    28. //内核 -> User
    29. read(fd,buf,sizeof(buf));
    30. */
    31. close(fd);
    32. return 0;
    33. }

    head.h

    1. #ifndef __HEAD_H__
    2. #define __HEAD_H__
    3. #define PHY_RCC 0x50000A28
    4. #define PHY_LED1_MODER 0x50006000
    5. #define PHY_LED1_ODR 0x50006014
    6. #define PHY_LED2_MODER 0x50007000
    7. #define PHY_LED2_ODR 0x50007014
    8. #define PHY_LED3_MODER 0x50006000
    9. #define PHY_LED3_ODR 0x50006014
    10. #endif

  • 相关阅读:
    [python刷题模板] 珂朵莉树 ODT (基于支持随机访问的跳表
    企业电子招标采购系统源码Spring Cloud + 前后端分离 + 二次开发
    企业搭建网站用哪种服务器
    rocketmq总结
    综合项目实战--jenkins节点模式
    【刷题记录15】Java工程师丨腾讯面试真题(3)
    SpringBoot属性注入
    《视觉SLAM十四讲》-- 后端 2
    Git详细介绍 -入门到实战万字篇(上)
    Element-ui 标签页el-radio-button左边框不显示问题
  • 原文地址:https://blog.csdn.net/ainitjq1314/article/details/133953669