驱动开发了解下

目录

  • 前提
  • 环境说明
    • 网络环境
      • 网络环境1
      • 网络环境2
  • 启动开发板,tftp下载内核和驱动,nfs挂载文件系统
  • 开发流程1
    • 编写驱动程序
    • 修改Makefile
    • 编译
    • 创建拷贝
    • 加载驱动
      • modprobe加载驱动
      • depmod
      • 查看当前系统中存在的模块
      • 查看系统所有的设备
      • 创建设备节点文件
      • APP测试chrdevbase 设备操作
      • 卸载驱动模块
  • 开发流程2
    • nfs挂载测试
    • 驱动加载
  • 加载测试led驱动

前提

系统篇完结,移植好系统,搭建好nfs tftp环境,并测试通过。

环境说明

网络环境

这里我采用手机usb共享给电脑,然后开发板网线连接电脑网口。

  1. 之前我是有2台电脑,然后都连上wifi,然后有一台电脑实现wifi和网口实现网桥,这样是可以保证在同一网段的,wifi共享方式不能保证在同一网段,它会默认ip是192.168.137网段,
  2. 然后最近搞了个usb转网口的,在这里我就只采用一台电脑,开始也是尝试使用wifi和网口搭建网桥,然后发现我Windows主机不能上网了,可今天我使用了usb就很好的解决了这一问题,网速还挺可观,但这里也有一个问题,就是这样我好像连接不上samba,也传输不了文件,应该是Windows主机与虚拟机不在同一网段了。
    驱动开发了解下_第1张图片
// 默认网关
192.168.210.61

//主机ip就没了
这也就导致Windows无法通过samba里连接虚拟机了

//网桥ip
192.168.210.245

//开发板 IP 地址 
192.168.210.150 

//服务器地址,也就是 Ubuntu 地址 
192.168.210.154 

驱动开发了解下_第2张图片

网络环境1

采用电脑端开发驱动,可以采用vscode或source insight,看个人喜好。
这里我采用使用samba实现文件传输,好处就是我可以在linux里创建一个sanba文件夹,然后通过ip地址实现网路映射到Windows主机上,我这里把挂载nfs目录放在samba文件夹里,然后我可以在里面存放开发程序,就不用写一个程序就使用传输软件传到linux虚拟机这边。

网络环境2

在我认为这样不行时,我想到可以设置Windows主机与虚拟机之间的共享文件夹,如果不使用网络的话就安装安装VMwareTools。
驱动开发了解下_第3张图片

驱动开发了解下_第4张图片
然后就是安装VMwareTools,完成上面操作后,桌面上会有这玩意,你可以双击打开

驱动开发了解下_第5张图片
驱动开发了解下_第6张图片
安装时注意全选yes,然后一直回车。

flynnsinu@ubuntu:/usr/local/bin/vmware-tools-distrib$ sudo ./vmware-install.pl 
The installer has detected an existing installation of open-vm-tools packages 
on this system and will not attempt to remove and replace these user-space 
applications. It is recommended to use the open-vm-tools packages provided by 
the operating system. If you do not want to use the existing installation of 
open-vm-tools packages and use VMware Tools, you must uninstall the 
open-vm-tools packages and re-run this installer.
The packages that need to be removed are:
open-vm-tools
Packages must be removed with the --purge option.
The installer will next check if there are any missing kernel drivers. Type yes
if you want to do this, otherwise type no [yes] yes

INPUT: [yes]

Creating a new VMware Tools installer database using the tar4 format.

Installing VMware Tools.

In which directory do you want to install the binary files? 
[/usr/bin] 

INPUT: [/usr/bin]  default

What is the directory that contains the init directories (rc0.d/ to rc6.d/)? 
[/etc] 

INPUT: [/etc]  default

What is the directory that contains the init scripts? 
[/etc/init.d] 

INPUT: [/etc/init.d]  default

In which directory do you want to install the daemon files? 
[/usr/sbin] 

INPUT: [/usr/sbin]  default

In which directory do you want to install the library files? 
[/usr/lib/vmware-tools] 

INPUT: [/usr/lib/vmware-tools]  default

The path "/usr/lib/vmware-tools" does not exist currently. This program is 
going to create it, including needed parent directories. Is this what you want?
[yes] yes

INPUT: [yes]

In which directory do you want to install the documentation files? 
[/usr/share/doc/vmware-tools] 

INPUT: [/usr/share/doc/vmware-tools]  default

