Selaa lähdekoodia

PWM fixes, slow down fan update

include LPC1768 syntax for M42

couple more pin_t changes

consistency

change M42 to R, P format

Revert "change M42 to R, P format"

This reverts commit 01f12f579e.
Bob-the-Kuhn 6 vuotta sitten
vanhempi
commit
c14000775b

+ 307
- 289
Marlin/src/HAL/HAL_LPC1768/LPC1768_PWM.cpp Näytä tiedosto

@@ -29,38 +29,49 @@
29 29
 /**
30 30
  * This is a hybrid system.
31 31
  *
32
- * The PWM1 module is used to directly control the Servo 0, 1 & 3 pins.  This keeps
32
+ * The PWM1 module is used to directly control the Servo 0, 1 & 3 pins and D9 & D10 pins.  This keeps
33 33
  * the pulse width jitter to under a microsecond.
34 34
  *
35 35
  * For all other pins the PWM1 module is used to generate interrupts.  The ISR
36 36
  * routine does the actual setting/clearing of pins.  The upside is that any pin can
37 37
  * have a PWM channel assigned to it.  The downside is that there is more pulse width
38 38
  * jitter. The jitter depends on what else is happening in the system and what ISRs
39
- * prempt the PWM ISR.  Writing to the SD card can add 20 microseconds to the pulse
40
- * width.
39
+ * pre-empt the PWM ISR.
41 40
  */
42 41
 
43 42
 /**
44
- * The data structures are setup to minimize the computation done by the ISR which
45
- * minimizes ISR execution time.  Execution times are 2.2 - 3.7 microseconds.
43
+ * The data structures are set up to minimize the computation done by the ISR which
44
+ * minimizes ISR execution time.  Execution times are 2-4µs except when updating to
45
+ * a new value when they are 19µs.
46 46
  *
47
- * Two tables are used.  active_table is used by the ISR.  Changes to the table are
48
- * are done by copying the active_table into the work_table, updating the work_table
49
- * and then swapping the two tables.  Swapping is done by manipulating pointers.
47
+ * Two tables are used.  One table contains the data used by the ISR to update/control
48
+ * the PWM pins.  The other is used as an aid when rebuilding the ISR table.
50 49
  *
51
- * Immediately after the swap the ISR uses the work_table until the start of the
52
- * next 20mS cycle. During this transition the "work_table" is actually the table
53
- * that was being used before the swap.  The "active_table" contains the data that
54
- * will start being used at the start of the next 20mS period.  This keeps the pins
55
- * well behaved during the transition.
50
+ * The LPC1768_PWM_attach_pin routine disables the ISR and then adds the new info to
51
+ * ISR table.  It can update the table directly because none of its changes affect
52
+ * what the ISR does.
56 53
  *
57
- * The ISR's priority is set to the maximum otherwise other ISRs can cause considerable
58
- * jitter in the PWM high time.
54
+ * LPC1768_PWM_detach_pin routine disables the ISR, disables the pin immediately if
55
+ * it's a directly controlled pin and updates the helper table.  It then flags the
56
+ * ISR that the ISR table needs to be rebuilt.
57
+ *
58
+ * LPC1768_PWM_write routine disables the ISR and updates the helper table.  It then
59
+ * flags the ISR that the ISR table needs to be rebuilt.
60
+ *
61
+ * The ISR's priority is set to less than the stepper ISR otherwise it could cause jitter
62
+ * in the step pulses.
59 63
  *
60 64
  * See the end of this file for details on the hardware/firmware interaction
61 65
  */
62 66
 
63 67
 #ifdef TARGET_LPC1768
68
+
69
+#include "../../inc/MarlinConfig.h"
70
+
71
+// #include <math.h>
72
+// #include <stdio.h>
73
+// #include <stdlib.h>
74
+
64 75
 #include <lpc17xx_pinsel.h>
65 76
 #include "LPC1768_PWM.h"
66 77
 #include "arduino.h"
@@ -68,48 +79,68 @@
68 79
 #define NUM_PWMS 6
69 80
 
70 81
 typedef struct {            // holds all data needed to control/init one of the PWM channels
71
-    uint8_t             sequence;       // 0: available slot, 1 - 6: PWM channel assigned to that slot
72
-    pin_t               pin;
73
-    uint16_t            PWM_mask;       // MASK TO CHECK/WRITE THE IR REGISTER
74
-    volatile uint32_t*  set_register;
75
-    volatile uint32_t*  clr_register;
76
-    uint32_t            write_mask;     // USED BY SET/CLEAR COMMANDS
77
-    uint32_t            microseconds;   // value written to MR register
78
-    uint32_t            min;            // lower value limit checked by WRITE routine before writing to the MR register
79
-    uint32_t            max;            // upper value limit checked by WRITE routine before writing to the MR register
80
-    bool                PWM_flag;       // 0 - USED BY sERVO, 1 - USED BY ANALOGWRITE
81
-    uint8_t             servo_index;    // 0 - MAX_SERVO -1 : servo index,  0xFF : PWM channel
82
-    bool                active_flag;    // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
83
-    uint8_t             assigned_MR;    // Which MR (1-6) is used by this logical channel
84
-    uint32_t            PCR_bit;        // PCR register bit to enable PWM1 control of this pin
85
-    uint32_t            PINSEL3_bits;   // PINSEL3 register bits to set pin mode to PWM1 control
82
+  uint8_t             sequence;       // 0: available slot, 1 - 6: PWM channel assigned to that slot
83
+  pin_t               pin;
84
+  uint16_t            PWM_mask;       // MASK TO CHECK/WRITE THE IR REGISTER
85
+  volatile uint32_t*  set_register;
86
+  volatile uint32_t*  clr_register;
87
+  uint32_t            write_mask;     // USED BY SET/CLEAR COMMANDS
88
+  uint32_t            microseconds;   // value written to MR register
89
+  uint32_t            min;            // lower value limit checked by WRITE routine before writing to the MR register
90
+  uint32_t            max;            // upper value limit checked by WRITE routine before writing to the MR register
91
+  bool                PWM_flag;       // 0 - USED BY hardware PWM, 1 - USED BY ANALOGWRITE
92
+  uint8_t             servo_index;    // 0 - MAX_SERVO -1 : servo index,  0xFF : PWM channel
93
+  bool                active_flag;    // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
94
+  uint32_t            PCR_bit;        // PCR register bit to enable PWM1 control of this pin
95
+  volatile uint32_t*  PINSEL_reg;     // PINSEL register
96
+  uint32_t            PINSEL_bits;    // PINSEL register bits to set pin mode to PWM1 control
86 97
 
87 98
 } PWM_map;
88 99
 
89 100
 
90
-#define MICRO_MAX 0xffffffff
101
+#define MICRO_MAX 0xFFFFFFFF
91 102
 
92
-#define PWM_MAP_INIT_ROW {0, P_NC, 0, 0, 0, 0, MICRO_MAX, 0, 0, 0, 0, 0, 0, 0, 0}
93
-#define PWM_MAP_INIT {PWM_MAP_INIT_ROW,\
94
-                      PWM_MAP_INIT_ROW,\
95
-                      PWM_MAP_INIT_ROW,\
96
-                      PWM_MAP_INIT_ROW,\
97
-                      PWM_MAP_INIT_ROW,\
98
-                      PWM_MAP_INIT_ROW,\
103
+#define PWM_MAP_INIT_ROW { 0, 0x7FFF, 0, 0, 0, 0, MICRO_MAX, 0, 0, 0, 0, 0, 0, 0, 0 }
104
+#define PWM_MAP_INIT { PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, \
105
+                       PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, \
99 106
                      };
