Explorar el Código

Merge pull request #10185 from ejtagle/bugfix-2.0.x

[2.0.x] DUE debugging: Solve WDT startup delay, add traceback & crash report uses programming port baud rate
Bob-the-Kuhn hace 7 años
padre
commit
f7857ac8a8
No account linked to committer's email address

+ 51
- 2
Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp Ver fichero

@@ -24,6 +24,7 @@
24 24
 
25 25
 #include "../../inc/MarlinConfig.h"
26 26
 #include "../../Marlin.h"
27
+#include "backtrace/backtrace.h"
27 28
 
28 29
 // Debug monitor that dumps to the Programming port all status when
29 30
 // an exception or WDT timeout happens - And then resets the board
@@ -57,8 +58,8 @@ static void TXBegin(void) {
57 58
   // Configure mode: 8bit, No parity, 1 bit stop
58 59
   UART->UART_MR = UART_MR_CHMODE_NORMAL | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT | UART_MR_PAR_NO;
59 60
 
60
-  // Configure baudrate (asynchronous, no oversampling) to 250000 bauds
61
-  UART->UART_BRGR = (SystemCoreClock / (250000 << 4));
61
+  // Configure baudrate (asynchronous, no oversampling) to BAUDRATE bauds
62
+  UART->UART_BRGR = (SystemCoreClock / (BAUDRATE << 4));
62 63
 
63 64
   // Enable receiver and transmitter
64 65
   UART->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
@@ -92,6 +93,32 @@ static void TXHex(uint32_t v) {
92 93
   }
93 94
 }
94 95
 
