Keil Logo

C51: Call C Functions within Startup Code


Information in this article applies to:

  • C51, all versions

QUESTION

I am using a 80C51RD with on-chip Flash and XRAM. In addition, I have an 32KByte external XDATA RAM. The external XDATA RAM is connected to an FPGA which requires programming before I can access it. This FPGA programming routine is part of the initialization code that I do at the beginning of the C main function.

However, I am now facing a problem:

The initializations of xdata variables in this external XDATA RAM is performed within the INIT.A51 file, and since it is called before main from the STARTUP.A51 code, my initialization fails.

Since I would prefer to stay in C with the FPGA initialization, I have a question:

Is it possible to call a C function within the STARTUP code before the INIT.A51 gets executed?

ANSWER

Yes, you can basically call a C function after you have set the stack pointer (done in STARTUP.A51 with the instruction MOV SP,#?STACK).

Change the STARTUP.A51 file as follows and add it to your project.

.
.
.
; function name of FPGA init function written in C.
EXTRN CODE (FPGA_init)

                RSEG    ?C_C51STARTUP

STARTUP1:
; insert at the STARTUP1 label the following code:
                MOV     SP,#?STACK-1        ; set stack pointer
                CALL    FPGA_init
; rest of the STARTUP file.
.
.
.

Note: You may remove the stack pointer initialization at the end of the STARTUP file since it is moved to the beginning of the startup code.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.