Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion platform/mbed_retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1053,12 +1053,13 @@ extern "C" uint32_t __HeapLimit;
extern "C" int errno;

// Dynamic memory allocation related syscall.
#if defined(TARGET_NUVOTON)
#if (defined(TARGET_NUVOTON) || defined(TWO_RAM_REGIONS))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaner would be nuvoton would also define two ram regions so we would have just one config and no target here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0xc0170 you are right.
I suggest we create an issue in mbed-os so that NUVOTON can use the same define once this PR is merged. What do you think ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, can be done separately, would be nice o t have a tracking issue so we do not forget, thanks


// Overwrite _sbrk() to support two region model (heap and stack are two distinct regions).
// __wrap__sbrk() is implemented in:
// TARGET_NUMAKER_PFM_NUC472 targets/TARGET_NUVOTON/TARGET_NUC472/TARGET_NUMAKER_PFM_NUC472/TOOLCHAIN_GCC_ARM/nuc472_retarget.c
// TARGET_NUMAKER_PFM_M453 targets/TARGET_NUVOTON/TARGET_M451/TARGET_NUMAKER_PFM_M453/TOOLCHAIN_GCC_ARM/m451_retarget.c
// TARGET_STM32L4 targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L4/l4_retarget.c
extern "C" void *__wrap__sbrk(int incr);
extern "C" caddr_t _sbrk(int incr) {
return (caddr_t) __wrap__sbrk(incr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,29 @@ SECTIONS
__end__ = .;
end = __end__;
*(.heap*)
. += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .);
__HeapLimit = .;
} > SRAM1

PROVIDE(__heap_size = SIZEOF(.heap));
PROVIDE(__mbed_sbrk_start = ADDR(.heap));
PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
/* Check if data + heap exceeds RAM1 limit */
ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow")
/* .stack_dummy section doesn't contains any symbols. It is only
* used for linker to calculate size of stack sections, and assign
* values to stack symbols later */
.stack_dummy (COPY):
{
*(.stack*)
} > SRAM1
} > SRAM2

/* Set stack top to end of RAM, and stack limit move down by
* size of stack_dummy section */
__StackTop = ORIGIN(SRAM1) + LENGTH(SRAM1);
__StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2);
_estack = __StackTop;
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
PROVIDE(__stack = __StackTop);
/* Check if stack exceeds RAM2 limit */
ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow")

/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,29 @@ SECTIONS
__end__ = .;
end = __end__;
*(.heap*)
. += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .);
__HeapLimit = .;
} > SRAM1

PROVIDE(__heap_size = SIZEOF(.heap));
PROVIDE(__mbed_sbrk_start = ADDR(.heap));
PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
/* Check if data + heap exceeds RAM1 limit */
ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow")
/* .stack_dummy section doesn't contains any symbols. It is only
* used for linker to calculate size of stack sections, and assign
* values to stack symbols later */
.stack_dummy (COPY):
{
*(.stack*)
} > SRAM1
} > SRAM2

/* Set stack top to end of RAM, and stack limit move down by
* size of stack_dummy section */
__StackTop = ORIGIN(SRAM1) + LENGTH(SRAM1);
__StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2);
_estack = __StackTop;
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
PROVIDE(__stack = __StackTop);
/* Check if stack exceeds RAM2 limit */
ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow")

/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,29 @@ SECTIONS
__end__ = .;
end = __end__;
*(.heap*)
. += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .);
__HeapLimit = .;
} > SRAM1

PROVIDE(__heap_size = SIZEOF(.heap));
PROVIDE(__mbed_sbrk_start = ADDR(.heap));
PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap));
/* Check if data + heap exceeds RAM1 limit */
ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow")
/* .stack_dummy section doesn't contains any symbols. It is only
* used for linker to calculate size of stack sections, and assign
* values to stack symbols later */
.stack_dummy (COPY):
{
*(.stack*)
} > SRAM1
} > SRAM2

/* Set stack top to end of RAM, and stack limit move down by
* size of stack_dummy section */
__StackTop = ORIGIN(SRAM1) + LENGTH(SRAM1);
__StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2);
_estack = __StackTop;
__StackLimit = __StackTop - SIZEOF(.stack_dummy);
PROVIDE(__stack = __StackTop);
/* Check if stack exceeds RAM2 limit */
ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow")

/* Check if data + heap + stack exceeds RAM limit */
ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack")
}
66 changes: 66 additions & 0 deletions targets/TARGET_STM/TARGET_STM32L4/l4_retarget.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
******************************************************************************
* @file l4_retarget.c
* @author MCD Application Team
* @brief CMSIS Cortex-M4 Core Peripheral Access Layer Source File for STM32L475xG
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#if (defined(TWO_RAM_REGIONS) && defined(__GNUC__) && !defined(__CC_ARM))
#include <errno.h>
#include "stm32l4xx.h"
extern uint32_t __mbed_sbrk_start;
extern uint32_t __mbed_krbs_start;

