My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DebugMonitor.cpp 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef ARDUINO_ARCH_SAM
  23. #include "../../core/macros.h"
  24. #include "../../core/serial.h"
  25. #include "../shared/backtrace/unwinder.h"
  26. #include "../shared/backtrace/unwmemaccess.h"
  27. #include <stdarg.h>
  28. // Debug monitor that dumps to the Programming port all status when
  29. // an exception or WDT timeout happens - And then resets the board
  30. // All the Monitor routines must run with interrupts disabled and
  31. // under an ISR execution context. That is why we cannot reuse the
  32. // Serial interrupt routines or any C runtime, as we don't know the
  33. // state we are when running them
  34. // A SW memory barrier, to ensure GCC does not overoptimize loops
  35. #define sw_barrier() __asm__ volatile("": : :"memory");
  36. // (re)initialize UART0 as a monitor output to 250000,n,8,1
  37. static void TXBegin() {
  38. // Disable UART interrupt in NVIC
  39. NVIC_DisableIRQ( UART_IRQn );
  40. // We NEED memory barriers to ensure Interrupts are actually disabled!
  41. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  42. __DSB();
  43. __ISB();
  44. // Disable clock
  45. pmc_disable_periph_clk( ID_UART );
  46. // Configure PMC
  47. pmc_enable_periph_clk( ID_UART );
  48. // Disable PDC channel
  49. UART->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
  50. // Reset and disable receiver and transmitter
  51. UART->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
  52. // Configure mode: 8bit, No parity, 1 bit stop
  53. UART->UART_MR = UART_MR_CHMODE_NORMAL | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_NO;
  54. // Configure baudrate (asynchronous, no oversampling) to BAUDRATE bauds
  55. UART->UART_BRGR = (SystemCoreClock / (BAUDRATE << 4));
  56. // Enable receiver and transmitter
  57. UART->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
  58. }
  59. // Send character through UART with no interrupts
  60. static void TX(char c) {
  61. while (!(UART->UART_SR & UART_SR_TXRDY)) { WDT_Restart(WDT); sw_barrier(); };
  62. UART->UART_THR = c;
  63. }
  64. // Send String through UART
  65. static void TX(const char* s) {
  66. while (*s) TX(*s++);
  67. }
  68. static void TXDigit(uint32_t d) {
  69. if (d < 10) TX((char)(d+'0'));
  70. else if (d < 16) TX((char)(d+'A'-10));
  71. else TX('?');
  72. }
  73. // Send Hex number thru UART
  74. static void TXHex(uint32_t v) {
  75. TX("0x");
  76. for (uint8_t i = 0; i < 8; i++, v <<= 4)
  77. TXDigit((v >> 28) & 0xF);
  78. }
  79. // Send Decimal number thru UART
  80. static void TXDec(uint32_t v) {
  81. if (!v) {
  82. TX('0');
  83. return;
  84. }
  85. char nbrs[14];
  86. char *p = &nbrs[0];
  87. while (v != 0) {
  88. *p++ = '0' + (v % 10);
  89. v /= 10;
  90. }
  91. do {
  92. p--;
  93. TX(*p);
  94. } while (p != &nbrs[0]);
  95. }
  96. // Dump a backtrace entry
  97. static bool UnwReportOut(void* ctx, const UnwReport* bte) {
  98. int* p = (int*)ctx;
  99. (*p)++;
  100. TX('#'); TXDec(*p); TX(" : ");
  101. TX(bte->name?bte->name:"unknown"); TX('@'); TXHex(bte->function);
  102. TX('+'); TXDec(bte->address - bte->function);
  103. TX(" PC:");TXHex(bte->address); TX('\n');
  104. return true;
  105. }
  106. #ifdef UNW_DEBUG
  107. void UnwPrintf(const char* format, ...) {
  108. char dest[256];
  109. va_list argptr;
  110. va_start(argptr, format);
  111. vsprintf(dest, format, argptr);
  112. va_end(argptr);
  113. TX(&dest[0]);
  114. }
  115. #endif
  116. /* Table of function pointers for passing to the unwinder */
  117. static const UnwindCallbacks UnwCallbacks = {
  118. UnwReportOut,
  119. UnwReadW,
  120. UnwReadH,
  121. UnwReadB
  122. #ifdef UNW_DEBUG
  123. , UnwPrintf
  124. #endif
  125. };
  126. /**
  127. * HardFaultHandler_C:
  128. * This is called from the HardFault_HandlerAsm with a pointer the Fault stack
  129. * as the parameter. We can then read the values from the stack and place them
  130. * into local variables for ease of reading.
  131. * We then read the various Fault Status and Address Registers to help decode
  132. * cause of the fault.
  133. * The function ends with a BKPT instruction to force control back into the debugger
  134. */
  135. extern "C"
  136. void HardFault_HandlerC(unsigned long *sp, unsigned long lr, unsigned long cause) {
  137. static const char* causestr[] = {
  138. "NMI","Hard","Mem","Bus","Usage","Debug","WDT","RSTC"
  139. };
  140. UnwindFrame btf;
  141. // Dump report to the Programming port (interrupts are DISABLED)
  142. TXBegin();
  143. TX("\n\n## Software Fault detected ##\n");
  144. TX("Cause: "); TX(causestr[cause]); TX('\n');
  145. TX("R0 : "); TXHex(((unsigned long)sp[0])); TX('\n');
  146. TX("R1 : "); TXHex(((unsigned long)sp[1])); TX('\n');
  147. TX("R2 : "); TXHex(((unsigned long)sp[2])); TX('\n');
  148. TX("R3 : "); TXHex(((unsigned long)sp[3])); TX('\n');
  149. TX("R12 : "); TXHex(((unsigned long)sp[4])); TX('\n');
  150. TX("LR : "); TXHex(((unsigned long)sp[5])); TX('\n');
  151. TX("PC : "); TXHex(((unsigned long)sp[6])); TX('\n');
  152. TX("PSR : "); TXHex(((unsigned long)sp[7])); TX('\n');
  153. // Configurable Fault Status Register
  154. // Consists of MMSR, BFSR and UFSR
  155. TX("CFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED28)))); TX('\n');
  156. // Hard Fault Status Register
  157. TX("HFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED2C)))); TX('\n');
  158. // Debug Fault Status Register
  159. TX("DFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED30)))); TX('\n');
  160. // Auxiliary Fault Status Register
  161. TX("AFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED3C)))); TX('\n');
  162. // Read the Fault Address Registers. These may not contain valid values.
  163. // Check BFARVALID/MMARVALID to see if they are valid values
  164. // MemManage Fault Address Register
  165. TX("MMAR : "); TXHex((*((volatile unsigned long *)(0xE000ED34)))); TX('\n');
  166. // Bus Fault Address Register
  167. TX("BFAR : "); TXHex((*((volatile unsigned long *)(0xE000ED38)))); TX('\n');
  168. TX("ExcLR: "); TXHex(lr); TX('\n');
  169. TX("ExcSP: "); TXHex((unsigned long)sp); TX('\n');
  170. btf.sp = ((unsigned long)sp) + 8*4; // The original stack pointer
  171. btf.fp = btf.sp;
  172. btf.lr = ((unsigned long)sp[5]);
  173. btf.pc = ((unsigned long)sp[6]) | 1; // Force Thumb, as CORTEX only support it
  174. // Perform a backtrace
  175. TX("\nBacktrace:\n\n");
  176. int ctr = 0;
  177. UnwindStart(&btf, &UnwCallbacks, &ctr);
  178. // Disable all NVIC interrupts
  179. NVIC->ICER[0] = 0xFFFFFFFF;
  180. NVIC->ICER[1] = 0xFFFFFFFF;
  181. // Relocate VTOR table to default position
  182. SCB->VTOR = 0;
  183. // Disable USB
  184. otg_disable();
  185. // Restart watchdog
  186. WDT_Restart(WDT);
  187. // Reset controller
  188. NVIC_SystemReset();
  189. for (;;) WDT_Restart(WDT);
  190. }
  191. __attribute__((naked)) void NMI_Handler() {
  192. __asm__ __volatile__ (
  193. ".syntax unified" "\n\t"
  194. A("tst lr, #4")
  195. A("ite eq")
  196. A("mrseq r0, msp")
  197. A("mrsne r0, psp")
  198. A("mov r1,lr")
  199. A("mov r2,#0")
  200. A("b HardFault_HandlerC")
  201. );
  202. }
  203. __attribute__((naked)) void HardFault_Handler() {
  204. __asm__ __volatile__ (
  205. ".syntax unified" "\n\t"
  206. A("tst lr, #4")
  207. A("ite eq")
  208. A("mrseq r0, msp")
  209. A("mrsne r0, psp")
  210. A("mov r1,lr")
  211. A("mov r2,#1")
  212. A("b HardFault_HandlerC")
  213. );
  214. }
  215. __attribute__((naked)) void MemManage_Handler() {
  216. __asm__ __volatile__ (
  217. ".syntax unified" "\n\t"
  218. A("tst lr, #4")
  219. A("ite eq")
  220. A("mrseq r0, msp")
  221. A("mrsne r0, psp")
  222. A("mov r1,lr")
  223. A("mov r2,#2")
  224. A("b HardFault_HandlerC")
  225. );
  226. }
  227. __attribute__((naked)) void BusFault_Handler() {
  228. __asm__ __volatile__ (
  229. ".syntax unified" "\n\t"
  230. A("tst lr, #4")
  231. A("ite eq")
  232. A("mrseq r0, msp")
  233. A("mrsne r0, psp")
  234. A("mov r1,lr")
  235. A("mov r2,#3")
  236. A("b HardFault_HandlerC")
  237. );
  238. }
  239. __attribute__((naked)) void UsageFault_Handler() {
  240. __asm__ __volatile__ (
  241. ".syntax unified" "\n\t"
  242. A("tst lr, #4")
  243. A("ite eq")
  244. A("mrseq r0, msp")
  245. A("mrsne r0, psp")
  246. A("mov r1,lr")
  247. A("mov r2,#4")
  248. A("b HardFault_HandlerC")
  249. );
  250. }
  251. __attribute__((naked)) void DebugMon_Handler() {
  252. __asm__ __volatile__ (
  253. ".syntax unified" "\n\t"
  254. A("tst lr, #4")
  255. A("ite eq")
  256. A("mrseq r0, msp")
  257. A("mrsne r0, psp")
  258. A("mov r1,lr")
  259. A("mov r2,#5")
  260. A("b HardFault_HandlerC")
  261. );
  262. }
  263. /* This is NOT an exception, it is an interrupt handler - Nevertheless, the framing is the same */
  264. __attribute__((naked)) void WDT_Handler() {
  265. __asm__ __volatile__ (
  266. ".syntax unified" "\n\t"
  267. A("tst lr, #4")
  268. A("ite eq")
  269. A("mrseq r0, msp")
  270. A("mrsne r0, psp")
  271. A("mov r1,lr")
  272. A("mov r2,#6")
  273. A("b HardFault_HandlerC")
  274. );
  275. }
  276. __attribute__((naked)) void RSTC_Handler() {
  277. __asm__ __volatile__ (
  278. ".syntax unified" "\n\t"
  279. A("tst lr, #4")
  280. A("ite eq")
  281. A("mrseq r0, msp")
  282. A("mrsne r0, psp")
  283. A("mov r1,lr")
  284. A("mov r2,#7")
  285. A("b HardFault_HandlerC")
  286. );
  287. }
  288. #endif // ARDUINO_ARCH_SAM