DIY Logging Volt/Ampmeter
sysmem.c
Go to the documentation of this file.
1 /**
2  ******************************************************************************
3  * @file sysmem.c
4  * @author Generated by STM32CubeIDE
5  * @brief STM32CubeIDE System Memory calls file
6  *
7  * For more information about which C functions
8  * need which of these lowlevel functions
9  * please consult the newlib libc manual
10  ******************************************************************************
11  * @attention
12  *
13  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
14  * All rights reserved.</center></h2>
15  *
16  * This software component is licensed by ST under BSD 3-Clause license,
17  * the "License"; You may not use this file except in compliance with the
18  * License. You may obtain a copy of the License at:
19  * opensource.org/licenses/BSD-3-Clause
20  *
21  ******************************************************************************
22  */
23 
24 /* Includes */
25 #include <errno.h>
26 #include <stdint.h>
27 
28 /**
29  * Pointer to the current high watermark of the heap usage
30  */
31 static uint8_t *__sbrk_heap_end = NULL;
32 
33 /**
34  * @brief _sbrk() allocates memory to the newlib heap and is used by malloc
35  * and others from the C library
36  *
37  * @verbatim
38  * ############################################################################
39  * # .data # .bss # newlib heap # MSP stack #
40  * # # # # Reserved by _Min_Stack_Size #
41  * ############################################################################
42  * ^-- RAM start ^-- _end _estack, RAM end --^
43  * @endverbatim
44  *
45  * This implementation starts allocating at the '_end' linker symbol
46  * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
47  * The implementation considers '_estack' linker symbol to be RAM end
48  * NOTE: If the MSP stack, at any point during execution, grows larger than the
49  * reserved size, please increase the '_Min_Stack_Size'.
50  *
51  * @param incr Memory size
52  * @return Pointer to allocated memory
53  */
54 void *_sbrk(ptrdiff_t incr)
55 {
56  extern uint8_t _end; /* Symbol defined in the linker script */
57  extern uint8_t _estack; /* Symbol defined in the linker script */
58  extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
59  const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
60  const uint8_t *max_heap = (uint8_t *)stack_limit;
61  uint8_t *prev_heap_end;
62 
63  /* Initialize heap end at first call */
64  if (NULL == __sbrk_heap_end)
65  {
66  __sbrk_heap_end = &_end;
67  }
68 
69  /* Protect heap from growing into the reserved MSP stack */
70  if (__sbrk_heap_end + incr > max_heap)
71  {
72  errno = ENOMEM;
73  return (void *)-1;
74  }
75 
76  prev_heap_end = __sbrk_heap_end;
77  __sbrk_heap_end += incr;
78 
79  return (void *)prev_heap_end;
80 }
__sbrk_heap_end
static uint8_t * __sbrk_heap_end
Definition: sysmem.c:31
errno
int errno
_sbrk
void * _sbrk(ptrdiff_t incr)
_sbrk() allocates memory to the newlib heap and is used by malloc and others from the C library
Definition: sysmem.c:54
NULL
#define NULL
Definition: SEGGER_RTT.c:177