Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded Linux Hands-on Training

Similar presentations


Presentation on theme: "Embedded Linux Hands-on Training"— Presentation transcript:

1 Embedded Linux Hands-on Training
For Embedded RSC and Specialists Nick Ni Embedded Applications Group

2 About me Started at Altera (Embedded Applications) in May 2011
SoC FPGA Virtual Target + FPGA-In-the-Loop Embedded software and SDK OpenCL (after this trip) Worked at Qualcomm as EDA consultant and Masters in University of Toronto Lived in 杭州 until 5 years old After that… 日本: 10 years Canada: 11 years USA: Current

3 Let’s introduce yourself
Let’s briefly introduce yourself around the table

4 Objectives To obtain basic Linux knowledge
To feel comfortable with embedded Linux issues with real silicon Basic embedded Linux knowledge Application development and debugging Kernel driver development and debugging

5 Motivation SoC FPGA soon shipping with Embedded Linux 3.3 kernel
Device drivers for all HPS hard IPs Running Yocto for its build system Android friendly Expect customers to use Linux out-of-box Write Linux drivers for their custom Ips Issues with driver/IP/silicon in context of Linux

6 Requirements – NEEK Board

7 Requirements – Linux PC
Windows 7 PC VirtualBox VirtualBox image for CentOS 6 (provided) (~20GB)

8 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development Linux Basics – Include idea behind commands Build Linux forum within Altera for helping each other out on the labs

9 Linux Basics Linux Basics – Include idea behind commands

10 What is Linux? Linux – the kernel from www.kernel.org
Linux Distribution – LOTS of packages (1000+) Mainly GNU Contribution Linux Distribution Perl GCC/GDB Text Editor Linux Basics – Include idea behind commands Make Toolchains Media Libraries Linux Kernel

11 Why Customers choose Linux
Cost Royalty free Free tools Free source code Powerful Complete Modifiable Popular Huge Community Portable

12 Embedded Linux Usage

