Implementation of embedded os
1 / 13

Implementation of Embedded OS - PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

Implementation of Embedded OS. Lab3 Linux Kernel Modules. Goal. Learn how to write Linux kernel modules. * source: The Linux Device Drivers, 3rd. Environment. Host System Windows XP Build System VirtualBox + Ubuntu 8.04 Target System Creator XScale PXA270 Software

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Implementation of Embedded OS

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Implementation of embedded os
Implementation of Embedded OS

Lab3 Linux Kernel Modules

Implementation of embedded os

Learn how to write Linux kernel modules.

/ 13

* source: The Linux Device Drivers, 3rd


  • Host System

    • Windows XP

  • Build System

    • VirtualBox + Ubuntu 8.04

  • Target System

    • Creator XScale PXA270

  • Software

    • Drivers for Creator PXA270 LCD, 8-bit LED lamps, and 4-digit 7-segment LED.

    • You can download them from RSWiki IEOS Course Software

/ 13

First step
First Step

/ 13

  • Finish lab2-kernel first.

    • Please make sure you have modified the flash partition in Linux and flashed the correct file system (20MB).

Linux device drivers
Linux Device Drivers




System Call Interface

Virtual File System (VFS)



Buffer Cache



Character Device Driver


Device Driver


Device Driver

Device Interface

Physical Device


/ 13

  • Three major types

    • Character device driver

    • Block device driver

    • Network device driver

  • Other types

    • USB driver

    • PCI driver

    • ……

A simple kernel module
A Simple Kernel Module

#include <linux/module.h> /* Needed by all modules */

#include <linux/kernel.h> /* Needed for KERN_INFO */

#include <linux/init.h> /* Needed for the macros */

static int __init init_hello(void)


printk(KERN_INFO "Hello, world\n");

return 0;


static void __exit cleanup_hello(void)


printk(KERN_INFO "Goodbye, world\n");




MODULE_LICENSE("GPL"); /* License type of this module */

MODULE_AUTHOR("DRIVER_AUTHOR"); /* Who wrote this module */

MODULE_DESCRIPTION("DRIVER_DESC"); /* What does this module do */

initial function

cleanup function

declaration of

initial/cleanup functions

/ 13

Makefile for kernel modules
Makefile for Kernel Modules

obj-m += hello.o


make -C <kernel path>M=$(PWD) modules


make -C <kernel path>M=$(PWD) clean

/ 13

  • Suppose the filename of the module source is hello.c.

    • Write a Makefile like this:

  • Type “make” to compile the module.

  • Then transfer the output file hello.ko to the target board.

  • Type “insmod hello.ko” to load the module and “rmmod hello” to remove the module.

Module parameters
Module Parameters

/* Needed for the macro module_param */

#include <linux/moduleparam.h>

static int myint = 0;/* define a integer variable */

/* declare the variable as a module parameter */

module_param(myint, int,


/ 13

  • You can define module parameters as follows.

  • You can pass the argument when loading the module as follows.

    • $ insmod hello.komyint=123

  • Further information about module parameters can be found on the websites listed in References.

Virtual file system vfs 1 2
Virtual File System (VFS) (1/2)


register_chrdev_region(devno, MAX_MINORS, MODULE_NAME));

cdev_init(pcdev, &fops);

cdev_add(pcdev, devno, MAX_MINORS);

/ 13

  • User processes can communicate with device drivers through several ways, and VFS is the most common one.

  • A device driver can define a set of file operations including open, read, write, ioctl, etc., and associate it with an device number (inode).

Virtual file system vfs 2 2
Virtual File System (VFS) (2/2)

/ 13

  • A process can use ordinary file I/O functions to access the device.

    • First, create the corresponding file at /dev.

    • $ mknod /dev/demo c <MAJOR_NUM> <MINOR_NUM>

    • In lab2, the major number is 120 and the minor number is 0.

    • Then, open the file /dev/demo and use the function ioctl to control the device.

An example of the user programs
An Example of the User Programs

#include <unistd.h>

#include <sys/fcntl.h>

#include <sys/ioctl.h>

#include <stdio.h>

#include "creator_pxa270_lcd.h"

int main()


int fd;

if((fd = open("/dev/demo", O_WRONLY)) < 0){

perror("couldn't open /dev/demo");

return 1;


ioctl(fd, _7SEG_IOCTL_ON, 0);


return 0;


/ 13

Please use the cross compiler to compile this program.

Lab requirements
Lab Requirements

/ 13

  • Compile the kernel module.

    • Write a Makefile for the driver provided.

    • Compile the driver as a module and load it into Linux.

  • Implement a stopwatch app.

    • The resolution should be in 0.1 seconds.

    • The value of the counter should be display on the 4-digit 7-segment LED in decimal form.

    • It can be started, suspended, resumed, and reset by pressing the buttons of the keypad.

  • Please refer to the reference PDFs available on our course website if you have any questions about the driver.


/ 13

  • Linux Device Drivers, Third Edition


  • The Linux Kernel Module Programming Guide


  • Login