Build an embedded Linux System Based on at91sam9g20

Source: Internet
Author: User

The target platform at91sam9g20 used in this paper is a SOC embedded microprocessor developed by ATMEL using the ARM926EJ-S processor kernel. It clock speed reaches 400 MHz and has the Advanced Peripheral DMA and distributed memory architecture of ATMEL, together with the 6-layer Bus Matrix, the system can transmit multiple data between memory, peripherals, and external interfaces at the same time without consuming the CPU clock cycle. Compared with the pin-compatible 200 MHz at91sam9260, at91sam9g20 provides up to 4 times of high-speed cache and On-Chip SRAM memory, with enhanced external nand flash memory error correction, and larger Ethernet FIFO, reduces transmission latency. The clock frequency of its external bus interface (EBI) is 133
MHz, used for high-speed data transmission of off-chip memory. These features allow developers to port windows SCE, Linux, and other operating systems to the target system based on this microprocessor.

The operating system is also an important part of the embedded system. Today's embedded operating systems include VxWorks, QNX, Palm OS, Windows CE, Linux, uClinux, and UCOS II. Each operating system has its own distinctive features, the embedded operating system transplanted in this article is Linux. Linux applications with extensive open source code have been transplanted to the embedded runtime environment, which can be customized or modified and transplanted to the hardware platform. Therefore, the embedded operating system is developed using Linux, it can accelerate the development of embedded systems and shorten the time for products to enter the market.

This article mainly includes three parts: Boot Loader implementation, Linux kernel transplantation, and file system implementation.

Build a cross-Development Environment

Before porting the operating system, you must first build a development environment on a PC with Linux installed. For example, if our host machine is ubuntu10.04, to obtain all operation permissions, log on to the Linux system as the root user. Create an arm path under the usr/local/directory, download the cross-compilation tool arm-linux-gcc-4.3.2.tgz, copy it to the folder/usr/local/ARM/, and decompress it; finally, you need to modify the environment variables and set the default cross-compilation tool to arm-linux-gcc-4.3.2. Use the Linux editing tool (such as Vim) to open the/root/. bashrc file and add the following code at the end of the file:

If

[-D/usr/local/arm];

Then Path =/usr/local/ARM/4.3.2/bin: "$ {path }"

Fi

After the cross-compilation tool is set up, you can enter the command arm-Linux-gcc-V in the terminal to verify that the tool is successfully set up, after successful setup, the arm-Linux-GCC version is displayed under the terminal. Note that you must log out of the user and log on to the system again before the setting takes effect.

Bootstrap program

After the system is powered on, a program is required for initialization: Shut down the dog, change the system clock, initialize the storage controller, and copy more code to the memory. This program is called boot loader. In short, boot loader is executed after the system is powered on. It initializes the hardware device, prepares the software environment, and finally calls the operating system kernel. The implementation of boot loader is very dependent on specific hardware. in embedded systems, hardware configurations vary widely. Even with the same CPU, peripheral resources are also different. Therefore, you need to port the boot loader based on specific hardware.

Boot Loader has two different operation modes: Start loading mode and download mode. After power-on, Boot Loader loads the operating system to ram from a solid-state storage device on the board. The entire process is not involved by users and is generally used for final products. The download mode is used during the development process. developers can use various commands to download files (such as kernel images and file system images) from the host through serial port or network communication ), store them directly into memory or flash-type solid-state storage devices.

To run the embedded operating system on at91sam9g20, at91sam9g20 adopts three methods. the boot loader consists of three parts: romboot, bootstrap, and U-boot. the process 1 of the level-1 Bootstrap program is shown in.


Figure 1: 3. flowchart of the level-1 Bootstrap program

The first-level boot program romboot is fixed in the at9lsam9g20, and runs the Boot Code after power-on or reset, the function is to load the second-level Bootstrap program stored in external flash to the SRAM inside the CPU for execution. Bootstrap is stored in the first 4 kb of External Flash space. Its functions include initializing clock, SDRAM Controller, debug serial port, and other hardware resources, and load level 3 boot program U-boot from flash to SDRAM for execution. U-boot guides and loads the embedded Linux operating system from flash into SDRAM, and gives control of CPU to Linux.

The U-boot version used for this transplantation is 1.3.4. This article will not detail it.

Kernel cropping and compilation

1. Preparations

And 2.6.30-at91-exp.3.tar.gz ). In the Linux development environment of the host machine, create your own working directory. For example, the working directory is home/work/development. Complete this step, as shown in figure 2.