96
+// Send Decimal number thru UART
97
+static void TXDec(uint32_t v) {
98
+  if (!v) {
99
+    TX('0');
100
+    return;
101
+  }
102
+
103
+  char nbrs[14];
104
+  char *p = &nbrs[0];
105
+  while (v != 0) {
106
+    *p++ = '0' + (v % 10);
107
+    v /= 10;
108
+  }
109
+  do {
110
+    p--;
111
+    TX(*p);
112
+  } while (p != &nbrs[0]);
113
+}
114
+
115
+// Dump a backtrace entry
116
+static void backtrace_dump_fn(int idx, const backtrace_t* bte, void* ctx) {
117
+  TX('#'); TXDec(idx); TX(' ');
118
+  TX(bte->name); TX('@');TXHex((uint32_t)bte->function); TX('+'); TXDec((uint32_t)bte->address - (uint32_t)bte->function);
119
+  TX(" PC:");TXHex((uint32_t)bte->address); TX('\n');
120
+}
121
+
95 122
 /**
96 123
  * HardFaultHandler_C:
97 124
  * This is called from the HardFault_HandlerAsm with a pointer the Fault stack
@@ -142,6 +169,28 @@ void HardFault_HandlerC(unsigned long *hardfault_args, unsigned long cause) {
142 169
   // Bus Fault Address Register
143 170
   TX("BFAR : "); TXHex((*((volatile unsigned long *)(0xE000ED38)))); TX('\n');
144 171
 
172
+  // Perform a backtrace
173
+  TX("\nBacktrace:\n\n");
174
+  backtrace_frame_t btf;
175
+  btf.sp = ((unsigned long)hardfault_args[7]);
176
+  btf.fp = btf.sp;
177
+  btf.lr = ((unsigned long)hardfault_args[5]);
178
+  btf.pc = ((unsigned long)hardfault_args[6]);
179
+  backtrace_dump(&btf, backtrace_dump_fn, nullptr);
180
+
181
+  // Disable all NVIC interrupts
182
+  NVIC->ICER[0] = 0xFFFFFFFF;
183
+  NVIC->ICER[1] = 0xFFFFFFFF;
184
+
185
+  // Relocate VTOR table to default position
186
+  SCB->VTOR = 0;
187
+
188
+  // Disable USB
189
+  otg_disable();
190
+
191
+  // Restart watchdog
192
+  WDT_Restart(WDT);
193
+
145 194
   // Reset controller
146 195
   NVIC_SystemReset();
147 196
   while(1) { WDT_Restart(WDT); }

+ 544
- 0
Marlin/src/HAL/HAL_DUE/backtrace/backtrace.c Ver fichero

@@ -0,0 +1,544 @@
1
+/*
2
+ * Libbacktrace
3
+ * Copyright 2015 Stephen Street <stephen@redrocketcomputing.com>
4
+ *
5
+ * This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ *
9
+ * This library was modified, some bugs fixed, stack address validated
10
+ * and adapted to be used in Marlin 3D printer firmware as backtracer
11
+ * for exceptions for debugging purposes in 2018 by Eduardo José Tagle.
12
+ */
13
+
14
+#ifdef ARDUINO_ARCH_SAM
15
+
16
+#include "backtrace.h"
17
+
18
+#include <stdint.h>
19
+#include <string.h>
20
+
21
+typedef struct unwind_control_block {
22
+  uint32_t vrs[16];
23
+  const uint32_t *current;
24
+  int remaining;
25
+  int byte;
26
+} unwind_control_block_t;
27
+
28
+typedef struct unwind_index {
29
+  uint32_t addr_offset;
30
+  uint32_t insn;
31
+} unwind_index_t;
32
+
33
+/* These symbols point to the unwind index and should be provide by the linker script */
34
+extern const unwind_index_t __exidx_start[];
35
+extern const unwind_index_t __exidx_end[];
36
+
37
+/* This prevents the linking of libgcc unwinder code */
38
+void __aeabi_unwind_cpp_pr0(void) {};
39
+void __aeabi_unwind_cpp_pr1(void) {};
40
+void __aeabi_unwind_cpp_pr2(void) {};
41
+
42
+/* These symbols point to the start and end of stack */
43
+extern const int _sstack;
44
+extern const int _estack;
45
+
46
+/* These symbols point to the start and end of the code section */
47
+extern const int _sfixed;
48
+extern const int _efixed;
49
+
50
+/* These symbols point to the start and end of initialized data (could be SRAM functions!) */
51
+extern const int _srelocate;
52
+extern const int _erelocate;
53
+
54
+/* Validate stack pointer (SP): It must be in the stack area */
55
+static inline __attribute__((always_inline)) int validate_sp(const void* sp) {
56
+  // SP must point into the allocated stack area
57
+  if ((uint32_t)sp >= (uint32_t)&_sstack && (uint32_t)sp <= (uint32_t)&_estack)
58
+    return 0;
59
+  return -1;
60
+}
61
+
62
+/* Validate code pointer (PC): It must be either in TEXT or in SRAM */
63
+static inline __attribute__((always_inline)) int validate_pc(const void* pc) {
64
+  // PC must point into the text (CODE) area
65
+  if ((uint32_t)pc >= (uint32_t)&_sfixed && (uint32_t)pc <= (uint32_t)&_efixed)
66
+    return 0;
67
+  // Or into the SRAM function area
68
+  if ((uint32_t)pc >= (uint32_t)&_srelocate && (uint32_t)pc <= (uint32_t)&_erelocate)
69
+    return 0;
70
+  return 0;
71
+}
72
+
73
+static inline __attribute__((always_inline)) uint32_t prel31_to_addr(const uint32_t *prel31) {
74
+  int32_t offset = (((int32_t)(*prel31)) << 1) >> 1;
75
+  return ((uint32_t)prel31 + offset) & 0x7fffffff;
76
+}
77
+
78
+static const struct unwind_index *unwind_search_index(const unwind_index_t *start, const unwind_index_t *end, uint32_t ip) {
79
+  const struct unwind_index *middle;
80
+
81
+  /* Perform a binary search of the unwind index */
82
+  while (start < end - 1) {
83
+    middle = start + ((end - start + 1) >> 1);
84
+    if (ip < prel31_to_addr(&middle->addr_offset))
85
+      end = middle;
86
+    else
87
+      start = middle;
88
+  }
89
+  return start;
90
+}
91
+
92
+static const char *unwind_get_function_name(void *address) {
93
+  uint32_t flag_word = *(uint32_t *)(address - 4);
94
+  if ((flag_word & 0xff000000) == 0xff000000) {
95
+    return (const char *)(address - 4 - (flag_word & 0x00ffffff));
96
+  }
97
+  return "unknown";
98
+}
99
+
100
+static int unwind_get_next_byte(unwind_control_block_t *ucb) {
101
+  int instruction;
102
+
103
+  /* Are there more instructions */
104
+  if (ucb->remaining == 0)
105
+    return -1;
106
+
107
+  /* Extract the current instruction */
108
+  instruction = ((*ucb->current) >> (ucb->byte << 3)) & 0xff;
109
+
110
+  /* Move the next byte */
111
+  --ucb->byte;
112
+  if (ucb->byte < 0) {
113
+    ++ucb->current;
114
+    ucb->byte = 3;
115
+  }
116
+  --ucb->remaining;
117
+
118
+  return instruction;
119
+}
120
+
121
+static int unwind_control_block_init(unwind_control_block_t *ucb, const uint32_t *instructions, const backtrace_frame_t *frame) {
122
+  /* Initialize control block */
123
+  memset(ucb, 0, sizeof(unwind_control_block_t));
124
+  ucb->current = instructions;
125
+
126
+  /* Is a short unwind description */
127
+  if ((*instructions & 0xff000000) == 0x80000000) {
128
+    ucb->remaining = 3;
129
+    ucb->byte = 2;
130
+  /* Is a long unwind description */
131
+  } else if ((*instructions & 0xff000000) == 0x81000000) {
132
+    ucb->remaining = ((*instructions & 0x00ff0000) >> 14) + 2;
133
+    ucb->byte = 1;
134
+  } else
135
+    return -1;
136
+
137
+  /* Initialize the virtual register set */
138
+  ucb->vrs[7] = frame->fp;
139
+  ucb->vrs[13] = frame->sp;
140
+  ucb->vrs[14] = frame->lr;
141
+  ucb->vrs[15] = 0;
142
+
143
+  /* All good */
144
+  return 0;
145
+}
146
+
147
+static int unwind_execute_instruction(unwind_control_block_t *ucb) {
148
+
149
+  int instruction;
150
+  uint32_t mask;
151
+  uint32_t reg;
152
+  uint32_t *vsp;
153
+
154
+  /* Consume all instruction byte */
155
+  while ((instruction = unwind_get_next_byte(ucb)) != -1) {
156
+
157
+    if ((instruction & 0xc0) == 0x00) { // ARM_EXIDX_CMD_DATA_POP
158
+      /* vsp = vsp + (xxxxxx << 2) + 4 */
159
+      ucb->vrs[13] += ((instruction & 0x3f) << 2) + 4;
160
+    } else
161
+    if ((instruction & 0xc0) == 0x40) { // ARM_EXIDX_CMD_DATA_PUSH
162
+      /* vsp = vsp - (xxxxxx << 2) - 4 */
163
+      ucb->vrs[13] -= ((instruction & 0x3f) << 2) - 4;
164
+    } else
165
+    if ((instruction & 0xf0) == 0x80) {
166
+      /* pop under mask {r15-r12},{r11-r4} or refuse to unwind */
167
+      instruction = instruction << 8 | unwind_get_next_byte(ucb);
168
+
169
+      /* Check for refuse to unwind */
170
+      if (instruction == 0x8000)        // ARM_EXIDX_CMD_REFUSED
171
+        return 0;
172
+
173
+      /* Pop registers using mask */    // ARM_EXIDX_CMD_REG_POP
174
+      vsp = (uint32_t *)ucb->vrs[13];
175
+      mask = instruction & 0xfff;
176
+
177
+      reg = 4;
178
+      while (mask) {
179
+        if ((mask & 1) != 0) {
180
+          if (validate_sp(vsp))
181
+            return -1;
182
+          ucb->vrs[reg] = *vsp++;
183
+        }
184
+        mask >>= 1;
185
+        ++reg;
186
+      }
187
+
188
+      /* Patch up the vrs sp if it was in the mask */
189
+      if ((instruction & (1 << (13 - 4))) != 0)
190
+        ucb->vrs[13] = (uint32_t)vsp;
191
+
192
+    } else
193
+    if ((instruction & 0xf0) == 0x90 && // ARM_EXIDX_CMD_REG_TO_SP
194
+        instruction != 0x9d &&
195
+        instruction != 0x9f) {
196
+      /* vsp = r[nnnn] */
197
+      ucb->vrs[13] = ucb->vrs[instruction & 0x0f];
198
+    } else
199
+    if ((instruction & 0xf0) == 0xa0) { // ARM_EXIDX_CMD_REG_POP
200
+      /* pop r4-r[4+nnn] or pop r4-r[4+nnn], r14*/
201
+      vsp = (uint32_t *)ucb->vrs[13];
202
+
203
+      for (reg = 4; reg <= (instruction & 0x07) + 4; ++reg) {
204
+        if (validate_sp(vsp))
205
+          return -1;
206
+        ucb->vrs[reg] = *vsp++;
207
+      }
208
+
209
+      if (instruction & 0x08) { // ARM_EXIDX_CMD_REG_POP
210
+        if (validate_sp(vsp))
211
+          return -1;
212
+        ucb->vrs[14] = *vsp++;
213
+      }
214
+
215
+      ucb->vrs[13] = (uint32_t)vsp;
216
+
217
+    } else
218
+    if (instruction == 0xb0) { // ARM_EXIDX_CMD_FINISH
219
+      /* finished */
220
+      if (ucb->vrs[15] == 0)
221
+        ucb->vrs[15] = ucb->vrs[14];
222
+
223
+      /* All done unwinding */
224
+      return 0;
225
+
226
+    } else
227
+    if (instruction == 0xb1) { // ARM_EXIDX_CMD_REG_POP
228
+      /* pop register under mask {r3,r2,r1,r0} */
229
+      vsp = (uint32_t *)ucb->vrs[13];
230
+      mask = unwind_get_next_byte(ucb);
231
+
232
+      reg = 0;
233
+      while (mask) {
234
+        if ((mask & 1) != 0) {
235
+          if (validate_sp(vsp))
236
+            return -1;
237
+          ucb->vrs[reg] = *vsp++;
238
+        }
239
+        mask >>= 1;
240
+        ++reg;
241
+      }
242
+      ucb->vrs[13] = (uint32_t)vsp;
243
+
244
+    } else
245
+    if (instruction == 0xb2) { // ARM_EXIDX_CMD_DATA_POP
246
+      /* vps = vsp + 0x204 + (uleb128 << 2) */
247
+      ucb->vrs[13] += 0x204 + (unwind_get_next_byte(ucb) << 2);
248
+
249
+    } else
250
+    if (instruction == 0xb3 || // ARM_EXIDX_CMD_VFP_POP
251
+      instruction == 0xc8 ||
252
+      instruction == 0xc9) {
253
+
254
+      /* pop VFP double-precision registers */
255
+      vsp = (uint32_t *)ucb->vrs[13];
256
+
257
+      /* D[ssss]-D[ssss+cccc] */
258
+      if (validate_sp(vsp))
259
+        return -1;
260
+      ucb->vrs[14] = *vsp++;
261
+
262
+      if (instruction == 0xc8) {
263
+        /* D[16+sssss]-D[16+ssss+cccc] */
264
+        ucb->vrs[14] |= 1 << 16;
265
+      }
266
+
267
+      if (instruction != 0xb3) {
268
+        /* D[sssss]-D[ssss+cccc] */
269
+        ucb->vrs[14] |= 1 << 17;
270
+      }
271
+
272
+      ucb->vrs[13] = (uint32_t)vsp;
273
+
274
+    } else
275
+    if ((instruction & 0xf8) == 0xb8 ||
276
+        (instruction & 0xf8) == 0xd0) {
277
+
278
+      /* Pop VFP double precision registers D[8]-D[8+nnn] */
279
+      ucb->vrs[14] = 0x80 | (instruction & 0x07);
280
+
281
+      if ((instruction & 0xf8) == 0xd0) {
282
+        ucb->vrs[14] = 1 << 17;
283
+      }
284
+
285
+    } else
286
+      return -1;
287
+  }
288
+
289
+  return instruction != -1;
290
+}
291
+
292
+static inline __attribute__((always_inline)) uint32_t *read_psp(void) {
293
+  /* Read the current PSP and return its value as a pointer */
294
+  uint32_t psp;
295
+
296
+  __asm volatile (
297
+    "   mrs %0, psp \n"
298
+    : "=r" (psp) : :
299
+  );
300
+
301
+  return (uint32_t*)psp;
302
+}
303
+
304
+static int unwind_frame(backtrace_frame_t *frame) {
305
+
306
+  unwind_control_block_t ucb;
307
+  const unwind_index_t *index;
308
+  const uint32_t *instructions;
309
+  int execution_result;
310
+
311
+  /* Search the unwind index for the matching unwind table */
312
+  index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
313
+  if (index == NULL)
314
+    return -1;
315
+
316
+  /* Make sure we can unwind this frame */
317
+  if (index->insn == 0x00000001)
318
+    return 0;
319
+
320
+  /* Get the pointer to the first unwind instruction */
321
+  if (index->insn & 0x80000000)
322
+    instructions = &index->insn;
323
+  else
324
+    instructions = (uint32_t *)prel31_to_addr(&index->insn);
325
+
326
+  /* Initialize the unwind control block */
327
+  if (unwind_control_block_init(&ucb, instructions, frame) < 0)
328
+    return -1;
329
+
330
+  /* Execute the unwind instructions */
331
+  while ((execution_result = unwind_execute_instruction(&ucb)) > 0);
332
+  if (execution_result == -1)
333
+    return -1;
334
+
335
+  /* Set the virtual pc to the virtual lr if this is the first unwind */
336
+  if (ucb.vrs[15] == 0)
337
+    ucb.vrs[15] = ucb.vrs[14];
338
+
339
+  /* Check for exception return */
340
+  /* TODO Test with other ARM processors to verify this method. */
341
+  if ((ucb.vrs[15] & 0xf0000000) == 0xf0000000) {
342
+    /* According to the Cortex Programming Manual (p.44), the stack address is always 8-byte aligned (Cortex-M7).
343
+       Depending on where the exception came from (MSP or PSP), we need the right SP value to work with.
344
+
345
+       ucb.vrs[7] contains the right value, so take it and align it by 8 bytes, store it as the current
346
+       SP to work with (ucb.vrs[13]) which is then saved as the current (virtual) frame's SP.
347
+    */
348
+    uint32_t *stack;
349
+    ucb.vrs[13] = (ucb.vrs[7] & ~7);
350
+
351
+    /* If we need to start from the MSP, we need to go down X words to find the PC, where:
352
+        X=2  if it was a non-floating-point exception
353
+        X=20 if it was a floating-point (VFP) exception
354
+
355
+       If we need to start from the PSP, we need to go up exactly 6 words to find the PC.
356
+       See the ARMv7-M Architecture Reference Manual p.594 and Cortex-M7 Processor Programming Manual p.44/p.45 for details.
357
+    */
358
+    if ((ucb.vrs[15] & 0xc) == 0) {
359
+      /* Return to Handler Mode: MSP (0xffffff-1) */
360
+      stack = (uint32_t*)(ucb.vrs[13]);
361
+
362
+      /* The PC is always 2 words down from the MSP, if it was a non-floating-point exception */
363
+      stack -= 2;
364
+
365
+      /* If there was a VFP exception (0xffffffe1), the PC is located another 18 words down */
366
+      if ((ucb.vrs[15] & 0xf0) == 0xe0) {
367
+        stack -= 18;
368
+      }
369
+    }
370
+    else {
371
+      /* Return to Thread Mode: PSP (0xffffff-d) */
372
+      stack = read_psp();
373
+
374
+      /* The PC is always 6 words up from the PSP */
375
+      stack += 6;
376
+    }
377
+
378
+    /* Store the PC */
379
+    ucb.vrs[15] = *stack--;
380
+
381
+    /* Store the LR */
382
+    ucb.vrs[14] = *stack--;
383
+  }
384
+
385
+  /* We are done if current frame pc is equal to the virtual pc, prevent infinite loop */
386
+  if (frame->pc == ucb.vrs[15])
387
+    return 0;
388
+
389
+  /* Update the frame */
390
+  frame->fp = ucb.vrs[7];
391
+  frame->sp = ucb.vrs[13];
392
+  frame->lr = ucb.vrs[14];
393
+  frame->pc = ucb.vrs[15];
394
+
395
+  /* All good */
396
+  return 1;
397
+}
398
+
399
+// Detect if function names are available
400
+static int __attribute__ ((noinline)) has_function_names(void) {
401
+  uint32_t flag_word = ((uint32_t*)&has_function_names)[-1];
402
+  return ((flag_word & 0xff000000) == 0xff000000) ? 1 : 0;
403
+}
404
+
405
+// Detect if unwind information is present or not
406
+static int has_unwind_info(void) {
407
+  return ((char*)(&__exidx_end) - (char*)(&__exidx_start)) > 16 ? 1 : 0; // 16 because there are default entries we can´t supress
408
+}
409
+
410
+int backtrace_dump(backtrace_frame_t *frame, backtrace_dump_fn_t dump_entry, void* ctx )
411
+{
412
+  backtrace_t entry;
413
+  int count = 1;
414
+
415
+  /* If there is no unwind information, perform a RAW try at it. Idea was taken from
416
+   * https://stackoverflow.com/questions/3398664/how-to-get-a-call-stack-backtrace-deeply-embedded-no-library-support
417
+   *
418
+   * And requires code to be compiled with the following flags:
419
+   * -mtpcs-frame -mtpcs-leaf-frame -fno-omit-frame-pointer
420
+   *  With these options, the Stack pointer is automatically
421
+   * pushed to the stack at the beginning of each function.
422
+   */
423
+  if (!has_unwind_info()) {
424
+
425
+    /*
426
+     *  We basically iterate through the current stack finding the
427
+     * following combination of values:
428
+     *  - <Frame Address>
429
+     *  - <Link Address>
430
+     * This combination will occur for each function in the call stack
431
+     */
432
+
433
+    uint32_t previous_frame_address = (uint32_t)frame->sp;
434
+    uint32_t* stack_pointer = (uint32_t*)frame->sp;
435
+
436
+    // loop following stack frames
437
+    while (1) {
438
+
439
+      // Validate stack address
440
+      if (validate_sp(stack_pointer))
441
+        break;
442
+
443
+      // Attempt to obtain next stack pointer
444
+      // The link address should come immediately after
445
+      const uint32_t possible_frame_address = *stack_pointer;
446
+      const uint32_t possible_link_address = *(stack_pointer+1);
447
+
448
+      // Next check that the frame addresss (i.e. stack pointer for the function)
449
+      // and Link address are within an acceptable range
450
+      if(possible_frame_address > previous_frame_address &&
451
+         validate_sp((const void *)possible_frame_address) == 0 &&
452
+        (possible_link_address & 1) != 0 && // in THUMB mode the address will be odd
453
+         validate_pc((const void *)possible_link_address) == 0) {
454
+
455
+        // We found two acceptable values.
456
+        entry.name = "unknown";
457
+        entry.address = (void*)possible_link_address;
458
+        entry.function = 0;
459
+
460
+        // If there are function names, try to solve name
461
+        if (has_function_names()) {
462
+          // Lets find the function name, if possible
463
+
464
+          // Align address to 4 bytes
465
+          uint32_t* pf = (uint32_t*) (((uint32_t)possible_link_address) & (-4));
466
+
467
+          // Scan backwards until we find the function name
468
+          while(validate_pc(pf-1) == 0) {
469
+
470
+            // Get name descriptor value
471
+            uint32_t v = pf[-1];
472
+
473
+            // Check if name descriptor is valid and name is terminated in 0.
474
+            if ((v & 0xffffff00) == 0xff000000 &&
475
+                (v & 0xff) > 1) {
476
+
477
+              // Assume the name was found!
478
+              entry.name = ((const char*)pf) - 4 - (v & 0xff);
479
+              entry.function = (void*)pf;
480
+              break;
481
+            }
482
+
483
+            // Go backwards to the previous word
484
+            --pf;
485
+          }
486
+        }
487
+        dump_entry(count, &entry, ctx);
488
+        ++count;
489
+
490
+        // Update the book-keeping registers for the next search
491
+        previous_frame_address = possible_frame_address;
492
+        stack_pointer = (uint32_t*)(possible_frame_address + 4);
493
+
494
+      } else {
495
+        // Keep iterating through the stack until we find an acceptable combination
496
+        ++stack_pointer;
497
+      }
498
+    }
499
+
500
+  } else {
501
+
502
+    /* Otherwise, unwind information is present. Use it to unwind frames */
503
+    do {
504
+      if (frame->pc == 0) {
505
+        /* Reached __exidx_end. */
506
+        entry.name = "<reached end of unwind table>";
507
+        entry.address = 0;
508
+        entry.function = 0;
509
+        dump_entry(count, &entry, ctx);
510
+        break;
511
+      }
512
+
513
+      if (frame->pc == 0x00000001) {
514
+        /* Reached .cantunwind instruction. */
515
+        entry.name = "<reached .cantunwind>";
516
+        entry.address = 0;
517
+        entry.function = 0;
518
+        dump_entry(count, &entry, ctx);
519
+        break;
520
+      }
521
+
522
+      /* Find the unwind index of the current frame pc */
523
+      const unwind_index_t *index = unwind_search_index(__exidx_start, __exidx_end, frame->pc);
524
+
525
+      /* Clear last bit (Thumb indicator) */
526
+      frame->pc &= 0xfffffffeU;
527
+
528
+      /* Generate the backtrace information */
529
+      entry.address = (void *)frame->pc;
530
+      entry.function = (void *)prel31_to_addr(&index->addr_offset);
531
+      entry.name = unwind_get_function_name(entry.function);
532
+      dump_entry(count, &entry, ctx);
533
+
534
+      /* Next backtrace frame */
535
+      ++count;
536
+
537
+    } while (unwind_frame(frame) == 1);
538
+  }
539
+
540
+  /* All done */
541
+  return count;
542
+}
543
+
544
+#endif

