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 3.9KB

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