Figure 2: preparations completed

2. Add a patch file

Go to the above linux-2.6.30 root directory and execute the following two commands:

Zcat 2.6.30-at91.patch.gz | patch-p1

For P in 2.6.30-at91-exp.3/*; do patch-P1 <$ P; done

After successful execution, will be downloaded from the Atmel official website for at91sam9g20 patch files added to the linux-2.6.30 kernel, including drivers, Kernel configuration files and so on.

3. U-boot the Linux Kernel

A. Determine the machine code

Generally, there is a machine code (mach_type) in both U-boot and kernel. The kernel can be guided only when the two machine codes are consistent. First, determine the mach_type in U-boot, In the U-boot include/ASM-arm/mach-types.h file for different CPUs defined a lot of mach_type, you can find the following definition:

# Define mach_type_at91sam9g20ek_2mmc 2288

Secondly, determine the mach_type in the kernel. In the ARCH/ARM/tools/Mach-types file of linux2.6.30, many mach_types are defined for different CPUs. You can also find the following definition:

At91sam9g20ek_2mmc mach_at91sam9g20ek_2mmc at91sam9g20ek_2mmc 2288

Only when the two definitions are consistent can u-boot the kernel. Otherwise, Mach errors may occur.

B. Add a boot address

The head. s file located in the kernel platform arm directory kernel is completed using assembly code, which is the first file to be executed by the kernel. The main function of this assembly code is to check the cpu id, architecture number, initialize page tables, CPU, BBS, and other operations, and jump to the start_kernel function.

Locate entry (stext) and add the following code:

Entry (stext)

MoV r0, #0

MoV R1, #0x8f0

LDR R2, = 0x20000100

Where R1 is stored as ubuntureid (0x8f0 in hexadecimal 2288), R2 stores the atags pointer. Linux Kernel startup sequence 3 is shown in.


Figure 3: Linux kernel startup sequence

4. Configure and compile

First, configure the compilation tool, open the MAKEFILE file in the Linux root directory, and find the following code to modify it:

Export kbuild_buildhost: =$ (subarch)

Arch? = Arm

Cross_compile? =/Usr/local/ARM/4.3.2/bin/ARM-Linux-

Develop platform for ARM platform, cross-compilation tool for arm-linux-gcc-4.3.2; Save and enter the linux-2.6.30 root directory, execute the following command:

# Make distclean

# Make clean

This step deletes some intermediate and temporary files generated by previous compilation to avoid compilation errors. Finally, execute the command

# Make at91sam9g20ek_2mmc_defconfig

# Make uimage

That is, the kernel is configured according to at91sam9g20, and the kernel image file is compiled and generated. Make uimage is a tool generated by u- boot compilation. You can find the mkimage file in the/tools directory of uboot, copy it to the/usr/local/bin directory of the system to run the make uimage command. After the compilation is complete (it may take about 10 minutes), you can find the uimage file in the arch/ARM/boot/directory, which is the kernel image file we need. You can download it to the Work Control Board to run it.

5. Modify and add drivers

The kernel image file generated in the preceding steps is cropped and modified according to the configuration on the Atmel official website. Because of the actual hardware configuration, you must modify the corresponding driver for the specific hardware device, to add a driver to the Linux kernel, you need to do the following:

Copy the written source code to the corresponding directory of the Linux kernel source code. Add the compilation configuration options of the project corresponding to the new source code to the kconfig file in the directory. Add the compilation conditions for the new source code to the MAKEFILE file in the directory.

Add the nandflash driver as an example. The storage chip of the at91sam9g20 work control board is implemented by Samsung's k9f2g08u0a. For example, the driver for at91sam9g20 is 9g20_nandflash.c, add this file to the/linux2.6.30/driver/MTD/NAND directory and modify the kconfig and makefile files. Add makefile

OBJ-$ (config_mtd_nand_atmel) + = 9g20_nandflash.o

Add the following code to the kconfig file:

Config mtd_nand_atmel

Tristate "Support for sam9g20 NAND Flash"

Depends on arch_at91

Help

Run make menuconfig in the root directory of linux2.6.30 and select the nandflash driver. As shown below.

Device Drivers --->

<*> Memory technology device (MTD) support --->

<*> NAND device support --->

<*> Support for sam9g20 NAND Flash

Configure the Kernel File and select the option to be configured. Press the "y" key to compile the option into the kernel. Press the "M" key to compile the option into the kernel module, press the "N" key to delete this option from the kernel.

To add partition information, add the partition table for NAND Flash in the board-sam9g20ek-2slot-mmc.c file:

Static struct mtd_partition _ initdata ek_nand_partition [] = {

{

. Name = "bootloader ",

. Offset = 0,

. Size = 0x00200000,

}, // 0 partitions

{

. Name = "Linux kernel ",

. Offset = 0x00200000,

. Size = 0x00200000,

}, // 2 m

{

. Name = "cramfs ",

. Offset = 0x00400000,

. Size = 0x00a00000,

}, // 10 m

{

. Name = "FS ",

. Offset = 0x00e00000,

. Size = 0x0f200000,

},

};

After saving, recompile the kernel to generate the kernel image file with the nandflash driver.

According to the partition table above, the nandflash Resource Distribution 4 shows that the bootloader is stored in the flash memory as described by the boot program, linux Kernel and the storage of the file system on this industrial control board.


Figure 4: NAND Flash resource distribution chart

File System implementation

When selecting a file system, you must consider the reliability, robustness, and enhancement requirements based on the needs of specific applications. For applications such as industrial control that do not need to update control programs frequently, it is sufficient to select a read-only file system such as caramfs, in addition, the cramfs compression rate is as high as 50%, which can greatly save our storage space. However, for applications that need to store data such as data collection, read-only file systems cannot meet the needs of application systems, we can choose a readable file system like jffs or yaffs. However, in practical applications, more factors need to be considered. The cramfs + jffs2 file system is used based on the characteristics of different file systems and the 9g20.

The procedure for creating a file system is as follows:

Download busybox and compile it to generate the most common commands in Linux. Here you can select a common system tool as needed. This file system uses the busybox-1.14.2, the use of cross-compilation tool for the arm-linux-gcc-4.3.2, here you need to note that the compilation of busybox and the compilation of Linux kernel cross-compilation tool is best to use the same version of the compilation tool.

The files required to create a file system are as follows:

Mkdir/9g20_rootfs

CD/9g20_rootfs

Mkdir bin Dev etc home lib MNT proc sbin sys tmp usr VaR

Copy the bin and sbin directories generated by busybox to the 9g20_rootfs directory, copy the dynamic connection library files required for compiling busybox to the 9g20_rootfs/lib directory.

Write the required configuration files, such as the etc directory's cross section, inittab, and fstab files, and pay attention to the File Execution permissions.

Download cramfs tool cramfs-1.1.tar. After compilation, copy the generated executable program mkcramf to the/usr/bin directory and run mkcramfs 9g20_rootfs 9g20. cramfs. The generated 9g20. cramfs file is the required file system.

Add the jffs2 partition and add the following command to the/etc/init. d/RCS file:

# Mount-T jffs2/dev/mtdblock3/home

Mtdblock3 will be mounted as the home directory in jffs2 file system, so that the/home directory is readable and writable, which not only solves the cramfs read-only restrictions, but also the system file is read-only, user programs and data can be read and written, which helps to protect system files and reduce the volume of file systems, especially for embedded systems.

After downloading the generated kernel image and file system image to the control panel, you can see that after the transplantation is complete, the system starts normally and the system runs normally and stably after the test is transplanted.

Summary

This article systematically describes how to build an embedded Linux system with the arm9processor of at91sam9g20 as the hardware platform, and describes how to write the driver in Linux using the nandflash driver as an example, finally, the file system solution is provided. Building an embedded Linux system is a complex subject and requires a certain understanding of the Linux operating system kernel, especially the Startup Process of Linux. The preparation of the driver must be familiar with hardware knowledge, we need to be familiar with the Linux kernel data structure. In addition, the real-time, security, stability, and simplification of the system need to be further considered by developers in the design. After system testing, the Linux system transplanted in this article runs stably, and the good performance of at91sam9g20 is fully applied, providing a solid foundation for subsequent application development.

Author: Yuan shasha, Jiang Jian, Chen Wei

References

[1] ATMEL corporation. at91sam9g20 preliminary, 2009.

[2] [us] Bouvet, sister, Chen Lijun, Zhang qiongsheng, and Zhang Hongwei. a deep understanding of the third edition of Linux kernel [M]. Beijing: China Power Press, 2007.09.

[3] Wei Dongshan. Full manual for Embedded Linux application development [M]. Beijing: People's post and telecommunications press, 2008.08.

[4] Huang shenxi, Fan Xiaoping, Liu lifang, Yang shengyue. Embedded Linux boot program design based on at91sam926x [J]. Microcomputer Application, 2009 (30)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.