+ 53
- 0
Marlin/src/HAL/HAL_DUE/backtrace/backtrace.h Ver fichero

@@ -0,0 +1,53 @@
1
+/*
2
+ * Libbacktrace
3
+ * Copyright 2015 Stephen Street <stephen@redrocketcomputing.com>
4
+ *
5
+ * This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ *
9
+ * This library was modified and adapted to be used in Marlin 3D printer
10
+ * firmware as backtracer for exceptions for debugging purposes in 2018
11
+ * by Eduardo José Tagle.
12
+ */
13
+
14
+/*
15
+ * For this library to work, you need to compile with the following options
16
+ * -funwind-tables => So we will have unwind information to perform the stack trace
17
+ * -mpoke-function-name => So we will have function names in the trace
18
+ */
19
+
20
+#ifndef _BACKTRACE_H_
21
+#define _BACKTRACE_H_
22
+
23
+#include <stdint.h>
24
+
25
+#ifdef __cplusplus
26
+extern "C" {
27
+#endif
28
+
29
+/* A frame */
30
+typedef struct backtrace_frame {
31
+  uint32_t fp;
32
+  uint32_t sp;
33
+  uint32_t lr;
34
+  uint32_t pc;
35
+} backtrace_frame_t;
36
+
37
+/* A backtrace */
38
+typedef struct backtrace {
39
+  void *function;
40
+  void *address;
41
+  const char *name;
42
+} backtrace_t;
43
+
44
+typedef void (*backtrace_dump_fn_t)(int idx, const backtrace_t* bte, void* ctx);
45
+
46
+/* Perform a backtrace, given the specified stack start frame */
47
+int backtrace_dump(backtrace_frame_t *startframe, backtrace_dump_fn_t fn, void* ctx );
48
+
49
+#ifdef __cplusplus
50
+}
51
+#endif
52
+
53
+#endif // _BACKTRACE_H_

