Code optimisation

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Code optimisation

1,571件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bhallett on Tue May 18 07:34:27 MST 2010
Hello all,

I am having trouble with one particular c module which I have developed in such that when I select to optimise that module (anything greater than -O0, no optimization) the module stops working in a predictable way.

One point I have noted is that with one particular test I performed
as (including CMSIS support):

LPC_TMR16B0->PR = 43;

unsigned long TEST = LPC_TMR16B0->PR;

the value TEST returns zero (0) when I optimise the code and 43 when no optimisation is selected, can anyone clarify what is happening here?

Performing a similar test with:

LPC_TMR16B0->TCR = 0x02;

unsigned long TEST = LPC_TMR16B0->TCR;

Returns the correct value optimised or not.

Regards

Barry
0 件の賞賛
返信
13 返答(返信)

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Jul 15 02:13:47 MST 2010

Quote: JoeMoney
got it, declared it as volatile and it works... its my first time programming in C so it seems that the best way is to declare any ram resources as a volatile entity


Just make sure you don't declare every variable with volatile - depending on your code base this could have a rather large effect on your code size and the performance of your program. Only declare those variables with volatile that need to be!

If you are new to embedded programming in C, then I have just come across the following online book, which might be useful to you. I've only had a very quickly glance at it - so no guarantees as to how good it actually is. It is also based around a different processor architecture/tools set - but the principles should basically apply:

http://users.ece.utexas.edu/~valvano/embed/toc1.htm

I would in particular suggest that you look at:

http://users.ece.utexas.edu/~valvano/embed/chap4/chap4.htm#VOLATILE

which contains an example which matches exactly with your problem case.

Regards,
CodeRedSupport
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by JoeMoney on Thu Jul 15 01:32:31 MST 2010

Quote: CodeRedSupport
Looking at a disassembly of the object code that you sent, it appears that "Delay_Done" has probably not been declared with volatile - and it needs to be, as it is modified by code outside of your loop (ie by your interrupt handler).

For details of how to create a disassembly of an object file, see:

http://lpcxpresso.code-red-tech.com/LPCXpresso/node/29
[login required]

Regards,
CodeRedSupport



got it, declared it as volatile and it works... its my first time programming in C so it seems that the best way is to declare any ram resources as a volatile entity


Thanks a lot!
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by JoeMoney on Thu Jul 15 01:26:05 MST 2010

Quote: CodeRedSupport
Ok. You modified your post whilst I was replying, and added a code snippet.

What is "Delay_Done" declared as ??

Regards,
CodeRedSupport.



sorry, forgot to mention it.. Delay_Done was declared as an uint8_t and supposed to be mapped to ram since it was declared in a seperate header file in which all of my variables are declared...
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Jul 15 00:48:22 MST 2010

Quote: CodeRedSupport
What is "Delay_Done" declared as ??


Looking at a disassembly of the object code that you sent, it appears that "Delay_Done" has probably not been declared with volatile - and it needs to be, as it is modified by code outside of your loop (ie by your interrupt handler).

For details of how to create a disassembly of an object file, see:

http://lpcxpresso.code-red-tech.com/LPCXpresso/node/29
[login required]

Regards,
CodeRedSupport
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Jul 15 00:34:19 MST 2010
Ok. You modified your post whilst I was replying, and added a code snippet.

What is "Delay_Done" declared as ??

Regards,
CodeRedSupport.
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bhallett on Thu Jul 15 00:32:10 MST 2010
Hi JoeMoney,

Since my last response to CodeRedSupport I have not been able to recreate the  problem but I do still wonder where the problem was.

Regards

Barry
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Jul 15 00:31:04 MST 2010
Although the reason why code built with optimisation fails to run correctly is sometimes a fault with the compiler, quite often it is actually down to the way that your code is written is not "optimisation friendly".

The two of the most common causes of problems are:

[LIST]
[*] Where variables which map onto memory mapped peripheral devices have  not been marked as 'volatile'. With a debug build, such code can often  work, as the compiler does not optimise out memory access. But if such  variables are not marked as 'volatile', a release build will often  optimise them out. For more information, please see: http://en.wikipedia.org/wiki/Volatile_variable
[/LIST]

[LIST]
[*] Timing loops. When compiled for release, if you have loops which  simply count up to a particular value, for example waiting for a  configuration register to change, the count can often no longer be  sufficient when the code is compiled for Release due to the additional  optimisation.
[/LIST]
Regards,
CodeRedSupport.
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by JoeMoney on Thu Jul 15 00:15:50 MST 2010
i'm also having the same issues in which the program is not running when optimization is active. I have sent the object file via email

