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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 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 F_CPU // 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 TEMP_TIMER_NUM 4 // index of timer to use for temperature (also +1 for 32bits counter)
  32. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  33. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  34. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  35. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  36. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
  37. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  38. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  39. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  40. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  41. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  42. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  43. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  44. #define TC_PRIORITY(t) (t == STEP_TIMER_NUM || t == PULSE_TIMER_NUM) ? 2 \
  45. : (t == TEMP_TIMER_NUM) ? 6 \
  46. : 7
  47. #define _TC_HANDLER(t) void TC##t##_Handler()
  48. #define TC_HANDLER(t) _TC_HANDLER(t)
  49. #define HAL_STEP_TIMER_ISR() TC_HANDLER(STEP_TIMER_NUM)
  50. #if STEP_TIMER_NUM != PULSE_TIMER_NUM
  51. #define HAL_PULSE_TIMER_ISR() TC_HANDLER(PULSE_TIMER_NUM)
  52. #endif
  53. #define HAL_TEMP_TIMER_ISR() TC_HANDLER(TEMP_TIMER_NUM)
  54. // --------------------------------------------------------------------------
  55. // Types
  56. // --------------------------------------------------------------------------
  57. typedef struct {
  58. Tc *pTimer;
  59. IRQn_Type IRQ_Id;
  60. uint8_t priority;
  61. } tTimerConfig;
  62. // --------------------------------------------------------------------------
  63. // Public Variables
  64. // --------------------------------------------------------------------------
  65. extern const tTimerConfig TimerConfig[];
  66. // --------------------------------------------------------------------------
  67. // Public functions
  68. // --------------------------------------------------------------------------
  69. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  70. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  71. Tc * const tc = TimerConfig[timer_num].pTimer;
  72. tc->COUNT32.CC[0].reg = HAL_TIMER_TYPE_MAX - compare;
  73. }
  74. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  75. Tc * const tc = TimerConfig[timer_num].pTimer;
  76. return (hal_timer_t)(HAL_TIMER_TYPE_MAX - tc->COUNT32.CC[0].reg);
  77. }
  78. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  79. Tc * const tc = TimerConfig[timer_num].pTimer;
  80. tc->COUNT32.CTRLBSET.reg = TC_CTRLBCLR_CMD_READSYNC;
  81. SYNC(tc->COUNT32.SYNCBUSY.bit.CTRLB || tc->COUNT32.SYNCBUSY.bit.COUNT);
  82. return HAL_TIMER_TYPE_MAX - tc->COUNT32.COUNT.reg;
  83. }
  84. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  85. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  86. bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
  87. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  88. Tc * const tc = TimerConfig[timer_num].pTimer;
  89. // Clear interrupt flag
  90. tc->COUNT32.INTFLAG.reg = TC_INTFLAG_OVF;
  91. }
  92. #define HAL_timer_isr_epilogue(timer_num)