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.5KB

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