Linux Device Drivers and Controlling LCD
E N D
Presentation Transcript
Computer System Laboratory Lab10 Linux Drivers
Experimental Goal LCD Understand the architecture of Linux drivers and learn how to control LCD Lab8
Environment • Host System • Windows XP/Ubuntu 8.04 • Build System • Ubuntu 8.04 • Target System • XScale PXA270 • Software • Linux kernel, please refer to Lab5 • BusyBox, please refer to Lab6 • Creator PXA270 LCD driver, please refer to Lab9 • Also include 8-bit LED lamps, 4-digit 7 segment LED and keypad drivers • You can download all the source codes from • RSWiki CSL Course Software Lab10
Device Drivers • What are the device drivers? • Make a particular piece of hardware respond to a well-defined internal programming interface • Hide completely the details of how the device works • User activities are performed by means of a set of standardized calls that are independent of the specific driver; mapping those calls to device-specific operations that act on real hardware is then the role of the device driver Lab10
Linux Device Drivers • Why learning Linux driver model? • The rate at which new hardware becomes available (and obsolete!) pguaranteesthat driver writers will be busy for the foreseeable future • Why Linux? • Linux shares the biggest market for embedded systems Lab10
Linux Device Drivers (Cont.) Lab10 • Three fundamental types: • Character device driver • Block device driver • Network device driver • Other types: • USB driver • PCI driver • ……
Writing Linux Drivers User Kernel Device File Device Driver define file_operations register driver (VFS) Virtual Device Driver Hardware implement system calls define chipset header define I/O wrapper functions Physical Device Driver implement chipset control functions • A Linux device driver can be divided into two drivers • Virtual device driver • Physical device driver Lab10
Linking Modules to Linux Lab10
How to Write a Module? • A “hello world” example • Note that the messages in printk will be showed in the kernel, you can use dmesg command to check the messages Lab10
Related Commands in Linux • insmod module.ko • This command is used to insert a module into the kernel • The command is like ld, in that it links any unresolved symbol in the module to the symbol table of the running kernel • Unlike the linker, however, it doesn’t modify the module’s disk file, but rather than in-memory copy • rmmod module_name • This command is used to remove a module from the kernel • The command invokes the delete_module() system call, which calls cleanup_module() in the module itself if the usage count is zero or returns an error otherwise • lsmod • List the module currently linked to Linux Lab10
Writing Driver - Virtual Device Driver struct file_operations dev_fops = { open: dev_open, read: dev_read write: dev_write, release: dev_release, ioctl: dev_ioctl, }; • int register_chrdev(unsigned int major, const char *name, struct file_operations *fops) • Define file_operations and implement system calls • Register driver (character device) Lab10
Writing Driver - Physical Device Driver #define DATA_PORT (*(volatile char*)(0x10000)) char read_b() { return DATA_PORT; } void write_b(char data) { DATA_PORT = data; } • Hardware dependent • Two common approaches to access hardware, i.e. memory mapped I/O and port I/O • Memory mapped I/O: • Access I/O (port) in the same way as memory is accessed • For example, suppose there is a device that has a 8 bit I/O port connected to the system, and its address is mapped at 0x10000. We can read and write the I/O port like this: Lab10
Writing Driver - Physical Device Driver (Cont.) if (check_region(0x378, 1)) { printk("<1>parallelport: cannot reserve 0x378\n"); } else { request_region(0x378, 1, "demo"); } char data=inb(0x378);//Read(in) data form the port 0x378 outb(data, 0x378); //Write(out) data to the port 0x378 release_region(0x378, 1); • Port I/O: • If the I/O system has its own address independent of memory, then it is port I/O • Register the I/O port (e.g. 0x378) • Use the I/O commands • Release I/O port Lab10
Creating Device File • In UNIX and Linux, devices are accessed in the same way as files are accessed • These device files are normally in the /devdirectory • We can create device file by the following command: • % mknod /dev/demo c 60 0 • More details about mknod can be found at: • http://linux.vbird.org/linux_basic/0230filesystem.php#mknod • http://rswiki.csie.org/dokuwiki/_media/courses:100_2:lab10_doc.pdf Lab10
Using Your Driver in Applications #include <stdio.h> int main() { char buf[512]; FILE *fp = fopen("/dev/demo", "w+"); if(fp == NULL) { printf("can't open device!\n"); return 0; } fread(buf, sizeof(buf), 1, fp); fwrite(buf, sizeof(buf), 1, fp); fclose(fp); return 0; } • Open the file /dev/demoand test its read and write functions just like a normal file Lab10
Lab Steps • Please download the driver of Lab9 and modify the LCD driver • Change the major number to 119 • e.g. #define MAJOR_NUM 119 • Change creator-pxa270 driver to module mode in menuconfig • <M> Creator-pxa270 LCD • Implement a new ioctl command to show “hello world” on LCD • Please add lsmod, insmod, rmmod commands in busybox • The commands are under Linux Module Utilities • You need to check the support of 2.6.x Linux Kernels • Do not forget to create the new LCD device file • % mknod /dev/lcd c 119 0 • Please write an application to display string on the LCD Panel, where the string is a command-line argument of the application Lab10
Lab Steps (Cont.) • Please print messages in driver modules when you insert and remove them • Please print messages in driver commands when you access the device • e.g. “open command is issued.” Lab10
Lab Requirement • Show more than one messages on LCD from applications • Show the messages of using the new ioctl command on LCD • Show the messages of accessing the device • Including open, close, write, ioctl, etc. Lab10
Bonus keypad • On PXA270, there is a 4x4 keypad device • In Lab8, we port a tetristo PXA270. Now, we would like to use the keypad to play the tetris • Please modify the driver and tetris to support the keypad • The keypad driver is included in the LCD driver • Please show the number of the key pressed on LCD • For more information about hardware registers, please refer to Create Creator PreSOCes Development Kit User’s Guide, 3.7.4 Lab10
Hints struct file_operations creator_pxa270_lcdtxt_fops = { ... write: creator_pxa270_lcdtxt_write, poll: creator_pxa270_lcdtxt_poll, ... • We can use ioctl() to read data from the keypad in applications • In the LCD driver, you can add poll file operation to support the select function used in the tetris, e.g. • For more information about poll, please refer to Linux Device Drivers 3rd, chapter 6, section “poll and select”, http://lwn.net/images/pdf/LDD3/ • In the tetris, there are three functions related to input from keyboard • tetris/src/input/inp_unixterm.c • readchr(), waitinput_stdin(), init_keybd() • We can replace the keyboard with keypad by modifying the 3 functions above Lab10