CW 5.7.0 - MC9S08GB60. Is the C code relocatable?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

CW 5.7.0 - MC9S08GB60. Is the C code relocatable?

Jump to solution
4,181 Views
ajn
Contributor I
I using MC9S08GB60 and Code Warrior version 5.7.0.
 
I wonder if the compiler/linker generate relocatable code, when I compile/link C code. If not how do I tell the compiler/linker that I like to have a function  relocatable? I need copy an execute this function from RAM.
 
Or is it only assembler that is possible to make relocatable?  
 
Is there any example how to create I2C module that is not depending on interrupt but only polling?
 
 
--
Message Edited by ajn on 2007-03-2309:09 AM
--
Alban Edit: moved thread from 8-bit (it is CW specific) + add CW version in title

Message Edited by Alban on 2007-03-23 12:46 PM

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
990 Views
CompilerGuru
NXP Employee
NXP Employee
First, when you just copy two functions from flash to ram without further precautions, that might or might not work, it depends on the code.
Here's a short summary how you can write the code in C so it will work or not compile.

- First, don't let the compiler generate any runtime routine calls. Those don't get copied to RAM, and unless you can keep them accessible, calling them will crash.
Doing this will actually limit the part of the C language you can use, but the good news is that you do not have to guess, the compiler will not compile it, if it contains any runtime function references. For example if you start to divide longs the compiler tries to generate a call to a long division runtime, but will fail because of this pragma. If you use simple C code, you should be fine though. If someone later changes your code to use long divisions, it will fail at compile time, at least.

// before your code to be relocated
#pragma MESSAGE ERROR C3605 /* C3605: Runtime object '_SEXT16_32' is used at PC 0xa*/

- Next put all the functions which should end up in RAM into their own section.
#pragma CODE_SEG WHATEVER
void fun(void) {
  // no long divisions here :smileywink:
}
.......

#pragma CODE_SEG DEFAULT

// disarm
#pragma MESSAGE DISABLE C3605 /* C3605: Runtime object '_SEXT16_32' is used at PC 0xa*/