+ 4
- 0
Marlin/src/HAL/HAL_DUE/usb/usb_task.c Ver fichero

@@ -301,7 +301,11 @@ void usb_task_init(void) {
301 301
 
302 302
   uint16_t *ptr;
303 303
 
304
+  // Disable USB peripheral so we start clean and avoid lockups
305
+  otg_disable();
304 306
   udd_disable();
307
+
308
+  // Set the USB interrupt to our stack
305 309
   UDD_SetStack(&USBD_ISR);
306 310
 
307 311
   // Start USB stack to authorize VBus monitoring

+ 12
- 0
platformio.ini Ver fichero

@@ -97,6 +97,18 @@ lib_deps     = ${common.lib_deps}
97 97
 lib_ignore   = c1921b4
98 98
 src_filter   = ${common.default_src_filter}
99 99
 monitor_baud = 250000
100
+[env:DUE_debug]
101
+# Used when WATCHDOG_RESET_MANUAL is enabled
102
+platform     = atmelsam
103
+framework    = arduino
104
+board        = due
105
+build_flags  = ${common.build_flags}
106
+  -funwind-tables 
107
+  -mpoke-function-name
108
+lib_deps     = ${common.lib_deps}
109
+lib_ignore   = c1921b4
110
+src_filter   = ${common.default_src_filter}
111
+monitor_baud = 250000
100 112
 
101 113
 #
102 114
 # NXP LPC1768 ARM Cortex-M3

Loading…
Cancelar
Guardar