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

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