FreeStack.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 2011-2021 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef FreeStack_h
  26. #define FreeStack_h
  27. /**
  28. * \file
  29. * \brief FreeStack() function.
  30. */
  31. #include <stdint.h>
  32. #if defined(__AVR__) || defined(DOXYGEN)
  33. #include <avr/io.h>
  34. /** Indicate FillStack() and UnusedStack() are available. */
  35. #define HAS_UNUSED_STACK 1
  36. /** boundary between stack and heap. */
  37. extern char *__brkval;
  38. /** End of bss section.*/
  39. extern char __bss_end;
  40. /** Amount of free stack space.
  41. * \return The number of free bytes.
  42. */
  43. inline int FreeStack() {
  44. char* sp = reinterpret_cast<char*>(SP);
  45. return __brkval ? sp - __brkval : sp - &__bss_end;
  46. }
  47. #elif defined(ARDUINO_ARCH_APOLLO3)
  48. #define HAS_UNUSED_STACK 0
  49. #elif defined(PLATFORM_ID) // Particle board
  50. #include "Arduino.h"
  51. inline int FreeStack() {
  52. return System.freeMemory();
  53. }
  54. #elif defined(__IMXRT1062__)
  55. #define HAS_UNUSED_STACK 1
  56. extern uint8_t _ebss;
  57. inline int FreeStack() {
  58. register uint32_t sp asm("sp");
  59. return reinterpret_cast<char*>(sp) - reinterpret_cast<char*>(&_ebss);
  60. }
  61. #elif defined(__arm__)
  62. #define HAS_UNUSED_STACK 1
  63. extern "C" char* sbrk(int incr);
  64. inline int FreeStack() {
  65. register uint32_t sp asm("sp");
  66. return reinterpret_cast<char*>(sp) - reinterpret_cast<char*>(sbrk(0));
  67. }
  68. #else // defined(__AVR__) || defined(DOXYGEN)
  69. #ifndef FREE_STACK_CPP
  70. #warning FreeStack is not defined for this system.
  71. #endif // FREE_STACK_CPP
  72. inline int FreeStack() {
  73. return 0;
  74. }
  75. #endif // defined(__AVR__) || defined(DOXYGEN)
  76. #if defined(HAS_UNUSED_STACK) || defined(DOXYGEN)
  77. /** Fill stack with 0x55 pattern */
  78. void FillStack();
  79. /**
  80. * Determine the amount of unused stack.
  81. *
  82. * FillStack() must be called to fill the stack with a 0x55 pattern.
  83. *
  84. * UnusedStack() may fail if malloc() or new is use.
  85. *
  86. * \return number of bytes with 0x55 pattern.
  87. */
  88. int UnusedStack();
  89. #else // HAS_UNUSED_STACK
  90. #define HAS_UNUSED_STACK 0
  91. inline void FillStack() {}
  92. inline int UnusedStack() {return 0;}
  93. #endif // defined(HAS_UNUSED_STACK)
  94. #endif // FreeStack_h