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.

HAL_timers_Due.cpp 5.2KB

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