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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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 TARGET_LPC1768
  23. #include "../../core/macros.h"
  24. #include "../../core/serial.h"
  25. #include <stdarg.h>
  26. #include "../shared/backtrace/unwinder.h"
  27. #include "../shared/backtrace/unwmemaccess.h"
  28. #include "watchdog.h"
  29. #include <debug_frmwrk.h>
  30. // Debug monitor that dumps to the Programming port all status when
  31. // an exception or WDT timeout happens - And then resets the board
  32. // All the Monitor routines must run with interrupts disabled and
  33. // under an ISR execution context. That is why we cannot reuse the
  34. // Serial interrupt routines or any C runtime, as we don't know the
  35. // state we are when running them
  36. // A SW memory barrier, to ensure GCC does not overoptimize loops
  37. #define sw_barrier() __asm__ volatile("": : :"memory");
  38. // (re)initialize UART0 as a monitor output to 250000,n,8,1
  39. static void TXBegin(void) {
  40. }
  41. // Send character through UART with no interrupts
  42. static void TX(char c) {
  43. _DBC(c);
  44. }
  45. // Send String through UART
  46. static void TX(const char* s) {
  47. while (*s) TX(*s++);
  48. }
  49. static void TXDigit(uint32_t d) {
  50. if (d < 10) TX((char)(d+'0'));
  51. else if (d < 16) TX((char)(d+'A'-10));
  52. else TX('?');
  53. }
  54. // Send Hex number thru UART
  55. static void TXHex(uint32_t v) {
  56. TX("0x");
  57. for (uint8_t i = 0; i < 8; i++, v <<= 4)
  58. TXDigit((v >> 28) & 0xF);
  59. }
  60. // Send Decimal number thru UART
  61. static void TXDec(uint32_t v) {
  62. if (!v) {
  63. TX('0');
  64. return;
  65. }
  66. char nbrs[14];
  67. char *p = &nbrs[0];
  68. while (v != 0) {
  69. *p++ = '0' + (v % 10);
  70. v /= 10;
  71. }
  72. do {
  73. p--;
  74. TX(*p);
  75. } while (p != &nbrs[0]);
  76. }
  77. // Dump a backtrace entry
  78. static bool UnwReportOut(void* ctx, const UnwReport* bte) {
  79. int* p = (int*)ctx;
  80. (*p)++;
  81. TX('#'); TXDec(*p); TX(" : ");
  82. TX(bte->name?bte->name:"unknown"); TX('@'); TXHex(bte->function);
  83. TX('+'); TXDec(bte->address - bte->function);
  84. TX(" PC:");TXHex(bte->address); TX('\n');
  85. return true;
  86. }
  87. #ifdef UNW_DEBUG
  88. void UnwPrintf(const char* format, ...) {
  89. char dest[256];
  90. va_list argptr;
  91. va_start(argptr, format);
  92. vsprintf(dest, format, argptr);
  93. va_end(argptr);
  94. TX(&dest[0]);
  95. }
  96. #endif
  97. /* Table of function pointers for passing to the unwinder */
  98. static const UnwindCallbacks UnwCallbacks = {
  99. UnwReportOut,
  100. UnwReadW,
  101. UnwReadH,
  102. UnwReadB
  103. #ifdef UNW_DEBUG
  104. ,UnwPrintf
  105. #endif
  106. };
  107. /**
  108. * HardFaultHandler_C:
  109. * This is called from the HardFault_HandlerAsm with a pointer the Fault stack
  110. * as the parameter. We can then read the values from the stack and place them
  111. * into local variables for ease of reading.
  112. * We then read the various Fault Status and Address Registers to help decode
  113. * cause of the fault.
  114. * The function ends with a BKPT instruction to force control back into the debugger
  115. */
  116. extern "C"
  117. void HardFault_HandlerC(unsigned long *sp, unsigned long lr, unsigned long cause) {
  118. static const char* causestr[] = {
  119. "NMI","Hard","Mem","Bus","Usage","Debug","WDT","RSTC"
  120. };
  121. UnwindFrame btf;
  122. // Dump report to the Programming port (interrupts are DISABLED)
  123. TXBegin();
  124. TX("\n\n## Software Fault detected ##\n");
  125. TX("Cause: "); TX(causestr[cause]); TX('\n');
  126. TX("R0 : "); TXHex(((unsigned long)sp[0])); TX('\n');
  127. TX("R1 : "); TXHex(((unsigned long)sp[1])); TX('\n');
  128. TX("R2 : "); TXHex(((unsigned long)sp[2])); TX('\n');
  129. TX("R3 : "); TXHex(((unsigned long)sp[3])); TX('\n');
  130. TX("R12 : "); TXHex(((unsigned long)sp[4])); TX('\n');
  131. TX("LR : "); TXHex(((unsigned long)sp[5])); TX('\n');
  132. TX("PC : "); TXHex(((unsigned long)sp[6])); TX('\n');
  133. TX("PSR : "); TXHex(((unsigned long)sp[7])); TX('\n');
  134. // Configurable Fault Status Register
  135. // Consists of MMSR, BFSR and UFSR
  136. TX("CFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED28)))); TX('\n');
  137. // Hard Fault Status Register
  138. TX("HFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED2C)))); TX('\n');
  139. // Debug Fault Status Register
  140. TX("DFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED30)))); TX('\n');
  141. // Auxiliary Fault Status Register
  142. TX("AFSR : "); TXHex((*((volatile unsigned long *)(0xE000ED3C)))); TX('\n');
  143. // Read the Fault Address Registers. These may not contain valid values.
  144. // Check BFARVALID/MMARVALID to see if they are valid values
  145. // MemManage Fault Address Register
  146. TX("MMAR : "); TXHex((*((volatile unsigned long *)(0xE000ED34)))); TX('\n');
  147. // Bus Fault Address Register
  148. TX("BFAR : "); TXHex((*((volatile unsigned long *)(0xE000ED38)))); TX('\n');
  149. TX("ExcLR: "); TXHex(lr); TX('\n');
  150. TX("ExcSP: "); TXHex((unsigned long)sp); TX('\n');
  151. btf.sp = ((unsigned long)sp) + 8*4; // The original stack pointer
  152. btf.fp = btf.sp;
  153. btf.lr = ((unsigned long)sp[5]);
  154. btf.pc = ((unsigned long)sp[6]) | 1; // Force Thumb, as CORTEX only support it
  155. // Perform a backtrace
  156. TX("\nBacktrace:\n\n");
  157. int ctr = 0;
  158. UnwindStart(&btf, &UnwCallbacks, &ctr);
  159. // Disable all NVIC interrupts
  160. NVIC->ICER[0] = 0xFFFFFFFF;
  161. NVIC->ICER[1] = 0xFFFFFFFF;
  162. // Relocate VTOR table to default position
  163. SCB->VTOR = 0;
  164. // Clear cause of reset to prevent entering smoothie bootstrap
  165. HAL_clear_reset_source();
  166. // Restart watchdog
  167. //WDT_Restart(WDT);
  168. watchdog_init();
  169. // Reset controller
  170. NVIC_SystemReset();
  171. while(1) { watchdog_init(); }
  172. }
  173. extern "C" {
  174. __attribute__((naked)) void NMI_Handler(void) {
  175. __asm__ __volatile__ (
  176. ".syntax unified" "\n\t"
  177. A("tst lr, #4")
  178. A("ite eq")
  179. A("mrseq r0, msp")
  180. A("mrsne r0, psp")
  181. A("mov r1,lr")
  182. A("mov r2,#0")
  183. A("b HardFault_HandlerC")
  184. );
  185. }
  186. __attribute__((naked)) void HardFault_Handler(void) {
  187. __asm__ __volatile__ (
  188. ".syntax unified" "\n\t"
  189. A("tst lr, #4")
  190. A("ite eq")
  191. A("mrseq r0, msp")
  192. A("mrsne r0, psp")
  193. A("mov r1,lr")
  194. A("mov r2,#1")
  195. A("b HardFault_HandlerC")
  196. );
  197. }
  198. __attribute__((naked)) void MemManage_Handler(void) {
  199. __asm__ __volatile__ (
  200. ".syntax unified" "\n\t"
  201. A("tst lr, #4")
  202. A("ite eq")
  203. A("mrseq r0, msp")
  204. A("mrsne r0, psp")
  205. A("mov r1,lr")
  206. A("mov r2,#2")
  207. A("b HardFault_HandlerC")
  208. );
  209. }
  210. __attribute__((naked)) void BusFault_Handler(void) {
  211. __asm__ __volatile__ (
  212. ".syntax unified" "\n\t"
  213. A("tst lr, #4")
  214. A("ite eq")
  215. A("mrseq r0, msp")
  216. A("mrsne r0, psp")
  217. A("mov r1,lr")
  218. A("mov r2,#3")
  219. A("b HardFault_HandlerC")
  220. );
  221. }
  222. __attribute__((naked)) void UsageFault_Handler(void) {
  223. __asm__ __volatile__ (
  224. ".syntax unified" "\n\t"
  225. A("tst lr, #4")
  226. A("ite eq")
  227. A("mrseq r0, msp")
  228. A("mrsne r0, psp")
  229. A("mov r1,lr")
  230. A("mov r2,#4")
  231. A("b HardFault_HandlerC")
  232. );
  233. }
  234. __attribute__((naked)) void DebugMon_Handler(void) {
  235. __asm__ __volatile__ (
  236. ".syntax unified" "\n\t"
  237. A("tst lr, #4")
  238. A("ite eq")
  239. A("mrseq r0, msp")
  240. A("mrsne r0, psp")
  241. A("mov r1,lr")
  242. A("mov r2,#5")
  243. A("b HardFault_HandlerC")
  244. );
  245. }
  246. /* This is NOT an exception, it is an interrupt handler - Nevertheless, the framing is the same */
  247. __attribute__((naked)) void WDT_IRQHandler(void) {
  248. __asm__ __volatile__ (
  249. ".syntax unified" "\n\t"
  250. A("tst lr, #4")
  251. A("ite eq")
  252. A("mrseq r0, msp")
  253. A("mrsne r0, psp")
  254. A("mov r1,lr")
  255. A("mov r2,#6")
  256. A("b HardFault_HandlerC")
  257. );
  258. }
  259. __attribute__((naked)) void RSTC_Handler(void) {
  260. __asm__ __volatile__ (
  261. ".syntax unified" "\n\t"
  262. A("tst lr, #4")
  263. A("ite eq")
  264. A("mrseq r0, msp")
  265. A("mrsne r0, psp")
  266. A("mov r1,lr")
  267. A("mov r2,#7")
  268. A("b HardFault_HandlerC")
  269. );
  270. }
  271. }
  272. #endif // TARGET_LPC1768