瀏覽代碼

Various fixes for DUE... (#10152)

- Watchdog reset during SD Card initialization.
- Move `DebugMonitor` to `DebugMonitor_Due.cpp`.
- Since the watchdog is enabled on boot do extra resets during init.
- Have `thermalManager` do watchdog reset before its ISR starts to prevent reset.
- Ensure that timers are stopped before reprogramming them to address tone issues.
- Improve SAM3XE reset when reflashed through the native port.
Eduardo José Tagle 6 年之前
父節點
當前提交
97e8a6ebd9

+ 238
- 0
Marlin/src/HAL/HAL_DUE/DebugMonitor_Due.cpp 查看文件

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

+ 20
- 8
Marlin/src/HAL/HAL_DUE/HAL_Due.cpp 查看文件

@@ -34,6 +34,7 @@
34 34
 #include "../HAL.h"
35 35
 
36 36
 #include <Wire.h>
37
+#include "usb/usb_task.h"
37 38
 
38 39
 // --------------------------------------------------------------------------
39 40
 // Externals
@@ -73,6 +74,18 @@ uint16_t HAL_adc_result;
73 74
 // Public functions
74 75
 // --------------------------------------------------------------------------
75 76
 
77
+// HAL initialization task
78
+void HAL_init(void) {
79
+  // Initialize the USB stack
80
+  usb_task_init();
81
+}
82
+
83
+// HAL idle task
84
+void HAL_idletask(void) {
85
+  // Perform USB stack housekeeping
86
+  usb_task_idle();
87
+}
88
+
76 89
 // disable interrupts
77 90
 void cli(void) { noInterrupts(); }
78 91
 
@@ -82,14 +95,13 @@ void sei(void) { interrupts(); }
82 95
 void HAL_clear_reset_source(void) { }
83 96
 
84 97
 uint8_t HAL_get_reset_source(void) {
85
-  switch ((RSTC->RSTC_SR >> 8) & 7) {
86
-    case 0: return RST_POWER_ON; break;
87
-    case 1: return RST_BACKUP; break;
88
-    case 2: return RST_WATCHDOG; break;
89
-    case 3: return RST_SOFTWARE; break;
90
-    case 4: return RST_EXTERNAL; break;
91
-    default:
92
-      return 0;
98
+  switch ((RSTC->RSTC_SR >> 8) & 0x07) {
99
+    case 0: return RST_POWER_ON;
100
+    case 1: return RST_BACKUP;
101
+    case 2: return RST_WATCHDOG;
102
+    case 3: return RST_SOFTWARE;
103
+    case 4: return RST_EXTERNAL;
104
+    default: return 0;
93 105
   }
94 106
 }
95 107
 

+ 4
- 3
Marlin/src/HAL/HAL_DUE/HAL_Due.h 查看文件

@@ -160,14 +160,15 @@ void toneInit();
160 160
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
161 161
 void noTone(const pin_t _pin);
162 162
 
163
-// Enable hooks into idle and setup for USB stack
163
+// Enable hooks into idle and setup for HAL
164 164
 #define HAL_IDLETASK 1
165 165
 #define HAL_INIT 1
166
+void HAL_idletask(void);
167
+void HAL_init(void);
168
+
166 169
 #ifdef __cplusplus
167 170
   extern "C" {
168 171
 #endif
169
-void HAL_idletask(void);
170
-void HAL_init(void);
171 172
 char *dtostrf (double __val, signed char __width, unsigned char __prec, char *__s);
172 173
 #ifdef __cplusplus
173 174
   }

+ 13
- 0
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.cpp 查看文件

@@ -96,6 +96,15 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
96 96
   IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
97 97
   uint32_t channel = TimerConfig[timer_num].channel;
98 98
 
99
+  // Disable interrupt, just in case it was already enabled
100
+  NVIC_DisableIRQ(irq);
101
+
102
+  // Disable timer interrupt
103
+  tc->TC_CHANNEL[channel].TC_IDR = TC_IDR_CPCS;
104
+
105
+  // Stop timer, just in case, to be able to reconfigure it
106
+  TC_Stop(tc, channel);
107
+
99 108
   pmc_set_writeprotect(false);
100 109
   pmc_enable_periph_clk((uint32_t)irq);
101 110
   NVIC_SetPriority(irq, TimerConfig [timer_num].priority);
@@ -103,12 +112,16 @@ void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
103 112
   // wave mode, reset counter on match with RC,
104 113
   TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
105 114
 
115
+  // Set compare value
106 116
   TC_SetRC(tc, channel, VARIANT_MCK / 2 / frequency);
117
+
118
+  // And start timer
107 119
   TC_Start(tc, channel);
108 120
 
109 121
   // enable interrupt on RC compare
110 122
   tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
111 123
 
124
+  // Finally, enable IRQ
112 125
   NVIC_EnableIRQ(irq);
113 126
 }
114 127
 

+ 0
- 11
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h 查看文件

@@ -109,23 +109,12 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
109 109
   return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
110 110
 }
