For example, uboot from command to driver

Source: Internet
Author: User

We know that the ultimate goal of uboot is to copy the OS kernel from flash to ram, jump to the entry address of the kernel of the operating system, and give control of the processor to the operating system. An important feature of U-boot is to use commands to perform underlying operations. By executing commands, we can achieve the above objectives. Here we will implement a simple led_blink hardware operation to parse the uboot process from command execution to led operation. According to this example, we can have a clear understanding of the implementation process of the drivers including nand, USB, serial port, and uboot porting.

The common command file is under the common directory. The file name format is pai_xxx.c.

Create our commands and the operation functions specified by the commands in this directory. The operation function calls the driver under the DRIVERS directory.ProgramThat is, directly operating the hardwareCode. The DRIVERS directory contains drivers of various specific devices, which are basically common. They use macros to introduce functions related to the platform or development board.

From the microcontroller perspective, the driver files under the DRIVERS directory are used by the makefile files in the same directory of the driver files to specify preprocessing, compilation, and assembly to generate static library files. The linker obtains the required code from the library files, copy to the generated executable file. Therefore, we need to specify the compilation rules in makefile for the driver files and command files we write.

The following describes the implementation process of led_blink.

First, from a functional perspective, we need to create two files to implement commands from the upper layer to the underlying physical operations. One is in the drivers/gpio/sml2440_led_blink.c file, and the other is in
File. In this way, we can write the driver in the sml2440_led_blink.c file according to the uboot rules, and write the command in the cmd_ledblink.c file.

The specific content of pai_ledblink.c is as follows:

 

 
// Common/cmd_ledblink.c # include <Common. h> # include <command. h> int do_led (void) {s3c24x0_led_blink (); Return 0;} u_boot_cmd (LED, 2, 1, do_led, "ledblink, just for test", "test command, make the LED flash "" learning instruction and driving process ");

Analysis:

 

U_boot_cmd (); is the format (macro) for creating commands, LED is the command name, 2 is the number of command parameters, 1 is the command can be executed repeatedly, do_led is the function pointer for command execution. So we wrote another do_led function, whose content is to call the underlying driver's s3c24x0_led_blink (); function. The header file is analyzed by yourself.

The content of drivers/gpio/sml2440_led_blink.c is as follows:

// Drivers/gpio/sml2440_led_blink.c # include <Common. h ># include <ASM/ARCH/s3c24x0_cpu.h> static void delay (unsigned long loops) {__ ASM _ volatile ("1: \ n" "Subs % 0, % 1, #1 \ n "" BNE 1B ":" = r "(loops):" 0 "(loops);} int s3c24x0_led_init (void) {struct s3c24x0_led * led = s3c24x0_get_base_led (); led-> gpfcon = 0x55; led-> gpfup = 0xff; led-> gpfdat = 0; return 0 ;} int s3c24x0_led_blink (void) {int n = 10; struct s3c24x0_led * led = s3c24x0_get_base_led (); While (n --) {led-> gpfdat = 0xff; delay (20000000 ); LED-> gpfdat = 0x00; delay (20000000);} return 0 ;}

Analysis: First, we know that the upper-layer Command led will call the s3c24x0_led_blink () function and we will continue writing it at the bottom layer. To implement this function and meet the requirements for underlying operations, we need to operate and control the LED registers. Reliable led flashes can be understood, that is, the result of high latency and low latency of the IO pin. Struct s3c24x0_led * led = s3c24x0_get_base_led. We define a continuous register space in s3c24x0. H. For details about the implementation method, refer to the link blog (this is just an implementation technique ). Then write data to the Register.

 

Then, in order to generate an executable file, we also need to modified the makefile and specify the rule to compile the source file. Add:

 
Cobjs-$ (config_sml2440_led) + = sml2440_led_blink.o

Then the static database will be generated by the connection.

 

 

 
Lib: = $ (OBJ) libgpio.

For makefile files under common, add the following code.

 

 

 
Cobjs-y + = cmd_ledblink.o

In this way, we will download the compiled uboot image file to flash and input help in terminal to see the LED driver and its description. When we press the LED, we can see that the LED blinks 10 times and terminal returns to the command acceptance mode. This completes the implementation under the uboot of led_blink.

 

 

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.