Lineo: Introduction to Real-Time Linux ... Day 1 Table of Contents 1 Introduction
Total Page:16
File Type:pdf, Size:1020Kb
Lineo: Introduction to Real-time Linux ... Day 1 Table of Contents 1 Introduction . 1 1.1 Introduction . 1 1.2 Is Linux Real-time ? . 1 1.3 The Solution . 1 1.4 Course Outline . 3 1.4.1 Code Examples . 3 1.4.2 Course Project . 3 1.5 Class Network Setup . 4 2 Kernel Module Programming . 6 2.1 Kernel Modules, Introduction . 6 2.2 Kernel Modules . 6 2.3 Module Basics . 7 2.4 Module Basics Version 2.4 . 7 2.5 Module Compilation . 7 2.6 Module Symbols . 8 2.7 Module Versions . 8 2.8 Module Parameters . 8 2.9 Simple Kernel Module Lab . 9 3 Device Driver Programming . 10 3.1 Device Drivers, Introduction . 10 3.2 Device Drivers Picture . 11 3.3 The /dev directory . 11 3.4 Major Numbers . 12 3.4.1 Table 1 . 12 3.5 Minor Numbers . 13 3.6 Driver Private Data . 13 3.7 Device Classes . 14 3.8 Registering a Device . 14 3.9 File Operations Structure (fops) . 15 3.10 Transferring Data . 16 4 Example Device Drivers . 17 4.1 Simple Char Mode Device Driver . 17 4.2 Lab: Simple Char Mode Device Driver . 18 4.3 More Complex Char Device Driver . 19 4.4 Lab: More Complex Char Device Driver . 20 4.5 Blocking I/O and Wait Queues . 21 4.6 Blocking I/O Code Example . 22 5 Device Driver I/O . 23 5.1 I/O Port API . 23 5.2 Single Transfers . 23 5.3 Block Transfers . 23 6 IRQs . 25 6.1 Interrupt Handlers . 25 6.2 Interrupt Handler Arguments . 25 6.2.1 Interrupt Setup . 25 6.2.2Lineo Interrupt Service Education. Services. 25 - i - 6.3 Deferred Interrupt Processing: Bottom Halves and Task Queues . 26 7 DMA and Memory . 27 7.1 Memory in General . 27 7.2 Addressing Memory . 28 7.3 I/O Memory . 28 7.4 DMA . 30 7.5 Allocating Memory for DMA . 30 7.6 DMA virtual and physical memory . 30 7.7 DMA Programming calls . 31 7.8 DMA Example . 31 8 RTAI . 32 8.1 RTAI Features . 32 8.2 RTAI Overview . 33 8.3 Architecture. The RTHAL . 34 8.4 RTAI Installation under RH 6.2 . 35 8.5 RTAI Installation under RH 6.2 part 2 . 37 8.6 RTAI Installation under RH 7.1 (Kernel Upgrade) . 38 8.7 RTAI Installation under RH 7.1 part 2 . 40 8.8 RTAI Install Lab . 40 8.9 Lab: Install RTAI from Lineo V3.0 CD (2.2.x) . 41 8.10 Install RTAI II . 42 8.11 Install RTAI III (2.2.x) . 42 8.12 Build RTAI . 43 9 Real-Time Scheduling . 44 9.1 Task Structure . 44 9.2 RTAI Scheduling . 45 9.3 One-shot vs. Periodic . 46 9.4 RTAI Scheduling . 46 9.5 RTAI Scheduling API . 46 9.6 Real-Time Timer Functions . 48 10 PWM Servo Controller Project . 49 10.1 Signal Generation with a Simple Real-time Task . 49 10.2 Exercise: Basic Controller . 49 10.2.1 Assignment . 49 10.2.2 Tips . 49 10.2.3 Basic RT Module Example . 50 10.3 Lab: Basic Servo Driver . 53 10.3.1 Basic RT Module Example Answer . 53 10.3.2 Basic RT Module Example Notes . 55 Lineo Education Services - ii - Lineo: Introduction to Real-time Linux ... Day 1 1 Introduction 1 Introduction 1.1 Introduction Real-time systems are concerned with when things happen as well as what happens. A real-time program must not only produce a correct answer, it must do so "on time." A late answer is a wrong answer. Real-time systems can be further characterized as either "soft" real-time or "hard" real-time. A hard real-time system absolutely must meet its scheduling deadline every time. Failure to meet the deadline may have catastrophic consequences, including loss of life. A fly-by-wire aircraft control system is an example of hard real-time. The control algorithms depend on regular sampling intervals. If sampling is delayed, the algorithm can become unstable. A soft real-time system may cause cosmetic or annoying disturbances, but in general it will be more tolerant of missed deadlines. A video playback system may not look good when missing deadlines, but it should not cause serious damage to a process. 1.2 Is Linux Real-time ? For these several reasons, standard Linux isn’t capable of a real-time response: Coarse-grained synchronization. Paging. The scheduler tries to be "fair." Linux reorders requests from multiple processes to make it more efficient. Linux will perform "batch" or "queued" operations to make more efficient use of hardware. 1.3 The Solution Whenever Linux tries to disable interrupts, the real-time kernel intercepts the request,Lineo records it. and returnsEducation to Linux. Services - 1 - 1.3 The Solution But, it does not disable interrupts! When a hardware interrupt occurs, the real-time kernel first determines where it is directed: Real-Time Task. Then schedule the task. Linux. Check the software interrupt flag: If enabled, invoke the appropriate Linux interrupt handler. If disabled, note that the interrupt occurred, and deliver it when Linux re-enables interrupts. Lineo Education Services - 2 - Lineo: Introduction to Real-time Linux ... Day 1 1.4 Course Outline 1.4 Course Outline This outline is not necessarily in the order of presentation. Introduction Kernel Modules Device Drivers Interrupts, DMA, and I/O Ports RTAI FIFOs Scheduling Memory Allocation and Shared Memory Inter-process Communication LXRT Debugging in User Space The /proc File System POSIX The Linux Trace Toolkit (LTT) 1.4.1 Code Examples Code examples are found on the /mnt/train/code/ClassLabs directory. Under this main directory you will find a subdirectory for each project. 1.4.2 Course Project A program to control a small Radio Control (RC) Servo will be used as a project during the course. This unit plugs in to the parallel port outlet on the PC and will drive one or more small RC Servos, a 5 volt LED. It will also allow an interrupt to be tied back into the PC via the parallel port. Lineo Education Services - 3 - 1.5 Class Network Setup 1.5 Class Network Setup This is the set up for the class network. If DHCP is used the instructor will have to ask you for your IP Address if you need help. You can get your IP Address as follows: /sbin/ifconfig It is assumed that the network is running and that you can ping the class server from the your computers. ( example ) ping 192.168.1.100 ping You will then want to allow the instructor to establish a telnet session into your system. Note the use of xinetd is only for RedHad 7.1 systems and above edit /etc/xinetd.d/telnet set disable = no Reread run killall -USR1 xinetd configuration. run tail -f Watch for the login. /var/log/messages Expected results at this stage:- May 8 17:02:47 devhp1 xinetd[717]: daytime disabled, removing May 8 17:02:47 devhp1 xinetd[717]: comsat disabled, removing May 8 17:02:47 devhp1 xinetd[717]: chargen-udp disabled, removing May 8 17:02:47 devhp1 xinetd[717]: chargen disabled, removing May 8 17:02:47 devhp1 xinetd[717]: amidxtape disabled, removing May 8 17:02:47 devhp1 xinetd[717]: Reconfigured: new=1 old=0 dropped=0 (services) May 8 17:03:29 devhp1 login(pam_unix)[1154]: session opened for user philw by (uid=0) May 8 17:03:29 devhp1 -- philw[1154]: LOGIN ON pts/1 BY philw FROM 192.168.1.231 May 8 17:08:50 devhp1 su(pam_unix)[1197]: session opened for user root by philw(uid=500) Now you can attempt to mount the NFS shared directory from the class server: ( Note the "192.168.1.100" address may change ) Lineo Education Services - 4 - Lineo: Introduction to Real-time Linux ... Day 1 1.5 Class Network Setup Initial mount -t nfs 192.168.1.100:/home/train<space> /mnt/train mount request. [root@devhp1 philw]# ls -l /mnt/train Look at total 26259 directory -rw-r--r-- 1 root root 25755214 May 8 12:18 linux-2.4.3.tar.gz contents. -rw-r--r-- 1 root root 1025242 May 8 12:18 rtai-24.1.4.tgz Lineo Education Services - 5 - 2 Kernel Module Programming 2 Kernel Module Programming 2.1 Kernel Modules, Introduction Kernel modules are effectively extensions to the kernel that can be introduced into a running system. Dynamic loading and symbol look-up techniques allow this process to alter kernel operation significantly during the init_module call. The additional resources that were introduced into the kernel during module load can be removed when the module is unloaded. Any resources still in use at the unload time have to be tracked. Figure 1 : Module overview 2.2 Kernel Modules As extensions of the kernel, modules execute at Privilege Level 0 and have access to the kernel’s symbol table. These are the implications of the previous statement: A module can execute privileged instructions such as I/O. A module can call any global kernel function. A module can easily bring down the entire system. A moduleLineo that fails toEducation install cannot be removed. Services There’s no protection against anything at Privilege Level 0. - 6 - Lineo: Introduction to Real-time Linux ... Day 1 2.3 Module Basics 2.3 Module Basics These are the basic module user functions: /sbin/insmod ./hello my_string="Hello" my_int=6 Insert a module into the kernel and set parameters.