The path "/usr/share/doc/vmware-tools" does not exist currently. This program 
is going to create it, including needed parent directories. Is this what you 
want? [yes] yes

INPUT: [yes]

The installation of VMware Tools 10.3.22 build-15902021 for Linux completed 
successfully. You can decide to remove this software from your system at any 
time by invoking the following command: "/usr/bin/vmware-uninstall-tools.pl".

Before running VMware Tools for the first time, you need to configure it by 
invoking the following command: "/usr/bin/vmware-config-tools.pl". Do you want 
this program to invoke the command for you now? [yes] yes

INPUT: [yes]


You have chosen to install VMware Tools on top of an open-vm-tools package.  
You will now be given the option to replace some commands provided by 
open-vm-tools.  Please note that if you replace any commands at this time and 
later remove VMware Tools, it may be necessary to re-install the open-vm-tools.

The file /usr/bin/vmware-hgfsclient that this program was about to install 
already exists.  Overwrite? [no] yes

INPUT: [yes]

The file /sbin/mount.vmhgfs that this program was about to install already 
exists.  Overwrite? [no] yes

INPUT: [yes]

The file /usr/bin/vmhgfs-fuse that this program was about to install already 
exists.  Overwrite? [no] yes

INPUT: [yes]

Initializing...


Making sure services for VMware Tools are stopped.

Stopping VMware Tools services in the virtual machine:
   VMware User Agent (vmware-user):                                    done
   Unmounting HGFS shares:                                             done
   Guest filesystem driver:                                            done


The module vmci has already been installed on this system by another installer 
or package and will not be modified by this installer.

The module vsock has already been installed on this system by another installer
or package and will not be modified by this installer.

The module vmxnet3 has already been installed on this system by another 
installer or package and will not be modified by this installer.

The module pvscsi has already been installed on this system by another 
installer or package and will not be modified by this installer.

The module vmmemctl has already been installed on this system by another 
installer or package and will not be modified by this installer.

The VMware Host-Guest Filesystem allows for shared folders between the host OS 
and the guest OS in a Fusion or Workstation virtual environment.  Do you wish 
to enable this feature? [yes] yes

INPUT: [yes]

The vmxnet driver is no longer supported on kernels 3.3 and greater. Please 
upgrade to a newer virtual NIC. (e.g., vmxnet3 or e1000e)


Skipping configuring automatic kernel modules as no drivers were installed by 
this installer.


Skipping rebuilding initrd boot image for kernel as no drivers to be included 
in boot image were installed by this installer.

The configuration of VMware Tools 10.3.22 build-15902021 for Linux for this 
running kernel completed successfully.

Enjoy,

--the VMware team

出现这个Enjoy,就代表完成。

驱动开发了解下_第7张图片

启动开发板,tftp下载内核和驱动,nfs挂载文件系统

使用网络环境2网速就是快。

驱动开发了解下_第8张图片
驱动开发了解下_第9张图片

开发流程1

也就是方便调试,可以在网络的基础上,针对于不完善的系统。

编写驱动程序

在这里插入图片描述

修改Makefile

系统篇编译的内核路径

KERNELDIR := /home/samba/flynnsin_linux/linux

驱动开发了解下_第10张图片

编译

这里可以普通用户也可root用户

root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# make
make -C /home/samba/flynnsin_linux/linux M=/mnt/hgfs/share/zzk_drive/01_chrdevbase modules
make[1]: 进入目录“/home/samba/flynnsin_linux/linux”
  CC [M]  /mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.o
  Building modules, stage 2.
make[2]: 警告:文件“/mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.o”的修改时间在未来 0.94 秒后
  MODPOST 1 modules
  CC      /mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.mod.o
  LD [M]  /mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.ko
make[2]: 警告:检测到时钟错误。您的创建可能是不完整的。
make[1]: 离开目录“/home/samba/flynnsin_linux/linux”

修改环境变量,当然这里也可以修改,linux文件夹下makefile

root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# export ARCH=arm
root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# export CROSS_COMPILE=arm-linux-gnueabihf-
root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# export
declare -x ARCH="arm"
declare -x CLUTTER_IM_MODULE="xim"
declare -x COLORTERM="truecolor"
declare -x CROSS_COMPILE="arm-linux-gnueabihf-"

make

root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# make
make -C /home/samba/flynnsin_linux/linux M=/mnt/hgfs/share/zzk_drive/01_chrdevbase modules
make[1]: 进入目录“/home/samba/flynnsin_linux/linux”
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: 离开目录“/home/samba/flynnsin_linux/linux”
root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# ls
1_chrdevbase.code-workspace  chrdevbase.c   chrdevbase.mod.c  chrdevbase.o  modules.order
chrdevbaseApp.c              chrdevbase.ko  chrdevbase.mod.o  Makefile      Module.symvers

