您的位置:首页 > 运维架构 > Linux

linux驱动入门-hello设备驱动程序编写

2016-07-31 11:54 621 查看

linux驱动入门-hello设备驱动程序编写

创建一个hello.c文件

[luxibao@centos ~]$ mkdir hello

[luxibao@centos ~]$ ls

dropbear-0.53.1          fl2440  公共的  视频  文档  音乐

dropbear-0.53.1.tar.bz2  hello   模板    图片  下载  桌面

[luxibao@centos ~]$ cd hello/

[luxibao@centos hello]$ ls

[luxibao@centos hello]$ vim hello.c

 

 

 

/******************************************************************************

 

}

MODULE_LICENSE("Dual BSD/GPL");

 *      Copyright: (C) 2016 luxibao<864809344@qq.com>

 *              All rights reserved.

 *

 *      Filename: hello.c

 *      Description: This file

 *

 *      Version: 1.0.0(2016年07月26日)

 *      Author: luxibao <864809344@qq.com>

 *      ChangeLog: 1, Release initial version on "2016年7月26日10时8分00秒"

 *

 *

 *******************************************************************************/

 

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

 

 

static int hello_init(void)

{

        printk(KERN_ALERT "Hello, luxibao\n");

        return 0;

}

 

 

 

static void hello_exit(void)

{

        printk(KERN_ALERT "Goodbye, luxibao\n");

}

 

 

module_init(hello_init);

module_exit(hello_exit);

 

这个模块定义了两个函数,其中一个在模块被装载打到内核是调用(hello——init),而另一个则是在模块被移除时调用(hello_exit)。

特殊宏(MODULE_LICENSE)用来告诉内核,该模块采取自由许可证,如果没有这样的声明,内核在装载该模块时会产生抱怨。

[luxibao@centos hello]$ ls

hello.c

Hello 驱动的Makefile编写:

适用于Linux操作系统:

在hello.c同一级目录下创建makefile

[luxibao@centos hello]$ vim makefile

 

obj-m=hello.o

modules:

        make -C /lib/modules/`uname -r`/build/ M=`pwd` modules

        make clean

 

clean:

        rm -f *.o *.mod.c *.order *.symvers

 

[luxibao@centos hello]$ ls

hello.c  makefile

[luxibao@centos hello]$ make

[luxibao@centos hello]$ make

make -C /lib/modules/`uname -r`/build/ M=`pwd` modules

make[1]: Entering directory `/usr/src/kernels/2.6.32-573.el6.x86_64'

  CC [M]  /home/luxibao/hello/hello.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /home/luxibao/hello/hello.mod.o

  LD [M]  /home/luxibao/hello/hello.ko.unsigned

  NO SIGN [M] /home/luxibao/hello/hello.ko

make[1]: Leaving directory `/usr/src/kernels/2.6.32-573.el6.x86_64'

make clean

make[1]: Entering directory `/home/luxibao/hello'

rm -f *.o *.mod.c *.order *.symvers


make[1]: Leaving directory `/home/luxibao/hello'

 

[luxibao@centos hello]$ ls

hello.c  hello.ko  hello.ko.unsigned  Makefile

[luxibao@centos hello]$ sudo insmod ./hello.ko

[luxibao@centos hello]$ dmesg

.....................................

SELinux: initialized (dev mqueue, type mqueue), uses transition SIDs

lo: Disabled Privacy Extensions

SELinux: initialized (dev proc, type proc), uses genfs_contexts

fuse init (API version 7.14)

psmouse.c: Wheel Mouse at isa0060/serio1/input0 lost synchronization, throwing 2 bytes away.

psmouse.c: resync failed, issuing reconnect request

Hello, luxibao

 

[luxibao@centos hello]$ sudo rmmod ./hello.ko

[luxibao@centos hello]$ dmesg

........................................

SELinux: initialized (dev proc, type proc), uses genfs_contexts

fuse init (API version 7.14)

psmouse.c: Wheel Mouse at isa0060/serio1/input0 lost synchronization, throwing 2 bytes away.

psmouse.c: resync failed, issuing reconnect request

Hello, luxibao

Goodbye, luxibao

 

[luxibao@centos hello]$ lsmod

 

[luxibao@centos hello]$ dmesg

SELinux: initialized (dev mqueue, type mqueue), uses transition SIDs

SELinux: initialized (dev proc, type proc), uses genfs_contexts

SELinux: initialized (dev mqueue, type mqueue), uses transition SIDs

lo: Disabled Privacy Extensions

SELinux: initialized (dev proc, type proc), uses genfs_contexts

fuse init (API version 7.14)

psmouse.c: Wheel Mouse at isa0060/serio1/input0 lost synchronization, throwing 2 bytes away.

psmouse.c: resync failed, issuing reconnect request

Hello, luxibao

Goodbye, luxibao

适用于fl2440开发板:

[luxibao@centos ~]$ mkdir hello.1

[luxibao@centos hello]$ ls

