Showing posts with label kernel module. Show all posts
Showing posts with label kernel module. Show all posts

Monday, March 17, 2014

Cross compile Raspberry Pi kernel to inslude tsc2007.ko

As tsc2007 is included in the source tree of Raspberry Pi source, this post show how to download (git clone) Raspberry Pi source and cross-compile on another host PC running Ubuntu. Setup .config to include tsc2007 module with menuconfig option in make.


Please note:


Create our working directory, raspberrypi:
  • $ mkdir raspberrypi
  • cd raspberrypi
Before start our works, install the necessary tools and source code of Raspberry Pi linux:
  • $ git clone https://github.com/raspberrypi/tools.git
  • $ git clone https://github.com/raspberrypi/linux.git
  • $ cd linux
(For default setup) Prepare the .config file from pre-packaged config, bcmrpi_cutdown_defconfig:
  • $ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- bcmrpi_cutdown_defconfig
To generate .config with tsc2007 menu, run with menuconfig option:
  • $ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- menuconfig
You can find the following code will be added in the generated .config.

CONFIG_TOUCHSCREEN_TSC2007=m



Build kernel:
  • $ make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi-
  • $ mkdir ../modules
  • $ make modules_install ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- INSTALL_MOD_PATH=../modules/
  • $ cd ../tools/mkimage/
  • $ ./imagetool-uncompressed.py ../../linux/arch/arm/boot/Image


Now insert a Raspbian installed SD Card, and run the command:
  • $ sudo rm /media/<boot-partition>/kernel.img
  • $ sudo mv kernel.img /media/<boot-partition>/
  • $ sudo rm -rf /media/<rootfs-partition>/lib/modules/
  • $ sudo rm -rf /media/<rootfs-partition>/lib/firmware/
  • $ cd ../../modules/
  • $ sudo cp -a lib/modules/ /media/<rootfs-partition>/lib/
  • $ sudo cp -a lib/firmware/ /media/<rootfs-partition>/lib/
  • $ sync



Now you can run Raspberry Pi with this new build Image. tsc2007.ko is included, but not yet run. The following steps set it run in power up. Because tsc2007 is controlled with i2c bus, so it will be enabled also.

By default, i2c is disabled in Raspbian. To run tsc2007 and i2c modules, follow the step:
  • $ sudo nano /etc/modules
    add the lines:
    i2c-bcm2708
    i2c-dev
    tsc2007

  • $ sudo nano /etc/modprobe.d/raspi-blacklist.conf
    comment the line of
    #blacklist i2c-bcm2708
  • install i2c-tools for testing
    $ sudo apt-get install i2c-tools
  • reboot
  • After reboot, run i2c-tools to test
    run the command with -1 option for rev 2 board, or -0 for rev 1 board.
    $ sudo i2cdetect -y 1
    sudo i2cdetect -y 0





You can verify tsc2007 is running by lsmod command. But I don't know any more is needed to make the touch part of "Tontec 2.4-inch TFT LCD 240x320 RGB Pixels Touch Screen Display Monitor" to work!

Thursday, January 23, 2014

Half success, half fail experiment, cross compile kernel module for Raspberry Pi on Ubuntu

This post show cross compile kernel module for Raspberry Pi on Ubuntu. It success to build kernel module, hello.ko, run on Raspberry Pi. It fail after the Pi update firmware with rpi-update!.

To make sure the source used to compile our code is same as that running on Raspberry Pi, Cross compile Raspberry Pi kernel from source on Ubuntu and move the kernel.img and files to SD Card.

In Ubuntu, create hello.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

static int __init hello_init(void)
{ 
 printk(KERN_INFO "Hello Raspberry Pi!\n");
 return 0;
}

static void __exit hello_exit(void)
{
 printk(KERN_INFO "Bye Raspberry Pi!\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile
obj-m += hello.o

all:
 make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -C /<git clone linux directory> M=$(PWD) modules

clean:
 make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- -C /<git clone linux directory> M=$(PWD) clean

Where <git clone linux directory> is the git clone linux directory in Cross compile Raspberry Pi kernel from source on Ubuntu and move the kernel.img and files to SD Card.

Then build the kernel module with the command:
$ make

Copy the generated hello.ko to Raspberry Pi.

Insert the module with the command:
$ sudo insmod hello.ko

or, remove it with the command:
$ sudo rmmod hello.ko

You can type dmesg command to check that the word "Hello Raspberry Pi!" and "Bye Raspberry Pi!" logged. They are printed to kernel by hello.ko with the function printk().


The fail half is:
Once you update Raspberry Pi's firmmware with the command:
$ sudo rpi-update

Then reboot. hello.ko cannot be inserted again in new firmware, caused with "Invalid module format"!


Remark:
- It's 3.10.27+ #3 before rpi-update, with hello.ko insert successful.


- Updated to 3.10.27+ #630 PREEMPT after rpi-update, with insmod fail.



Sunday, January 19, 2014

failed experiment: build "Hello World" kernel module for Raspberry Pi

It is a failed experiment to build "Hello World" kernel module for Raspberry Pi, follow the step:
  • Run with a new installed Raspbian, wheezy, 2014-01-07.
  • Download and transfer the source code of Raspberry Pi
  • Create hello.c and Makefile follow "The Linux Kernel Module Programming Guide", 2.3. Hello World (part 2).

    hello.c
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    
    static int __init hello_init(void)
    { 
     printk(KERN_INFO "Hello Raspberry Pi!\n");
     return 0;
    }
    
    static void __exit hello_exit(void)
    {
     printk(KERN_INFO "Bye Raspberry Pi!\n");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);
    

    Makefile
    obj-m += hello.o
    
    all:
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
    
    clean:
     make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
    

  • Then make it, without error.
  • When I try to insert the module hello.ko, error of invalid module format reported.
    $ sudo insmod ./hello.ko
    Error: could not insert module ./hello.ko: Invalid module format



  • "disagrees about version of symbol module_layout" logged in /var/log/messages'. I assume it is something mis-match between the kernel version in my source and the running system.
  • Then I update Raspberry Pi to most update with:
    $ sudo rpi-update
    $ sudo apt-get update
    $ sudo apt-get upgrade
    and reboot
  • I try to insmod again, but fail with "Unable to handle NULL pointer dereference at virtual address 00000004", and the system hang-up.
    "Unable to handle NULL pointer dereference at virtual address 00000004"
I'm a newbies in Linux programming, and ask for help. Please leave me comment for any advice.