Hair-tearing time with a PI32 Stack Overflow.

malcolmsmc wrote on Monday, June 20, 2011:

I am attempting to transfer code from another RTOS (inhouse on a Renesas chip) to Freertos in the PIC32. All appears to work except that I get a random Stack Overflow exception.

When I examine the problem I find the following:
The crash occurs in the vTaskSwitchContext() called from the vPortYeildISR()
The current tcb is the IDLE task
The Top of stack is pointing into the xISRStack instead of it own stack.

Anybody with any ideas before I become bald?

richard_damon wrote on Tuesday, June 21, 2011:

Sounds like something is corrupting your task info.

A few things to look at:
Interrupt that calls FromISR routine having too high of an interrupt priority
Interrupt that calls a non FromISR API routine
Improper nesting of interrrupts
A non-interrupt calling a FromISR routine
Idle Hook function blocks
You have a stack overflow in some task (try turning on overflow detection)

malcolmsmc wrote on Tuesday, June 21, 2011:

My first assumption was stack corruption so I run all interrupts at the same priority to avoid nesting, carefully checked the FromISR calls and use the default idle loop and disabled the pre-emption.
I run with the stack overflow  turned on (2) which is how I found the problem.

The locations round the stack pointer in the TCB are OK except for the stack pointer value itself. The appearance is of the idle task calling an interrupt routine which returns without resetting the stack pointer!

rtel wrote on Tuesday, June 21, 2011:

From the two comments:

All appears to work except that I get a random Stack Overflow exception.

and

I run with the stack overflow  turned on (2) which is how I found the problem.

Are you getting a stack overflow exception?  If so, that is not coming from having the stack overflow protection turned on in FreeRTOS.  The mechanism in FreeRTOS will cause vApplicationStackOverflow() to be called, not an exception to occur.  I know some PIC architectures have stack limit registers catch stack overflows, but I’m not sure that applies to the MIPS based parts, and FreeRTOS does not use them anyway so that functionality should be off in the hardware.

The Top of stack is pointing into the xISRStack instead of it own stack.

Do you mean the top of stack used by the idle task?  If so, then I agree that is wrong.  Otherwise, in an interrupt that called vTaskSwitchContext() I would expect the stack pointer at the time the function was called to be in the ISR stack, as, after all, it is being called from an ISR.

so I run all interrupts at the same priority

Was that the lowest priority - so the same priority used by the kernel for its tick interrupt?
Are you using the assembly wrappers for your interrupts?

Regards.

malcolmsmc wrote on Tuesday, June 21, 2011:

OK - I was a little imprecise.

For Stack Overflow exception read vApplicationStackOverflow() call.

Th idle task pxTopOfStack points into the xISRStack area.

All interrupts are run at the configKERNEL_INTERRUPT_PRIORITY.

I am using the same interupt structure as used for the example :

………
.section .FreeRTOS, “ax”, @progbits
.set noreorder
.set noat
.ent uart4Wrapper

uart4Wrapper:

portSAVE_CONTEXT
jal uart4Handler
nop
portRESTORE_CONTEXT

.end uart4Wrapper

………

As far as I am aware I do no context switching in the interrupt service routines unless they occur in xQueueSendFromISR or xSemaphoreGiveFromISR functions.