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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #pragma once
  22. /**
  23. * HAL For LPC1768
  24. */
  25. #include <stdint.h>
  26. #include "../../core/macros.h"
  27. #define SBIT_TIMER0 1
  28. #define SBIT_TIMER1 2
  29. #define SBIT_CNTEN 0
  30. #define SBIT_MR0I 0 // Timer 0 Interrupt when TC matches MR0
  31. #define SBIT_MR0R 1 // Timer 0 Reset TC on Match
  32. #define SBIT_MR0S 2 // Timer 0 Stop TC and PC on Match
  33. #define SBIT_MR1I 3
  34. #define SBIT_MR1R 4
  35. #define SBIT_MR1S 5
  36. #define SBIT_MR2I 6
  37. #define SBIT_MR2R 7
  38. #define SBIT_MR2S 8
  39. #define SBIT_MR3I 9
  40. #define SBIT_MR3R 10
  41. #define SBIT_MR3S 11
  42. // ------------------------
  43. // Defines
  44. // ------------------------
  45. #define _HAL_TIMER(T) _CAT(LPC_TIM, T)
  46. #define _HAL_TIMER_IRQ(T) TIMER##T##_IRQn
  47. #define __HAL_TIMER_ISR(T) extern "C" void TIMER##T##_IRQHandler()
  48. #define _HAL_TIMER_ISR(T) __HAL_TIMER_ISR(T)
  49. typedef uint32_t hal_timer_t;
  50. #define HAL_TIMER_TYPE_MAX 0xFFFFFFFF
  51. #define HAL_TIMER_RATE ((F_CPU) / 4) // frequency of timers peripherals
  52. #ifndef STEP_TIMER_NUM
  53. #define STEP_TIMER_NUM 0 // Timer Index for Stepper
  54. #endif
  55. #ifndef PULSE_TIMER_NUM
  56. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  57. #endif
  58. #ifndef TEMP_TIMER_NUM
  59. #define TEMP_TIMER_NUM 1 // Timer Index for Temperature
  60. #endif
  61. #ifndef PWM_TIMER_NUM
  62. #define PWM_TIMER_NUM 3 // Timer Index for PWM
  63. #endif
  64. #define TEMP_TIMER_RATE 1000000
  65. #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
  66. #define STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
  67. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
  68. #define STEPPER_TIMER_PRESCALE (CYCLES_PER_MICROSECOND / STEPPER_TIMER_TICKS_PER_US)
  69. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  70. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  71. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  72. #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
  73. #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
  74. #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
  75. #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
  76. #define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
  77. #ifndef HAL_STEP_TIMER_ISR
  78. #define HAL_STEP_TIMER_ISR() _HAL_TIMER_ISR(STEP_TIMER_NUM)
  79. #endif
  80. #ifndef HAL_TEMP_TIMER_ISR
  81. #define HAL_TEMP_TIMER_ISR() _HAL_TIMER_ISR(TEMP_TIMER_NUM)
  82. #endif
  83. // Timer references by index
  84. #define STEP_TIMER_PTR _HAL_TIMER(STEP_TIMER_NUM)
  85. #define TEMP_TIMER_PTR _HAL_TIMER(TEMP_TIMER_NUM)
  86. // ------------------------
  87. // Public functions
  88. // ------------------------
  89. void HAL_timer_init();
  90. void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency);
  91. FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const hal_timer_t compare) {
  92. switch (timer_num) {
  93. case 0: STEP_TIMER_PTR->MR0 = compare; break; // Stepper Timer Match Register 0
  94. case 1: TEMP_TIMER_PTR->MR0 = compare; break; // Temp Timer Match Register 0
  95. }
  96. }
  97. FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
  98. switch (timer_num) {
  99. case 0: return STEP_TIMER_PTR->MR0; // Stepper Timer Match Register 0
  100. case 1: return TEMP_TIMER_PTR->MR0; // Temp Timer Match Register 0
  101. }
  102. return 0;
  103. }
  104. FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
  105. switch (timer_num) {
  106. case 0: return STEP_TIMER_PTR->TC; // Stepper Timer Count
  107. case 1: return TEMP_TIMER_PTR->TC; // Temp Timer Count
  108. }
  109. return 0;
  110. }
  111. FORCE_INLINE static void HAL_timer_enable_interrupt(const uint8_t timer_num) {
  112. switch (timer_num) {
  113. case 0: NVIC_EnableIRQ(TIMER0_IRQn); break; // Enable interrupt handler
  114. case 1: NVIC_EnableIRQ(TIMER1_IRQn); break; // Enable interrupt handler
  115. }
  116. }
  117. FORCE_INLINE static void HAL_timer_disable_interrupt(const uint8_t timer_num) {
  118. switch (timer_num) {
  119. case 0: NVIC_DisableIRQ(TIMER0_IRQn); break; // Disable interrupt handler
  120. case 1: NVIC_DisableIRQ(TIMER1_IRQn); break; // Disable interrupt handler
  121. }
  122. // We NEED memory barriers to ensure Interrupts are actually disabled!
  123. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  124. __DSB();
  125. __ISB();
  126. }
  127. // This function is missing from CMSIS
  128. FORCE_INLINE static bool NVIC_GetEnableIRQ(IRQn_Type IRQn) {
  129. return (NVIC->ISER[((uint32_t)IRQn) >> 5] & (1 << ((uint32_t)IRQn) & 0x1F)) != 0;
  130. }
  131. FORCE_INLINE static bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
  132. switch (timer_num) {
  133. case 0: return NVIC_GetEnableIRQ(TIMER0_IRQn); // Check if interrupt is enabled or not
  134. case 1: return NVIC_GetEnableIRQ(TIMER1_IRQn); // Check if interrupt is enabled or not
  135. }
  136. return false;
  137. }
  138. FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
  139. switch (timer_num) {
  140. case 0: SBI(STEP_TIMER_PTR->IR, SBIT_CNTEN); break;
  141. case 1: SBI(TEMP_TIMER_PTR->IR, SBIT_CNTEN); break;
  142. }
  143. }
  144. #define HAL_timer_isr_epilogue(TIMER_NUM)