How to Compile driver on Board

From BananaPro/Pi
Jump to: navigation, search

Other languages:
English • ‎中文(简体)‎

Make Linux Kernel Headers

Before we make the Linux kernel headers, install the tool and library:

apt-get install build-essential libncurses5-dev

Get the Linux Kernel Source

git clone https://github.com/LeMaker/linux-actions.git

Make .config File

mkdir -p /usr/src/linux-headers-$(uname -r)
cd linux-actions 
make O=/usr/src/linux-headers-$(uname -r) atm705a_s500_defconfig

Redirected the intermediate files(.config file included) to the directory "O" specified.

Make Linux Kernel Headers

cd linux-actions
make O=/usr/src/linux-headers-$(uname -r) modules_prepare

modules_prepare - Set up for building external modules
Before you use the linux kernel headers, creat a soft link named "build" and point it to /usr/src/linux-headers-$(uname -r).
Lnbuild.png

Remove the soft link named "source" in directory /usr/src/linux-headers-$(uname -r), and finally:
Module.symvers.png

Make drivers On Board

To compile the drivers on board, you need to point the kernel source path to Linux Kernel Headers.

Create a Test Driver

Create a test driver named helloworld for example:
Leds-driver.png
helloworld.c: The test driver source code.

#include<linux/init.h>           
#include<linux/module.h>     
static int __init hello_init(void) 
{
     printk(KERN_WARNING "hello,world.\n");   
     return 0;
}
 
static void __exit hello_exit(void) 
{
     printk(KERN_WARNING "goodbye world\n");
 
}
 
module_init(hello_init);  
module_exit(hello_exit); 
MODULE_LICENSE("GPL");   
MODULE_AUTHOR("lemaker");   
MODULE_DESCRIPTION("Just for test");

Makefile:

KDIR := /lib/modules/`uname -r`/build # Point to Linux Kernel Headers
PWD := $(shell pwd)
 
obj-m := helloworld.o
 
default:
        make -C $(KDIR) M=$(PWD) modules
 
clean:
        rm -rf *.o *.cmd *.ko *.mod.c *.tmp_versions *.order *.symvers

Compile the driver

# cd test
# make

Run the command and the *.ko file will be output, for example:
Koout.png