C51 函数/程序段的定位

时间:2022-03-24 16:11:14

  在Keil C中可能需要指定某个函数或者某段程序链接后存放在程序区中的位置。

  1. 如何指定某个函数在程序区中的位置

QUESTION

How do I locate a C function at an absolute address? I succeeded in variables using _at_, but it doesn’t work for functions.

ANSWER

You must use the BL51 Linker or LX51 Linker to locate functions at fixed addresses. To do so, make a note of the function name and the source file in which it is saved. The compiler creates a symbol name for the function using the following format:

?PR?function_name?file_name

So, the function named func in main.c would have a symbol name of:

?PR?FUNC?MAIN

Everything is uppercase to comply with the OMF51 standard.

There may be derivatives of this naming convention. For example, if the function accepts arguments that can all be passed in registers, the function name is prefixed with an underscore (‘_’) and the above would be:

?PR?_FUNC?MAIN

You may look in the linker’s map file to figure out the symbolic names for your functions.

Once you know the symbolic name, it is a simple matter of telling the linker where to locate it using the CODE directive. For example:

BL51 main.obj code(?pr?func?main(2000h))

Tells the linker to locate the function func at code address 0×2000.

If you use the µVision IDE you may enter the starting address for segments under Project – Options for Target. For the BL51 Linker/Locater enter the following code segment with the address under BL51 Locate – Code. For the LX51 Linker/Locater enter it under LX51 Locate – User Segments:

?pr?func?main(C:0x2000)

  2,如何指定一段代码在程序区中的位置

QUESTION

I am writing an IAP loader and would like to locate it in the first 1024 Bytes of my CODE memory. How can I ensure this? I have tried to use wildcards, but this did not work out, since it collidates with interrupt vectors.

ANSWER

The best approch is to locate the program code with a USER CLASS. User classes are an extension of the LX51 Linker/Locater (which is available in the PK51 Professional Developer’s Kit).

For example, you can write the following pragma within the C source files that contain the IAP loader program code:

#pragma userclass (code = IAP) // generates CODE_IAP class

This userclass directive places the program code of the module to the memory class CODE_IAP. Now you can separately locate this memory class when you enter it in µVision under Project – Options for Target – LX51 Locate – User Class. To locate the IAP code to the first 1KB of your program section enter here.

CODE_IAP (C:0 - C:0x3FF)

Note that you have to select the Use Extended Linker LX51 under Project – Options for Target – LX51 Locate – User Class.

MORE INFORMATION

Refer to USERCLASS in the Cx51 User’s Guide.

Refer to CLASSES in the LX51 User’s Guide.

  

  补充:

1.  如果没有在“Options for Target”中找到“ LX51 Locate”标签,请先在“Device”标签页中选中“Use Extended Linker(LX51) instead of "BL51"

2. 指定的USERCLASS的地址不能和其他CODE 区的地址重叠,包括原始的CODE区。