Преглед на файлове

Allow G2_PWM to be slimmer

Scott Lahteine преди 4 години
родител
ревизия
0175189c34
променени са 2 файла, в които са добавени 109 реда и са изтрити 48 реда
  1. 108
    47
      Marlin/src/HAL/DUE/fastio/G2_PWM.cpp
  2. 1
    1
      Marlin/src/module/configuration_store.cpp

+ 108
- 47
Marlin/src/HAL/DUE/fastio/G2_PWM.cpp Целия файл

@@ -46,6 +46,31 @@
46 46
 
47 47
 #include "G2_PWM.h"
48 48
 
49
+#if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
50
+  #define G2_PWM_X 1
51
+#else
52
+  #define G2_PWM_X 0
53
+#endif
54
+#if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
55
+  #define G2_PWM_Y 1
56
+#else
57
+  #define G2_PWM_Y 0
58
+#endif
59
+#if PIN_EXISTS(MOTOR_CURRENT_PWM_Z)
60
+  #define G2_PWM_Z 1
61
+#else
62
+  #define G2_PWM_Z 0
63
+#endif
64
+#if PIN_EXISTS(MOTOR_CURRENT_PWM_E)
65
+  #define G2_PWM_E 1
66
+#else
67
+  #define G2_PWM_E 0
68
+#endif
69
+#define G2_MASK_X(V) (G2_PWM_X * (V))
70
+#define G2_MASK_Y(V) (G2_PWM_Y * (V))
71
+#define G2_MASK_Z(V) (G2_PWM_Z * (V))
72
+#define G2_MASK_E(V) (G2_PWM_E * (V))
73
+
49 74
 volatile uint32_t *SODR_A = &PIOA->PIO_SODR,
50 75
                   *SODR_B = &PIOB->PIO_SODR,
51 76
                   *CODR_A = &PIOA->PIO_CODR,
@@ -55,10 +80,18 @@ PWM_map ISR_table[NUM_PWMS] = PWM_MAP_INIT;
55 80
 
