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

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