111 111
 
112
-FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t counter) {
113
-  const tTimerConfig * const pConfig = &TimerConfig[timer_num];
114
-  pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV = counter;
115
-}
116
-
117 112
 // if counter too high then bump up compare
118 113
 FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
119 114
   const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
120 115
   if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
121 116
 }
122 117
 
123
-// if counter too high then clear it
124
-FORCE_INLINE static void HAL_timer_restrain_count(const uint8_t timer_num, const uint16_t interval_ticks) {
125
-  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
126
-  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_count(timer_num, 0);
127
-}
128
-
129 118
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
130 119
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
131 120
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 1
- 9
Marlin/src/HAL/HAL_DUE/Tone.cpp 查看文件

@@ -33,17 +33,10 @@
33 33
 static pin_t tone_pin;
34 34
 volatile static int32_t toggles;
35 35
 
36
-void toneInit() {
37
-  HAL_timer_start(TONE_TIMER_NUM, 100000);
38
-  HAL_timer_disable_interrupt(TONE_TIMER_NUM);
39
-}
40
-
41 36
 void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration) {
42 37
   tone_pin = _pin;
43 38
   toggles = 2 * frequency * duration / 1000;
44
-  HAL_timer_set_count(TONE_TIMER_NUM, 0);  // ensure first beep is correct (make sure counter is less than the compare value)
45
-  HAL_timer_set_compare(TONE_TIMER_NUM, VARIANT_MCK / 2 / 2 / frequency); // 84MHz / 2 prescaler / 2 interrupts per cycle /Hz
46
-  HAL_timer_enable_interrupt(TONE_TIMER_NUM);
39
+  HAL_timer_start(TONE_TIMER_NUM, 2 * frequency);
47 40
 }
48 41
 
49 42
 void noTone(const pin_t _pin) {
@@ -60,7 +53,6 @@ HAL_TONE_TIMER_ISR {
60 53
     digitalWrite(tone_pin, (pin_state ^= 1));
61 54
   }
62 55
   else noTone(tone_pin);                         // turn off interrupt
63
-  HAL_timer_restrain_count(TONE_TIMER_NUM, 10);  // make sure next ISR isn't delayed by up to 2 minutes
64 56
 }
65 57
 
66 58
 #endif // ARDUINO_ARCH_SAM

+ 3
- 2
Marlin/src/HAL/HAL_DUE/usb/ctrl_access.c 查看文件

@@ -642,5 +642,6 @@ U16 stream_stop(U8 id)
642 642
 
643 643
 //! @}
644 644
 
645
-#endif  // ACCESS_STREAM == true
646
-#endif
645
+#endif // ACCESS_STREAM
646
+
647
+#endif // ARDUINO_ARCH_SAM

+ 1
- 1
Marlin/src/HAL/HAL_DUE/usb/sysclk.c 查看文件

@@ -119,4 +119,4 @@ void sysclk_disable_usb(void)
119 119
 /**INDENT-ON**/
120 120
 /// @endcond
121 121
 
122
-#endif
122
+#endif // ARDUINO_ARCH_SAM

+ 1
- 1
Marlin/src/HAL/HAL_DUE/usb/udc.c 查看文件

@@ -1146,4 +1146,4 @@ bool udc_process_setup(void)
1146 1146
 
1147 1147
 //! @}
1148 1148
 
1149
-#endif
1149
+#endif // ARDUINO_ARCH_SAM

+ 1
- 1
Marlin/src/HAL/HAL_DUE/usb/udi_cdc.c 查看文件

@@ -1152,4 +1152,4 @@ iram_size_t udi_cdc_write_buf(const void* buf, iram_size_t size)
1152 1152
 
1153 1153
 //@}
1154 1154
 
1155
-#endif
1155
+#endif // ARDUINO_ARCH_SAM

+ 4
- 2
Marlin/src/HAL/HAL_DUE/usb/udi_cdc_desc.c 查看文件

