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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/MarlinConfigPre.h"
  24. #if NEEDS_HARDWARE_PWM // Specific meta-flag for features that mandate PWM
  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 OCR3C
  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. #elif defined(OCR3B)
  102. case TIMER3B: ++q;
  103. case TIMER3A: {
  104. Timer timer = {
  105. /*TCCRnQ*/ { &TCCR3A, &TCCR3B, nullptr },
  106. /*OCRnQ*/ { &OCR3A, &OCR3B, nullptr },
  107. /*ICRn*/ &ICR3,
  108. /*n, q*/ 3, q
  109. };
  110. return timer;
  111. }
  112. #endif
  113. #ifdef TCCR4A
  114. case TIMER4C: ++q;
  115. case TIMER4B: ++q;
  116. case TIMER4A: {
  117. Timer timer = {
  118. /*TCCRnQ*/ { &TCCR4A, &TCCR4B, &TCCR4C },
  119. /*OCRnQ*/ { &OCR4A, &OCR4B, &OCR4C },
  120. /*ICRn*/ &ICR4,
  121. /*n, q*/ 4, q
  122. };
  123. return timer;
  124. }
  125. #endif
  126. #ifdef TCCR5A
  127. case TIMER5C: ++q;
  128. case TIMER5B: ++q;
  129. case TIMER5A: {
  130. Timer timer = {
  131. /*TCCRnQ*/ { &TCCR5A, &TCCR5B, &TCCR5C },
  132. /*OCRnQ*/ { &OCR5A, &OCR5B, &OCR5C },
  133. /*ICRn*/ &ICR5,
  134. /*n, q*/ 5, q
  135. };
  136. return timer;
  137. }
  138. #endif
  139. }
  140. Timer timer = {
  141. /*TCCRnQ*/ { nullptr, nullptr, nullptr },
  142. /*OCRnQ*/ { nullptr, nullptr, nullptr },
  143. /*ICRn*/ nullptr,
  144. 0, 0
  145. };
  146. return timer;
  147. }
  148. void set_pwm_frequency(const pin_t pin, int f_desired) {
  149. Timer timer = get_pwm_timer(pin);
  150. if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
  151. uint16_t size;
  152. if (timer.n == 2) size = 255; else size = 65535;
  153. uint16_t res = 255; // resolution (TOP value)
  154. uint8_t j = 0; // prescaler index
  155. uint8_t wgm = 1; // waveform generation mode
  156. // Calculating the prescaler and resolution to use to achieve closest frequency
  157. if (f_desired != 0) {
  158. int f = (F_CPU) / (2 * 1024 * size) + 1; // Initialize frequency as lowest (non-zero) achievable
  159. uint16_t prescaler[] = { 0, 1, 8, /*TIMER2 ONLY*/32, 64, /*TIMER2 ONLY*/128, 256, 1024 };
  160. // loop over prescaler values
  161. LOOP_S_L_N(i, 1, 8) {
  162. uint16_t res_temp_fast = 255, res_temp_phase_correct = 255;
  163. if (timer.n == 2) {
  164. // No resolution calculation for TIMER2 unless enabled USE_OCR2A_AS_TOP
  165. #if ENABLED(USE_OCR2A_AS_TOP)
  166. const uint16_t rtf = (F_CPU) / (prescaler[i] * f_desired);
  167. res_temp_fast = rtf - 1;
  168. res_temp_phase_correct = rtf / 2;
  169. #endif
  170. }
  171. else {
  172. // Skip TIMER2 specific prescalers when not TIMER2
  173. if (i == 3 || i == 5) continue;
  174. const uint16_t rtf = (F_CPU) / (prescaler[i] * f_desired);
  175. res_temp_fast = rtf - 1;
  176. res_temp_phase_correct = rtf / 2;
  177. }
  178. LIMIT(res_temp_fast, 1u, size);
  179. LIMIT(res_temp_phase_correct, 1u, size);
  180. // Calculate frequencies of test prescaler and resolution values
  181. const int f_temp_fast = (F_CPU) / (prescaler[i] * (1 + res_temp_fast)),
  182. f_temp_phase_correct = (F_CPU) / (2 * prescaler[i] * res_temp_phase_correct),
  183. f_diff = ABS(f - f_desired),
  184. f_fast_diff = ABS(f_temp_fast - f_desired),
  185. f_phase_diff = ABS(f_temp_phase_correct - f_desired);
  186. // If FAST values are closest to desired f
  187. if (f_fast_diff < f_diff && f_fast_diff <= f_phase_diff) {
  188. // Remember this combination
  189. f = f_temp_fast;
  190. res = res_temp_fast;
  191. j = i;
  192. // Set the Wave Generation Mode to FAST PWM
  193. if (timer.n == 2) {
  194. wgm = (
  195. #if ENABLED(USE_OCR2A_AS_TOP)
  196. WGM2_FAST_PWM_OCR2A
  197. #else
  198. WGM2_FAST_PWM
  199. #endif
  200. );
  201. }
  202. else wgm = WGM_FAST_PWM_ICRn;
  203. }
  204. // If PHASE CORRECT values are closes to desired f
  205. else if (f_phase_diff < f_diff) {
  206. f = f_temp_phase_correct;
  207. res = res_temp_phase_correct;
  208. j = i;
  209. // Set the Wave Generation Mode to PWM PHASE CORRECT
  210. if (timer.n == 2) {
  211. wgm = (
  212. #if ENABLED(USE_OCR2A_AS_TOP)
  213. WGM2_PWM_PC_OCR2A
  214. #else
  215. WGM2_PWM_PC
  216. #endif
  217. );
  218. }
  219. else wgm = WGM_PWM_PC_ICRn;
  220. }
  221. }
  222. }
  223. _SET_WGMnQ(timer.TCCRnQ, wgm);
  224. _SET_CSn(timer.TCCRnQ, j);
  225. if (timer.n == 2) {
  226. #if ENABLED(USE_OCR2A_AS_TOP)
  227. _SET_OCRnQ(timer.OCRnQ, 0, res); // Set OCR2A value (TOP) = res
  228. #endif
  229. }
  230. else
  231. _SET_ICRn(timer.ICRn, res); // Set ICRn value (TOP) = res
  232. }
  233. void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
  234. // If v is 0 or v_size (max), digitalWrite to LOW or HIGH.
  235. // Note that digitalWrite also disables pwm output for us (sets COM bit to 0)
  236. if (v == 0)
  237. digitalWrite(pin, invert);
  238. else if (v == v_size)
  239. digitalWrite(pin, !invert);
  240. else {
  241. Timer timer = get_pwm_timer(pin);
  242. if (timer.n == 0) return; // Don't proceed if protected timer or not recognised
  243. // Set compare output mode to CLEAR -> SET or SET -> CLEAR (if inverted)
  244. _SET_COMnQ(timer.TCCRnQ, (timer.q
  245. #ifdef TCCR2
  246. + (timer.q == 2) // COM20 is on bit 4 of TCCR2, thus requires q + 1 in the macro
  247. #endif
  248. ), COM_CLEAR_SET + invert
  249. );
  250. uint16_t top;
  251. if (timer.n == 2) { // if TIMER2
  252. top = (
  253. #if ENABLED(USE_OCR2A_AS_TOP)
  254. *timer.OCRnQ[0] // top = OCR2A
  255. #else
  256. 255 // top = 0xFF (max)
  257. #endif
  258. );
  259. }
  260. else
  261. top = *timer.ICRn; // top = ICRn
  262. _SET_OCRnQ(timer.OCRnQ, timer.q, v * float(top) / float(v_size)); // Scale 8/16-bit v to top value
  263. }
  264. }
  265. #endif // NEEDS_HARDWARE_PWM
  266. #endif // __AVR__