Embedded system 2008/7/16 莊宜勳

56
1 Embedded system Embedded system 2008/7/16 莊莊莊

Transcript of Embedded system 2008/7/16 莊宜勳

Page 1: Embedded system 2008/7/16 莊宜勳

1

Embedded systemEmbedded system

2008/7/16 莊宜勳

Page 2: Embedded system 2008/7/16 莊宜勳

2

OutlineOutline

What is Embedded SystemEmbedded SystemBooting Process

Setup Host/Target DevelopmentHost / Target Development SetupsDevelop ToolBuilding OS

Application PortingInstall an applicationOptimizing Application Issues

Homework

Page 3: Embedded system 2008/7/16 莊宜勳

3

What is Embedded SystemWhat is Embedded System

Page 4: Embedded system 2008/7/16 莊宜勳

4

Embedded System ?Embedded System ?

WhatEmbedded System is a special-purpose computer system designed to specific functions.

WhereIt can be found everywhere

MP3 player, air condition, vehicle control system, and so on.

How“We” often use linux-based operate system

Page 5: Embedded system 2008/7/16 莊宜勳

5

The Scale of Embedded SystemThe Scale of Embedded System

Small-scaleLow-powerAbout 2 MB ROM, 4 MB RAM

Medium-scaleAbout 32MB ROM, 64MB RAMPerhaps with storage deviceEX: PDA, MP3 player

Large-scalePower-full or multi-coreUsually no resource constrain

Page 6: Embedded system 2008/7/16 莊宜勳

6

System LayerSystem Layer

Hardware

Firmware

Operating System

Application

Hardware

Firmware

Operating System

Application

Hardware

Firmware

Desktop computer Complex embedded computer

Simple embedded computer

Page 7: Embedded system 2008/7/16 莊宜勳

7

Architecture of Embedded Linux System

Page 8: Embedded system 2008/7/16 莊宜勳

8

HardwareHardware

CPUsARM (arm7tdmi, arm9, strongARM, Xscale, …etc.)

MIPS

X86, 8086

SH

PowerPC…

Memory Technology DeviceROM

Flash

RAM

HD or CF card or USB storage

Page 9: Embedded system 2008/7/16 莊宜勳

9

Hardware (cont.)Hardware (cont.)

Peripheralskeypad

USB device

RS232 (UART)

Network

IrDA

CF card others memory cards

Page 10: Embedded system 2008/7/16 莊宜勳

10

Embedded OSEmbedded OS

DOSPalm OS WinCESymbianLinux

uCLinux - without MMURTLinux - for real-time systemAndroid – Java and linux-based OS by googleOpenMoko Etc.

Page 11: Embedded system 2008/7/16 莊宜勳

11

LibraryLibrary

GNU C Library – glibcStandardInclude several libraries, ex: libm, libc, and so on.Too large for embedded system

uC-libc Original designed for uClinuxFor No MMU systemSupport m68000, ColdFire and ARMMost APIs are compatible to Glibc, but not all

uClibcAlso support MMUMore compatible to glibc, but still not allSupport m68000, ColdFire, ARM, MIPS, x86, SuperH, PowerPCSupport share library

Page 12: Embedded system 2008/7/16 莊宜勳

12

Booting ProcessBooting Process

Page 13: Embedded system 2008/7/16 莊宜勳

13

What the hell is the black box doing?What the hell is the black box doing?

Power on

BIOSLoad the hardware configurationFind the booting device

MBR of booting deviceMBR (master boot record) is in the first sector of booting deviceBoot loader is stored in the MBR of booting deviceWhen booting, it will read the booting information of boot loader in MBR.

Page 14: Embedded system 2008/7/16 莊宜勳

14

It’s time to prepare for workingIt’s time to prepare for working

Loading KernelBoot loader knows where the kernel is stored.

De-compress the compressed kernel image and start to drive the hardware device.

InitThe first executed process is init.

It reads the file “/etc/inittab”

Run-levelrun some application of /etc/rc.d/rcx.d

Login/bin/login

Page 15: Embedded system 2008/7/16 莊宜勳

15

Root FilesystemRoot Filesystem

Root filesystem contains the set of applications, libraries, and related files needed to run the system

According to the requirement of the system, the architecture of Root filesystem is different.

Generally, the most useful directories of root filesystem arebin

dev

etc

lib

sbin

usr

proc*

Page 16: Embedded system 2008/7/16 莊宜勳

16

MBRMBR

address Description Size (byte)

0 Code area 440 (max 446)

440 Optional Disk signature 4

444 Usually Nulls; 0x0000 2

446 Table of primary partitions 64

510 MBR signature 2

Total: 512 bytes

Page 17: Embedded system 2008/7/16 莊宜勳

17

What is boot loader ?What is boot loader ?

Definition of Boot LoaderThe first section of code to be executed after the embedded system is powered on.

Boot Loader in x86 PC consists of two partsBIOS (Basic Input/Output System)OS Loader (located in MBR of Hard Disk)