#define STM32L4_HEAP_ALIGN 32
#define STM32L4_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1))
/**
* The default implementation of _sbrk() (in platform/mbed_retarget.cpp) for GCC_ARM requires one-region model (heap and
* stack share one region), which doesn't fit two-region model (heap and stack are two distinct regions), for example,
* STM32L475xG locates heap on SRAM1 and stack on SRAM2.
* Define __wrap__sbrk() to override the default _sbrk(). It is expected to get called through gcc
* hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
*/
void *__wrap__sbrk(int incr)
{
static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
uint32_t heap_ind_old = STM32L4_ALIGN_UP(heap_ind, STM32L4_HEAP_ALIGN);
uint32_t heap_ind_new = STM32L4_ALIGN_UP(heap_ind_old + incr, STM32L4_HEAP_ALIGN);

if (heap_ind_new > &__mbed_krbs_start) {
errno = ENOMEM;
return (void *) -1;
}

heap_ind = heap_ind_new;

return (void *) heap_ind_old;
}
#endif /* GCC_ARM toolchain && TWO_RAM_REGIONS*/

33 changes: 26 additions & 7 deletions targets/TARGET_STM/mbed_rtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,21 @@

#ifndef INITIAL_SP

#if (defined(TARGET_STM32F051R8) ||\
#if (defined(TARGET_STM32L475VG) ||\
defined(TARGET_STM32L476RG) ||\
defined(TARGET_STM32L476JG) ||\
defined(TARGET_STM32L476VG) ||\
defined(TARGET_STM32L486RG))
/* only GCC_ARM and IAR toolchains have the stack on SRAM2 */
#if (((defined(__GNUC__) && !defined(__CC_ARM)) ||\
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not for ARMCC? only those 2 toolchains are here supported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @0xc0170
IAR was done in #5844
This PR is for GCC_ARM (required for Cloud example).
ARM will come soon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume will be created as a new PR that will address ARMCC support for this. I would like to avoid having different behavior between toolchains

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will do it .

defined(__IAR_SYSTEMS_ICC__ )) &&\
defined(TWO_RAM_REGIONS))
#define INITIAL_SP (0x10008000UL)
#else
#define INITIAL_SP (0x20018000UL)
#endif /* toolchains */

#elif (defined(TARGET_STM32F051R8) ||\
defined(TARGET_STM32F100RB) ||\
defined(TARGET_STM32L031K6) ||\
defined(TARGET_STM32L053C8) ||\
Expand Down Expand Up @@ -68,12 +82,7 @@
#elif defined(TARGET_STM32L152RE)
#define INITIAL_SP (0x20014000UL)

#elif (defined(TARGET_STM32F401RE) ||\
defined(TARGET_STM32L475VG) ||\
defined(TARGET_STM32L476RG) ||\
defined(TARGET_STM32L476JG) ||\
defined(TARGET_STM32L476VG) ||\
defined(TARGET_STM32L486RG))
#elif defined(TARGET_STM32F401RE)
#define INITIAL_SP (0x20018000UL)

#elif (defined(TARGET_STM32F207ZG) ||\
Expand Down Expand Up @@ -110,5 +119,15 @@
#endif

#endif // INITIAL_SP
#if (defined(__GNUC__) && !defined(__CC_ARM) && defined(TWO_RAM_REGIONS))
extern uint32_t __StackLimit[];
extern uint32_t __StackTop[];
extern uint32_t __end__[];
extern uint32_t __HeapLimit[];
#define HEAP_START ((unsigned char*)__end__)
#define HEAP_SIZE ((uint32_t)((uint32_t)__HeapLimit - (uint32_t)HEAP_START))
#define ISR_STACK_START ((unsigned char*)__StackLimit)
#define ISR_STACK_SIZE ((uint32_t)((uint32_t)__StackTop - (uint32_t)__StackLimit))
#endif

#endif // MBED_MBED_RTX_H
8 changes: 4 additions & 4 deletions targets/targets.json
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@
}
},
"detect_code": ["0765"],
"macros_add": ["USBHOST_OTHER"],
"macros_add": ["USBHOST_OTHER", "TWO_RAM_REGIONS"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is fine here but instead of macro, if this is a config that would result in MBED_CONF_MULTIPLE_RAM_REGIONS (if there are 3 , would it matter? if yes, then two is fine in the name) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xc0170 the 'TWO' means that heap and stack are located in 2 separate ram regions.
It won't matter if you have 3 ram regions on the MCU. Only 2 will be concerned by this change.

"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_ASYNCH", "SERIAL_FC", "TRNG", "FLASH"],
"release_versions": ["2", "5"],
"device_name": "STM32L476RG",
Expand All @@ -1571,7 +1571,7 @@
}
},
"detect_code": ["0766"],
"macros_add": ["USBHOST_OTHER"],
"macros_add": ["USBHOST_OTHER", "TWO_RAM_REGIONS"],
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_ASYNCH", "SERIAL_FC", "TRNG", "FLASH"],
"release_versions": ["5"],
"device_name": "STM32L476JG"
Expand Down Expand Up @@ -1863,7 +1863,7 @@
},
"supported_form_factors": ["ARDUINO"],
"detect_code": ["0764"],
"macros_add": ["USBHOST_OTHER"],
"macros_add": ["USBHOST_OTHER", "TWO_RAM_REGIONS"],
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_FC", "TRNG", "FLASH"],
"release_versions": ["2", "5"],
"device_name": "STM32L475VG",
Expand All @@ -1885,7 +1885,7 @@
}
},
"detect_code": ["0820"],
"macros_add": ["USBHOST_OTHER"],
"macros_add": ["USBHOST_OTHER", "TWO_RAM_REGIONS"],
"device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_FC", "TRNG", "FLASH"],
"release_versions": ["2", "5"],
"device_name": "STM32L476VG",
Expand Down