# IAR 5.41 getting to Hard Fault with low opt

**URL:** <https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385>\
**Category:** Kernel\
**Created:** [August 12, 2011, 4:21pm UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385 "2011-08-12T16:21:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 12, 2011, 4:21pm UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385/1 "2011-08-12T16:21:50Z")

</div>

**[baranov](https://sourceforge.net/u/baranov/)** wrote on [Friday, August 12, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/8e599053/#bd7e):

Hello. I am not really sure that the problem is FreeRtos related.&nbsp;  
I have a project with one task consisting only of vTaskDelay(5000);  
When I compile the project with medium and high optimization options it works fine. When I switch to low it goes to Hard Fault Interrupt. When I single step, it runs to vPortStartFirstTask() and gets into:

```
vPortSVCHandler;
	ldr	r3, =pxCurrentTCB
	ldr r1, [r3]
	ldr r0, [r1]
	ldmia r0!, {r4-r11}
	msr psp, r0
	mov r0, #0
	msr	basepri, r0
	orr r14, r14, #13
	bx r14

```

On exit from it my debugger shows:  
\*\*Usage fault: Attempt to use a coprocessor instruction! \*\*  
Of course I tried to vary stack and heap sizes but it did not clarify the situation.  
Could anybody give a clue, what did the optimization option change?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [August 14, 2011, 10:35am UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385/2 "2011-08-14T10:35:58Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, August 14, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/8e599053/#07ee):

I am guessing, and it is just a guess, that you are using a Cortex-M3 device. If so, then IAR V5.x is out of date.&nbsp; Other than that, how is your C start up code written?&nbsp; The symptom you describe can often happen when the start up is written completely in C, and is caused by a loop variable being stored on the stack with zero optimisation (and so getting wiped out by its own initialisation code) compared to the same variable being held in a register at higher optimisation (and so being safe from being wiped out by memory clearing and initialisation code).

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 15, 2011, 12:34pm UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385/3 "2011-08-15T12:34:35Z")

</div>

**[baranov](https://sourceforge.net/u/baranov/)** wrote on [Monday, August 15, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/8e599053/#1508):

Thank you very much!. Yes, it is STM32.&nbsp; I will try IAR 6.10.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 15, 2011, 2:04pm UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385/4 "2011-08-15T14:04:36Z")

</div>

**[baranov](https://sourceforge.net/u/baranov/)** wrote on [Monday, August 15, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/8e599053/#0241):

IAR 6.10 did not help.  
I saw that in **"medium**" option&nbsp; vPortSVCHandler&nbsp; enters assembler function shown above&nbsp; with  
LR = 0xFFFFFFF9, which is correct and in “ **low** ” option LR has some other value. In my case it is e.g. 0x800D7FF. I guess, I simply have to use medium optimization.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 15, 2011, 2:13pm UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385/5 "2011-08-15T14:13:35Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, August 15, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/8e599053/#2216):

Can you post the code that initializes the C run time environment. The bit that zeros out the memory, normally in a file called crt0 or cstart or startup, etc.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 15, 2011, 2:52pm UTC](https://forums.freertos.org/t/iar-5-41-getting-to-hard-fault-with-low-opt/3385/6 "2011-08-15T14:52:47Z")

</div>

**[baranov](https://sourceforge.net/u/baranov/)** wrote on [Monday, August 15, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/8e599053/#8910):

I think, I solved the problem.  
I had:

```
const intvec_elem __vector_table[] =
{
    { . __ptr =__ sfe( "CSTACK" ) },
    __iar_program_start,
    NMIException,
    HardFaultException,
    MemManageException,
    BusFaultException,
    UsageFaultException,
    0, 0, 0, 0, // Reserved
   SVCHandler,
    DebugMonitor,
    0, // Reserved
    PendSVC,
    SysTickHandler
};

```

and

```
void SVCHandler( void )
{
#ifdef FREERTOS
  	vPortSVCHandler();
#endif
}

```

I replaced it by:

```
const intvec_elem __vector_table[] =
{
    { . __ptr =__ sfe( "CSTACK" ) },
    __iar_program_start,
    NMIException,
    HardFaultException,
    MemManageException,
    BusFaultException,
    UsageFaultException,
    0, 0, 0, 0, // Reserved
   vPortSVCHandler,
    DebugMonitor,
    0, // Reserved
    PendSVC,
    SysTickHandler
};

```

It eliminated call from SVCHandler and now it works with low optimization too.  
Any comments including scornful will be highly appreciated.