Ex. LILO and GRUB

In some embedded systems the role of the boot loader is more complicated

Since these systems may not have a BIOS to initial system configuration

Page 18: Embedded system 2008/7/16 莊宜勳

18

Boot loader Boot loader

Boot Loader is varied from CPU to CPU, from board to board

Since Boot Loader is very close to hardware

Hardware manufacturer may provide corresponding boot loader.

Examples:

LILO、 GRUBx86 compatible boot loader

PPCBOOTBoot loader for PowerPC based embedded Linux systems

Page 19: Embedded system 2008/7/16 莊宜勳

19

Boot loader (cont.)Boot loader (cont.)

PMONFor MIPS architecture

Das U-Boot“Universal Boot loader“

For PowerPC, ARM, XScale, MIPS, Coldfire, NIOS, x86, etc.

Page 20: Embedded system 2008/7/16 莊宜勳

20

BTWBTW

Because of the boot loader functionality, the boot loader we use have to depend on our OS

The boot loader have to “know” the kernel file-system.

LILO and GRUB support Windows and Linux, but the windows boot loader does not.

Page 21: Embedded system 2008/7/16 莊宜勳

21

GRUBGRUB

grub.confdefault 0 timeout 5

title Fedora Core root (hd0,0)kernel /vmlinuz-2.6.18-1 root=/dev/sda1 initrd /initrd-2.6.18-1.img

title=Windows XP root (hd0,5) makeactive chainloader +1

Page 22: Embedded system 2008/7/16 莊宜勳

22

Setup Host/Target DevelopmentSetup Host/Target Development

Page 23: Embedded system 2008/7/16 莊宜勳

23

First type of Host/Target Development First type of Host/Target Development SetupsSetups

Linked SetupHost contains the cross-platform development environment

Target contains an appropriate bootloader, kernel, and root filesystem

Kernel could be available via TFTP

Root filesystem could be NFS

Page 24: Embedded system 2008/7/16 莊宜勳

24

Second type of Host/Target Development Second type of Host/Target Development SetupsSetups

Removable Storage Setup OS is written into storage by the host, and then is transferred to the target, and is used to boot the target deviceHost contains the cross-platform development environmentTarget contains bootloaderThe rest of the components are stored on a removable storage media

Page 25: Embedded system 2008/7/16 莊宜勳

25

Third type of Host/Target Development Third type of Host/Target Development SetupsSetups

Standalone Setup Target is a self-contained development system and includes all the required software to boot, operate, and develop additional software

Page 26: Embedded system 2008/7/16 莊宜勳

26

Heterogeneous EnvironmentHeterogeneous Environment

Page 27: Embedded system 2008/7/16 莊宜勳

27

Cross-Compiler ToolchainCross-Compiler Toolchain

Toolchain means not only compilerBut also Library, Linker (ld), assembler (as), other binutils, etc.

For two reasons we need the ToolchainDifferent architecture (ex: X86 & arm)

Different Library

Usually Toolchain is downloaded from Internet and just use it

If you have to setup Toolchain by yourself, you will get into big trouble

Page 28: Embedded system 2008/7/16 莊宜勳

28

Setup Cross-Compiler ToolchainSetup Cross-Compiler Toolchain

Componentsgcc

binutilsas, ld, nm, etc

Libraryglibc or uClibc

PatchFix bug

Add some functions

Page 29: Embedded system 2008/7/16 莊宜勳

29

Setup Cross Compiler ToolchainSetup Cross Compiler Toolchain

Versions are very importantnot all versions of one tool will build properly when combined with different versions of the others“New” doesn’t mean “Suitable”The only way to find the appropriate tool set is just “Try” or Google it

Page 30: Embedded system 2008/7/16 莊宜勳

30

Setup Cross Compiler ToolchainSetup Cross Compiler Toolchain

Five main steps

1. Kernel headers setup

2. Binary utilities setup

3. Bootstrap compiler setupSome languages supported by gcc, such as C++, require C library support

Only support C language here

4. C library setupCompile library used in target system

5. Full compiler setupBuild full compiler with C library

Page 31: Embedded system 2008/7/16 莊宜勳

31

Develop ToolDevelop Tool

Page 32: Embedded system 2008/7/16 莊宜勳

32

Make and MakefileMake and Makefile

Development problemsIt is hard to manage the relationship of files in large project.Every change requires long compilation

MotivationTo manage the project well and automatically in the case of

Many lines of codeMultiple componentsMore than one programmer

Page 33: Embedded system 2008/7/16 莊宜勳

33

Make and Makefile (cont.)Make and Makefile (cont.)

A Makefile is a file (script) containingProject structure (files, dependencies)

Instructions for files creation

The “make” command reads a Makefile, understands the project structure and makes up the executable

Note that the Makefile mechanism is not limited to C programs

Page 34: Embedded system 2008/7/16 莊宜勳

34

MakefileMakefile

Rule syntax

main.o: main.c sum.h

gcc –c main.c Rule

