Skip to content

Enhance memory management #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 16, 2018
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
2 changes: 1 addition & 1 deletion boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Nucleo_64.menu.pnum.NUCLEO_L152RE.build.cmsis_lib_gcc=arm_cortexM3l_math
Nucleo_64.menu.pnum.NUCLEO_L476RG=Nucleo L476RG
Nucleo_64.menu.pnum.NUCLEO_L476RG.node=NODE_L476RG
Nucleo_64.menu.pnum.NUCLEO_L476RG.upload.maximum_size=1048576
Nucleo_64.menu.pnum.NUCLEO_L476RG.upload.maximum_data_size=131072
Nucleo_64.menu.pnum.NUCLEO_L476RG.upload.maximum_data_size=98304
Nucleo_64.menu.pnum.NUCLEO_L476RG.build.mcu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard
Nucleo_64.menu.pnum.NUCLEO_L476RG.build.board=NUCLEO_L476RG
Nucleo_64.menu.pnum.NUCLEO_L476RG.build.series=STM32L4xx
Expand Down
22 changes: 12 additions & 10 deletions cores/arduino/syscalls_stm32.c → cores/arduino/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*
*/

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

register char * stack_ptr asm("sp");

__attribute__((weak))
caddr_t _sbrk( int incr ) {
extern char _estack; /* Defined in the linker script */
extern char _Min_Stack_Size; /* Defined in the linker script */
extern char _end; /* Defined by the linker */
static char *heap_end = NULL ;
char *prev_heap_end ;

if ( heap_end == NULL ) {
heap_end = &_end ;
}
prev_heap_end = heap_end;
static char *heap_end = &_end ;
char *prev_heap_end = heap_end;

if (heap_end + incr > stack_ptr) {
if (heap_end + incr > (char *)__get_MSP()) {
/* Heap and stack collision */
errno = ENOMEM;
return (caddr_t) -1;
}
/* Ensure to keep minimun stack size defined in the linker script */
if (heap_end + incr >= (char*)(&_estack - &_Min_Stack_Size)) {
errno = ENOMEM;
return (caddr_t) -1;
}

heap_end += incr ;
return (caddr_t) prev_heap_end ;
Expand Down
18 changes: 14 additions & 4 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,13 @@ void TwoWire::allocateRxBuffer(size_t length)
if(rxBufferAllocated < length) {
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
if(length < BUFFER_LENGTH) { length = BUFFER_LENGTH; }
rxBuffer = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
rxBufferAllocated = (rxBuffer != nullptr) ? length: 0;
uint8_t *tmp = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
if(tmp != nullptr) {
rxBuffer = tmp;
rxBufferAllocated = length;
} else {
_Error_Handler("No enough memory! (%i)\n", length);
}
}
}

Expand All @@ -445,8 +450,13 @@ inline void TwoWire::allocateTxBuffer(size_t length)
if(txBufferAllocated < length) {
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
if(length < BUFFER_LENGTH) { length = BUFFER_LENGTH; }
txBuffer = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
txBufferAllocated = (txBuffer != nullptr) ? length: 0;
uint8_t *tmp = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
if(tmp != nullptr) {
txBuffer = tmp;
txBufferAllocated = length;
} else {
_Error_Handler("No enough memory! (%i)\n", length);
}
}
}

Expand Down
28 changes: 4 additions & 24 deletions variants/NUCLEO_L432KC/ldscript.ld
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@
ENTRY(Reset_Handler)

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

/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K
SRAM2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 256K
}

/* Define output sections */
Expand All @@ -58,7 +57,7 @@ SECTIONS
} >FLASH

/* The program code and other data goes into FLASH */
.text ALIGN(8):
.text :
{
. = ALIGN(8);
*(.text) /* .text sections (code) */
Expand Down Expand Up @@ -140,25 +139,6 @@ SECTIONS
_edata = .; /* define a global symbol at data end */
} >RAM AT> FLASH

_sisram2 = LOADADDR(.sram2);

/* CCM-RAM section
*
* IMPORTANT NOTE!
* If initialized variables will be placed in this section,
* the startup code needs to be modified to copy the init-values.
*/
.sram2 :
{
. = ALIGN(8);
_ssram2 = .; /* create a global symbol at sram2 start */
*(.sram2)
*(.sram2*)

. = ALIGN(8);
_esram2 = .; /* create a global symbol at sram2 end */
} >SRAM2 AT> FLASH


/* Uninitialized data section */
. = ALIGN(4);
Expand Down