• 驱动开发课程LED点亮


    head.h

    1. #ifndef __HEAD_H__
    2. #define __HEAD_H__
    3. #define PHY_LED1_MODER 0x50006000
    4. #define PHY_LED1_ODR 0x50006014
    5. #define PHY_RCC 0x50000A28
    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

    test.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/mychrdev",O_RDWR);
    11. if(fd<0)
    12. {
    13. printf("打开设备文件失败\n");
    14. return -1;
    15. }
    16. printf("打开设备文件成功\n");
    17. while(1)
    18. {
    19. printf("请输入要进行的操作:0(关灯) 1(开灯)>");
    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. }

    demo.c

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include "head.h"
    7. unsigned int major;
    8. char kbuf[128]={};
    9. unsigned int *vir_led1_moder;
    10. unsigned int *vir_led1_odr;
    11. unsigned int *vir_led2_moder;
    12. unsigned int *vir_led2_odr;
    13. unsigned int *vir_led3_moder;
    14. unsigned int *vir_led3_odr;
    15. unsigned int *vir_rcc;
    16. int mycdev_open(struct inode *inode, struct file *file)
    17. {
    18. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    19. return 0;
    20. }
    21. ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof)
    22. {
    23. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    24. int ret;
    25. ret=copy_to_user(ubuf,kbuf,size);
    26. if(ret)
    27. {
    28. printk("copy_to_user filed\n");
    29. return -EIO;
    30. }
    31. return 0;
    32. }
    33. ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *lof)
    34. {
    35. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    36. int ret;
    37. ret=copy_from_user(kbuf,ubuf,size);
    38. if(ret)
    39. {
    40. printk("copy_from_user filed\n");
    41. return -EIO;
    42. }
    43. if(kbuf[0] == '1')
    44. {
    45. if(kbuf[1] == '0')
    46. {
    47. (*vir_led1_odr) &= (~(0x1<<10));
    48. }
    49. else if(kbuf[1]=='1')
    50. {
    51. (*vir_led1_odr) |= (0x1<<10);
    52. }
    53. }
    54. else if(kbuf[0]=='2')
    55. {
    56. if(kbuf[1] == '0')
    57. {
    58. (*vir_led2_odr) &= (~(0x1<<10));
    59. }
    60. else if(kbuf[1]=='1')
    61. {
    62. (*vir_led2_odr) |= (0x1<<10);
    63. }
    64. }
    65. else if(kbuf[0]=='3')
    66. {
    67. if(kbuf[1] == '0')
    68. {
    69. (*vir_led3_odr) &= (~(0x1<<8));
    70. }
    71. else if(kbuf[1]=='1')
    72. {
    73. (*vir_led3_odr) |= (0x1<<8);
    74. }
    75. }
    76. return 0;
    77. }
    78. int mycdev_close(struct inode *inode, struct file *file)
    79. {
    80. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    81. return 0;
    82. }
    83. struct file_operations fops =
    84. {
    85. .open=mycdev_open,
    86. .read=mycdev_read,
    87. .write=mycdev_write,
    88. .release=mycdev_close,
    89. };
    90. static int __init mycdev_init(void)
    91. {
    92. major = register_chrdev(0,"mychrdev",&fops);
    93. if(major<0)
    94. {
    95. printk("字符设备驱动注册失败\n");
    96. return major;
    97. }
    98. printk("注册字符设备驱动成功major=%d\n",major);
    99. vir_led1_moder = ioremap(PHY_LED1_MODER,4);
    100. if(vir_led1_moder == NULL)
    101. {
    102. printk("物理内存地址映射失败%d\n",__LINE__);
    103. return -EFAULT;
    104. }
    105. vir_led1_odr = ioremap(PHY_LED1_ODR,4);
    106. if(vir_led1_odr == NULL)
    107. {
    108. printk("物理内存地址映射失败%d\n",__LINE__);
    109. return -EFAULT;
    110. }
    111. vir_led2_moder = ioremap(PHY_LED2_MODER,4);
    112. if(vir_led2_moder == NULL)
    113. {
    114. printk("物理内存地址映射失败%d\n",__LINE__);
    115. return -EFAULT;
    116. }
    117. vir_led2_odr = ioremap(PHY_LED2_ODR,4);
    118. if(vir_led2_odr == NULL)
    119. {
    120. printk("物理内存地址映射失败%d\n",__LINE__);
    121. return -EFAULT;
    122. }
    123. vir_led3_moder = ioremap(PHY_LED3_MODER,4);
    124. if(vir_led3_moder == NULL)
    125. {
    126. printk("物理内存地址映射失败%d\n",__LINE__);
    127. return -EFAULT;
    128. }
    129. vir_led3_odr = ioremap(PHY_LED3_ODR,4);
    130. if(vir_led3_odr == NULL)
    131. {
    132. printk("物理内存地址映射失败%d\n",__LINE__);
    133. return -EFAULT;
    134. }
    135. vir_rcc = ioremap(PHY_RCC,4);
    136. if(vir_rcc == NULL)
    137. {
    138. printk("物理内存地址映射失败%d\n",__LINE__);
    139. return -EFAULT;
    140. }
    141. printk("寄存器内存映射成功\n");
    142. (*vir_rcc) |= (0x1<<4);
    143. (*vir_rcc) |= (0x1<<5);
    144. (*vir_led1_moder) &= (~(0x3<<20));
    145. (*vir_led1_moder) |= (0x1<<20);
    146. (*vir_led1_odr) &= (~(0x1<<10));
    147. (*vir_led2_moder) &= (~(0x3<<20));
    148. (*vir_led2_moder) |= (0x1<<20);
    149. (*vir_led2_odr) &= (~(0x1<<10));
    150. (*vir_led3_moder) &= (~(0x3<<16));
    151. (*vir_led3_moder) |= (0x1<<16);
    152. (*vir_led3_odr) &= (~(0x1<<8));
    153. return 0;
    154. }
    155. static void __exit mycdev_exit(void)
    156. {
    157. iounmap(vir_led1_moder);
    158. iounmap(vir_led1_odr);
    159. iounmap(vir_led2_moder);
    160. iounmap(vir_led2_odr);
    161. iounmap(vir_led3_moder);
    162. iounmap(vir_led3_odr);
    163. iounmap(vir_rcc);
    164. unregister_chrdev(major,"mychrdev");
    165. }
    166. module_init(mycdev_init);
    167. module_exit(mycdev_exit);
    168. MODULE_LICENSE("GPL");

    现象

  • 相关阅读:
    C++ Python网易云音乐播放器
    Android实现设置界面
    使用 AI 学习 Python web 的 django 代码(1/30天)
    【Touchstone 1.0&2.0数据格式解析】
    Qt客户端开发的流程
    springboot配置redis、Spring cache
    51单片机光照强度检测自动路灯开关仿真( proteus仿真+程序+报告+讲解视频)
    VScode通过ssh连接树莓派进行远程开发调试(踩坑总结)
    optimizer和loss.backward()相关函数
    pandas使用read_csv函数读取csv数据、设置parse_dates参数将csv数据中的指定字段数据列解析为时间日期对象
  • 原文地址:https://blog.csdn.net/weixin_53373681/article/details/133953505