- next in the prm, allocate one are in RAM and one in flash for your code
(or reuse some RAM if you know what you are doing Smiley Happy

In the prm file:
ROM_IMAGE = READ_ONLY 0xF000 TO 0xF2FF RELOCATE_TO 0x0200;
...
PLACEMENT
...

WHATEVER INTO ROM_IMAGE;


What this will do is not to avoid to generate absolute references (JMP ext, JSR ext), but instead they will just jump to the right RAM address instead.

Note that I'm convinced that it does work this way, but I did not try it out. Therefore there might be many tipos in the snippets above.


A few links (asked google for RELOCATE_TO HC12)


Really look into this:
http://forums.freescale.com/attachments/freescale/CFCOMM/1592/1/tn228.pdf

The linker manual, you also have this one locally. Search for RELOCATE_TO.

http://www.freescale.com/files/soft_dev_tools/doc/ref_manual/CW_Build_Tools_Utilities_RM.pdf


Hope it helps (to cite CrazyCat)

Daniel

View solution in original post

0 Kudos
Reply
7 Replies
990 Views
ajn
Contributor I
Thanks CompilerGuru,
 
that was the answer I was looking for...
 
Ake
0 Kudos
Reply
990 Views
ajn
Contributor I
Hello!
Thanks for your fast reply.
 
I know I am able to create a relocatable assemble project, but as I stated before I would like to write the code in C. So if I move a function written in C to a RAM area and execute it from there, could I be sure that there are no jmp or other instruction in that function that jumps back to a label/address that is located in FLASH where the function was located from the begining?
 
I have already create two C-functions that seems to work when they are copied to RAM (I am able to write data in a FLASH page and that is not possible if the code was located and executed from FLASH) but what happens if I create a program with several function will it still work? Or is there a pragma or something else I need to use when i write code in C to prevent jumps to fix address locations?
 
Example:
I like to have instruction like:
jump to new adress where new adress is equal to:  current addres - 4
 
and not like: 
jmp 0xF000. (fix address located in FLASH for example) 
 
Ake 
 
 
 

Message Edited by ajn on 2007-03-2307:04 PM

Message Edited by ajn on 2007-03-2307:05 PM

0 Kudos
Reply
991 Views
CompilerGuru
NXP Employee
NXP Employee
First, when you just copy two functions from flash to ram without further precautions, that might or might not work, it depends on the code.
Here's a short summary how you can write the code in C so it will work or not compile.

- First, don't let the compiler generate any runtime routine calls. Those don't get copied to RAM, and unless you can keep them accessible, calling them will crash.
Doing this will actually limit the part of the C language you can use, but the good news is that you do not have to guess, the compiler will not compile it, if it contains any runtime function references. For example if you start to divide longs the compiler tries to generate a call to a long division runtime, but will fail because of this pragma. If you use simple C code, you should be fine though. If someone later changes your code to use long divisions, it will fail at compile time, at least.

// before your code to be relocated
#pragma MESSAGE ERROR C3605 /* C3605: Runtime object '_SEXT16_32' is used at PC 0xa*/

- Next put all the functions which should end up in RAM into their own section.
#pragma CODE_SEG WHATEVER
void fun(void) {
  // no long divisions here :smileywink:
}
.......

#pragma CODE_SEG DEFAULT

// disarm
#pragma MESSAGE DISABLE C3605 /* C3605: Runtime object '_SEXT16_32' is used at PC 0xa*/

- next in the prm, allocate one are in RAM and one in flash for your code
(or reuse some RAM if you know what you are doing Smiley Happy

In the prm file:
ROM_IMAGE = READ_ONLY 0xF000 TO 0xF2FF RELOCATE_TO 0x0200;
...
PLACEMENT
...

WHATEVER INTO ROM_IMAGE;


What this will do is not to avoid to generate absolute references (JMP ext, JSR ext), but instead they will just jump to the right RAM address instead.

Note that I'm convinced that it does work this way, but I did not try it out. Therefore there might be many tipos in the snippets above.


A few links (asked google for RELOCATE_TO HC12)


Really look into this:
http://forums.freescale.com/attachments/freescale/CFCOMM/1592/1/tn228.pdf

The linker manual, you also have this one locally. Search for RELOCATE_TO.

http://www.freescale.com/files/soft_dev_tools/doc/ref_manual/CW_Build_Tools_Utilities_RM.pdf


Hope it helps (to cite CrazyCat)

Daniel
0 Kudos
Reply
990 Views
Alban
Senior Contributor II
C Code is not location dependant by itself, it is you making it location dependant by mentioning addresses.
Change your principle of thinking not to include addresses.
Your code can call other functions, but I don't see any reason for C code to say to go at current location-3.
Either it's in a table and you use a pointer/index, or it is in the software execution and you write too low level for C code.
 
C code should not be about writing ASM in C, but is a different level of abstraction which does not require the programmer to know all the details of the hardware implementation. This is the job of the compiler to translate it in machine understandable language.
 
Alban.
0 Kudos
Reply
990 Views
CompilerGuru
NXP Employee
NXP Employee


C code should not be about writing ASM in C, but is a different level of abstraction which does not require the programmer to know all the details of the hardware implementation. This is the job of the compiler to translate it in machine understandable language.

 

Alban.





That's all true, but once we start doing non standard things like making some parts of the application non accessible during flash programming, then those compiler internals start to matter. So in almost all of your C code, you should not care. In the rest, you have to, of course using assembly for that part is also a way to go.

Daniel
0 Kudos
Reply
990 Views
CompilerGuru
NXP Employee
NXP Employee
I'm not sure I did understand the question because of the used terminology.
I'm using this terminology

- absolute code: code address fixed at compile/assemble time.
- relocatable code: code address fixed at link time
- position independent code: code address fixed at run time

So the question is, do you need position independent code, or is relocatable code working too.
Relocatable code can be configured in the linker to be places into flash, but so that addresses do resolve to ram addresses (search RELOCATE_TO in the linker manual and in this forum). Basically relocatable code will run just at one address (and this address maybe different from where the code is places, if necessary).
Position independent code can be moved around at runtime freely, multiple copies may even exist and work at once.
The HC08 compiler does support to generate relocatable code only, if you do really need PIC, then you have to use assembly.

Daniel
0 Kudos
Reply
990 Views
Alban
Senior Contributor II
Hi Ajn,
 
I advise you to only put one subject by thread (I²C will probably be skipped).
 
You can configure the compiler to generate location independant assembler, yes.
It is in the options of CodeWarrior. Go into your target options and in the options of the compiler.
 
I'm moving the thread as the question is CodeWarrior specific. I also renamed the thread to mention CW version number.
 
Cheers,
Alban.
0 Kudos
Reply