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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 TimerConfig [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 = TimerConfig[timer_num].pTimerRegs;
  60. IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
  61. uint32_t channel = TimerConfig[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, TimerConfig [timer_num].priority);
  75. // wave mode, reset counter on match with RC,
  76. TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1);
  77. // Set compare value
  78. TC_SetRC(tc, channel, VARIANT_MCK / 2 / frequency);
  79. // And start timer
  80. TC_Start(tc, channel);
  81. // enable interrupt on RC compare
  82. tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
  83. // Finally, enable IRQ
  84. NVIC_EnableIRQ(irq);
  85. }
  86. void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  87. IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
  88. NVIC_EnableIRQ(irq);
  89. }
  90. void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  91. IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
  92. NVIC_DisableIRQ(irq);
  93. // We NEED memory barriers to ensure Interrupts are actually disabled!
  94. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  95. __DSB();
  96. __ISB();
  97. }
  98. // missing from CMSIS: Check if interrupt is enabled or not
  99. static bool NVIC_GetEnabledIRQ(IRQn_Type IRQn) {
  100. return (NVIC->ISER[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F))) != 0;
  101. }
  102. bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  103. IRQn_Type irq = TimerConfig[timer_num].IRQ_Id;
  104. return NVIC_GetEnabledIRQ(irq);
  105. }
  106. #endif // ARDUINO_ARCH_SAM