1 / 13

Implementation of Embedded OS

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

luther
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Implementation of Embedded OS Lab3 Linux Kernel Modules

  2. Goal Learn how to write Linux kernel modules. / 13 * source: The Linux Device Drivers, 3rd

  3. 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 Software / 13

  4. 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).

  5. Linux Device Drivers User Mode Application System Call Interface Virtual File System (VFS) Kernel Mode Buffer Cache Network Subsystem Character Device Driver Block Device Driver Network Device Driver Device Interface Physical Device Hardware / 13 • Three major types • Character device driver • Block device driver • Network device driver • Other types • USB driver • PCI driver • ……

  6. 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_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 / 13

  7. Makefile for Kernel Modules obj-m += hello.o all: make -C <kernel path>M=$(PWD) modules clean: 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.

  8. 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, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); / 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.

  9. Virtual File System (VFS) (1/2) devno = MKDEV(MAJOR_NUM, MINOR_NUM); 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).

  10. 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.

  11. 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); close(fd); return 0; } / 13 Please use the cross compiler to compile this program.

  12. 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. References / 13 • Linux Device Drivers, Third Edition • http://lwn.net/Kernel/LDD3/ • The Linux Kernel Module Programming Guide • http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html

More Related