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.

G2_PWM.cpp 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  23. * The PWM module is only used to generate interrupts at specified times. It
  24. * is NOT used to directly toggle pins. The ISR writes to the pin assigned to
  25. * that interrupt.
  26. *
  27. * All PWMs use the same repetition rate. The G2 needs about 10KHz min in order to
  28. * not have obvious ripple on the Vref signals.
  29. *
  30. * The data structures are setup to minimize the computation done by the ISR which
  31. * minimizes ISR execution time. Execution times are 0.8 to 1.1 microseconds.
  32. *
  33. * FIve PWM interrupt sources are used. Channel 0 sets the base period. All Vref
  34. * signals are set active when this counter overflows and resets to zero. The compare
  35. * values in channels 1-4 are set to give the desired duty cycle for that Vref pin.
  36. * When counter 0 matches the compare value then that channel generates an interrupt.
  37. * The ISR checks the source of the interrupt and sets the corresponding pin inactive.
  38. *
  39. * Some jitter in the Vref signal is OK so the interrupt priority is left at its default value.
  40. */
  41. #include "../../../inc/MarlinConfig.h"
  42. #if MB(PRINTRBOARD_G2)
  43. #include "G2_PWM.h"
  44. #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
  45. #define G2_PWM_X 1
  46. #else
  47. #define G2_PWM_X 0
  48. #endif
  49. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
  50. #define G2_PWM_Y 1
  51. #else
  52. #define G2_PWM_Y 0
  53. #endif
  54. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
  55. #define G2_PWM_Z 1
  56. #else
  57. #define G2_PWM_Z 0
  58. #endif
  59. #if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
  60. #define G2_PWM_E 1
  61. #else
  62. #define G2_PWM_E 0
  63. #endif
  64. #define G2_MASK_X(V) (G2_PWM_X * (V))
  65. #define G2_MASK_Y(V) (G2_PWM_Y * (V))
  66. #define G2_MASK_Z(V) (G2_PWM_Z * (V))
  67. #define G2_MASK_E(V) (G2_PWM_E * (V))
  68. volatile uint32_t *SODR_A = &PIOA->PIO_SODR,
  69. *SODR_B = &PIOB->PIO_SODR,
  70. *CODR_A = &PIOA->PIO_CODR,
  71. *CODR_B = &PIOB->PIO_CODR;
  72. PWM_map ISR_table[NUM_PWMS] = PWM_MAP_INIT;
  73. void Stepper::digipot_init() {
  74. #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
  75. OUT_WRITE(MOTOR_CURRENT_PWM_X_PIN, 0); // init pins
  76. #endif
  77. #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
  78. OUT_WRITE(MOTOR_CURRENT_PWM_Y_PIN, 0);
  79. #endif
  80. #if G2_PWM_Z
  81. OUT_WRITE(MOTOR_CURRENT_PWM_Z_PIN, 0);
  82. #endif
  83. #if G2_PWM_E
  84. OUT_WRITE(MOTOR_CURRENT_PWM_E_PIN, 0);
  85. #endif
  86. #define WPKEY (0x50574D << 8) // “PWM” in ASCII
  87. #define WPCMD_DIS_SW 0 // command to disable Write Protect SW
  88. #define WPRG_ALL (PWM_WPCR_WPRG0 | PWM_WPCR_WPRG1 | PWM_WPCR_WPRG2 | PWM_WPCR_WPRG3 | PWM_WPCR_WPRG4 | PWM_WPCR_WPRG5) // all Write Protect Groups
  89. #define PWM_CLOCK_F F_CPU / 1000000UL // set clock to 1MHz
  90. PMC->PMC_PCER1 = PMC_PCER1_PID36; // enable PWM controller clock (disabled on power up)
  91. PWM->PWM_WPCR = WPKEY | WPRG_ALL | WPCMD_DIS_SW; // enable setting of all PWM registers
  92. PWM->PWM_CLK = PWM_CLOCK_F; // enable CLK_A and set it to 1MHz, leave CLK_B disabled
  93. PWM->PWM_CH_NUM[0].PWM_CMR = 0b1011; // set channel 0 to Clock A input & to left aligned
  94. if (G2_PWM_X) PWM->PWM_CH_NUM[1].PWM_CMR = 0b1011; // set channel 1 to Clock A input & to left aligned
  95. if (G2_PWM_Y) PWM->PWM_CH_NUM[2].PWM_CMR = 0b1011; // set channel 2 to Clock A input & to left aligned
  96. if (G2_PWM_Z) PWM->PWM_CH_NUM[3].PWM_CMR = 0b1011; // set channel 3 to Clock A input & to left aligned
  97. if (G2_PWM_E) PWM->PWM_CH_NUM[4].PWM_CMR = 0b1011; // set channel 4 to Clock A input & to left aligned
  98. PWM->PWM_CH_NUM[0].PWM_CPRD = PWM_PERIOD_US; // set channel 0 Period
  99. PWM->PWM_IER2 = PWM_IER1_CHID0; // generate interrupt when counter0 overflows
  100. PWM->PWM_IER2 = PWM_IER2_CMPM0
  101. | G2_MASK_X(PWM_IER2_CMPM1)
  102. | G2_MASK_Y(PWM_IER2_CMPM2)
  103. | G2_MASK_Z(PWM_IER2_CMPM3)
  104. | G2_MASK_E(PWM_IER2_CMPM4)
  105. ; // generate interrupt on compare event
  106. if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPV = 0x010000000LL | G2_VREF_COUNT(G2_VREF(motor_current_setting[0])); // interrupt when counter0 == CMPV - used to set Motor 1 PWM inactive
  107. if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPV = 0x010000000LL | G2_VREF_COUNT(G2_VREF(motor_current_setting[0])); // interrupt when counter0 == CMPV - used to set Motor 2 PWM inactive
  108. if (G2_PWM_Z) PWM->PWM_CMP[3].PWM_CMPV = 0x010000000LL | G2_VREF_COUNT(G2_VREF(motor_current_setting[1])); // interrupt when counter0 == CMPV - used to set Motor 3 PWM inactive
  109. if (G2_PWM_E) PWM->PWM_CMP[4].PWM_CMPV = 0x010000000LL | G2_VREF_COUNT(G2_VREF(motor_current_setting[2])); // interrupt when counter0 == CMPV - used to set Motor 4 PWM inactive
  110. if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPM = 0x0001; // enable compare event
  111. if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPM = 0x0001; // enable compare event
  112. if (G2_PWM_Z) PWM->PWM_CMP[3].PWM_CMPM = 0x0001; // enable compare event
  113. if (G2_PWM_E) PWM->PWM_CMP[4].PWM_CMPM = 0x0001; // enable compare event
  114. PWM->PWM_SCM = PWM_SCM_UPDM_MODE0 | PWM_SCM_SYNC0
  115. | G2_MASK_X(PWM_SCM_SYNC1)
  116. | G2_MASK_Y(PWM_SCM_SYNC2)
  117. | G2_MASK_Z(PWM_SCM_SYNC3)
  118. | G2_MASK_E(PWM_SCM_SYNC4)
  119. ; // sync 1-4 with 0, use mode 0 for updates
  120. PWM->PWM_ENA = PWM_ENA_CHID0
  121. | G2_MASK_X(PWM_ENA_CHID1)
  122. | G2_MASK_Y(PWM_ENA_CHID2)
  123. | G2_MASK_Z(PWM_ENA_CHID3)
  124. | G2_MASK_E(PWM_ENA_CHID4)
  125. ; // enable channels used by G2
  126. PWM->PWM_IER1 = PWM_IER1_CHID0
  127. | G2_MASK_X(PWM_IER1_CHID1)
  128. | G2_MASK_Y(PWM_IER1_CHID2)
  129. | G2_MASK_Z(PWM_IER1_CHID3)
  130. | G2_MASK_E(PWM_IER1_CHID4)
  131. ; // enable interrupts for channels used by G2
  132. NVIC_EnableIRQ(PWM_IRQn); // Enable interrupt handler
  133. NVIC_SetPriority(PWM_IRQn, NVIC_EncodePriority(0, 10, 0)); // normal priority for PWM module (can stand some jitter on the Vref signals)
  134. }
  135. void Stepper::digipot_current(const uint8_t driver, const int16_t current) {
  136. if (!(PWM->PWM_CH_NUM[0].PWM_CPRD == PWM_PERIOD_US)) digipot_init(); // Init PWM system if needed
  137. switch (driver) {
  138. case 0:
  139. if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current)); // update X & Y
  140. if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));
  141. if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPMUPD = 0x0001; // enable compare event
  142. if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPMUPD = 0x0001; // enable compare event
  143. if (G2_PWM_X || G2_PWM_Y) PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
  144. break;
  145. case 1:
  146. if (G2_PWM_Z) {
  147. PWM->PWM_CMP[3].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current)); // update Z
  148. PWM->PWM_CMP[3].PWM_CMPMUPD = 0x0001; // enable compare event
  149. PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
  150. }
  151. break;
  152. default:
  153. if (G2_PWM_E) {
  154. PWM->PWM_CMP[4].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current)); // update E
  155. PWM->PWM_CMP[4].PWM_CMPMUPD = 0x0001; // enable compare event
  156. PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
  157. }
  158. break;
  159. }
  160. }
  161. volatile uint32_t PWM_ISR1_STATUS, PWM_ISR2_STATUS;
  162. void PWM_Handler() {
  163. PWM_ISR1_STATUS = PWM->PWM_ISR1;
  164. PWM_ISR2_STATUS = PWM->PWM_ISR2;
  165. if (PWM_ISR1_STATUS & PWM_IER1_CHID0) { // CHAN_0 interrupt
  166. if (G2_PWM_X) *ISR_table[0].set_register = ISR_table[0].write_mask; // set X to active
  167. if (G2_PWM_Y) *ISR_table[1].set_register = ISR_table[1].write_mask; // set Y to active
  168. if (G2_PWM_Z) *ISR_table[2].set_register = ISR_table[2].write_mask; // set Z to active
  169. if (G2_PWM_E) *ISR_table[3].set_register = ISR_table[3].write_mask; // set E to active
  170. }
  171. else {
  172. if (G2_PWM_X && (PWM_ISR2_STATUS & PWM_IER2_CMPM1)) *ISR_table[0].clr_register = ISR_table[0].write_mask; // set X to inactive
  173. if (G2_PWM_Y && (PWM_ISR2_STATUS & PWM_IER2_CMPM2)) *ISR_table[1].clr_register = ISR_table[1].write_mask; // set Y to inactive
  174. if (G2_PWM_Z && (PWM_ISR2_STATUS & PWM_IER2_CMPM3)) *ISR_table[2].clr_register = ISR_table[2].write_mask; // set Z to inactive
  175. if (G2_PWM_E && (PWM_ISR2_STATUS & PWM_IER2_CMPM4)) *ISR_table[3].clr_register = ISR_table[3].write_mask; // set E to inactive
  176. }
  177. return;
  178. }
  179. #endif // PRINTRBOARD_G2