|
| 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>© 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 <stdbool.h> |
| 37 | +#include <errno.h> |
| 38 | +#include "stm32l4xx.h" |
| 39 | +extern uint32_t __mbed_sbrk_start; |
| 40 | +extern uint32_t __mbed_krbs_start; |
| 41 | +extern uint32_t __mbed_sbrk_start_0; |
| 42 | +extern uint32_t __mbed_krbs_start_0; |
| 43 | + |
| 44 | +/** |
| 45 | + * The default implementation of _sbrk() (in platform/mbed_retarget.cpp) for GCC_ARM only supports one region. |
| 46 | + * This implementation supports two regions by continuing the allocation to the second region after the first |
| 47 | + * one is full. |
| 48 | + * Define __wrap__sbrk() to override the default _sbrk(). It is expected to get called through gcc |
| 49 | + * hooking mechanism ('-Wl,--wrap,_sbrk') or in _sbrk(). |
| 50 | + */ |
| 51 | +void *__wrap__sbrk(int incr) |
| 52 | +{ |
| 53 | + static uint32_t heap_ind = (uint32_t) &__mbed_sbrk_start_0; |
| 54 | + static bool once = true; |
| 55 | + uint32_t heap_ind_old = heap_ind; |
| 56 | + uint32_t heap_ind_new = heap_ind_old + incr; |
| 57 | + |
| 58 | + /** |
| 59 | + * If the new address is outside the first region, start allocating from the second region. |
| 60 | + */ |
| 61 | + if (once && (heap_ind_new >= (uint32_t)&__mbed_krbs_start_0)) { |
| 62 | + once = false; |
| 63 | + heap_ind_old = (uint32_t)&__mbed_sbrk_start; |
| 64 | + heap_ind_new = heap_ind_old + incr; |
| 65 | + /** |
| 66 | + * If the new address is outside the second region, return out-of-memory. |
| 67 | + */ |
| 68 | + } else if (heap_ind_new >= (uint32_t)&__mbed_krbs_start) { |
| 69 | + errno = ENOMEM; |
| 70 | + return (void *) - 1; |
| 71 | + } |
| 72 | + |
| 73 | + heap_ind = heap_ind_new; |
| 74 | + |
| 75 | + return (void *) heap_ind_old; |
| 76 | +} |
| 77 | +#endif /* GCC_ARM toolchain && TWO_RAM_REGIONS*/ |
| 78 | + |
0 commit comments