当然你也可以使用这个make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
但推荐使用export

root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- -j4
make -C /home/samba/flynnsin_linux/linux M=/mnt/hgfs/share/zzk_drive/01_chrdevbase modules
make[1]: 进入目录“/home/samba/flynnsin_linux/linux”
  CC [M]  /mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.o
  Building modules, stage 2.
make[2]: 警告:文件“/mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.o”的修改时间在未来 1.1 秒后
  MODPOST 1 modules
  CC      /mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.mod.o
  LD [M]  /mnt/hgfs/share/zzk_drive/01_chrdevbase/chrdevbase.ko
make[2]: 警告:检测到时钟错误。您的创建可能是不完整的。
make[1]: 离开目录“/home/samba/flynnsin_linux/linux”
root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# ls
1_chrdevbase.code-workspace  chrdevbase.c   chrdevbase.mod.c  chrdevbase.o  modules.order
chrdevbaseApp.c              chrdevbase.ko  chrdevbase.mod.o  Makefile      Module.symvers
root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- clean
make -C /home/samba/flynnsin_linux/linux M=/mnt/hgfs/share/zzk_drive/01_chrdevbase clean
make[1]: 进入目录“/home/samba/flynnsin_linux/linux”
  CLEAN   /mnt/hgfs/share/zzk_drive/01_chrdevbase/.tmp_versions
  CLEAN   /mnt/hgfs/share/zzk_drive/01_chrdevbase/Module.symvers
make[1]: 离开目录“/home/samba/flynnsin_linux/linux”

生成.ko文件即代表编译成功
编译app测试文件

root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# arm-linux-gnueabihf-gcc chrdevbaseApp.c -o chrdevbaseApp
root@ubuntu:/mnt/hgfs/share/zzk_drive/01_chrdevbase# ls
1_chrdevbase.code-workspace  chrdevbaseApp.c  chrdevbase.ko     chrdevbase.mod.o  Makefile       Module.symvers
chrdevbaseApp                chrdevbase.c     chrdevbase.mod.c  chrdevbase.o      modules.order

创建拷贝

insmod和 modprobe,insmod 是最简单的模块加载命令,此命令用于加载指定的.ko 模块,比如加载 drv.ko
这个驱动模块,命 令如下: insmod drv.ko insmod 命令不能解决模块的依赖关系,比如 drv.ko 依赖 first.ko
这个模块,就必须先使用 insmod 命令加载 first.ko 这个模块,然后再加载 drv.ko 这个模块。但是 modprobe
就不会存在这 个问题,modprobe 会分析模块的依赖关系,然后会将所有的依赖模块都加载到内核中,因此 modprobe 命令相比
insmod 要智能一些。modprobe 命令主要智能在提供了模块的依赖性分析、 错误检查、错误报告等功能,推荐使用 modprobe
命令来加载驱动。modprobe 命令默认会去 /lib/modules/目录中查找模块,比如本书使用的
Linux kernel 的版本号为 4.1.15, 因此 modprobe 命令默认会到/lib/modules/4.1.15
这个目录中查找相应的驱动模块,一般自己制 作的根文件系统中是不会有这个目录的,所以需要自己手动创建。

就是mpoprobe可以自动加载依赖,当然这边还可以使用insmod加载,但推荐使用modprobe。

root@ubuntu:/home/samba/linux/nfs/rootfs/lib# mkdir modules
root@ubuntu:/home/samba/linux/nfs/rootfs/lib# cd modules/
root@ubuntu:/home/samba/linux/nfs/rootfs/lib/modules# mkdir 4.1.15
root@ubuntu:/home/samba/linux/nfs/rootfs/lib/modules# cd 4.1.15/
cp chrdevbase.ko chrdevbaseApp /home/samba/linux/nfs/rootfs/lib/modules/4.1.15/

加载驱动

modprobe加载驱动

/lib/modules/4.1.15 # modprobe chrdevbase.ko
modprobe: can't open 'modules.dep': No such file or directory

无法打开“modules.dep”这个文件,我们不用手动创建 modules.dep 这个文件。使用命令生成。

depmod

/lib/modules/4.1.15 # depmod
/lib/modules/4.1.15 # ls
chrdevbase.ko    modules.alias    modules.symbols
chrdevbaseApp    modules.dep

