MEMORY{ FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 256K SRAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K}SECTIONS{ .text : { KEEP(*(.isr_vector)) *(.text*) *(.rodata*) _etext = .; } > FLASH .data : AT (ADDR(.text) + SIZEOF(.text)) { _data = .; *(vtable) *(.data*) _edata = .; } > SRAM .bss : { _bss = .; *(.bss*) *(COMMON) _ebss = .; . = ALIGN (8); _end = .; } > SRAM}/* end of allocated ram _end */PROVIDE( _HEAP_START = _end );/* end of the heap -> align 8 byte */ PROVIDE ( _HEAP_END = ALIGN(ORIGIN(SRAM) + LENGTH(SRAM) - 8 ,8) );
#include // linker (standalone.ld) sets heap start and endextern unsigned int _HEAP_START;extern unsigned int _HEAP_END;static caddr_t heap = NULL;// low level bulk memory allocator - used by malloccaddr_t _sbrk ( int increment ) { caddr_t prevHeap; caddr_t nextHeap; if (heap == NULL) { // first allocation heap = (caddr_t)&_HEAP_START; } prevHeap = heap; // Always return data aligned on a 8 byte boundary nextHeap = (caddr_t)(((unsigned int)(heap + increment) + 7) & ~7); // get current stack pointer register caddr_t stackPtr asm ("sp"); // Check enough space and there is no collision with stack coming the other way // if stack is above start of heap if ( (((caddr_t)&_HEAP_START <>stackPtr) && (nextHeap > stackPtr)) || (nextHeap >= (caddr_t)&_HEAP_END)) { return NULL; // error - no more memory } else { heap = nextHeap; return (caddr_t) prevHeap; } }
PROVIDE ( _HEAP_END = ALIGN(ORIGIN(SRAM) + LENGTH(SRAM) ,8) );
... (nextHeap > (caddr_t)&_HEAP_END)){ return NULL; // error - no more memory
#ifndef SYSTEM_RAM_H_#define SYSTEM_RAM_H_#include #ifdef __cplusplusextern "C"{#endiftypedef struct dynamicRAMInfoType { UINT size; UINT startAddr; UINT endAddr;};typedef void* RAM_ADDRESS;struct dynamicRAMInfoType sizeofDynamicRAM(void);boolean validHeapAddress(RAM_ADDRESS addr);RAM_ADDRESS freeMemory(RAM_ADDRESS addr);#ifdef __cplusplus}#endif#endif // SYSTEM_RAM_H_
#include #include #include // linker (standalone.ld) sets heap start and endextern unsigned int _HEAP_START;extern unsigned int _HEAP_END;#ifdef __cplusplusextern "C"{#endifstruct dynamicRAMInfoType sizeofDynamicRAM(void) { struct dynamicRAMInfoType info; info.startAddr = (UINT)&_HEAP_START; info.endAddr = (UINT)&_HEAP_END; info.size = info.endAddr - info.startAddr; return info;}boolean validHeapAddress(RAM_ADDRESS addr) { if (((UINT)addr >= (UINT)&_HEAP_START) && ((UINT)addr <>UINT)&_HEAP_END)) { return TRUE; } else { return FALSE; }}RAM_ADDRESS freeMemory(RAM_ADDRESS addr) { free(addr); return NULL;}#ifdef __cplusplus}#endif
extern int __heap_start__;extern int __heap_end__;extern void *_sbrk(int incr){ static unsigned char *heap = NULL; unsigned char *prev_heap; if (heap == NULL) { heap = (unsigned char *)&__heap_start__; } prev_heap = heap; if ((heap + incr) >= (unsigned char *)&__heap_end__) { return 0; } heap += incr; return (void *)prev_heap;}