Skip to content

Commit 40a0b28

Browse files
authored
Merge pull request ARMmbed#12225 from evedon/ed-heap-stack-test
Fix baremetal heap and stack initialisation
2 parents 43a8c90 + 3e3af70 commit 40a0b28

File tree

4 files changed

+119
-48
lines changed

4 files changed

+119
-48
lines changed

TESTS/mbedmicro-rtos-mbed/heap_and_stack/main.cpp

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
#if defined(TARGET_CORTEX_A) || !DEVICE_USTICKER || !defined(MBED_CONF_RTOS_PRESENT)
18+
#if defined(TARGET_CORTEX_A) || !DEVICE_USTICKER
1919
#error [NOT_SUPPORTED] test not supported.
2020
#else
2121

@@ -39,9 +39,9 @@ static const int test_timeout = 30;
3939
// Malloc fill pattern
4040
#define MALLOC_FILL 0x55
4141

42-
extern uint32_t mbed_heap_start;
42+
extern unsigned char *mbed_heap_start;
4343
extern uint32_t mbed_heap_size;
44-
extern uint32_t mbed_stack_isr_start;
44+
extern unsigned char *mbed_stack_isr_start;
4545
extern uint32_t mbed_stack_isr_size;
4646

4747
#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP)
@@ -57,6 +57,27 @@ struct linked_list {
5757
};
5858

5959

60+
// Global test variables
61+
#define TEST_VALUE 789
62+
static struct Test {
63+
Test() : val(TEST_VALUE) {}
64+
~Test() {}
65+
int val;
66+
} t;
67+
68+
int test_function()
69+
{
70+
return TEST_VALUE;
71+
}
72+
static int global_int = test_function();
73+
74+
/** Test global variables initialisation
75+
*/
76+
void test_global_variables_initialisation(void)
77+
{
78+
TEST_ASSERT_EQUAL(TEST_VALUE, global_int);
79+
TEST_ASSERT_EQUAL(TEST_VALUE, t.val);
80+
}
6081

