Embedded Linux and Device Driver Lecture
Embedded Linux and Device Driver Lecture
Fall 2012
Course Description
Bootloader (i.e. Redboot, uBoot). Linux Library uclibc for microcontrollers (Kernel APIs or system calls). Understand Linux File System and RamDisk. Crossplatform GNU toolchain to compile Linux Kernel and uclibc for target.
Loadable Kernel modules (device drivers) and load/unload the Kernel modules. Char/Block/Network Device drivers and /Proc File System Drivers. Linux Library APIs (i.e. System Calls) and IOCTL calls.
Hands-On Lab
Build Embedded Linux System for Cirrus Logic ARM Microcontroller. Develop Char/Block Device Driver for RAM Filesystem. Develop /Proc File System Drivers. Cirrus Logic Microcontroller chip 9302 ARM 9 Core. 32 MByte RAM, 16 MByte Flash memory, network and serial ports. Redhat Linux Host Development System to develop and download embedded Linux/RamDisk target systems.
Fall 2012 SILICON VALLEY UNIVERSITY CONFIDENTIAL
Course Description
Class Evaluation:
30 %: Midterm. First half material. 40 %: Final. Second half material. 30 %: Homework + : Extra credit.
Lab Evaluation:
References:
Building Embedded Linux Systems 2nd Edition, OReilly Media, 2008 Materials included from Suleman Saya, UC Santa Cruz Linux Device Drivers 3rd Edition, Rubini & Corbet Materials included from Raghav Vinjamuri, UC Santa Cruz http://class.svuca.edu/~sau/CE562/ Homework Assignments LinuxDeviceDrivers3rdEdition
Fall 2012
Course Description
Fall 2012
Course Description
Fall 2012
Linux At the Beginning GNU and GNU Public License (GPL) Embedded Linux Criterias Embedded Linux Development Linux Kernel Infrastructure Cross-Platform Development
Embedded Linux Development Requirements For Host System Setup Host System For Cross-Development Setup Target Board For Cross-Development Target Board Boot Process Target Board / Host System Communication RAM Disk
Fall 2012
Embedded Linux Course Outline Section 3 Embedded Linux ToolChain ARM Tool-Chain
Binutils Kernel Headers Cross Compiler First / Second Stage Glibc Library
Linux Source Directory Hierarchy Root File System Inodes Virtual File System
Fall 2012
Kernel Modules Module Organization Module Installation (Insmod) and Removal (rmmod) Linux Device Drivers Character Drivers Block Drivers Major and Minor Number Process Fundamentals Process States Process Scheduling Preemptive / Cooperative Multitasking Scheduling Policy Process Classification Interactive Processes Batch Processes
SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
Process VS Threads
Thread Management
User Level Threads Kernel Level Threads Clone() / Fork() / VFork() Threads
Linux Interrupt Handlers Fast Interrupt Handlers Slow Interrupt Handlers Top Half Handlers Bottom Half Handlers Tasklets Workqueues
Fall 2012
Fall 2012
10
1991 University Of Helsinki in Finland by Linus B. Torvalds. Originally Minux, alternative to DOS. Minux developed by Andrew Tanenbaum to teach Unix. Minux supported protected mode. Minux rewrote for additional funtionality and features as Linux. Currently all code in Minux has been replaced. Minux file system format supported under Linux. Core is the Virtual File System (VFS). Linux built with GNU compiler and library. Released on 10/1991. Linux is POSIX compliant. Compatiable with Unix System V. GNU utilities and tools (compiler, assembler, loader, etc) are freeware. GNU Public License (GPL) specifies any modifications to Linux has to be freely published. Linux is open source because of GNU utilities and tools.
SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
11
Computer system integrated into a larger applicances that is not a computer (i.e. oven, gas station pump, ATM machine) or small devices (i.e. cell phone, PDA Physcial size is small. The computer system is on a small board. CPU, RAM, and low power consumption. Based on concept of the microcontroller, single integrated circuit that contains all the technology required to run an application. Combined onto a chip. Reduces chips and wiring to control a device. Reduce complexity, size, and cost. CPU RAM for program and data storage. Flash memory for program storage. Input/Output interfaces (i.e. serial, USB, ethernet, USB). Timers. Embedded system designed to perform simple, repeatable tasks. Simpliying tasks reduces complexity, minimize CPU processing power, and minimize RAM/Flash requirements. Maximum performance for minimum size and weight. Not apparent to user. Provide device with network-ability, available as modem, ethernet port, USB, or wireless. Provide device with user Interaction, likely (i.e. cell phone, PDA, gas pumps, ATMs). Some run without human intervention, but might be required to respond to realtime events (i.e. LEDs).
Fall 2012
12
Manufacturing Replaces general purpose computers to control equipment. Drive down manufacturing costs and subsequently drives down end user prices. Solution for complex problems in product design. Car has embedded systems to control brakes, ariconditioning, ignition systems. Without embedded systems, need complicated and fault prone electronics or general purpose PC. Consumer electronics Cell phones, digital cameras, pagers, PDAs, DVD players, copiers, printers, scanners, fax machines, Network switches/routers, stereo systems, televisions, game consoles, electronic instruments, electronic toys. Consumer products Ovens, dishwashers, washer, dryers, home security systems. Business products automobile electronics (i.e. audio system, anti-lock brakes), ATMs, hospital life-support systems, hospital medical testing systems, airplane on-board electronic monitors.
SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
13
Why use Linux? Other Embedded OS options (i.e. VxWorks by WindRiver, pSOS by WindRiver, QNX, Nucleus by Accelerated Technologies, RTKernel, Symbian OS, Microware OS-9 by RadiSys, Windows CE by Microsoft, OSE by Enea, MicroC by OS-II). Linux quality and reliability. Large community support (i.e. Google). No need to pay for customer support. Open source. GNU utilities and tools made Linux appealing. Even condensed version of Linux on dedicated system has many solutions. Linux Custom Distributions: Monta Vista, TimeSys, Linux Free Distirbutions: Redhat, Degian, and Suse. Multitasking scheduler allows many processes/threads to run at the same time. Symmetric Multi Processor (SMP) Memory protection between processes, prevents one process from bringing down system. Multiplatform solution since Linux runs on many CPUs (ARM, x386, PowerPC, MIPS). Networking protocols (TCP/IP, IP services, Netware, AppleTalk, Supports Virtual File System (VFS) for transparent access to many file systems. Dynamic loadable kernel modules.
Fall 2012
14
Kernel Architecture
Linux is modular, monolithic kernel. Kernel is single process with one address and memory space. Services (i.e. scheduler, file manager, memory manager) made through direct function calls. User space, each process (i.e. application) protected by their own user memory. Invalid memory access will crash only the process. Kernel space, memory space is not protected. Invalid memory access will crash the system.
User Space
App1 App2 . . .
Kernel Space
Networking
Scheduler uclibc
File Manager . . .
Architecture / Platform
X386 PowerPC ARM MIPS . . .
Fall 2012
15
Linux is modular, monolithic kernel. Kernel is single process with one address and memory space. Services (i.e. scheduler, file manager, memory manager) made through direct function calls. Linux Kernel modules exported symbols and functions are directly callable by other modules. Linux Kernel makes invalid memory reference, the kernel and all user processes crashes. Contrast micro-Kernel. Services are separate processes. IPC between services to request service. QNX example of microKernel. When QNX service makes invalid memory reference, the kernel does not crash, only that service crashes. struct_task data structure used by every process and thread. Allows Kernel to schedule both process and threads together. First process created is the init process and all other processes are child processes.
Fall 2012
16
Directory Structure
/boot Linux bootup files (i.e. Linux Kernel) /etc Linux system configuration files. /etc/inittab Processes started at bootup (i.e. Runlevel) /etc/fstab File systems and mount points. passwd /etc/passwd Users are defined and user accounts. /bin Linux system binaries.
vmlinux
etc
bin usr
inittab
mount bin init log tty0 cdrom
fstab
sbin var
dev
mnt home lib proc Fall 2012
student1
devices
17
ATM machine. Transaction occurs after 10 seconds or 20 seconds, but not critical to occur at the same time every time. At least the transaction completes. Video streaming.
Best effort and minimized latency from event. Linux latency in scheduler and memory manager makes Linux Soft Real-Time. For Hard Real-Time, Linux Kernel must be rewritten. Absolute deadline must be met and no deadline missed.
Airline computers controls radar, alarm indicators, airplane controls. Nuclear reactor temperator gauges and sensors must trigger cool down action immediately or disaster will occur.
Absolute deterministic response to an event. OS that provides required service in bounded response time. In bounded response time, need to know when event must occur. VxWorks provides the most real-time critical events.
Fall 2012
18
Preemption
Linux 2.4 nonpreemptive. System call (uclibc library) completed before the CPU was released. Higher priority process or interrupt prevented from running. For example, keyboard not responsive until system call was completed. Linux 2.6 system call was preemptable. A process forced to release CPU. Linux 2.6 scheduler determined time slice for each process and which process to run based on runqueue per priority level. Active and Expired priority arrays. Linux 2.4 scheduler uses one global runqueue. Traverse entire runqueue to determine next process to run.
Scheduling Policies (NORMAL, FIFO, RR, BATCH) Process Class (Interactive, Batch, Real-Time)
Interactive spends time performing I/O (i.e. editors). Batch do not need user interaction (i.e. compiler). Real-Time provide deterministic response time (i.e. robot controllers).
Fall 2012
19
Section 1 Embedded Linux Introduction Linux Development Requirements Cross Development Platform on Linux Host
Microcontrollers has limited resources, CPU is not powerful enough to run compiler, a file system, or a development environment. Host system running Linux 2.4, but the target system is Linux 2.6. The Linux 2.4 GCC compiler will be used to compile the Linux 2.6 GCC cross compiler. Host system is x86 processor, but the target system has ARM 9 based microcontroller. Cross compiler used to build toolchain (i.e. assembler, linker, utilties) for target system. Cross compiler and toolchain used to build Linux 2.6 Kernel for target system.
Target simulators on host allow testing before loading target system.
SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
20
200 MHz Cirrus Logic EP-9302 ARM920T core processor with math co-processor and MMU
ARM stands for Advanced RISC Machine (Reduced Instruction Set Computer Instruction Set Architecture). Simplicity suitable for low power applications in mobile and embedded electronics. 90% of all embedded 32-bit RISC processors.
10/100 MBps Ethernet 2 USB Ports RS232 Serial Port Real-time clock and watch-dog timer A/D, D/A converter 32 MByte 100 MHz SDRAM, 16 MByte Flash Marverick Math Engine
Fall 2012
21
Cross Development Environment Host System will send bootloader commands over Target System serial port. Host system ethernet port send Linux Kernel image and RAMDisk to target system RAM.
1) Host Linux Distribution, RedHat Linux. 2)Target System Linux 2.6 3) GNU Cross Development. 4) Root permission. 5) tftp Server. 6) tftpboot directory. 7) IPNetwork 8) Minicom communicate to target over serial cable. Transfer Kernel image and RAMDisk (ext2 File System) to Target System RAM or Flash.
22
System
Serial Port
Host
System
192.168.1.100 192.168.1.101
Ethernet Port
Switch
Fall 2012
Reset Microcontroller First stage bootloader located in small ROM (2K or 4K Bytes). Initializes CPU, MMU, on-chip devices, configures memory map. ROM bootloader loads second stage bootloader (i.e. Redboot) from fixed address from flash to RAM. ROM bootloader must have flash driver. NAND Flash large, less cost, cannot execute-in-place. NOR Flash smaller, execute-in-place (i.e. Second Stage Bootloader execute from NOR). Second stage bootloader will load the Linux Kernel and RAMDisk from: Automatically decompress the Linux Kernel and RAMDisk from flash to RAM. Manually interrupt bootloader (<cntrl> <c>) and use tftp to load Linux Kernel from server. After Linux Kernel starts running, bootloader is no longer in RAM. Bootloader configuration (i.e. IP address, host system (server) IP address) saved in high address space in flash. Only for development and testing. Bootloader configured with the starting address space in flash where Linux is located and where RAMDisk is located. During development, bootloader will load Kernel and RAMDisk from host system. Bootloader executes a jump to the Kernel code to configure the microprocessor registers and start_kernel() function. Bootloader architecture specific. Bootloader has memory map for the flash and for loading Kernel and RAMDisk and will be different for another microcontroller.
Fall 2012
23
Kernel initializes cache, hardware devices, and mounts root file system (i.e. RAMDisk). Without root file system, Kernel will hang. /etc directory contains Kernel configuration files (i.e. inittab, rc.d, fstab). Kernel executes the init process, reading its configuration file, /etc/inittab, and executes scripts dependent on selected runlevel. Init process executes startup script, /etc/rc.d/rc.sysinit, configuring and starting networking and other system services (i.e. /etc/rc.d/init.d contains service scripts). Init process enters a runlevel, where different processes are started by scripts to run activate the resources for that runlevel (default in /etc/inittab). Runlevel 5 used for graphical interface (used for PC). Starts services in /etc/rc.d/rc5.d. Runlevel 3 brings up the system console window (used for embedded system). Starts services in /etc/rc.d/rc3.d. Local initialization, /etc/rc.local.
SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
24
# inittab This file describes how the INIT # process should setup the system # in a certain run-level. Id:5:initdefault: #System initialization si::sysinit:/etc/rc.d/rc.sysinit l0:0:wait:/etc/rc.d/rc 0 l1:1:wait/etc/rc.d/rc 1 l2:2:wait/etc/rc.d/rc 2 l3:3:wait:/etc/rc.d/rc 3 l4:4:wait:/etc/rc.d/rc 4 l5:5:wait:/etc/rc.d/rc 5 l6:6:wait/etc/rc.d/rc 6 #Run gettys in standard runlevels 1:2345:respawn:/sbin/mingetty tty1 #Run xdm in runlevel 5 X:5:respawn:/etc/X11/prefdm - nodaemon
Fall 2012
Linux Boot Process Root File System Embedded system, RAMDisk is used for file system. No hard disk in embedded system. RAMDisk is in system memory and acts like a block device. Supports different kinds of file systems (i.e. FAT, ext2, ext3). Default file system is ext2. Before loaded from flash or tftp from host system, root file system is a compressed file. Decompressed into RAM as ext2 file system. Root file system must contain everything needed to support a full Linux system. The basic file system structure: Directories: /dev, /proc, /bin, /etc, /lib, /usr, /tmp Set of utilties (/bin): sh, ls, cp, mv, etc. System config files (/etc): rc.d, inittab, fstab, etc. Devices (/dev): hda, tty<x>, fd, etc. Runtime library used by utilities. Root file system is mounted in RAM from RAMDisk, persists in RAM until system reboots. Development option: NFS-mount the root file system from the host system. Using NFS need configuration options enabled when Kernel is built. Linux interface through Virtual File System (VFS), standard set of I/O interfaces that can be used over different devices (i.e. hard disk, CDROM, NFS, NTFS, Apple File System). Physical device abstacted away from the user. Common Open/Read/Write/Close APIs are used regardless of underlying file system.
SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
26
Host Linux distribution (i.e. Redhat Linux). GNU Cross Development environment. tftpboot directory (root level) contains the Kernel image and root file system (i.e. RAMDisk). NOTE: chmod R 777 /tftpboot /etc/init.d directory contains start/stop scripts for services in the system. /etc/init.d/xinetd (extended InterNet Daemon) must be running. Manages all Internet-based connectivity (i.e. ftp, tftp, telnet, ).
/etc/init.d/xinetd <start|stop|status|restart> /etc/xinetd.d directory contains the configuration files for all applications managed by xinetd (i.e. tftp configuration file is here). Included in the xinetd configuration file. Each xinetd application has separate file, organized per application for security reason and allow easier customization. IP addresses assigned to Host System and assigned to Embedded System must be on same subnet.
Fall 2012
27
Fall 2012
28
Section 2 Embedded Linux Development Linux Boot Process Host System Requirements
minicom s (minicom setup mode). Set BPS to 57600, No Flow Control, Data Bits 8, Parity None, Stop Bits 1. Set Hardware Flow Control to No.
USB serial device allows user access, otherwise only root permission can initialize minicom.
chmod 666 /dev/ttyUSB0 (command in /etc/rc.d/rc.sysinit). /etc/rc.local has local startup commands. Cirrus Logic Microcontroller with RS232 serial use Serial-toUSB converter. NOTE: Cirrus Logic Microcontroller USB not active until after Linux boots. Bootloader must have USB driver to use during boot.
Fall 2012
29
A Serial Device: /dev/ttyS1 /dev/ttyUSB0 B Lockfile Location: /var/lock C Callin Program: D Callout Program: E Bps/Par/Bits: 38400 8N1 57600 8N1 F Hardware Flow Control: Yes No G Software Flow Control: No Change which setting? SILICON VALLEY UNIVERSITY CONFIDENTIAL
Fall 2012
30
Linux In Desktop PC
During powerup, BIOS in host (X86 system) access the hard drive Master Boot Record. In MBR is GRUB, Linux bootloader in Linux distributions. GRUB provides choice to boot one of multiple OS (i.e. Linux or Windows).
/boot/grub/grub.conf contains GRUB configuration file with menu selection (i.e. which OS) and disk partition containing Linux Kernel and the root file system.
GRUB loads Linux Kernel from /boot directory ( contains Linux Kernel(s), Kernel System Map(s), initrd (initial Ramdisk with drivers), Kernel config file(s) from Kernel build ).
Initrd is temporary root file system with executables (i.e. insmod) and drivers to mount the root file system on disk. After mounting, initrd is unmounted and memory freed. NOTE: In embedded Linux systems, initrd is the final root file system.
Linux Kernel starts init process. Init process is the root/parent process of all other process executing on Linux. Init process runs script, /etc/rc.d/rc.sysinit. Init process runs script, /etc/inittab, to execute scripts to start processes based on the runlevel.
Fall 2012
31
Power cycle target, when + sign on console appear, execute <ctrl> <c> to interrupt bootloader (Redboot) from continuing with autoboot. The bootloader prompt Redboot> will appear. Redboot> fconfig l Run script at boot: true Boot script: .. fis load ramdisk .. fis load zImage .. exec r 0x800000 x 0x300000 Boot script timeout (1000ms resolution): 1 Use BOOTP for network configuration: false Gateway IP address: 0.0.0.0 Local IP address: 192.168.1.200 Local IP address mask: 0.0.0.0 Default server IP address: 0.0.0.0 DNS servier IP address: 0.0.0.0 Set eth0 network hardware address [MAC]: true Eth0 network hardware address [MAC]: 0x00:0x00:0x00:0x00:0x4c:0x33 GDB connection port: 9000 Force console for special debug messages: false Network debug at boot time: false
Fall 2012
32
Power cycle target, when + sign on console appear, execute <ctrl> <c> to interrupt bootloader (Redboot) from continuing with autoboot. The bootloader prompt Redboot> will appear. Redboot> load r v b 0x800000 h 192.168.1.230 ramdisk.gz Loads data to the target RAM or flash file system. The IP address of Redboot (i.e. fconfig Local IP Address) must be in same subnet as tftp server IP address, 192.168.1.230. -r: Raw or binary data. Requires b option to specify the location in RAM. -v: Display small spinner when the download is in progress. -b 0x800000: Address in RAM to load the data. This is the location of the RAMDisk as configured internally in Redboot. -h 192.168.1.230: IP address of the tftp server. ramdisk.gz: The name of the file on the tftp server. Redboot> fis load zImage Flash Image System. Loads the file, zImage, from the FIS directory. Once loaded, image can be executed. Redboot> exec r 0x800000 s 0x300000 Execute the Linux kernel from Redboot internal configuration. -r 0x800000: Address in RAM of the RAMDisk (i.e. Root File System). -s 0x300000: Length of the RAMDisk image.
Fall 2012
33