actiondependency

tab

Page 35: Embedded system 2008/7/16 莊宜勳

35

MakefileMakefile

ExampleProgram contains 3 files

main.c., sum.c, sum.h

sum.h included in both .c files

Executable should be the file summarysummary

sum.omain.o

sum.csum.h sum.hmain.c

Page 36: Embedded system 2008/7/16 莊宜勳

36

Makefile (cont.)Makefile (cont.)

summary: main.o sum.o

gcc –o summary main.o sum.o

main.o: main.c sum.h

gcc –c main.c

sum.o: sum.c sum.h

gcc –c sum.c

Page 37: Embedded system 2008/7/16 莊宜勳

37

Building your OSBuilding your OS

Page 38: Embedded system 2008/7/16 莊宜勳

38

Building uClinuxBuilding uClinux

uClinux-dist http://www.uclinux.org/pub/uClinux/dist/

Full source packageincluding kernel, libraries and application

Page 39: Embedded system 2008/7/16 莊宜勳

39

Platform ConfigPlatform Config

make menuconfig/ make xconfigSelect your platform & kernel version

Page 40: Embedded system 2008/7/16 莊宜勳

40

Kernel ConfigKernel Config

Kernel setting

Page 41: Embedded system 2008/7/16 莊宜勳

41

Application ConfigApplication Config

Application setting

Page 42: Embedded system 2008/7/16 莊宜勳

42

Start to compile uClinuxStart to compile uClinux

Compilemake dep

Check the dependence of files

make

make Errors occur solve it (Google it) make again

Page 43: Embedded system 2008/7/16 莊宜勳

43

Make for each componentsMake for each components

Make linux_onlyUsed to make kernel

Make user_onlyUsed to make application

Make lib_onlyUsed to make necessary library

Make romfs將編譯好的用戶程式產生 Romfs檔系統( romfs目錄)。

Make image根據 romfs目錄產生檔系統映射檔,然後編譯核心,產生核心映射檔。

Page 44: Embedded system 2008/7/16 莊宜勳

44

Final outputFinal output

Finally, there are two files generated:zImage

uClinux kernel 2.4.x compress image

romfs.imgRom file-system

Write files into corresponding locationbootloader.bin

zImage

romfs.img

Page 45: Embedded system 2008/7/16 莊宜勳

45

Application PortingApplication Porting

Page 46: Embedded system 2008/7/16 莊宜勳

46

Install an applicationInstall an application

Configureconfigure –h

for information about parameters

Some times the Makefile is generated by configureconfigure --parameters

Ex: configure --enable-release --enable-optimizations

Compilemake

make Errors occur solve it (Google it) make again

make installInstall application or lib into specific location

Page 47: Embedded system 2008/7/16 莊宜勳

47

Example: VLCExample: VLC

EnvironmentFedora Core 4 / Fedora Core 8

Kernel: 2.6.11-1 / 2.6.14

vlc-0.8.6b.tar.bz2configure --enable-dvb

make; make install

Page 48: Embedded system 2008/7/16 莊宜勳

48

When installingWhen installing

Add the path of Toolchain to PATHexport PATH=/example/toolchain/path:$PATH

--prefix=PREFIXIndicate where to install application

--target=TARGETconfigure for building compilers for TARGET [HOST]

Page 49: Embedded system 2008/7/16 莊宜勳

49

Optimizing Application IssuesOptimizing Application Issues

Page 50: Embedded system 2008/7/16 莊宜勳

50

Down SizeDown Size

Remove unused part of applicationconfigure --disable-(something)

Reduce binary code sizestrip

One of binutils tool

Strip symbols and debug messages from object files

uClinuxELF -> FLAT

Optimum size in compile timegcc -Os

Cut down libraryAce in the hole

Page 51: Embedded system 2008/7/16 莊宜勳

51

EfficiencyEfficiency

Dynamic linking -> Static

Use more efficient function in programEx: memcpy vs mmap

Even use assembly language to rewrite critical part

Optimum in configureconfigure --enable-release --enable-optimizations

Turn up gcc optimum levelgcc -O0 ~ -O3

Page 52: Embedded system 2008/7/16 莊宜勳

52

ETC.ETC.

Low response time

Minimal memory usage

Power saving

Page 53: Embedded system 2008/7/16 莊宜勳

53

HomeworkHomework

Page 54: Embedded system 2008/7/16 莊宜勳

54

VLCVLC

Fulfill install process of VLC by page 35Report configure parameters, encountering problems, the result and what you has learned from this

Page 55: Embedded system 2008/7/16 莊宜勳

55

ReferenceReference

Building Embedded Linux Systems, Karim Yaghmour, O’Reilly, 2003

uClinux, http://www.uclinux.org

VideoLAN developers, http://www.videolan.org/developers/vlc.html

Page 56: Embedded system 2008/7/16 莊宜勳

56

This slider was originally written by lijw in 2006

revised by erdatsai in 2007

revised by JACKY in 2008