sys_debug.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  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. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef SYS_DEBUG_H_INCLUDED
  43. #define SYS_DEBUG_H_INCLUDED
  44. #include "hal_debug_interface.h"
  45. #include "hal_trace_interface.h"
  46. #include <stdbool.h>
  47. #include <stdarg.h>
  48. /* This header file contains macros for debugging. */
  49. #ifndef __FILENAME__
  50. #define __FILENAME__ __FILE__
  51. #endif // __FILENAME__
  52. #ifndef ASSERT
  53. #ifdef CONFIG_DEBUG
  54. #define ASSERT(CONDITION_STATEMENT) \
  55. do \
  56. { \
  57. bool LOCAL_CONDITION_CHECK = (CONDITION_STATEMENT); \
  58. if (LOCAL_CONDITION_CHECK != true) \
  59. { \
  60. sys_assert_handler((#CONDITION_STATEMENT), __LINE__, __FILENAME__); \
  61. } \
  62. } while (0)
  63. #else
  64. #define ASSERT(CONDITION_STATEMENT)
  65. #endif // CONFIG_DEBUG
  66. #endif // ASSERT
  67. #ifndef ASSERT_INFO
  68. #ifdef CONFIG_DEBUG
  69. #define ASSERT_INFO(CONDITION_STATEMENT, INFO_FMT, ...) \
  70. do \
  71. { \
  72. bool LOCAL_CONDITION_CHECK = (CONDITION_STATEMENT); \
  73. if (LOCAL_CONDITION_CHECK != true) \
  74. { \
  75. sys_assert_info_handler((#CONDITION_STATEMENT), __LINE__, __FILENAME__, \
  76. INFO_FMT, __VA_ARGS__); \
  77. } \
  78. } while (0)
  79. #else
  80. #define ASSERT_INFO(CONDITION_STATEMENT, INFO_FMT, ...)
  81. #endif // CONFIG_DEBUG
  82. #endif // ASSERT_INFO
  83. #ifndef ASSERT_STATIC
  84. #ifdef CONFIG_DEBUG
  85. #define ASSERT_STATIC(e) do { enum {SA = 1/(e)}; } while (0)
  86. #else
  87. #define ASSERT_STATIC(e)
  88. #endif // CONFIG_DEBUG
  89. #endif // ASSERT_STATIC
  90. /**
  91. * @defgroup sys_debug Debugging macros
  92. * @ingroup sys_15_4
  93. * @{
  94. * @brief Functions used for debugging.
  95. */
  96. /**@brief System assertion fault handler.
  97. *
  98. * @details This macro should be used whenever an assertion fault is detected.
  99. *
  100. * @param[in] CONDITION_STRING Assertion condition string, which occurred to be not true.
  101. */
  102. #define SYS_ASSERT_HANDLER(CONDITION_STRING) \
  103. do \
  104. { \
  105. sys_assert_handler(CONDITION_STRING, __LINE__, __FILE__); \
  106. } while (0)
  107. #ifndef TRACE_PUTS
  108. #ifdef CONFIG_TRACE
  109. #define TRACE_PUTS(s) HAL_TRACE_INTERFACE_PUTS(s)
  110. #else
  111. #define TRACE_PUTS(s)
  112. #endif //CONFIG_TRACE
  113. #endif //TRACE_PUTS
  114. #ifndef TRACE
  115. #ifdef CONFIG_TRACE
  116. #define TRACE(INFO_FMT, ...) sys_trace_handler(INFO_FMT, __VA_ARGS__)
  117. #else
  118. #define TRACE(INFO_FMT, ...)
  119. #endif // CONFIG_DEBUG
  120. #endif // TRACE
  121. /**@brief System assertion fault handler function.
  122. *
  123. * @param[in] condition Assertion condition string, which was expected to be true.
  124. *
  125. * @param[in] line Line number.
  126. *
  127. * @param[in] file File name.
  128. */
  129. extern void sys_assert_handler(
  130. const char * condition, const int line, const char * file);
  131. /**@brief System assertion fault handler function with additional assertion information.
  132. *
  133. * @param[in] condition Assertion condition string, which was expected to be true.
  134. *
  135. * @param[in] line Line number.
  136. *
  137. * @param[in] file File name.
  138. *
  139. * @param[in] info_fmt Format string for additional assert information.
  140. *
  141. * @param[in] ... Arguments list corresponding to the format string.
  142. */
  143. extern void sys_assert_info_handler(
  144. const char * condition, const int line, const char * file,
  145. const char * info_fmt, ...);
  146. /**@brief System trace output handler function.
  147. *
  148. * @param[in] fmt Format string for trace output.
  149. *
  150. * @param[in] ... Arguments list corresponding to the format string.
  151. */
  152. extern void sys_trace_handler(const char * fmt, ...);
  153. /** @} */
  154. #endif // SYS_DEBUG_H_INCLUDED