# portRestoreContext clobbers PIC32MZ EF Status register

**URL:** <https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227>\
**Category:** Kernel\
**Created:** [September 8, 2015, 1:22pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227 "2015-09-08T13:22:45Z")\
**Posts on this page:** 15\
**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:** [September 8, 2015, 1:22pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/1 "2015-09-08T13:22:45Z")

</div>

**[pbjork](https://sourceforge.net/u/pbjork/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#826d):

At the end of xPortStartScheduler(), the portRestoreContext is botching the stack frame for the first task to run. Somehow it is pulling a bad value for the CP0 Status register off the stack and corrupting several important bits including the CU1 fpu coprocessor enable bit. I don’t observe any other problems with the first task; it starts up well and continues on to several other tasks, seemingly without a hitch. It does seem, though, if I restore the Status register manually after corruption, that it gets corrupted again at each call of portRestoreContext.

Here’s what’s at the ( UBaseType\_t \* ) pxCurrentTCB: a nice address = 0x80004F2C.  
The contents of this is, according to the memory view, is 0xA5A5A5A5. I know, not a valid value. The saved stack pointer, just before the call to portRestoreContext, is a different kind of invalid, I think:

uxSavedTaskStackPointer = \*( UBaseType\_t \* ) pxCurrentTCB = 0x00000000 !

I don’t know why this doesn’t come up as the value 0xA5A5A5A5 that the memory view shows. But neither value seems valid as a stack pointer.

Any ideas as to the cause of this?

I’ve raised the configMAX\_SYSCALL\_INTERRUPT\_PRIORITY and configMAX\_PRIORITIES to relatively high levels. I’m trying to see whether any FreeRTOS API gets called before xPortStartScheduler(). Any other diagnostics?

Setup: the latest FreeRTOS v 8.x.x, shipping with MHC 1.06 for PIC32MZ EF family processors.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2015, 1:34pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/2 "2015-09-08T13:34:17Z")

</div>

