开发者

Two different programs in Flash

Is it possible to RUN 2 different C programs(ie 2 main()), stored in Flash(micro controller), one at a time?

I have a bootloader code which is a separate program and resides in separate protected section of ROM. Then I have my application program which resides in separate ROM section. Although, residing in memory is not an issue, but how will开发者_JAVA技巧 linker interpret this? How can I switch between 2 programs. Is this possible?

For example: Once I am done with bootloader, I can make it jump to Application function, but how will linker know this function?

Just to add, I am using Freescale HCS08 series and IDE is Codewarrior.

Further, here are the sequence of steps: I load a Bootloader code in ROM. Then this bootloader code is required to load my application code. And then my application code should take over.

Bootloader Code: Program Application Area ROM Start Application Program

Application Code: Check if to run Bootloader code or application itself.


Main is just a function. You may rename it and write another main which call either of them.

If you do not want rename main in the source you may mangle its name by define or compiler key:

cc -Dmain=main1 ...

(for first program), and

cc -Dmain=main2 ...

(for the second). Selector main:

int main(void) {
    if(x) return main1();
    else return main2();
}

Then link all together and download to your controller.

But there's problem with ISR's: you cannot assign two routines to single irq vector. If vectors are hardcoded to some flash location (like in most 8-bit controllers) you cannot switch ISR's. You will have to write ISR wrapper, recognizing which program is run and calling appropriate ISR.

UPD Second issue is that statically linked variables from first and second program will be in RAM simultaneously while only one set of them is used. This may exhaust RAM (small amount of which often exists in microcontroller) too early.

UPD2 Oh, now I really understand. If you want to link and download them separately, you should deal with linker maps. In this case same symbol names (such as many main's) s not an issue. In linker map you should define known entry point [set it to absolute address], from which either application code starts. Startup code (commonly it is assemble code) should be linked from this address. From selector you should decide and jump to defined location directly. (Do this only for bootloader if your app is also a selector).

Entry point provided by linker may be accessible by program as extern function:

int app2_start(void);

{
   .... /* condition check */
   app2_start(); /* this symbol defined in linker map, not in any source */
}

But this is not the address of it's main(), because C RTL have do make many initialisations (stack, initialised variables, heap, IO, etc.) before main() can start.

There's more common way that the bootloader decides, should it run itself or application, because if application code fails, boodloader may became inaccessible.


The way I've seen this done is to stick the entry point into a header for the application. Then have the boot loader pull that entry point out and jump to it with an appropriate inline assembly instruction. You may need a linker script to get the entry point itself from the application. Gnu ld uses ENTRY.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