Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementation of Embedded OS Lab3 Linux Kernel Modules.

Similar presentations


Presentation on theme: "Implementation of Embedded OS Lab3 Linux Kernel Modules."— Presentation transcript:

1 Implementation of Embedded OS Lab3 Linux Kernel Modules

2 Lab3 Goal  Learn how to write Linux kernel modules. 2014/4/8 / 132 * source: The Linux Device Drivers, 3rd

3 Lab3 Environment  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 SoftwareRSWiki IEOS Course Software 2014/4/8 / 133

4 Lab3 First Step 2014/4/8 / 134  Finish lab2-kernel first.  Please make sure you have modified the flash partition in Linux and flashed the correct file system (20MB).

5 Lab3 Linux Device Drivers 2014/4/8 / 135  Three major types  Character device driver  Block device driver  Network device driver  Other types  USB driver  PCI driver  …… User Mode Kernel Mode Hardware Application Physical Device System Call Interface Virtual File System (VFS) Buffer Cache Network Subsystem Network Subsystem Character Device Driver Block Device Driver Block Device Driver Network Device Driver Network Device Driver Device Interface

6 Lab3 A Simple Kernel Module 2014/4/8 / 136 #include /* Needed by all modules */ #include /* Needed for KERN_INFO */ #include /* 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_init(init_hello); module_exit(cleanup_hello); 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

7 Lab3 Makefile for Kernel Modules 2014/4/8 / 137  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. obj-m += hello.o all: make -C M=$(PWD) modules clean: make -C M=$(PWD) clean

8 Lab3 Module Parameters 2014/4/8 / 138  You can define module parameters as follows.  You can pass the argument when loading the module as follows.  $ insmod hello.ko myint=123  Further information about module parameters can be found on the websites listed in References. /* Needed for the macro module_param */ #include static int myint = 0;/* define a integer variable */ /* declare the variable as a module parameter */ module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);

9 Lab3 Virtual File System (VFS) (1/2) 2014/4/8 / 139  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). devno = MKDEV(MAJOR_NUM, MINOR_NUM); register_chrdev_region(devno, MAX_MINORS, MODULE_NAME)); cdev_init(pcdev, &fops); cdev_add(pcdev, devno, MAX_MINORS);

10 Lab3 Virtual File System (VFS) (2/2) 2014/4/8 / 1310  A process can use ordinary file I/O functions to access the device.  First, create the corresponding file at /dev.  $ mknod /dev/demo c  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.

11 Lab3 An Example of the User Programs 2014/4/8 / 1311  Please use the cross compiler to compile this program. #include #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); close(fd); return 0; }

12 Lab3 Lab Requirements 2014/4/8 / 1312  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 Lab3 References 2014/4/8 / 1313  Linux Device Drivers, Third Edition  http://lwn.net/Kernel/LDD3/ http://lwn.net/Kernel/LDD3/  The Linux Kernel Module Programming Guide  http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html


Download ppt "Implementation of Embedded OS Lab3 Linux Kernel Modules."

Similar presentations


Ads by Google