nrf_stack_info.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * Copyright (c) 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup nrf_stack_info Stack info functions and definitions
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Functions and definitions used to obtain information about the state of the stack.
  47. */
  48. #ifndef NRF_STACK_INFO_H__
  49. #define NRF_STACK_INFO_H__
  50. #include <stddef.h>
  51. #include <stdbool.h>
  52. #include "compiler_abstraction.h"
  53. #include "app_util.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /**
  58. * @brief Top (highest) stack address.
  59. */
  60. #define NRF_STACK_INFO_TOP ((uint32_t)STACK_TOP)
  61. /**
  62. * @brief Base (lowest) stack address.
  63. */
  64. #define NRF_STACK_INFO_BASE ((uint32_t)STACK_BASE)
  65. /**
  66. * @brief Function to get the current stack pointer value.
  67. *
  68. * @return Current stack pointer value.
  69. */
  70. #define NRF_STACK_INFO_GET_SP() ((uint32_t)GET_SP())
  71. __STATIC_INLINE size_t nrf_stack_info_get_available(void);
  72. __STATIC_INLINE size_t nrf_stack_info_get_depth(void);
  73. __STATIC_INLINE bool nrf_stack_info_overflowed(void);
  74. __STATIC_INLINE bool nrf_stack_info_is_on_stack(void const * const p_address);
  75. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  76. /**
  77. * @brief Calculate the available (free) space on the stack.
  78. *
  79. * @return Number of available bytes on the stack.
  80. */
  81. __STATIC_INLINE size_t nrf_stack_info_get_available(void)
  82. {
  83. uint32_t sp = NRF_STACK_INFO_GET_SP();
  84. if (sp > NRF_STACK_INFO_BASE)
  85. {
  86. return (size_t)(sp - NRF_STACK_INFO_BASE);
  87. }
  88. // Stack overflow
  89. return 0;
  90. }
  91. /**
  92. * @brief Calculate the current stack depth (occupied space).
  93. *
  94. * @return Current stack depth in bytes.
  95. */
  96. __STATIC_INLINE size_t nrf_stack_info_get_depth(void)
  97. {
  98. return (size_t)(NRF_STACK_INFO_TOP - NRF_STACK_INFO_GET_SP());
  99. }
  100. /**
  101. * @brief Function for checking if the stack is currently overflowed.
  102. *
  103. * @details This function checks if the stack is currently in an overflowed by comparing the stack
  104. * pointer with the stack base address. Intended to be used to be used to ease debugging.
  105. *
  106. * @return true if stack is currently overflowed, false otherwise.
  107. */
  108. __STATIC_INLINE bool nrf_stack_info_overflowed(void)
  109. {
  110. if (NRF_STACK_INFO_GET_SP() < NRF_STACK_INFO_BASE)
  111. {
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * @brief Function for checking if provided address is located in stack space.
  118. *
  119. * @param[in] p_address Address to be checked.
  120. *
  121. * @return true if address is in stack space, false otherwise.
  122. */
  123. __STATIC_INLINE bool nrf_stack_info_is_on_stack(void const * const p_address)
  124. {
  125. if (((uint32_t)p_address >= NRF_STACK_INFO_BASE) && ((uint32_t)p_address < NRF_STACK_INFO_TOP))
  126. {
  127. return true;
  128. }
  129. return false;
  130. }
  131. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  132. #ifdef __cplusplus
  133. }
  134. #endif
  135. #endif // NRF_STACK_INFO_H__
  136. /**@} */