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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #pragma once
  20. #include <stdint.h>
  21. // ------------------------
  22. // Types
  23. // ------------------------
  24. typedef uint16_t hal_timer_t;
  25. #define HAL_TIMER_TYPE_MAX 0xFFFF
  26. // ------------------------
  27. // Defines
  28. // ------------------------
  29. #define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz
  30. #ifndef STEP_TIMER_NUM
  31. #define STEP_TIMER_NUM 1
  32. #endif
  33. #ifndef PULSE_TIMER_NUM
  34. #define PULSE_TIMER_NUM STEP_TIMER_NUM
  35. #endif
  36. #ifndef TEMP_TIMER_NUM
  37. #define TEMP_TIMER_NUM 0
  38. #endif
  39. #define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0)
  40. #define STEPPER_TIMER_RATE HAL_TIMER_RATE
  41. #define STEPPER_TIMER_PRESCALE 8
  42. #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
  43. #define PULSE_TIMER_RATE STEPPER_TIMER_RATE // frequency of pulse timer
  44. #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
  45. #define PULSE_TIMER_TICKS_PER_US STEPPER_TIMER_TICKS_PER_US
  46. #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
  47. #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
  48. #define STEPPER_ISR_ENABLED() TEST(TIMSK1, OCIE1A)
  49. #define ENABLE_TEMPERATURE_INTERRUPT() SBI(TIMSK0, OCIE0B)
  50. #define DISABLE_TEMPERATURE_INTERRUPT() CBI(TIMSK0, OCIE0B)
  51. #define TEMPERATURE_ISR_ENABLED() TEST(TIMSK0, OCIE0B)
  52. FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
  53. switch (timer_num) {
  54. case STEP_TIMER_NUM:
  55. // waveform generation = 0100 = CTC
  56. SET_WGM(1, CTC_OCRnA);
  57. // output mode = 00 (disconnected)
  58. SET_COMA(1, NORMAL);
  59. // Set the timer pre-scaler
  60. // Generally we use a divider of 8, resulting in a 2MHz timer
  61. // frequency on a 16MHz MCU. If you are going to change this, be
  62. // sure to regenerate speed_lookuptable.h with
  63. // create_speed_lookuptable.py
  64. SET_CS(1, PRESCALER_8); // CS 2 = 1/8 prescaler
  65. // Init Stepper ISR to 122 Hz for quick starting
  66. // (F_CPU) / (STEPPER_TIMER_PRESCALE) / frequency
  67. OCR1A = 0x4000;
  68. TCNT1 = 0;
  69. break;
  70. case TEMP_TIMER_NUM:
  71. // Use timer0 for temperature measurement
  72. // Interleave temperature interrupt with millies interrupt
  73. OCR0B = 128;
  74. break;
  75. }
  76. }
  77. #define TIMER_OCR_1 OCR1A
  78. #define TIMER_COUNTER_1 TCNT1
  79. #define TIMER_OCR_0 OCR0A
  80. #define TIMER_COUNTER_0 TCNT0
  81. #define _CAT(a,V...) a##V
  82. #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
  83. #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
  84. #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
  85. /**
  86. * On AVR there is no hardware prioritization and preemption of
  87. * interrupts, so this emulates it. The UART has first priority
  88. * (otherwise, characters will be lost due to UART overflow).
  89. * Then: Stepper, Endstops, Temperature, and -finally- all others.
  90. */
  91. #define HAL_timer_isr_prologue(TIMER_NUM)
  92. #define HAL_timer_isr_epilogue(TIMER_NUM)
  93. /* 18 cycles maximum latency */
  94. #ifndef HAL_STEP_TIMER_ISR
  95. #define HAL_STEP_TIMER_ISR() \
  96. extern "C" void TIMER1_COMPA_vect() __attribute__ ((signal, naked, used, externally_visible)); \
  97. extern "C" void TIMER1_COMPA_vect_bottom() asm ("TIMER1_COMPA_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  98. void TIMER1_COMPA_vect() { \
  99. __asm__ __volatile__ ( \
  100. A("push r16") /* 2 Save R16 */ \
  101. A("in r16, __SREG__") /* 1 Get SREG */ \
  102. A("push r16") /* 2 Save SREG into stack */ \
  103. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  104. A("push r16") /* 2 Save TIMSK0 into the stack */ \
  105. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  106. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  107. A("lds r16, %[timsk1]") /* 2 Load into R0 the stepper timer Interrupt mask register [TIMSK1] */ \
  108. A("andi r16,~%[msk1]") /* 1 Disable the stepper ISR */ \
  109. A("sts %[timsk1], r16") /* 2 And set the new value */ \
  110. A("push r16") /* 2 Save TIMSK1 into stack */ \
  111. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  112. A("push r16") /* 2 Save RAMPZ into stack */ \
  113. A("in r16, 0x3C") /* 1 Get EIND register */ \
  114. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  115. A("push r1") \
  116. A("push r18") \
  117. A("push r19") \
  118. A("push r20") \
  119. A("push r21") \
  120. A("push r22") \
  121. A("push r23") \
  122. A("push r24") \
  123. A("push r25") \
  124. A("push r26") \
  125. A("push r27") \
  126. A("push r30") \
  127. A("push r31") \
  128. A("clr r1") /* C runtime expects this register to be 0 */ \
  129. A("call TIMER1_COMPA_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  130. A("pop r31") \
  131. A("pop r30") \
  132. A("pop r27") \
  133. A("pop r26") \
  134. A("pop r25") \
  135. A("pop r24") \
  136. A("pop r23") \
  137. A("pop r22") \
  138. A("pop r21") \
  139. A("pop r20") \
  140. A("pop r19") \
  141. A("pop r18") \
  142. A("pop r1") \
  143. A("pop r0") \
  144. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  145. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  146. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  147. A("pop r16") /* 2 Get the original TIMSK1 value but with stepper ISR disabled */ \
  148. A("ori r16,%[msk1]") /* 1 Reenable the stepper ISR */ \
  149. A("cli") /* 1 Disable global interrupts - Reenabling Stepper ISR can reenter amd temperature can reenter, and we want that, if it happens, after this ISR has ended */ \
  150. A("sts %[timsk1], r16") /* 2 And restore the old value - This reenables the stepper ISR */ \
  151. A("pop r16") /* 2 Get the temperature timer Interrupt mask register [TIMSK0] */ \
  152. A("sts %[timsk0], r16") /* 2 And restore the old value - This reenables the temperature ISR */ \
  153. A("pop r16") /* 2 Get the old SREG value */ \
  154. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  155. A("pop r16") /* 2 Restore R16 value */ \
  156. A("reti") /* 4 Return from interrupt */ \
  157. : \
  158. : [timsk0] "i" ((uint16_t)&TIMSK0), \
  159. [timsk1] "i" ((uint16_t)&TIMSK1), \
  160. [msk0] "M" ((uint8_t)(1<<OCIE0B)),\
  161. [msk1] "M" ((uint8_t)(1<<OCIE1A)) \
  162. : \
  163. ); \
  164. } \
  165. void TIMER1_COMPA_vect_bottom()
  166. #endif // HAL_STEP_TIMER_ISR
  167. #ifndef HAL_TEMP_TIMER_ISR
  168. /* 14 cycles maximum latency */
  169. #define HAL_TEMP_TIMER_ISR() \
  170. extern "C" void TIMER0_COMPB_vect() __attribute__ ((signal, naked, used, externally_visible)); \
  171. extern "C" void TIMER0_COMPB_vect_bottom() asm ("TIMER0_COMPB_vect_bottom") __attribute__ ((used, externally_visible, noinline)); \
  172. void TIMER0_COMPB_vect() { \
  173. __asm__ __volatile__ ( \
  174. A("push r16") /* 2 Save R16 */ \
  175. A("in r16, __SREG__") /* 1 Get SREG */ \
  176. A("push r16") /* 2 Save SREG into stack */ \
  177. A("lds r16, %[timsk0]") /* 2 Load into R0 the Temperature timer Interrupt mask register */ \
  178. A("andi r16,~%[msk0]") /* 1 Disable the temperature ISR */ \
  179. A("sts %[timsk0], r16") /* 2 And set the new value */ \
  180. A("sei") /* 1 Enable global interrupts - It is safe, as the temperature ISR is disabled, so we cannot reenter it */ \
  181. A("push r16") /* 2 Save TIMSK0 into stack */ \
  182. A("in r16, 0x3B") /* 1 Get RAMPZ register */ \
  183. A("push r16") /* 2 Save RAMPZ into stack */ \
  184. A("in r16, 0x3C") /* 1 Get EIND register */ \
  185. A("push r0") /* C runtime can modify all the following registers without restoring them */ \
  186. A("push r1") \
  187. A("push r18") \
  188. A("push r19") \
  189. A("push r20") \
  190. A("push r21") \
  191. A("push r22") \
  192. A("push r23") \
  193. A("push r24") \
  194. A("push r25") \
  195. A("push r26") \
  196. A("push r27") \
  197. A("push r30") \
  198. A("push r31") \
  199. A("clr r1") /* C runtime expects this register to be 0 */ \
  200. A("call TIMER0_COMPB_vect_bottom") /* Call the bottom handler - No inlining allowed, otherwise registers used are not saved */ \
  201. A("pop r31") \
  202. A("pop r30") \
  203. A("pop r27") \
  204. A("pop r26") \
  205. A("pop r25") \
  206. A("pop r24") \
  207. A("pop r23") \
  208. A("pop r22") \
  209. A("pop r21") \
  210. A("pop r20") \
  211. A("pop r19") \
  212. A("pop r18") \
  213. A("pop r1") \
  214. A("pop r0") \
  215. A("out 0x3C, r16") /* 1 Restore EIND register */ \
  216. A("pop r16") /* 2 Get the original RAMPZ register value */ \
  217. A("out 0x3B, r16") /* 1 Restore RAMPZ register to its original value */ \
  218. A("pop r16") /* 2 Get the original TIMSK0 value but with temperature ISR disabled */ \
  219. A("ori r16,%[msk0]") /* 1 Enable temperature ISR */ \
  220. A("cli") /* 1 Disable global interrupts - We must do this, as we will reenable the temperature ISR, and we don't want to reenter this handler until the current one is done */ \
  221. A("sts %[timsk0], r16") /* 2 And restore the old value */ \
  222. A("pop r16") /* 2 Get the old SREG */ \
  223. A("out __SREG__, r16") /* 1 And restore the SREG value */ \
  224. A("pop r16") /* 2 Restore R16 */ \
  225. A("reti") /* 4 Return from interrupt */ \
  226. : \
  227. : [timsk0] "i"((uint16_t)&TIMSK0), \
  228. [msk0] "M" ((uint8_t)(1<<OCIE0B)) \
  229. : \
  230. ); \
  231. } \
  232. void TIMER0_COMPB_vect_bottom()
  233. #endif // HAL_TEMP_TIMER_ISR