Quellcode durchsuchen

Add a module for Due SRAM-based Interrupt Vector Table

This is the ONLY way to be able to override ISRs stolen by the Arduino Runtime, such as Serial or USB device interrupts. This feature is needed so Arduino modules can be replaced and enhanced.
etagle vor 6 Jahren
Ursprung
Commit
8d9c3cc2b7

+ 95
- 0
Marlin/src/HAL/HAL_DUE/InterruptVectors_Due.cpp Datei anzeigen

@@ -0,0 +1,95 @@
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
+/**
24
+ * InterruptVectors_Due.cpp - This module relocates the Interrupt vector table to SRAM,
25
+ *  allowing to register new interrupt handlers at runtime. Specially valuable and needed
26
+ * because Arduino runtime allocates some interrupt handlers that we NEED to override to
27
+ * properly support extended functionality, as for example, USB host or USB device (MSD, MTP)
28
+ * and custom serial port handlers, and we don't actually want to modify and/or recompile the
29
+ * Arduino runtime. We just want to run as much as possible on Stock Arduino
30
+ *
31
+ * Copyright (c) 2017 Eduardo José Tagle. All right reserved
32
+ */
33
+#ifdef ARDUINO_ARCH_SAM
34
+
35
+#include "HAL_Due.h"
36
+#include "InterruptVectors_Due.h"
37
+
38
+/* The relocated Exception/Interrupt Table - Must be aligned to 128bytes,
39
+   as bits 0-6 on VTOR register are reserved and must be set to 0 */
40
+__attribute__ ((aligned(128)))
41
+static DeviceVectors ram_tab = { NULL };
42
+
43
+/**
44
+ * This function checks if the exception/interrupt table is already in SRAM or not.
45
+ * If it is not, then it copies the ROM table to the SRAM and relocates the table
46
+ * by reprogramming the NVIC registers
47
+ */
48
+static pfnISR_Handler* get_relocated_table_addr(void) {
49
+  // Get the address of the interrupt/exception table
50
+  uint32_t isrtab = SCB->VTOR;
51
+
52
+  // If already relocated, we are done!
53
+  if (isrtab >= IRAM0_ADDR)
54
+    return (pfnISR_Handler*)isrtab;
55
+
56
+  // Get the address of the table stored in FLASH
57
+  const pfnISR_Handler* romtab = (const pfnISR_Handler*)isrtab;
58
+
59
+  // Copy it to SRAM
60
+  memcpy(&ram_tab, romtab, sizeof(ram_tab));
61
+
62
+  // Disable global interrupts
63
+  CRITICAL_SECTION_START;
64
+
65
+  // Set the vector table base address to the SRAM copy
66
+  SCB->VTOR = (uint32_t)(&ram_tab);
67
+
68
+  // Reenable interrupts
69
+  CRITICAL_SECTION_END;
70
+
71
+  // Return the address of the table
72
+  return (pfnISR_Handler*)(&ram_tab);
73
+}
74
+
75
+pfnISR_Handler install_isr(IRQn_Type irq, pfnISR_Handler newHandler) {
76
+  // Get the address of the relocated table
77
+  const pfnISR_Handler *isrtab = get_relocated_table_addr();
78
+
79
+  // Disable global interrupts
80
+  CRITICAL_SECTION_START;
81
+
82
+  // Get the original handler
83
+  pfnISR_Handler oldHandler = isrtab[irq + 16];
84
+
85
+  // Install the new one
86
+  isrtab[irq + 16] = newHandler;
87
+
88
+  // Reenable interrupts
89
+  CRITICAL_SECTION_END;
90
+
91
+  // Return the original one
92
+  return oldHandler;
93
+}
94
+
95
+#endif

+ 52
- 0
Marlin/src/HAL/HAL_DUE/InterruptVectors_Due.h Datei anzeigen

@@ -0,0 +1,52 @@
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
+/**
24
+ * InterruptVectors_Due.h
25
+ *
26
+ * Copyright (c) 2017 Eduardo José Tagle. All right reserved
27
+ *
28
+ * This module relocates the Interrupt vector table to SRAM, allowing new
29
+ * interrupt handlers to be added at runtime. This is required because the
30
+ * Arduino runtime steals interrupt handlers that Marlin MUST use to support
31
+ * extended functionality such as USB hosts and USB devices (MSD, MTP) and
32
+ * custom serial port handlers. Rather than modifying and/or recompiling the
33
+ * Arduino runtime, We just want to run as much as possible on Stock Arduino.
34
+ *
35
+ * Copyright (c) 2017 Eduardo José Tagle. All right reserved
36
+ */
37
+
38
+#ifndef INTERRUPTVECTORS_DUE_H
39
+#define INTERRUPTVECTORS_DUE_H
40
+
41
+#include "../../inc/MarlinConfig.h"
42
+
43
+#ifdef ARDUINO_ARCH_SAM
44
+
45
+// ISR handler type
46
+typedef void (*pfnISR_Handler)(void);
47
+
48
+// Install a new interrupt vector handler for the given irq, returning the old one
49
+pfnISR_Handler install_isr(IRQn_Type irq, pfnISR_Handler newHandler);
50
+
51
+#endif // ARDUINO_ARCH_SAM
52
+#endif // INTERRUPTVECTORS_DUE_H

Laden…
Abbrechen
Speichern