13 Linux Distributions Operating systems including a large collection of software applications such as word processors, spreadsheets, media players, and database applications. consist of the Linux kernel a set of libraries and utilities from the GNU Project Some distributions optimize for size (embedded) use more compact libraries (i.e. GNU utilities, such as BusyBox, uClibc, or newlib Over 300 active distributions

14 Popular desktop distributions

15 uClinux Used for Nios-II
*was* a fork of the Linux kernel for microcontrollers (in embedded systems) without a memory management unit (MMU). Integrated in mainline kernel from Consists of patches and tools for microcontrollers. Consists of small-size library packages busybox dropbear gdbserver

16 Let’s play around with Linux!
You should already have VirtualBox installed Load CentOS 6 VirtualBox Image Right click and “open terminal”

17 Getting Help Command manual Command help % man <command>
E.g. % man tar Command help % <command> --help

18 Command Options -a = lists all the files and directories, even hidden ones (e.g. /home/user/.bashrc) -l = lists the full detail about all the files and directories -d = lists the directory -c = don’t create file if it already present -f = force -k = block Size -R = recursive -t = type -V = version.

19 Package Installation Different distributions use different installers
CentOS YUM RedHut DEBIAN APT UBUNTU FEDORA Look at YUM Help to see how to use YUM

20 Linux File Properties Each file has
<dir><root rwx><group rxw><user rxw> <directories> <owner> <group> <size> <last modified date> <filename>

21 User and Privileges Variety of commands for user/privilege control
Functionality Chmod Change Permission Chown Change Owner Chgrp Change Group Su Switch User Sudo “Root” Mode e.g. Change permission to read/exec for user/group and full access to root % chmod 755 <file> Change the owner of file % chown <new user> <file> Switch current user to ROOT % sudo su -

22 File Access Commands Command Functionality grep
Search a string in a list of files echo output a string (to console by default) cat Read and output contents of a file (to console by default) more File viewing less File viewing with more features (better for large files) touch Update timestamp of a file find Find files e.g. Search “integer” in any files in current directory (ignore subdirectories) % grep integer ./* Search “integer” in two files called “file1” and “file2” % grep integer file1 file2 Output “hello world” to console % echo “hello world” Read file content of “file1” % cat file1

23 I/O Redirection e.g. Write “hello” to “file1”
Command Functionality >> Redirect and append to another output target < Redirect from another input source | Pipe output of a command as input of another command > Rediret to another output target e.g. Write “hello” to “file1” % echo “hello” > file1 Append “world” to “file1” % echo “world” >> file1 Send stderr to stdout % grep * 2>&1 Display the contents of a directory using “less” command % ls | less

24 Other useful Information
% history Shows you your command history Bash start-up script Located in ~/.bashrc Add your commands to be run automatically when opening a console % ifconfig Networking information % ps View the processes currently running e.g. ps aux | grep <user> = all <user>’s running processes % kill kill a process. (kill -9 if kill doesn’t work) % top View the CPU usage of processes % dmesg Displays kernel messages. Useful for driver development and kernel debugging More info on and google

25 Pop Quiz What do the commands below do?
% find . | xargs grep string % grep int ./*/*.c 2> /tmp/log % ls –l | grep “\.txt$” % tar czvf /var/snapshot-$(date +%Y%m%d).tar.gz /home/devel How do you list out all header files (.h) under current directory and sort them in ascending size

26 Linux File System Three types of files
Regular Files contains a sequence of bytes that generally corresponds to code or data. Directory Files contains an entry for every file and subdirectory that it is placed. Device Files correspond to the printers or other devices connected to the system.

27 Linux Directories /bin = the binaries which are needed to run LINUX.
/boot = all the files required for booting LINUX on system. /dev = the devices for all the files. /etc = the configuration files of the various software. /home = like My Documents in Windows. /lib = the libraries required for the system files. /lost+found = files which are damaged or which are not linked to any directory (e.g. due to the incorrect shutdown) /mnt = where we mount the devices and other files /opt = optional softwares are installed. /root = The directory for the user root /proc = represent the current state of the kernel /sys = exports information about devices and drivers from the kernel device model to user space, and is also used for configuration

28 Exercise: Installing GVIM
Search for the package “vim-X11” using YUM % yum search <package name> Install GVIM using YUM (with SUDO in the front) % sudo yum install <package name> Run GVIM Uninstall GVIM % sudo yum remove <package name> Download GVIM Package from internet Install the downloaded Package locally using YUM % sudo yum –Uvh <package name>.rpm

29 Exercise: Write a Shell Script
Your file should begin with “#!/bin/sh” Write a shell script that creates a file that looks like: ******************************************************* *** This file contains all files with .c extension under <current directory> containing word “string” *** <file 1> <file 2>

30 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development Linux Basics – Include idea behind commands Build Linux forum within Altera for helping each other out on the labs

31 High-level Architecture
Hardware

32 Software Components Cross-compiler Bootloader Linux Kernel C library
Compiler that runs on the development machine, but generates code for the target Bootloader Started by the hardware, responsible for basic initialization, loading and executing the kernel Linux Kernel Contains the process and memory management, network stack, device drivers and provides services to userspace applications C library The interface between the kernel and the userspace applications Libraries and applications

33 Device Tree A data structure for describing hardware
No need to hard code every detail of a device into an OS Hardware details are passed to OS at boot time

34 Root filesystem several filesystems are mounted and create a global hierarchy of files and directories A particular filesystem, the root filesystem, is mounted as / containing all the libraries applications data The kernel is usually kept separate Usually Root Filesystem is stored in non-volatile storage (e.g. Flash) Ramdisk filesystem is also possible which runs off memory Changes do not get saved!

35 Developing Embedded Linux
Board Support Package development A BSP contains a bootloader and kernel with the suitable device drivers for the targeted hardware System integration Integrate all the components, bootloader, kernel, third-party libraries and applications and in-house applications into a working system Development of applications Normal Linux applications, but using specifically chosen libraries

36 Boot Sequences Power Cycle
read bootloader from a media (Flash) and copy to RAM bootloader initializes HW and reads compressed kernel Read Device Tree blob to decide how to initialize drivers Runs user init /etc/rc Runs the program init based on /etc/inittab Mount root filesystem from a media (Flash) Uncompress initialize kernel

37 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development Linux Basics – Include idea behind commands Build Linux forum within Altera for helping each other out on the labs

38 uClinux Linux distribution project
Originally for non-MMU microcontrollers Now include Linux kernel 2.6+ User applications Libraries Toolchains We build uClinux for Nios-II running on NEEK!

39 Images you will build Linux kernel image U-Boot Bootloader image
vmImage (Gunzip’ed binary for U-boot booting) vmlinux (ELF) U-Boot Bootloader image u-boot (elf) u-boot.bin (binary) Root filesystem rootfs.jffs2 (JFFS2 Filesystem) rootfs.initramfs (Raw) rootfs.initramfs.gz (Gunzip’ed raw)

40 uClinux Build Flow Under the hood Configure uClinux with
vendor processor platform Generate and customize device tree from .sopcinfo Configure User Applications/Library Build all images *except* u-boot Configure Linux Kernel Under the hood Build User app/lib rootfs.initramfs rootfs.initramfs.gz rootfs.jffs2 Re-Build Linux Kernel with ramdisk initramfs_data.cpio linux.initramfs.gz Build linux kernel vmlinux vmlinux.bin vmlinux.gz vmImage

41 U-Boot Build Flow U-Boot needs to be built independently from Linux Kernel and Root filesystem Generate and Customize U-Boot header file from sopcinfo Build u-boot

42 Cross-Compiler Need to build cross-compiler for Nios-II
Runs on x86 PC Outputs Nios-II instructions Already set up in the Vbox image Try % which nios2-linux-gnu-gcc Try % echo $PATH

43 Exercise: Generate Device Tree
A tools from Nios-II community called sopc2dts can convert “most of” .sopcinfo to device tree and header files % cd ~/nios2-linux/tools/sopc2dts % java -jar sopc2dts.jar --gui In the GUI, choose file: <eth_std_main_system.sopcinfo file from ethernet standard Quartus II project for NEEK> Select following in the GUI: Boardinfo General Select master: cpu:cpu Memory nodes: select both memory (CTRL key) Click Apply Kernel Bootargs: debug console=ttyJ0,115200 Ethernet Highlight tse_mac Edit MAC-address Output Save As: <your choice of name>.dts

44 Exercise: Customize Device Tree
Fix the following bold parts in generated .dts manually to fix the tool bug ext_flash: { #address-cells = < 1 >; #size-cells = < 1 >; compatible = "ALTR,cfi_flash-altera_generic_tristate_controller", "cfi-flash"; reg = < 0x x00b00000 >; bank-width = < 2 >; device-width = < 1 >; }; //end (ext_flash) ethernet_subsystem_ethernet_bridge: { compatible = "ALTR,avalon-12.0","simple-bus"; reg = < 0x800a000 0x3000>; #address-cells = <1>; #size-cells = <1>; ranges = <0x x0800A000 0x 0x x0800A440 0x 0x x0800A400 0x 0x x0800C000 0x >; ethernet_subsystem_tse_mac: { compatible = "ALTR,tse-12.0","ALTR,tse-1.0"; reg = < 0x0 0x400 0x400 0x40 0x440 0x40 0x2000 0x1000>; interrupt-parent = < &cpu >; interrupts = < 1 6 >;

45 Exercise: Configure uClinux
Go to uClinux directory and start configuration tool % cd ~/nios2-linux/uClinux-dist % make menuconfig Select “Vendor/Product Selection” Select Altera for Vendor and nios2 for Products Press “Exit” to go back one level and Select “Kernel/Library/Defaults” Select “Customize kernel Settings” and “App/Lib Settings” Exit and save when prompted

46 Exercise: Configure Linux Kernel
Menuconfig of linux kernel should appear soon after Select the followings Platform Options Mem base: 0x Compile and link device tree into kernel image Add path to Device tree source file Enable MUL/ DIV instruction Processor Type and Features Unselect “Ignore kernel command string from DTB” Device Drivers Network device support Ethernet driver support Altera Device Altera Triple-Speed Ethernet Mac support Character devices Enable /dev/kmem virtual device support Serial drivers Altera JTAG UART support Altera JTAG UART console support Memory Technology Device (MTD) support Mapping drivers for chip access Flash device in physical memory map based on OF description Exit and save. If asked for any questions, just enter for default choice

47 Exercise: Configure Filesystem
uClinux Distribution Configuration menuconfig will appear Select BusyBox -> Networking Utilities -> httpd Exit and Save

48 Exercise: Build Linux Kernel and Filesystem
Simply type “make” in uClinux-dist directory to kick-off the build process On VirtualBox, it will take min Native box would be much faster (~10min)

49 Exercise: Building U-Boot
U-Boot needs to be built separately Use sopc2dts to generate Header file instead of .dts file (No need to do this for this lab) U-Boot is already configured and .h generated in the Vbox image u-boot/include/configs/3c25_eth_std_main_system.h u-boot/board/altera/nios2-generic/3c25_eth_std_main_system.h Simply build for now % cd ~/nios2-linux/u-boot % make CROSS_COMPILE=nios2-linux-gnu- 3c25_eth_std_main_system_config % make CROSS_COMPILE=nios2-linux-gnu- You should see U-boot and U-boot.bin built

50 Exercise: You are ready to bring-up NEEK with Linux!
You should have:

51 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development

52 Board Bring-up Flow Convert U-Boot image to a BootROM image and program to CFI flash Fetch RootFS, Kernel and device tree from TFTP server and program them to CFI Hit reset and linux boot up! Boot U-Boot and set up ethernet and erase sections of CFI flash

53 CFI Flash Partitioning
BootROM / U-Boot (0x0) RootFS (0x100000) U-Boot Env (0xC00000) Device Tree(0xD00000) Linux Kernel (0xE00000) U-Boot View Linux View

54 Exercise: Set up a Shared Folder
To pass software images to Windows 7 In VirtualBox menu, go to, Devices -> Shared Folders Change folder path to “shared_folder” in the distributed package In Linux, mount the shared drive to /mnt % sudo mount –t vboxsf shared_folder /mnt Copy the built images to /mnt uClinux-dist/images/* u-boot/u-boot* uClinux-dist/linux-2.6.x/arch/nios2/boot/system.dtb

55 Exercise: Install TFTP server
TFTPD64 Set your “shared_folder” as TFTP directory IMPORTANT: Your host Windows machine may have firewall that prevents tftp access from outside If it does NOT work, try allow “tftpd” to go through firewall in “Windows Firewalls”

56 Exercise: Program U-Boot as BootROM image
In NiosII Command Shell, Program FPGA with SOF file in shared_folder\niosii-ethernet-standard-3c25\niosii-ethernet-standard-3c25.sof % nios2-configure-sof <.sof> Convert U-Boot as BootROM data Go to shared_folder % elf2flash --input u-boot --output u-boot.flash --base 0xe end 0xe0ffffff --reset 0xe boot=<path to nio2eds>/components/altera_nios2/boot_loader_cfi.srec Download into Flash % ../nios2-flash-programmer --base=0xe u-boot.flash Start Nios2-terminal and hit “CPU reset” on the board You should see U-Boot booted up

57 Exercise: Program Flash Contents
In U-Boot Set environmental variables % setenv ipaddr <NEEK IP> % setenv serverip <Host IP> % setenv ethaddr <NEEK MAC> % setenv bootcmd 'set bootargs root=/dev/mtdblock0 rootfstype=jffs2 rw; cp.b e0d00000 d ; bootm e0e00000 – d ' Program RootFS % protect off all % erase e f00000 % tftp d rootfs.jffs2 % cp.b d e $filesize Program Kernel and Device Tree % tftp d system.dtb % cp.b d e0d00000 $filesize % tftp d vmImage % cp.b d e0e00000 $filesize Save U-Boot environments % saveenv

58 Exercise: Cross your fingers!
Hit “CPU reset” on the board and… Hope Linux boots completely!

59 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development

60 Software Packages Pre-built Application and Library codes
Access hardware through Linux Kernel System Call API Thousands of packages already exist by the Open-Source community Busybox Web browser Text editor SSH Development tools (e.g. gcc/gdb) Compression Graphics Audio

61 Busybox The Swiss Army Knife of Embedded Linux
Tiny versions of many common UNIX utilities Replacements for most of the utilities in GNU fileutils, shellutils, etc. Generally have fewer options than their full-featured GNU cousins Provides a fairly complete environment for any small or embedded system. Designed for size-optimization and limited resources in mind.

62 Methods to build software packages
Option 1: Build manually from source files Download the source files Configure to cross-compile into the target hardware Build it Install it Copy it to the filesystem Option 2: Use distribution to automatically build it Configure uClinux to include the package Build uClinux (automatically configure/build/install/copy to FS for you)

63 Exercise: Build SSH server
Go to nios2-linux/dropbear Create and go into a new dir called build-dropbear-0.52 Configure/build/install dropbear to cross compile for Nios-II % cd build-dropbear-0.52 % ../dropbear-0.52/configure --host=nios2-linux-gnu --with-zlib=/home/student/nios2-linux/uClinux-dist/lib/zlib prefix=/home/student/linux-labs/dropbear/install-dropbear-0.52 % make % make install

64 Exercise: Build SSH server (cont’d)
Copy install-dropbear-0.52 to /mnt On NEEK Set up ethernet with an static IP % ifconfig eth0 <NEEK IP given to you in the class> fetch the all the files in install-dropbear-0.52/bin and /sbin to /bin and /sbin in Nios-II linux filesystem % tftp -g <tftp server ip> -r <file name> -l </bin/dropbear or /bin/dropbearkey> Generate Host RSA key % mkdir -p /etc/dropbear % dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key Set root password since SSH server needs one % passwd Start SSH server % dropbear Now use a SSH client program on your workstation (e.g. Putty) and connect to: IP>

65 Exercise: Use Distribution to build
Go to uClinux-dist and select “dropbear” in menuconfig % cd uClinux-dist Select “Kernel/Library/Def Sel” -> Customize App/Lib Settings” Save and Exit Select “Network App” -> “dropbear” % make Once finished, look into romfs/etc/dropbear and romfs/etc/inetd.conf to see what got generated

66 Exercise: Use Distribution to build SSH (Cont’d)
Copy the built images to /mnt uClinux-dist/images/* uClinux-dist/linux-2.6.x/arch/nios2/boot/system.dtb In NiosII Command Shell, Start Nios2-terminal and hit “CPU reset” on the board You should see U-Boot booted up, press any key to interrupt U-Boot In U-Boot Program RootFS % protect off all % erase e b00000 % tftp d rootfs.jffs2 % cp.b d e $filesize Hit “CPU Reset” on the board and linux should boot up with SSH server automatically started

67 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development

68 Application Uses In-house/third-party Libraries and Standard C Library
No direct access to Hardware Resources Talk to drivers through file interface /dev/mtdblock0 = 1st partition of a Storage device /dev/ttyS0 = 1st UART /dev/ttyJ0 = 1st JTAG UART

69 Pop Quiz: Use JTAG UART driver
Use nios-terminal and a SSH client connected to Nios-II to talk to each other using /dev/ttyJ0

70 Pop Quiz: Simplest Solution
nios2-terminal -> SSH client In SSH client % cat /dev/ttyJ0 In nios2-terminal % echo “<string>” > /dev/ttyJ0 SSH client -> nios2-terminal

71 Linux Application What you write What are provided Source files .c
Header files .h Makefile What are provided Standard C libraries .a or .so Usually located inside the toolchain ~/nios2-linux/toolchain-mmu/x86-linux2/nios2-linux-gnu/libc Compiler will look for headers and libraries to link to in the directory by default User libraries Configured and built by user (manually or with distribution) Usually located in some user specified location For uClinux, it’s in uClinux-dist/user Need to explicitly specify the header and library location in Makefile

72 Linux Application Build Flow
User Library .c/.h .c/.h .c/.h .c/.h GCC GCC GCC GCC .o .o .o .o AR Standard C Lib .a/.so .a/.so GCC GCC GCC Binary / ELF

73 Compiler and Useful Tools
Native desktop tools without any prefix Cross-compiler adds prefix e.g. nios2-linux-gnu-gcc e.g. arm-none-linux-gnueabi-objdump Tool Description GCC Compiler GDB Debugger DDD GDB with GUI OBJDUMP Display information from object files NM List symbols from object files AR Create, modify, and extract from archives OBJCOPY Copy and Translate object files STRIP Discard symbols from object files

74 I/O Libraries in Standard C
Add the following line in your source file #include <stdio.h> See the detailed API syntax % man <API> API Description fopen opens a file vfscanf a file stream or a buffer using variable argument list freopen opens a different file with an existing stream vsscanf fflush synchronizes an output stream with the actual file printf prints formatted byte/wchar_t output to stdout, fclose closes a file fprintf a file stream or a buffer setbuf sets the buffer for a file stream sprintf setvbuf sets the buffer and its size for a file stream snprintf fwide switches a file stream between wide character I/O and narrow character I/O vprintf fread reads from a file vfprintf a file stream, or a buffer using variable argument list fwrite writes to a file vsprintf fgetc reads a byte/wchar_t from a file stream vsnprintf getc ftell returns the current file position indicator fgets reads a byte/wchar_t string from a file stream fgetpos gets the file position indicator fputc writes a byte/wchar_t to a file stream fseek moves the file position indicator to a specific location in a file putc fsetpos fputs writes a byte/wchar_t string to a file stream rewind moves the file position indicator to the beginning in a file getchar reads a byte/wchar_t from stdin clearerr clears errors gets reads a byte string from stdin feof checks for the end-of-file putchar writes a byte/wchar_t to stdout ferror checks for a file error puts writes a byte string to stdout perror displays a character string corresponding of the current error to stderr ungetc puts a byte/wchar_t back into a file stream remove erases a file scanf reads formatted byte/wchar_t input from stdin, rename renames a file fscanf tmpfile returns a pointer to a temporary file sscanf tmpnam returns a unique filename vscanf reads formatted input byte/wchar_t from stdin, strcmp compares two c strings.

75 Application Driver Interface
/* Open UART device at /dev/ttyAL0 */ fd = fopen("/dev/ttyAL0", "r+w”); /* Write a char* prompt to UART */ count = fwrite(prompt, 1, strlen(prompt), fd); /* Read a line from UART device */ fgets(input, size, fd); /* Print to debug console. Default to JTAG Uart */ printf(%s: Read %s\n”, __func__, input); /* Close UART device */ fclose(fd)

76 Makefile CC = nios2-linux-gnu-gcc CFLAGS = -g TARGET = uart_app objs = uart_app.o all: $(TARGET) uart_app: $(objs) $(CC) $(CFLAGS) $(objs) -o .c.o: $(CC) $(CFLAGS) $< -c -o clean: rm -rf $(objs) $(TARGET) Target TAB

77 Application Debugging
Use a debug server to run your application under development Use GDBSERVER for debugging using GDB Through TCP/IP protocol Already built into the root filesystem Often called “Run-Mode Debugging”

78 Exercise: Write a UART Console
Use /dev/ttyAL0 Use Standard C I/O libraries Write an shell application that prompt “##UART Shell>>” and wait for user input into JTAG UART Start parsing the user input string upon a newline ‘\n’ Implement following functions: ##UART Shell>> keyfind <dir> <keyword> where <dir> is absolute path to a parent directory where you start you search from <keyword> is a string you are looking for ##UART Shell>> exit End application and go back to standard linux console Write a Makefile for building the final application ELF/Binary

79 Exercise: Write a UART application (Cont’d)
Upload the ELF to running Linux via TFTP Run “gdbserver” on Nios-II linux % gdbserver host:2345 <ELF file> Use DDD to debug from your VirtualBox % ddd –debugger nios2-linux-gnu-gdb <ELF file> In DDD console: % target remote <NEEK IP>:2345 Use Putty as host receiver with Serial/115200/8-N-1/No HW Flow Ctrl …and Good luck!

80 Agenda Agenda Linux Basics Introduction to embedded Linux
Building Linux Packages Board Bring-up Software Packages Application development Driver development

81 User space and Kernel space
Linux manages the machine's hardware, offering the user a System Call Interface Any subroutines or functions forming part of the kernel (modules and device drivers, for example) User space End-user programs, like the UNIX shell or other GUI based applications Do not directly interact with Hardware but through the System Call Interface

82 System Call Interface Hardware devices are seen as device files from user space /dev/ttyJ0 /dev/sda1 … Basic System Call interface are like file operations System Call Description Insmod (system) Load driver module Open Open device Read Read from device Write Write to device Close Close device Rmmod (system) Unregister driver module

83 System Call Interface in Driver
These functions will be called by user application Like the one you wrote in the UART application int uart_open(struct inode *inode, struct file *filp); int uart_release(struct inode *inode, struct file *filp); ssize_t uart_read(struct file *filp, char *buf, size_t count, loff_t *f_pos); ssize_t uart_write(struct file *filp, char *buf, size_t count, loff_t *f_pos); void uart_exit(void); int uart_init(void); /* Structure that declares the usual file */ struct file_operations uart_fops = { read: uart_read, write: uart_write, open: uart_open, release: uart_release };

84 Sysfs Interface RAM-based filesystem Provides a means to export
Kernel data structures Attributes Linkages between them to userspace Some devices allow you to control HW directly by writing to sysfs E.g. echo 1 > /sys/devices/platform/asus_laptop/wlan E.g. echo file > /sys/devices/platform/usb-otg/gadget/file

85 Sysfs Interface in Driver
static ssize_t irqgen_show_interval(struct device *dev, struct device_attribute *attr, const char *buf); static ssize_t irqgen_store_interval(struct device *dev, struct device_attribute *attr, const char *buf, size_t size); static struct class *irqgen_class; static struct device *dev; static DEVICE_ATTR(interval, 0644, irqgen_show_interval, irqgen_store_interval); static int __init custom_irqgen_driver_init(void) { irqgen_class = class_create(THIS_MODULE, "irqgen"); dev = device_create(irqgen_class, NULL, MKDEV(MAJOR, MINOR), NULL, "irqgen"); result = device_create_file(dev, &dev_attr_interval); return 0; } static void __exit custom_irqgen_driver_exit(void) device_remove_file(dev, &dev_attr_interval); device_unregister(dev); class_destroy(irqgen_class);

86 /dev/mem Interface /dev/mem /dev/kmem /dev/allkmem
a special file that provides access to the physical memory of the computer. /dev/kmem a special file that provides access to the virtual address space of the operating system kernel, excluding memory that is associated with an I/O device. /dev/allkmem a special file that provides access to the virtual address space of the operating system kernel, including memory that is associated with an I/O device. Use to examine and modify the system Warning: BAD THINGS can happen if you are NOT careful. Use with CARE.

87 /dev/mem usage example
fd = open(/dev/mem”, O_RDWR|O_SYNC); // Map physical memory range of a peripheral memory map (0xE – 0xE800017f) to userspace memory mem = mmap(NULL, 0x100, PROT_READ|PROT_WRITE, MAP_SHARED, \ fd, 0xE ); // Write to E in Peripheral register map *(mem+4) = 0x2f // Read from E in Peripheral register map value = *(mem+8);

88 Data passing Never access data from userspace directly in kernel
Use following functions to copy to kernel space unsigned long copy_from_user (void * to, const void __user * from, unsigned long n); unsigned long copy_to_user (void __user * to, const void * from, unsigned long n);

89 Device Files Usually sit in /dev type Major number Minor number
block (b) character (c) unbuffered character (u) FIFO (p) Major number Minor number Use “mknod” to create a device file % mknod /dev/irqgen0 c 251 0

90 Allocating kernel resource
Reserve a major/minor number Register System Call Interface struct file_operations irqgen_fops = { read: irqgen_read, write: irqgen_write, open: irqgen_open, release: irqgen_release }; Int irqgen_major = 251; // Set to 0 for auto-allocation result = register_chrdev(irqgen_major, "irqgen", &irqgen_fops); if(result < 0){ printk(KERN_ALERT "irqgen: cannot obtain major number %d\n", irqgen_major); return result; }

91 Interrupt Registration
Register interrupt service routine with a IRQ line static irqreturn_t irqgen_interrupt_handler(int irq, void* dev_id){ return IRQ_RETVAL(1); } request_irq(IRQ_NUM, irqgen_interrupt_handler, 0, “irqgen0”, NULL);

92 Hardware Access Remap physical memory mapped I/O base to Virtual Address virt_addr = ioremap(base, size) Low-level hardware access writel(data, virt_addr+offset) readl(virt_addr+offset)

93 Building Kernel Driver
Loadable Kernel Module Build as a loadable module (.ko) Select (M) instead of (*) in menuconfig for a driver OR Build it externally with Makefile pointing to the kernel source Prefered during development! Statically build into the kernel image Add a driver to kernel source Include the driver in menuconfig Re-build linux kernel image

94 Loadable Kernel Module
Makefile points to kernel source Generate a .ko file Load .ko file in running Linux using “insmod” obj-m := <your driver source file>.o … CROSS_COMPILE = nios2-linux-gnu- KDIR := <dir to linux-2.6.x> ARCH := nios2 all: $(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=`pwd` modules clean: rm $(obj-m) rm *.ko rm *.mod.* rm *.symvers rm *.order

95 Debugging Kernel Code Printk()! (Most Common) Nios II GDB Server
Uses JTAG to debug in instruction set level Uses “nios2-gdb-server” Lauterbach Trace32 Uses JTAG and MICTOR connectors Able to capture “Instruction Trace” KGDB Source level debugger for kernel Uses serial port (RS232) to communicate with host Intrusive to the kernel source (need to add package) Often called “Stop-Mode Debugging”

96 Debugging using Nios II GDB Server
Compile Linux kernel with “Compile the kernel with debug info” (already done for you in your vmlinux) (menuconfig) Kernel hacking -> Boot Linux on Nios II In Host, start with % nios2-gdb-server –tcpport <port> In Linux VirtualBox, start GDB with vmlinux built in uClinux/linux-2.6.x % nios2-linux-gnu-gdb vmlinux (GDB) % target remote <host IP>:<port> Now you can debug any STATICALLY COMPILED kernel code/drivers

97 Debugging Loadable Kernel Module
It’s tricky because the module gets loaded to a dynamic memory address during run-time Need to re-map all code/data segments in ELF file Linux provides /sys filesystem which provide you the info /sys/module/<driver name>/sections/

98 Debugging Loadable Kernel Module (cont’d)
In VirtualBox Start GDB without ELF % nios2-linux-gnu-gdb Connect to nios2_linux_server Remap ELF filePrai (GDB) % add-file-symbol <your driver>.ko <.text address> -s .data <.data address> -s … Now GDB can debug the driver like other part of kernel

99 Debugging with KGDB Warning! Very unstable with Virtual I/O RS232 to VirtualBox Compile Linux kernel with KGDB (menuconfig) kernel hacking -> KGDB: kernel debugger -> KGDB: use kgdb over the serial console (Already Done for you in the vmlinux image) Reset the board and interrupt U-boot Modify U-Boot environments (U-boot) % setenv bootcmd 'set bootargs debug kgdboc=ttyAL0, kgdbwait console=ttyJ0, root=/dev/mtdblock0 rootfstype=jffs2 rw; cp.b e0d00000 d ; bootm e0e d ‘ (U-boot) % saveenv Reset the board and let Linux Boot

100 Debugging with KGDB (cont’d)
Linux booting should pause at ttyAL0 at MMIO 0x (irq = 2) is a Altera UART kgdb: Registered I/O driver kgdboc. kgdb: Waiting for connection from remote gdb... Quit any serial program like Putty On VirtualBox GUI, plug in the Serial-USB device On VirtualBox, Check which device file the serial cable is registered to (should be /dev/ttyUSB0) % dmesg Run GDB % nios2-linux-gnu-gdb –b (GDB) % target remote /dev/ttyUSB0 (GDB) % symbol-file linux-2.6.x/vmlinux Now the GDB should work the same way

101 Final and Ultimate Exercise: Write Complete Software for a custom IP
Programmable-interval interrupt generator IP See HDL file to find out how it works Write a kernel driver Write an application which Change interrupt interval uniformly % irqgen_app uniform <interval in clk cycle> Change interrupt interval incre/decrementally % irqgen_app var <start interval> <end interval> Also change interrupt interval using sysfs Debug application and driver and HW (SignalTap)

102 Final and Ultimate Exercise Steps:
Programmable-interval interrupt generator IP Open Qsys system “eth_std_main_system” Look at IP called “ramdom_number_generator.v” See HDL file to find out: Software accessible register offsets Bit field definition IP base address (+0xE for Virtual Address)

103 Final and Ultimate Exercise Steps:
Write a kernel driver Initialize the interrupt interval and enable interrupts write function to write new interval value Implement sysfs interface Start from template found in ~/linux-labs/linux-irqgen-driver/ Fill in all “TODO:” sections Take a look at other drivers in ~/nios2-linux/linux-2.6/drivers for examples Search on web for examples or references Build driver Simply hit %make in ~/linux-labs/linux-irqgen-driver/ if successful, copy custom-irqgen-driver.ko to /mnt so TFTP server can host the file

104 Final and Ultimate Exercise Steps:
Load driver TFTP to running linux on Nios-II (nios2-linux) % tftp –g <host IP> -r custom-irqgen-driver.ko Load the driver (nios2-linux) % insmod custom-irqgen-driver.ko If not yet exist, create /dev/irqgen0 (nios2-linux) % mknod /dev/irqgen0 c 251 0 When finished unload the driver (nios2-linux) % rmmod custom-irqgen-driver Test the interval modification using sysfs (nios2-linux) % echo <interval> > /sys/class/irqgen/irqgen/interval Try as interval and you should see a number printed on screen every second Debug Driver using nios2-gdb-server and printk()

105 Final and Ultimate Exercise Steps:
Write an application which Change interrupt interval uniformly % irqgen_app uniform <interval in clk cycle> Change interrupt interval incre/decrementally % irqgen_app var <start interval> <end interval> Start with templates found in ~/linux-labs/irqgen_app Fill in all “TODO” simply hit “make” in ~/linux-labs/irqgen_app copy irqgen_app to /mnt so TFTP server can host it Test application TFTP to running Nios-II Linux Run the two commands for irqgen_app shown above Try as interval for uniform and you should see a number printed on screen every second Try and as intervals for var and you should see a number printed on screen in changing interval Debug application using “run-mode” gdbserver in nios-II linux

106 Optional Exercise: If you finish too fast and nothing better to do
Add read interface to the IP Read 4KB RAM from 0x0 to 0xFFF Enhance the kernel driver read function to read current interval add 2 more files in sysfs for reading “start” and “end” interval Enhance the application which Read current start/end interval % irqgen_app status (example output) >> (Start – End) = ( – 12500) clocks Enhance the custom IP by adding “read” interface to read the RAM which stores interval values

107


Download ppt "Embedded Linux Hands-on Training"

Similar presentations


Ads by Google