Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[51] Using 2 main functions in one Keil project

Status
Not open for further replies.

dean_winchester

Advanced Member level 4
Joined
Oct 19, 2010
Messages
108
Helped
22
Reputation
44
Reaction score
21
Trophy points
1,298
Location
India
Activity points
1,813
Hello All,

I am making 2 applications [ separate apps having separate main functions ] on 8051 controller . So, i need to jump from APP1 to APP2 depending upon the execution.
I am using the KEIL V7.10 Compiler. I am having trouble jumping from one app to other. It jumps from one app, but do not know where does it jump to.. :(
I am using 2 banking mode also on controller to access 128KB memory on chip flash.

Please help me out here,,,, i am stuck at one state for a longggg longgg time!!

:)
 

In a program have only one main function..

If you wrote 2 main function in program, Compiler shows error.
 

as the previous post states you can only have one main() function in a program as main is the enty or starting point of code execution.
Why not have two functions main1() and main2() which are called as required, e.g.
Code:
int main(void)
{
    while(1)      // loop forever
       if(...)   main1();
       else     main2();
}

void main1()
{
   ..
}

void main2()
{
...
}
 
by two main functions i mean..
In my Keil Project Settings. i have TWO Targets [separate apps, so separate main functions] . the output is separate .hex files.

I will burn both the hex files to my 8051..

0x0000 ~ 0x0FFF == APP1.hex
0x1000 ~ 0x1FFFF == APP2.hex
 

does the linker map give you the entery point of the two programs - you can then jump to the appropriate address? what do you do about the stack pointer etc?
 

easy way merge the two project files as a single project and do like #3.

otherwise change the starting address of the second application, ensure it is not overlapping with the first code then write some boot from assembly code to select which part to execute first.

Or you can use the function pointer to do the job instead of assembly...

Also It it is not at all going to possible if you got any ISR s to work with..
 

easy way merge the two project files as a single project and do like #3.

otherwise change the starting address of the second application, ensure it is not overlapping with the first code then write some boot from assembly code to select which part to execute first.

Or you can use the function pointer to do the job instead of assembly...

Also It it is not at all going to possible if you got any ISR s to work with..

YES. i am not using any ISR in APP1, but APP2 uses ISR. i HAVE REDIRECTED the ISR to start address of the APP2. Also, i am linking it using a asm file. Can you please let me know how can i use function pointer to implement the same. Any doc will be helpful.

tHanks in advance :)

- - - Updated - - -

does the linker map give you the entery point of the two programs - you can then jump to the appropriate address? what do you do about the stack pointer etc?

Stack Pointer and all memory init are done differently in both apps. I jump to the location specified by me in the Keil settings, but when i check the map file, the main is not at that addres... Do i need to give the address of main function of APP2 in APP1. ? CONFUSED~~~ :bang:
 

I will burn both the hex files to my 8051..

0x0000 ~ 0x0FFF == APP1.hex
0x1000 ~ 0x1FFFF == APP2.hex

Without special requisites, "jumping" between two separately compiled applications involves a cold start either of APP1 or APP2, overwriting all data in the previously executing application. This happens e.g. if you have a bootloader and application image stored in a microcontroller. Is this what you want?

Otherwise you need to think about specific allocation of data memory, disabling the standard memory initialization in startup code and many details more. You may also find it less than optimal that the run-time library (RTL) code is doubled.

Or do you want some kind of multitasking where both applications are activated alternatingly?

- - - Updated - - -

Referring to your latest post that appeared while I was answering: You still don't say what you exactly want to achieve. The discussion would be much easier if you explain the purpose of your design.
 

@FvM: YES, i want to achieve the boot code and APP code in the controller. If BOOT is OK then i need to jump from BOOT CODE to the APPlication code to run interfaced devices and all.
In boot code i am not at all using ISR, it just checks for a particular condition.. and Jumps to aPP.
 

Make sure no part of the code in both programs are not overlapped !!!
from APP1

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
main()
{
void (*FuncPtr)(void);
FuncPtr = (void *)Starting address of main of APP2;
. . . 
if( you want switch to APP2 ?)
{
(*FuncPtr)();
}
. . . 
}



I never tried this before, If you try this your code will work fine for some time for sure.
Stack overflow can occur any time, If you switch often and also I am not sure about the init values of functions ..
 

Make sure no part of the code in both programs are not overlapped !!!
from APP1

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
main()
{
void (*FuncPtr)(void);
FuncPtr = (void *)Starting address of main of APP2;
. . . 
if( you want switch to APP2 ?)
{
(*FuncPtr)();
}
. . . 
}



I never tried this before, If you try this your code will work fine for some time for sure.
Stack overflow can occur any time, If you switch often and also I am not sure about the init values of functions ..


umm.. Will surely give this one a try.. :)
 

YES, i want to achieve the boot code and APP code in the controller. If BOOT is OK then i need to jump from BOOT CODE to the APPlication code to run interfaced devices and all.
In boot code i am not at all using ISR, it just checks for a particular condition.. and Jumps to aPP.
In this case, you'll simply jump to the reset vector (begin of startup code) of APP2 which is probably not identical to main(). It's necessary to perform the regular initialization of APP2, otherwise it won't run properly.

The startup code usually doesn't depend too much on an exact reset state of the processor, except for interrupts being disabled at that time. In case of doubt, inspect the startup source code. It's shipped with Keil as far as I remember.
 

@FvM: YES, i want to achieve the boot code and APP code in the controller. If BOOT is OK then i need to jump from BOOT CODE to the APPlication code to run interfaced devices and all.
In boot code i am not at all using ISR, it just checks for a particular condition.. and Jumps to aPP.

Do you using any ISR in application ?

If you have startup code then put the starting location of the startup code in the function pointer,

Also the switching will be like the controller will completely forget the last executing application.

but the init values of peripherals will remain as in the last application so ensure you make them default on your own.

for example if you enabled UART in the last app then your uart will continue reception in app2 still you are not using UART on APP2..
 

The ISR question has been already answered in post #7 and #9, I think.
 

I didnt noticed, If one APP using ISR then its not a problem, because its a easy cause for a program overlap.

Choose one APP as your default one and boot from that and ensure that the APP2 cant boot from the chip reset.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top