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_Due.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef ARDUINO_ARCH_SAM
  23. #include "../../inc/MarlinConfig.h"
  24. #include "../../Marlin.h"
  25. #include "backtrace/backtrace.h"
  26. // Debug monitor that dumps to the Programming port all status when
  27. // an exception or WDT timeout happens - And then resets the board
  28. // All the Monitor routines must run with interrupts disabled and
  29. // under an ISR execution context. That is why we cannot reuse the
  30. // Serial interrupt routines or any C runtime, as we don't know the
  31. // state we are when running them
  32. // A SW memory barrier, to ensure GCC does not overoptimize loops
  33. #define sw_barrier() asm volatile("": : :"memory");
  34. // (re)initialize UART0 as a monitor output to 250000,n,8,1
  35. static void TXBegin(void) {
  36. // Disable UART interrupt in NVIC
  37. NVIC_DisableIRQ( UART_IRQn );
  38. // Disable clock
  39. pmc_disable_periph_clk( ID_UART );
  40. // Configure PMC
  41. pmc_enable_periph_clk( ID_UART );
  42. // Disable PDC channel
  43. UART->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
  44. // Reset and disable receiver and transmitter
  45. UART->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
  46. // Configure mode: 8bit, No parity, 1 bit stop
  47. UART->UART_MR = UART_MR_CHMODE_NORMAL | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_NO;
  48. // Configure baudrate (asynchronous, no oversampling) to BAUDRATE bauds
  49. UART->UART_BRGR = (SystemCoreClock / (BAUDRATE << 4));
  50. // Enable receiver and transmitter
  51. UART->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
  52. }
  53. // Send character through UART with no interrupts
  54. static void TX(char c) {
  55. while (!(UART->UART_SR & UART_SR_TXRDY)) { WDT_Restart(WDT); sw_barrier(); };
  56. UART->UART_THR = c;
  57. }
  58. // Send String through UART
  59. static void TX(const char* s) {
  60. while (*s) {
  61. TX(*s++);
  62. }
  63. }
  64. static void TXDigit(uint32_t d) {
  65. if (d < 10) TX((char)(d+'0'));
  66. else if (d < 16) TX((char)(d+'A'-10));
  67. else TX('?');
  68. }
  69. // Send Hex number thru UART
  70. static void TXHex(uint32_t v) {
  71. TX("0x");
  72. for (int i=0; i<8; i++, v <<= 4) {
  73. TXDigit((v >> 28) & 0xF);
  74. }
  75. }
  76. // Send Decimal number thru UART
  77. static void TXDec(uint32_t v) {
  78. if (!v) {
  79. TX('0');
  80. return;
  81. }
  82. char nbrs[14];
  83. char *p = &nbrs[0];
  84. while (v != 0) {
  85. *p++ = '0' + (v % 10);
  86. v /= 10;
  87. }
  88. do {
  89. p--;
  90. TX(*p);
  91. } while (p != &nbrs[0]);
  92. }
  93. // Dump a backtrace entry
  94. static void backtrace_dump_fn(int idx, const backtrace_t* bte, void* ctx) {
  95. TX('#'); TXDec(idx); TX(' ');
  96. TX(bte->name); TX('@');TXHex((uint32_t)bte->function); TX('+'); TXDec((uint32_t)bte->address - (uint32_t)bte->function);
  97. TX(" PC:");TXHex((uint32_t)bte->address); TX('\n');
  98. }
  99. /**
  100. * HardFaultHandler_C:
  101. * This is called from the HardFault_HandlerAsm with a pointer the Fault stack
  102. * as the parameter. We can then read the values from the stack and place them
  103. * into local variables for ease of reading.
  104. * We then read the various Fault Status and Address Registers to help decode
  105. * cause of the fault.
  106. * The function ends with a BKPT instruction to force control back into the debugger
  107. */
  108. extern "C"
  109. void HardFault_HandlerC(unsigned long *hardfault_args, unsigned long cause) {
  110. static const char* causestr[] = {
  111. "NMI","Hard","Mem","Bus","Usage","Debug","WDT","RSTC"
  112. };
  113. // Dump report to the Programming port (interrupts are DISABLED)
  114. TXBegin();
  115. TX("\n\n## Software Fault detected ##\n");
  116. TX("Cause: "); TX(causestr[cause]); TX('\n');
  117. TX("R0 : "); TXHex(((unsigned long)hardfault_args[0])); TX('\n');
  118. TX("R1 : "); TXHex(((unsigned long)hardfault_args[1])); TX('\n');
  119. TX("R2 : "); TXHex(((unsigned long)hardfault_args[2])); TX('\n');
  120. TX("R3 : "); TXHex(((unsigned long)hardfault_args[3])); TX('\n');
  121. TX("R12 : "); TXHex(((unsigned long)hardfault_args[4])); TX('\n');
  122. TX("LR : "); TXHex(((unsigned long)hardfault_args[5])); TX('\n');
  123. TX("PC : "); TXHex(((unsigned long)hardfault_args[6])); TX('\n');
  124. TX("PSR : "); TXHex(((unsigned long)hardfault_args[7])); TX('\n');
  125. // Configurable Fault Status Register
  126. // Consists of MMSR, BFSR and UFSR
  127. TX("CFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED28)))); TX('\n');
  128. // Hard Fault Status Register
  129. TX("HFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED2C)))); TX('\n');
  130. // Debug Fault Status Register
  131. TX("DFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED30)))); TX('\n');
  132. // Auxiliary Fault Status Register
  133. TX("AFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED3C)))); TX('\n');
  134. // Read the Fault Address Registers. These may not contain valid values.
  135. // Check BFARVALID/MMARVALID to see if they are valid values
  136. // MemManage Fault Address Register
  137. TX("MMAR : "); TXHex((*((volatile unsigned long *)(0xE000ED34)))); TX('\n');
  138. // Bus Fault Address Register
  139. TX("BFAR : "); TXHex((*((volatile unsigned long *)(0xE000ED38)))); TX('\n');
  140. // Perform a backtrace
  141. TX("\nBacktrace:\n\n");
  142. backtrace_frame_t btf;
  143. btf.sp = ((unsigned long)hardfault_args[7]);
  144. btf.fp = btf.sp;
  145. btf.lr = ((unsigned long)hardfault_args[5]);
  146. btf.pc = ((unsigned long)hardfault_args[6]);
  147. backtrace_dump(&btf, backtrace_dump_fn, nullptr);
  148. // Disable all NVIC interrupts
  149. NVIC->ICER[0] = 0xFFFFFFFF;
  150. NVIC->ICER[1] = 0xFFFFFFFF;
  151. // Relocate VTOR table to default position
  152. SCB->VTOR = 0;
  153. // Disable USB
  154. otg_disable();
  155. // Restart watchdog
  156. WDT_Restart(WDT);
  157. // Reset controller
  158. NVIC_SystemReset();
  159. while(1) { WDT_Restart(WDT); }
  160. }
  161. __attribute__((naked)) void NMI_Handler(void) {
  162. __asm volatile (
  163. " tst lr, #4 \n"
  164. " ite eq \n"
  165. " mrseq r0, msp \n"
  166. " mrsne r0, psp \n"
  167. " mov r1,#0 \n"
  168. " b HardFault_HandlerC \n"
  169. );
  170. }
  171. __attribute__((naked)) void HardFault_Handler(void) {
  172. __asm volatile (
  173. " tst lr, #4 \n"
  174. " ite eq \n"
  175. " mrseq r0, msp \n"
  176. " mrsne r0, psp \n"
  177. " mov r1,#1 \n"
  178. " b HardFault_HandlerC \n"
  179. );
  180. }
  181. __attribute__((naked)) void MemManage_Handler(void) {
  182. __asm volatile (
  183. " tst lr, #4 \n"
  184. " ite eq \n"
  185. " mrseq r0, msp \n"
  186. " mrsne r0, psp \n"
  187. " mov r1,#2 \n"
  188. " b HardFault_HandlerC \n"
  189. );
  190. }
  191. __attribute__((naked)) void BusFault_Handler(void) {
  192. __asm volatile (
  193. " tst lr, #4 \n"
  194. " ite eq \n"
  195. " mrseq r0, msp \n"
  196. " mrsne r0, psp \n"
  197. " mov r1,#3 \n"
  198. " b HardFault_HandlerC \n"
  199. );
  200. }
  201. __attribute__((naked)) void UsageFault_Handler(void) {
  202. __asm volatile (
  203. " tst lr, #4 \n"
  204. " ite eq \n"
  205. " mrseq r0, msp \n"
  206. " mrsne r0, psp \n"
  207. " mov r1,#4 \n"
  208. " b HardFault_HandlerC \n"
  209. );
  210. }
  211. __attribute__((naked)) void DebugMon_Handler(void) {
  212. __asm volatile (
  213. " tst lr, #4 \n"
  214. " ite eq \n"
  215. " mrseq r0, msp \n"
  216. " mrsne r0, psp \n"
  217. " mov r1,#5 \n"
  218. " b HardFault_HandlerC \n"
  219. );
  220. }
  221. __attribute__((naked)) void WDT_Handler(void) {
  222. __asm volatile (
  223. " tst lr, #4 \n"
  224. " ite eq \n"
  225. " mrseq r0, msp \n"
  226. " mrsne r0, psp \n"
  227. " mov r1,#6 \n"
  228. " b HardFault_HandlerC \n"
  229. );
  230. }
  231. __attribute__((naked)) void RSTC_Handler(void) {
  232. __asm volatile (
  233. " tst lr, #4 \n"
  234. " ite eq \n"
  235. " mrseq r0, msp \n"
  236. " mrsne r0, psp \n"
  237. " mov r1,#7 \n"
  238. " b HardFault_HandlerC \n"
  239. );
  240. }
  241. #endif