6182
/* TODO: add memory layout test.
6283
*
@@ -122,7 +143,7 @@ static void allocate_and_fill_heap(linked_list *&head)
122143
if (NULL == temp) {
123144
break;
124145
}
125-
bool result = rangeinrange((uint32_t) temp, sizeof(linked_list), mbed_heap_start, mbed_heap_size);
146+
bool result = rangeinrange((uint32_t) temp, sizeof(linked_list), (uint32_t)mbed_heap_start, mbed_heap_size);
126147
#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP)
127148
if (false == result) {
128149
result = rangeinrange((uint32_t) temp, sizeof(linked_list), (uint32_t)mbed_heap_start_0, mbed_heap_size_0);
@@ -174,7 +195,8 @@ void test_heap_in_range(void)
174195
initial_heap = (char *) malloc(1);
175196
TEST_ASSERT_NOT_NULL(initial_heap);
176197

177-
bool result = inrange((uint32_t) initial_heap, mbed_heap_start, mbed_heap_size);
198+
bool result = inrange((uint32_t) initial_heap, (uint32_t)mbed_heap_start, mbed_heap_size);
199+
178200
#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP)
179201
if (false == result) {
180202
result = inrange((uint32_t) initial_heap, (uint32_t)mbed_heap_start_0, mbed_heap_size_0);
@@ -184,6 +206,7 @@ void test_heap_in_range(void)
184206
free(initial_heap);
185207
}
186208

209+
#if MBED_CONF_RTOS_PRESENT
187210
/** Test for Main thread stack
188211
189212
Given a Main thread and its stack
@@ -203,6 +226,7 @@ void test_main_stack_in_range(void)
203226

204227
TEST_ASSERT_TRUE_MESSAGE(result, "Main stack in wrong location");
205228
}
229+
#endif // #if MBED_CONF_RTOS_PRESENT
206230

207231
/** Test for Scheduler/ISR thread stack
208232
@@ -214,7 +238,7 @@ void test_isr_stack_in_range(void)
214238
{
215239
// MSP stack should be very near end (test using within 128 bytes)
216240
uint32_t msp = __get_MSP();
217-
bool result = inrange(msp, mbed_stack_isr_start + mbed_stack_isr_size - 0x400, 0x400);
241+
bool result = inrange(msp, (uint32_t)mbed_stack_isr_start + mbed_stack_isr_size - 0x400, 0x400);
218242

219243
TEST_ASSERT_TRUE_MESSAGE(result, "Interrupt stack in wrong location");
220244
}
@@ -244,8 +268,11 @@ void test_heap_allocation_free(void)
244268

245269
// Test cases
246270
Case cases[] = {
271+
Case("Test global variables initialisation", test_global_variables_initialisation),
247272
Case("Test heap in range", test_heap_in_range),
273+
#if MBED_CONF_RTOS_PRESENT
248274
Case("Test main stack in range", test_main_stack_in_range),
275+
#endif
249276
Case("Test isr stack in range", test_isr_stack_in_range),
250277
Case("Test heap allocation and free", test_heap_allocation_free)
251278
};
@@ -263,4 +290,4 @@ int main()
263290
return !utest::v1::Harness::run(specification);
264291
}
265292

266-
#endif // defined(TARGET_CORTEX_A) || !DEVICE_USTICKER || !defined(MBED_CONF_RTOS_PRESENT)
293+
#endif // defined(TARGET_CORTEX_A) || !DEVICE_USTICKER

platform/source/TARGET_CORTEX_M/TOOLCHAIN_IAR/cmain.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
EXTERN exit
4747
EXTERN __iar_dynamic_initialization
4848
EXTERN mbed_sdk_init
49+
EXTWEAK __mbed_init
4950
EXTERN mbed_main
5051
EXTERN SystemInit
5152

@@ -68,6 +69,9 @@ __cmain:
6869
FUNCALL __cmain, mbed_sdk_init
6970
BL mbed_sdk_init
7071
MOVS r0,#0 ; No parameters
72+
FUNCALL __cmain, __mbed_init
73+
BL __mbed_init
74+
MOVS r0,#0 ; No parameters
7175
FUNCALL __cmain, __iar_dynamic_initialization
7276
BL __iar_dynamic_initialization ; C++ dynamic initialization
7377

platform/source/mbed_retarget.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -561,23 +561,6 @@ std::FILE *fdopen(FileHandle *fh, const char *mode)
561561
extern "C" FILEHANDLE PREFIX(_open)(const char *name, int openflags)
562562
{
563563
#if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
564-
#if !defined(MBED_CONF_RTOS_PRESENT)
565-
// valid only for mbed 2
566-
// for ulib, this is invoked after RAM init, prior c++
567-
// used as hook, as post stack/heap is not active there
568-
extern void mbed_copy_nvic(void);
569-
extern void mbed_sdk_init(void);
570-
571-
static int mbed_sdk_inited = 0;
572-
if (!mbed_sdk_inited) {
573-
mbed_copy_nvic();
574-
mbed_sdk_init();
575-
#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
576-
us_ticker_init();
577-
#endif
578-
mbed_sdk_inited = 1;
579-
}
580-
#endif
581564
// Before version 5.03, we were using a patched version of microlib with proper names
582565
// This is the workaround that the microlib author suggested us
583566
static int n = 0;

platform/source/mbed_sdk_boot.c

Lines changed: 81 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,20 @@
2121
#include "cmsis.h"
2222
#include "hal/us_ticker_api.h"
2323

24-
/* This startup is for mbed 2 baremetal. There is no config for RTOS for mbed 2,
25-
* therefore we protect this file with MBED_CONF_RTOS_PRESENT
26-
* Note: The new consolidated started for mbed OS is in rtos/mbed_boot code file.
24+
/* This startup is for baremetal. There is no RTOS in baremetal,
25+
* therefore we protect this file with MBED_CONF_RTOS_PRESENT.
26+
* Note: The start-up code for mbed OS is in rtos/source/TARGET_CORTEX/mbed_boot code file.
2727
*/
2828
#if !defined(MBED_CONF_RTOS_PRESENT)
2929

30+
/* Heap limits - only used if set */
31+
extern unsigned char *mbed_heap_start;
32+
extern uint32_t mbed_heap_size;
33+
34+
/* Stack limits */
35+
unsigned char *mbed_stack_isr_start = 0;
36+
uint32_t mbed_stack_isr_size = 0;
37+
3038
/* mbed_main is a function that is called before main()
3139
* mbed_sdk_init() is also a function that is called before main(), but unlike
3240
* mbed_main(), it is not meant for user code, but for the SDK itself to perform
@@ -67,62 +75,111 @@ void mbed_copy_nvic(void)
6775
#endif /* !defined(__CORTEX_M0) && !defined(__CORTEX_A9) */
6876
}
6977

