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

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