check.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "check.h"
  2. #include "common_defines.h"
  3. #include <furi_hal_console.h>
  4. #include <furi_hal_power.h>
  5. #include <furi_hal_rtc.h>
  6. #include <stdio.h>
  7. #include <FreeRTOS.h>
  8. #include <task.h>
  9. #include <stdio.h>
  10. extern size_t xPortGetTotalHeapSize(void);
  11. extern size_t xPortGetFreeHeapSize(void);
  12. extern size_t xPortGetMinimumEverFreeHeapSize(void);
  13. static void __furi_put_uint32_as_text(uint32_t data) {
  14. char tmp_str[] = "-2147483648";
  15. itoa(data, tmp_str, 10);
  16. furi_hal_console_puts(tmp_str);
  17. }
  18. static void __furi_print_stack_info() {
  19. furi_hal_console_puts("\r\n\tstack watermark: ");
  20. __furi_put_uint32_as_text(uxTaskGetStackHighWaterMark(NULL) * 4);
  21. }
  22. static void __furi_print_heap_info() {
  23. furi_hal_console_puts("\r\n\t heap total: ");
  24. __furi_put_uint32_as_text(xPortGetTotalHeapSize());
  25. furi_hal_console_puts("\r\n\t heap free: ");
  26. __furi_put_uint32_as_text(xPortGetFreeHeapSize());
  27. furi_hal_console_puts("\r\n\t heap watermark: ");
  28. __furi_put_uint32_as_text(xPortGetMinimumEverFreeHeapSize());
  29. }
  30. static void __furi_print_name(bool isr) {
  31. if(isr) {
  32. furi_hal_console_puts("[ISR ");
  33. __furi_put_uint32_as_text(__get_IPSR());
  34. furi_hal_console_puts("] ");
  35. } else {
  36. const char* name = pcTaskGetName(NULL);
  37. if(name == NULL) {
  38. furi_hal_console_puts("[main] ");
  39. } else {
  40. furi_hal_console_puts("[");
  41. furi_hal_console_puts(name);
  42. furi_hal_console_puts("] ");
  43. }
  44. }
  45. }
  46. static FURI_NORETURN void __furi_halt() {
  47. asm volatile(
  48. #ifdef FURI_DEBUG
  49. "bkpt 0x00 \n"
  50. #endif
  51. "loop%=: \n"
  52. "wfi \n"
  53. "b loop%= \n"
  54. :
  55. :
  56. : "memory");
  57. __builtin_unreachable();
  58. }
  59. FURI_NORETURN void furi_crash(const char* message) {
  60. bool isr = FURI_IS_ISR();
  61. __disable_irq();
  62. if(message == NULL) {
  63. message = "Fatal Error";
  64. }
  65. furi_hal_console_puts("\r\n\033[0;31m[CRASH]");
  66. __furi_print_name(isr);
  67. furi_hal_console_puts(message);
  68. if(!isr) {
  69. __furi_print_stack_info();
  70. }
  71. __furi_print_heap_info();
  72. #ifdef FURI_DEBUG
  73. furi_hal_console_puts("\r\nSystem halted. Connect debugger for more info\r\n");
  74. furi_hal_console_puts("\033[0m\r\n");
  75. __furi_halt();
  76. #else
  77. furi_hal_rtc_set_fault_data((uint32_t)message);
  78. furi_hal_console_puts("\r\nRebooting system.\r\n");
  79. furi_hal_console_puts("\033[0m\r\n");
  80. furi_hal_power_reset();
  81. #endif
  82. __builtin_unreachable();
  83. }
  84. FURI_NORETURN void furi_halt(const char* message) {
  85. bool isr = FURI_IS_ISR();
  86. __disable_irq();
  87. if(message == NULL) {
  88. message = "System halt requested.";
  89. }
  90. furi_hal_console_puts("\r\n\033[0;31m[HALT]");
  91. __furi_print_name(isr);
  92. furi_hal_console_puts(message);
  93. furi_hal_console_puts("\r\nSystem halted. Bye-bye!\r\n");
  94. furi_hal_console_puts("\033[0m\r\n");
  95. __furi_halt();
  96. }