• IMX6ULL开发——第一个驱动程序


    实现第一个应用程序:在IMX6ULL开发板上运行驱动程序hello_drv_test

    hello_drv_test

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. /*
    8. * ./hello_drv_test -w abc
    9. * ./hello_drv_test -r
    10. */
    11. int main(int argc, char **argv)
    12. {
    13. int fd;
    14. char buf[1024];
    15. int len;
    16. /* 1. 判断参数 */
    17. if (argc < 2)
    18. {
    19. printf("Usage: %s -w \n", argv[0]);
    20. printf(" %s -r\n", argv[0]);
    21. return -1;
    22. }
    23. /* 2. 打开文件 */
    24. fd = open("/dev/hello", O_RDWR);
    25. if (fd == -1)
    26. {
    27. printf("can not open file /dev/hello\n");
    28. return -1;
    29. }
    30. /* 3. 写文件或读文件 */
    31. if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
    32. {
    33. len = strlen(argv[2]) + 1;
    34. len = len < 1024 ? len : 1024;
    35. write(fd, argv[2], len);
    36. }
    37. else
    38. {
    39. len = read(fd, buf, 1024);
    40. buf[1023] = '\0';
    41. printf("APP read : %s\n", buf);
    42. }
    43. close(fd);
    44. return 0;
    45. }

    hello_drv.c

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. /* 1. 确定主设备号 */
    17. static int major = 0;
    18. static char kernel_buf[1024];
    19. static struct class *hello_class;
    20. #define MIN(a, b) (a < b ? a : b)
    21. /* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */
    22. static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
    23. {
    24. int err;
    25. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    26. err = copy_to_user(buf, kernel_buf, MIN(1024, size));
    27. return MIN(1024, size);
    28. }
    29. static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
    30. {
    31. int err;
    32. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    33. err = copy_from_user(kernel_buf, buf, MIN(1024, size));
    34. return MIN(1024, size);
    35. }
    36. static int hello_drv_open (struct inode *node, struct file *file)
    37. {
    38. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    39. return 0;
    40. }
    41. static int hello_drv_close (struct inode *node, struct file *file)
    42. {
    43. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    44. return 0;
    45. }
    46. /* 2. 定义自己的file_operations结构体 */
    47. static struct file_operations hello_drv = {
    48. .owner = THIS_MODULE,
    49. .open = hello_drv_open,
    50. .read = hello_drv_read,
    51. .write = hello_drv_write,
    52. .release = hello_drv_close,
    53. };
    54. /* 4. 把file_operations结构体告诉内核:注册驱动程序 */
    55. /* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
    56. static int __init hello_init(void)
    57. {
    58. int err;
    59. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    60. major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */
    61. hello_class = class_create(THIS_MODULE, "hello_class");
    62. err = PTR_ERR(hello_class);
    63. if (IS_ERR(hello_class)) {
    64. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    65. unregister_chrdev(major, "hello");
    66. return -1;
    67. }
    68. device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
    69. return 0;
    70. }
    71. /* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 */
    72. static void __exit hello_exit(void)
    73. {
    74. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    75. device_destroy(hello_class, MKDEV(major, 0));
    76. class_destroy(hello_class);
    77. unregister_chrdev(major, "hello");
    78. }
    79. /* 7. 其他完善:提供设备信息,自动创建设备节点 */
    80. module_init(hello_init);
    81. module_exit(hello_exit);
    82. MODULE_LICENSE("GPL");

    Makefile

    1. # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
    2. # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
    3. # 2.1 ARCH, 比如: export ARCH=arm64
    4. # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
    5. # 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
    6. # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
    7. # 请参考各开发板的高级用户使用手册
    8. KERN_DIR = /home/book/100ask_roc-rk3399-pc/linux-4.4
    9. all:
    10. make -C $(KERN_DIR) M=`pwd` modules
    11. $(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c
    12. clean:
    13. make -C $(KERN_DIR) M=`pwd` modules clean
    14. rm -rf modules.order
    15. rm -f hello_drv_test
    16. obj-m += hello_drv.o

    一般的步骤:(1)编译内核(内核,设备树,驱动)

                          (2)编译安装内核模块

                          (3)安装内核和模块到开发板上(即更新了内核和设备树)

                          (4)实现驱动程序

    前提:编译驱动程序之前为什么要编译内核

            (1)驱动程序要用到内核文件。例如比如驱动程序中这样包含头文件:#include ,其中的 asm 是 一个链接文件,指向 asm-arm 或 asm-mips,这需要先配置、编译内核才会生成 asm 这个链接文件。

            (2)编译驱动时用的内核、开发板上运行到内核,要一致。所以用我们编译出来的内核去代替原来的内核。保证编译驱动的内核是我们自己编译出来的。

            (3)当板子使用新内核时,板子上对应的驱动也要换成新编译出的。所以要先编译内核和模块,并且要放到板子上

    步骤一:编译内核(内核,设备树,驱动)

    (1)进入内核源码的目录

    cd /home/book/100ask_imx6ull-sdk

    (2)配置(确定内核是给arm还x86编译的)

     make 100ask_imx6ull_defconfig

    (3)编译内核

     make zImage -j4

    (4)编译设备树

     make dtbs

    (5)将内核和设备树复制到指定位置

    1. cp arch/arm/boot/zImage ~/nfs_rootfs
    2. cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs

    步骤二:编译安装内核模块

    (1)进入内核源码所在目录

     cd ~/100ask_imx6ull-sdk/Linux-4.9.88/

    (2)编译内核模块

    make modules

    (3)安装内核模块

    make ARCH=arm INSTALL_MOD_PATH=/home/book/nfs_rootfs modules_install

    从上面可知我们将设备树(dtb),内核(zImage)和模块(module)都放到了~/nfs_rootfs/下

    步骤三:安装内核和模块到开发板上(即更新了内核和设备树)

    (1)先将ubuntu与板子挂载

    mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt

    (2)更新内核

    cp /mnt/zImage /boot

    (3)更新设备树

    cp /mnt/100ask_imx6ull-14x14.dtb /boot

    (4)更新内核模块

    cp /mnt/lib/modules /lib -rfd

    (5)同步一下(将内存里的内容强制的刷到flash里面去)

    sync

    步骤四:实现驱动

    (1)将驱动程序放到ubuntu里面

    方法:先使用Filezilla将程序从windows转移到ubuntu

    (2)修改Makefile

            指定开发板使用的内核源码路径

    KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.0.88    //其他不做修改

    (3)编译驱动程序

            使用Makefile的进行编译

    make

    (4)通过挂载,将编译后的驱动程序传到板子上

    mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs /mnt

    (5)进入到程序目录下,然后装载驱动程序

    insmod hello_drv.ko

    (6)执行测试程序

    1. ./hello_drv_test -w lsr
    2. ./hello_drv_test -r

  • 相关阅读:
    【MacOs系统-M2安装2022新版AWVS渗透工具】-保姆级安装教程
    一文搞懂this指向
    Java枚举
    用Leangoo领歌免费敏捷工具做敏捷需求管理
    改变本轮牛市走势的核心是什么?2021-04-19
    UFT On录制Web应用时无法识别Chrome浏览器对象
    一篇搞懂 Java线程池
    蒋鑫鸿:9.10国际黄金原油最新外盘行情趋势点评附解一套技术指导
    Ceph入门到精通-Linux内核网络参数优化小结
    MySQL库的库操作指南
  • 原文地址:https://blog.csdn.net/L1834056458/article/details/133976563