# Problem (and fix) for Cortex M3 port

**URL:** <https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200>\
**Category:** Kernel\
**Created:** [May 11, 2008, 9:11am UTC](https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200 "2008-05-11T09:11:29Z")\
**Posts on this page:** 5\
**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:** [May 11, 2008, 9:11am UTC](https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200/1 "2008-05-11T09:11:29Z")

</div>

**[artlip](https://sourceforge.net/u/artlip/)** wrote on [Sunday, May 11, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/b952aef3/#d6e2):

Hi,

I found a small issue with the port for Cortex M3 (at least for GCC and IAR).  
In case when vector table is not located at the begining od FLASH space then initialization of (main) stack will fail.  
It is because there is a code like this (file port.c function xPortStartScheduler):

(in version 5.0.0)  
vPortStartFirstTask( \*((unsigned portLONG \*) 0 ) );

(in version 4.7.2)  
prvSetMSP( \*((unsigned portLONG \*) 0 ) );

I found this during debugging my bootloader, so I think that use of any bootloader for application with this port will fail.

For my purposes I defined (in FreeRTOSConfig.h) macro:  
#define DEF\_APP\_ADDRESS&nbsp;&nbsp;&nbsp; 0x8002000

and change above lines apropriately to:

vPortStartFirstTask( \*((unsigned portLONG \*) 0x8002000 ) );  
prvSetMSP( \*((unsigned portLONG \*) 0x8002000 ) );

Regards,  
–  
Artur Lipowski

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 11, 2008, 10:59am UTC](https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200/2 "2008-05-11T10:59:12Z")

</div>

**[ravaz](https://sourceforge.net/u/ravaz/)** wrote on [Sunday, May 11, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/b952aef3/#d6e2/47b2):

I had the same problem I solved changing the vPortStartFirstTask like this:

void vPortStartFirstTask(void)  
{  
&nbsp;&nbsp;&nbsp; \_\_asm volatile(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; ldr r0, =0xE000ED08&nbsp; \n" /\* Load the NVIC\_TABLE offset address. \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; ldr r0, [r0]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n" /\* Get the vector table offset. \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; ldr r0, [r0]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n" /\* Get the stack pointer. \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; msr msp, r0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n" /\* Set the msp back to the start of the stack. \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "&nbsp;&nbsp;&nbsp; svc 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \n" /\* System call to start first task. \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );  
}

This way you directly load the stack addres from vector table.

Regards,

Ramon

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 11, 2008, 6:53pm UTC](https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200/3 "2008-05-11T18:53:59Z")

</div>

**[zaphodbe](https://sourceforge.net/u/zaphodbe/)** wrote on [Sunday, May 11, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/b952aef3/#d6e2/ee81):

I had the same problme see “Debugging tasks with IAR EWARM” and did a similar fix using \_\_sfb(".intvec"), which is IAR’s macro to get the address of the \_\_vector\_table section “.intvec”.&nbsp; Although getting the NVIC table offset is more portable between compilers.&nbsp;

However this brings up a good question.&nbsp; This is effectively reseting the stack that was created by the compiler for the initialization and setup of FreeRTOS.&nbsp; Although the exit capability "vTaskEndScheduler" of FreeRTOS is currently not implemented in the Cortex-M3 port, reseting the stack would not allow this functionality if we did want it.&nbsp;

Is there a reason the stack gets reset like this at this point?  
If not it seems to me that it would be better not to reload the MSP?

---

<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:** [May 11, 2008, 7:54pm UTC](https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200/4 "2008-05-11T19:54:22Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Sunday, May 11, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/b952aef3/#d6e2/ee81/02f8):

\>Is there a reason the stack gets reset like this at this point?

This is just to allow you to reuse/recover some RAM.&nbsp; [FreeRTOS.org](http://FreeRTOS.org) runs on some pretty tiny CM3 devices, for which it is more relevant.&nbsp; The ports than run onto of DOS are the only ports that really make use of the end scheduler function call, as there you have another OS to return to.

Is there are change request in the SourceForge system already regarding the assumption of the vector table being at zero?&nbsp; I would be grateful if somebody could add one if not, or update the existing one to reference the portable solution.

Thanks for everybody’s contribution.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 12, 2008, 8:46am UTC](https://forums.freertos.org/t/problem-and-fix-for-cortex-m3-port/4200/5 "2008-05-12T08:46:17Z")

</div>

**[artlip](https://sourceforge.net/u/artlip/)** wrote on [Monday, May 12, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/b952aef3/#d6e2/ee81/02f8/e03b):

\> I would be grateful if somebody could add one if not, or update  
\> the existing one to reference the portable solution.

Done.  
Request ID 1962259.

Regards,  
–  
Artur Lipowski