56 81
 void Stepper::digipot_init() {
57 82
 
58
-  OUT_WRITE(MOTOR_CURRENT_PWM_X_PIN, 0);  // init pins
59
-  OUT_WRITE(MOTOR_CURRENT_PWM_Y_PIN, 0);
60
-  OUT_WRITE(MOTOR_CURRENT_PWM_Z_PIN, 0);
61
-  OUT_WRITE(MOTOR_CURRENT_PWM_E_PIN, 0);
83
+  #if PIN_EXISTS(MOTOR_CURRENT_PWM_X)
84
+    OUT_WRITE(MOTOR_CURRENT_PWM_X_PIN, 0);  // init pins
85
+  #endif
86
+  #if PIN_EXISTS(MOTOR_CURRENT_PWM_Y)
87
+    OUT_WRITE(MOTOR_CURRENT_PWM_Y_PIN, 0);
88
+  #endif
89
+  #if G2_PWM_Z
90
+    OUT_WRITE(MOTOR_CURRENT_PWM_Z_PIN, 0);
91
+  #endif
92
+  #if G2_PWM_E
93
+    OUT_WRITE(MOTOR_CURRENT_PWM_E_PIN, 0);
94
+  #endif
62 95
 
63 96
   #define WPKEY          (0x50574D << 8) // “PWM” in ASCII
64 97
   #define WPCMD_DIS_SW   0  // command to disable Write Protect SW
@@ -71,30 +104,51 @@ void Stepper::digipot_init() {
71 104
   PWM->PWM_WPCR = WPKEY | WPRG_ALL | WPCMD_DIS_SW;        // enable setting of all PWM registers
72 105
   PWM->PWM_CLK = PWM_CLOCK_F;                             // enable CLK_A and set it to 1MHz, leave CLK_B disabled
73 106
   PWM->PWM_CH_NUM[0].PWM_CMR = 0b1011;                    // set channel 0 to Clock A input & to left aligned
74
-  PWM->PWM_CH_NUM[1].PWM_CMR = 0b1011;                    // set channel 1 to Clock A input & to left aligned
75
-  PWM->PWM_CH_NUM[2].PWM_CMR = 0b1011;                    // set channel 2 to Clock A input & to left aligned
76
-  PWM->PWM_CH_NUM[3].PWM_CMR = 0b1011;                    // set channel 3 to Clock A input & to left aligned
77
-  PWM->PWM_CH_NUM[4].PWM_CMR = 0b1011;                    // set channel 4 to Clock A input & to left aligned
107
+  if (G2_PWM_X) PWM->PWM_CH_NUM[1].PWM_CMR = 0b1011;      // set channel 1 to Clock A input & to left aligned
108
+  if (G2_PWM_Y) PWM->PWM_CH_NUM[2].PWM_CMR = 0b1011;      // set channel 2 to Clock A input & to left aligned
109
+  if (G2_PWM_Z) PWM->PWM_CH_NUM[3].PWM_CMR = 0b1011;      // set channel 3 to Clock A input & to left aligned
110
+  if (G2_PWM_E) PWM->PWM_CH_NUM[4].PWM_CMR = 0b1011;      // set channel 4 to Clock A input & to left aligned
78 111
 
79 112
   PWM->PWM_CH_NUM[0].PWM_CPRD = PWM_PERIOD_US;            // set channel 0 Period
80 113
 
81 114
   PWM->PWM_IER2 = PWM_IER1_CHID0;                         // generate interrupt when counter0 overflows
82
-  PWM->PWM_IER2 = PWM_IER2_CMPM0 | PWM_IER2_CMPM1 | PWM_IER2_CMPM2 | PWM_IER2_CMPM3 | PWM_IER2_CMPM4;        // generate interrupt on compare event
83
-
84
-  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
85
-  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
86
-  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
87
-  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
88
-
89
-  PWM->PWM_CMP[1].PWM_CMPM = 0x0001;  // enable compare event
90
-  PWM->PWM_CMP[2].PWM_CMPM = 0x0001;  // enable compare event
91
-  PWM->PWM_CMP[3].PWM_CMPM = 0x0001;  // enable compare event
92
-  PWM->PWM_CMP[4].PWM_CMPM = 0x0001;  // enable compare event
93
-
94
-  PWM->PWM_SCM = PWM_SCM_UPDM_MODE0 | PWM_SCM_SYNC0 | PWM_SCM_SYNC1 | PWM_SCM_SYNC2 | PWM_SCM_SYNC3 | PWM_SCM_SYNC4; // sync 1-4 with 0, use mode 0 for updates
95
-
96
-  PWM->PWM_ENA = PWM_ENA_CHID0 | PWM_ENA_CHID1 | PWM_ENA_CHID2 | PWM_ENA_CHID3 | PWM_ENA_CHID4;         // enable the channels used by G2
97
-  PWM->PWM_IER1 = PWM_IER1_CHID0 | PWM_IER1_CHID1 | PWM_IER1_CHID2 | PWM_IER1_CHID3 | PWM_IER1_CHID4;        // enable interrupts for the channels used by G2
115
+  PWM->PWM_IER2 = PWM_IER2_CMPM0
116
+    | G2_MASK_X(PWM_IER2_CMPM1)
117
+    | G2_MASK_Y(PWM_IER2_CMPM2)
118
+    | G2_MASK_Z(PWM_IER2_CMPM3)
119
+    | G2_MASK_E(PWM_IER2_CMPM4)
120
+  ; // generate interrupt on compare event
121
+
122
+  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
123
+  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
124
+  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
125
+  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
126
+
127
+  if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPM = 0x0001;  // enable compare event
128
+  if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPM = 0x0001;  // enable compare event
129
+  if (G2_PWM_Z) PWM->PWM_CMP[3].PWM_CMPM = 0x0001;  // enable compare event
130
+  if (G2_PWM_E) PWM->PWM_CMP[4].PWM_CMPM = 0x0001;  // enable compare event
131
+
132
+  PWM->PWM_SCM = PWM_SCM_UPDM_MODE0 | PWM_SCM_SYNC0
133
+    | G2_MASK_X(PWM_SCM_SYNC1)
134
+    | G2_MASK_Y(PWM_SCM_SYNC2)
135
+    | G2_MASK_Z(PWM_SCM_SYNC3)
136
+    | G2_MASK_E(PWM_SCM_SYNC4)
137
+  ; // sync 1-4 with 0, use mode 0 for updates
138
+
139
+  PWM->PWM_ENA = PWM_ENA_CHID0
140
+    | G2_MASK_X(PWM_ENA_CHID1)
141
+    | G2_MASK_Y(PWM_ENA_CHID2)
142
+    | G2_MASK_Z(PWM_ENA_CHID3)
143
+    | G2_MASK_E(PWM_ENA_CHID4)
144
+  ; // enable channels used by G2
145
+
146
+  PWM->PWM_IER1 = PWM_IER1_CHID0
147
+    | G2_MASK_X(PWM_IER1_CHID1)
148
+    | G2_MASK_Y(PWM_IER1_CHID2)
149
+    | G2_MASK_Z(PWM_IER1_CHID3)
150
+    | G2_MASK_E(PWM_IER1_CHID4)
151
+  ; // enable interrupts for channels used by G2
98 152
 
99 153
   NVIC_EnableIRQ(PWM_IRQn);     // Enable interrupt handler
100 154
   NVIC_SetPriority(PWM_IRQn, NVIC_EncodePriority(0, 10, 0));  // normal priority for PWM module (can stand some jitter on the Vref signals)
@@ -105,20 +159,27 @@ void Stepper::digipot_current(const uint8_t driver, const int16_t current) {
105 159
   if (!(PWM->PWM_CH_NUM[0].PWM_CPRD == PWM_PERIOD_US)) digipot_init();  // Init PWM system if needed
106 160
 
107 161
   switch (driver) {
108
-    case 0: PWM->PWM_CMP[1].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));    // update X & Y
109
-            PWM->PWM_CMP[2].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));
110
-            PWM->PWM_CMP[1].PWM_CMPMUPD = 0x0001;  // enable compare event
111
-            PWM->PWM_CMP[2].PWM_CMPMUPD = 0x0001;  // enable compare event
112
-            PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
113
-            break;
114
-    case 1: PWM->PWM_CMP[3].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));    // update Z
115
-            PWM->PWM_CMP[3].PWM_CMPMUPD = 0x0001;  // enable compare event
116
-            PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
117
-            break;
118
-    default:PWM->PWM_CMP[4].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));    // update E
119
-            PWM->PWM_CMP[4].PWM_CMPMUPD = 0x0001;  // enable compare event
120
-            PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
121
-            break;
162
+    case 0:
163
+      if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));    // update X & Y
164
+      if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));
165
+      if (G2_PWM_X) PWM->PWM_CMP[1].PWM_CMPMUPD = 0x0001;  // enable compare event
166
+      if (G2_PWM_Y) PWM->PWM_CMP[2].PWM_CMPMUPD = 0x0001;  // enable compare event
167
+      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
168
+      break;
169
+    case 1:
170
+      if (G2_PWM_Z) {
171
+        PWM->PWM_CMP[3].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));    // update Z
172
+        PWM->PWM_CMP[3].PWM_CMPMUPD = 0x0001;  // enable compare event
173
+        PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
174
+      }
175
+      break;
176
+    default:
177
+      if (G2_PWM_E) {
178
+        PWM->PWM_CMP[4].PWM_CMPVUPD = 0x010000000LL | G2_VREF_COUNT(G2_VREF(current));    // update E
179
+        PWM->PWM_CMP[4].PWM_CMPMUPD = 0x0001;  // enable compare event
180
+        PWM->PWM_SCUC = PWM_SCUC_UPDULOCK; // tell the PWM controller to update the values on the next cycle
181
+      }
182
+      break;
122 183
   }