78+
void mbed_init(void)
79+
{
80+
mbed_copy_nvic();
81+
mbed_sdk_init();
82+
#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
83+
us_ticker_init();
84+
#endif
85+
}
86+
7087
/* Toolchain specific main code */
7188

7289
#if defined (__ARMCC_VERSION)
7390

74-
int $Super$$main(void);
91+
void $Sub$$__cpp_initialize__aeabi_(void);
92+
void $Super$$__cpp_initialize__aeabi_(void);
93+
void $Sub$$__cpp_initialize__aeabi_(void)
94+
{
95+
/* This should invoke C++ initializers but we keep
96+
* this empty and invoke them in mbed_toolchain_init.
97+
*/
98+
}
7599

76-
int $Sub$$main(void)
100+
void mbed_toolchain_init()
77101
{
78-
mbed_main();
79-
mbed_error_initialize();
80-
return $Super$$main();
102+
/* Run the C++ global object constructors */
103+
$Super$$__cpp_initialize__aeabi_();
81104
}
82105

83106
void _platform_post_stackheap_init(void)
84107
{
85-
mbed_copy_nvic();
86-
mbed_sdk_init();
87-
#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
88-
us_ticker_init();
108+
mbed_init();
109+
}
110+
111+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
112+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
113+
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Base[];
114+
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Length[];
115+
116+
int $Super$$main(void);
117+
int $Sub$$main(void)
118+
{
119+
mbed_stack_isr_start = (unsigned char *) Image$$ARM_LIB_STACK$$ZI$$Base;
120+
mbed_stack_isr_size = (uint32_t) Image$$ARM_LIB_STACK$$ZI$$Length;
121+
mbed_heap_start = (unsigned char *) Image$$ARM_LIB_HEAP$$ZI$$Base;
122+
mbed_heap_size = (uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Length;
123+
124+
#if defined(__MICROLIB)
125+
// post stack/heap is not active in microlib
126+
// so call the function explicitly.
127+
_platform_post_stackheap_init();
89128
#endif
129+
mbed_toolchain_init();
130+
mbed_main();
131+
return $Super$$main();
90132
}
133+
134+
135+
136+
91137
//Define an empty os_cb_sections to remove a RTX warning when building with no RTOS due
92138
//to the --keep=os_cb_sections linker option
93139
const uint32_t os_cb_sections[] __attribute__((section(".rodata"))) = {};
94140

95141
#elif defined (__GNUC__)
96142

143+
extern uint32_t __StackLimit;
144+
extern uint32_t __StackTop;
145+
extern uint32_t __end__;
146+
extern uint32_t __HeapLimit;
147+
97148
extern int __real_main(void);
98149

99150
void software_init_hook(void)
100151
{
101-
mbed_copy_nvic();
102-
mbed_sdk_init();
103-
#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
104-
us_ticker_init();
105-
#endif
152+
mbed_stack_isr_start = (unsigned char *) &__StackLimit;
153+
mbed_stack_isr_size = (uint32_t) &__StackTop - (uint32_t) &__StackLimit;
154+
mbed_heap_start = (unsigned char *) &__end__;
155+
mbed_heap_size = (uint32_t) &__HeapLimit - (uint32_t) &__end__;
156+
157+
mbed_init();
106158
software_init_hook_rtos();
107159
}
108160

109161

110162
int __wrap_main(void)
111163
{
112164
mbed_main();
113-
mbed_error_initialize();
114165
return __real_main();
115166
}
116167

117168
#elif defined (__ICCARM__)
118169

119-
int __low_level_init(void)
170+
/* Defined by linker script */
171+
#pragma section="CSTACK"
172+
#pragma section="HEAP"
173+
174+
void __mbed_init(void)
120175
{
121-
mbed_copy_nvic();
122-
#if DEVICE_USTICKER && MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
123-
us_ticker_init();
124-
#endif
125-
return 1;
176+
mbed_heap_start = (unsigned char *)__section_begin("HEAP");
177+
mbed_heap_size = (uint32_t)__section_size("HEAP");
178+
179+
mbed_stack_isr_start = (unsigned char *)__section_begin("CSTACK");
180+
mbed_stack_isr_size = (uint32_t)__section_size("CSTACK");
181+
182+
mbed_init();
126183
}
127184

128185
#endif

0 commit comments

Comments
 (0)