树莓派 - 驱动hello

树莓派上Linux驱动,从hello world 开始 ... 

hello.c 

#include <linux/init.h>  
#include <linux/module.h>  
#include <linux/moduleparam.h>  
  
MODULE_LICENSE("Dual BSD/GPL");  
  
static int hello_init(void)  
{  
    printk(KERN_ALERT"Hello, world\n");  
    return 0;  
}  
  
static void hello_exit(void)  
{  
    printk(KERN_ALERT"Goodbye, cruel world\n");  
}  
  
module_init(hello_init);  
module_exit(hello_exit);

Makefile

ifneq ($(KERNELRELEASE),)  
  
obj-m := hello.o  
  
else  
      
KDIR := /home/xxx/Raspberry/linux 
all:  
	make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=/home/xxx/Raspberry/tools-master/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-

clean:  
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers  modul*  
  
endif 

make生成hello.ko

将hello.ko 通过sftp 传给树莓派板


在树莓派班上

$ sudo insmod hello.ko

查看:

$ dmesg | grep Hello
[ 7454.663309] Hello, world
$ lsmod |grep hello
hello                  16384  0


pi@raspberrypi:/sys/module/hello $ tree -a
.
├── coresize
├── holders
├── initsize
├── initstate
├── notes
│   └── .note.gnu.build-id
├── refcnt
├── sections
│   ├── .ARM.exidx
│   ├── .ARM.extab
│   ├── .gnu.linkonce.this_module
│   ├── __mcount_loc
│   ├── .note.gnu.build-id
│   ├── .rodata.str1.4
│   ├── .strtab
│   ├── .symtab
│   └── .text
├── srcversion
├── taint
└── uevent

3 directories, 17 files


pi@raspberrypi:~ $ sudo rmmod hello
pi@raspberrypi:~ $ dmesg | grep Goodbye
[ 8385.391257] Goodbye, cruel world

参考

http://www.cnblogs.com/xianrou/p/6327897.html


猜你喜欢

转载自blog.csdn.net/feiwatson/article/details/80724481