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

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