hello.c  hello.ko  hello.ko.unsigned  Makefile

[luxibao@centos hello]$ sz hello.c

rz

Starting zmodem transfer.  Press Ctrl+C to cancel.

Transferring hello.c...

  100%     748 bytes  748 bytes/s 00:00:01       0 Errors

 

[luxibao@centos hello]$

[luxibao@centos hello]$ ls

hello.c  hello.ko  hello.ko.unsigned  Makefile

[luxibao@centos hello]$ cd ../

[luxibao@centos ~]$ cd hello.1

[luxibao@centos hello.1]$ ls

Makefile

[luxibao@centos hello.1]$ rz

rz waiting to receive.

Starting zmodem transfer.  Press Ctrl+C to cancel.

Transferring hello.c...

  100%     748 bytes  748 bytes/s 00:00:01       0 Errors

 

 

[luxibao@centos hello.1]$ vim Makefile

 

C=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc

KDIR?=/home/luxibao/fl2440/kernel/linux-3.0

obj-m:=hello.o

 

default:

        $(MAKE) -C $(KDIR) M=`pwd` modules

        make clean

 

clean:

        rm -f *.o *mod.c *.order *.symvers

 

[luxibao@centos hello.1]$ ls

hello.c  Makefile

[luxibao@centos hello.1]$ make

make -C /home/luxibao/fl2440/kernel/linux-3.0 M=`pwd` modules

make[1]: Entering directory `/home/luxibao/fl2440/kernel/linux-3.0'

  CC [M]  /home/luxibao/hello.1/hello.o

  Building modules, stage 2.

  MODPOST 1 modules

  CC      /home/luxibao/hello.1/hello.mod.o

  LD [M]  /home/luxibao/hello.1/hello.ko

make[1]: Leaving directory `/home/luxibao/fl2440/kernel/linux-3.0'

make clean

make[1]: Entering directory `/home/luxibao/hello.1'

rm -f *.o *mod.c *.order *.symvers

make[1]: Leaving directory `/home/luxibao/hello.1'

[luxibao@centos hello.1]$ ls

hello.c  hello.ko  Makefile

在开发板上操作:

>: ifconfig

eth0      Link encap:Ethernet  HWaddr D6:86:38:B9:4D:CF  

          inet addr:192.168.1.111  Bcast:192.168.1.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:54 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:7236 (7.0 KiB)  TX bytes:0 (0.0 B)

          Interrupt:51 Base address:0x2300

>: tftp -gr hello.ko 192.168.1.2

hello.ko             100% |*******************************|  2134   0:00:00 ETA

>: insmod hello.ko

Hello, luxibao

>: ls /lib/

ld-uClibc-0.9.33.2.so       libnsl-0.9.33.2.so

ld-uClibc.so.0              libnsl.so.0

libc.so.0                   libpthread-0.9.33.2.so

libcrypt-0.9.33.2.so        libpthread.so.0

libcrypt.so.0               libresolv-0.9.33.2.so

libdl-0.9.33.2.so           libresolv.so.0

libdl.so.0                  librt-0.9.33.2.so

libgcc_s.so                 librt.so.0

libgcc_s.so.1               libstdc++.so

libm-0.9.33.2.so            libstdc++.so.6

libm.so.0                   libstdc++.so.6.0.14

libmudflap.so               libstdc++.so.6.0.14-gdb.py

libmudflap.so.0             libuClibc-0.9.33.2.so

libmudflap.so.0.0.0         libutil-0.9.33.2.so

libmudflapth.so             libutil.so.0

libmudflapth.so.0           modules

libmudflapth.so.0.0.0

>: dmesg

Linux version 3.0.0 (luxibao@centos.6localdomain) (gcc version 4.5.4 (Buildroot 2012.08) ) #16 Sat Jul 30 10:18:27 CST 2016

CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177

CPU: VIVT data cache, VIVT instruction cache

Machine: SMDK2440

.................

usb 1-1: new full speed USB device number 2 using s3c2410-ohci

hub 1-1:1.0: USB hub found

hub 1-1:1.0: 4 ports detected

dm9000 dm9000: eth0: link down

dm9000 dm9000: eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1

Hello, luxibao

>: lsmod

hello 628 0 - Live 0xbf000000

>: rmmod hello

Goodbye, luxibao

>: dmesg 

............................

Freeing init memory: 132K

usb 1-1: new full speed USB device number 2 using s3c2410-ohci

hub 1-1:1.0: USB hub found

hub 1-1:1.0: 4 ports detected

dm9000 dm9000: eth0: link down

dm9000 dm9000: eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1

Hello, luxibao

Goodbye, luxibao

 

遇到的问题和解决方法:

 

1.编写的Makefile程序make 报错:





解决:出去多余的?号,Tab键代替空格,检查命令是否出错,单词是否拼写错误。

 

2.忘记在hello.1下创建hello.c文件






解决:创建hello.c文件或者从hello文件夹下直接copy   hello.c过来

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息