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.8KB

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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * HAL Timers for Teensy 4.0 (IMXRT1062DVL6A) / 4.1 (IMXRT1062DVJ6A)
  24. */
  25. #ifdef __IMXRT1062__
  26. #include "../../inc/MarlinConfig.h"
  27. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  28. switch (timer_num) {
  29. case 0:
  30. CCM_CSCMR1 &= ~CCM_CSCMR1_PERCLK_CLK_SEL; // turn off 24mhz mode
  31. CCM_CCGR1 |= CCM_CCGR1_GPT1_BUS(CCM_CCGR_ON);
  32. GPT1_CR = 0; // disable timer
  33. GPT1_SR = 0x3F; // clear all prior status
  34. GPT1_PR = GPT1_TIMER_PRESCALE - 1;
  35. GPT1_CR |= GPT_CR_CLKSRC(1); //clock selection #1 (peripheral clock = 150 MHz)
  36. GPT1_CR |= GPT_CR_ENMOD; //reset count to zero before enabling
  37. GPT1_CR |= GPT_CR_OM1(1); // toggle mode
  38. GPT1_OCR1 = (GPT1_TIMER_RATE / frequency) -1; // Initial compare value
  39. GPT1_IR = GPT_IR_OF1IE; // Compare3 value
  40. GPT1_CR |= GPT_CR_EN; //enable GPT2 counting at 150 MHz
  41. OUT_WRITE(15, HIGH);
  42. attachInterruptVector(IRQ_GPT1, &stepTC_Handler);
  43. NVIC_SET_PRIORITY(IRQ_GPT1, 16);
  44. break;
  45. case 1:
  46. CCM_CSCMR1 &= ~CCM_CSCMR1_PERCLK_CLK_SEL; // turn off 24mhz mode
  47. CCM_CCGR0 |= CCM_CCGR0_GPT2_BUS(CCM_CCGR_ON);
  48. GPT2_CR = 0; // disable timer
  49. GPT2_SR = 0x3F; // clear all prior status
  50. GPT2_PR = GPT2_TIMER_PRESCALE - 1;
  51. GPT2_CR |= GPT_CR_CLKSRC(1); //clock selection #1 (peripheral clock = 150 MHz)
  52. GPT2_CR |= GPT_CR_ENMOD; //reset count to zero before enabling
  53. GPT2_CR |= GPT_CR_OM1(1); // toggle mode
  54. GPT2_OCR1 = (GPT2_TIMER_RATE / frequency) -1; // Initial compare value
  55. GPT2_IR = GPT_IR_OF1IE; // Compare3 value
  56. GPT2_CR |= GPT_CR_EN; //enable GPT2 counting at 150 MHz
  57. OUT_WRITE(14, HIGH);
  58. attachInterruptVector(IRQ_GPT2, &tempTC_Handler);
  59. NVIC_SET_PRIORITY(IRQ_GPT2, 32);
  60. break;
  61. }
  62. }
  63. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  64. switch (timer_num) {
  65. case 0:
  66. NVIC_ENABLE_IRQ(IRQ_GPT1);
  67. break;
  68. case 1:
  69. NVIC_ENABLE_IRQ(IRQ_GPT2);
  70. break;
  71. }
  72. }
  73. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  74. switch (timer_num) {
  75. case 0: NVIC_DISABLE_IRQ(IRQ_GPT1); break;
  76. case 1: NVIC_DISABLE_IRQ(IRQ_GPT2); break;
  77. }
  78. // We NEED memory barriers to ensure Interrupts are actually disabled!
  79. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  80. asm volatile("dsb");
  81. }
  82. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  83. switch (timer_num) {
  84. case 0: return (NVIC_IS_ENABLED(IRQ_GPT1));
  85. case 1: return (NVIC_IS_ENABLED(IRQ_GPT2));
  86. }
  87. return false;
  88. }
  89. void HAL_timer_isr_prologue(const uint8_t timer_num) {
  90. switch (timer_num) {
  91. case 0:
  92. GPT1_SR = GPT_IR_OF1IE; // clear OF3 bit
  93. break;
  94. case 1:
  95. GPT2_SR = GPT_IR_OF1IE; // clear OF3 bit
  96. break;
  97. }
  98. asm volatile("dsb");
  99. }
  100. #endif // __IMXRT1062__