|
@@ -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
|
*/
|