l4_retarget.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. ******************************************************************************
  3. * @file l4_retarget.c
  4. * @author MCD Application Team
  5. * @brief CMSIS Cortex-M4 Core Peripheral Access Layer Source File for STM32L475xG
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2018 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if (defined(TWO_RAM_REGIONS) && defined(__GNUC__) && !defined(__CC_ARM) && !defined(__ARMCC_VERSION))
  36. #include <errno.h>
  37. #include "stm32l4xx.h"
  38. extern uint32_t __mbed_sbrk_start;
  39. extern uint32_t __mbed_krbs_start;
  40. #define STM32L4_HEAP_ALIGN 32
  41. #define STM32L4_ALIGN_UP(X, ALIGN) (((X) + (ALIGN) - 1) & ~((ALIGN) - 1))
  42. /**
  43. * The default implementation of _sbrk() (in platform/mbed_retarget.cpp) for GCC_ARM requires one-region model (heap and
  44. * stack share one region), which doesn't fit two-region model (heap and stack are two distinct regions), for example,
  45. * STM32L475xG locates heap on SRAM1 and stack on SRAM2.
  46. * Define __wrap__sbrk() to override the default _sbrk(). It is expected to get called through gcc
  47. * hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk().
  48. */
  49. void *__wrap__sbrk(int incr)
  50. {
  51. static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start;
  52. uint32_t heap_ind_old = STM32L4_ALIGN_UP(heap_ind, STM32L4_HEAP_ALIGN);
  53. uint32_t heap_ind_new = STM32L4_ALIGN_UP(heap_ind_old + incr, STM32L4_HEAP_ALIGN);
  54. if (heap_ind_new > &__mbed_krbs_start) {
  55. errno = ENOMEM;
  56. return (void *) - 1;
  57. }
  58. heap_ind = heap_ind_new;
  59. return (void *) heap_ind_old;
  60. }
  61. #endif /* GCC_ARM toolchain && TWO_RAM_REGIONS*/