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

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