diff --git a/hal/common/retarget.cpp b/hal/common/retarget.cpp index 21c6a7023b..29f5d05f4c 100644 --- a/hal/common/retarget.cpp +++ b/hal/common/retarget.cpp @@ -469,8 +469,16 @@ extern "C" uint32_t __HeapLimit; #undef errno extern "C" int errno; -// For ARM7 only -register unsigned char * stack_ptr __asm ("sp"); +// Stack pointer handling +#ifdef __ICCARM__ +#define __current_sp() __get_SP() +#else +static inline unsigned int __current_sp(void) +{ + register unsigned sp asm("sp"); + return sp; +} +#endif /* __ICCARM__ */ // Dynamic memory allocation related syscall. extern "C" caddr_t _sbrk(int incr) { @@ -478,12 +486,10 @@ extern "C" caddr_t _sbrk(int incr) { unsigned char* prev_heap = heap; unsigned char* new_heap = heap + incr; -#if defined(TARGET_ARM7) - if (new_heap >= stack_ptr) { -#elif defined(TARGET_CORTEX_A) +#if defined(TARGET_CORTEX_A) if (new_heap >= (unsigned char*)&__HeapLimit) { /* __HeapLimit is end of heap section */ #else - if (new_heap >= (unsigned char*)__get_MSP()) { + if (new_heap >= (unsigned char*)__current_sp()) { #endif errno = ENOMEM; return (caddr_t)-1;