**[pbjork](https://sourceforge.net/u/pbjork/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#9ebc):

Observations:

I do notice that OSAL\_MUTEX\_Create() is called by some third party code that I’m integrating into the project before xPortStartScheduler() is called. OSAL\_USE\_RTOS is defined as 1. I don’t think this is a problem. Or do I need to rewrite it to use a FreeRTOS semaphore or mutex?

I also see that when the first task is actually called, it is NOT the highest priority task.

Problem?

---

<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:** [September 8, 2015, 4:12pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/3 "2015-09-08T16:12:17Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#826d/f4ec):

The EF parts are not supported yet - but will be soon.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2015, 4:20pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/4 "2015-09-08T16:20:11Z")

</div>

**[pbjork](https://sourceforge.net/u/pbjork/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#d4ea):

Further observations:

I eliminated everything nonessential. At main(), I create a system task, then start the scheduler. That’s all. The Status Register comes back corrupted after portRestoreContext, to the system task.

Must be a configuration setting somewhere.

---

<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:** [September 8, 2015, 4:26pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/5 "2015-09-08T16:26:20Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#d4ea/0e51):

Please see my reply to your original post. EF parts are not supported  
yet, but will be soon.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2015, 4:26pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/6 "2015-09-08T16:26:54Z")

</div>

**[pbjork](https://sourceforge.net/u/pbjork/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#826d/f4ec/4e4d):

Oh my.

No wonder. The Harmony release notes make no mention of this. I’ll have to put out a word on the Harmony forum.

Any idea of how soon the EF parts will be supported?

---

<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:** [September 8, 2015, 4:32pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/7 "2015-09-08T16:32:25Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#826d/f4ec/4e4d/265a):

The work is already in progress, but I’m afraid I can’t put a date on it  
yet.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2015, 4:36pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/8 "2015-09-08T16:36:30Z")

</div>

**[pbjork](https://sourceforge.net/u/pbjork/)** wrote on [Tuesday, September 08, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#d4ea/0e51/c03e):

Thanks, just saw it, responded, and put out a note on the Microchip Harmony forum.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 17, 2015, 9:26am UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/9 "2015-09-17T09:26:17Z")

</div>

**[iberoxarxa](https://sourceforge.net/u/iberoxarxa/)** wrote on [Thursday, September 17, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#6179):

From Iberoxarxa we got the first results with Lua 5.3.1 running on FreeRTOS on PIC32MZ2048EFH064. MZ ports have been modified to achieve this:

- CP0 status is initialized in pxPortInitialiseStack. portINITIAL\_SR is invalid for this silicon, because it sets CU1 to 0 disabling the FPU. It is mandatory to apply 0x20000000 mask to portINITIAL\_SR for enabling FPU.

- FPU registers are 64 bit wide. There are 32 registers ($f0 to $f31). This registers must saved and restore in portSAVE\_CONTEXT, portRESTORE\_CONTEXT and vPortYieldISR. For this use sdc1 / ldc1 MIPS instruction. Be careful with that, so sdc1 / ldc1 requires a 8 word alignment stack. Changes in pxPortInitialiseStack are required for this.

- FPU has 4 control registers. Register FCSR must be saved / restored in portSAVE\_CONTEXT, portRESTORE\_CONTEXT and vPortYieldISR.

Doing this our project: FreeRTOS + Lua 5.3.1 + Lwip runs.

Attached you have the modified port files. Probably all is not fine with our work. More tests are needed. We hope that this modifications will be useful for FreeRTOS team.

You must put “#define configUSE\_FPU 1” in your FreeRTOSConfig.h file.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 17, 2015, 1:20pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/10 "2015-09-17T13:20:48Z")

</div>

**[iberoxarxa](https://sourceforge.net/u/iberoxarxa/)** wrote on [Thursday, September 17, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#ade4):

isr stack must be 8 byte alignment. This is done putting this in port.c:

#if( configUSE\_FPU == 1 )  
const StackType\_t \* const xISRStackTop = &( xISRStack[configISR\_STACK\_SIZE - 8] );  
#else  
const StackType\_t \* const xISRStackTop = &( xISRStack[configISR\_STACK\_SIZE - 7] );  
#endif

With this (and previous post) FreeRTOS inour project is stable. 3 hours running, responding to ping, and executing Lua scripts without any problem.

---

<div class="post-metadata">

**Author:** ![tlafleur](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tlafleur](https://forums.freertos.org/u/tlafleur)\
**Post date:** [September 17, 2015, 1:31pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/11 "2015-09-17T13:31:48Z")

</div>

**[tlafleur](https://sourceforge.net/u/tlafleur/)** wrote on [Thursday, September 17, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#ade4/9aa1):

Also, will you be posting your LUA project core?

~~\_/) `_/)` \_/) ``` \_/)~~

On Sep 17, 2015, at 6:20 AM, Jaume Olivé Petrus [iberoxarxa@users.sf.net](mailto:iberoxarxa@users.sf.net) wrote:

isr stack must be 8 byte alignment. This is done putting this in port.c:

if( configUSE\_FPU == 1 )

const StackType\_t \* const xISRStackTop = &( xISRStack[configISR\_STACK\_SIZE - 8] );

else

const StackType\_t \* const xISRStackTop = &( xISRStack[configISR\_STACK\_SIZE - 7] );

endif

With this (and previous post) FreeRTOS inour project is stable. 3 hours running, responding to ping, and executing Lua scripts without any problem.

portRestoreContext clobbers PIC32MZ EF Status register

Sent from [sourceforge.net](http://sourceforge.net) because you indicated interest in [https://sourceforge.net/p/freertos/discussion/382005/](https://sourceforge.net/p/freertos/discussion/382005/)

To unsubscribe from further messages, please visit [https://sourceforge.net/auth/subscriptions/](https://sourceforge.net/auth/subscriptions/)

---

<div class="post-metadata">

**Author:** ![tlafleur](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tlafleur](https://forums.freertos.org/u/tlafleur)\
**Post date:** [September 17, 2015, 1:37pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/12 "2015-09-17T13:37:17Z")

</div>

**[tlafleur](https://sourceforge.net/u/tlafleur/)** wrote on [Thursday, September 17, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#6179/f400):

You may want to consider using the pre define macro to wrap you changes

define \_\_PIC32\_FEATURE\_SET “EF”

i~~\_/) `_/)` \_/) ``` \_/)~~i

Tom Lafleur  
(858) 759-9692

> On Sep 17, 2015, at 2:26 AM, Jaume Olivé Petrus [iberoxarxa@users.sf.net](mailto:iberoxarxa@users.sf.net) wrote:
> 
> From Iberoxarxa we got the first results with Lua 5.3.1 running on FreeRTOS on PIC32MZ2048EFH064. MZ ports have been modified to achieve this:
> 
> CP0 status is initialized in pxPortInitialiseStack. portINITIAL\_SR is invalid for this silicon, because it sets CU1 to 0 disabling the FPU. It is mandatory to apply 0x20000000 mask to portINITIAL\_SR for enabling FPU.
> 
> FPU registers are 64 bit wide. There are 32 registers ($f0 to $f31). This registers must saved and restore in portSAVE\_CONTEXT, portRESTORE\_CONTEXT and vPortYieldISR. For this use sdc1 / ldc1 MIPS instruction. Be careful with that, so sdc1 / ldc1 requires a 8 word alignment stack. Changes in pxPortInitialiseStack are required for this.
> 
> FPU has 4 control registers. Register FCSR must be saved / restored in portSAVE\_CONTEXT, portRESTORE\_CONTEXT and vPortYieldISR.
> 
> Doing this our project: FreeRTOS + Lua 5.3.1 + Lwip runs.
> 
> Attached you have the modified port files. Probably all is not fine with our work. More tests are needed. We hope that this modifications will be useful for FreeRTOS team.
> 
> You must put “#define configUSE\_FPU 1” in your FreeRTOSConfig.h file.
> 
> Attachments:
> 
> ef-parts.zip (17.1 kB; application/zip)  
> portRestoreContext clobbers PIC32MZ EF Status register
> 
> Sent from [sourceforge.net](http://sourceforge.net) because you indicated interest in [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/p/freertos/discussion/382005/)
> 
> To unsubscribe from further messages, please visit [SourceForge.net: Log In to SourceForge.net](https://sourceforge.net/auth/subscriptions/)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 17, 2015, 1:39pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/13 "2015-09-17T13:39:21Z")

</div>

**[iberoxarxa](https://sourceforge.net/u/iberoxarxa/)** wrote on [Thursday, September 17, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#ade4/9aa1/b756):

Project will be release to general public before December 31, 2015.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 21, 2015, 1:44pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/14 "2015-09-21T13:44:58Z")

</div>

**[pbjork](https://sourceforge.net/u/pbjork/)** wrote on [Monday, September 21, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#d7c1):

I have been informed by Microchip staff that PIC32MZ EF support for FreeRTOS should be included in the next MHC Harmony release, 1.0.7. Not sure of the release date nor that RTEL concurs, however.

---

<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:** [September 21, 2015, 1:49pm UTC](https://forums.freertos.org/t/portrestorecontext-clobbers-pic32mz-ef-status-register/2227/15 "2015-09-21T13:49:33Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, September 21, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/67669bf4/#d7c1/74c0):

Yes - concur with that. Hopefully we will be able to provide it as a  
separate download prior to that.

Regards.