100 107
 
101
-PWM_map PWM1_map_A[NUM_PWMS] = PWM_MAP_INIT;
102
-PWM_map PWM1_map_B[NUM_PWMS] = PWM_MAP_INIT;
108
+PWM_map ISR_table[NUM_PWMS] = PWM_MAP_INIT;
103 109
 
104
-PWM_map *active_table = PWM1_map_A;
105
-PWM_map *work_table = PWM1_map_B;
106
-PWM_map *ISR_table;
110
+#define IR_BIT(p) ((p) >= 0 && (p) <= 3 ? (p) : p + 4 )
111
+#define PIN_IS_INVERTED(p) 0  // placeholder in case inverting PWM output is offered
107 112
 
108 113
 
109
-#define IR_BIT(p) (p >= 0 && p <= 3 ? p : p + 4 )
110
-#define COPY_ACTIVE_TABLE    for (uint8_t i = 0; i < 6 ; i++) work_table[i] = active_table[i]
111
-#define PIN_IS_INVERTED(p) 0  // place holder in case inverting PWM output is offered
112 114
 
115
+#define P1_18_PWM_channel  1  // servo 3
116
+#define P1_20_PWM_channel  2  // servo 0
117
+#define P1_21_PWM_channel  3  // servo 1
118
+#define P2_4_PWM_channel   5  // D9
119
+#define P2_5_PWM_channel   6  // D10
120
+
121
+// used to keep track of which Match Registers have been used and if they will be used by the
122
+// PWM1 module to directly control the pin or will be used to generate an interrupt
123
+typedef struct {                    // status of PWM1 channel
124
+  uint8_t             map_used;     // 0 - this MR register not used/assigned
125
+  uint8_t             map_PWM_INT;  // 0 - available for interrupts, 1 - in use by PWM
126
+  pin_t               map_PWM_PIN;  // pin for this PwM1 controlled pin / port
127
+  volatile uint32_t*  MR_register;  // address of the MR register for this PWM1 channel
128
+  uint32_t            PCR_bit;      // PCR register bit to enable PWM1 control of this pin
129
+                                    // 0 - don't switch to PWM1 direct control
130
+  volatile uint32_t*  PINSEL_reg;   // PINSEL register
131
+  uint32_t            PINSEL_bits;  // PINSEL register bits to set pin mode to PWM1 control
132
+} MR_map;
133
+
134
+MR_map map_MR[NUM_PWMS];
135
+
136
+void LPC1768_PWM_update_map_MR(void) {
137
+  map_MR[0] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P1_18_PWM_channel) ? 1 : 0), P1_18, &LPC_PWM1->MR1, 0, 0, 0 };
138
+  map_MR[1] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P1_20_PWM_channel) ? 1 : 0), P1_20, &LPC_PWM1->MR2, 0, 0, 0 };
139
+  map_MR[2] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P1_21_PWM_channel) ? 1 : 0), P1_21, &LPC_PWM1->MR3, 0, 0, 0 };
140
+  map_MR[3] = { 0, 0, P_NC, &LPC_PWM1->MR4, 0, 0, 0 };
141
+  map_MR[4] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P2_4_PWM_channel) ? 1 : 0), P2_4, &LPC_PWM1->MR5, 0, 0, 0 };
142
+  map_MR[5] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P2_5_PWM_channel) ? 1 : 0), P2_5, &LPC_PWM1->MR6, 0, 0, 0 };
143
+}
113 144
 
114 145
 /**
115 146
  *  Prescale register and MR0 register values
@@ -144,317 +175,322 @@ PWM_map *ISR_table;
144 175
  *
145 176
  */
146 177
 
178
+bool ISR_table_update = false;  // flag to tell the ISR that the tables need to be updated & swapped
147 179
 
148 180
 void LPC1768_PWM_init(void) {
149
-  #define SBIT_CNTEN     0  // PWM1 counter & pre-scaler enable/disable
150
-  #define SBIT_CNTRST    1  // reset counters to known state
151
-  #define SBIT_PWMEN     3  // 1 - PWM, 0 - timer
152
-  #define SBIT_PWMMR0R   1
153
-  #define PCPWM1         6
181
+  #define SBIT_CNTEN      0  // PWM1 counter & pre-scaler enable/disable
182
+  #define SBIT_CNTRST     1  // reset counters to known state
183
+  #define SBIT_PWMEN      3  // 1 - PWM, 0 - timer
184
+  #define SBIT_PWMMR0R    1
185
+  #define PCPWM1          6
154 186
   #define PCLK_PWM1      12
155 187
 
156
-  LPC_SC->PCONP |= (1 << PCPWM1);      // enable PWM1 controller (enabled on power up)
188
+  SBI(LPC_SC->PCONP, PCPWM1);                                             // Enable PWM1 controller (enabled on power up)
157 189
   LPC_SC->PCLKSEL0 &= ~(0x3 << PCLK_PWM1);
158 190
   LPC_SC->PCLKSEL0 |= (LPC_PWM1_PCLKSEL0 << PCLK_PWM1);
159
-  LPC_PWM1->MR0 = LPC_PWM1_MR0;                // TC resets every 19,999 + 1 cycles - sets PWM cycle(Ton+Toff) to 20 mS
160
-                                        // MR0 must be set before TCR enables the PWM
161
-  LPC_PWM1->TCR = _BV(SBIT_CNTEN) | _BV(SBIT_CNTRST)| _BV(SBIT_PWMEN);;  // enable counters, reset counters, set mode to PWM
162
-  LPC_PWM1->TCR &= ~(_BV(SBIT_CNTRST));  // take counters out of reset
163
-  LPC_PWM1->PR  =  LPC_PWM1_PR;
164
-  LPC_PWM1->MCR = (_BV(SBIT_PWMMR0R) | _BV(0));     // Reset TC if it matches MR0, disable all interrupts except for MR0
165
-  LPC_PWM1->CTCR = 0;                   // disable counter mode (enable PWM mode)
166
-
167
-  LPC_PWM1->LER = 0x07F;  // Set the latch Enable Bits to load the new Match Values for MR0 - MR6
168
-  // Set all PWMs to single edge
169
-  LPC_PWM1->PCR = 0;    // single edge mode for all channels, PWM1 control of outputs off
170
-
171
-  NVIC_EnableIRQ(PWM1_IRQn);     // Enable interrupt handler
172
-  //      NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 10, 0));  // normal priority for PWM module
173
-  NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 0, 0));  // minimizes jitter due to higher priority ISRs
191
+
192
+  LPC_PWM1->MR0  = LPC_PWM1_MR0;                                          // TC resets every 19,999 + 1 cycles - sets PWM cycle(Ton+Toff) to 20 mS
193
+  // MR0 must be set before TCR enables the PWM
194
+  LPC_PWM1->TCR  = _BV(SBIT_CNTEN) | _BV(SBIT_CNTRST) | _BV(SBIT_PWMEN);  // Enable counters, reset counters, set mode to PWM
195
+  LPC_PWM1->TCR  &= ~(_BV(SBIT_CNTRST));                                  // Take counters out of reset
196
+  LPC_PWM1->PR   =  LPC_PWM1_PR;
197
+  LPC_PWM1->MCR  = _BV(SBIT_PWMMR0R) | _BV(0);                            // Reset TC if it matches MR0, disable all interrupts except for MR0
198
+  LPC_PWM1->CTCR = 0;                                                     // Disable counter mode (enable PWM mode)
199
+  LPC_PWM1->LER  = 0x07F;                                                 // Set the latch Enable Bits to load the new Match Values for MR0 - MR6
200
+  LPC_PWM1->PCR  = 0;                                                     // Single edge mode for all channels, PWM1 control of outputs off
201
+
202
+  NVIC_EnableIRQ(PWM1_IRQn);                                              // Enable interrupt handler
203
+  NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 10, 0));             // Normal priority for PWM module
204
+  //NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 0, 0));            // Minimizes jitter due to higher priority ISRs
174 205
 }
