aboutsummaryrefslogtreecommitdiff
path: root/stm32f091/esp/Src/sysmem.c
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-10-11 18:34:48 +0200
committerlonkaars <loek@pipeframe.xyz>2022-10-11 18:34:48 +0200
commitacb25f74631597b2b24dca2c16da9dbb9ed2ecb1 (patch)
tree3eb6151054551a1586eeb1dbd67cda23dc2e47b2 /stm32f091/esp/Src/sysmem.c
parent1d385074e33f831e88499603c46e25170b770ada (diff)
parent8c1381da90da1d8c972ee6065dfe5d0ee5e5f1e1 (diff)
Merge branch 'QT_ESP' of https://github.com/NielsCoding/avans-whether-station into esp
Diffstat (limited to 'stm32f091/esp/Src/sysmem.c')
-rw-r--r--stm32f091/esp/Src/sysmem.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/stm32f091/esp/Src/sysmem.c b/stm32f091/esp/Src/sysmem.c
new file mode 100644
index 0000000..54081ac
--- /dev/null
+++ b/stm32f091/esp/Src/sysmem.c
@@ -0,0 +1,79 @@
+/**
+ ******************************************************************************
+ * @file sysmem.c
+ * @author Generated by STM32CubeIDE
+ * @brief STM32CubeIDE System Memory calls file
+ *
+ * For more information about which C functions
+ * need which of these lowlevel functions
+ * please consult the newlib libc manual
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2022 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Includes */
+#include <errno.h>
+#include <stdint.h>
+
+/**
+ * Pointer to the current high watermark of the heap usage
+ */
+static uint8_t *__sbrk_heap_end = NULL;
+
+/**
+ * @brief _sbrk() allocates memory to the newlib heap and is used by malloc
+ * and others from the C library
+ *
+ * @verbatim
+ * ############################################################################
+ * # .data # .bss # newlib heap # MSP stack #
+ * # # # # Reserved by _Min_Stack_Size #
+ * ############################################################################
+ * ^-- RAM start ^-- _end _estack, RAM end --^
+ * @endverbatim
+ *
+ * This implementation starts allocating at the '_end' linker symbol
+ * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack
+ * The implementation considers '_estack' linker symbol to be RAM end
+ * NOTE: If the MSP stack, at any point during execution, grows larger than the
+ * reserved size, please increase the '_Min_Stack_Size'.
+ *
+ * @param incr Memory size
+ * @return Pointer to allocated memory
+ */
+void *_sbrk(ptrdiff_t incr)
+{
+ extern uint8_t _end; /* Symbol defined in the linker script */
+ extern uint8_t _estack; /* Symbol defined in the linker script */
+ extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
+ const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
+ const uint8_t *max_heap = (uint8_t *)stack_limit;
+ uint8_t *prev_heap_end;
+
+ /* Initialize heap end at first call */
+ if (NULL == __sbrk_heap_end)
+ {
+ __sbrk_heap_end = &_end;
+ }
+
+ /* Protect heap from growing into the reserved MSP stack */
+ if (__sbrk_heap_end + incr > max_heap)
+ {
+ errno = ENOMEM;
+ return (void *)-1;
+ }
+
+ prev_heap_end = __sbrk_heap_end;
+ __sbrk_heap_end += incr;
+
+ return (void *)prev_heap_end;
+}