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.

timers.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * HAL Timers for Teensy 3.5 (MK64FX512) and Teensy 3.6 (MK66FX1M0)
  24. */
  25. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  26. #include "../../inc/MarlinConfig.h"
  27. /** \brief Instruction Synchronization Barrier
  28. Instruction Synchronization Barrier flushes the pipeline in the processor,
  29. so that all instructions following the ISB are fetched from cache or
  30. memory, after the instruction has been completed.
  31. */
  32. FORCE_INLINE static void __ISB() {
  33. __asm__ __volatile__("isb 0xF":::"memory");
  34. }
  35. /** \brief Data Synchronization Barrier
  36. This function acts as a special kind of Data Memory Barrier.
  37. It completes when all explicit memory accesses before this instruction complete.
  38. */
  39. FORCE_INLINE static void __DSB() {
  40. __asm__ __volatile__("dsb 0xF":::"memory");
  41. }
  42. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  43. switch (timer_num) {
  44. case 0:
  45. FTM0_MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
  46. FTM0_SC = 0x00; // Set this to zero before changing the modulus
  47. FTM0_CNT = 0x0000; // Reset the count to zero
  48. FTM0_MOD = 0xFFFF; // max modulus = 65535
  49. FTM0_C0V = (FTM0_TIMER_RATE) / frequency; // Initial FTM Channel 0 compare value
  50. FTM0_SC = (FTM_SC_CLKS(0b1) & FTM_SC_CLKS_MASK) | (FTM_SC_PS(FTM0_TIMER_PRESCALE_BITS) & FTM_SC_PS_MASK); // Bus clock 60MHz divided by prescaler 8
  51. FTM0_C0SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSA;
  52. break;
  53. case 1:
  54. FTM1_MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; // Disable write protection, Enable FTM1
  55. FTM1_SC = 0x00; // Set this to zero before changing the modulus
  56. FTM1_CNT = 0x0000; // Reset the count to zero
  57. FTM1_MOD = 0xFFFF; // max modulus = 65535
  58. FTM1_C0V = (FTM1_TIMER_RATE) / frequency; // Initial FTM Channel 0 compare value 65535
  59. FTM1_SC = (FTM_SC_CLKS(0b1) & FTM_SC_CLKS_MASK) | (FTM_SC_PS(FTM1_TIMER_PRESCALE_BITS) & FTM_SC_PS_MASK); // Bus clock 60MHz divided by prescaler 4
  60. FTM1_C0SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSA;
  61. break;
  62. }
  63. }
  64. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  65. switch (timer_num) {
  66. case 0: NVIC_ENABLE_IRQ(IRQ_FTM0); break;
  67. case 1: NVIC_ENABLE_IRQ(IRQ_FTM1); break;
  68. }
  69. }
  70. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  71. switch (timer_num) {
  72. case 0: NVIC_DISABLE_IRQ(IRQ_FTM0); break;
  73. case 1: NVIC_DISABLE_IRQ(IRQ_FTM1); break;
  74. }
  75. // We NEED memory barriers to ensure Interrupts are actually disabled!
  76. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  77. __DSB();
  78. __ISB();
  79. }
  80. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  81. switch (timer_num) {
  82. case 0: return NVIC_IS_ENABLED(IRQ_FTM0);
  83. case 1: return NVIC_IS_ENABLED(IRQ_FTM1);
  84. }
  85. return false;
  86. }
  87. void HAL_timer_isr_prologue(const uint8_t timer_num) {
  88. switch (timer_num) {
  89. case 0:
  90. FTM0_CNT = 0x0000;
  91. FTM0_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag
  92. FTM0_C0SC &= ~FTM_CSC_CHF; // Clear FTM Channel Compare flag
  93. break;
  94. case 1:
  95. FTM1_CNT = 0x0000;
  96. FTM1_SC &= ~FTM_SC_TOF; // Clear FTM Overflow flag
  97. FTM1_C0SC &= ~FTM_CSC_CHF; // Clear FTM Channel Compare flag
  98. break;
  99. }
  100. }
  101. #endif // Teensy3.5 or Teensy3.6