175 206
 
176 207
 
177
-bool PWM_table_swap = false;  // flag to tell the ISR that the tables have been swapped
178
-bool PWM_MR0_wait = false;  // flag to ensure don't delay MR0 interrupt
208
+bool LPC1768_PWM_attach_pin(pin_t pin, uint32_t min /* = 1 */, uint32_t max /* = (LPC_PWM1_MR0 - MR0_MARGIN) */, uint8_t servo_index /* = 0xff */) {
179 209
 
210
+  pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));  // Sometimes the upper byte is garbled
180 211
 
181
-bool LPC1768_PWM_attach_pin(pin_t pin, uint32_t min /* = 1 */, uint32_t max /* = (LPC_PWM1_MR0 - MR0_MARGIN) */, uint8_t servo_index /* = 0xff */) {
182
-  while (PWM_table_swap) delay(5);  // don't do anything until the previous change has been implemented by the ISR
183
-  COPY_ACTIVE_TABLE;  // copy active table into work table
212
+  NVIC_DisableIRQ(PWM1_IRQn);    // make it safe to update the active table
213
+                                 // OK to update the active table because the
214
+                                 // ISR doesn't use any of the changed items
184 215
   uint8_t slot = 0;
185 216
   for (uint8_t i = 0; i < NUM_PWMS ; i++)         // see if already in table
186
-    if (work_table[i].pin == pin) return 1;
217
+    if (ISR_table[i].pin == pin) {
218
+      NVIC_EnableIRQ(PWM1_IRQn);  // re-enable PWM interrupts
219
+      return 1;
220
+    }
187 221
 
188 222
   for (uint8_t i = 1; (i < NUM_PWMS + 1) && !slot; i++)         // find empty slot
189
-    if ( !(work_table[i - 1].set_register)) slot = i;  // any item that can't be zero when active or just attached is OK
223
+    if ( !(ISR_table[i - 1].set_register)) { slot = i; break; }  // any item that can't be zero when active or just attached is OK
190 224
   if (!slot) return 0;
191 225
   slot--;  // turn it into array index
192 226
 
193
-  work_table[slot].pin = pin;     // init slot
194
-  work_table[slot].PWM_mask = 0;  // real value set by PWM_write
195
-  work_table[slot].set_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET;
196
-  work_table[slot].clr_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR;
197
-  work_table[slot].write_mask = LPC_PIN(LPC1768_PIN_PIN(pin));
198
-  work_table[slot].microseconds = MICRO_MAX;
199
-  work_table[slot].min = min;
200
-  work_table[slot].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
201
-  work_table[slot].servo_index = servo_index;
202
-  work_table[slot].active_flag = false;
203
-
204
-  //swap tables
205
-  PWM_MR0_wait = true;
206
-  while (PWM_MR0_wait) delay(5);  //wait until MR0 interrupt has happend so don't delay it.
227
+  ISR_table[slot].pin          = pin;     // init slot
228
+  ISR_table[slot].PWM_mask     = 0;  // real value set by PWM_write
229
+  ISR_table[slot].set_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET;
230
+  ISR_table[slot].clr_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR;
231
+  ISR_table[slot].write_mask   = LPC_PIN(LPC1768_PIN_PIN(pin));
232
+  ISR_table[slot].microseconds = MICRO_MAX;
233
+  ISR_table[slot].min          = min;
234
+  ISR_table[slot].max          = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
235
+  ISR_table[slot].servo_index  = servo_index;
236
+  ISR_table[slot].active_flag  = false;
207 237
 
208
-  NVIC_DisableIRQ(PWM1_IRQn);
209
-  PWM_map *pointer_swap = active_table;
210
-  active_table = work_table;
211
-  work_table = pointer_swap;
212
-  PWM_table_swap = true;  // tell the ISR that the tables have been swapped
213 238
   NVIC_EnableIRQ(PWM1_IRQn);  // re-enable PWM interrupts
214 239
 
215 240
   return 1;
216 241
 }
217 242
 
218
-#define pin_11_PWM_channel 2
219
-#define pin_6_PWM_channel  3
220
-#define pin_4_PWM_channel  1
221 243
 
222
-// used to keep track of which Match Registers have been used and if they will be used by the
223
-// PWM1 module to directly control the pin or will be used to generate an interrupt
224
-typedef struct {                        // status of PWM1 channel
225
-    uint8_t map_used;                   // 0 - this MR register not used/assigned
226
-    uint8_t map_PWM_INT;                // 0 - available for interrupts, 1 - in use by PWM
227
-    pin_t map_PWM_PIN;                  // pin for this PwM1 controlled pin / port
228
-    volatile uint32_t* MR_register;     // address of the MR register for this PWM1 channel
229
-    uint32_t PCR_bit;                   // PCR register bit to enable PWM1 control of this pin
230
-    uint32_t PINSEL3_bits;              // PINSEL3 register bits to set pin mode to PWM1 control
231
-} MR_map;
244
+bool LPC1768_PWM_detach_pin(pin_t pin) {
232 245
 
233
-MR_map map_MR[NUM_PWMS];
246
+  pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));
234 247
 
235
-void LPC1768_PWM_update_map_MR(void) {
236
-  map_MR[0] = {0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + pin_4_PWM_channel)  ? 1 : 0), P1_18, &LPC_PWM1->MR1, 0, 0};
237
-  map_MR[1] = {0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + pin_11_PWM_channel) ? 1 : 0), P1_20, &LPC_PWM1->MR2, 0, 0};
238
-  map_MR[2] = {0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + pin_6_PWM_channel)  ? 1 : 0), P1_21, &LPC_PWM1->MR3, 0, 0};
239
-  map_MR[3] = {0, 0, P_NC, &LPC_PWM1->MR4, 0, 0};
240
-  map_MR[4] = {0, 0, P_NC, &LPC_PWM1->MR5, 0, 0};
241
-  map_MR[5] = {0, 0, P_NC, &LPC_PWM1->MR6, 0, 0};
242
-}
248
+  NVIC_EnableIRQ(PWM1_IRQn);   // ?? fixes compiler problem??  ISR won't start
249
+                               // unless put in an extra "enable"
250
+  NVIC_DisableIRQ(PWM1_IRQn);
243 251
 
252
+  uint8_t slot = 0xFF;
253
+  for (uint8_t i = 0; i < NUM_PWMS; i++)         // find slot
254
+    if (ISR_table[i].pin == pin) { slot = i; break; }
255
+  if (slot == 0xFF) return false;    // return error if pin not found
244 256
 
245
-uint32_t LPC1768_PWM_interrupt_mask = 1;
257
+  LPC1768_PWM_update_map_MR();
246 258
 
