Arm Bottom Learning notes-Bare Board experimental program parsing-light LED

Source: Internet
Author: User
First clear goal: Light LED

How to light the LED.
View schematic, LED lights connected to the CPU pin, as long as the control of the pin output high and low level can control led on-off.
How to control the pin output high and low level.
Two steps to configure the appropriate registers:
1. Configure the PIN function (input/output/other functions);
2. Set the output value 0/1, which is high/low;
How to lay out code files and compile.
There are two ways to lay out code files:
1. Configure the CPU registers directly with the assembly code;
2. Assemble the +C code;
Why not just C code implementation.
C Language Program is the main function as the entrance, the main function is nothing special, but also to be called by others, and perform the return. In the absence of a startup file, the C program cannot be executed by itself. Therefore, can not only use C code to achieve the function of lighting.

Let's discuss the issues encountered in implementing the goal process using the assembly +C code implementation:
The C program has the main function returned by WHO. Return to where.
We usually develop the program on the host with the operating system, there are some libraries or boot files in the system, plus our application. Then the main function in our program is called by the startup file or library. The main function executes and then goes back to the system library function to do some cleanup work.
In bare-metal programs, similar libraries or boot files are not provided by the operating system and are written by the developer themselves. The library or startup file here can also be referred to as software-related initialization. This part is implemented in the assembly code. In addition to this, our assembly code should also include hardware initialization.
What things the software initializes to do.
1 Setting up Stacks
2 Set the return address of main;
3 Call main;
4 cleaning work.
Hardware initialization to do what things.
1. Turn off the watchdog;
2. Initialize the clock;
3.sdram initialization;

Below we analyze the hardware and software initialization in the assembly code mentioned above, why do these tasks:
In the process of writing large-scale software, in order to improve the readability and portability of code, we usually use C language for code writing. In C language, the invocation of functions is very common. function calls, parameters that need to be passed during a jump, and values to be returned when the function is returned need to be placed in memory, a process called data compression stack. So specify the stack before calling the main function, which is the set stack.
What is the set stack. That is, the stack pointer sp points to a block of memory.
Now that you want to point the stack pointer to a block of memory, it involves initializing the memory medium. The memory on the stack above is exactly the on-chip SRAM and does not need to be initialized separately. If the memory points to the SDRAM outside the slice, the SDRAM is initialized first to read and write. SDRAM initialization is a hardware initialization.
1. Why turn off the watchdog?
What is a watchdog.
For a preliminary understanding of the watchdog, in a short article, the "understanding Watchdog" http://blog.csdn.net/eric41050808/article/details/17336507 is introduced.
Simply put, a watchdog is a switch that is set to prevent the system from suspending or running for long periods of time. The essence is something that can be restarted periodically. If the system is cleared before the watchdog timer is exhausted, the system does not restart, and we also think that the system does not have a long time to hang or run the problem. Conversely, the watchdog will restart the system.
Why did you shut it down.
In our program, because not very complex system, there is no independent part of the timer to clear the 0 watchdog, so simply turn off the watchdog, or the system will continue to restart, affecting our experiment.
Can not be closed.
It's also possible not to turn off the watchdog, but as described above, we must clear the timer before it runs out. But in our part of the LED lit code that was finished in assembly language, there was really no watchdog shutdown, and nothing unusual happened. Because the light led operation is simple, the assembly only to two registers operation, the program running time is very short, even if we do not turn off the watchdog, the system restarts, for us to light the LED has no effect, because the CPU restart and code run quickly, the human eye can not tell the LED flashing, said to have no impact on the experiment. The experiment code for convenience did not do the work of shutting down the watchdog.
2. Initialize the clock: Why the clock is initialized.
When the system was just started, the system clock was provided by the external crystal oscillator, the crystal frequency is very low, some development boards only a few megabytes and a few 10 trillion. This frequency is too low for the CPU. Currently on the market CPU operating frequency generally around 1.5G, external crystal oscillator frequency can not be directly used, need to initialize the set to achieve high frequency. In addition to the CPU frequency, the system will generally have SDRAM frequency (HCLK) and serial timer (PCLK), etc., which need to be set when the clock is initialized. This is the work of initializing the clock part. The specific system clock work is described separately.
3. Initialize SDRAM (why SDRAM is initialized). SDRAM is an external extension, large storage space, slow, if the program is placed in the SDRAM) separately described.
These are the hardware initialization instructions.

The initial combination of hardware and software is the startup file. This experiment does not care about the speed problem, therefore does not do the clock initialization, moreover the code launches directly in the on-chip SRAM, therefore does not apply the SDRAM, so does not need to initialize. So in the above hardware initialization, just do the work of shutting down the watchdog.
In the experiment of lighting LEDs, hardware initialization simply does the work of shutting down the watchdog. The reason is that it's OK to light the LED clock slowly, without initializing the clock; The experimental platform uses an on-chip SRAM without initializing the SDRAM.

Why stack is set. From the disassembly from the view, main one call can see {FP, IP, LR, PC} Several registers are saved to the SP, the call return is {FP,SP,PC} is restored again, so it is good to set the stack, otherwise error.

The following is the mini2440 on the Development Board of the example code, the source from the Vedon Teacher's learning materials, the first paragraph is only in assembly language to achieve a lit led, the second paragraph is the corresponding makefile. The code itself has no reference, mainly looking at annotations, in order to learn.

Assembly Code:

[Plain]  View Plain Copy @******************************************************************************   @ file:led_on. s   @  function: LED lighting program, lit led1   @************************************************************                           .text   .global _start   _start:                      The LDR     R0,=0X56000010      @ R0 is set to the Gpbcon register. This register                                              @  function for selecting each pin of port B:                                            @  is the output, input, or other                MOV      R1, #0x00000400                         STR     R1,[R0]              @  set GPB5 to output,  bit [10:9]=0b01                LDR    The   R0,=0X56000014      @ R0 is set to the Gpbdat register. This register                                             @  for Reading/ Write port B data for each pin                MOV      R1, #0x00000000       @  Change this value to 0x00000020,                                              @  allows LED1 to go off                 STR     R1,[R0]              @ GPB5 output 0,led1 lit    main_loop:                B       MAIN_LOOP  

Makefile File:[Plain]  View plain copy led_on.bin : led_on. s       arm-linux-gcc -g -c -o led_on.o led_on. s       arm-linux-ld -ttext 0x0000000 -g led_on.o -o  led_on_elf       arm-linux-objcopy -o binary -s led_on_ elf led_on.bin   clean:       rm -f   led_ on.bin led_on_elf *.o   #第一条命令中-G To add some debugging information;-C compilation is not connected;-O indicates the output file name    compile #预处理. c2.s- > assembly. S2.o-> links (multiple. O compositing 1 executables)    #第二条链接命令,  -ttext 0x0000000 represents the starting address of the code snippet is 0;   #第三条转成二进制文件 ,-o binary represents a binary copy, note that this is the uppercase o   #为什么把代码段地址指定为0. The reasons are as follows:   #2440有两种启动方式, NAND start-up, or nor start    #2440内部有4ksram, external NAND and sdram   #以nand   When Flash starts, 1. The NAND 4k is copied into the SRAM, 2.CPU from the 0 address to execute, these two steps are hardware automatic, no matter what this 4k put something, put things, will be so.    #如果外接nor  flash,nandWill not be in the image of the memory directly executed, must be copied to the on-chip SRAM to start execution, 0x00000000 address in the SRAM start position    #一旦选择从nor启动, and nor this hardware, can be in the memory of the image read the data, but not like the memory to write data, So you can start directly from nor, that is, the 0 address of the address space at the start of the nor    #CPU   

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.