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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (C) 2016 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. /**
  22. * HAL for Arduino Due and compatible (SAM3X8E)
  23. *
  24. * For ARDUINO_ARCH_SAM
  25. */
  26. #ifndef _HAL_TIMERS_H
  27. #define _HAL_TIMERS_H
  28. // --------------------------------------------------------------------------
  29. // Includes
  30. // --------------------------------------------------------------------------
  31. #include <stdint.h>
  32. // --------------------------------------------------------------------------
  33. // Defines
  34. // --------------------------------------------------------------------------
  35. #define FORCE_INLINE __attribute__((always_inline)) inline
  36. typedef uint32_t timer_t;
  37. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
  38. #define STEP_TIMER_NUM 0 // index of timer to use for stepper
  39. #define TEMP_TIMER_NUM 1 // index of timer to use for temperature
  40. #define HAL_TIMER_RATE ((SystemCoreClock) / 4) // frequency of timers peripherals
  41. #define STEPPER_TIMER_PRESCALE 1.0 // prescaler for setting stepper frequency
  42. #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  43. #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
  44. #define HAL_TEMP_TIMER_RATE 1000000
  45. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  46. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  47. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  48. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  49. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  50. #define HAL_ENABLE_ISRs() do { if (thermalManager.in_temp_isr) DISABLE_TEMPERATURE_INTERRUPT(); else ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
  51. #define HAL_STEP_TIMER_ISR extern "C" void TIMER0_IRQHandler(void)
  52. #define HAL_TEMP_TIMER_ISR extern "C" void TIMER1_IRQHandler(void)
  53. // --------------------------------------------------------------------------
  54. // Types
  55. // --------------------------------------------------------------------------
  56. // --------------------------------------------------------------------------
  57. // Public Variables
  58. // --------------------------------------------------------------------------
  59. // --------------------------------------------------------------------------
  60. // Public functions
  61. // --------------------------------------------------------------------------
  62. void HAL_timer_init(void);
  63. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  64. static FORCE_INLINE void HAL_timer_set_count(const uint8_t timer_num, const timer_t count) {
  65. switch (timer_num) {
  66. case 0:
  67. LPC_TIM0->MR0 = count;
  68. if (LPC_TIM0->TC > count)
  69. LPC_TIM0->TC = count - 5; // generate an immediate stepper ISR
  70. break;
  71. case 1:
  72. LPC_TIM1->MR0 = count;
  73. if (LPC_TIM1->TC > count)
  74. LPC_TIM1->TC = count - 5; // make sure we don't have one extra long period
  75. break;
  76. }
  77. }
  78. static FORCE_INLINE timer_t HAL_timer_get_count(const uint8_t timer_num) {
  79. switch (timer_num) {
  80. case 0: return LPC_TIM0->MR0;
  81. case 1: return LPC_TIM1->MR0;
  82. }
  83. return 0;
  84. }
  85. static FORCE_INLINE timer_t HAL_timer_get_current_count(const uint8_t timer_num) {
  86. switch (timer_num) {
  87. case 0: return LPC_TIM0->TC;
  88. case 1: return LPC_TIM1->TC;
  89. }
  90. return 0;
  91. }
  92. void HAL_timer_enable_interrupt(const uint8_t timer_num);
  93. void HAL_timer_disable_interrupt(const uint8_t timer_num);
  94. void HAL_timer_isr_prologue(const uint8_t timer_num);
  95. #endif // _HAL_TIMERS_DUE_H