@@ -255,5 +255,7 @@ UDC_DESC_STORAGE udc_config_t udc_config = {
255 255
 
256 256
 //@}
257 257
 //@}
258
-#endif
259
-#endif
258
+
259
+#endif // SDSUPPORT
260
+
261
+#endif // ARDUINO_ARCH_SAM

+ 3
- 2
Marlin/src/HAL/HAL_DUE/usb/udi_composite_desc.c 查看文件

@@ -187,5 +187,6 @@ UDC_DESC_STORAGE udc_config_t udc_config = {
187 187
 /**INDENT-ON**/
188 188
 //@}
189 189
 
190
-#endif
191
-#endif
190
+#endif // ARDUINO_ARCH_SAM
191
+
192
+#endif // SDSUPPORT

+ 3
- 2
Marlin/src/HAL/HAL_DUE/usb/udi_msc.c 查看文件

@@ -1127,5 +1127,6 @@ bool udi_msc_trans_block(bool b_read, uint8_t * block, iram_size_t block_size,
1127 1127
 
1128 1128
 //@}
1129 1129
 
1130
-#endif
1131
-#endif
1130
+#endif // SDSUPPORT
1131
+
1132
+#endif // ARDUINO_ARCH_SAM

+ 1
- 1
Marlin/src/HAL/HAL_DUE/usb/uotghs_device_due.c 查看文件

@@ -2070,4 +2070,4 @@ static bool udd_ep_interrupt(void)
2070 2070
 
2071 2071
 //@}
2072 2072
 
2073
-#endif
2073
+#endif // ARDUINO_ARCH_SAM

+ 0
- 1
Marlin/src/HAL/HAL_DUE/usb/uotghs_otg.h 查看文件

@@ -238,5 +238,4 @@ void otg_dual_disable(void);
238 238
 }
239 239
 #endif
240 240
 
241
-
242 241
 #endif /* UOTGHS_OTG_H_INCLUDED */

+ 10
- 3
Marlin/src/HAL/HAL_DUE/usb/usb_task.c 查看文件

@@ -56,7 +56,7 @@
56 56
 static volatile bool main_b_cdc_enable = false;
57 57
 static volatile bool main_b_dtr_active = false;
58 58
 
59
-void HAL_idletask(void) {
59
+void usb_task_idle(void) {
60 60
   #if ENABLED(SDSUPPORT)
61 61
     // Attend SD card access from the USB MSD -- Prioritize access to improve speed
62 62
     int delay = 2;
@@ -107,8 +107,15 @@ void usb_task_cdc_set_dtr(const uint8_t port, const bool b_enable) {
107 107
 
108 108
   if (1200 == dwDTERate) {
109 109
     // We check DTR state to determine if host port is open (bit 0 of lineState).
110
-    if (!b_enable)
110
+    if (!b_enable) {
111
+
112
+      // Set RST pin to go low for 65535 clock cycles on reset
113
+      //  This helps restarting when firmware flash ends
114
+      RSTC->RSTC_MR = 0xA5000F01;
115
+
116
+      // Schedule delayed reset
111 117
       initiateReset(250);
118
+    }
112 119
     else
113 120
       cancelReset();
114 121
   }
@@ -290,7 +297,7 @@ bool usb_task_other_requests(void) {
290 297
   return true;
291 298
 }
292 299
 
293
-void HAL_init(void) {
300
+void usb_task_init(void) {
294 301
 
295 302
   uint16_t *ptr;
296 303
 

+ 18
- 2
Marlin/src/HAL/HAL_DUE/usb/usb_task.h 查看文件

@@ -49,6 +49,10 @@
49 49
 
50 50
 #include "usb_protocol_cdc.h"
51 51
 
52
+#ifdef __cplusplus
53
+extern "C" {
54
+#endif
55
+
52 56
 /*! \brief Called by MSC interface
53 57
  * Callback running when USB Host enable MSC interface
54 58
  *
@@ -111,8 +115,20 @@ void usb_task_cdc_rx_notify(const uint8_t port);
111 115
  */
112 116
 void usb_task_cdc_config(const uint8_t port, usb_cdc_line_coding_t *cfg);
113 117
 
114
-/* The USB device interrupt
118
+/*! \brief The USB device interrupt
115 119
  */
116 120
 void USBD_ISR(void);
117 121
 
118
-#endif // _MAIN_H_
122
+/*! \brief USB task init
123
+ */
124
+void usb_task_init(void);
125
+
126
+/*! \brief USB task idle
127
+ */
128
+void usb_task_idle(void);
129
+
130
+#ifdef __cplusplus
131
+}
132
+#endif
133
+
134
+#endif // _USB_TASK_H_

+ 77
- 6
Marlin/src/HAL/HAL_DUE/watchdog_Due.cpp 查看文件

@@ -23,17 +23,88 @@
23 23
 #ifdef ARDUINO_ARCH_SAM
24 24
 
25 25
 #include "../../inc/MarlinConfig.h"
26
+#include "../../Marlin.h"
27
+#include "watchdog_Due.h"
26 28
 
27
-#if ENABLED(USE_WATCHDOG)
29
+// Override Arduino runtime to either config or disable the watchdog
30
+//
31
+// We need to configure the watchdog as soon as possible in the boot
32
+// process, because watchdog initialization at hardware reset on SAM3X8E
33
+// is unreliable, and there is risk of unintended resets if we delay
34
+// that initialization to a later time.
35
+void watchdogSetup(void) {
28 36
 
29
-  #include "watchdog_Due.h"
37
+  #if ENABLED(USE_WATCHDOG)
30 38
 
31
-  void watchdogSetup(void) {
32
-    // do whatever. don't remove this function.
33
-  }
39
+    // 4 seconds timeout
40
+    uint32_t timeout = 4000;
41
+
42
+    // Calculate timeout value in WDT counter ticks: This assumes
43
+    // the slow clock is running at 32.768 kHz watchdog
44
+    // frequency is therefore 32768 / 128 = 256 Hz
45
+    timeout = (timeout << 8) / 1000;
46
+    if (timeout == 0)
47
+      timeout = 1;
48
+    else if (timeout > 0xFFF)
49
+      timeout = 0xFFF;
50
+
51
+    // We want to enable the watchdog with the specified timeout
52
+    uint32_t value =
53
+      WDT_MR_WDV(timeout) |               // With the specified timeout
54
+      WDT_MR_WDD(timeout) |               // and no invalid write window
55
+    #if !(SAMV70 || SAMV71 || SAME70 || SAMS70)
56
+      WDT_MR_WDRPROC   |                  // WDT fault resets processor only - We want
57
+                                          // to keep PIO controller state
58
+    #endif
59
+      WDT_MR_WDDBGHLT  |                  // WDT stops in debug state.
60
+      WDT_MR_WDIDLEHLT;                   // WDT stops in idle state.
61
+
62
+    #if ENABLED(WATCHDOG_RESET_MANUAL)
63
+      // We enable the watchdog timer, but only for the interrupt.
64
+
65
+      // Configure WDT to only trigger an interrupt
66
+      value |= WDT_MR_WDFIEN;             // Enable WDT fault interrupt.
67
+
68
+      // Disable WDT interrupt (just in case, to avoid triggering it!)
69
+      NVIC_DisableIRQ(WDT_IRQn);
70
+
71
+      // Initialize WDT with the given parameters
72
+      WDT_Enable(WDT, value);
73
+
74
+      // Configure and enable WDT interrupt.
75
+      NVIC_ClearPendingIRQ(WDT_IRQn);
76
+      NVIC_SetPriority(WDT_IRQn, 0); // Use highest priority, so we detect all kinds of lockups
77
+      NVIC_EnableIRQ(WDT_IRQn);
34 78
 
35
-  void watchdog_init(void) { watchdogEnable(4000); }
79
+    #else
36 80
 
81
+      // a WDT fault triggers a reset
82
+      value |= WDT_MR_WDRSTEN;
83
+
84
+      // Initialize WDT with the given parameters
85
+      WDT_Enable(WDT, value);
86
+
87
+    #endif
88
+
89
+    // Reset the watchdog
90
+    WDT_Restart(WDT);
91
+
92
+  #else
93
+
94
+    // Make sure to completely disable the Watchdog
95
+    WDT_Disable(WDT);
96
+
97
+  #endif
98
+}
99
+
100
+#if ENABLED(USE_WATCHDOG)
101
+  // Initialize watchdog - On SAM3X, Watchdog was already configured
102
+  //  and enabled or disabled at startup, so no need to reconfigure it
103
+  //  here.
104
+  void watchdog_init(void) {
105
+    // Reset watchdog to start clean
106
+    WDT_Restart(WDT);
107
+  }
37 108
 #endif // USE_WATCHDOG
38 109
 
39 110
 #endif

+ 0
- 3
Marlin/src/Marlin.cpp 查看文件

@@ -648,9 +648,6 @@ void setup() {
648 648
 
649 649
   #ifdef HAL_INIT
650 650
     HAL_init();
651
-    #if defined(ARDUINO_ARCH_SAM) && PIN_EXISTS(BEEPER) && ENABLED(SPEAKER)
652
-      toneInit();
653
-    #endif
654 651
   #endif
655 652
 
656 653
   #if ENABLED(MAX7219_DEBUG)

+ 3
- 0
Marlin/src/inc/Conditionals_post.h 查看文件

@@ -1396,4 +1396,7 @@
1396 1396
   #define HAS_FOLDER_SORTING (FOLDER_SORTING || ENABLED(SDSORT_GCODE))
1397 1397
 #endif
1398 1398
 
1399
+// If platform requires early initialization of watchdog to properly boot
1400
+#define EARLY_WATCHDOG (ENABLED(USE_WATCHDOG) && defined(ARDUINO_ARCH_SAM))
1401
+
1399 1402
 #endif // CONDITIONALS_POST_H

+ 18
- 0
Marlin/src/module/temperature.cpp 查看文件

@@ -117,6 +117,10 @@ int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
117 117
 
118 118
 // private:
119 119
 
120
+#if EARLY_WATCHDOG
121
+  bool Temperature::inited = false;
122
+#endif
123
+
120 124
 #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
121 125
   uint16_t Temperature::redundant_temperature_raw = 0;
122 126
   float Temperature::redundant_temperature = 0.0;
@@ -761,6 +765,14 @@ float Temperature::get_pid_output(const int8_t e) {
761 765
  */
762 766
 void Temperature::manage_heater() {
763 767
 
768
+  #if EARLY_WATCHDOG
769
+    // If thermal manager is still not running, make sure to at least reset the watchdog!
770
+    if (!inited) {
771
+      watchdog_reset();
772
+      return;
773
+    }
774
+  #endif
775
+
764 776
   #if ENABLED(PROBING_HEATERS_OFF) && ENABLED(BED_LIMIT_SWITCHING)
765 777
     static bool last_pause_state;
766 778
   #endif
@@ -1053,6 +1065,12 @@ void Temperature::updateTemperaturesFromRawValues() {
1053 1065
  */
1054 1066
 void Temperature::init() {
1055 1067
 
1068
+  #if EARLY_WATCHDOG
1069
+    // Flag that the thermalManager should be running
1070
+    if (inited) return;
1071
+    inited = true;
1072
+  #endif
1073
+
1056 1074
   #if MB(RUMBA) && (TEMP_SENSOR_0 == -1 || TEMP_SENSOR_1 == -1 || TEMP_SENSOR_2 == -1 || TEMP_SENSOR_BED == -1)
1057 1075
     // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
1058 1076
     MCUCR = _BV(JTD);

+ 5
- 0
Marlin/src/module/temperature.h 查看文件

@@ -202,6 +202,11 @@ class Temperature {
202 202
 
203 203
   private:
204 204
 
205
+    #if EARLY_WATCHDOG
206
+      // If temperature controller is running
207
+      static bool inited;
208
+    #endif
209
+
205 210
     #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
206 211
       static uint16_t redundant_temperature_raw;
207 212
       static float redundant_temperature;

+ 15
- 0
Marlin/src/sd/Sd2Card.cpp 查看文件

@@ -260,6 +260,11 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
260 260
   // must supply min of 74 clock cycles with CS high.
261 261
   for (uint8_t i = 0; i < 10; i++) spiSend(0xFF);
262 262
 
263
+  // Initialization can cause the watchdog to timeout, so reinit it here
264
+  #if ENABLED(USE_WATCHDOG)
265
+    watchdog_reset();
266
+  #endif
267
+
263 268
   // command to go idle in SPI mode
264 269
   while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) {
265 270
     if (((uint16_t)millis() - t0) > SD_INIT_TIMEOUT) {
@@ -272,6 +277,11 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
272 277
     crcSupported = (cardCommand(CMD59, 1) == R1_IDLE_STATE);
273 278
   #endif
274 279
 
280
+  // Initialization can cause the watchdog to timeout, so reinit it here
281
+  #if ENABLED(USE_WATCHDOG)
282
+    watchdog_reset();
283
+  #endif
284
+
275 285
   // check SD version
276 286
   for (;;) {
277 287
     if (cardCommand(CMD8, 0x1AA) == (R1_ILLEGAL_COMMAND | R1_IDLE_STATE)) {
@@ -292,6 +302,11 @@ bool Sd2Card::init(uint8_t sckRateID, pin_t chipSelectPin) {
292 302
     }
293 303
   }
294 304
 
305
+  // Initialization can cause the watchdog to timeout, so reinit it here
306
+  #if ENABLED(USE_WATCHDOG)
307
+    watchdog_reset();
308
+  #endif
309
+
295 310
   // initialize card and send host supports SDHC if SD2
296 311
   arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0;
297 312
   while ((status_ = cardAcmd(ACMD41, arg)) != R1_READY_STATE) {

Loading…
取消
儲存