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.

InterruptVectors_Due.cpp 3.1KB

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