247
-void LPC1768_PWM_update(void) {
248
-  for (uint8_t i = NUM_PWMS; --i;) {  // (bubble) sort table by microseconds
249
-    bool didSwap = false;
250
-    PWM_map temp;
251
-    for (uint16_t j = 0; j < i; ++j) {
252
-      if (work_table[j].microseconds > work_table[j + 1].microseconds) {
253
-        temp = work_table[j + 1];
254
-        work_table[j + 1] = work_table[j];
255
-        work_table[j] = temp;
256
-        didSwap = true;
257
-      }
258
-    }
259
-    if (!didSwap) break;
259
+  // OK to make these changes before the MR0 interrupt
260
+  switch(pin) {
261
+    case P1_20:                        // Servo 0, PWM1 channel 2  (Pin 11  P1.20 PWM1.2)
262
+      LPC_PWM1->PCR &= ~(_BV(8 + P1_20_PWM_channel));                 // disable PWM1 module control of this pin
263
+      map_MR[P1_20_PWM_channel - 1].PCR_bit = 0;
264
+      LPC_PINCON->PINSEL3 &= ~(0x3 <<  8);    // return pin to general purpose I/O
265
+      map_MR[P1_20_PWM_channel - 1].PINSEL_bits = 0;
266
+      map_MR[P1_20_PWM_channel - 1].map_PWM_INT = 0;               // 0 - available for interrupts, 1 - in use by PWM
267
+      break;
268
+    case P1_21:                        // Servo 1, PWM1 channel 3  (Pin 6  P1.21 PWM1.3)
269
+      LPC_PWM1->PCR &= ~(_BV(8 + P1_21_PWM_channel));                  // disable PWM1 module control of this pin
270
+      map_MR[P1_21_PWM_channel - 1].PCR_bit = 0;
271
+      LPC_PINCON->PINSEL3 &= ~(0x3 << 10);  // return pin to general purpose I/O
272
+      map_MR[P1_21_PWM_channel - 1].PINSEL_bits = 0;
273
+      map_MR[P1_21_PWM_channel - 1].map_PWM_INT = 0;                // 0 - available for interrupts, 1 - in use by PWM
274
+      break;
275
+    case P1_18:                        // Servo 3, PWM1 channel 1 (Pin 4  P1.18 PWM1.1)
276
+      LPC_PWM1->PCR &= ~(_BV(8 + P1_18_PWM_channel));                  // disable PWM1 module control of this pin
277
+      map_MR[P1_18_PWM_channel - 1].PCR_bit =  0;
278
+      LPC_PINCON->PINSEL3 &= ~(0x3 <<  4);  // return pin to general purpose I/O
279
+      map_MR[P1_18_PWM_channel - 1].PINSEL_bits =  0;
280
+      map_MR[P1_18_PWM_channel - 1].map_PWM_INT = 0;                // 0 - available for interrupts, 1 - in use by PWM
281
+      break;
282
+    case P2_4:                        // D9 FET, PWM1 channel 5  (Pin 9  P2_4 PWM1.5)
283
+      LPC_PWM1->PCR &= ~(_BV(8 + P2_4_PWM_channel));                  // disable PWM1 module control of this pin
284
+      map_MR[P2_4_PWM_channel - 1].PCR_bit = 0;
285
+      LPC_PINCON->PINSEL4 &= ~(0x3 << 10);  // return pin to general purpose I/O
286
+      map_MR[P2_4_PWM_channel - 1].PINSEL_bits = 0;
287
+      map_MR[P2_4_PWM_channel - 1].map_PWM_INT = 0;                // 0 - available for interrupts, 1 - in use by PWM
288
+      break;
289
+    case P2_5:                        // D10 FET, PWM1 channel 6 (Pin 10  P2_5 PWM1.6)
290
+      LPC_PWM1->PCR &= ~(_BV(8 + P2_5_PWM_channel));                  // disable PWM1 module control of this pin
291
+      map_MR[P2_5_PWM_channel - 1].PCR_bit =  0;
292
+      LPC_PINCON->PINSEL4 &= ~(0x3 <<  4);  // return pin to general purpose I/O
293
+      map_MR[P2_5_PWM_channel - 1].PINSEL_bits =  0;
294
+      map_MR[P2_5_PWM_channel - 1].map_PWM_INT = 0;                // 0 - available for interrupts, 1 - in use by PWM
295
+      break;
296
+    default:
297
+      break;
260 298
   }
261 299
 
262
-  LPC1768_PWM_interrupt_mask = 0;                          // set match registers to new values, build IRQ mask
263
-  for (uint8_t i = 0; i < NUM_PWMS; i++) {
264
-    if (work_table[i].active_flag == true) {
265
-      work_table[i].sequence = i + 1;
300
+  ISR_table[slot] = PWM_MAP_INIT_ROW;
266 301
 
267
-      // first see if there is a PWM1 controlled pin for this entry
268
-      bool found = false;
269
-      for (uint8_t j = 0; (j < NUM_PWMS) && !found; j++) {
270
-        if ( (map_MR[j].map_PWM_PIN == work_table[i].pin) && map_MR[j].map_PWM_INT ) {
271
-          *map_MR[j].MR_register = work_table[i].microseconds;  // found one of the PWM pins
272
-          work_table[i].PWM_mask = 0;
273
-          work_table[i].PCR_bit = map_MR[j].PCR_bit;            // PCR register bit to enable PWM1 control of this pin
274
-          work_table[i].PINSEL3_bits = map_MR[j].PINSEL3_bits;  // PINSEL3 register bits to set pin mode to PWM1 control} MR_map;
275
-          map_MR[j].map_used = 2;
276
-          work_table[i].assigned_MR = j +1;                    // only used to help in debugging
277
-          found = true;
278
-        }
279
-      }
302
+  ISR_table_update = true;
303
+  NVIC_EnableIRQ(PWM1_IRQn);  // re-enable PWM interrupts
280 304
 
281
-      // didn't find a PWM1 pin so get an interrupt
282
-      for (uint8_t k = 0; (k < NUM_PWMS) && !found; k++) {
283
-        if ( !(map_MR[k].map_PWM_INT || map_MR[k].map_used)) {
284
-          *map_MR[k].MR_register = work_table[i].microseconds;  // found one for an interrupt pin
285
-          map_MR[k].map_used = 1;
286
-          LPC1768_PWM_interrupt_mask |= _BV(3 * (k + 1));  // set bit in the MCR to enable this MR to generate an interrupt
287
-          work_table[i].PWM_mask = _BV(IR_BIT(k + 1));  // bit in the IR that will go active when this MR generates an interrupt
288
-          work_table[i].assigned_MR = k +1;                // only used to help in debugging
289
-          found = true;
290
-        }
291
-      }
292
-    }
293
-    else
294
-      work_table[i].sequence = 0;
295
-  }
296
-  LPC1768_PWM_interrupt_mask |= (uint32_t) _BV(0);  // add in MR0 interrupt
305
+  return 1;
306
+}
297 307
 
298
-   // swap tables
299 308
 
300
-  PWM_MR0_wait = true;
301
-  while (PWM_MR0_wait) delay(5);  //wait until MR0 interrupt has happend so don't delay it.
309
+bool LPC1768_PWM_write(pin_t pin, uint32_t value) {
302 310
 
303
-  NVIC_DisableIRQ(PWM1_IRQn);
304
-  LPC_PWM1->LER = 0x07E;  // Set the latch Enable Bits to load the new Match Values for MR1 - MR6
305
-  PWM_map *pointer_swap = active_table;
306
-  active_table = work_table;
307
-  work_table = pointer_swap;
308
-  PWM_table_swap = true;  // tell the ISR that the tables have been swapped
309
-  NVIC_EnableIRQ(PWM1_IRQn);  // re-enable PWM interrupts
310
-}
311
+  pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));
311 312
 
313
+  NVIC_DisableIRQ(PWM1_IRQn);
312 314
 
