My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

fast_pwm.cpp 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef __AVR__
  23. #include "../../inc/MarlinConfig.h"
  24. struct Timer {
  25. volatile uint8_t* TCCRnQ[3]; // max 3 TCCR registers per timer
  26. volatile uint16_t* OCRnQ[3]; // max 3 OCR registers per timer
  27. volatile uint16_t* ICRn; // max 1 ICR register per timer
  28. uint8_t n; // the timer number [0->5]
  29. uint8_t q; // the timer output [0->2] (A->C)
  30. bool isPWM; // True if pin is a "hardware timer"
  31. bool isProtected; // True if timer is protected
  32. };
  33. // Macros for the Timer structure
  34. #define _SET_WGMnQ(T, V) do{ \
  35. *(T.TCCRnQ)[0] = (*(T.TCCRnQ)[0] & ~(0x3 << 0)) | (( int(V) & 0x3) << 0); \
  36. *(T.TCCRnQ)[1] = (*(T.TCCRnQ)[1] & ~(0x3 << 3)) | (((int(V) >> 2) & 0x3) << 3); \
  37. }while(0)
  38. // Set TCCR CS bits
  39. #define _SET_CSn(T, V) (*(T.TCCRnQ)[1] = (*(T.TCCRnQ[1]) & ~(0x7 << 0)) | ((int(V) & 0x7) << 0))
  40. // Set TCCR COM bits
  41. #define _SET_COMnQ(T, Q, V) (*(T.TCCRnQ)[0] = (*(T.TCCRnQ)[0] & ~(0x3 << (6-2*(Q)))) | (int(V) << (6-2*(Q))))
  42. // Set OCRnQ register
  43. #define _SET_OCRnQ(T, Q, V) (*(T.OCRnQ)[Q] = int(V) & 0xFFFF)
  44. // Set ICRn register (one per timer)
  45. #define _SET_ICRn(T, V) (*(T.ICRn) = int(V) & 0xFFFF)
  46. /**
  47. * Return a Timer struct describing a pin's timer.
  48. */
  49. const Timer get_pwm_timer(const pin_t pin) {
  50. uint8_t q = 0;
  51. switch (digitalPinToTimer(pin)) {
  52. #ifdef TCCR0A
  53. IF_DISABLED(AVR_AT90USB1286_FAMILY, case TIMER0A:)
  54. #endif
  55. #ifdef TCCR1A
  56. case TIMER1A: case TIMER1B:
  57. #endif
  58. break; // Protect reserved timers (TIMER0 & TIMER1)
  59. #ifdef TCCR0A
  60. case TIMER0B: // Protected timer, but allow setting the duty cycle on OCR0B for pin D4 only
  61. return Timer({ { &TCCR0A, nullptr, nullptr }, { (uint16_t*)&OCR0A, (uint16_t*)&OCR0B, nullptr }, nullptr, 0, 1, true, true });
  62. #endif
  63. #if HAS_TCCR2
  64. case TIMER2:
  65. return Timer({ { &TCCR2, nullptr, nullptr }, { (uint16_t*)&OCR2, nullptr, nullptr }, nullptr, 2, 0, true, false });
  66. #elif ENABLED(USE_OCR2A_AS_TOP)
  67. case TIMER2A: break; // Protect TIMER2A since its OCR is used by TIMER2B
  68. case TIMER2B:
  69. return Timer({ { &TCCR2A, &TCCR2B, nullptr }, { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr }, nullptr, 2, 1, true, false });
  70. #elif defined(TCCR2A)
  71. case TIMER2B: ++q; case TIMER2A:
  72. return Timer({ { &TCCR2A, &TCCR2B, nullptr }, { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr }, nullptr, 2, q, true, false });
  73. #endif
  74. #ifdef OCR3C
  75. case TIMER3C: ++q; case TIMER3B: ++q; case TIMER3A:
  76. return Timer({ { &TCCR3A, &TCCR3B, &TCCR3C }, { &OCR3A, &OCR3B, &OCR3C }, &ICR3, 3, q, true, false });
  77. #elif defined(OCR3B)
  78. case TIMER3B: ++q; case TIMER3A:
  79. return Timer({ { &TCCR3A, &TCCR3B, nullptr }, { &OCR3A, &OCR3B, nullptr }, &ICR3, 3, q, true, false });
  80. #endif
  81. #ifdef TCCR4A
  82. case TIMER4C: ++q; case TIMER4B: ++q; case TIMER4A:
  83. return Timer({ { &TCCR4A, &TCCR4B, &TCCR4C }, { &OCR4A, &OCR4B, &OCR4C }, &ICR4, 4, q, true, false });
  84. #endif
  85. #ifdef TCCR5A
  86. case TIMER5C: ++q; case TIMER5B: ++q; case TIMER5A:
  87. return Timer({ { &TCCR5A, &TCCR5B, &TCCR5C }, { &OCR5A, &OCR5B, &OCR5C }, &ICR5, 5, q, true, false });
  88. #endif
  89. }
  90. return Timer();
  91. }
  92. void MarlinHAL::set_pwm_frequency(const pin_t pin, const uint16_t f_desired) {
  93. const Timer timer = get_pwm_timer(pin);
  94. if (timer.isProtected || !timer.isPWM) return; // Don't proceed if protected timer or not recognized
  95. const bool is_timer2 = timer.n == 2;
  96. const uint16_t maxtop = is_timer2 ? 0xFF : 0xFFFF;
  97. uint16_t res = 0xFF; // resolution (TOP value)
  98. uint8_t j = CS_NONE; // prescaler index
  99. uint8_t wgm = WGM_PWM_PC_8; // waveform generation mode
  100. // Calculating the prescaler and resolution to use to achieve closest frequency
  101. if (f_desired != 0) {
  102. constexpr uint16_t prescaler[] = { 1, 8, (32), 64, (128), 256, 1024 }; // (*) are Timer 2 only
  103. uint16_t f = (F_CPU) / (2 * 1024 * maxtop) + 1; // Start with the lowest non-zero frequency achievable (1 or 31)
  104. LOOP_L_N(i, COUNT(prescaler)) { // Loop through all prescaler values
  105. const uint16_t p = prescaler[i];
  106. uint16_t res_fast_temp, res_pc_temp;
  107. if (is_timer2) {
  108. #if ENABLED(USE_OCR2A_AS_TOP) // No resolution calculation for TIMER2 unless enabled USE_OCR2A_AS_TOP
  109. const uint16_t rft = (F_CPU) / (p * f_desired);
  110. res_fast_temp = rft - 1;
  111. res_pc_temp = rft / 2;
  112. #else
  113. res_fast_temp = res_pc_temp = maxtop;
  114. #endif
  115. }
  116. else {
  117. if (p == 32 || p == 128) continue; // Skip TIMER2 specific prescalers when not TIMER2
  118. const uint16_t rft = (F_CPU) / (p * f_desired);
  119. res_fast_temp = rft - 1;
  120. res_pc_temp = rft / 2;
  121. }
  122. LIMIT(res_fast_temp, 1U, maxtop);
  123. LIMIT(res_pc_temp, 1U, maxtop);
  124. // Calculate frequencies of test prescaler and resolution values
  125. const uint32_t f_diff = _MAX(f, f_desired) - _MIN(f, f_desired),
  126. f_fast_temp = (F_CPU) / (p * (1 + res_fast_temp)),
  127. f_fast_diff = _MAX(f_fast_temp, f_desired) - _MIN(f_fast_temp, f_desired),
  128. f_pc_temp = (F_CPU) / (2 * p * res_pc_temp),
  129. f_pc_diff = _MAX(f_pc_temp, f_desired) - _MIN(f_pc_temp, f_desired);
  130. if (f_fast_diff < f_diff && f_fast_diff <= f_pc_diff) { // FAST values are closest to desired f
  131. // Set the Wave Generation Mode to FAST PWM
  132. wgm = is_timer2 ? uint8_t(TERN(USE_OCR2A_AS_TOP, WGM2_FAST_PWM_OCR2A, WGM2_FAST_PWM)) : uint8_t(WGM_FAST_PWM_ICRn);
  133. // Remember this combination
  134. f = f_fast_temp; res = res_fast_temp; j = i + 1;
  135. }
  136. else if (f_pc_diff < f_diff) { // PHASE CORRECT values are closes to desired f
  137. // Set the Wave Generation Mode to PWM PHASE CORRECT
  138. wgm = is_timer2 ? uint8_t(TERN(USE_OCR2A_AS_TOP, WGM2_PWM_PC_OCR2A, WGM2_PWM_PC)) : uint8_t(WGM_PWM_PC_ICRn);
  139. f = f_pc_temp; res = res_pc_temp; j = i + 1;
  140. }
  141. }
  142. }
  143. _SET_WGMnQ(timer, wgm);
  144. _SET_CSn(timer, j);
  145. if (is_timer2) {
  146. TERN_(USE_OCR2A_AS_TOP, _SET_OCRnQ(timer, 0, res)); // Set OCR2A value (TOP) = res
  147. }
  148. else
  149. _SET_ICRn(timer, res); // Set ICRn value (TOP) = res
  150. }
  151. void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
  152. // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
  153. // Note that digitalWrite also disables PWM output for us (sets COM bit to 0)
  154. if (v == 0)
  155. digitalWrite(pin, invert);
  156. else if (v == v_size)
  157. digitalWrite(pin, !invert);
  158. else {
  159. const Timer timer = get_pwm_timer(pin);
  160. if (timer.isPWM) {
  161. if (timer.n == 0) {
  162. _SET_COMnQ(timer, timer.q, COM_CLEAR_SET); // Only allow a TIMER0B select...
  163. _SET_OCRnQ(timer, timer.q, v); // ...and OCR0B duty update. For output pin D4 no frequency changes are permitted.
  164. }
  165. else if (!timer.isProtected) {
  166. const uint16_t top = timer.n == 2 ? TERN(USE_OCR2A_AS_TOP, *timer.OCRnQ[0], 255) : *timer.ICRn;
  167. _SET_COMnQ(timer, SUM_TERN(HAS_TCCR2, timer.q, timer.q == 2), COM_CLEAR_SET + invert); // COM20 is on bit 4 of TCCR2, so +1 for q==2
  168. _SET_OCRnQ(timer, timer.q, uint16_t(uint32_t(v) * top / v_size)); // Scale 8/16-bit v to top value
  169. }
  170. }
  171. else
  172. digitalWrite(pin, v < v_size / 2 ? LOW : HIGH);
  173. }
  174. }
  175. void MarlinHAL::init_pwm_timers() {
  176. // Init some timer frequencies to a default 1KHz
  177. const pin_t pwm_pin[] = {
  178. #ifdef __AVR_ATmega2560__
  179. 10, 5, 6, 46
  180. #elif defined(__AVR_ATmega1280__)
  181. 12, 31
  182. #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega1284__)
  183. 15, 6
  184. #elif defined(__AVR_AT90USB1286__) || defined(__AVR_mega64) || defined(__AVR_mega128)
  185. 16, 24
  186. #endif
  187. };
  188. LOOP_L_N(i, COUNT(pwm_pin))
  189. set_pwm_frequency(pwm_pin[i], 1000);
  190. }
  191. #endif // __AVR__