Bare metal program for lighting LED lights with tq2440 buttons

Source: Internet
Author: User

I. When it comes to the bare metal program of arm, many people will immediately think of a development tool named ads, but we can also do the bare metal program of arm in Linux, the specific implementation process is as follows:

Step 1: edit the code.

Step 2: Compile the Code. The Code is divided into three parts: 1. Link Script 2. Use the command line to determine the file sequence during the link 3. Use the command line to compile the code. The three parts can all be written as a MAKEFILE file, and the make command can be executed during compilation.

Step 3: Burn the compiled ". bin" file into the Development Board and power it on again to observe the effect.


Second, tq2440 button light LED light bare metal program analysis:

1. Startup file:

Arm cannot directly execute C language programs. It must use a crt0.s assembler to complete initialization before executing C language programs, this file named crt0.s is called the -- Startup File.

The purpose of the Startup File is to perform some initialization work and prepare for the final execution of the C language program. The initialization work mentioned above includes two aspects: 1. software initialization. The specific process is to set the stack --> set the return address --> call the main function --> to clean up the call. 2. hardware initialization. The specific process is to disable the watchdog --> initialize the clock --> initialize the SDRAM.

Tq2440 press the button to light up the Startup file of the LED light bare metal program:

@*************************************** ***************************************@ File: crt0.s @ function: use it to transfer to the C program @********************************** **************************************** ****. text. global _ start_start: LDR r0, = 0x53000000 @ watchdog Register address mov R1, #0x0 STR R1, [R0] @ write 0, prohibit watchdog, otherwise, the CPU will continuously restart the LDR sp, = 1024*4 @ to set the stack. Note: The value cannot exceed 4 kb, because the available memory is only 4 k @ the code in NAND Flash. After the reset, it will be moved to the internal RAM. This Ram only has 4 k bl main @ to call the main function halt_loop: B halt_loop in the C program.

2. makefile:

key_led.bin : crt0.S  key_led.c arm-linux-gcc -g -c -o crt0.o crt0.Sarm-linux-gcc -g -c -o key_led.o key_led.carm-linux-ld -Ttext 0x0000000 -g  crt0.o key_led.o -o key_led_elfarm-linux-objcopy -O binary -S key_led_elf key_led.binarm-linux-objdump -D -m arm  key_led_elf > key_led.disclean:rm -f   key_led.dis key_led.bin key_led_elf *.o

Makefile rules:

Target file: dependent on file 1 dependent on file 2 .....

Tab key command

Conditions for executing the makefile command: the target file does not exist; the dependent file of the target file has been updated; The makefile command can be executed if either of the preceding conditions is met.

3. tq2440 press the button to light up the source code of the LED light bare metal program:

// Led control registers and data registers # define gpbcon (* (volatile unsigned long *) 0x56000010) # define gpbdat (* (volatile unsigned long *) 0x56000014) // key control register and data register # define gpfcon (* (volatile unsigned long *) 0x56000050) # define gpfdat (* (volatile unsigned long *) 0x56000054) /** led1, led2, led3, and led4 correspond to gpb5, gpb6, gpb7, and gpb8 */# definegpb5_out (1 <(5*2 )) # definegpb6_out (1 <(6*2) # define gpb7_out (1 <(7*2) # define gpb8_out (1 <(8*2)) # Definegpb5_msk (3 <(5*2) # definegpb6_msk (3 <(6*2) # definegpb7_msk (3 <(7*2 )) # define gpb8_msk (3 <(8*2)/** K1, K2, K3, k4 corresponds to gpf1, gpf4, gpf2, gpf0 */# define gpf1_in (0 <(1*2) # define gpf4_in (0 <(4*2 )) # define gpf2_in (0 <(2*2) # define gpf0_in (0 <(0*2) # define gpf1_msk (3 <(1*2 )) # define gpf4_msk (3 <(4*2) # define gpf2_msk (3 <(2*2) # define gpf0_msk (3 <(0*2 )) int main () {unsigned long dwdat; // led1, LED 4 pins corresponding to 2, led3, and led4 are set to output gpbcon & = ~ (Optional | gpb6_msk | gpb7_msk | gpb8_msk); // set the corresponding bits of the four registers to 00 gpbcon | = gpb5_out | gpb6_out | gpb7_out | gpb8_out; // K1, K2, the four pins corresponding to K3 and K4 are set as input gpfcon & = ~ (Gpf1_msk | gpf4_msk | gpf2_msk | gpf0_msk); // set the four registers to 00 gpfcon | = gpfcon in | gpf4_in | gpf2_in | gpf0_in; while (1) {// If kN is 0 (indicates pressing), ledn is 0 (indicating lighting) dwdat = gpfdat; // read The GPF pin level status if (dwdat & (1 <1) // K1 does not press gpbdat |=( 1 <5 ); // led1 extinguish else gpbdat & = ~ (1 <5); // led1 light up if (dwdat & (1 <4) // K2 does not press gpbdat |=( 1 <6 ); // led2 extinguish else gpbdat & = ~ (1 <6); // led2 light up if (dwdat & (1 <2) // K3 is not pressed gpbdat | = (1 <7 ); // led3 extinguish else gpbdat & = ~ (1 <7); // led3 light up if (dwdat & (1 <0) // K4 does not press gpbdat |=( 1 <8 ); // led4 extinguish else gpbdat & = ~ (1 <8); // led4 light up} return 0 ;}

I will not talk about the specific principles and hardware analysis here. I have previously written an analysis called "tq2440 key-light LED driver, the difference is that in Linux, LEDs are terminated by keys, while bare metal programs are not interrupted by keys, we only treat the pins connected by the buttons as a general gpio port, and set the level of the LED pins by judging whether the buttons are pressed at Different Pin levels, in this way, the LED lights can be switched on and off.

This article from the "can't stop thinking" blog, please be sure to keep this source http://9110091.blog.51cto.com/9100091/1571445

Bare metal program for lighting LED lights with tq2440 buttons

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.