• 驱动 实现三盏灯开关


    1. #ifndef __H__
    2. #define __H__
    3. #define LED_MODERE 0X50006000
    4. #define LED_MODERF 0X50007000
    5. #define LED_ODRE 0X50006014
    6. #define LED_ODRF 0X50007014
    7. #define LED_RCC 0X50000A28
    8. #endif
    1. #include<stdio.h>
    2. #include<sys/types.h>
    3. #include<sys/stat.h>
    4. #include<fcntl.h>
    5. #include<unistd.h>
    6. #include<stdlib.h>
    7. #include<string.h>
    8. #include "head.h"
    9. int main(int argc, const char *argv[])
    10. {
    11. char buf[128]="";
    12. int fd=open("/dev/mychrdev",O_RDWR);
    13. if(fd<0)
    14. {
    15. printf("设备文件打开失败\n");
    16. exit(-1);
    17. }
    18. while(1)
    19. {
    20. printf("请开灯关灯(0/1)\n");
    21. fgets(buf,sizeof(buf),stdin);
    22. buf[strlen(buf)-1]='\0';
    23. write(fd,buf,sizeof(buf));
    24. memset(buf,0,sizeof(buf));
    25. read(fd,buf,sizeof(buf));
    26. }
    27. close(fd);
    28. return 0;
    29. }
    1. #include <linux/init.h>
    2. #include <linux/module.h>
    3. #include <linux/fs.h>
    4. #include <linux/uaccess.h>
    5. #include <linux/compiler.h>
    6. #include <linux/io.h>
    7. #include "head.h"
    8. unsigned int major;
    9. char kbuf[128]="";
    10. unsigned int *vir_modere;
    11. unsigned int *vir_moderf;
    12. unsigned int *vir_rcc;
    13. unsigned int *vir_odre;
    14. unsigned int *vir_odrf;
    15. int mycdev_open(struct inode *inode,struct file *file)
    16. {
    17. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    18. return 0;
    19. }
    20. ssize_t mycdev_read(struct file *file,char *ubuf,size_t size,loff_t *lof)
    21. {
    22. int ret;
    23. ret=copy_to_user(ubuf,kbuf,size);
    24. if(ret)
    25. {
    26. printk("copy_from_user err\n");
    27. return -EIO;
    28. }
    29. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    30. return 0;
    31. }
    32. ssize_t mycdev_write(struct file *file,const char *ubuf,size_t size,loff_t *lof)
    33. {
    34. int ret;
    35. ret=copy_from_user(kbuf,ubuf,size);
    36. if(ret)
    37. {
    38. printk("copy_from_user err\n");
    39. return -EIO;
    40. }
    41. if(kbuf[0]=='1')
    42. {
    43. //开灯
    44. //led1
    45. (*vir_odre) |= (0x1<<10);
    46. //led2
    47. (*vir_odrf) |= (0x1<<10);
    48. //led3
    49. (*vir_odre) |= (0x1<<8);
    50. printk("开灯-->%d\n",*kbuf);
    51. }
    52. else if(kbuf[0]=='0')
    53. {
    54. //关灯
    55. //led1
    56. (*vir_odre) &= (~(0x1<<10));
    57. //led2
    58. (*vir_odrf) &= (~(0x1<<10));
    59. //led3
    60. (*vir_odre) &= (~(0x1<<8));
    61. printk("关灯-->%d\n",*kbuf);
    62. }
    63. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    64. return 0;
    65. }
    66. int mycdev_close(struct inode *inode,struct file *file)
    67. {
    68. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    69. return 0;
    70. }
    71. struct file_operations fops = {
    72. .open=mycdev_open,
    73. .read=mycdev_read,
    74. .write=mycdev_write,
    75. .release=mycdev_close,
    76. };
    77. static int __init mycdev_init(void)
    78. {
    79. major = register_chrdev(0,"mychrdev",&fops);
    80. if(major < 0)
    81. {
    82. printk("字符设备驱动注册失败\n");
    83. return major;
    84. }
    85. printk("字符设备驱动注册成功%d\n",major);
    86. //进行映射
    87. vir_rcc=ioremap(LED_RCC,4);
    88. if(vir_rcc==NULL)
    89. {
    90. printk("物理映射失败%d\n",__LINE__);
    91. }
    92. vir_modere=ioremap(LED_MODERE,4);
    93. if(vir_rcc==NULL)
    94. {
    95. printk("物理映射失败%d\n",__LINE__);
    96. }
    97. vir_moderf=ioremap(LED_MODERF,4);
    98. if(vir_rcc==NULL)
    99. {
    100. printk("物理映射失败%d\n",__LINE__);
    101. }
    102. vir_odre=ioremap(LED_ODRE,4);
    103. if(vir_rcc==NULL)
    104. {
    105. printk("物理映射失败%d\n",__LINE__);
    106. }
    107. vir_odrf=ioremap(LED_ODRF,4);
    108. if(vir_rcc==NULL)
    109. {
    110. printk("物理映射失败%d\n",__LINE__);
    111. }
    112. //rcc使能
    113. (*vir_rcc) |= (0x1<<4);
    114. (*vir_rcc) |= (0x1<<5);
    115. // moder设置输出模式
    116. //led1,PE10
    117. (*vir_modere) &= (~(0x3<<20));
    118. (*vir_modere) |= (0x1<<20);
    119. //led2PF10
    120. (*vir_moderf) &= (~(0x3<<20));
    121. (*vir_moderf) |= (0x1<<20);
    122. //led3,PE8
    123. (*vir_modere) &= (~(0x3<<16));
    124. (*vir_modere) |= (0x1<<16);
    125. //设置默认为关灯
    126. (*vir_odre) &= (~(0x1<<10));
    127. (*vir_odrf) &= (~(0x1<<10));
    128. (*vir_odre) &= (~(0x1<<8));
    129. return 0;
    130. }
    131. static void __exit mycdev_exit(void)
    132. {
    133. unregister_chrdev(major,"mychrdev");
    134. iounmap(vir_rcc);
    135. iounmap(vir_modere);
    136. iounmap(vir_moderf);
    137. iounmap(vir_odre);
    138. iounmap(vir_odrf);
    139. }
    140. module_init(mycdev_init);
    141. module_exit(mycdev_exit);
    142. MODULE_LICENSE("GPL");

  • 相关阅读:
    Mycat分库分表分片方式
    Java long类型转换易犯的错误
    【Flink】 FlinkCDC读取Mysql( DataStream 方式)(带完整源码,直接可使用)
    git常用命令
    程序员也许都缺一个“二舅”精神
    被动语态练习题
    SAP PI/PO 视频,自己录制
    每日汇评:黄金将测试1935美元的200日移动均线
    openjudge 1.5.23 药房管理
    计算机毕业设计ssm高校教师科研能力评定系统40n60系统+程序+源码+lw+远程部署
  • 原文地址:https://blog.csdn.net/Kukuderener/article/details/132800252