Upload
arron-cobb
View
232
Download
8
Tags:
Embed Size (px)
Citation preview
© 2011 Altera Corporation—Confidential
Embedded Linux Hands-on Training
For Embedded RSC and Specialists
Nick Ni
Embedded Applications Group
© 2011 Altera Corporation—Confidential
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
2
© 2011 Altera Corporation—Confidential
Let’s introduce yourself
Let’s briefly introduce yourself around the table
3
© 2011 Altera Corporation—Confidential
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
4
© 2011 Altera Corporation—Confidential
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
5
© 2011 Altera Corporation—Confidential
Requirements – NEEK Board
6
© 2011 Altera Corporation—Confidential
Requirements – Linux PC
7
Windows 7 PC- VirtualBox - VirtualBox image for CentOS 6
(provided) (~20GB)
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
8
© 2011 Altera Corporation—Confidential
Linux Basics
9
© 2011 Altera Corporation—Confidential
What is Linux?
10
Linux – the kernel from www.kernel.org Linux Distribution – LOTS of packages (1000+)
- Mainly GNU Contribution
Linux Kernel
Linux Distribution
Perl
Make Toolchains
GCC/GDB
Media Libraries
Text Editor
© 2011 Altera Corporation—Confidential
Why Customers choose Linux
Cost- Royalty free- Free tools- Free source code
Powerful Complete Modifiable Popular
- Huge Community
Portable
11
© 2011 Altera Corporation—Confidential
Embedded Linux Usage
12
© 2011 Altera Corporation—Confidential
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 http://en.wikipedia.org/wiki/Comparison_of_Linux_dis
tributions13
© 2011 Altera Corporation—Confidential
Popular desktop distributions
14
© 2011 Altera Corporation—Confidential
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 2.5.46 Consists of patches and tools for microcontrollers. Consists of small-size library packages
- busybox- dropbear- gdbserver- …
15
© 2011 Altera Corporation—Confidential
Let’s play around with Linux!
You should already have VirtualBox installed
Load CentOS 6 VirtualBox Image
Right click and “open terminal”
16
© 2011 Altera Corporation—Confidential
Getting Help
Command manual- % man <command>- E.g. % man tar
Command help- % <command> --help
17
© 2011 Altera Corporation—Confidential
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.
18
© 2011 Altera Corporation—Confidential
Package Installation
Different distributions use different installers
19
Distribution
CentOS YUM
RedHut YUM
DEBIAN APT
UBUNTU APT
FEDORA YUM
Look at YUM Help to see how to use YUM
© 2011 Altera Corporation—Confidential
Linux File Properties
Each file has<dir><root rwx><group rxw><user rxw> <directories> <owner> <group> <size> <last
modified date> <filename>
20
© 2011 Altera Corporation—Confidential
User and Privileges
Variety of commands for user/privilege control
21
Command 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 -
© 2011 Altera Corporation—Confidential
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
22
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 file2Output “hello world” to console% echo “hello world”Read file content of “file1”% cat file1
© 2011 Altera Corporation—Confidential
I/O Redirection
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
23
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
© 2011 Altera Corporation—Confidential
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 http://www.systemverilog.in/pdf/LINUX-KACPER.pdf and google
24
© 2011 Altera Corporation—Confidential
Pop Quiz
25
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
© 2011 Altera Corporation—Confidential
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.
26
© 2011 Altera Corporation—Confidential
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
27
© 2011 Altera Corporation—Confidential
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
Run GVIM28
© 2011 Altera Corporation—Confidential
Exercise: Write a Shell Script
Your file should begin with “#!/bin/sh”
Write a shell script that creates a file that looks like:
29
********************************************************** This file contains all files with .c extension under <current directory> containing word “string” **********************************************************<file 1><file 2>…
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
30
© 2011 Altera Corporation—Confidential
High-level Architecture
31
Hardware
© 2011 Altera Corporation—Confidential
Software Components
Cross-compiler- 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
32
© 2011 Altera Corporation—Confidential
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
33
© 2011 Altera Corporation—Confidential
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!
34
© 2011 Altera Corporation—Confidential
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
35
© 2011 Altera Corporation—Confidential
36
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
Mount root filesystem from a media (Flash)
Runs the program init based on /etc/inittab
Runs user init /etc/rc
Boot Sequences
Uncompress initialize kernel
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
37
© 2011 Altera Corporation—Confidential
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!
38
© 2011 Altera Corporation—Confidential
Images you will build
Linux kernel 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)
39
© 2011 Altera Corporation—Confidential
40
Configure uClinux with- vendor- processor- platform
Configure Linux Kernel
Configure User Applications/Library
Build all images *except* u-boot
Re-Build Linux Kernel with ramdisk-initramfs_data.cpio-linux.initramfs.gz
Build User app/lib-rootfs.initramfs-rootfs.initramfs.gz-rootfs.jffs2
Build linux kernel- vmlinux- vmlinux.bin- vmlinux.gz- vmImage
uClinux Build Flow
Generate and customize device tree from .sopcinfo
Under the hood
© 2011 Altera Corporation—Confidential
U-Boot Build Flow
U-Boot needs to be built independently from Linux Kernel and Root filesystem
41
Generate and Customize U-Boot header file from sopcinfo
Build u-boot
© 2011 Altera Corporation—Confidential
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
42
© 2011 Altera Corporation—Confidential
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:
43
BoardinfoGeneral
Select master: cpu:cpuMemory nodes: select both memory (CTRL key)
Click ApplyKernel Bootargs: debug console=ttyJ0,115200
Click ApplyEthernet
Highlight tse_macEdit MAC-address
Click ApplyOutput
Save As: <your choice of name>.dts
© 2011 Altera Corporation—Confidential
Exercise: Customize Device Tree
Fix the following bold parts in generated .dts manually to fix the tool bug
44
ext_flash: flash@0x100000 { #address-cells = < 1 >; #size-cells = < 1 >; compatible = "ALTR,cfi_flash-altera_generic_tristate_controller", "cfi-flash"; reg = < 0x00100000 0x00b00000 >; bank-width = < 2 >; device-width = < 1 >; }; //end flash@0x0 (ext_flash)…
ethernet_subsystem_ethernet_bridge: bridge@0x800a000 {compatible = "ALTR,avalon-12.0","simple-bus";reg = < 0x800a000 0x3000>;#address-cells = <1>;#size-cells = <1>;ranges = <0x00000000 0x0800A000 0x00000400
0x00000440 0x0800A440 0x000000400x00000400 0x0800A400 0x000000400x00002000 0x0800C000 0x00001000>;
ethernet_subsystem_tse_mac: ethernet@0x0 {
compatible = "ALTR,tse-12.0","ALTR,tse-1.0";reg = < 0x0 0x400 0x400 0x40 0x440 0x40 0x2000 0x1000>;interrupt-parent = < &cpu >;interrupts = < 1 6 >;
© 2011 Altera Corporation—Confidential
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
45
© 2011 Altera Corporation—Confidential
Exercise: Configure Linux Kernel
Menuconfig of linux kernel should appear soon after Select the followings
46
Platform OptionsMem base: 0x10000000Compile and link device tree into kernel imageAdd path to Device tree source fileEnable MUL/ DIV instruction
Processor Type and FeaturesUnselect “Ignore kernel command string from DTB”
Device DriversNetwork device support
Ethernet driver supportAltera DeviceAltera Triple-Speed Ethernet Mac support
Character devicesEnable /dev/kmem virtual device supportSerial drivers
Altera JTAG UART supportAltera JTAG UART console support
Memory Technology Device (MTD) supportMapping 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
© 2011 Altera Corporation—Confidential
Exercise: Configure Filesystem
uClinux Distribution Configuration menuconfig will appear
Select BusyBox -> Networking Utilities -> httpd Exit and Save
47
© 2011 Altera Corporation—Confidential
Exercise: Build Linux Kernel and Filesystem Simply type “make” in uClinux-dist directory to
kick-off the build process On VirtualBox, it will take 30-60 min Native box would be much faster (~10min)
48
© 2011 Altera Corporation—Confidential
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
49
© 2011 Altera Corporation—Confidential
Exercise: You are ready to bring-up NEEK with Linux! You should have:
50
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
51
© 2011 Altera Corporation—Confidential
52
Convert U-Boot image to a BootROM image and program to CFI flash
Boot U-Boot and set up ethernet and erase sections of CFI flash
Fetch RootFS, Kernel and device tree from TFTP server and program them to CFI
Hit reset and linux boot up!
Board Bring-up Flow
© 2011 Altera Corporation—Confidential
CFI Flash Partitioning
BootROM / U-Boot (0x0)
RootFS (0x100000)
U-Boot Env (0xC00000)
Device Tree(0xD00000)
Linux Kernel (0xE00000)
53
U-Boot View
Linux View
© 2011 Altera Corporation—Confidential
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
54
© 2011 Altera Corporation—Confidential
Exercise: Install TFTP server
TFTPD64 http://tftpd32.jounin.net/tftpd32_downl
oad.html
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”55
© 2011 Altera Corporation—Confidential
Exercise: Program U-Boot asBootROM image
56
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 dataGo to shared_folder% elf2flash --input u-boot --output u-boot.flash --base 0xe0000000 --end
0xe0ffffff --reset 0xe0000000 --boot=<path to nio2eds>/components/altera_nios2/boot_loader_cfi.srec
- Download into Flash% ../nios2-flash-programmer --base=0xe0000000 u-boot.flash
- Start Nios2-terminal and hit “CPU reset” on the board- You should see U-Boot booted up
© 2011 Altera Corporation—Confidential
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 d1000000 20000;
bootm e0e00000 – d1000000'
- Program RootFS % protect off all % erase e0100000 +00f00000 % tftp d1000000 rootfs.jffs2 % cp.b d1000000 e0100000 $filesize
- Program Kernel and Device Tree % tftp d1000000 system.dtb % cp.b d1000000 e0d00000 $filesize % tftp d1000000 vmImage % cp.b d1000000 e0e00000 $filesize
- Save U-Boot environments % saveenv
57
© 2011 Altera Corporation—Confidential
Exercise: Cross your fingers!
Hit “CPU reset” on the board and… Hope Linux boots completely!
58
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
59
© 2011 Altera Corporation—Confidential
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- …
60
© 2011 Altera Corporation—Confidential
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. http://busybox.net/
61
© 2011 Altera Corporation—Confidential
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)
62
© 2011 Altera Corporation—Confidential
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
63
© 2011 Altera Corporation—Confidential
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:- root@<NEEK IP>
64
© 2011 Altera Corporation—Confidential
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”- Save and Exit- % make
Once finished, look into romfs/etc/dropbear and romfs/etc/inetd.conf to see what got generated
65
© 2011 Altera Corporation—Confidential
Exercise: Use Distribution to build SSH (Cont’d)
66
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 e1000000 +00b00000% tftp d1000000 rootfs.jffs2% cp.b d1000000 e0100000 $filesize
Hit “CPU Reset” on the board and linux should boot up with SSH server automatically started
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
67
© 2011 Altera Corporation—Confidential
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- …
68
© 2011 Altera Corporation—Confidential
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
69
© 2011 Altera Corporation—Confidential
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- In SSH client
% echo “<string>” > /dev/ttyJ0
70
© 2011 Altera Corporation—Confidential
Linux Application
What you write- 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
71
© 2011 Altera Corporation—Confidential
Linux Application Build Flow
72
.c/.h
.a/.so
.o
.a/.so
GCC
.c/.h
.o
GCC
.c/.h
.o
GCC
.c/.h
.o
GCC
AR
Binary / ELF
GCC GCC GCC
User Library Application
Standard C Lib
© 2011 Altera Corporation—Confidential
Compiler and Useful Tools
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
73
Native desktop tools without any prefix Cross-compiler adds prefix
e.g. nios2-linux-gnu-gcc e.g. arm-none-linux-gnueabi-objdump
© 2011 Altera Corporation—Confidential
I/O Libraries in Standard C
74
API Description API Descriptionfopen opens a file vfscanf a file stream or a buffer using variable argument listfreopen opens a different file with an existing stream vsscanffflush 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 buffersetbuf sets the buffer for a file stream sprintfsetvbuf sets the buffer and its size for a file stream snprintf
fwideswitches a file stream between wide character I/O and narrow character I/O vprintf prints formatted byte/wchar_t output to stdout,
fread reads from a file vfprintf a file stream, or a buffer using variable argument listfwrite writes to a file vsprintffgetc
reads a byte/wchar_t from a file streamvsnprintf
getc ftell returns the current file position indicatorfgets reads a byte/wchar_t string from a file stream fgetpos gets the file position indicatorfputc
writes a byte/wchar_t to a file streamfseek moves the file position indicator to a specific location in a file
putc fsetpos moves the file position indicator to a specific location in a filefputs writes a byte/wchar_t string to a file stream rewind moves the file position indicator to the beginning in a filegetchar reads a byte/wchar_t from stdin clearerr clears errorsgets reads a byte string from stdin feof checks for the end-of-fileputchar writes a byte/wchar_t to stdout ferror checks for a file error
puts writes a byte string to stdout perrordisplays a character string corresponding of the current error to stderr
ungetc puts a byte/wchar_t back into a file stream remove erases a filescanf reads formatted byte/wchar_t input from stdin, rename renames a filefscanf a file stream or a buffer tmpfile returns a pointer to a temporary filesscanf tmpnam returns a unique filenamevscanf reads formatted input byte/wchar_t from stdin, strcmp compares two c strings.
Add the following line in your source file- #include <stdio.h>
See the detailed API syntax- % man <API>
© 2011 Altera Corporation—Confidential
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)
75
© 2011 Altera Corporation—Confidential
Makefile
CC = nios2-linux-gnu-gccCFLAGS = -gTARGET = uart_appobjs = uart_app.o
all: $(TARGET)
uart_app: $(objs) $(CC) $(CFLAGS) $(objs) -o $@
.c.o: $(CC) $(CFLAGS) $< -c -o $@
clean: rm -rf $(objs) $(TARGET)
76
Target
TAB
© 2011 Altera Corporation—Confidential
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”
77
© 2011 Altera Corporation—Confidential
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
78
© 2011 Altera Corporation—Confidential
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!
79
© 2011 Altera Corporation—Confidential
Agenda
Agenda
Linux Basics
Introduction to embedded Linux
Building Linux Packages
Board Bring-up
Software Packages
Application development
Driver development
80
© 2011 Altera Corporation—Confidential
User space and Kernel space
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
81
© 2011 Altera Corporation—Confidential
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
82
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
© 2011 Altera Corporation—Confidential
System Call Interface in Driver
These functions will be called by user application - Like the one you wrote in the UART application
83
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 };
© 2011 Altera Corporation—Confidential
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
84
© 2011 Altera Corporation—Confidential
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);
}
85
© 2011 Altera Corporation—Confidential
/dev/mem Interface
/dev/mem- 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.
86
© 2011 Altera Corporation—Confidential
/dev/mem usage example
fd = open(/dev/mem”, O_RDWR|O_SYNC);
// Map physical memory range of a peripheral memory map (0xE8000080 – 0xE800017f) to userspace memorymem = mmap(NULL, 0x100, PROT_READ|PROT_WRITE, MAP_SHARED, \ fd, 0xE8000080);
// Write to E8000084 in Peripheral register map*(mem+4) = 0x2f
// Read from E8000088 in Peripheral register mapvalue = *(mem+8);
87
© 2011 Altera Corporation—Confidential
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);
88
© 2011 Altera Corporation—Confidential
Device Files
Usually sit in /dev type
- 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
89
© 2011 Altera Corporation—Confidential
Allocating kernel resource
Reserve a major/minor number Register System Call Interface
90
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;}
© 2011 Altera Corporation—Confidential
Interrupt Registration
91
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);
© 2011 Altera Corporation—Confidential
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)
92
© 2011 Altera Corporation—Confidential
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
93
© 2011 Altera Corporation—Confidential
Loadable Kernel Module
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 *.korm *.mod.*rm *.symversrm *.order
94
Makefile points to kernel source Generate a .ko file Load .ko file in running Linux using “insmod”
© 2011 Altera Corporation—Confidential
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”
95
© 2011 Altera Corporation—Confidential
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
96
© 2011 Altera Corporation—Confidential
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/
97
© 2011 Altera Corporation—Confidential
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
98
© 2011 Altera Corporation—Confidential
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,115200 kgdbwait console=ttyJ0,115200 root=/dev/mtdblock0 rootfstype=jffs2 rw; cp.b e0d00000 d1000000 20000; bootm e0e00000 - d1000000‘
- (U-boot) % saveenv
Reset the board and let Linux Boot
99
© 2011 Altera Corporation—Confidential
Debugging with KGDB (cont’d)
Linux booting should pause at- ttyAL0 at MMIO 0x6000000 (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 115200- (GDB) % target remote /dev/ttyUSB0- (GDB) % symbol-file linux-2.6.x/vmlinux
Now the GDB should work the same way
100
© 2011 Altera Corporation—Confidential
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)
101
© 2011 Altera Corporation—Confidential
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 offsetsBit field definition IP base address (+0xE0000000 for Virtual Address)
102
© 2011 Altera Corporation—Confidential
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
103
© 2011 Altera Corporation—Confidential
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 12500000 as interval and you should see a number printed on
screen every second
Debug Driver using nios2-gdb-server and printk()
104
© 2011 Altera Corporation—Confidential
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 12500000 as interval for uniform and you should see a number printed on screen
every second- Try 12500 and 12500000 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
105
© 2011 Altera Corporation—Confidential
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) = (125000000 – 12500) clocks
Enhance the custom IP by adding “read” interface to read the RAM which stores interval values
106
© 2011 Altera Corporation—Confidential ALTERA, ARRIA, CYCLONE, HARDCOPY, MAX, MEGACORE, NIOS, QUARTUS and STRATIX words and logos are trademarks of Altera Corporation and registered in the United States and are trademarks or registered trademarks in other countries.
Thank You