313
-bool LPC1768_PWM_write(pin_t pin, uint32_t value) {
314
-  while (PWM_table_swap) delay(5);  // don't do anything until the previous change has been implemented by the ISR
315
-  COPY_ACTIVE_TABLE;  // copy active table into work table
316 315
   uint8_t slot = 0xFF;
317 316
   for (uint8_t i = 0; i < NUM_PWMS; i++)         // find slot
318
-    if (work_table[i].pin == pin) slot = i;
317
+    if (ISR_table[i].pin == pin) { slot = i; break; }
319 318
   if (slot == 0xFF) return false;    // return error if pin not found
320 319
 
321 320
   LPC1768_PWM_update_map_MR();
322 321
 
323 322
   switch(pin) {
324 323
     case P1_20:                        // Servo 0, PWM1 channel 2 (Pin 11  P1.20 PWM1.2)
325
-      map_MR[pin_11_PWM_channel - 1].PCR_bit = _BV(8 + pin_11_PWM_channel);  // enable PWM1 module control of this pin
326
-      map_MR[pin_11_PWM_channel - 1].map_PWM_INT = 1;               // 0 - available for interrupts, 1 - in use by PWM
327
-      map_MR[pin_11_PWM_channel - 1].PINSEL3_bits = 0x2 <<  8;      // ISR must do this AFTER setting PCR
324
+      map_MR[P1_20_PWM_channel - 1].PCR_bit = _BV(8 + P1_20_PWM_channel);  // enable PWM1 module control of this pin
325
+      map_MR[P1_20_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
326
+      map_MR[P1_20_PWM_channel - 1].PINSEL_bits = 0x2 <<  8;      // ISR must do this AFTER setting PCR
328 327
       break;
329 328
     case P1_21:                        // Servo 1, PWM1 channel 3 (Pin 6  P1.21 PWM1.3)
330
-      map_MR[pin_6_PWM_channel - 1].PCR_bit = _BV(8 + pin_6_PWM_channel);                  // enable PWM1 module control of this pin
331
-      map_MR[pin_6_PWM_channel - 1].map_PWM_INT = 1;                // 0 - available for interrupts, 1 - in use by PWM
332
-      map_MR[pin_6_PWM_channel - 1].PINSEL3_bits = 0x2 << 10;       // ISR must do this AFTER setting PCR
329
+      map_MR[P1_21_PWM_channel - 1].PCR_bit = _BV(8 + P1_21_PWM_channel);                  // enable PWM1 module control of this pin
330
+      map_MR[P1_21_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
331
+      map_MR[P1_21_PWM_channel - 1].PINSEL_bits = 0x2 << 10;       // ISR must do this AFTER setting PCR
333 332
       break;
334 333
     case P1_18:                        // Servo 3, PWM1 channel 1 (Pin 4  P1.18 PWM1.1)
335
-      map_MR[pin_4_PWM_channel - 1].PCR_bit = _BV(8 + pin_4_PWM_channel);                  // enable PWM1 module control of this pin
336
-      map_MR[pin_4_PWM_channel - 1].map_PWM_INT = 1;                // 0 - available for interrupts, 1 - in use by PWM
337
-      map_MR[pin_4_PWM_channel - 1].PINSEL3_bits =  0x2 <<  4;       // ISR must do this AFTER setting PCR
334
+      map_MR[P1_18_PWM_channel - 1].PCR_bit = _BV(8 + P1_18_PWM_channel);                  // enable PWM1 module control of this pin
335
+      map_MR[P1_18_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
336
+      map_MR[P1_18_PWM_channel - 1].PINSEL_bits =  0x2 <<  4;       // ISR must do this AFTER setting PCR
337
+      break;
338
+    case P2_4:                        // D9 FET, PWM1 channel 5 (Pin 9  P2_4 PWM1.5)
339
+      map_MR[P2_4_PWM_channel - 1].PCR_bit = _BV(8 + P2_4_PWM_channel);                  // enable PWM1 module control of this pin
340
+      map_MR[P2_4_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL4;
341
+      map_MR[P2_4_PWM_channel - 1].PINSEL_bits = 0x1 << 8;       // ISR must do this AFTER setting PCR
342
+      break;
343
+    case P2_5:                        // D10 FET, PWM1 channel 6 (Pin 10  P2_5 PWM1.6)
344
+      map_MR[P2_5_PWM_channel - 1].PCR_bit = _BV(8 + P2_5_PWM_channel);                  // enable PWM1 module control of this pin
345
+      map_MR[P2_5_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL4;
346
+      map_MR[P2_5_PWM_channel - 1].PINSEL_bits =  0x1 <<  10;       // ISR must do this AFTER setting PCR
338 347
       break;
339
-    default:                                                        // ISR pins
340
-      pinMode(pin, OUTPUT);  // set pin to output but don't write anything in case it's already in use
348
+    default:  // ISR pins
349
+      pinMode(pin, OUTPUT);  // set pin to output
341 350
       break;
342 351
   }
343 352
 
344
-  work_table[slot].microseconds = MAX(MIN(value, work_table[slot].max), work_table[slot].min);
345
-  work_table[slot].active_flag = true;
353
+  ISR_table[slot].microseconds = MAX(MIN(value, ISR_table[slot].max), ISR_table[slot].min);
354
+  ISR_table[slot].active_flag = 1;
346 355
 
347
-  LPC1768_PWM_update();
356
+  ISR_table_update = true;
357
+
358
+  NVIC_EnableIRQ(PWM1_IRQn);  // re-enable PWM interrupts
348 359
 
349 360
   return 1;
350 361
 }
351 362
 
352 363
 
353
-bool LPC1768_PWM_detach_pin(pin_t pin) {
354
-  while (PWM_table_swap) delay(5);  // don't do anything until the previous change has been implemented by the ISR
355
-  COPY_ACTIVE_TABLE;  // copy active table into work table
356
-  uint8_t slot = 0xFF;
357
-  for (uint8_t i = 0; i < NUM_PWMS; i++)         // find slot
358
-    if (work_table[i].pin == pin) slot = i;
359
-  if (slot == 0xFF) return false;    // return error if pin not found
364
+uint32_t LPC1768_PWM_interrupt_mask = 1;
360 365
 
361
-  LPC1768_PWM_update_map_MR();
362 366
 
363
-  // OK to make these changes before the MR0 interrupt
364
-  switch(pin) {
365
-    case P1_20:                        // Servo 0, PWM1 channel 2  (Pin 11  P1.20 PWM1.2)
366
-      LPC_PWM1->PCR &= ~(_BV(8 + pin_11_PWM_channel));                 // disable PWM1 module control of this pin
367
-      map_MR[pin_11_PWM_channel - 1].PCR_bit = 0;
368
-      LPC_PINCON->PINSEL3 &= ~(0x3 <<  8);    // return pin to general purpose I/O
369
-      map_MR[pin_11_PWM_channel - 1].PINSEL3_bits = 0;
370
-      map_MR[pin_11_PWM_channel - 1].map_PWM_INT = 0;               // 0 - available for interrupts, 1 - in use by PWM
371
-      break;
372
-    case P1_21:                        // Servo 1, PWM1 channel 3  (Pin 6  P1.21 PWM1.3)
373
-      LPC_PWM1->PCR &= ~(_BV(8 + pin_6_PWM_channel));                  // disable PWM1 module control of this pin
374
-      map_MR[pin_6_PWM_channel - 1].PCR_bit = 0;
375
-      LPC_PINCON->PINSEL3 &= ~(0x3 << 10);  // return pin to general purpose I/O
376
-      map_MR[pin_6_PWM_channel - 1].PINSEL3_bits = 0;
377
-      map_MR[pin_6_PWM_channel - 1].map_PWM_INT = 0;                // 0 - available for interrupts, 1 - in use by PWM
378
-      break;
379
-    case P1_18:                        // Servo 3, PWM1 channel 1 (Pin 4  P1.18 PWM1.1)
380
-      LPC_PWM1->PCR &= ~(_BV(8 + pin_4_PWM_channel));                  // disable PWM1 module control of this pin
381
-      map_MR[pin_4_PWM_channel - 1].PCR_bit =  0;
382
-      LPC_PINCON->PINSEL3 &= ~(0x3 <<  4);  // return pin to general purpose I/O
383
-      map_MR[pin_4_PWM_channel - 1].PINSEL3_bits =  0;
384
-      map_MR[pin_4_PWM_channel - 1].map_PWM_INT = 0;                // 0 - available for interrupts, 1 - in use by PWM
385
-      break;
386
-    default:
387
-      break;
367
+void LPC1768_PWM_update(void) {     // only called by the ISR
368
+  LPC1768_PWM_interrupt_mask = 0;                          // set match registers to new values, build IRQ mask
369
+  // first setup directly controlled PWM pin slots
370
+
371
+  bool found;
372
+  for (uint8_t i = 0; i < NUM_PWMS; i++) {
373
+    ISR_table[i].PCR_bit = 0;     // clear entries
374
+    ISR_table[i].PINSEL_reg = 0;
375
+    ISR_table[i].PINSEL_bits = 0;
376
+    ISR_table[i].PWM_flag = 1;    // mark slot as interrupt mode until find differently
377
+
378
+    if (ISR_table[i].active_flag) {
379
+      ISR_table[i].sequence = i + 1;
380
+
381
+      // first see if there is a PWM1 controlled pin for this entry
382
+      found = false;
383
+      for (uint8_t j = 0; (j < NUM_PWMS) && !found; j++) {
384
+        if ( (map_MR[j].map_PWM_PIN == ISR_table[i].pin)) {
385
+          map_MR[j].map_PWM_INT = 1;                            // flag that it's already setup for direct control
386
+          ISR_table[i].PWM_mask = 0;
387
+          ISR_table[i].PCR_bit = map_MR[j].PCR_bit;            // PCR register bit to enable PWM1 control of this pin
388
+          ISR_table[i].PINSEL_reg = map_MR[j].PINSEL_reg;  // PINSEL register address to set pin mode to PWM1 control} MR_map;
389
+          ISR_table[i].PINSEL_bits = map_MR[j].PINSEL_bits;  // PINSEL register bits to set pin mode to PWM1 control} MR_map;
390
+          map_MR[j].map_used = 2;
391
+          ISR_table[i].PWM_flag = 0;
392
+          *map_MR[j].MR_register = ISR_table[i].microseconds;
393
+          found = true;
394
+        }
395
+      }
396
+    }
397
+    else
398
+    ISR_table[i].sequence = 0;
388 399
   }
389 400
 
390
-  pinMode(pin, INPUT);
401
+  // next fill in interrupt slots
402
+  for (uint8_t i = 0; i < NUM_PWMS; i++) {
391 403
 
392
-  work_table[slot] = PWM_MAP_INIT_ROW;
404
+    if (ISR_table[i].active_flag && ISR_table[i].PWM_flag) {
393 405
 
394
-  LPC1768_PWM_update();
406
+      // setup interrupt slot
407
+      found = false;
408
+      for (uint8_t k = 0; (k < NUM_PWMS) && !found; k++) {
409
+        if ( !(map_MR[k].map_PWM_INT || map_MR[k].map_used)) {
410
+          *map_MR[k].MR_register = ISR_table[i].microseconds;  // found one for an interrupt pin
411
+          map_MR[k].map_used = 1;
412
+          LPC1768_PWM_interrupt_mask |= _BV(3 * (k + 1));  // set bit in the MCR to enable this MR to generate an interrupt
413
+          ISR_table[i].set_register = PIN_IS_INVERTED(ISR_table[i].pin) ? &LPC_GPIO(LPC1768_PIN_PORT(ISR_table[i].pin))->FIOCLR : &LPC_GPIO(LPC1768_PIN_PORT(ISR_table[i].pin))->FIOSET;
414
+          ISR_table[i].clr_register = PIN_IS_INVERTED(ISR_table[i].pin) ? &LPC_GPIO(LPC1768_PIN_PORT(ISR_table[i].pin))->FIOSET : &LPC_GPIO(LPC1768_PIN_PORT(ISR_table[i].pin))->FIOCLR;
415
+          ISR_table[i].write_mask = LPC_PIN(LPC1768_PIN_PIN(ISR_table[i].pin));
416
+          ISR_table[i].PWM_mask = _BV(IR_BIT(k + 1));  // bit in the IR that will go active when this MR generates an interrupt
417
+          ISR_table[i].PWM_flag = 1;
418
+          found = true;
419
+        }
420
+      }
421
+    }
422
+  }
395 423
 
396
-  return 1;
424
+  LPC1768_PWM_interrupt_mask |= (uint32_t) _BV(0);  // add in MR0 interrupt
425
+
426
+  LPC_PWM1->LER = 0x07E;  // Set the latch Enable Bits to load the new Match Values for MR1 - MR6
397 427
 }
398 428
 
399 429
 
400 430
 bool useable_hardware_PWM(pin_t pin) {
401
-  COPY_ACTIVE_TABLE;  // copy active table into work table
431
+
432
+  pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));
433
+
434
+  NVIC_DisableIRQ(PWM1_IRQn);
435
+
436
+  bool return_flag = false;
402 437
   for (uint8_t i = 0; i < NUM_PWMS; i++)         // see if it's already setup
403
-    if (work_table[i].pin == pin && work_table[i].sequence) return true;
438
+    if (ISR_table[i].pin == pin && ISR_table[i].sequence) return_flag = true;
404 439
   for (uint8_t i = 0; i < NUM_PWMS; i++)         // see if there is an empty slot
405
-    if (!work_table[i].sequence) return true;
406
-  return false;    // only get here if neither the above are true
440
+    if (!ISR_table[i].sequence) return_flag = true;
441
+  NVIC_EnableIRQ(PWM1_IRQn);  // re-enable PWM interrupts
442
+  return return_flag;
407 443
 }
408 444
 
409 445
 ////////////////////////////////////////////////////////////////////////////////
410 446
 
411 447
 #define HAL_PWM_LPC1768_ISR  extern "C" void PWM1_IRQHandler(void)
412 448
 
413
-
414 449
 // Both loops could be terminated when the last active channel is found but that would
415 450
 // result in variations ISR run time which results in variations in pulse width
416 451
 
417 452
 /**
418
- * Changes to PINSEL3, PCR and MCR are only done during the MR0 interrupt otherwise
453
+ * Changes to PINSEL, PCR and MCR are only done during the MR0 interrupt otherwise
419 454
  * the wrong pin may be toggled or even have the system hang.
420 455
  */
421 456
 
422 457
 
423 458
 HAL_PWM_LPC1768_ISR {
424
-  if (PWM_table_swap) ISR_table = work_table;   // use old table if a swap was just done
425
-  else ISR_table = active_table;
426
-
427
-  if (LPC_PWM1->IR & 0x1) {                                      // MR0 interrupt
428
-    ISR_table = active_table;                    // MR0 means new values could have been loaded so set everything
429
-    if (PWM_table_swap) LPC_PWM1->MCR = LPC1768_PWM_interrupt_mask; // enable new PWM individual channel interrupts
430 459
 
460
+  if (LPC_PWM1->IR & 0x1) {                       // MR0 interrupt
461
+    if (ISR_table_update) {                       // new values have been loaded so set everything
462
+      LPC1768_PWM_update();                       // update & swap table
463
+      LPC_PWM1->MCR = LPC1768_PWM_interrupt_mask; // enable new PWM individual channel interrupts
464
+    }
431 465
     for (uint8_t i = 0; i < NUM_PWMS; i++) {
432
-      if(ISR_table[i].active_flag && !((ISR_table[i].pin == P1_20) ||
433
-                                       (ISR_table[i].pin == P1_21) ||
434
-                                       (ISR_table[i].pin == P1_18)))
466
+      if (ISR_table[i].active_flag && !((ISR_table[i].pin == P1_20) ||
467
+                                        (ISR_table[i].pin == P1_21) ||
468
+                                        (ISR_table[i].pin == P1_18) ||
469
+                                        (ISR_table[i].pin == P2_4)  ||
470
+                                        (ISR_table[i].pin == P2_5))
471
+      ) {
435 472
         *ISR_table[i].set_register = ISR_table[i].write_mask;       // set pins for all enabled interrupt channels active
436
-      if (PWM_table_swap && ISR_table[i].PCR_bit) {
437
-        LPC_PWM1->PCR |= ISR_table[i].PCR_bit;              // enable PWM1 module control of this pin
438
-        LPC_PINCON->PINSEL3 |= ISR_table[i].PINSEL3_bits;   // set pin mode to PWM1 control - must be done after PCR
473
+      }
474
+      if (ISR_table_update && ISR_table[i].PCR_bit) {
475
+        LPC_PWM1->PCR |= ISR_table[i].PCR_bit;                  // enable PWM1 module control of this pin
476
+        *ISR_table[i].PINSEL_reg |= ISR_table[i].PINSEL_bits;   // set pin mode to PWM1 control - must be done after PCR
439 477
       }
440 478
     }
441
-    PWM_table_swap = false;
442
-    PWM_MR0_wait = false;
479
+    ISR_table_update = false;
443 480
     LPC_PWM1->IR = 0x01;                                             // clear the MR0 interrupt flag bit
444 481
   }
445 482
   else {
446
-    for (uint8_t i = 0; i < NUM_PWMS ; i++)
447
-      if (ISR_table[i].active_flag && (LPC_PWM1->IR & ISR_table[i].PWM_mask) ){
483
+    for (uint8_t i = 0; i < NUM_PWMS; i++)
484
+      if (ISR_table[i].active_flag && (LPC_PWM1->IR & ISR_table[i].PWM_mask)) {
448 485
         LPC_PWM1->IR = ISR_table[i].PWM_mask;       // clear the interrupt flag bits for expected interrupts
449 486
         *ISR_table[i].clr_register = ISR_table[i].write_mask;   // set channel to inactive
450 487
       }
451 488
   }
452 489
 
453 490
   LPC_PWM1->IR = 0x70F;  // guarantees all interrupt flags are cleared which, if there is an unexpected
454
-                           // PWM interrupt, will keep the ISR from hanging which will crash the controller
455
-
456
-return;
491
+                         // PWM interrupt, will keep the ISR from hanging which will crash the controller
457 492
 }
493
+
458 494
 #endif
459 495
 
460 496
 /////////////////////////////////////////////////////////////////
@@ -466,44 +502,26 @@ return;
466 502
  *  interrupt.  The only exception is detaching pins.  It doesn't matter when they go
467 503
  *  tristate.
468 504
  *
469
- *  The LPC1768_PWM_init routine kicks off the MR0 interrupt.  This interrupt is never disabled or
470
- *  delayed.
505
+ *  The LPC1768_PWM_init routine kicks off the MR0 interrupt.  This interrupt is never disabled.  It
506
+ *  can be delayed by higher priority interrupts.  Actions on directly controlled pins are not delayed
507
+ *  by other interrupts
471 508
  *
472
- *  The PWM_table_swap flag is set when the firmware has swapped in an updated table.  It is
473
- *  cleared by the ISR during the MR0 interrupt as it completes the swap and accompanying updates.
474
- *  It serves two purposes:
475
- *    1) Tells the ISR that the tables have been swapped
476
- *    2) Keeps the firmware from starting a new update until the previous one has been completed.
477
- *
478
- *  The PWM_MR0_wait flag is set when the firmware is ready to swap in an updated table and cleared by
479
- *  the ISR during the MR0 interrupt.  It is used to avoid delaying the MR0 interrupt when swapping in
480
- *  an updated table.  This avoids glitches in pulse width and/or repetition rate.
509
+ *  The ISR_table_update flag is set when the ISR table needs to be rebuilt.  It is
510
+ *  cleared by the ISR during the MR0 interrupt after it rebuilds the ISR table.
481 511
  *
482 512
  *  The sequence of events during a write to a PWM channel is:
483
- *    1) Waits until PWM_table_swap flag is false before starting
484
- *    2) Copies the active table into the work table
485
- *    3) Updates the work table
486
- *         NOTES - MR1-MR6 are updated at this time.  The updates aren't put into use until the first
513
+ *    1) Attach routine puts the pin number in the ISR table but doesn't mark it active.
514
+ *    2) Write routine marks the pin as active, updates the helper table and flags the ISR that the
515
+ *       ISR table needs to be rebuilt.
516
+ *    3) On the MR0 interrupt the ISR:
517
+ *         a. Rebuilds the ISR table if needed.
518
+ *                 MR1-MR6 are updated at this time.  The updates aren't put into use until the first
487 519
  *                 MR0 after the LER register has been written.  The LER register is written during the