查看当前系统中存在的模块

/lib/modules/4.1.15 # modprobe chrdevbase.ko
chrdevbase init!
/lib/modules/4.1.15 # lsmod
Module                  Size  Used by    Tainted: G
chrdevbase              2036  0

查看系统所有的设备

cat /proc/devices

驱动开发了解下_第11张图片

创建设备节点文件

/lib/modules/4.1.15 # mknod /dev/chrdevbase c 200 0
/lib/modules/4.1.15 # ls /dev/chrdevbase
/dev/chrdevbase
/lib/modules/4.1.15 # ls /dev/chrdevbase -l
crw-r--r--    1 0        0         200,   0 Apr  3 16:50 /dev/chrdevbase

APP测试chrdevbase 设备操作

/lib/modules/4.1.15 # ./chrdevbaseApp /dev/chrdevbase 1
chrdevbase open!
kernel senddata ok!
chrdevbase release!a!

/lib/modules/4.1.15 # ./chrdevbaseApp /dev/chrdevbase 2
chrdevbase open!
kernel recevdata:usr data!
chrdevbase release!

卸载驱动模块

/lib/modules/4.1.15 # rmmod chrdevbase.ko
chrdevbase exit!
/lib/modules/4.1.15 # lsmod
Module                  Size  Used by    Tainted: G

开发流程2

编写驱动那些环节一致。

nfs挂载测试

这里因为文件系统相对完善,有时我没需要加需求,我们可以使用这种方式测试。
这里把虚拟机上nfs目录挂载到开发板nfs_get目录。
nfsvers=3针对nfs版本号,这里要设置对,不然会有版本报错原因。

mount -t nfs -o nolock,nfsvers=3 192.168.129.154:/home/flynnsnu/linux/nfs nfs_get/

驱动开发了解下_第12张图片

驱动加载

root@ATK-IMX6U:~/nfs_get/02_led# modprobe led.ko
modprobe: FATAL: Module led.ko not found in directory /lib/modules/4.1.15-g06f53e4

驱动开发了解下_第13张图片
mknod /dev/led c 200 0
驱动开发了解下_第14张图片
驱动程序

if(ledstat == LEDON) {	
		led_switch(LEDON);		/* 打开LED灯 */
		printk("on\r\n");
	} else if(ledstat == LEDOFF) {
		led_switch(LEDOFF);	/* 关闭LED灯 */
		printk("off\r\n");
	}

这里当初测试时是加载了正点原子出厂内核,led灯被用作呼吸灯,使用没有现象。

在这里插入图片描述

加载测试led驱动

/lib/modules/4.1.15 # ls
chrdevbase.ko    led.ko           modules.alias    modules.symbols
chrdevbaseApp    ledApp           modules.dep
/lib/modules/4.1.15 # modprobe led.ko
modprobe: module led.ko not found in modules.dep
/lib/modules/4.1.15 # depmod
/lib/modules/4.1.15 # ls
chrdevbase.ko    led.ko           modules.alias    modules.symbols
chrdevbaseApp    ledApp           modules.dep
/lib/modules/4.1.15 # modprobe led.ko
/lib/modules/4.1.15 # lsmod
Module                  Size  Used by    Tainted: G
led                     1838  0
/lib/modules/4.1.15 # cat /proc/devices
Character devices:
  1 mem
  4 /dev/vc/0
  4 tty
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 29 fb
 81 video4linux
 89 i2c
 90 mtd
116 alsa
128 ptm
136 pts
180 usb
189 usb_device
200 led
207 ttymxc
226 drm
250 ttyLP
251 watchdog
252 ptp
253 pps
254 rtc

Block devices:
  1 ramdisk
259 blkext
  7 loop
  8 sd
 31 mtdblock
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc
/lib/modules/4.1.15 # mknod /dev/led c 200 0
/lib/modules/4.1.15 # ls /dev/led -l
crw-r--r--    1 0        0         200,   0 Apr  3 17:09 /dev/led
/lib/modules/4.1.15 # ./ledApp /dev/led 1
on
/lib/modules/4.1.15 # ./ledApp /dev/led 0
off
/lib/modules/4.1.15 # remod led.ko
-/bin/sh: remod: not found
/lib/modules/4.1.15 # rmmod led.ko
/lib/modules/4.1.15 #

就写到这,大致流程就是这样,后面就是驱动框架的搭建了。

你可能感兴趣的:(linux驱动篇,linux,驱动开发,c语言)