Skip to content

Commit db97ad2

Browse files
authored
Merge pull request stm32duino#348 from fpistm/memory
Enhance memory management
2 parents 5d30bc4 + fd5ee3a commit db97ad2

File tree

4 files changed

+31
-39
lines changed

4 files changed

+31
-39
lines changed

boards.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ Nucleo_64.menu.pnum.NUCLEO_L152RE.build.cmsis_lib_gcc=arm_cortexM3l_math
282282
Nucleo_64.menu.pnum.NUCLEO_L476RG=Nucleo L476RG
283283
Nucleo_64.menu.pnum.NUCLEO_L476RG.node=NODE_L476RG
284284
Nucleo_64.menu.pnum.NUCLEO_L476RG.upload.maximum_size=1048576
285-
Nucleo_64.menu.pnum.NUCLEO_L476RG.upload.maximum_data_size=131072
285+
Nucleo_64.menu.pnum.NUCLEO_L476RG.upload.maximum_data_size=98304
286286
Nucleo_64.menu.pnum.NUCLEO_L476RG.build.mcu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
287287
Nucleo_64.menu.pnum.NUCLEO_L476RG.build.board=NUCLEO_L476RG
288288
Nucleo_64.menu.pnum.NUCLEO_L476RG.build.series=STM32L4xx

cores/arduino/syscalls_stm32.c renamed to cores/arduino/syscalls.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
*/
77

8+
#include "stm32_def.h"
89
#if defined ( __GNUC__ ) /* GCC CS3 */
910
#include <sys/stat.h>
1011
#endif
@@ -25,23 +26,24 @@ extern size_t uart_debug_write(uint8_t *data, uint32_t size);
2526
#define UNUSED(x) x ## _UNUSED
2627
#endif
2728

28-
register char * stack_ptr asm("sp");
29-
29+
__attribute__((weak))
3030
caddr_t _sbrk( int incr ) {
31+
extern char _estack; /* Defined in the linker script */
32+
extern char _Min_Stack_Size; /* Defined in the linker script */
3133
extern char _end; /* Defined by the linker */
32-
static char *heap_end = NULL ;
33-
char *prev_heap_end ;
34-
35-
if ( heap_end == NULL ) {
36-
heap_end = &_end ;
37-
}
38-
prev_heap_end = heap_end;
34+
static char *heap_end = &_end ;
35+
char *prev_heap_end = heap_end;
3936

40-
if (heap_end + incr > stack_ptr) {
37+
if (heap_end + incr > (char *)__get_MSP()) {
4138
/* Heap and stack collision */
4239
errno = ENOMEM;
4340
return (caddr_t) -1;
4441
}
42+
/* Ensure to keep minimun stack size defined in the linker script */
43+
if (heap_end + incr >= (char*)(&_estack - &_Min_Stack_Size)) {
44+
errno = ENOMEM;
45+
return (caddr_t) -1;
46+
}
4547

4648
heap_end += incr ;
4749
return (caddr_t) prev_heap_end ;

libraries/Wire/src/Wire.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,13 @@ void TwoWire::allocateRxBuffer(size_t length)
435435
if(rxBufferAllocated < length) {
436436
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
437437
if(length < BUFFER_LENGTH) { length = BUFFER_LENGTH; }
438-
rxBuffer = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
439-
rxBufferAllocated = (rxBuffer != nullptr) ? length: 0;
438+
uint8_t *tmp = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
439+
if(tmp != nullptr) {
440+
rxBuffer = tmp;
441+
rxBufferAllocated = length;
442+
} else {
443+
_Error_Handler("No enough memory! (%i)\n", length);
444+
}
440445
}
441446
}
442447

@@ -445,8 +450,13 @@ inline void TwoWire::allocateTxBuffer(size_t length)
445450
if(txBufferAllocated < length) {
446451
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
447452
if(length < BUFFER_LENGTH) { length = BUFFER_LENGTH; }
448-
txBuffer = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
449-
txBufferAllocated = (txBuffer != nullptr) ? length: 0;
453+
uint8_t *tmp = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
454+
if(tmp != nullptr) {
455+
txBuffer = tmp;
456+
txBufferAllocated = length;
457+
} else {
458+
_Error_Handler("No enough memory! (%i)\n", length);
459+
}
450460
}
451461
}
452462

variants/NUCLEO_L432KC/ldscript.ld

+4-24
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@
3333
ENTRY(Reset_Handler)
3434

3535
/* Highest address of the user mode stack */
36-
_estack = 0x2000c000; /* end of RAM */
36+
_estack = 0x20010000; /* end of RAM */
3737
/* Generate a link error if heap and stack don't fit into RAM */
3838
_Min_Heap_Size = 0x200; /* required amount of heap */
3939
_Min_Stack_Size = 0x400; /* required amount of stack */
4040

4141
/* Specify the memory areas */
4242
MEMORY
4343
{
44-
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K
45-
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K
46-
SRAM2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K
44+
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
45+
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 256K
4746
}
4847

4948
/* Define output sections */
@@ -58,7 +57,7 @@ SECTIONS
5857
} >FLASH
5958

6059
/* The program code and other data goes into FLASH */
61-
.text ALIGN(8):
60+
.text :
6261
{
6362
. = ALIGN(8);
6463
*(.text) /* .text sections (code) */
@@ -140,25 +139,6 @@ SECTIONS
140139
_edata = .; /* define a global symbol at data end */
141140
} >RAM AT> FLASH
142141

143-
_sisram2 = LOADADDR(.sram2);
144-
145-
/* CCM-RAM section
146-
*
147-
* IMPORTANT NOTE!
148-
* If initialized variables will be placed in this section,
149-
* the startup code needs to be modified to copy the init-values.
150-
*/
151-
.sram2 :
152-
{
153-
. = ALIGN(8);
154-
_ssram2 = .; /* create a global symbol at sram2 start */
155-
*(.sram2)
156-
*(.sram2*)
157-
158-
. = ALIGN(8);
159-
_esram2 = .; /* create a global symbol at sram2 end */
160-
} >SRAM2 AT> FLASH
161-
162142

163143
/* Uninitialized data section */
164144
. = ALIGN(4);

0 commit comments

Comments
 (0)