找回密码
 立即注册
查看: 777|回复: 0

添加自定义驱动代码示例

[复制链接]

12

主题

29

帖子

249

积分

版主

Rank: 7Rank: 7Rank: 7

积分
249

BQ3568-HM

发表于 2023-3-8 10:39:52 | 显示全部楼层 |阅读模式
本帖最后由 bearkey 于 2023-3-8 10:41 编辑

4. Develop Android Driver Sample

4.1 SimpleDriver Sample
4.1.1 Enter kernel driver dir and create hello dir and hello.c:
$ cd kernel/drivers$ mkdir hello$ vim hello.c
4.1.2 Start codingin hello.c:
       #include <linux/init.h>
       #include <linux/module.h>
       #include <linux/kernel.h>
  
       MODULE_LICENSE("Dual BSD/GPL");
   
       static int hello_init(void) {
        printk(KERN_ALERT "hello,Iam a simple driver sample\n");
       return 0;
       }
       static void hello_exit(void) {
       printk(KERN_ALERT "goodbye,kernel\n");
       }
  
       module_init(hello_init);
       module_exit(hello_exit);
  
       MODULE_AUTHOR("Beiqi");
       MODULE_DESCRIPTION("This is a simpledriver example!\n");
       MODULE_ALIAS("A simplestexample");
4.1.3 Create Makefile:
       obj-m := hello.o
       KERNELBUILD:=/lib/modules/$(shell uname -r)/build
       all:
              make -C $(KERNELBUILD)M=$(shell pwd) modules
       clean: rm -rf *.o *.ko *.mod.c.*.cmd *.markers *.order *.symvers .tmp_versions
4.1.4 Build module:$ make                                       
4.1.5 install module:$ sudo insmod hello.ko                        
4.1.5 run to see log:$ dmesg | tail -5                              
4.1.6 unstall module:$ sudo rmmod hello.ko
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则