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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  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 Arduino Due and compatible (SAM3X8E)
  24. */
  25. #ifdef ARDUINO_ARCH_SAM
  26. // ------------------------
  27. // Includes
  28. // ------------------------
  29. #include "../../inc/MarlinConfig.h"
  30. #include "HAL.h"
  31. // ------------------------
  32. // Local defines
  33. // ------------------------
  34. #define NUM_HARDWARE_TIMERS 9
  35. // ------------------------
  36. // Private Variables
  37. // ------------------------
  38. const tTimerConfig timer_config[NUM_HARDWARE_TIMERS] = {
  39. { TC0, 0, TC0_IRQn, 3}, // 0 - [servo timer5]
  40. { TC0, 1, TC1_IRQn, 0}, // 1
  41. { TC0, 2, TC2_IRQn, 2}, // 2 - stepper
  42. { TC1, 0, TC3_IRQn, 0}, // 3 - stepper for BOARD_ARCHIM1
  43. { TC1, 1, TC4_IRQn, 15}, // 4 - temperature
  44. { TC1, 2, TC5_IRQn, 3}, // 5 - [servo timer3]
  45. { TC2, 0, TC6_IRQn, 14}, // 6 - tone
  46. { TC2, 1, TC7_IRQn, 0}, // 7
  47. { TC2, 2, TC8_IRQn, 0}, // 8
  48. };
  49. // ------------------------
  50. // Public functions
  51. // ------------------------
  52. /*
  53. Timer_clock1: Prescaler 2 -> 42MHz
  54. Timer_clock2: Prescaler 8 -> 10.5MHz
  55. Timer_clock3: Prescaler 32 -> 2.625MHz
  56. Timer_clock4: Prescaler 128 -> 656.25kHz
  57. */
  58. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
  59. Tc *tc = timer_config[timer_num].pTimerRegs;
  60. IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  61. uint32_t channel = timer_config[timer_num].channel;
  62. // Disable interrupt, just in case it was already enabled
  63. NVIC_DisableIRQ(irq);
  64. // We NEED memory barriers to ensure Interrupts are actually disabled!
  65. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  66. __DSB();
  67. __ISB();
  68. // Disable timer interrupt
  69. tc->TC_CHANNEL[channel].TC_IDR = TC_IDR_CPCS;
  70. // Stop timer, just in case, to be able to reconfigure it
  71. TC_Stop(tc, channel);
  72. pmc_set_writeprotect(false);
  73. pmc_enable_periph_clk((uint32_t)irq);
  74. NVIC_SetPriority(irq, timer_config[timer_num].priority);
  75. // wave mode, reset counter on match with RC,
  76. TC_Configure(tc, channel,
  77. TC_CMR_WAVE
  78. | TC_CMR_WAVSEL_UP_RC
  79. | (HAL_TIMER_PRESCALER == 2 ? TC_CMR_TCCLKS_TIMER_CLOCK1 : 0)
  80. | (HAL_TIMER_PRESCALER == 8 ? TC_CMR_TCCLKS_TIMER_CLOCK2 : 0)
  81. | (HAL_TIMER_PRESCALER == 32 ? TC_CMR_TCCLKS_TIMER_CLOCK3 : 0)
  82. | (HAL_TIMER_PRESCALER == 128 ? TC_CMR_TCCLKS_TIMER_CLOCK4 : 0)
  83. );
  84. // Set compare value
  85. TC_SetRC(tc, channel, VARIANT_MCK / (HAL_TIMER_PRESCALER) / frequency);
  86. // And start timer
  87. TC_Start(tc, channel);
  88. // enable interrupt on RC compare
  89. tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
  90. // Finally, enable IRQ
  91. NVIC_EnableIRQ(irq);
  92. }
  93. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  94. IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  95. NVIC_EnableIRQ(irq);
  96. }
  97. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  98. IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  99. NVIC_DisableIRQ(irq);
  100. // We NEED memory barriers to ensure Interrupts are actually disabled!
  101. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  102. __DSB();
  103. __ISB();
  104. }
  105. // missing from CMSIS: Check if interrupt is enabled or not
  106. static bool NVIC_GetEnabledIRQ(IRQn_Type IRQn) {
  107. return TEST(NVIC->ISER[uint32_t(IRQn) >> 5], uint32_t(IRQn) & 0x1F);
  108. }
  109. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  110. IRQn_Type irq = timer_config[timer_num].IRQ_Id;
  111. return NVIC_GetEnabledIRQ(irq);
  112. }
  113. #endif // ARDUINO_ARCH_SAM