123 184
 }
124 185
 
@@ -127,17 +188,17 @@ volatile uint32_t PWM_ISR1_STATUS, PWM_ISR2_STATUS;
127 188
 void PWM_Handler() {
128 189
   PWM_ISR1_STATUS = PWM->PWM_ISR1;
129 190
   PWM_ISR2_STATUS = PWM->PWM_ISR2;
130
-  if (PWM_ISR1_STATUS & PWM_IER1_CHID0) {                           // CHAN_0 interrupt
131
-    *ISR_table[0].set_register = ISR_table[0].write_mask;                                          // set X to active
132
-    *ISR_table[1].set_register = ISR_table[1].write_mask;                                          // set Y to active
133
-    *ISR_table[2].set_register = ISR_table[2].write_mask;                                          // set Z to active
134
-    *ISR_table[3].set_register = ISR_table[3].write_mask;                                          // set E to active
191
+  if (PWM_ISR1_STATUS & PWM_IER1_CHID0) {                                                         // CHAN_0 interrupt
192
+    if (G2_PWM_X) *ISR_table[0].set_register = ISR_table[0].write_mask;                           // set X to active
193
+    if (G2_PWM_Y) *ISR_table[1].set_register = ISR_table[1].write_mask;                           // set Y to active
194
+    if (G2_PWM_Z) *ISR_table[2].set_register = ISR_table[2].write_mask;                           // set Z to active
195
+    if (G2_PWM_E) *ISR_table[3].set_register = ISR_table[3].write_mask;                           // set E to active
135 196
   }
136 197
   else {
137
-    if (PWM_ISR2_STATUS & PWM_IER2_CMPM1)  *ISR_table[0].clr_register = ISR_table[0].write_mask;   // set X to inactive
138
-    if (PWM_ISR2_STATUS & PWM_IER2_CMPM2)  *ISR_table[1].clr_register = ISR_table[1].write_mask;   // set Y to inactive
139
-    if (PWM_ISR2_STATUS & PWM_IER2_CMPM3)  *ISR_table[2].clr_register = ISR_table[2].write_mask;   // set Z to inactive
140
-    if (PWM_ISR2_STATUS & PWM_IER2_CMPM4)  *ISR_table[3].clr_register = ISR_table[3].write_mask;   // set E to inactive
198
+    if (G2_PWM_X && (PWM_ISR2_STATUS & PWM_IER2_CMPM1)) *ISR_table[0].clr_register = ISR_table[0].write_mask; // set X to inactive
199
+    if (G2_PWM_Y && (PWM_ISR2_STATUS & PWM_IER2_CMPM2)) *ISR_table[1].clr_register = ISR_table[1].write_mask; // set Y to inactive
200
+    if (G2_PWM_Z && (PWM_ISR2_STATUS & PWM_IER2_CMPM3)) *ISR_table[2].clr_register = ISR_table[2].write_mask; // set Z to inactive
201
+    if (G2_PWM_E && (PWM_ISR2_STATUS & PWM_IER2_CMPM4)) *ISR_table[3].clr_register = ISR_table[3].write_mask; // set E to inactive
141 202
   }
142 203
   return;
143 204
 }

+ 1
- 1
Marlin/src/module/configuration_store.cpp Целия файл

@@ -2696,7 +2696,7 @@ void MarlinSettings::reset() {
2696 2696
 
2697 2697
   #if HAS_MOTOR_CURRENT_PWM
2698 2698
     constexpr uint32_t tmp_motor_current_setting[3] = PWM_MOTOR_CURRENT;
2699
-    for (uint8_t q = 3; q--;)
2699
+    LOOP_L_N(q, 3)
2700 2700
       stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q]));
2701 2701
   #endif
2702 2702
 

Loading…
Отказ
Запис