Implementation of Embedded OS Lab3 Linux Kernel Modules.
-
Upload
margaret-malone -
Category
Documents
-
view
224 -
download
3
Transcript of Implementation of Embedded OS Lab3 Linux Kernel Modules.
Implementation of Embedded OS
Lab3 Linux Kernel Modules
/ 132 Lab3
Goal Learn how to write Linux kernel modules.
2014/4/8* source: The Linux Device Drivers, 3rd
/ 133 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 Software
2014/4/8
/ 134 Lab3
First Step
2014/4/8
Finish lab2-kernel first. Please make sure you have modified the flash
partition in Linux and flashed the correct file system (20MB).
/ 135 Lab3
Linux Device Drivers
2014/4/8
Three major types Character device driver Block device driver Network device driver
Other types USB driver PCI driver ……
UserMode
KernelMode
Hardware
Application
Physical Device
System Call Interface
Virtual File System (VFS)
Buffer Cache
NetworkSubsystem
Character Device Driver
BlockDevice Driver
NetworkDevice Driver
Device Interface
/ 136 Lab3
A Simple Kernel Module
2014/4/8
#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
/ 137 Lab3
Makefile for Kernel Modules
2014/4/8
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 <kernel path> M=$(PWD) modules
clean:make -C <kernel path> M=$(PWD) clean
/ 138 Lab3
Module Parameters
2014/4/8
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 <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);
/ 139 Lab3
Virtual File System (VFS) (1/2)
2014/4/8
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);
/ 1310 Lab3
Virtual File System (VFS) (2/2)
2014/4/8
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.
/ 1311 Lab3
An Example of the User Programs
2014/4/8
Please use the cross compiler to compile this program.#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;}
/ 1312 Lab3
Lab Requirements
2014/4/8
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.
/ 1313 Lab3
References
2014/4/8
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