488
- *                 table swap process.
489
- *               - The MCR mask is created at this time.  It is not used until the ISR writes the MCR
490
- *                 during the MR0 interrupt in the table swap process.
491
- *    4) Sets the PWM_MR0_wait flag
492
- *    5) ISR clears the PWM_MR0_wait flag during the next MR0 interrupt
493
- *    6) Once the PWM_MR0_wait flag is cleared then the firmware:
494
- *          disables the ISR interrupt
495
- *          swaps the pointers to the tables
496
- *          writes to the LER register
497
- *          sets the PWM_table_swap flag active
498
- *          re-enables the ISR
499
- *     7) On the next interrupt the ISR changes its pointer to the work table which is now the old,
500
- *        unmodified, active table.
501
- *     8) On the next MR0 interrupt the ISR:
502
- *          switches over to the active table
503
- *          clears the PWM_table_swap and PWM_MR0_wait flags
504
- *          updates the MCR register with the possibly new interrupt sources/assignments
505
- *          writes to the PCR register to enable the direct control of the Servo 0, 1 & 3 pins by the PWM1 module
506
- *          sets the PINSEL3 register to function/mode 0x2 for the Servo 0, 1 & 3 pins
507
- *             NOTE - PCR must be set before PINSEL
508
- *          sets the pins controlled by the ISR to their active states
520
+ *                 table rebuild process.  The result is new timing takes 20-40 mS to be implemented.
521
+ *         b. Sets the interrupt controlled pin(s) to their active state
522
+ *         c. Writes to the PCR register to enable the directly controlled pins
523
+ *         d. Sets the PINSEL register to the function/mode for the directly controlled pins
524
+ *
525
+ *    4) For each interrupt controlled pin there is another ISR call.  During this call the pin is set
526
+ *       to its inactive state.  The call is initiated when a MR1-MR6 reg times out.
509 527
  */

