Which stack is used for ISR? (ARM Cortex M4 port)

incrediball wrote on Wednesday, November 07, 2018:

We’re using the port for the Atmel SAM4 on an older FreeRTOS version but one question that I have never definitively managed to answer or have answered is which stack is used for interrupt service routines? I believe it is the task stack, i.e. the stack of the task that has been interrupted. Is that correct? If so, what happens to the stack (I believe it is called the system stack) that was in use before vTaskStartScheduler() is called? Is it ever used again or is it used for FreeRTOS internal stuff? Is it possible to make an ISR switch to using the system stack? Since we are not using nested interrupts it seems wasteful to not have a single stack that the ISRs can use rather than reserving some amount in the stack for every task.

I would like to properly assess stack usage throughout our system but without being clear on what exactly uses which stack, this is not so simple. There is also a reliability aspect to consider since it may be unlikely that a task may hit its peak usage at the exact time when an interrupt fires that needs the most stack, however when it does… Therefore for this reason it would also be good if all ISRs can use a single stack, in my case at least.

rtel wrote on Wednesday, November 07, 2018:

The stack that was used by main() before the scheduler is started is
re-used as the stack used by interrupts.

incrediball wrote on Wednesday, November 07, 2018:

Thank you for the information, that’s good news. Incidentally I had the idea of logging the value of the stack pointer from one of my interrupts and I found that that was the case. I should have thought of that beforehand…

pblase wrote on Thursday, November 22, 2018:

Question: where is this stack defined?
I’m having a problem where globally defined variables in the main program are getting stepped on during the FreeRTOS task stuff that happens during switching.

.bss 0x200004d0 0x20 ProElec_R2_ATSAMC21N.o
0x200004d0 ErrorInfo
0x200004d4 retval
0x200004d8 Heartbeat_LED_timer <==== is getting stepped on
.bss 0x200004f0 0x968 thirdparty/RTOS/freertos/FreeRTOSV10.0.0/Source/portable/MemMang/heap_1.o
.bss 0x20000e58 0xec thirdparty/RTOS/freertos/FreeRTOSV10.0.0/Source/tasks.o
0x20000e58 pxCurrentTCB
.bss 0x20000f44 0x3c thirdparty/RTOS/freertos/FreeRTOSV10.0.0/Source/timers.o

For instance in tasks.c, in prvAddCurrentTaskToDelayedList(), line 4516
const TickType_t xConstTickCount = xTickCount;
writes into the Heartbeat_LED_timer struct.

rtel wrote on Thursday, November 22, 2018:

If this is AtmelStudio/ASF again I assume a .ld file somewhere, which
may get auto generated by the settings in the tool - it is allocated by
the tools in any case. Note you cannot use variables that were on the
stack of main() after the scheduler has started as from that point
interrupts use the same stack.