Example says Uboot from command to drive

Source: Internet
Author: User
Tags volatile

Example says Uboot from command to drive

This article transferred from: http://blog.csdn.net/zhouxinlin2009/article/details/45389987

We know that the ultimate goal of Uboot is to copy the OS kernel from flash into RAM and jump to the entry address of the OS sub-kernel, handing control of the processor to the operating system. A very important feature of U-boot is the use of commands to implement the underlying operations, we can achieve the above goal by executing instructions. Here to implement a simple led_blink hardware operation, parsing uboot from the command execution to the operation of the LED process. In accordance with this example, we can have a relatively clear understanding of the implementation of drivers, including NAND,USB, serial ports, and the porting of Uboot.

Under the common directory is a commonly used command file with the file name format cmd_xxx.c. As shown in figure:



Create our commands in this directory, as well as the command-specific action functions. The action function calls the driver in the drivers directory, which is the code that directly operates the hardware. The drivers directory contains drivers for a variety of specific devices, which are basically generic, and they introduce functions from outside to the platform or Development Board through macros. As shown in figure:



From the point of view of the microcontroller, the driver file under the drivers directory is specified to preprocess, compile, assemble and generate the static library file from the makefile file of the same directory as the driver file, and the linker obtains the required code from the library file and copies it to the generated executable file. So we need to write our own driver files and command files to specify the compilation rules in Makefile.

The following is a concrete implementation of the Led_blink process.

First from the functional point of view, we want to achieve from the upper command operation to the underlying physical operations, need to build two files, one is in the drivers/gpio/sml2440_led_blink.c file;
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.

CMD_LEDBLINK.C specific content 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 instructions, let the LEDs Blink"
"Learning instructions and driving processes"
);

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 instruction, let led blink"
	"learning instruction and drive Flow"
);

Analysis:

U_boot_cmd (); Is the format of the instruction (macro), the LED is the name of the instruction, 2 is the number of parameters of the instruction, 1 is the instruction can be executed repeatedly, do_led is the instruction executes the function pointer. So we're going to write a do_led function whose content is to invoke the S3c24x0_led_blink () of the underlying driver; The header file is analyzed by itself.

The contents of the Drivers/gpio/sml2440_led_blink.c file are as follows:
[CPP] View Plain copy print? 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; &nbsp ;     return 0; }  int s3c24x0_led_blink (void)   {      int N = ten; &nbsp ;     struct s3c24x0_led *led = s3c24x0_get_base_led ();      while (n--) {           Led->gpfdat = 0xFF;          delay (20000000);           Led->gpfdat = 0x00;          delay ( 20000000);         }      return 0;    & nbsp;  } 

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 = ten;
	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 command led will call the S3c24x0_led_blink () function, which we continue to write at the bottom. In order to implement this function, we need to manipulate the register of the LEDs to meet the requirements of the underlying operation.  Believe that the way led flashing can be understood, is to let the IO pin delay pull high, delay pull low results. struct s3c24x0_led *led = s3c24x0_get_base_led (); In http://blog.csdn.net/seek_0380/article/details/8764777 This it has been described, is to remove a header address to fix the memory space that conforms to the custom register requirements. We define a contiguous register space in S3c24x0.h, see the link post (here is just an implementation tip) for the implementation method. Then write the data to the register.


Then, in order to generate the executable, we also need to modified the makefile, specifying the rule to make the source file compile. Add to the makefile file under Drivers/gpio: [CPP] view plain copy print? cobjs-$ (config_sml2440_led) + = SML2440_LED_BLINK.O

cobjs-$ (config_sml2440_led)		+ = SML2440_LED_BLINK.O

It will then be connected to generate a static library

[CPP] view plain copy print? LIB: = $ (obj) libgpio.a

LIB 	: = $ (obj) libgpio.a


For the makefile file below common, add the following code.

[CPP] view plain copy print? Cobjs-y + = CMD_LEDBLINK.O

Cobjs-y + = CMD_LEDBLINK.O


This allows us to download the compiled uboot image file into Flash, and in the terminal input help, we will see the LED driver and its description. We type the LED, we will see the LED flashing 10 times, terminal back to the command acceptance mode. The implementation of this Led_blink uboot is complete.

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.