导航

    全志在线开发者论坛

    • 注册
    • 登录
    • 搜索
    • 版块
    • 话题
    • 在线文档
    • 社区主页

    【全志T113-S3_100ask】2-编写第一个驱动

    其它全志芯片讨论区
    百问网全志t113-s3
    1
    1
    1219
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • Y
      giao LV 6 最后由 yanmingjian 编辑

      (1)开发环境

      windows开发环境:VsCode
      linux 开发环境:Ubuntu 18.04 环境参考

      1、指定交叉编译

      在buildroot 目录下发现两个 arm-linux-gnueabi-gcc

      root@znh-ubuntu:/disk/buildroot-100ask_t113-pro/buildroot# find ./* -name "*gnueabi-gcc"
      ./output/host/opt/ext-toolchain/bin/arm-linux-gnueabi-gcc
      ./output/host/bin/arm-linux-gnueabi-gcc
      

      但 ./output/host/bin/arm-linux-gnueabi-gcc 是链接过去的

      c7a46f0a61d24d149466e43da584b784.png

      ./output/host/opt/ext-toolchain/bin/arm-linux-gnueabi-gcc 是原始的

      f577b95a46d9419dbbb2dc632ba6c0f1.png

      在这里我使用链接后的gcc
      加载环境变量:

      export PATH=$PATH:/disk/buildroot-100ask_t113-pro/buildroot/output/host/bin
      export ARCH=arm
      export CROSS_COMPILE=arm-linux-gnueabi-
      
      root@znh-ubuntu:~# export PATH=$PATH:/disk/buildroot-100ask_t113-pro/buildroot/output/host/bin
      root@znh-ubuntu:~# export ARCH=arm
      root@znh-ubuntu:~# export CROSS_COMPILE=arm-linux-gnueabi-
      root@znh-ubuntu:~#
      root@znh-ubuntu:~# arm-linux-gnueabi-gcc -v
      使用内建 specs。
      COLLECT_GCC=arm-linux-gnueabi-gcc
      COLLECT_LTO_WRAPPER=/disk/t113GitHub/eLinuxCore_100ask-t113-pro/toolchain/gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabi/bin/../libexec/gcc/arm-linux-gnueabi/7.2.1/lto-wrapper
      目标:arm-linux-gnueabi
      配置为:'/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/snapshots/gcc.git~linaro-7.2-2017.11/configure' SHELL=/bin/bash --with-mpc=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu --with-mpfr=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gmp=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gnu-as --with-gnu-ld --disable-libmudflap --enable-lto --enable-shared --without-included-gettext --enable-nls --disable-sjlj-exceptions --enable-gnu-unique-object --enable-linker-build-id --disable-libstdcxx-pch --enable-c99 --enable-clocale=gnu --enable-libstdcxx-debug --enable-long-long --with-cloog=no --with-ppl=no --with-isl=no --disable-multilib --with-float=soft --with-mode=thumb --with-tune=cortex-a9 --with-arch=armv7-a --enable-threads=posix --enable-multiarch --enable-libstdcxx-time=yes --enable-gnu-indirect-function --with-build-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/sysroots/arm-linux-gnueabi --with-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu/arm-linux-gnueabi/libc --enable-checking=release --disable-bootstrap --enable-languages=c,c++,fortran,lto --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=arm-linux-gnueabi --prefix=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu
      线程模型:posix
      gcc 版本 7.2.1 20171011 (Linaro GCC 7.2-2017.11)
      

      不想每次否加载可以写在自启动脚本

      vim ~/.bashrc
      

      f71d9f87bd1542bdb05759c4ee985910.png

      ##(2)编写驱动

      1、编写 helloword驱动 hello_drv.c

      #include <linux/module.h>
      
      #include <linux/fs.h>
      #include <linux/errno.h>
      #include <linux/miscdevice.h>
      #include <linux/kernel.h>
      #include <linux/major.h>
      #include <linux/mutex.h>
      #include <linux/proc_fs.h>
      #include <linux/seq_file.h>
      #include <linux/stat.h>
      #include <linux/init.h>
      #include <linux/device.h>
      #include <linux/tty.h>
      #include <linux/kmod.h>
      #include <linux/gfp.h>
      
      /* 1. 确定主设备号                                                                 */
      static int major = 0;
      static char kernel_buf[1024];
      static struct class *hello_class;
      
      
      #define MIN(a, b) (a < b ? a : b)
      
      /* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
      static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
      {
      	int err;
      	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      	err = copy_to_user(buf, kernel_buf, MIN(1024, size));
      	return MIN(1024, size);
      }
      
      static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
      {
      	int err;
      	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      	err = copy_from_user(kernel_buf, buf, MIN(1024, size));
      	return MIN(1024, size);
      }
      
      static int hello_drv_open (struct inode *node, struct file *file)
      {
      	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      	return 0;
      }
      
      static int hello_drv_close (struct inode *node, struct file *file)
      {
      	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      	return 0;
      }
      
      /* 2. 定义自己的file_operations结构体                                              */
      static struct file_operations hello_drv = {
      	.owner	 = THIS_MODULE,
      	.open    = hello_drv_open,
      	.read    = hello_drv_read,
      	.write   = hello_drv_write,
      	.release = hello_drv_close,
      };
      
      /* 4. 把file_operations结构体告诉内核:注册驱动程序                                */
      /* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
      static int __init hello_init(void)
      {
      	int err;
      	
      	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      	major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */
      
      
      	hello_class = class_create(THIS_MODULE, "hello_class");
      	err = PTR_ERR(hello_class);
      	if (IS_ERR(hello_class)) {
      		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      		unregister_chrdev(major, "hello");
      		return -1;
      	}
      	
      	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
      	
      	return 0;
      }
      
      /* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数           */
      static void __exit hello_exit(void)
      {
      	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
      	device_destroy(hello_class, MKDEV(major, 0));
      	class_destroy(hello_class);
      	unregister_chrdev(major, "hello");
      }
      
      
      /* 7. 其他完善:提供设备信息,自动创建设备节点                                     */
      
      module_init(hello_init);
      module_exit(hello_exit);
      
      MODULE_LICENSE("GPL");
      

      2、编写测试驱动文件 hello_drv_test.c

      #include <sys/types.h>
      #include <sys/stat.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <stdio.h>
      #include <string.h>
      
      /*
       * ./hello_drv_test -w abc
       * ./hello_drv_test -r
       */
      int main(int argc, char **argv)
      {
      	int fd;
      	char buf[1024];
      	int len;
      	
      	/* 1. 判断参数 */
      	if (argc < 2) 
      	{
      		printf("Usage: %s -w <string>\n", argv[0]);
      		printf("       %s -r\n", argv[0]);
      		return -1;
      	}
      
      	/* 2. 打开文件 */
      	fd = open("/dev/hello", O_RDWR);
      	if (fd == -1)
      	{
      		printf("can not open file /dev/hello\n");
      		return -1;
      	}
      
      	/* 3. 写文件或读文件 */
      	if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
      	{
      		len = strlen(argv[2]) + 1;
      		len = len < 1024 ? len : 1024;
      		write(fd, argv[2], len);
      	}
      	else
      	{
      		len = read(fd, buf, 1024);		
      		buf[1023] = '\0';
      		printf("APP read : %s\n", buf);
      	}
      	
      	close(fd);
      	
      	return 0;
      }
      

      3、编写Makefile

      # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
      # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
      # 2.1 ARCH,          比如: export ARCH=arm64
      # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
      # 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 
      # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
      #       请参考各开发板的高级用户使用手册
      
      KERN_DIR = /disk/buildroot-100ask_t113-pro/buildroot/output/build/linux-d96275805a67d54998123d36e59108cb1ed52ad5
      
      all:
      	make -C $(KERN_DIR) M=`pwd` modules 
      	$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 
      
      clean:
      	make -C $(KERN_DIR) M=`pwd` modules clean
      	rm -rf modules.order
      	rm -f hello_drv_test
      
      obj-m	+= hello_drv.o
      

      关于 KERN_DIR ,本环境使用的是 buildroot ,通过GitHub把kernel下载下来的,源码在./buildroot-100ask_t113-pro/buildroot/dl/linux/git 下,但是该源码未经过编译,而上一节已经编译过 buildroot 生成镜像img了,那就是已经编译过了,但是指定该目录编译不了

      82c44d9bc6af4be29e232d1fe8f53cd3.png

      真正的编译目录在
      ./buildroot-100ask_t113-pro/buildroot/output/build/linux-d96275805a67d54998123d36e59108cb1ed52ad5
      (我不确实后面的字母数字是不是每个人一样)

      f98a38e3af8544d090c968382aa25756.png

      (3)测试

      将生成的 hello_drv.ko、hello_drv_test 放到开发板上
      在这里使用 tftp 下载。(TFTP的搭建参考:链接)
      但第一次使用系统没有连接网络(现在已经插入网线)

      # ifconfig
      lo        Link encap:Local Loopback
                inet addr:127.0.0.1  Mask:255.0.0.0
                inet6 addr: ::1/128 Scope:Host
                UP LOOPBACK RUNNING  MTU:65536  Metric:1
                RX packets:6 errors:0 dropped:0 overruns:0 frame:0
                TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
                collisions:0 txqueuelen:1000
                RX bytes:603 (603.0 B)  TX bytes:603 (603.0 B)
      

      1、使用udhcpc 自动联网

      # udhcpc
      udhcpc: started, v1.35.0
      [ 3538.458305] libphy: 4500000.eth: probed
      [ 3538.462913] sunxi-gmac 4500000.eth eth0: eth0: Type(7) PHY ID 001cc816 at 0 IRQ poll (4500000.eth-0:00)
      udhcpc: broadcasting discover
      [ 3541.673890] sunxi-gmac 4500000.eth eth0: Link is Up - 100Mbps/Full - flow control off
      [ 3541.682714] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
      udhcpc: broadcasting discover
      udhcpc: broadcasting select for 192.168.3.45, server 192.168.3.1
      udhcpc: lease of 192.168.3.45 obtained from 192.168.3.1, lease time 604800
      deleting routers
      adding dns 192.168.3.1
      

      2、通过 tftp 下载资源

      # tftp -g -r hello_drv.ko 192.168.3.44
      # tftp -g -r hello_drv_test 192.168.3.44
      #
      # ls
      hello_drv.ko    hello_drv_test
      

      3、测试驱动

      (1)加载驱动

      #  insmod hello_drv.ko
      [ 3860.564181] hello_drv: loading out-of-tree module taints kernel.
      [ 3860.571552] /disk/vsCode/01_hello/hello_drv.c hello_init line 70
      
      
      
      (2)查看驱动是否加载
      # ls /dev/hello*
      /dev/hello
      # lsmod
      Module                  Size  Used by    Tainted: G
      hello_drv              16384  0
      
      (3)添加权限
      # chmod 777 ./hello_drv_test
      
      
      (4)向驱动写入数据
      # ./hello_drv_test  -w abc
      [ 3906.061995] /disk/vsCode/01_hello/hello_drv.c hello_drv_open line 45
      [ 3906.069274] /disk/vsCode/01_hello/hello_drv.c hello_drv_write line 38
      [ 3906.076579] /disk/vsCode/01_hello/hello_drv.c hello_drv_close line 51
      
      (5)读取驱动数据
      # ./hello_drv_test  -r
      [ 3913.268282] /disk/vsCode/01_hello/hello_drv.c hello_drv_open line 45
      [ 3913.275535] /disk/vsCode/01_hello/hello_drv.c hello_drv_read line 30
      APP read : abc
      [ 3913.283024] /disk/vsCode/01_hello/hello_drv.c hello_drv_close line 51
      
      (6)卸载驱动
      # rmmod hello_drv
      [ 4304.419170] /disk/vsCode/01_hello/hello_drv.c hello_exit line 90
      # lsmod
      Module                  Size  Used by    Tainted: G
      sunxi_ce               57344  0
      
      

      原文链接:https://blog.csdn.net/qq_46079439/article/details/125897482

      1 条回复 最后回复 回复 引用 分享 0
      • 1 / 1
      • First post
        Last post

      Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号

      行为准则 | 用户协议 | 隐私权政策