扬州西区网站建设,展览馆网站建设,做网站销售经常遇到的问题,媒体发布平台使用 Ubuntu Linux 测试 Linux 驱动 
1. 测试 Linux 驱动准备工作 
 对于一个 Linux 驱动程序#xff0c;一开始可以在 Ubuntu Linux 上做前期开发和测试。对于访问硬件部分也可以在 Ubuntu Linux 用软件进行模拟,切记不能代替真实的环境#xff01;当基本开发完成后#…使用 Ubuntu Linux 测试 Linux 驱动 
1. 测试 Linux 驱动准备工作 
 对于一个 Linux 驱动程序一开始可以在 Ubuntu Linux 上做前期开发和测试。对于访问硬件部分也可以在 Ubuntu Linux 用软件进行模拟,切记不能代替真实的环境当基本开发完成后就需要在开发板或工程样机上使 用真实的硬件进行测试当然最后还需要在最终销售的产品上进行测试。最终测试通过Linux 驱动才能算真 正开发完成。 
1.1 实现步骤 
1先测试自己 ubuntu 虚拟机的版本号 
sunubuntu:~$ uname -r2找到对应版本的内核源码编译的目录 
sunubuntu:/lib/modules$ ls
5.15.0-43-generic  5.15.0-56-generic  5.19.0-32-generic
5.15.0-53-generic  5.15.0-60-generic  5.19.0-35-genericsunubuntu:/lib/modules$ cd 5.19.0-32-generic
sunubuntu:/lib/modules/5.19.0-32-generic$ ls
build              modules.builtin.alias.bin  modules.order
initrd             modules.builtin.bin        modules.softdep
kernel             modules.builtin.modinfo    modules.symbols
modules.alias      modules.dep                modules.symbols.bin
modules.alias.bin  modules.dep.bin            vdso
modules.builtin    modules.devnamesunubuntu:/lib/modules/5.19.0-32-generic$ cd build
sunubuntu:/lib/modules/5.19.0-32-generic/build$ ls
arch    Documentation  init      Kconfig   mm              scripts   ubuntu
block   drivers        io_uring  kernel    Module.symvers  security  usr
certs   fs             ipc       lib       net             sound     virt
crypto  include        Kbuild    Makefile  samples    
3修改测试驱动程序的 Makefile 文件 
KERNEL_DIR  /lib/modules/5.19.0-32-generic/build #ubuntu 内核目录
CUR_DIR  $(shell pwd)
MYAPP  virtdev_test
all:make -C $(KERNEL_DIR) M$(CUR_DIR) modulesgcc -o $(MYAPP) $(MYAPP).c
clean:make -C $(KERNEL_DIR) M$(CUR_DIR) cleanrm -rf $(MYAPP)
obj-m  virtdev_drv.o2. 测试驱动程序 
2.1 编写驱动程序 virtdev_drv.c 
#include linux/module.h
#include linux/init.h
#include linux/kernel.h
#include linux/fs.h
#include linux/miscdevice.h
#include asm/uaccess.h
#include asm/atomic.h
// 定义设备文件名
#define DEVICE_NAME virtdev
static int atom  0;
// 参数 atom0: 多个进程可以同时打开 vritdev 设备文件
// 参数 atom 非 0同时只能有一个进程打开 virtdev 设备文件
static atomic_t int_atomic_available  ATOMIC_INIT(1);
// 原子变量值为 1
static int virtdev_open(struct inode *node, struct file *file)
{if (atom){if (!atomic_dec_and_test(int_atomic_available)){atomic_inc(int_atomic_available);return -EBUSY;}}return 0;
}static int virtdev_close(struct inode *node, struct file *file)
{if (atom){atomic_inc(int_atomic_available);}return 0;
}static struct file_operations dev_fops 
{.owner  THIS_MODULE,.open  virtdev_open,.release  virtdev_close
};static struct miscdevice misc  {.minor  MISC_DYNAMIC_MINOR,.name  DEVICE_NAME,.fops  dev_fops
};// 初始化 Linux 驱动
static int __init virtdev_init(void)
{// 建立设备文件int ret  misc_register(misc);printk(jaylen atomic%d\n, atom);printk(virtdev_init_success\n);return ret;
}// 卸载 Linux 驱动
static void __exit virtdev_exit(void)
{printk(jaylen atomic%d\n, atom);printk(jaylen virtdev_exit_success\n);// 删除设备文件misc_deregister(misc);
}// 注册初始化 Linux 驱动的函数
module_init(virtdev_init);
// 注册卸载 Linux 驱动的函数
module_exit(virtdev_exit);module_param(atom, int, S_IRUGO | S_IWUSR);
MODULE_LICENSE(GPL); 
2.2 编写应用程序 virtdev_test.c 
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include stdlib.h
#include unistd.h
#include stdio.h
#include errno.h
int main(int argc, char *argv[])
{printf(app pid%d is running\n, getpid());int fd  open(/dev/virtdev, O_RDWR);printf(ret:%d\n, fd);if (fd  0){printf(errno:%d\n, errno);}else{sleep(10);close(fd);}printf(app pid%d is over\n, getpid());return 0;
}2.3 编译驱动 
在 ubuntu 普通用户下完成编译驱动 
unubuntu:ubuntu_drv_test$ make
make -C /lib/modules/5.19.0-32-generic/build M/home/sun/work/ubuntu_drv_test modules
make[1]: 进入目录“/usr/src/linux-headers-5.19.0-32-generic”
warning: the compiler differs from the one used to build the kernelThe kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0You are using:           gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0CC [M]  /home/sun/work/ubuntu_drv_test/virtdev_drv.oMODPOST /home/sun/work/ubuntu_drv_test/Module.symversCC [M]  /home/sun/work/ubuntu_drv_test/virtdev_drv.mod.oLD [M]  /home/sun/work/ubuntu_drv_test/virtdev_drv.koBTF [M] /home/sun/work/ubuntu_drv_test/virtdev_drv.ko
Skipping BTF generation for /home/sun/work/ubuntu_drv_test/virtdev_drv.ko due to unavailability of vmlinux
make[1]: 离开目录“/usr/src/linux-headers-5.19.0-32-generic”
gcc -o virtdev_test virtdev_test.c1.2.4 测试驱动 
在 ubuntu 下需要切换到管理员用户下才能安装测试驱动 
sunubuntu:ubuntu_drv_test$ sudo su
rootubuntu:/home/sun/work/ubuntu_drv_test# ls
hello_test     Module.symvers  virtdev_drv.mod    virtdev_drv.o
Makefile       virtdev_drv.c   virtdev_drv.mod.c  virtdev_test
modules.order  virtdev_drv.ko  virtdev_drv.mod.o  virtdev_test.c 
安装驱动 
rootubuntu:/home/sun/work/ubuntu_drv_test# insmod virtdev_drv.ko 传参加载驱动 
rootubuntu:/home/sun/work/ubuntu_drv_test# insmod virtdev_drv.ko atom1打印并清空缓冲区日志 dmesg -c, 以下是测试过程 
驱动加载过程 
rootubuntu:/home/sun/work/ubuntu_drv_test# dmesg
[25849.067381] robin atomic0
[25849.067387] robin virtdev_exit_success
[25867.980387] robin atomic1
[25867.980395] virtdev_init_successrootubuntu:/home/sun/work/ubuntu_drv_test# lsmod
Module                  Size  Used by
virtdev_drv            16384  1rootubuntu:/home/sun/work/ubuntu_drv_test# ls /dev/virtdev -al
crw------- 1 root root 10, 121  3月 11 22:14 /dev/virtdev 
用户测试 
sunubuntu:ubuntu_drv_test$ sudo ./virtdev_test 
app pid43934 is running
ret:3
app pid43934 is over 
驱动卸载 
rootubuntu:/home/sun/work/ubuntu_drv_test# rmmod virtdev_drv
rootubuntu:/home/sun/work/ubuntu_drv_test# dmesg 
[25849.067381] robin atomic0
[25849.067387] robin virtdev_exit_success
[25867.980387] robin atomic1
[25867.980395] virtdev_init_success
[26054.089448] robin atomic1
[26054.089456] robin virtdev_exit_success 
3 Linux 内核日志查看之 dmesg 命令简介 
Linux 内核启动时会加载硬件驱动在有新硬件时也会加载驱动如果想要查看内核的活动可以使用 dmesg 命令。Linux 内核日志存储在一个 ring-buffer 中它的大小是固定的当队列满时新的消息会覆盖掉最旧的消 息。实际上在 boot 阶段所有的应用还没有启动syslogd 也未启动这时内核日志是非常重要的信息。 除了设备初始化日志、内核模块日志它还会包含一些应用崩溃的相关信息记录了解 dmesg 的使用对于调 试程序相当重要。 
对于嵌入式设备的调试它会比较清楚地展现当前的 log 信息。 dmesg -c 显示并清除当前的日志内容。 下次再 dmesg 时就没有以前的日志了。 文章转载自: http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.wknbc.cn.gov.cn.wknbc.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.mjqms.cn.gov.cn.mjqms.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.rccpl.cn.gov.cn.rccpl.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.wanjia-sd.com.gov.cn.wanjia-sd.com http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.grbp.cn.gov.cn.grbp.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.nkpml.cn.gov.cn.nkpml.cn http://www.morning.wjpsn.cn.gov.cn.wjpsn.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.dljujia.com.gov.cn.dljujia.com http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.gfkb.cn.gov.cn.gfkb.cn http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.jrpmf.cn.gov.cn.jrpmf.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.gsjfn.cn.gov.cn.gsjfn.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.zbkwj.cn.gov.cn.zbkwj.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.ftmly.cn.gov.cn.ftmly.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.qnbzs.cn.gov.cn.qnbzs.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.kszkm.cn.gov.cn.kszkm.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.qhczg.cn.gov.cn.qhczg.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.fkyqt.cn.gov.cn.fkyqt.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com http://www.morning.mmynk.cn.gov.cn.mmynk.cn http://www.morning.pwwjs.cn.gov.cn.pwwjs.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn