diff --git a/targets/TARGET_NUVOTON/TARGET_M451/device/TOOLCHAIN_GCC_ARM/m451_retarget.c b/targets/TARGET_NUVOTON/TARGET_M451/device/TOOLCHAIN_GCC_ARM/m451_retarget.c index 470af43..c45bd82 100644 --- a/targets/TARGET_NUVOTON/TARGET_M451/device/TOOLCHAIN_GCC_ARM/m451_retarget.c +++ b/targets/TARGET_NUVOTON/TARGET_M451/device/TOOLCHAIN_GCC_ARM/m451_retarget.c @@ -11,10 +11,13 @@ #include "M451Series.h" #include +#include "nu_miscutil.h" extern uint32_t __mbed_sbrk_start; extern uint32_t __mbed_krbs_start; +#define NU_HEAP_ALIGN 32 + /** * The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't * fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to @@ -23,8 +26,8 @@ void *__wrap__sbrk(int incr) { static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start; - uint32_t heap_ind_old = heap_ind; - uint32_t heap_ind_new = (heap_ind_old + incr + 7) & ~7; + uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN); + uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN); if (heap_ind_new > &__mbed_krbs_start) { errno = ENOMEM; diff --git a/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/nuc472_retarget.c b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/nuc472_retarget.c index d5c3d87..273b3df 100644 --- a/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/nuc472_retarget.c +++ b/targets/TARGET_NUVOTON/TARGET_NUC472/device/TOOLCHAIN_GCC_ARM/nuc472_retarget.c @@ -11,10 +11,13 @@ #include "NUC472_442.h" #include +#include "nu_miscutil.h" extern uint32_t __mbed_sbrk_start; extern uint32_t __mbed_krbs_start; +#define NU_HEAP_ALIGN 32 + /** * The default implementation of _sbrk() (in common/retarget.cpp) for GCC_ARM requires one-region model (heap and stack share one region), which doesn't * fit two-region model (heap and stack are two distinct regions), for example, NUMAKER-PFM-NUC472 locates heap on external SRAM. Define __wrap__sbrk() to @@ -23,8 +26,8 @@ void *__wrap__sbrk(int incr) { static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start; - uint32_t heap_ind_old = heap_ind; - uint32_t heap_ind_new = (heap_ind_old + incr + 7) & ~7; + uint32_t heap_ind_old = NU_ALIGN_UP(heap_ind, NU_HEAP_ALIGN); + uint32_t heap_ind_new = NU_ALIGN_UP(heap_ind_old + incr, NU_HEAP_ALIGN); if (heap_ind_new > &__mbed_krbs_start) { errno = ENOMEM; diff --git a/targets/TARGET_NUVOTON/nu_miscutil.h b/targets/TARGET_NUVOTON/nu_miscutil.h index 5d06724..fd1cc9e 100644 --- a/targets/TARGET_NUVOTON/nu_miscutil.h +++ b/targets/TARGET_NUVOTON/nu_miscutil.h @@ -24,6 +24,8 @@ #define NU_MAX(a,b) ((a)>(b)?(a):(b)) #define NU_MIN(a,b) ((a)<(b)?(a):(b)) #define NU_CLAMP(x, min, max) NU_MIN(NU_MAX((x), (min)), (max)) +#define NU_ALIGN_DOWN(X, ALIGN) ((X) & ~((ALIGN) - 1)) +#define NU_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1)) void nu_nop(uint32_t n);