# vTaskSwitchContext not working on PIC32

**URL:** <https://forums.freertos.org/t/vtaskswitchcontext-not-working-on-pic32/5253>\
**Category:** Kernel\
**Created:** [September 16, 2008, 10:30pm UTC](https://forums.freertos.org/t/vtaskswitchcontext-not-working-on-pic32/5253 "2008-09-16T22:30:53Z")\
**Posts on this page:** 3\
**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 16, 2008, 10:30pm UTC](https://forums.freertos.org/t/vtaskswitchcontext-not-working-on-pic32/5253/1 "2008-09-16T22:30:53Z")

</div>

**[kg4ysn](https://sourceforge.net/u/kg4ysn/)** wrote on [Tuesday, September 16, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/e77a02e4/#fc69):

Hey all,

I posted a message the other day regarding problems with the PIC32 and the MPLAB SIM environment.&nbsp; As I poked around some more, it turns out that the problem may be more involved.

Within a single time slice, prvIdleTask performs as expected – the PC works down through the function to vApplicationIdleHook and then loops.&nbsp; I was able to confirm this by toggling some GPIO pins and watching them with an oscilloscope.

During the evaluation of line 1699 in tasks.c, a “sltiu v0,v0,2” instruction is issued.&nbsp; The next command should be “bne v0,zero,0x9d0014f4.”&nbsp; However, the PC instead jumps to 9FC0\_1200, which is the location for portSAVE\_CONTEXT from ISR\_Support.h.&nbsp; So I’m guessing that some kind of timer triggered off an asynchronous event, and we’re now entering vTaskSwitchContext.

vTaskSwitchContext processes through, and as portRESTORE\_CONTEXT is called, we end with our "ERET" instruction at 9D00\_4B78.&nbsp; But instead of jumping back to where we were at 9D00\_14F4, we jump right back to portSAVE\_CONTEXT and begin the process anew.&nbsp; This continues ad infinium, causing the simulator to crash (due to a spurious error related to "sd ra,-1(ra)" being processed as NOP) and the real PIC32 board to hang.

The full contents of FreeRTOSConfig.h are below.&nbsp; Anyone have an idea on how to solve this one?&nbsp; Is this a bug in v5.0.3, or is it a user error?

Thanks,  
Chris

#define configUSE\_PREEMPTION&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define configUSE\_IDLE\_HOOK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define configUSE\_TICK\_HOOK&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  
#define configTICK\_RATE\_HZ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( portTickType ) 1000 )  
#define configCPU\_CLOCK\_HZ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( unsigned portLONG ) 80000000UL )&nbsp;  
#define configPERIPHERAL\_CLOCK\_HZ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( unsigned portLONG ) 40000000UL )  
#define configMAX\_PRIORITIES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( unsigned portBASE\_TYPE ) 5 )  
#define configMINIMAL\_STACK\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( 400 )  
#define configISR\_STACK\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( 500 )  
#define configTOTAL\_HEAP\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( ( size\_t ) 28000 )  
#define configMAX\_TASK\_NAME\_LEN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( 8 )  
#define configUSE\_TRACE\_FACILITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  
#define configUSE\_16\_BIT\_TICKS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  
#define configIDLE\_SHOULD\_YIELD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define configUSE\_MUTEXES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define configCHECK\_FOR\_STACK\_OVERFLOW&nbsp;&nbsp;&nbsp; 2  
#define configQUEUE\_REGISTRY\_SIZE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0

#define configUSE\_CO\_ROUTINES&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  
#define configMAX\_CO\_ROUTINE\_PRIORITIES ( 2 )

#define INCLUDE\_vTaskPrioritySet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define INCLUDE\_uxTaskPriorityGet&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define INCLUDE\_vTaskDelete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0  
#define INCLUDE\_vTaskCleanUpResources&nbsp;&nbsp;&nbsp; 0  
#define INCLUDE\_vTaskSuspend&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define INCLUDE\_vTaskDelayUntil&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define INCLUDE\_vTaskDelay&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1  
#define INCLUDE\_uxTaskGetStackHighWaterMark&nbsp;&nbsp;&nbsp; 1

#define configKERNEL\_INTERRUPT\_PRIORITY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0x01

#define configMAX\_SYSCALL\_INTERRUPT\_PRIORITY&nbsp;&nbsp;&nbsp; 0x03

---

<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 17, 2008, 6:42am UTC](https://forums.freertos.org/t/vtaskswitchcontext-not-working-on-pic32/5253/2 "2008-09-17T06:42:40Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, September 17, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/e77a02e4/#fc69/40f3):

Exiting one interrupt then entering another is not necessarily an error - this could be a legitimate execution patter especially if interrupts are nesting.

It is my understanding the the simulator is a bit ‘limited’ when it comes to PIC32.&nbsp; I’m sure it will be improved.

Which hardware are you using/do you have access to an Explorer 16?&nbsp; Ideally can you confirm that you have the demo as provided in the [FreeRTOS.org](http://FreeRTOS.org) download running before making any modifications?

If you have different hardware, then can you try a simple LED flashing example first - see [http://www.freertos.org/porting-a-freertos-demo-to-different-hardware.html](http://www.freertos.org/porting-a-freertos-demo-to-different-hardware.html) .

The PIC32 port is being used on some quite complex projects so I am as confident as I can be that it is sound, but obviously I cannot guarantee it.&nbsp; I too have used it quite extensively.

Have you checked that you are not overflowing your stacks?

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 17, 2008, 3:41pm UTC](https://forums.freertos.org/t/vtaskswitchcontext-not-working-on-pic32/5253/3 "2008-09-17T15:41:43Z")

</div>

**[kg4ysn](https://sourceforge.net/u/kg4ysn/)** wrote on [Wednesday, September 17, 2008](https://sourceforge.net/p/freertos/discussion/382005/thread/e77a02e4/#fc69/e3d3):

Hi Richard,

Thanks for the posting.&nbsp; There definitely wasn’t any overflowing of stacks; I used an LED-style example to do debugging to determine if this was the case.

After comparing against the demo code, I found that I had missed a tiny #pragma config declaration.&nbsp; Once I corrected that, the timers seemed to fire off correctly, and context switching worked as expected.&nbsp; So chalk this one up to user error.&nbsp; 🙂

Thanks for taking the time to look into it!

Chris
