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.

fast_pwm.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2019 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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef __AVR__
  23. #include "../../inc/MarlinConfigPre.h"
  24. #if ENABLED(FAST_PWM_FAN)
  25. #include "HAL.h"
  26. struct Timer {
  27. volatile uint8_t* TCCRnQ[3]; // max 3 TCCR registers per timer
  28. volatile uint16_t* OCRnQ[3]; // max 3 OCR registers per timer
  29. volatile uint16_t* ICRn; // max 1 ICR register per timer
  30. uint8_t n; // the timer number [0->5]
  31. uint8_t q; // the timer output [0->2] (A->C)
  32. };
  33. /**
  34. * get_pwm_timer
  35. * Get the timer information and register of the provided pin.
  36. * Return a Timer struct containing this information.
  37. * Used by set_pwm_frequency, set_pwm_duty
  38. */
  39. Timer get_pwm_timer(const pin_t pin) {
  40. uint8_t q = 0;
  41. switch (digitalPinToTimer(pin)) {
  42. // Protect reserved timers (TIMER0 & TIMER1)
  43. #ifdef TCCR0A
  44. #if !AVR_AT90USB1286_FAMILY
  45. case TIMER0A:
  46. #endif
  47. case TIMER0B:
  48. #endif
  49. #ifdef TCCR1A
  50. case TIMER1A: case TIMER1B:
  51. #endif
  52. break;
  53. #if defined(TCCR2) || defined(TCCR2A)
  54. #ifdef TCCR2
  55. case TIMER2: {
  56. Timer timer = {
  57. /*TCCRnQ*/ { &TCCR2, nullptr, nullptr},
  58. /*OCRnQ*/ { (uint16_t*)&OCR2, nullptr, nullptr},
  59. /*ICRn*/ nullptr,
  60. /*n, q*/ 2, 0
  61. };
  62. }
  63. #elif defined TCCR2A
  64. #if ENABLED(USE_OCR2A_AS_TOP)
  65. case TIMER2A: break; // protect TIMER2A
  66. case TIMER2B: {
  67. Timer timer = {
  68. /*TCCRnQ*/ { &TCCR2A, &TCCR2B, nullptr},
  69. /*OCRnQ*/ { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr},
  70. /*ICRn*/ nullptr,
  71. /*n, q*/ 2, 1
  72. };
  73. return timer;
  74. }
  75. #else
  76. case TIMER2B: ++q;
  77. case TIMER2A: {
  78. Timer timer = {
  79. /*TCCRnQ*/ { &TCCR2A, &TCCR2B, nullptr},
  80. /*OCRnQ*/ { (uint16_t*)&OCR2A, (uint16_t*)&OCR2B, nullptr},
  81. /*ICRn*/ nullptr,
  82. 2, q
  83. };
  84. return timer;
  85. }
  86. #endif
  87. #endif
  88. #endif
  89. #ifdef TCCR3A
  90. case TIMER3C: ++q;
  91. case TIMER3B: ++q;
  92. case TIMER3A: {
  93. Timer timer = {
  94. /*TCCRnQ*/ { &TCCR3A, &TCCR3B, &TCCR3C},
  95. /*OCRnQ*/ { &OCR3A, &OCR3B, &OCR3C},
  96. /*ICRn*/ &ICR3,
  97. /*n, q*/ 3, q
  98. };
  99. return timer;
  100. }
  101. #endif
  102. #ifdef TCCR4A
  103. case TIMER4C: ++q;
  104. case TIMER4B: ++q;
  105. case TIMER4A: {
  106. Timer timer = {
  107. /*TCCRnQ*/ { &TCCR4A, &TCCR4B, &TCCR4C},
  108. /*OCRnQ*/ { &OCR4A, &OCR4B, &OCR4C},
  109. /*ICRn*/ &ICR4,
  110. /*n, q*/ 4, q
  111. };
  112. return timer;
  113. }
  114. #endif
  115. #ifdef TCCR5A
  116. case TIMER5C: ++q;
  117. case TIMER5B: ++q;
  118. case TIMER5A: {
  119. Timer timer = {
  120. /*TCCRnQ*/ { &TCCR5A, &TCCR5B, &TCCR5C},
  121. /*OCRnQ*/ { &OCR5A, &OCR5B, &OCR5C },
  122. /*ICRn*/ &ICR5,
  123. /*n, q*/ 5, q
  124. };
  125. return timer;
  126. }
  127. #endif
  128. }
  129. Timer timer = {
  130. /*TCCRnQ*/ { nullptr, nullptr, nullptr},
  131. /*OCRnQ*/ { nullptr, nullptr, nullptr},
  132. /*ICRn*/ nullptr,
  133. 0, 0
  134. };
  135. return timer;
  136. }
  137. void set_pwm_frequency(const pin_t pin, int f_desired) {
  138. Timer timer = get_pwm_timer(pin);
  139. if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
  140. uint16_t size;
  141. if (timer.n == 2) size = 255; else size = 65535;
  142. uint16_t res = 255; // resolution (TOP value)
  143. uint8_t j = 0; // prescaler index
  144. uint8_t wgm = 1; // waveform generation mode
  145. // Calculating the prescaler and resolution to use to achieve closest frequency
  146. if (f_desired != 0) {
  147. int f = (F_CPU) / (2 * 1024 * size) + 1; // Initialize frequency as lowest (non-zero) achievable
  148. uint16_t prescaler[] = { 0, 1, 8, /*TIMER2 ONLY*/32, 64, /*TIMER2 ONLY*/128, 256, 1024 };
  149. // loop over prescaler values
  150. for (uint8_t i = 1; i < 8; i++) {
  151. uint16_t res_temp_fast = 255, res_temp_phase_correct = 255;
  152. if (timer.n == 2) {
  153. // No resolution calculation for TIMER2 unless enabled USE_OCR2A_AS_TOP
  154. #if ENABLED(USE_OCR2A_AS_TOP)
  155. const uint16_t rtf = (F_CPU) / (prescaler[i] * f_desired);
  156. res_temp_fast = rtf - 1;
  157. res_temp_phase_correct = rtf / 2;
  158. #endif
  159. }
  160. else {
  161. // Skip TIMER2 specific prescalers when not TIMER2
  162. if (i == 3 || i == 5) continue;
  163. const uint16_t rtf = (F_CPU) / (prescaler[i] * f_desired);
  164. res_temp_fast = rtf - 1;
  165. res_temp_phase_correct = rtf / 2;
  166. }
  167. LIMIT(res_temp_fast, 1u, size);
  168. LIMIT(res_temp_phase_correct, 1u, size);
  169. // Calculate frequencies of test prescaler and resolution values
  170. const int f_temp_fast = (F_CPU) / (prescaler[i] * (1 + res_temp_fast)),
  171. f_temp_phase_correct = (F_CPU) / (2 * prescaler[i] * res_temp_phase_correct),
  172. f_diff = ABS(f - f_desired),
  173. f_fast_diff = ABS(f_temp_fast - f_desired),
  174. f_phase_diff = ABS(f_temp_phase_correct - f_desired);
  175. // If FAST values are closest to desired f
  176. if (f_fast_diff < f_diff && f_fast_diff <= f_phase_diff) {
  177. // Remember this combination
  178. f = f_temp_fast;
  179. res = res_temp_fast;
  180. j = i;
  181. // Set the Wave Generation Mode to FAST PWM
  182. if (timer.n == 2) {
  183. wgm = (
  184. #if ENABLED(USE_OCR2A_AS_TOP)
  185. WGM2_FAST_PWM_OCR2A
  186. #else
  187. WGM2_FAST_PWM
  188. #endif
  189. );
  190. }
  191. else wgm = WGM_FAST_PWM_ICRn;
  192. }
  193. // If PHASE CORRECT values are closes to desired f
  194. else if (f_phase_diff < f_diff) {
  195. f = f_temp_phase_correct;
  196. res = res_temp_phase_correct;
  197. j = i;
  198. // Set the Wave Generation Mode to PWM PHASE CORRECT
  199. if (timer.n == 2) {
  200. wgm = (
  201. #if ENABLED(USE_OCR2A_AS_TOP)
  202. WGM2_PWM_PC_OCR2A
  203. #else
  204. WGM2_PWM_PC
  205. #endif
  206. );
  207. }
  208. else wgm = WGM_PWM_PC_ICRn;
  209. }
  210. }
  211. }
  212. _SET_WGMnQ(timer.TCCRnQ, wgm);
  213. _SET_CSn(timer.TCCRnQ, j);
  214. if (timer.n == 2) {
  215. #if ENABLED(USE_OCR2A_AS_TOP)
  216. _SET_OCRnQ(timer.OCRnQ, 0, res); // Set OCR2A value (TOP) = res
  217. #endif
  218. }
  219. else
  220. _SET_ICRn(timer.ICRn, res); // Set ICRn value (TOP) = res
  221. }
  222. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
  223. // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
  224. // Note that digitalWrite also disables pwm output for us (sets COM bit to 0)
  225. if (v == 0)
  226. digitalWrite(pin, invert);
  227. else if (v == v_size)
  228. digitalWrite(pin, !invert);
  229. else {
  230. Timer timer = get_pwm_timer(pin);
  231. if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
  232. // Set compare output mode to CLEAR -> SET or SET -> CLEAR (if inverted)
  233. _SET_COMnQ(timer.TCCRnQ, (timer.q
  234. #ifdef TCCR2
  235. + (timer.q == 2) // COM20 is on bit 4 of TCCR2, thus requires q + 1 in the macro
  236. #endif
  237. ), COM_CLEAR_SET + invert
  238. );
  239. uint16_t top;
  240. if (timer.n == 2) { // if TIMER2
  241. top = (
  242. #if ENABLED(USE_OCR2A_AS_TOP)
  243. *timer.OCRnQ[0] // top = OCR2A
  244. #else
  245. 255 // top = 0xFF (max)
  246. #endif
  247. );
  248. }
  249. else
  250. top = *timer.ICRn; // top = ICRn
  251. _SET_OCRnQ(timer.OCRnQ, timer.q, v * float(top / v_size)); // Scale 8/16-bit v to top value
  252. }
  253. }
  254. #endif // FAST_PWM_FAN
  255. #endif // __AVR__