check.c 5.4 KB

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