• 上海华清071班


    驱动.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/ioctl.h>
    8. #include "text.h"
    9. #define CNAME "myled"
    10. int major;
    11. char kbuf[128]={0};
    12. struct class *cls;
    13. struct device *dev;
    14. int rets;
    15. int ret;
    16. image_t image;
    17. int mycdev_open(struct inode *inode, struct file *file)
    18. {
    19. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    20. return 0;
    21. }
    22. long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    23. {
    24. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    25. switch(cmd)
    26. {
    27. case UACCESS_BUF:
    28. rets=copy_from_user(kbuf,(char*)arg,sizeof(char [128]));
    29. if(rets)
    30. {
    31. printk("copy form user led on\n");
    32. return -EIO;
    33. }
    34. printk("copy form user kbuf=%s\n",kbuf);
    35. case UACCESS_IMAGE:
    36. ret=copy_from_user(&image,(image_t*)arg,sizeof(image_t));
    37. if(ret)
    38. {
    39. printk("copy form user led on\n");
    40. return -EIO;
    41. }
    42. printk("width=%d high=%d\n",image.width,image.high);
    43. image.width+=10;
    44. image.high+=10;
    45. ret=copy_to_user((image_t*)arg,&image,sizeof(image_t));
    46. if(ret)
    47. {
    48. printk("copy form user led on\n");
    49. return -EIO;
    50. }
    51. }
    52. return 0;
    53. }
    54. int mycdev_release(struct inode *inode, struct file *file)
    55. {
    56. printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    57. return 0;
    58. }
    59. const struct file_operations fops={
    60. .open = mycdev_open,
    61. .unlocked_ioctl = mycdev_ioctl,
    62. .release = mycdev_release,
    63. };
    64. static int __init mycdev_init(void)
    65. {
    66. major=register_chrdev(0,CNAME,&fops);
    67. if(major<0)
    68. {
    69. printk("error\n");
    70. return major;
    71. }
    72. cls=class_create(THIS_MODULE,CNAME);
    73. if(IS_ERR(cls))
    74. {
    75. printk("error\n");
    76. return PTR_ERR(cls);
    77. }
    78. dev=device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
    79. if(IS_ERR(dev))
    80. {
    81. printk("error\n");
    82. return PTR_ERR(dev);
    83. }
    84. printk("major=%d\n",major);
    85. return 0;
    86. }
    87. static void __exit mycdev_exit(void)
    88. {
    89. device_destroy(cls,major);
    90. class_destroy(cls);
    91. unregister_chrdev(major,CNAME);
    92. }
    93. module_init(mycdev_init);
    94. module_exit(mycdev_exit);
    95. MODULE_LICENSE("GPL");

    Makefile文件

    1. arch?=arm
    2. file?=demo
    3. ifeq ($(arch),x86)
    4. LIANXI=/lib/modules/$(shell uname -r)/build
    5. endif
    6. ifeq ($(arch),arm)
    7. LIANXI=/home/ubuntu/linux-5.10.61
    8. endif
    9. PWD:=$(shell pwd)
    10. all:
    11. make -C $(LIANXI) M=$(PWD) modules
    12. clean:
    13. make -C $(LIANXI) M=$(PWD) clean
    14. obj-m:=$(file).o

    应用层.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 <stdlib.h>
    7. #include <string.h>
    8. #include <sys/ioctl.h>
    9. #include "text.h"
    10. char buf[128]={"i am huyue"};
    11. int main(int argc, char const *argv[])
    12. {
    13. int fd=-1;
    14. fd=open("/dev/myled",O_RDWR);
    15. if(-1==fd)
    16. {
    17. perror("open is error");
    18. exit(1);
    19. }
    20. image_t image={20,1034};
    21. ioctl(fd,UACCESS_BUF,buf);
    22. ioctl(fd,UACCESS_IMAGE,&image);
    23. printf("%d %d\n",image.width,image.high);
    24. close(fd);
    25. return 0;
    26. }

    .h文件

    1. #ifndef __LED_H__
    2. #define __LED_H__
    3. typedef struct{
    4. volatile unsigned int MODER;
    5. volatile unsigned int OTYPER;
    6. volatile unsigned int OSPEEDR;
    7. volatile unsigned int PUPDR;
    8. volatile unsigned int IDR;
    9. volatile unsigned int ODR;
    10. }led_t;
    11. typedef enum
    12. {
    13. LED1,
    14. LED2,
    15. LED3
    16. }led_te;
    17. typedef struct {
    18. int width;
    19. int high;
    20. }image_t;
    21. #define PHY_GPIOE_MODER 0x50006000
    22. #define PHY_GPIOF_MODER 0x50007000
    23. #define PHY_RCC 0x50000A28
    24. #define LED_ON _IOW('a',1,int)
    25. #define LED_OFF _IOW('a',0,int)
    26. #define UACCESS_BUF _IOW('a',1,char [128])
    27. #define UACCESS_IMAGE _IOWR('a',0,image_t)
    28. #endif

     

  • 相关阅读:
    C++11 新特性 持续记录
    负载均衡架构设计技巧
    Unity关于无法新建项目的可能解决办法
    Oracle数据库中的table@xyz是什么意思?
    docker安装tomcat
    基于XML方式的Bean的配置概述
    Qt中定时器的所有使用方式
    EventLoop框架C++服务端(Tcp/Ip)搭建
    git clone失败
    听GPT 讲Rust源代码--library/std(1)
  • 原文地址:https://blog.csdn.net/m0_72479866/article/details/127911686