My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HAL_timers_SAMD51.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * SAMD51 HAL developed by Giuliano Zaro (AKA GMagician)
  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. #include <stdint.h>
  23. // --------------------------------------------------------------------------
  24. // Defines
  25. // --------------------------------------------------------------------------
  26. typedef uint32_t hal_timer_t;
  27. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
  28. #define HAL_TIMER_RATE SystemCoreClock // frequency of timers peripherals
  29. #define STEP_TIMER_NUM 0 // index of timer to use for stepper (also +1 for 32bits counter)
  30. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  31. #define TONE_TIMER_NUM 2 // index of timer to use for beeper tones (also +1 for 32bits counter)
  32. #define TEMP_TIMER_NUM 4 // index of timer to use for temperature (also +1 for 32bits counter)
  33. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  34. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  35. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  36. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  37. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
  38. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  39. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  40. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  41. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  42. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  43. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  44. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  45. #define TC_PRIORITY(t) (t == STEP_TIMER_NUM || t == PULSE_TIMER_NUM) ? 2 \
  46. : (t == TEMP_TIMER_NUM) ? 6 \
  47. : (t == TONE_TIMER_NUM) ? 5 : 7
  48. #define _TC_HANDLER(t) void TC##t##_Handler()
  49. #define TC_HANDLER(t) _TC_HANDLER(t)
  50. #define HAL_STEP_TIMER_ISR() TC_HANDLER(STEP_TIMER_NUM)
  51. #if STEP_TIMER_NUM != PULSE_TIMER_NUM
  52. #define HAL_PULSE_TIMER_ISR() TC_HANDLER(PULSE_TIMER_NUM)
  53. #endif
  54. #define HAL_TEMP_TIMER_ISR() TC_HANDLER(TEMP_TIMER_NUM)
  55. #define HAL_TONE_TIMER_ISR() TC_HANDLER(TONE_TIMER_NUM)
  56. // --------------------------------------------------------------------------
  57. // Types
  58. // --------------------------------------------------------------------------
  59. typedef struct {
  60. Tc *pTimer;
  61. IRQn_Type IRQ_Id;
  62. uint8_t priority;
  63. } tTimerConfig;
  64. // --------------------------------------------------------------------------
  65. // Public Variables
  66. // --------------------------------------------------------------------------
  67. extern const tTimerConfig TimerConfig[];
  68. // --------------------------------------------------------------------------
  69. // Public functions
  70. // --------------------------------------------------------------------------
  71. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  72. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  73. Tc * const tc = TimerConfig[timer_num].pTimer;
  74. tc->COUNT32.CC[0].reg = HAL_TIMER_TYPE_MAX - compare;
  75. }
  76. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  77. Tc * const tc = TimerConfig[timer_num].pTimer;
  78. return (hal_timer_t)(HAL_TIMER_TYPE_MAX - tc->COUNT32.CC[0].reg);
  79. }
  80. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  81. Tc * const tc = TimerConfig[timer_num].pTimer;
  82. tc->COUNT32.CTRLBSET.reg = TC_CTRLBCLR_CMD_READSYNC;
  83. SYNC(tc->COUNT32.SYNCBUSY.bit.CTRLB || tc->COUNT32.SYNCBUSY.bit.COUNT);
  84. return HAL_TIMER_TYPE_MAX - tc->COUNT32.COUNT.reg;
  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. Tc * const tc = TimerConfig[timer_num].pTimer;
  91. // Clear interrupt flag
  92. tc->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF;
  93. }
  94. #define HAL_timer_isr_epilogue(timer_num)