|
@@ -32,7 +32,7 @@
|
32
|
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
|
|
- * For all other pins the PWM1 module is used to generate interrupts. The ISR
|
|
35
|
+ * For all other pins a timer 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
|
|
@@ -41,25 +41,11 @@
|
41
|
41
|
|
42
|
42
|
/**
|
43
|
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.
|
|
44
|
+ * minimizes ISR execution time. Execution times are 5-14µs depending on how full the
|
|
45
|
+ * ISR table is. 14uS is for a 20 element ISR table.
|
46
|
46
|
*
|
47
|
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.
|
49
|
|
- *
|
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.
|
53
|
|
- *
|
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.
|
|
48
|
+ * the PWM pins. The other is used as an aid when updating the ISR table.
|
63
|
49
|
*
|
64
|
50
|
* See the end of this file for details on the hardware/firmware interaction
|
65
|
51
|
*/
|
|
@@ -86,46 +72,36 @@
|
86
|
72
|
#ifdef TARGET_LPC1768
|
87
|
73
|
|
88
|
74
|
#include "../../inc/MarlinConfig.h"
|
89
|
|
-
|
90
|
75
|
#include <lpc17xx_pinsel.h>
|
91
|
76
|
#include "LPC1768_PWM.h"
|
92
|
77
|
#include "arduino.h"
|
93
|
78
|
|
94
|
|
-#define NUM_PWMS 6
|
|
79
|
+#define NUM_ISR_PWMS 20
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+#define LPC_PORT_OFFSET (0x0020)
|
|
83
|
+#define LPC_PIN(pin) (1UL << pin)
|
|
84
|
+#define LPC_GPIO(port) ((volatile LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + LPC_PORT_OFFSET * port))
|
|
85
|
+
|
95
|
86
|
|
96
|
87
|
typedef struct { // holds all data needed to control/init one of the PWM channels
|
97
|
|
- uint8_t sequence; // 0: available slot, 1 - 6: PWM channel assigned to that slot
|
|
88
|
+ bool active_flag; // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
|
98
|
89
|
pin_t pin;
|
99
|
|
- uint16_t PWM_mask; // MASK TO CHECK/WRITE THE IR REGISTER
|
100
|
90
|
volatile uint32_t* set_register;
|
101
|
91
|
volatile uint32_t* clr_register;
|
102
|
92
|
uint32_t write_mask; // USED BY SET/CLEAR COMMANDS
|
103
|
93
|
uint32_t microseconds; // value written to MR register
|
104
|
94
|
uint32_t min; // lower value limit checked by WRITE routine before writing to the MR register
|
105
|
95
|
uint32_t max; // upper value limit checked by WRITE routine before writing to the MR register
|
106
|
|
- bool PWM_flag; // 0 - USED BY hardware PWM, 1 - USED BY ANALOGWRITE
|
107
|
96
|
uint8_t servo_index; // 0 - MAX_SERVO -1 : servo index, 0xFF : PWM channel
|
108
|
|
- bool active_flag; // THIS TABLE ENTRY IS ACTIVELY TOGGLING A PIN
|
109
|
|
- uint32_t PCR_bit; // PCR register bit to enable PWM1 control of this pin
|
110
|
|
- volatile uint32_t* PINSEL_reg; // PINSEL register
|
111
|
|
- uint32_t PINSEL_bits; // PINSEL register bits to set pin mode to PWM1 control
|
112
|
|
-
|
113
|
97
|
} PWM_map;
|
114
|
98
|
|
|
99
|
+PWM_map PWM1_map_A[NUM_ISR_PWMS]; // compiler will initialize to all zeros
|
|
100
|
+PWM_map PWM1_map_B[NUM_ISR_PWMS]; // compiler will initialize to all zeros
|
115
|
101
|
|
116
|
|
-#define MICRO_MAX 0xFFFFFFFF
|
117
|
|
-
|
118
|
|
-#define PWM_MAP_INIT_ROW { 0, 0x7FFF, 0, 0, 0, 0, MICRO_MAX, 0, 0, 0, 0, 0, 0, 0, 0 }
|
119
|
|
-#define PWM_MAP_INIT { PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, \
|
120
|
|
- PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, PWM_MAP_INIT_ROW, \
|
121
|
|
- };
|
122
|
|
-
|
123
|
|
-PWM_map ISR_table[NUM_PWMS] = PWM_MAP_INIT;
|
124
|
|
-
|
125
|
|
-#define IR_BIT(p) ((p) >= 0 && (p) <= 3 ? (p) : p + 4 )
|
126
|
|
-#define PIN_IS_INVERTED(p) 0 // placeholder in case inverting PWM output is offered
|
127
|
|
-
|
128
|
|
-
|
|
102
|
+PWM_map *active_table = PWM1_map_A;
|
|
103
|
+PWM_map *work_table = PWM1_map_B;
|
|
104
|
+PWM_map *temp_table;
|
129
|
105
|
|
130
|
106
|
#define P1_18_PWM_channel 1 // servo 3
|
131
|
107
|
#define P1_20_PWM_channel 2 // servo 0
|
|
@@ -134,35 +110,13 @@ PWM_map ISR_table[NUM_PWMS] = PWM_MAP_INIT;
|
134
|
110
|
#define P2_04_PWM_channel 5 // D9
|
135
|
111
|
#define P2_05_PWM_channel 6 // D10
|
136
|
112
|
|
137
|
|
-// used to keep track of which Match Registers have been used and if they will be used by the
|
138
|
|
-// PWM1 module to directly control the pin or will be used to generate an interrupt
|
139
|
|
-typedef struct { // status of PWM1 channel
|
140
|
|
- uint8_t map_used; // 0 - this MR register not used/assigned
|
141
|
|
- uint8_t map_PWM_INT; // 0 - available for interrupts, 1 - in use by PWM
|
142
|
|
- pin_t map_PWM_PIN; // pin for this PwM1 controlled pin / port
|
143
|
|
- volatile uint32_t* MR_register; // address of the MR register for this PWM1 channel
|
144
|
|
- uint32_t PCR_bit; // PCR register bit to enable PWM1 control of this pin
|
145
|
|
- // 0 - don't switch to PWM1 direct control
|
146
|
|
- volatile uint32_t* PINSEL_reg; // PINSEL register
|
147
|
|
- uint32_t PINSEL_bits; // PINSEL register bits to set pin mode to PWM1 control
|
148
|
|
-} MR_map;
|
149
|
|
-
|
150
|
|
-MR_map map_MR[NUM_PWMS];
|
151
|
|
-
|
152
|
|
-void LPC1768_PWM_update_map_MR(void) {
|
153
|
|
- 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 };
|
154
|
|
- 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 };
|
155
|
|
- 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 };
|
156
|
|
- map_MR[3] = {
|
157
|
|
- #if MB(MKS_SBASE)
|
158
|
|
- 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P1_23_PWM_channel) ? 1 : 0), P1_23, &LPC_PWM1->MR4, 0, 0, 0
|
159
|
|
- #else
|
160
|
|
- 0, 0, P_NC, &LPC_PWM1->MR4, 0, 0, 0
|
161
|
|
- #endif
|
162
|
|
- };
|
163
|
|
- map_MR[4] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P2_04_PWM_channel) ? 1 : 0), P2_04, &LPC_PWM1->MR5, 0, 0, 0 };
|
164
|
|
- map_MR[5] = { 0, (uint8_t) (LPC_PWM1->PCR & _BV(8 + P2_05_PWM_channel) ? 1 : 0), P2_05, &LPC_PWM1->MR6, 0, 0, 0 };
|
165
|
|
-}
|
|
113
|
+typedef struct {
|
|
114
|
+ uint32_t min;
|
|
115
|
+ uint32_t max;
|
|
116
|
+ bool assigned;
|
|
117
|
+} table_direct;
|
|
118
|
+
|
|
119
|
+table_direct direct_table[6]; // compiler will initialize to all zeros
|
166
|
120
|
|
167
|
121
|
/**
|
168
|
122
|
* Prescale register and MR0 register values
|
|
@@ -181,8 +135,8 @@ void LPC1768_PWM_update_map_MR(void) {
|
181
|
135
|
* 0.25 399 4,999 199 4,999 99 4,999 49 4,999 0.720
|
182
|
136
|
* 0.125 799 2,499 399 2,499 199 2,499 99 2,499 1.440
|
183
|
137
|
*
|
184
|
|
- * The desired prescale frequency comes from an input in the range of 544 - 2400 microseconds and the
|
185
|
|
- * desire to just shift the input left or right as needed.
|
|
138
|
+ * The desired prescale frequency column comes from an input in the range of 544 - 2400 microseconds
|
|
139
|
+ * and the desire to just shift the input left or right as needed.
|
186
|
140
|
*
|
187
|
141
|
* A resolution of 0.2 degrees seems reasonable so a prescale frequency output of 1MHz is being used.
|
188
|
142
|
* It also means we don't need to scale the input.
|
|
@@ -197,9 +151,10 @@ void LPC1768_PWM_update_map_MR(void) {
|
197
|
151
|
*
|
198
|
152
|
*/
|
199
|
153
|
|
200
|
|
-bool ISR_table_update = false; // flag to tell the ISR that the tables need to be updated & swapped
|
201
|
|
-
|
202
|
154
|
void LPC1768_PWM_init(void) {
|
|
155
|
+
|
|
156
|
+ ///// directly controlled PWM pins (interrupts not used for these)
|
|
157
|
+
|
203
|
158
|
#define SBIT_CNTEN 0 // PWM1 counter & pre-scaler enable/disable
|
204
|
159
|
#define SBIT_CNTRST 1 // reset counters to known state
|
205
|
160
|
#define SBIT_PWMEN 3 // 1 - PWM, 0 - timer
|
|
@@ -221,43 +176,116 @@ void LPC1768_PWM_init(void) {
|
221
|
176
|
LPC_PWM1->LER = 0x07F; // Set the latch Enable Bits to load the new Match Values for MR0 - MR6
|
222
|
177
|
LPC_PWM1->PCR = 0; // Single edge mode for all channels, PWM1 control of outputs off
|
223
|
178
|
|
224
|
|
- NVIC_EnableIRQ(PWM1_IRQn); // Enable interrupt handler
|
225
|
|
- NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 10, 0)); // Normal priority for PWM module
|
226
|
|
- //NVIC_SetPriority(PWM1_IRQn, NVIC_EncodePriority(0, 0, 0)); // Minimizes jitter due to higher priority ISRs
|
|
179
|
+ //// interrupt controlled PWM setup
|
|
180
|
+
|
|
181
|
+ LPC_SC->PCONP |= 1 << 23; // power on timer3
|
|
182
|
+ HAL_PWM_TIMER->PR = LPC_PWM1_PR;
|
|
183
|
+ HAL_PWM_TIMER->MCR = 0x0B; // Interrupt on MR0 & MR1, reset on MR0
|
|
184
|
+ HAL_PWM_TIMER->MR0 = LPC_PWM1_MR0;
|
|
185
|
+ HAL_PWM_TIMER->MR1 = 0;
|
|
186
|
+ HAL_PWM_TIMER->TCR = _BV(0); // enable
|
|
187
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn);
|
|
188
|
+ NVIC_SetPriority(HAL_PWM_TIMER_IRQn, NVIC_EncodePriority(0, 4, 0));
|
227
|
189
|
}
|
228
|
190
|
|
229
|
191
|
|
230
|
|
-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 */) {
|
|
192
|
+bool ISR_table_update = false; // flag to tell the ISR that the tables need to be updated & swapped
|
|
193
|
+uint8_t ISR_index = 0; // index used by ISR to skip already actioned entries
|
|
194
|
+#define COPY_ACTIVE_TABLE for (uint8_t i = 0; i < NUM_ISR_PWMS ; i++) work_table[i] = active_table[i]
|
|
195
|
+uint32_t first_MR1_value = LPC_PWM1_MR0 + 1;
|
|
196
|
+
|
|
197
|
+void LPC1768_PWM_sort(void) {
|
|
198
|
+
|
|
199
|
+ for (uint8_t i = NUM_ISR_PWMS; --i;) { // (bubble) sort table by microseconds
|
|
200
|
+ bool didSwap = false;
|
|
201
|
+ PWM_map temp;
|
|
202
|
+ for (uint16_t j = 0; j < i; ++j) {
|
|
203
|
+ if (work_table[j].microseconds > work_table[j + 1].microseconds) {
|
|
204
|
+ temp = work_table[j + 1];
|
|
205
|
+ work_table[j + 1] = work_table[j];
|
|
206
|
+ work_table[j] = temp;
|
|
207
|
+ didSwap = true;
|
|
208
|
+ }
|
|
209
|
+ }
|
|
210
|
+ if (!didSwap) break;
|
|
211
|
+ }
|
|
212
|
+}
|
|
213
|
+
|
|
214
|
+bool LPC1768_PWM_attach_pin(pin_t pin, uint32_t min /* = 1 */, uint32_t max /* = (LPC_PWM1_MR0 - 1) */, uint8_t servo_index /* = 0xff */) {
|
231
|
215
|
|
232
|
216
|
pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF)); // Sometimes the upper byte is garbled
|
233
|
217
|
|
234
|
|
- NVIC_DisableIRQ(PWM1_IRQn); // make it safe to update the active table
|
|
218
|
+//// direct control PWM code
|
|
219
|
+ switch(pin) {
|
|
220
|
+ case P1_23: // MKS Sbase Servo 0, PWM1 channel 4 (J3-8 PWM1.4)
|
|
221
|
+ direct_table[P1_23_PWM_channel - 1].min = min;
|
|
222
|
+ direct_table[P1_23_PWM_channel - 1].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
|
223
|
+ direct_table[P1_23_PWM_channel - 1].assigned = true;
|
|
224
|
+ return true;
|
|
225
|
+ case P1_20: // Servo 0, PWM1 channel 2 (Pin 11 P1.20 PWM1.2)
|
|
226
|
+ direct_table[P1_20_PWM_channel - 1].min = min;
|
|
227
|
+ direct_table[P1_20_PWM_channel - 1].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
|
228
|
+ direct_table[P1_20_PWM_channel - 1].assigned = true;
|
|
229
|
+ return true;
|
|
230
|
+ case P1_21: // Servo 1, PWM1 channel 3 (Pin 6 P1.21 PWM1.3)
|
|
231
|
+ direct_table[P1_21_PWM_channel - 1].min = min;
|
|
232
|
+ direct_table[P1_21_PWM_channel - 1].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
|
233
|
+ direct_table[P1_21_PWM_channel - 1].assigned = true;
|
|
234
|
+ return true;
|
|
235
|
+ case P1_18: // Servo 3, PWM1 channel 1 (Pin 4 P1.18 PWM1.1)
|
|
236
|
+ direct_table[P1_18_PWM_channel - 1].min = min;
|
|
237
|
+ direct_table[P1_18_PWM_channel - 1].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
|
238
|
+ direct_table[P1_18_PWM_channel - 1].assigned = true;
|
|
239
|
+ return true;
|
|
240
|
+ case P2_04: // D9 FET, PWM1 channel 5 (Pin 9 P2_04 PWM1.5)
|
|
241
|
+ direct_table[P2_04_PWM_channel - 1].min = min;
|
|
242
|
+ direct_table[P2_04_PWM_channel - 1].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
|
243
|
+ direct_table[P2_04_PWM_channel - 1].assigned = true;
|
|
244
|
+ return true;
|
|
245
|
+ case P2_05: // D10 FET, PWM1 channel 6 (Pin 10 P2_05 PWM1.6)
|
|
246
|
+ direct_table[P2_05_PWM_channel - 1].min = min;
|
|
247
|
+ direct_table[P2_05_PWM_channel - 1].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
|
248
|
+ direct_table[P2_05_PWM_channel - 1].assigned = true;
|
|
249
|
+ return true;
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+//// interrupt controlled PWM code
|
|
253
|
+ NVIC_DisableIRQ(HAL_PWM_TIMER_IRQn); // make it safe to update the active table
|
235
|
254
|
// OK to update the active table because the
|
236
|
255
|
// ISR doesn't use any of the changed items
|
|
256
|
+
|
|
257
|
+ if (ISR_table_update) //use work table if that's the newest
|
|
258
|
+ temp_table = work_table;
|
|
259
|
+ else
|
|
260
|
+ temp_table = active_table;
|
|
261
|
+
|
237
|
262
|
uint8_t slot = 0;
|
238
|
|
- for (uint8_t i = 0; i < NUM_PWMS ; i++) // see if already in table
|
239
|
|
- if (ISR_table[i].pin == pin) {
|
240
|
|
- NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
|
|
263
|
+ for (uint8_t i = 0; i < NUM_ISR_PWMS; i++) // see if already in table
|
|
264
|
+ if (temp_table[i].pin == pin) {
|
|
265
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
241
|
266
|
return 1;
|
242
|
267
|
}
|
243
|
268
|
|
244
|
|
- for (uint8_t i = 1; (i < NUM_PWMS + 1) && !slot; i++) // find empty slot
|
245
|
|
- if ( !(ISR_table[i - 1].set_register)) { slot = i; break; } // any item that can't be zero when active or just attached is OK
|
246
|
|
- if (!slot) return 0;
|
|
269
|
+ for (uint8_t i = 1; (i < NUM_ISR_PWMS + 1) && !slot; i++) // find empty slot
|
|
270
|
+ if ( !(temp_table[i - 1].set_register)) { slot = i; break; } // any item that can't be zero when active or just attached is OK
|
|
271
|
+
|
|
272
|
+ if (!slot) {
|
|
273
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
|
274
|
+ return 0;
|
|
275
|
+ }
|
|
276
|
+
|
247
|
277
|
slot--; // turn it into array index
|
248
|
278
|
|
249
|
|
- ISR_table[slot].pin = pin; // init slot
|
250
|
|
- ISR_table[slot].PWM_mask = 0; // real value set by PWM_write
|
251
|
|
- ISR_table[slot].set_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET;
|
252
|
|
- ISR_table[slot].clr_register = PIN_IS_INVERTED(pin) ? &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET : &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR;
|
253
|
|
- ISR_table[slot].write_mask = LPC_PIN(LPC1768_PIN_PIN(pin));
|
254
|
|
- ISR_table[slot].microseconds = MICRO_MAX;
|
255
|
|
- ISR_table[slot].min = min;
|
256
|
|
- ISR_table[slot].max = MIN(max, LPC_PWM1_MR0 - MR0_MARGIN);
|
257
|
|
- ISR_table[slot].servo_index = servo_index;
|
258
|
|
- ISR_table[slot].active_flag = false;
|
|
279
|
+ temp_table[slot].pin = pin; // init slot
|
|
280
|
+ temp_table[slot].set_register = &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOSET;
|
|
281
|
+ temp_table[slot].clr_register = &LPC_GPIO(LPC1768_PIN_PORT(pin))->FIOCLR;
|
|
282
|
+ temp_table[slot].write_mask = LPC_PIN(LPC1768_PIN_PIN(pin));
|
|
283
|
+ temp_table[slot].min = min;
|
|
284
|
+ temp_table[slot].max = max; // different max for ISR PWMs than for direct PWMs
|
|
285
|
+ temp_table[slot].servo_index = servo_index;
|
|
286
|
+ temp_table[slot].active_flag = false;
|
259
|
287
|
|
260
|
|
- NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
|
|
288
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
261
|
289
|
|
262
|
290
|
return 1;
|
263
|
291
|
}
|
|
@@ -267,201 +295,154 @@ bool LPC1768_PWM_detach_pin(pin_t pin) {
|
267
|
295
|
|
268
|
296
|
pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));
|
269
|
297
|
|
270
|
|
- NVIC_DisableIRQ(PWM1_IRQn);
|
271
|
|
-
|
272
|
|
- uint8_t slot = 0xFF;
|
273
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) // find slot
|
274
|
|
- if (ISR_table[i].pin == pin) { slot = i; break; }
|
275
|
|
- if (slot == 0xFF) { // return error if pin not found
|
276
|
|
- NVIC_EnableIRQ(PWM1_IRQn);
|
277
|
|
- return false;
|
278
|
|
- }
|
279
|
|
-
|
280
|
|
- LPC1768_PWM_update_map_MR();
|
281
|
|
-
|
282
|
|
- // OK to make these changes before the MR0 interrupt
|
|
298
|
+//// direct control PWM code
|
283
|
299
|
switch(pin) {
|
284
|
300
|
case P1_23: // MKS Sbase Servo 0, PWM1 channel 4 (J3-8 PWM1.4)
|
|
301
|
+ if (!direct_table[P1_23_PWM_channel - 1].assigned) return false;
|
285
|
302
|
CBI(LPC_PWM1->PCR, 8 + P1_23_PWM_channel); // disable PWM1 module control of this pin
|
286
|
|
- map_MR[P1_23_PWM_channel - 1].PCR_bit = 0;
|
287
|
303
|
LPC_PINCON->PINSEL3 &= ~(0x3 << 14); // return pin to general purpose I/O
|
288
|
|
- map_MR[P1_23_PWM_channel - 1].PINSEL_bits = 0;
|
289
|
|
- map_MR[P1_23_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
|
290
|
|
- break;
|
|
304
|
+ direct_table[P1_23_PWM_channel - 1].assigned = false;
|
|
305
|
+ return true;
|
291
|
306
|
case P1_20: // Servo 0, PWM1 channel 2 (Pin 11 P1.20 PWM1.2)
|
|
307
|
+ if (!direct_table[P1_20_PWM_channel - 1].assigned) return false;
|
292
|
308
|
CBI(LPC_PWM1->PCR, 8 + P1_20_PWM_channel); // disable PWM1 module control of this pin
|
293
|
|
- map_MR[P1_20_PWM_channel - 1].PCR_bit = 0;
|
294
|
309
|
LPC_PINCON->PINSEL3 &= ~(0x3 << 8); // return pin to general purpose I/O
|
295
|
|
- map_MR[P1_20_PWM_channel - 1].PINSEL_bits = 0;
|
296
|
|
- map_MR[P1_20_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
|
297
|
|
- break;
|
|
310
|
+ direct_table[P1_20_PWM_channel - 1].assigned = false;
|
|
311
|
+ return true;
|
298
|
312
|
case P1_21: // Servo 1, PWM1 channel 3 (Pin 6 P1.21 PWM1.3)
|
|
313
|
+ if (!direct_table[P1_21_PWM_channel - 1].assigned) return false;
|
299
|
314
|
CBI(LPC_PWM1->PCR, 8 + P1_21_PWM_channel); // disable PWM1 module control of this pin
|
300
|
|
- map_MR[P1_21_PWM_channel - 1].PCR_bit = 0;
|
301
|
315
|
LPC_PINCON->PINSEL3 &= ~(0x3 << 10); // return pin to general purpose I/O
|
302
|
|
- map_MR[P1_21_PWM_channel - 1].PINSEL_bits = 0;
|
303
|
|
- map_MR[P1_21_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
|
304
|
|
- break;
|
|
316
|
+ direct_table[P1_21_PWM_channel - 1].assigned = false;
|
|
317
|
+ return true;
|
305
|
318
|
case P1_18: // Servo 3, PWM1 channel 1 (Pin 4 P1.18 PWM1.1)
|
|
319
|
+ if (!direct_table[P1_18_PWM_channel - 1].assigned) return false;
|
306
|
320
|
CBI(LPC_PWM1->PCR, 8 + P1_18_PWM_channel); // disable PWM1 module control of this pin
|
307
|
|
- map_MR[P1_18_PWM_channel - 1].PCR_bit = 0;
|
308
|
321
|
LPC_PINCON->PINSEL3 &= ~(0x3 << 4); // return pin to general purpose I/O
|
309
|
|
- map_MR[P1_18_PWM_channel - 1].PINSEL_bits = 0;
|
310
|
|
- map_MR[P1_18_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
|
311
|
|
- break;
|
|
322
|
+ direct_table[P1_18_PWM_channel - 1].assigned = false;
|
|
323
|
+ return true;
|
312
|
324
|
case P2_04: // D9 FET, PWM1 channel 5 (Pin 9 P2_04 PWM1.5)
|
|
325
|
+ if (!direct_table[P2_04_PWM_channel - 1].assigned) return false;
|
313
|
326
|
CBI(LPC_PWM1->PCR, 8 + P2_04_PWM_channel); // disable PWM1 module control of this pin
|
314
|
|
- map_MR[P2_04_PWM_channel - 1].PCR_bit = 0;
|
315
|
327
|
LPC_PINCON->PINSEL4 &= ~(0x3 << 10); // return pin to general purpose I/O
|
316
|
|
- map_MR[P2_04_PWM_channel - 1].PINSEL_bits = 0;
|
317
|
|
- map_MR[P2_04_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
|
318
|
|
- break;
|
|
328
|
+ direct_table[P2_04_PWM_channel - 1].assigned = false;
|
|
329
|
+ return true;
|
319
|
330
|
case P2_05: // D10 FET, PWM1 channel 6 (Pin 10 P2_05 PWM1.6)
|
|
331
|
+ if (!direct_table[P2_05_PWM_channel - 1].assigned) return false;
|
320
|
332
|
CBI(LPC_PWM1->PCR, 8 + P2_05_PWM_channel); // disable PWM1 module control of this pin
|
321
|
|
- map_MR[P2_05_PWM_channel - 1].PCR_bit = 0;
|
322
|
333
|
LPC_PINCON->PINSEL4 &= ~(0x3 << 4); // return pin to general purpose I/O
|
323
|
|
- map_MR[P2_05_PWM_channel - 1].PINSEL_bits = 0;
|
324
|
|
- map_MR[P2_05_PWM_channel - 1].map_PWM_INT = 0; // 0 - available for interrupts, 1 - in use by PWM
|
325
|
|
- break;
|
326
|
|
- default:
|
|
334
|
+ direct_table[P2_05_PWM_channel - 1].assigned = false;
|
|
335
|
+ return true;
|
|
336
|
+ }
|
|
337
|
+
|
|
338
|
+//// interrupt controlled PWM code
|
|
339
|
+ NVIC_DisableIRQ(HAL_PWM_TIMER_IRQn);
|
|
340
|
+
|
|
341
|
+ if (ISR_table_update) {
|
|
342
|
+ ISR_table_update = false; // don't update yet - have another update to do
|
|
343
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
|
344
|
+ }
|
|
345
|
+ else {
|
|
346
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
|
347
|
+ COPY_ACTIVE_TABLE; // copy active table into work table
|
|
348
|
+ }
|
|
349
|
+
|
|
350
|
+ uint8_t slot = 0xFF;
|
|
351
|
+ for (uint8_t i = 0; i < NUM_ISR_PWMS; i++) { // find slot
|
|
352
|
+ if (work_table[i].pin == pin) {
|
|
353
|
+ slot = i;
|
327
|
354
|
break;
|
|
355
|
+ }
|
328
|
356
|
}
|
|
357
|
+ if (slot == 0xFF) // return error if pin not found
|
|
358
|
+ return false;
|
329
|
359
|
|
330
|
|
- ISR_table[slot] = PWM_MAP_INIT_ROW;
|
|
360
|
+ work_table[slot] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
|
331
|
361
|
|
|
362
|
+ LPC1768_PWM_sort(); // sort table by microseconds
|
332
|
363
|
ISR_table_update = true;
|
333
|
|
- NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
|
334
|
|
-
|
335
|
|
- return 1;
|
|
364
|
+ return true;
|
336
|
365
|
}
|
337
|
366
|
|
338
|
|
-
|
|
367
|
+// value is 0-20,000 microseconds (0% to 100% duty cycle)
|
|
368
|
+// servo routine provides values in the 544 - 2400 range
|
339
|
369
|
bool LPC1768_PWM_write(pin_t pin, uint32_t value) {
|
340
|
370
|
|
341
|
371
|
pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));
|
342
|
372
|
|
343
|
|
- NVIC_DisableIRQ(PWM1_IRQn);
|
344
|
|
-
|
345
|
|
- uint8_t slot = 0xFF;
|
346
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) // find slot
|
347
|
|
- if (ISR_table[i].pin == pin) { slot = i; break; }
|
348
|
|
- if (slot == 0xFF) { // return error if pin not found
|
349
|
|
- NVIC_EnableIRQ(PWM1_IRQn);
|
350
|
|
- return false;
|
351
|
|
- }
|
352
|
|
-
|
353
|
|
- LPC1768_PWM_update_map_MR();
|
354
|
|
-
|
|
373
|
+//// direct control PWM code
|
355
|
374
|
switch(pin) {
|
356
|
375
|
case P1_23: // MKS Sbase Servo 0, PWM1 channel 4 (J3-8 PWM1.4)
|
357
|
|
- map_MR[P1_23_PWM_channel - 1].PCR_bit = _BV(8 + P1_23_PWM_channel); // enable PWM1 module control of this pin
|
358
|
|
- map_MR[P1_23_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
|
359
|
|
- map_MR[P1_23_PWM_channel - 1].PINSEL_bits = 0x2 << 14; // ISR must do this AFTER setting PCR
|
360
|
|
- break;
|
|
376
|
+ if (!direct_table[P1_23_PWM_channel - 1].assigned) return false;
|
|
377
|
+ LPC_PWM1->PCR |= _BV(8 + P1_23_PWM_channel); // enable PWM1 module control of this pin
|
|
378
|
+ LPC_PINCON->PINSEL3 = 0x2 << 14; // must set pin function AFTER setting PCR
|
|
379
|
+ // load the new time value
|
|
380
|
+ LPC_PWM1->MR4 = MAX(MIN(value, direct_table[P1_23_PWM_channel - 1].max), direct_table[P1_23_PWM_channel - 1].min);
|
|
381
|
+ LPC_PWM1->LER = 0x1 << P1_23_PWM_channel; // Set the latch Enable Bit to load the new Match Value on the next MR0
|
|
382
|
+ return true;
|
361
|
383
|
case P1_20: // Servo 0, PWM1 channel 2 (Pin 11 P1.20 PWM1.2)
|
362
|
|
- map_MR[P1_20_PWM_channel - 1].PCR_bit = _BV(8 + P1_20_PWM_channel); // enable PWM1 module control of this pin
|
363
|
|
- map_MR[P1_20_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
|
364
|
|
- map_MR[P1_20_PWM_channel - 1].PINSEL_bits = 0x2 << 8; // ISR must do this AFTER setting PCR
|
365
|
|
- break;
|
|
384
|
+ if (!direct_table[P1_20_PWM_channel - 1].assigned) return false;
|
|
385
|
+ LPC_PWM1->PCR |= _BV(8 + P1_20_PWM_channel); // enable PWM1 module control of this pin
|
|
386
|
+ LPC_PINCON->PINSEL3 |= 0x2 << 8; // must set pin function AFTER setting PCR
|
|
387
|
+ // load the new time value
|
|
388
|
+ LPC_PWM1->MR2 = MAX(MIN(value, direct_table[P1_20_PWM_channel - 1].max), direct_table[P1_20_PWM_channel - 1].min);
|
|
389
|
+ LPC_PWM1->LER = 0x1 << P1_20_PWM_channel; // Set the latch Enable Bit to load the new Match Value on the next MR0
|
|
390
|
+ return true;
|
366
|
391
|
case P1_21: // Servo 1, PWM1 channel 3 (Pin 6 P1.21 PWM1.3)
|
367
|
|
- map_MR[P1_21_PWM_channel - 1].PCR_bit = _BV(8 + P1_21_PWM_channel); // enable PWM1 module control of this pin
|
368
|
|
- map_MR[P1_21_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
|
369
|
|
- map_MR[P1_21_PWM_channel - 1].PINSEL_bits = 0x2 << 10; // ISR must do this AFTER setting PCR
|
370
|
|
- break;
|
|
392
|
+ if (!direct_table[P1_21_PWM_channel - 1].assigned) return false;
|
|
393
|
+ LPC_PWM1->PCR |= _BV(8 + P1_21_PWM_channel); // enable PWM1 module control of this pin
|
|
394
|
+ LPC_PINCON->PINSEL3 |= 0x2 << 10; // must set pin function AFTER setting PCR
|
|
395
|
+ // load the new time value
|
|
396
|
+ LPC_PWM1->MR3 = MAX(MIN(value, direct_table[P1_21_PWM_channel - 1].max), direct_table[P1_21_PWM_channel - 1].min);
|
|
397
|
+ LPC_PWM1->LER = 0x1 << P1_21_PWM_channel; // Set the latch Enable Bit to load the new Match Value on the next MR0
|
|
398
|
+ return true;
|
371
|
399
|
case P1_18: // Servo 3, PWM1 channel 1 (Pin 4 P1.18 PWM1.1)
|
372
|
|
- map_MR[P1_18_PWM_channel - 1].PCR_bit = _BV(8 + P1_18_PWM_channel); // enable PWM1 module control of this pin
|
373
|
|
- map_MR[P1_18_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL3;
|
374
|
|
- map_MR[P1_18_PWM_channel - 1].PINSEL_bits = 0x2 << 4; // ISR must do this AFTER setting PCR
|
375
|
|
- break;
|
|
400
|
+ if (!direct_table[P1_18_PWM_channel - 1].assigned) return false;
|
|
401
|
+ LPC_PWM1->PCR |= _BV(8 + P1_18_PWM_channel); // enable PWM1 module control of this pin
|
|
402
|
+ LPC_PINCON->PINSEL3 |= 0x2 << 4; // must set pin function AFTER setting PCR
|
|
403
|
+ // load the new time value
|
|
404
|
+ LPC_PWM1->MR1 = MAX(MIN(value, direct_table[P1_18_PWM_channel - 1].max), direct_table[P1_18_PWM_channel - 1].min);
|
|
405
|
+ LPC_PWM1->LER = 0x1 << P1_18_PWM_channel; // Set the latch Enable Bit to load the new Match Value on the next MR0
|
|
406
|
+ return true;
|
376
|
407
|
case P2_04: // D9 FET, PWM1 channel 5 (Pin 9 P2_04 PWM1.5)
|
377
|
|
- map_MR[P2_04_PWM_channel - 1].PCR_bit = _BV(8 + P2_04_PWM_channel); // enable PWM1 module control of this pin
|
378
|
|
- map_MR[P2_04_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL4;
|
379
|
|
- map_MR[P2_04_PWM_channel - 1].PINSEL_bits = 0x1 << 8; // ISR must do this AFTER setting PCR
|
380
|
|
- break;
|
|
408
|
+ if (!direct_table[P2_04_PWM_channel - 1].assigned) return false;
|
|
409
|
+ LPC_PWM1->PCR |= _BV(8 + P2_04_PWM_channel); // enable PWM1 module control of this pin
|
|
410
|
+ LPC_PINCON->PINSEL4 |= 0x1 << 8; // must set pin function AFTER setting PCR
|
|
411
|
+ // load the new time value
|
|
412
|
+ LPC_PWM1->MR5 = MAX(MIN(value, direct_table[P2_04_PWM_channel - 1].max), direct_table[P2_04_PWM_channel - 1].min);
|
|
413
|
+ LPC_PWM1->LER = 0x1 << P2_04_PWM_channel; // Set the latch Enable Bit to load the new Match Value on the next MR0
|
|
414
|
+ return true;
|
381
|
415
|
case P2_05: // D10 FET, PWM1 channel 6 (Pin 10 P2_05 PWM1.6)
|
382
|
|
- map_MR[P2_05_PWM_channel - 1].PCR_bit = _BV(8 + P2_05_PWM_channel); // enable PWM1 module control of this pin
|
383
|
|
- map_MR[P2_05_PWM_channel - 1].PINSEL_reg = &LPC_PINCON->PINSEL4;
|
384
|
|
- map_MR[P2_05_PWM_channel - 1].PINSEL_bits = 0x1 << 10; // ISR must do this AFTER setting PCR
|
385
|
|
- break;
|
386
|
|
- default: // ISR pins
|
387
|
|
- pinMode(pin, OUTPUT); // set pin to output
|
388
|
|
- break;
|
|
416
|
+ if (!direct_table[P2_05_PWM_channel - 1].assigned) return false;
|
|
417
|
+ LPC_PWM1->PCR |= _BV(8 + P2_05_PWM_channel); // enable PWM1 module control of this pin
|
|
418
|
+ LPC_PINCON->PINSEL4 |= 0x1 << 10; // must set pin function AFTER setting PCR
|
|
419
|
+ // load the new time value
|
|
420
|
+ LPC_PWM1->MR6 = MAX(MIN(value, direct_table[P2_05_PWM_channel - 1].max), direct_table[P2_05_PWM_channel - 1].min);
|
|
421
|
+ LPC_PWM1->LER = 0x1 << P2_05_PWM_channel; // Set the latch Enable Bit to load the new Match Value on the next MR0
|
|
422
|
+ return true;
|
389
|
423
|
}
|
390
|
424
|
|
391
|
|
- ISR_table[slot].microseconds = MAX(MIN(value, ISR_table[slot].max), ISR_table[slot].min);
|
392
|
|
- ISR_table[slot].active_flag = 1;
|
393
|
|
-
|
394
|
|
- ISR_table_update = true;
|
395
|
|
-
|
396
|
|
- NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
|
397
|
|
-
|
398
|
|
- return 1;
|
399
|
|
-}
|
|
425
|
+//// interrupt controlled PWM code
|
|
426
|
+ NVIC_DisableIRQ(HAL_PWM_TIMER_IRQn);
|
|
427
|
+ if (!ISR_table_update) // use the most up to date table
|
|
428
|
+ COPY_ACTIVE_TABLE; // copy active table into work table
|
400
|
429
|
|
401
|
|
-
|
402
|
|
-uint32_t LPC1768_PWM_interrupt_mask = 1;
|
403
|
|
-
|
404
|
|
-
|
405
|
|
-void LPC1768_PWM_update(void) { // only called by the ISR
|
406
|
|
- LPC1768_PWM_interrupt_mask = 0; // set match registers to new values, build IRQ mask
|
407
|
|
- // first setup directly controlled PWM pin slots
|
408
|
|
-
|
409
|
|
- bool found;
|
410
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) {
|
411
|
|
- ISR_table[i].PCR_bit = 0; // clear entries
|
412
|
|
- ISR_table[i].PINSEL_reg = 0;
|
413
|
|
- ISR_table[i].PINSEL_bits = 0;
|
414
|
|
- ISR_table[i].PWM_flag = 1; // mark slot as interrupt mode until find differently
|
415
|
|
-
|
416
|
|
- if (ISR_table[i].active_flag) {
|
417
|
|
- ISR_table[i].sequence = i + 1;
|
418
|
|
-
|
419
|
|
- // first see if there is a PWM1 controlled pin for this entry
|
420
|
|
- found = false;
|
421
|
|
- for (uint8_t j = 0; (j < NUM_PWMS) && !found; j++) {
|
422
|
|
- if ( (map_MR[j].map_PWM_PIN == ISR_table[i].pin)) {
|
423
|
|
- map_MR[j].map_PWM_INT = 1; // flag that it's already setup for direct control
|
424
|
|
- ISR_table[i].PWM_mask = 0;
|
425
|
|
- ISR_table[i].PCR_bit = map_MR[j].PCR_bit; // PCR register bit to enable PWM1 control of this pin
|
426
|
|
- ISR_table[i].PINSEL_reg = map_MR[j].PINSEL_reg; // PINSEL register address to set pin mode to PWM1 control} MR_map;
|
427
|
|
- ISR_table[i].PINSEL_bits = map_MR[j].PINSEL_bits; // PINSEL register bits to set pin mode to PWM1 control} MR_map;
|
428
|
|
- map_MR[j].map_used = 2;
|
429
|
|
- ISR_table[i].PWM_flag = 0;
|
430
|
|
- *map_MR[j].MR_register = ISR_table[i].microseconds;
|
431
|
|
- found = true;
|
432
|
|
- }
|
433
|
|
- }
|
434
|
|
- }
|
435
|
|
- else
|
436
|
|
- ISR_table[i].sequence = 0;
|
|
430
|
+ uint8_t slot = 0xFF;
|
|
431
|
+ for (uint8_t i = 0; i < NUM_ISR_PWMS; i++) // find slot
|
|
432
|
+ if (work_table[i].pin == pin) { slot = i; break; }
|
|
433
|
+ if (slot == 0xFF) { // return error if pin not found
|
|
434
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn);
|
|
435
|
+ return false;
|
437
|
436
|
}
|
438
|
437
|
|
439
|
|
- // next fill in interrupt slots
|
440
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) {
|
441
|
|
-
|
442
|
|
- if (ISR_table[i].active_flag && ISR_table[i].PWM_flag) {
|
443
|
|
-
|
444
|
|
- // setup interrupt slot
|
445
|
|
- found = false;
|
446
|
|
- for (uint8_t k = 0; (k < NUM_PWMS) && !found; k++) {
|
447
|
|
- if ( !(map_MR[k].map_PWM_INT || map_MR[k].map_used)) {
|
448
|
|
- *map_MR[k].MR_register = ISR_table[i].microseconds; // found one for an interrupt pin
|
449
|
|
- map_MR[k].map_used = 1;
|
450
|
|
- LPC1768_PWM_interrupt_mask |= _BV(3 * (k + 1)); // set bit in the MCR to enable this MR to generate an interrupt
|
451
|
|
- 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;
|
452
|
|
- 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;
|
453
|
|
- ISR_table[i].write_mask = LPC_PIN(LPC1768_PIN_PIN(ISR_table[i].pin));
|
454
|
|
- ISR_table[i].PWM_mask = _BV(IR_BIT(k + 1)); // bit in the IR that will go active when this MR generates an interrupt
|
455
|
|
- ISR_table[i].PWM_flag = 1;
|
456
|
|
- found = true;
|
457
|
|
- }
|
458
|
|
- }
|
459
|
|
- }
|
460
|
|
- }
|
|
438
|
+ work_table[slot].microseconds = MAX(MIN(value, work_table[slot].max), work_table[slot].min);;
|
|
439
|
+ work_table[slot].active_flag = true;
|
461
|
440
|
|
462
|
|
- LPC1768_PWM_interrupt_mask |= (uint32_t) _BV(0); // add in MR0 interrupt
|
|
441
|
+ LPC1768_PWM_sort(); // sort table by microseconds
|
|
442
|
+ ISR_table_update = true;
|
463
|
443
|
|
464
|
|
- LPC_PWM1->LER = 0x07E; // Set the latch Enable Bits to load the new Match Values for MR1 - MR6
|
|
444
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
|
445
|
+ return 1;
|
465
|
446
|
}
|
466
|
447
|
|
467
|
448
|
|
|
@@ -469,97 +450,102 @@ bool useable_hardware_PWM(pin_t pin) {
|
469
|
450
|
|
470
|
451
|
pin = GET_PIN_MAP_PIN(GET_PIN_MAP_INDEX(pin & 0xFF));
|
471
|
452
|
|
472
|
|
- NVIC_DisableIRQ(PWM1_IRQn);
|
|
453
|
+ NVIC_DisableIRQ(HAL_PWM_TIMER_IRQn);
|
473
|
454
|
|
474
|
455
|
bool return_flag = false;
|
475
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) // see if it's already setup
|
476
|
|
- if (ISR_table[i].pin == pin && ISR_table[i].sequence) return_flag = true;
|
477
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) // see if there is an empty slot
|
478
|
|
- if (!ISR_table[i].sequence) return_flag = true;
|
479
|
|
- NVIC_EnableIRQ(PWM1_IRQn); // re-enable PWM interrupts
|
|
456
|
+ for (uint8_t i = 0; i < NUM_ISR_PWMS; i++) // see if it's already setup
|
|
457
|
+ if (active_table[i].pin == pin) return_flag = true;
|
|
458
|
+ for (uint8_t i = 0; i < NUM_ISR_PWMS; i++) // see if there is an empty slot
|
|
459
|
+ if (!active_table[i].set_register) return_flag = true;
|
|
460
|
+ NVIC_EnableIRQ(HAL_PWM_TIMER_IRQn); // re-enable PWM interrupts
|
480
|
461
|
return return_flag;
|
481
|
462
|
}
|
482
|
463
|
|
|
464
|
+
|
483
|
465
|
////////////////////////////////////////////////////////////////////////////////
|
484
|
466
|
|
485
|
|
-#define HAL_PWM_LPC1768_ISR extern "C" void PWM1_IRQHandler(void)
|
486
|
467
|
|
487
|
|
-// Both loops could be terminated when the last active channel is found but that would
|
488
|
|
-// result in variations ISR run time which results in variations in pulse width
|
|
468
|
+#define PWM_LPC1768_ISR_SAFETY_FACTOR 5 // amount of time needed to guarantee MR1 count will be above TC
|
|
469
|
+volatile bool in_PWM_isr = false;
|
489
|
470
|
|
490
|
|
-/**
|
491
|
|
- * Changes to PINSEL, PCR and MCR are only done during the MR0 interrupt otherwise
|
492
|
|
- * the wrong pin may be toggled or even have the system hang.
|
493
|
|
- */
|
|
471
|
+HAL_PWM_TIMER_ISR {
|
|
472
|
+ bool first_active_entry = true;
|
|
473
|
+ uint32_t next_MR1_val;
|
494
|
474
|
|
|
475
|
+ if (in_PWM_isr) goto exit_PWM_ISR; // prevent re-entering this ISR
|
|
476
|
+ in_PWM_isr = true;
|
495
|
477
|
|
496
|
|
-HAL_PWM_LPC1768_ISR {
|
497
|
|
-
|
498
|
|
- if (LPC_PWM1->IR & 0x1) { // MR0 interrupt
|
499
|
|
- if (ISR_table_update) { // new values have been loaded so set everything
|
500
|
|
- LPC1768_PWM_update(); // update & swap table
|
501
|
|
- LPC_PWM1->MCR = LPC1768_PWM_interrupt_mask; // enable new PWM individual channel interrupts
|
|
478
|
+ if (HAL_PWM_TIMER->IR & 0x01) { // MR0 interrupt
|
|
479
|
+ next_MR1_val = first_MR1_value; // only used if have a blank ISR table
|
|
480
|
+ if (ISR_table_update) { // new values have been loaded so swap tables
|
|
481
|
+ temp_table = active_table;
|
|
482
|
+ active_table = work_table;
|
|
483
|
+ work_table = temp_table;
|
|
484
|
+ ISR_table_update = false;
|
502
|
485
|
}
|
503
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++) {
|
504
|
|
- if (ISR_table[i].active_flag && !((ISR_table[i].pin == P1_20) ||
|
505
|
|
- (ISR_table[i].pin == P1_21) ||
|
506
|
|
- (ISR_table[i].pin == P1_18) ||
|
507
|
|
- (ISR_table[i].pin == P2_04) ||
|
508
|
|
- (ISR_table[i].pin == P2_05))
|
509
|
|
- ) {
|
510
|
|
- *ISR_table[i].set_register = ISR_table[i].write_mask; // set pins for all enabled interrupt channels active
|
|
486
|
+ }
|
|
487
|
+ HAL_PWM_TIMER->IR = 0x3F; // clear all interrupts
|
|
488
|
+
|
|
489
|
+ for (uint8_t i = 0; i < NUM_ISR_PWMS; i++) {
|
|
490
|
+ if (active_table[i].active_flag) {
|
|
491
|
+ if (first_active_entry) {
|
|
492
|
+ first_active_entry = false;
|
|
493
|
+ next_MR1_val = active_table[i].microseconds;
|
511
|
494
|
}
|
512
|
|
- if (ISR_table_update && ISR_table[i].PCR_bit) {
|
513
|
|
- LPC_PWM1->PCR |= ISR_table[i].PCR_bit; // enable PWM1 module control of this pin
|
514
|
|
- *ISR_table[i].PINSEL_reg |= ISR_table[i].PINSEL_bits; // set pin mode to PWM1 control - must be done after PCR
|
|
495
|
+ if (HAL_PWM_TIMER->TC < active_table[i].microseconds) {
|
|
496
|
+ *active_table[i].set_register = active_table[i].write_mask; // set pin high
|
515
|
497
|
}
|
516
|
|
- }
|
517
|
|
- ISR_table_update = false;
|
518
|
|
- LPC_PWM1->IR = 0x01; // clear the MR0 interrupt flag bit
|
519
|
|
- }
|
520
|
|
- else {
|
521
|
|
- for (uint8_t i = 0; i < NUM_PWMS; i++)
|
522
|
|
- if (ISR_table[i].active_flag && (LPC_PWM1->IR & ISR_table[i].PWM_mask)) {
|
523
|
|
- LPC_PWM1->IR = ISR_table[i].PWM_mask; // clear the interrupt flag bits for expected interrupts
|
524
|
|
- *ISR_table[i].clr_register = ISR_table[i].write_mask; // set channel to inactive
|
|
498
|
+ else {
|
|
499
|
+ *active_table[i].clr_register = active_table[i].write_mask; // set pin low
|
|
500
|
+ next_MR1_val = (i == NUM_ISR_PWMS -1)
|
|
501
|
+ ? LPC_PWM1_MR0 + 1 // done with table, wait for MR0
|
|
502
|
+ : active_table[i + 1].microseconds; // set next MR1 interrupt?
|
525
|
503
|
}
|
|
504
|
+ }
|
526
|
505
|
}
|
527
|
|
-
|
528
|
|
- LPC_PWM1->IR = 0x70F; // guarantees all interrupt flags are cleared which, if there is an unexpected
|
529
|
|
- // PWM interrupt, will keep the ISR from hanging which will crash the controller
|
|
506
|
+ if (first_active_entry) next_MR1_val = LPC_PWM1_MR0 + 1; // empty table so disable MR1 interrupt
|
|
507
|
+ HAL_PWM_TIMER->MR1 = MAX(next_MR1_val, HAL_PWM_TIMER->TC + PWM_LPC1768_ISR_SAFETY_FACTOR); // set next
|
|
508
|
+ in_PWM_isr = false;
|
|
509
|
+
|
|
510
|
+ exit_PWM_ISR:
|
|
511
|
+ return;
|
530
|
512
|
}
|
531
|
|
-
|
532
|
513
|
#endif
|
533
|
514
|
|
|
515
|
+
|
534
|
516
|
/////////////////////////////////////////////////////////////////
|
535
|
517
|
///////////////// HARDWARE FIRMWARE INTERACTION ////////////////
|
536
|
518
|
/////////////////////////////////////////////////////////////////
|
537
|
519
|
|
538
|
520
|
/**
|
539
|
|
- * Almost all changes to the hardware registers must be coordinated with the Match Register 0 (MR0)
|
540
|
|
- * interrupt. The only exception is detaching pins. It doesn't matter when they go
|
541
|
|
- * tristate.
|
|
521
|
+ * There are two distinct systems used for PWMs:
|
|
522
|
+ * directly controlled pins
|
|
523
|
+ * ISR controlled pins.
|
542
|
524
|
*
|
543
|
|
- * The LPC1768_PWM_init routine kicks off the MR0 interrupt. This interrupt is never disabled. It
|
544
|
|
- * can be delayed by higher priority interrupts. Actions on directly controlled pins are not delayed
|
545
|
|
- * by other interrupts
|
|
525
|
+ * The two systems are independent of each other. The use the same counter frequency so there's no
|
|
526
|
+ * translation needed when setting the time values. The init, attach, detach and write routines all
|
|
527
|
+ * start with the direct pin code which is followed by the ISR pin code.
|
546
|
528
|
*
|
547
|
|
- * The ISR_table_update flag is set when the ISR table needs to be rebuilt. It is
|
548
|
|
- * cleared by the ISR during the MR0 interrupt after it rebuilds the ISR table.
|
|
529
|
+ * The PMW1 module handles the directly controlled pins. Each directly controlled pin is associated
|
|
530
|
+ * with a match register (MR1 - MR6). When the associated MR equals the module's TIMER/COUNTER (TC)
|
|
531
|
+ * then the pins is set to low. The MR0 register controls the repetition rate. When the TC equals
|
|
532
|
+ * MR0 then the TC is reset and ALL directly controlled pins are set high. The resulting pulse widths
|
|
533
|
+ * are almost immune to system loading and ISRs. No PWM1 interrupts are used.
|
549
|
534
|
*
|
550
|
|
- * The sequence of events during a write to a PWM channel is:
|
551
|
|
- * 1) Attach routine puts the pin number in the ISR table but doesn't mark it active.
|
552
|
|
- * 2) Write routine marks the pin as active, updates the helper table and flags the ISR that the
|
553
|
|
- * ISR table needs to be rebuilt.
|
554
|
|
- * 3) On the MR0 interrupt the ISR:
|
555
|
|
- * a. Rebuilds the ISR table if needed.
|
556
|
|
- * MR1-MR6 are updated at this time. The updates aren't put into use until the first
|
557
|
|
- * MR0 after the LER register has been written. The LER register is written during the
|
558
|
|
- * table rebuild process. The result is new timing takes 20-40 mS to be implemented.
|
559
|
|
- * b. Sets the interrupt controlled pin(s) to their active state
|
560
|
|
- * c. Writes to the PCR register to enable the directly controlled pins
|
561
|
|
- * d. Sets the PINSEL register to the function/mode for the directly controlled pins
|
|
535
|
+ * The ISR controlled pins use the TIMER/COUNTER, MR0 and MR1 registers from one timer. MR0 controls
|
|
536
|
+ * period of the controls the repetition rate. When the TC equals MR0 then the TC is reset and an
|
|
537
|
+ * interrupt is generated. When the TC equals MR1 then an interrupt is generated.
|
562
|
538
|
*
|
563
|
|
- * 4) For each interrupt controlled pin there is another ISR call. During this call the pin is set
|
564
|
|
- * to its inactive state. The call is initiated when a MR1-MR6 reg times out.
|
|
539
|
+ * Each interrupt does the following:
|
|
540
|
+ * 1) Swaps the tables if it's a MR0 interrupt and the swap flag is set. It then clears the swap flag.
|
|
541
|
+ * 2) Scans the entire ISR table (it's been sorted low to high time)
|
|
542
|
+ * a. If its the first active entry then it grabs the time as a tentative time for MR1
|
|
543
|
+ * b. If active and TC is less than the time then it sets the pin high
|
|
544
|
+ * c. If active and TC is more than the time it sets the pin high
|
|
545
|
+ * d. On every entry that sets a pin low it grabs the NEXT entry's time for use as the next MR1.
|
|
546
|
+ * This results in MR1 being set to the time in the first active entry that does NOT set a
|
|
547
|
+ * pin low.
|
|
548
|
+ * e. If it's setting the last entry's pin low then it sets MR1 to a value bigger than MR0
|
|
549
|
+ * f. If no value has been grabbed for the next MR1 then it's an empty table and MR1 is set to a
|
|
550
|
+ * value greater than MR0
|
565
|
551
|
*/
|