check.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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[13] = {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. "str lr, [r12, #48] \n" \
  22. : \
  23. : \
  24. : "memory");
  25. /** Restore registers and halt MCU
  26. *
  27. * - Always use it with GET_MESSAGE_AND_STORE_REGISTERS
  28. * - If debugger is(was) connected this routine will raise bkpt
  29. * - If debugger is not connected then endless loop
  30. *
  31. */
  32. #define RESTORE_REGISTERS_AND_HALT_MCU(debug) \
  33. register const bool r0 asm("r0") = debug; \
  34. asm volatile("cbnz r0, with_debugger%= \n" \
  35. "ldr r12, =__furi_check_registers\n" \
  36. "ldm r12, {r0-r11} \n" \
  37. "loop%=: \n" \
  38. "wfi \n" \
  39. "b loop%= \n" \
  40. "with_debugger%=: \n" \
  41. "ldr r12, =__furi_check_registers\n" \
  42. "ldm r12, {r0-r11} \n" \
  43. "debug_loop%=: \n" \
  44. "bkpt 0x00 \n" \
  45. "wfi \n" \
  46. "b debug_loop%= \n" \
  47. : \
  48. : "r"(r0) \
  49. : "memory");
  50. extern size_t xPortGetTotalHeapSize(void);
  51. extern size_t xPortGetFreeHeapSize(void);
  52. extern size_t xPortGetMinimumEverFreeHeapSize(void);
  53. static void __furi_put_uint32_as_text(uint32_t data) {
  54. char tmp_str[] = "-2147483648";
  55. itoa(data, tmp_str, 10);
  56. furi_hal_console_puts(tmp_str);
  57. }
  58. static void __furi_put_uint32_as_hex(uint32_t data) {
  59. char tmp_str[] = "0xFFFFFFFF";
  60. itoa(data, tmp_str, 16);
  61. furi_hal_console_puts(tmp_str);
  62. }
  63. static void __furi_print_register_info() {
  64. // Print registers
  65. for(uint8_t i = 0; i < 12; i++) {
  66. furi_hal_console_puts("\r\n\tr");
  67. __furi_put_uint32_as_text(i);
  68. furi_hal_console_puts(" : ");
  69. __furi_put_uint32_as_hex(__furi_check_registers[i]);
  70. }
  71. furi_hal_console_puts("\r\n\tlr : ");
  72. __furi_put_uint32_as_hex(__furi_check_registers[12]);
  73. }
  74. static void __furi_print_stack_info() {
  75. furi_hal_console_puts("\r\n\tstack watermark: ");
  76. __furi_put_uint32_as_text(uxTaskGetStackHighWaterMark(NULL) * 4);
  77. }
  78. static void __furi_print_heap_info() {
  79. furi_hal_console_puts("\r\n\t heap total: ");
  80. __furi_put_uint32_as_text(xPortGetTotalHeapSize());
  81. furi_hal_console_puts("\r\n\t heap free: ");
  82. __furi_put_uint32_as_text(xPortGetFreeHeapSize());
  83. furi_hal_console_puts("\r\n\t heap watermark: ");
  84. __furi_put_uint32_as_text(xPortGetMinimumEverFreeHeapSize());
  85. }
  86. static void __furi_print_name(bool isr) {
  87. if(isr) {
  88. furi_hal_console_puts("[ISR ");
  89. __furi_put_uint32_as_text(__get_IPSR());
  90. furi_hal_console_puts("] ");
  91. } else {
  92. const char* name = pcTaskGetName(NULL);
  93. if(name == NULL) {
  94. furi_hal_console_puts("[main] ");
  95. } else {
  96. furi_hal_console_puts("[");
  97. furi_hal_console_puts(name);
  98. furi_hal_console_puts("] ");
  99. }
  100. }
  101. }
  102. FURI_NORETURN void __furi_crash() {
  103. __disable_irq();
  104. GET_MESSAGE_AND_STORE_REGISTERS();
  105. bool isr = FURI_IS_IRQ_MODE();
  106. if(__furi_check_message == NULL) {
  107. __furi_check_message = "Fatal Error";
  108. } else if(__furi_check_message == (void*)__FURI_ASSERT_MESSAGE_FLAG) {
  109. __furi_check_message = "furi_assert failed";
  110. } else if(__furi_check_message == (void*)__FURI_CHECK_MESSAGE_FLAG) {
  111. __furi_check_message = "furi_check failed";
  112. }
  113. furi_hal_console_puts("\r\n\033[0;31m[CRASH]");
  114. __furi_print_name(isr);
  115. furi_hal_console_puts(__furi_check_message);
  116. __furi_print_register_info();
  117. if(!isr) {
  118. __furi_print_stack_info();
  119. }
  120. __furi_print_heap_info();
  121. #ifndef FURI_DEBUG
  122. // Check if debug enabled by DAP
  123. // 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
  124. bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
  125. if(debug) {
  126. #endif
  127. furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
  128. furi_hal_console_puts("\033[0m\r\n");
  129. furi_hal_debug_enable();
  130. RESTORE_REGISTERS_AND_HALT_MCU(true);
  131. #ifndef FURI_DEBUG
  132. } else {
  133. furi_hal_rtc_set_fault_data((uint32_t)__furi_check_message);
  134. furi_hal_console_puts("\r\nRebooting system.\r\n");
  135. furi_hal_console_puts("\033[0m\r\n");
  136. furi_hal_power_reset();
  137. }
  138. #endif
  139. __builtin_unreachable();
  140. }
  141. FURI_NORETURN void __furi_halt() {
  142. __disable_irq();
  143. GET_MESSAGE_AND_STORE_REGISTERS();
  144. bool isr = FURI_IS_IRQ_MODE();
  145. if(__furi_check_message == NULL) {
  146. __furi_check_message = "System halt requested.";
  147. }
  148. furi_hal_console_puts("\r\n\033[0;31m[HALT]");
  149. __furi_print_name(isr);
  150. furi_hal_console_puts(__furi_check_message);
  151. furi_hal_console_puts("\r\nSystem halted. Bye-bye!\r\n");
  152. furi_hal_console_puts("\033[0m\r\n");
  153. // Check if debug enabled by DAP
  154. // 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
  155. bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
  156. RESTORE_REGISTERS_AND_HALT_MCU(debug);
  157. __builtin_unreachable();
  158. }