zoukankan      html  css  js  c++  java
  • Linux驱动之HelloWorld

      最近看android的一些源码,里面有一些功能是用驱动实现的。于是就兴起看了一些驱动相关的东西,准备日后深入。这没有技术含量的水文,仅作为日后的备忘吧。

      系统使用的是ubuntu 12.0.04,内核是3.2.0。看很多别人的说法是写驱动之前先要编译内核源码树,但貌似在这个系统上是不要的,直接写C文件和Makefile文件,很快就能测试了。c文件如下:

      

     1 #include<linux/init.h>
     2 #include<linux/kernel.h>
     3 #include<linux/module.h>
     4 
     5 MODULE_LICENSE("zhongjinwen BSD/GPL");
     6 static int hello_init(void)
     7 {
     8     printk(KERN_ALERT"Hello,world
    ");
     9     return 0;
    10 }
    11 
    12 static void hello_exit(void)
    13 {
    14     printk(KERN_ALERT"Goodbye,Hello world
    ");
    15 }
    16 module_init(hello_init);
    17 module_exit(hello_exit);

    Makefile文件:

    obj-m:=hello.o
    KERNELDIR:=/lib/modules/$(shell uname -r)/build
    all:
        make -C $(KERNELDIR) M=$(shell pwd) modules
    clean:
        rm -rf *.o *.order *.mod.c *.ko *.symvers

      终端输入make,会生成相应的ko文件。

      然后sudo insmod ./hello.ko。这时应该已经加载成功,可以使用sudo lsmod命令查看是否有hello这个模块。另外,输入命令cat /var/log/syslog,看到最后面显示“Hello,World”,也说明模块加载成功。

      linux驱动是一块比较难的内容,好好加油吧。

  • 相关阅读:
    ‎CocosBuilder 学习笔记(2) ccbi 文件结构分析
    ‎Cocos2d-x 学习笔记(22) TableView
    ‎Cocos2d-x 学习笔记(21.1) ScrollView “甩出”效果与 deaccelerateScrolling 方法
    ‎Cocos2d-x 学习笔记(21) ScrollView (CCScrollView)
    pkg-config
    变量定义
    perror 与 strerror
    popen and system
    exit
    uint8_t
  • 原文地址:https://www.cnblogs.com/zhizhizhiyuan/p/3645983.html
Copyright © 2011-2022 走看看