About # define enterpwdn (clkcon)

Source: Internet
Author: User

We often see that in Embedded design, the entry address of a program segment is converted into a function. Let's analyze its components:
For example, there is a statement in the dual-cell on the 0x00000020 address of Bootloader:
@ Address is 0x00000020
B powerdown @ jump to the flag "powerdown"
...
...
Powerdown:
...
...
Then we can see the macro description in a C header file:
# Define enterpwdn (clkcon) (void (*) (INT) 0x20) (clkcon)
The first day is a mess. Now, let's sort it out.
It is not hard to see that when we program call the enterpwdn (clkcon) function, the compiler first converts enterpwdn (clkcon)
(Void (*) (INT) 0x20) (clkcon) statement.
This statement is divided into three parts.
1 :( void (*) (INT)
2: 0x20
3 :( clkcon)
Clkcon is a function parameter. What about 0x20? Of course, it is the entry address of the function to be converted. What about (void (*) (INT ?? Haha ......, This part, as a whole, describes the type of the converted function, that is, no return value, with an integer parameter. The "(*)" in the middle means to convert it into a function (or convert 0x20 to an address, because in arm assembly, the C language function name is treated as an address number ). Just like the forced type conversion we usually use, (INT) temp, but here is to convert a number to another type, and that is to convert an address to a function.

About arm:# Define enterpwdn (clkcon) (void (*) (INT) 0x00000020) (clkcon)

1. First, you must know the macro definition with parameters. The definition method is as follows:

# Define macro name (parameter) String

As follows: # define add (A, B) (A) + (B)

So: in the program, B = add (2*3), (4*5) is equivalent to B = (2*3) + (4*5 ).

For more information, see C language books.

2. See the following:Enterpwdn (0x7fff4 );

Will be replaced:(Void (*) (INT) 0x00000020) (0x7fff4 );

This involves function pointers. The usage of function pointers is as follows:

Void fun (int x);/* declare a function */

Void (* pfun) (int x);/* declare a function pointer */

Pfun = fun;/* assign the first address of the func function to the pointer f */

You can call the function fun to write: A. Fun (5); B. (* pfun) (5 );

Then:(Void (*) (INT) 0x00000020) (0x7fff4 );Moderate blue0x7fff4Is the passed parameter, red(Void (x) (INT) 0x00000020Is the function name. In fact, the function name itself is an address. In the above formula, if you know that the function fun has an address of 0x012345, you can write it like this theoretically: 0x012345 (5). Of course, you cannot write it like this, although fun is at 0x012345, the compiler does not know the parameters and return values of this function. So we need to force the conversion.

Next, let's take a look at how to convert an address into a function name:

If you convert a in char a; to an int-type variable, perform the following: B = (INT);

SimilarlyZero X 00000020To convert an address to a function name, you must also forcibly convert the address inZero X 00000020Add the type to be converted before.(Void (*) (INT ))Is the function type. It corresponds to void (* pfun) (int x); // declares a function pointer.

3. Let's take a look.Enterpwdn (0x7fff4 );The specific parameter passing after the call involves the mutual call between C and assembly.

View

_ Entry
Resetentry
 
B resethandler

B handlerundef; handler for undefined Mode
B handlerswi; handler for SWI interrupt
B handlerpabort; handler for pabort
B handlerdabort; handler for dabort
B.; reserved
B handlerirq; handler for IRQ interrupt
B handlerfiq; handler for FIQ interrupt

; @ 0x20
B enterpwdn; must be @ 0x20.

JumpZero X 00000020The address will jump to the entry enterpwdn.

Enterpwdn
MoV R2, R0; r2 = rclkcon
Tst r0, #0x8; sleep mode?
BNE enter_sleep

The first sentence is mov R2 and R0. In fact, R0 stores parameters.0x7fff4This involves C call Assembly. I will not be ugly if I look for a reference.

What does # define enterpwdn (clkcon) (void (*) (INT) 0x20) (clkcon) in arm mean?

1. first, this is a macro definition with parameters. Therefore, if the statement enterpwdn (0x7fff4) is expanded, it is the following statement (void (*) (INT) 0x20) (0xffff4 ))

2. (void (*) (INT) 0x20

(Void (*) (INT) as a forced type conversion, convert 0x20 to a function pointer type, the prototype of this function is void fun (INT ); the 0xffff4 is the parameter of the function, so (void (*) (INT) 0x20) (0xffff4 )) the whole sentence means to call the function at 0x20. The parameter is 0xffff4. in fact, the following is actually jump to the address 0x20 to execute the code, and the parameter to r0, atpcs, 1-4 parameters corresponding to the storage register for the r0-r3.

3. Now let's look at the definition of this part in 2440init. S.

B resethandler
B handlerundef; handler for undefined Mode
B handlerswi; handler for SWI interrupt
B handlerpabort; handler for pabort
B handlerdabort; handler for dabort
B.; reserved
B handlerirq; handler for IRQ interrupt
B handlerfiq; handler for FIQ interrupt

; @ 0x20
B enterpwdn; must be @ 0x20.

Enterpwdn
MoV R2, R0; r2 = rclkcon
Tst r0, #0x8; sleep mode?
BNE enter_sleep

B enterpwdn; must be @ 0x20. in this statement, the corresponding command address is 0x20, which is 0x20 in the preceding (void (*) (INT) 0x20), and then the statement jumps to enterpwdn for execution, however, the parameter value is passed to r0, And you can view the hardware description later.

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.