Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer System Laboratory

Similar presentations


Presentation on theme: "Computer System Laboratory"— Presentation transcript:

1 Computer System Laboratory
Lab10 Linux Drivers

2 Experimental Goal Understand the architecture of Linux drivers and learn how to control LCD LCD Lab8 2019/4/14

3 Environment Host System Build System Target System Software
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 2019/4/14

4 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 2019/4/14

5 Linux Device Drivers Why learning Linux driver model? Why Linux?
The rate at which new hardware becomes available (and obsolete!) pguarantees that driver writers will be busy for the foreseeable future Why Linux? Linux shares the biggest market for embedded systems Lab10 2019/4/14

6 Linux Device Drivers (Cont.)
Three fundamental types: Character device driver Block device driver Network device driver Other types: USB driver PCI driver …… Lab10 2019/4/14

7 Writing Linux Drivers A Linux device driver can be divided into two drivers Virtual device driver Physical device driver filename description type major number minor number /dev/ttyS0 First UART serial port char 4 64 User Device File Kernel 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 Lab10 2019/4/14

8 Linking Modules to Linux
Lab10 2019/4/14

9 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 2019/4/14

10 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 2019/4/14

11 Writing Driver - Virtual Device Driver
Define file_operations and implement system calls Register driver (character device) 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) Lab10 2019/4/14

12 Writing Driver - Physical Device Driver
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 0x We can read and write the I/O port like this: #define DATA_PORT (*(volatile char*)(0x10000)) char read_b() { return DATA_PORT; } void write_b(char data) { DATA_PORT = data; } Lab10 2019/4/14

13 Writing Driver - Physical Device Driver (Cont.)
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 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); Lab10 2019/4/14

14 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 /dev directory We can create device file by the following command: % mknod /dev/demo c 60 0 More details about mknod can be found at: Lab10 2019/4/14

15 Using Your Driver in Applications
Open the file /dev/demo and test its read and write functions just like a normal file #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); Lab10 2019/4/14

16 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 2019/4/14

17 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 2019/4/14

18 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 2019/4/14

19 Bonus On PXA270, there is a 4x4 keypad device
In Lab8, we port a tetris to 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, keypad LCD 有 15*15 的大小 Lab10 2019/4/14

20 Hints 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”, 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 struct file_operations creator_pxa270_lcdtxt_fops = { ... write: creator_pxa270_lcdtxt_write, poll: creator_pxa270_lcdtxt_poll, Lab10 2019/4/14


Download ppt "Computer System Laboratory"

Similar presentations


Ads by Google