app_error.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * Copyright (c) 2014 - 2019, 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.
  47. */
  48. #include "nrf.h"
  49. #include <stdio.h>
  50. #include "app_error.h"
  51. #include "nordic_common.h"
  52. #include "sdk_errors.h"
  53. /**@brief Function for error handling, which is called when an error has occurred.
  54. *
  55. * @warning This handler is an example only and does not fit a final product. You need to analyze
  56. * how your product is supposed to react in case of error.
  57. *
  58. * @param[in] error_code Error code supplied to the handler.
  59. * @param[in] line_num Line number where the handler is called.
  60. * @param[in] p_file_name Pointer to the file name.
  61. */
  62. void app_error_handler_bare(ret_code_t error_code)
  63. {
  64. error_info_t error_info =
  65. {
  66. .line_num = 0,
  67. .p_file_name = NULL,
  68. .err_code = error_code,
  69. };
  70. app_error_fault_handler(NRF_FAULT_ID_SDK_ERROR, 0, (uint32_t)(&error_info));
  71. UNUSED_VARIABLE(error_info);
  72. }
  73. void app_error_save_and_stop(uint32_t id, uint32_t pc, uint32_t info)
  74. {
  75. /* static error variables - in order to prevent removal by optimizers */
  76. static volatile struct
  77. {
  78. uint32_t fault_id;
  79. uint32_t pc;
  80. uint32_t error_info;
  81. assert_info_t * p_assert_info;
  82. error_info_t * p_error_info;
  83. ret_code_t err_code;
  84. uint32_t line_num;
  85. const uint8_t * p_file_name;
  86. } m_error_data = {0};
  87. // The following variable helps Keil keep the call stack visible, in addition, it can be set to
  88. // 0 in the debugger to continue executing code after the error check.
  89. volatile bool loop = true;
  90. UNUSED_VARIABLE(loop);
  91. m_error_data.fault_id = id;
  92. m_error_data.pc = pc;
  93. m_error_data.error_info = info;
  94. switch (id)
  95. {
  96. case NRF_FAULT_ID_SDK_ASSERT:
  97. m_error_data.p_assert_info = (assert_info_t *)info;
  98. m_error_data.line_num = m_error_data.p_assert_info->line_num;
  99. m_error_data.p_file_name = m_error_data.p_assert_info->p_file_name;
  100. break;
  101. case NRF_FAULT_ID_SDK_ERROR:
  102. m_error_data.p_error_info = (error_info_t *)info;
  103. m_error_data.err_code = m_error_data.p_error_info->err_code;
  104. m_error_data.line_num = m_error_data.p_error_info->line_num;
  105. m_error_data.p_file_name = m_error_data.p_error_info->p_file_name;
  106. break;
  107. }
  108. UNUSED_VARIABLE(m_error_data);
  109. // If printing is disrupted, remove the irq calls, or set the loop variable to 0 in the debugger.
  110. __disable_irq();
  111. while (loop);
  112. __enable_irq();
  113. }