diff --git a/flight/PiOS/STM32F10x/link_stm32f10x_HD_NB.ld b/flight/PiOS/STM32F10x/link_stm32f10x_HD_NB.ld index 6b6ba6ef0..29ce327d2 100644 --- a/flight/PiOS/STM32F10x/link_stm32f10x_HD_NB.ld +++ b/flight/PiOS/STM32F10x/link_stm32f10x_HD_NB.ld @@ -39,6 +39,9 @@ MEMORY /* higher address of the user mode stack */ _estack = 0x20010000; +/* This is the size of the stack for early init and for all FreeRTOS IRQs */ +_irq_stack_size = 0x400; + /* default stack sizes. These are used by the startup in order to allocate stacks for the different modes. @@ -258,7 +261,23 @@ SECTIONS _sidata = _etext; } >FLASH - + + /* + * This stack is used both as the initial sp during early init as well as ultimately + * being used as the STM32's MSP (Main Stack Pointer) which is the same stack that + * is used for _all_ interrupt handlers. The end of this stack should be placed + * against the lowest address in RAM so that a stack overrun results in a hard fault + * at the first access beyond the end of the stack. + */ + .irq_stack : + { + . = ALIGN(4); + _irq_stack_end = . ; + . = . + _irq_stack_size ; + . = ALIGN(4); + _irq_stack_top = . ; + } >RAM + /* This is the initialized data section The program executes knowing that the data is in the RAM diff --git a/flight/PiOS/STM32F10x/startup_stm32f10x_HD_NB.S b/flight/PiOS/STM32F10x/startup_stm32f10x_HD_NB.S index 34caf921b..206b0cdd8 100644 --- a/flight/PiOS/STM32F10x/startup_stm32f10x_HD_NB.S +++ b/flight/PiOS/STM32F10x/startup_stm32f10x_HD_NB.S @@ -50,7 +50,6 @@ defined in linker script */ .word _ebss /* stack used for SystemInit_ExtMemCtl; always internal RAM used */ -.equ Initial_spTop, 0x20000400 .equ BootRAM, 0xF1E0F85F /** * @brief This is the code that gets called when the processor first @@ -140,7 +139,7 @@ Infinite_Loop: g_pfnVectors: - .word Initial_spTop + .word _irq_stack_top .word Reset_Handler .word NMI_Handler .word HardFault_Handler