this is the part that i also suspects is the culprit

void DelayUSec (uint32_t Time)
{
Delay_Done = 0;
((LPC_TMR_TypeDef *) ((0x40000000UL) + 0x18000))->MCR |= 0x01;
((LPC_TMR_TypeDef *) ((0x40000000UL) + 0x18000))->MR0 = ((LPC_TMR_TypeDef *) ((0x40000000UL) + 0x18000))->TC + (Time * 48);
while(!Delay_Done);
((LPC_TMR_TypeDef *) ((0x40000000UL) + 0x18000))->MCR &= ~(0x01);
}

and the interrupt handler:

void TIMER32_1_IRQHandler (void)
{
uint32_t Stat;
Stat = ((LPC_TMR_TypeDef *) ((0x40000000UL) + 0x18000))->IR;


if (Stat & 0x01)
{
  if (!Delay_Done)
   Delay_Done = 1;

  ((LPC_TMR_TypeDef *) ((0x40000000UL) + 0x18000))->IR = 0x01;
}

}
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed May 19 06:22:47 MST 2010

Quote: bhallett

In the process of organising a sample application for you the problem seems to have gone away i.e. when producing a application which included my module in question the module worked as expected. When going back to my main application and making the module identical to the sample app module all worked as expected. Needless to say I will hold off sending you a sample app until/if the problem re-appears.


I'm glad to hear that you now have a working version of your application.

From an investigative point of view, I don't necessarily need a full sample application - just a compileable version of the function that was giving you the problem would be fine. I just need to be able to build it and look at the assembly code generated. If your function makes use of lots of defines, your would either need to make sure you also supplied those defines (either directly or in header files), or run your source file through the preprocessor using the compiler -E option.

To use the -E option...

[LIST=1]
[*]Right click on the file in the Project Explorer view, and select properties.
[*]In C/C++ Build -> Settings -> MCU  C Compiler, select Miscellaneous
[*]Add "-E" to the end of the list of other flags.
[*]Click OK.
[*]Now right click on the file again in the Project Explorer view, and select "Build selected file(s)".
[*]Now open the \Debug\src folder of the project in project explorer view, and find [COLOR=Orange][I]filename[/I][/COLOR].o. Double click on this and it should launch  the preprocessed file in an editor window. Now copy the appropriate parts of this file into a separate file to send to us.
[*]Now right click on the source file in the Project Explorer view, and select properties again. Now either click on "Restore Defaults", or simply edit the Miscellenous entry to remove the "-E" option.
[/LIST]

Quote:

When using CMSIS support do I have to make sure that the Release/Debug build, optimisation levels etc are the same as my main application?

There is no specific need to use the same optimisation levels in a library project as in your application project. Just be aware that if you end up debugging code within your library project that your debug view might become affected by optimisation issues, as described in....

http://lpcxpresso.code-red-tech.com/LPCXpresso/node/26

This also applies to the files within your project - there is nothing to stop you using different optimisation levels for different files...

http://lpcxpresso.code-red-tech.com/LPCXpresso/node/33

Regards,
CodeRedSupport
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bhallett on Wed May 19 03:41:24 MST 2010
Hi CodeRedSupport,

In the process of organising a sample application for you the problem seems to have gone away i.e. when producing a application which included my module in question the module worked as expected. When going back to my main application and making the module identical to the sample app module all worked as expected. Needless to say I will hold off sending you a sample app until/if the problem re-appears.

When using CMSIS support do I have to make sure that the Release/Debug build, optimisation levels etc are the same as my main application?

Regards

Barry
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Wed May 19 00:48:58 MST 2010

Quote: bhallett

I can provide you with sample code but can I send it to you directly?


Given the issue in question, yes. Please use the "[COLOR=Orange][I]email : support[/I][COLOR=Black]"[/COLOR][/COLOR] link at: http://www.code-red-tech.com/support.php and quote this forum thread in your request so that we can tie the two together.

Regard,
CodeRedSupport.
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bhallett on Wed May 19 00:40:25 MST 2010
Hi CodeRedSupport,

I can provide you with sample code but can I send it to you directly?

At the moment I am trying to resolve the problem on the LPCXpresso PCB with the LPC1343 MCU but ultimately it will be going on a target which contains a LPC1342 MCU.

Regards

Barry
0 件の賞賛
返信

1,547件の閲覧回数
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Tue May 18 08:39:30 MST 2010
Can you provide a compilable version of the module, or even just the function, that shows up this problem?

Also, can you confirm which MCU you are targeting?

Regards,

CodeRedSupport.
0 件の賞賛
返信