check.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "check.h"
  2. #include "common_defines.h"
  3. #include <stm32wbxx.h>
  4. #include <furi_hal_console.h>
  5. #include <furi_hal_power.h>
  6. #include <furi_hal_rtc.h>
  7. #include <furi_hal_debug.h>
  8. #include <stdio.h>
  9. #include <FreeRTOS.h>
  10. #include <task.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. PLACE_IN_SECTION("MB_MEM2") const char* __furi_check_message = NULL;
  14. PLACE_IN_SECTION("MB_MEM2") uint32_t __furi_check_registers[12] = {0};
  15. /** Load r12 value to __furi_check_message and store registers to __furi_check_registers */
  16. #define GET_MESSAGE_AND_STORE_REGISTERS() \
  17. asm volatile("ldr r11, =__furi_check_message \n" \
  18. "str r12, [r11] \n" \
  19. "ldr r12, =__furi_check_registers \n" \
  20. "stm r12, {r0-r11} \n" \
  21. : \
  22. : \
  23. : "memory");
  24. /** Restore registers and halt MCU
  25. *
  26. * - Always use it with GET_MESSAGE_AND_STORE_REGISTERS
  27. * - If debugger is(was) connected this routine will raise bkpt
  28. * - If debugger is not connected then endless loop
  29. *
  30. */
  31. #define RESTORE_REGISTERS_AND_HALT_MCU(debug) \
  32. register const bool r0 asm("r0") = debug; \
  33. asm volatile("cbnz r0, with_debugger%= \n" \
  34. "ldr r12, =__furi_check_registers\n" \
  35. "ldm r12, {r0-r11} \n" \
  36. "loop%=: \n" \
  37. "wfi \n" \
  38. "b loop%= \n" \
  39. "with_debugger%=: \n" \
  40. "ldr r12, =__furi_check_registers\n" \
  41. "ldm r12, {r0-r11} \n" \
  42. "debug_loop%=: \n" \
  43. "bkpt 0x00 \n" \
  44. "wfi \n" \
  45. "b debug_loop%= \n" \
  46. : \
  47. : "r"(r0) \
  48. : "memory");
  49. extern size_t xPortGetTotalHeapSize(void);
  50. extern size_t xPortGetFreeHeapSize(void);
  51. extern size_t xPortGetMinimumEverFreeHeapSize(void);
  52. static void __furi_put_uint32_as_text(uint32_t data) {
  53. char tmp_str[] = "-2147483648";
  54. itoa(data, tmp_str, 10);
  55. furi_hal_console_puts(tmp_str);
  56. }
  57. static void __furi_print_stack_info() {
  58. furi_hal_console_puts("\r\n\tstack watermark: ");
  59. __furi_put_uint32_as_text(uxTaskGetStackHighWaterMark(NULL) * 4);
  60. }
  61. static void __furi_print_heap_info() {
  62. furi_hal_console_puts("\r\n\t heap total: ");
  63. __furi_put_uint32_as_text(xPortGetTotalHeapSize());
  64. furi_hal_console_puts("\r\n\t heap free: ");
  65. __furi_put_uint32_as_text(xPortGetFreeHeapSize());
  66. furi_hal_console_puts("\r\n\t heap watermark: ");
  67. __furi_put_uint32_as_text(xPortGetMinimumEverFreeHeapSize());
  68. }
  69. static void __furi_print_name(bool isr) {
  70. if(isr) {
  71. furi_hal_console_puts("[ISR ");
  72. __furi_put_uint32_as_text(__get_IPSR());
  73. furi_hal_console_puts("] ");
  74. } else {
  75. const char* name = pcTaskGetName(NULL);
  76. if(name == NULL) {
  77. furi_hal_console_puts("[main] ");
  78. } else {
  79. furi_hal_console_puts("[");
  80. furi_hal_console_puts(name);
  81. furi_hal_console_puts("] ");
  82. }
  83. }
  84. }
  85. FURI_NORETURN void __furi_crash() {
  86. __disable_irq();
  87. GET_MESSAGE_AND_STORE_REGISTERS();
  88. bool isr = FURI_IS_IRQ_MODE();
  89. if(__furi_check_message == NULL) {
  90. __furi_check_message = "Fatal Error";
  91. }
  92. furi_hal_console_puts("\r\n\033[0;31m[CRASH]");
  93. __furi_print_name(isr);
  94. furi_hal_console_puts(__furi_check_message);
  95. if(!isr) {
  96. __furi_print_stack_info();
  97. }
  98. __furi_print_heap_info();
  99. // Check if debug enabled by DAP
  100. // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
  101. bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
  102. if(debug) {
  103. furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
  104. furi_hal_console_puts("\033[0m\r\n");
  105. furi_hal_debug_enable();
  106. RESTORE_REGISTERS_AND_HALT_MCU(true);
  107. } else {
  108. furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
  109. furi_hal_console_puts("\r\nRebooting system.\r\n");
  110. furi_hal_console_puts("\033[0m\r\n");
  111. furi_hal_power_reset();
  112. }
  113. __builtin_unreachable();
  114. }
  115. FURI_NORETURN void __furi_halt() {
  116. __disable_irq();
  117. GET_MESSAGE_AND_STORE_REGISTERS();
  118. bool isr = FURI_IS_IRQ_MODE();
  119. if(__furi_check_message == NULL) {
  120. __furi_check_message = "System halt requested.";
  121. }
  122. furi_hal_console_puts("\r\n\033[0;31m[HALT]");
  123. __furi_print_name(isr);
  124. furi_hal_console_puts(__furi_check_message);
  125. furi_hal_console_puts("\r\nSystem halted. Bye-bye!\r\n");
  126. furi_hal_console_puts("\033[0m\r\n");
  127. // Check if debug enabled by DAP
  128. // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
  129. bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
  130. RESTORE_REGISTERS_AND_HALT_MCU(debug);
  131. __builtin_unreachable();
  132. }