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.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #pragma once
  22. /**
  23. * HAL for Arduino Due and compatible (SAM3X8E)
  24. *
  25. * For ARDUINO_ARCH_SAM
  26. */
  27. // --------------------------------------------------------------------------
  28. // Includes
  29. // --------------------------------------------------------------------------
  30. #include <stdint.h>
  31. // --------------------------------------------------------------------------
  32. // Defines
  33. // --------------------------------------------------------------------------
  34. #define FORCE_INLINE __attribute__((always_inline)) inline
  35. typedef uint32_t hal_timer_t;
  36. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
  37. #define HAL_TIMER_RATE ((F_CPU) / 2) // frequency of timers peripherals
  38. #define STEP_TIMER_NUM 2 // index of timer to use for stepper
  39. #define TEMP_TIMER_NUM 4 // index of timer to use for temperature
  40. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  41. #define TONE_TIMER_NUM 6 // index of timer to use for beeper tones
  42. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  43. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  44. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  45. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  46. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  47. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  48. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  49. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  50. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  51. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  52. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  53. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  54. #define HAL_STEP_TIMER_ISR() void TC2_Handler()
  55. #define HAL_TEMP_TIMER_ISR() void TC4_Handler()
  56. #define HAL_TONE_TIMER_ISR() void TC6_Handler()
  57. // --------------------------------------------------------------------------
  58. // Types
  59. // --------------------------------------------------------------------------
  60. typedef struct {
  61. Tc *pTimerRegs;
  62. uint16_t channel;
  63. IRQn_Type IRQ_Id;
  64. uint8_t priority;
  65. } tTimerConfig;
  66. // --------------------------------------------------------------------------
  67. // Public Variables
  68. // --------------------------------------------------------------------------
  69. extern const tTimerConfig TimerConfig[];
  70. // --------------------------------------------------------------------------
  71. // Public functions
  72. // --------------------------------------------------------------------------
  73. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  74. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  75. const tTimerConfig * const pConfig = &TimerConfig[timer_num];
  76. pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC = compare;
  77. }
  78. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  79. const tTimerConfig * const pConfig = &TimerConfig[timer_num];
  80. return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC;
  81. }
  82. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  83. const tTimerConfig * const pConfig = &TimerConfig[timer_num];
  84. return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
  85. }
  86. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  87. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  88. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  89. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  90. const tTimerConfig * const pConfig = &TimerConfig[timer_num];
  91. // Reading the status register clears the interrupt flag
  92. pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_SR;
  93. }
  94. #define HAL_timer_isr_epilogue(TIMER_NUM)