+ 1
- 3
Marlin/src/HAL/HAL_LPC1768/LPC1768_PWM.h Näytä tiedosto

@@ -69,9 +69,7 @@
69 69
 #define MR0_MARGIN 200       // if channel value too close to MR0 the system locks up
70 70
 
71 71
 void LPC1768_PWM_init(void);
72
-bool LPC1768_PWM_attach_pin(pin_t pin, uint32_t min = 1, uint32_t max = (LPC_PWM1_MR0 - MR0_MARGIN), uint8_t servo_index = 0xff);
73
-void LPC1768_PWM_update_map_MR(void);
74
-void LPC1768_PWM_update(void);
72
+bool LPC1768_PWM_attach_pin(pin_t pin, uint32_t min=1, uint32_t max=(LPC_PWM1_MR0 - (MR0_MARGIN)), uint8_t servo_index=0xFF);
75 73
 bool LPC1768_PWM_write(pin_t pin, uint32_t value);
76 74
 bool LPC1768_PWM_detach_pin(pin_t pin);
77 75
 bool useable_hardware_PWM(pin_t pin);

+ 1
- 1
Marlin/src/HAL/HAL_LPC1768/fastio.h Näytä tiedosto

@@ -39,7 +39,7 @@
39 39
 #include "arduino.h"
