Skip to content

Commit

Permalink
boot/startup/cortexm-m4: Add separate memory region for MCU stack
Browse files Browse the repository at this point in the history
Stack was always placed at the end of region RAM.
This adds separate memory region for MCU stack.
For some MCUs it is more convenient to place stack in separate memory
space and that was not possible without custom linker script.

__HeapLimit and __HeapBase assignments are simpler to read
__StackTop and __StackLimi as well.

dumplicate .bssnz section is removed
(NOLOAD) attributes added to .bssnz and .bss

.mtb section removed for now

Signed-off-by: Jerzy Kasenberg <jerzy.kasenberg@codecoup.pl>
  • Loading branch information
kasjer committed May 15, 2024
1 parent b16ca49 commit 5814aa0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
7 changes: 4 additions & 3 deletions boot/startup/include/mynewt_config.ld.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
#ifndef BSSNZ_RAM
#define BSSNZ_RAM RAM
#endif
#ifndef STACK_RAM
#define STACK_RAM RAM
#endif

#ifndef MYNEWT_VAL_RESET_HANDLER
#define RESET_HANDLER Reset_Handler
Expand All @@ -60,8 +57,12 @@
#define STACK_SIZE MYNEWT_VAL_MAIN_STACK_SIZE
#endif

#ifndef RAM_START
#define RAM_START MYNEWT_VAL_MCU_RAM_START
#endif
#ifndef RAM_SIZE
#define RAM_SIZE MYNEWT_VAL_MCU_RAM_SIZE
#endif

#ifndef MYNEWT_CODE
#if MYNEWT_VAL_RAM_RESIDENT
Expand Down
42 changes: 20 additions & 22 deletions boot/startup/mynewt_cortex_m4.ld
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <syscfg/syscfg.h>
#include <sysflash/sysflash.h>
#include <mcu_config.ld.h>
#include <bsp_config.ld.h>
#include <target_config.ld.h>
#include <mynewt_config.ld.h>
Expand All @@ -36,7 +37,18 @@ MEMORY
#ifdef FLASH_AREA_IMAGE_0_OFFSET
SLOT0 (rx!w) : ORIGIN = FLASH_AREA_IMAGE_0_OFFSET, LENGTH = FLASH_AREA_IMAGE_0_SIZE
#endif
/*
* If STACK_REGION is defined it means that MCU stack is place in other region then RAM
* int that case RAM region size is exactly RAM_SIZE
* If STACK_REGION is NOT defined STACK_RAM region is place at the end of RAM
* and RAM region size is shortened.
*/
#ifdef STACK_REGION
RAM (rwx) : ORIGIN = RAM_START, LENGTH = RAM_SIZE
#else
RAM (rwx) : ORIGIN = RAM_START, LENGTH = (RAM_SIZE - STACK_SIZE)
STACK_RAM (rw) : ORIGIN = RAM_START + RAM_SIZE - STACK_SIZE, LENGTH = STACK_SIZE
#endif
#include <memory_regions.ld.h>
}

Expand Down Expand Up @@ -245,7 +257,7 @@ SECTIONS
__data_image__ = LOADADDR(.data);
_sidata = LOADADDR(.data);

.bssnz :
.bssnz (NOLOAD) :
{
. = ALIGN(4);
__bssnz_start__ = .;
Expand All @@ -267,14 +279,8 @@ SECTIONS
__ecorebss = .;
} > COREBSS_RAM
#endif
.bssnz (NOLOAD):
{
. = ALIGN(4);
*(.bss.core.nz)
. = ALIGN(4);
} > RAM

.bss :
.bss (NOLOAD) :
{
. = ALIGN(4);
_sbss = .;
Expand All @@ -289,27 +295,19 @@ SECTIONS
/* Heap starts after BSS */
. = ALIGN(8);
__HeapBase = .;
/* Top of head is the bottom of the stack */
__HeapLimit = ORIGIN(RAM) + LENGTH(RAM);

/* Dummy section to calculate whether we need to move stack out of MTB
* buffer or not. */
.mtb (NOLOAD) :
{
KEEP(*(.mtb));
}
/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__HeapBase <= __HeapLimit, "No space for MCU heap")

_ram_start = ORIGIN(RAM);

/* Set stack top to end of RAM, and stack limit move down by
* size of stack_dummy section */
__StackTop = ORIGIN(RAM) + LENGTH(RAM) - SIZEOF(.mtb);
__StackTop = ORIGIN(STACK_RAM) + LENGTH(STACK_RAM);
__StackLimit = ORIGIN(STACK_RAM);
_estack = __StackTop;
__StackLimit = __StackTop - STACK_SIZE;
PROVIDE(__stack = __StackTop);

/* Top of head is the bottom of the stack */
__HeapLimit = __StackLimit;

/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__HeapBase <= __HeapLimit, "region RAM overflowed with stack")
}

0 comments on commit 5814aa0

Please sign in to comment.