app_error.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Copyright (c) 2013 - 2020, 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 app_error Common application error handler
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Common application error handler and macros for utilizing a common error handler.
  47. */
  48. #ifndef APP_ERROR_H__
  49. #define APP_ERROR_H__
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include <stdbool.h>
  53. #include "nrf.h"
  54. #include "sdk_errors.h"
  55. #include "nordic_common.h"
  56. #include "app_error_weak.h"
  57. #ifdef ANT_STACK_SUPPORT_REQD
  58. #include "ant_error.h"
  59. #endif // ANT_STACK_SUPPORT_REQD
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #define NRF_FAULT_ID_SDK_RANGE_START (0x00004000) /**< The start of the range of error IDs defined in the SDK. */
  64. /**@defgroup APP_ERROR_FAULT_IDS Fault ID types
  65. * @{ */
  66. #define NRF_FAULT_ID_SDK_ERROR (NRF_FAULT_ID_SDK_RANGE_START + 1) /**< An error stemming from a call to @ref APP_ERROR_CHECK or @ref APP_ERROR_CHECK_BOOL. The info parameter is a pointer to an @ref error_info_t variable. */
  67. #define NRF_FAULT_ID_SDK_ASSERT (NRF_FAULT_ID_SDK_RANGE_START + 2) /**< An error stemming from a call to ASSERT (nrf_assert.h). The info parameter is a pointer to an @ref assert_info_t variable. */
  68. /**@} */
  69. /**@brief Structure containing info about an error of the type @ref NRF_FAULT_ID_SDK_ERROR.
  70. */
  71. typedef struct
  72. {
  73. uint32_t line_num; /**< The line number where the error occurred. */
  74. uint8_t const * p_file_name; /**< The file in which the error occurred. */
  75. uint32_t err_code; /**< The error code representing the error that occurred. */
  76. } error_info_t;
  77. /**@brief Structure containing info about an error of the type @ref NRF_FAULT_ID_SDK_ASSERT.
  78. */
  79. typedef struct
  80. {
  81. uint32_t line_num; /**< The line number where the error occurred. */
  82. uint8_t const * p_file_name; /**< The file in which the error occurred. */
  83. } assert_info_t;
  84. /**@brief Defines required by app_error_handler assembler intructions.
  85. */
  86. #define APP_ERROR_ERROR_INFO_OFFSET_LINE_NUM (offsetof(error_info_t, line_num))
  87. #define APP_ERROR_ERROR_INFO_OFFSET_P_FILE_NAME (offsetof(error_info_t, p_file_name))
  88. #define APP_ERROR_ERROR_INFO_OFFSET_ERR_CODE (offsetof(error_info_t, err_code))
  89. #define APP_ERROR_ERROR_INFO_SIZE (sizeof(error_info_t))
  90. #define APP_ERROR_ERROR_INFO_SIZE_ALIGNED_8BYTE \
  91. ALIGN_NUM(APP_ERROR_ERROR_INFO_SIZE, sizeof(uint64_t))
  92. /**@brief Function for error handling, which is called when an error has occurred.
  93. *
  94. * @param[in] error_code Error code supplied to the handler.
  95. * @param[in] line_num Line number where the handler is called.
  96. * @param[in] p_file_name Pointer to the file name.
  97. */
  98. void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name);
  99. /**@brief Function for error handling, which is called when an error has occurred.
  100. *
  101. * @param[in] error_code Error code supplied to the handler.
  102. */
  103. void app_error_handler_bare(ret_code_t error_code);
  104. /**@brief Function for saving the parameters and entering an eternal loop, for debug purposes.
  105. *
  106. * @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  107. * @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  108. * unavailable.
  109. * @param[in] info Optional additional information regarding the fault. Refer to each fault
  110. * identifier for details.
  111. */
  112. void app_error_save_and_stop(uint32_t id, uint32_t pc, uint32_t info);
  113. /**@brief Function for logging details of error and flushing logs.
  114. *
  115. * @param[in] id Fault identifier. See @ref NRF_FAULT_IDS.
  116. * @param[in] pc The program counter of the instruction that triggered the fault, or 0 if
  117. * unavailable.
  118. * @param[in] info Optional additional information regarding the fault. Refer to each fault
  119. * identifier for details.
  120. */
  121. void app_error_log_handle(uint32_t id, uint32_t pc, uint32_t info);
  122. /**@brief Macro for calling error handler function.
  123. *
  124. * @param[in] ERR_CODE Error code supplied to the error handler.
  125. */
  126. #ifdef DEBUG
  127. #define APP_ERROR_HANDLER(ERR_CODE) \
  128. do \
  129. { \
  130. app_error_handler((ERR_CODE), __LINE__, (uint8_t*) __FILE__); \
  131. } while (0)
  132. #else
  133. #define APP_ERROR_HANDLER(ERR_CODE) \
  134. do \
  135. { \
  136. app_error_handler_bare((ERR_CODE)); \
  137. } while (0)
  138. #endif
  139. /**@brief Macro for calling error handler function if supplied error code any other than NRF_SUCCESS.
  140. *
  141. * @param[in] ERR_CODE Error code supplied to the error handler.
  142. */
  143. #define APP_ERROR_CHECK(ERR_CODE) \
  144. do \
  145. { \
  146. const uint32_t LOCAL_ERR_CODE = (ERR_CODE); \
  147. if (LOCAL_ERR_CODE != NRF_SUCCESS) \
  148. { \
  149. APP_ERROR_HANDLER(LOCAL_ERR_CODE); \
  150. } \
  151. } while (0)
  152. /**@brief Macro for calling error handler function if supplied boolean value is false.
  153. *
  154. * @param[in] BOOLEAN_VALUE Boolean value to be evaluated.
  155. */
  156. #define APP_ERROR_CHECK_BOOL(BOOLEAN_VALUE) \
  157. do \
  158. { \
  159. const uint32_t LOCAL_BOOLEAN_VALUE = (BOOLEAN_VALUE); \
  160. if (!LOCAL_BOOLEAN_VALUE) \
  161. { \
  162. APP_ERROR_HANDLER(0); \
  163. } \
  164. } while (0)
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif // APP_ERROR_H__
  169. /** @} */