40 40
 #include "pinmapping.h"
41 41
 
42
-bool useable_hardware_PWM(uint8_t pin);
42
+bool useable_hardware_PWM(pin_t pin);
43 43
 #define USEABLE_HARDWARE_PWM(pin) useable_hardware_PWM(pin)
44 44
 
45 45
 #define LPC_PORT_OFFSET         (0x0020)

+ 6
- 1
Marlin/src/Marlin.cpp Näytä tiedosto

@@ -522,7 +522,12 @@ void manage_inactivity(bool ignore_stepper_queue/*=false*/) {
522 522
     tmc2130_checkOverTemp();
523 523
   #endif
524 524
 
525
-  planner.check_axes_activity();
525
+  // Limit check_axes_activity frequency to 10Hz
526
+  static millis_t next_check_axes_ms = 0;
527
+  if (ELAPSED(ms, next_check_axes_ms)) {
528
+    planner.check_axes_activity();
529
+    next_check_axes_ms = ms + 100UL;
530
+  }
526 531
 }
527 532
 
528 533
 /**

+ 5
- 0
Marlin/src/gcode/control/M42.cpp Näytä tiedosto

@@ -28,6 +28,11 @@
28 28
  * M42: Change pin status via GCode
29 29
  *
30 30
  *  P<pin>  Pin number (LED if omitted)
31
+ *            For LPC1768 use M42 P1.20 S255 if wanting to set P1_20 to logic 1
32
+ *              NOTE - Repetier Host truncates trailing zeros on a decimal when
33
+ *                     sending commands so typing M42 P1.20 S255 results in 
34
+ *                     M42 P1.2 S255 being sent.  Pronterface doesn't have this issue.
35
+ *                     
31 36
  *  S<byte> Pin status from 0 - 255
32 37
  */
33 38
 void GcodeSuite::M42() {

+ 32
- 51
Marlin/src/module/temperature.cpp Näytä tiedosto

@@ -1271,57 +1271,38 @@ void Temperature::init() {
1271 1271
 
1272 1272
 #if ENABLED(FAST_PWM_FAN)
1273 1273
 
1274
-  void Temperature::setPwmFrequency(const uint8_t pin, int val) {
1275
-    val &= 0x07;
1276
-    switch (digitalPinToTimer(pin)) {
1277
-      #ifdef TCCR0A
1278
-        #if !AVR_AT90USB1286_FAMILY
1279
-          case TIMER0A:
1274
+  void Temperature::setPwmFrequency(const pin_t pin, int val) {
1275
+    #ifdef ARDUINO
1276
+      val &= 0x07;
1277
+      switch (digitalPinToTimer(pin)) {
1278
+        #ifdef TCCR0A
1279
+          #if !AVR_AT90USB1286_FAMILY
1280
+            case TIMER0A:
1281
+          #endif
1282
+          case TIMER0B:                           //_SET_CS(0, val);
1283
+                                                    break;
1280 1284
         #endif
1281
-        case TIMER0B:
1282
-          //_SET_CS(0, val);
1283
-          break;
1284
-      #endif
1285
-      #ifdef TCCR1A
1286
-        case TIMER1A:
1287
-        case TIMER1B:
1288
-          //_SET_CS(1, val);
1289
-          break;
1290
-      #endif
1291
-      #ifdef TCCR2
1292
-        case TIMER2:
1293
-        case TIMER2:
1294
-          _SET_CS(2, val);
1295
-          break;
1296
-      #endif
1297
-      #ifdef TCCR2A
1298
-        case TIMER2A:
1299
-        case TIMER2B:
1300
-          _SET_CS(2, val);
1301
-          break;
1302
-      #endif
1303
-      #ifdef TCCR3A
1304
-        case TIMER3A:
1305
-        case TIMER3B:
1306
-        case TIMER3C:
1307
-          _SET_CS(3, val);
1308
-          break;
1309
-      #endif
1310
-      #ifdef TCCR4A
1311
-        case TIMER4A:
1312
-        case TIMER4B:
1313
-        case TIMER4C:
1314
-          _SET_CS(4, val);
1315
-          break;
1316
-      #endif
1317
-      #ifdef TCCR5A
1318
-        case TIMER5A:
1319
-        case TIMER5B:
1320
-        case TIMER5C:
1321
-          _SET_CS(5, val);
1322
-          break;
1323
-      #endif
1324
-    }
1285
+        #ifdef TCCR1A
1286
+          case TIMER1A: case TIMER1B:             //_SET_CS(1, val);
1287
+                                                    break;
1288
+        #endif
1289
+        #ifdef TCCR2
1290
+          case TIMER2: case TIMER2:                 _SET_CS(2, val); break;
1291
+        #endif
1292
+        #ifdef TCCR2A
1293
+          case TIMER2A: case TIMER2B:               _SET_CS(2, val); break;
1294
+        #endif
1295
+        #ifdef TCCR3A
1296
+          case TIMER3A: case TIMER3B: case TIMER3C: _SET_CS(3, val); break;
1297
+        #endif
1298
+        #ifdef TCCR4A
1299
+          case TIMER4A: case TIMER4B: case TIMER4C: _SET_CS(4, val); break;
1300
+        #endif
1301
+        #ifdef TCCR5A
1302
+          case TIMER5A: case TIMER5B: case TIMER5C: _SET_CS(5, val); break;
1303
+        #endif
1304
+      }
1305
+    #endif  
1325 1306
   }
1326 1307
 
1327 1308
 #endif // FAST_PWM_FAN
@@ -1332,7 +1313,7 @@ void Temperature::init() {
1332 1313
    * their target temperature by a configurable margin.
1333 1314
    * This is called when the temperature is set. (M104, M109)
1334 1315
    */
1335
-  void Temperature::start_watching_heater(uint8_t e) {
1316
+  void Temperature::start_watching_heater(const uint8_t e) {
1336 1317
     #if HOTENDS == 1
1337 1318
       UNUSED(e);
1338 1319
     #endif

+ 2
- 2
Marlin/src/module/temperature.h Näytä tiedosto

@@ -361,14 +361,14 @@ class Temperature {
361 361
     static int16_t degTargetBed() { return target_temperature_bed; }
362 362
 
363 363
     #if WATCH_HOTENDS
364
-      static void start_watching_heater(uint8_t e = 0);
364
+      static void start_watching_heater(const uint8_t e = 0);
365 365
     #endif
366 366
 
367 367
     #if WATCH_THE_BED
368 368
       static void start_watching_bed();
369 369
     #endif
370 370
 
371
-    static void setTargetHotend(const int16_t celsius, uint8_t e) {
371
+    static void setTargetHotend(const int16_t celsius, const uint8_t e) {
372 372
       #if HOTENDS == 1
373 373
         UNUSED(e);
374 374
       #endif

Loading…
Peruuta
Tallenna