AVR32UC3A Ethernet Port - Stack Issues

sm92 wrote on Thursday, March 06, 2008:

I am writing an application based on the Ethernet Interface demo app for FreeRTOS, and am having trouble with what I think is a stack overflow issue.

When my code is linked, memory is allocated as follows:

0x7448 to 0xEFFF for the heap
0xF000 to 0xFFFF for the stack

On startup, my code initializes the power manager on the AVR32, and initializes external SRAM and a couple ADCs.

It then calls vStartEthernetTask, which starts the ethernet task included in the demo app, but modified to handle the data that I send to it over ethernet (a few switch statements, nothing major).

Up until this point, the stack point is in the 0xFFxx range, as it should be, as not much has happened yet.

The next call is to vTaskStartScheduler(), which is where the problem is. By tracing this call using the JTAG MkII debugger, I see that the code calls xPortStartScheduler() in port.c. This then calls portRESTORE_CONTEXT(), which is defined in portmacro.h to be a bunch of assembly code (I’m not really sure what it does).

As soon as portRESTORE_CONTEXT() executes, the stack pointer jumps way down into the 0x7exx range, obliterating a bunch of the heap. The code continues to run fine for a while (the stack pointer stays in the same 0x7exx area) but eventually I get data_address or bus_read (or similar) errors, which I am assuming are due to the heap being corrupted.

Any ideas why the stack gets so massive when initializing the scheduler?


davedoors wrote on Thursday, March 06, 2008:

I don’t know about avr32 specifics but what you describe is what I would expect to see.

When you create a task the stack used by the task is allocated from the heap. Remember that each task has its own stack. Once the first task is started the stack pointer should be pointing some place within your heap.

sm92 wrote on Thursday, March 06, 2008:

That makes sense. Thanks.