|
@@ -25,10 +25,15 @@
|
25
|
25
|
#include "../../inc/MarlinConfig.h"
|
26
|
26
|
#include "HardwareSerial.h"
|
27
|
27
|
|
28
|
|
-HardwareSerial Serial = HardwareSerial(LPC_UART0);
|
29
|
|
-HardwareSerial Serial1 = HardwareSerial((LPC_UART_TypeDef *) LPC_UART1);
|
30
|
|
-HardwareSerial Serial2 = HardwareSerial(LPC_UART2);
|
31
|
|
-HardwareSerial Serial3 = HardwareSerial(LPC_UART3);
|
|
28
|
+#if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
|
|
29
|
+ HardwareSerial Serial = HardwareSerial(LPC_UART0);
|
|
30
|
+#elif SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
|
|
31
|
+ HardwareSerial Serial1 = HardwareSerial((LPC_UART_TypeDef *) LPC_UART1);
|
|
32
|
+#elif SERIAL_PORT == 2 || SERIAL_PORT_2 == 2
|
|
33
|
+ HardwareSerial Serial2 = HardwareSerial(LPC_UART2);
|
|
34
|
+#elif SERIAL_PORT == 3 || SERIAL_PORT_2 == 3
|
|
35
|
+ HardwareSerial Serial3 = HardwareSerial(LPC_UART3);
|
|
36
|
+#endif
|
32
|
37
|
|
33
|
38
|
void HardwareSerial::begin(uint32_t baudrate) {
|
34
|
39
|
|
|
@@ -37,9 +42,7 @@ void HardwareSerial::begin(uint32_t baudrate) {
|
37
|
42
|
UART_FIFO_CFG_Type FIFOConfig;
|
38
|
43
|
|
39
|
44
|
if (UARTx == LPC_UART0) {
|
40
|
|
- /*
|
41
|
|
- * Initialize UART0 pin connect
|
42
|
|
- */
|
|
45
|
+ // Initialize UART0 pin connect
|
43
|
46
|
PinCfg.Funcnum = 1;
|
44
|
47
|
PinCfg.OpenDrain = 0;
|
45
|
48
|
PinCfg.Pinmode = 0;
|
|
@@ -48,11 +51,8 @@ void HardwareSerial::begin(uint32_t baudrate) {
|
48
|
51
|
PINSEL_ConfigPin(&PinCfg);
|
49
|
52
|
PinCfg.Pinnum = 3;
|
50
|
53
|
PINSEL_ConfigPin(&PinCfg);
|
51
|
|
- }
|
52
|
|
- else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
|
53
|
|
- /*
|
54
|
|
- * Initialize UART1 pin connect
|
55
|
|
- */
|
|
54
|
+ } else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
|
|
55
|
+ // Initialize UART1 pin connect
|
56
|
56
|
PinCfg.Funcnum = 1;
|
57
|
57
|
PinCfg.OpenDrain = 0;
|
58
|
58
|
PinCfg.Pinmode = 0;
|
|
@@ -61,11 +61,8 @@ void HardwareSerial::begin(uint32_t baudrate) {
|
61
|
61
|
PINSEL_ConfigPin(&PinCfg);
|
62
|
62
|
PinCfg.Pinnum = 16;
|
63
|
63
|
PINSEL_ConfigPin(&PinCfg);
|
64
|
|
- }
|
65
|
|
- else if (UARTx == LPC_UART2) {
|
66
|
|
- /*
|
67
|
|
- * Initialize UART2 pin connect
|
68
|
|
- */
|
|
64
|
+ } else if (UARTx == LPC_UART2) {
|
|
65
|
+ // Initialize UART2 pin connect
|
69
|
66
|
PinCfg.Funcnum = 1;
|
70
|
67
|
PinCfg.OpenDrain = 0;
|
71
|
68
|
PinCfg.Pinmode = 0;
|
|
@@ -74,11 +71,8 @@ void HardwareSerial::begin(uint32_t baudrate) {
|
74
|
71
|
PINSEL_ConfigPin(&PinCfg);
|
75
|
72
|
PinCfg.Pinnum = 11;
|
76
|
73
|
PINSEL_ConfigPin(&PinCfg);
|
77
|
|
- }
|
78
|
|
- else if (UARTx == LPC_UART3) {
|
79
|
|
- /*
|
80
|
|
- * Initialize UART2 pin connect
|
81
|
|
- */
|
|
74
|
+ } else if (UARTx == LPC_UART3) {
|
|
75
|
+ // Initialize UART2 pin connect
|
82
|
76
|
PinCfg.Funcnum = 1;
|
83
|
77
|
PinCfg.OpenDrain = 0;
|
84
|
78
|
PinCfg.Pinmode = 0;
|
|
@@ -114,14 +108,10 @@ void HardwareSerial::begin(uint32_t baudrate) {
|
114
|
108
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, ENABLE);
|
115
|
109
|
UART_IntConfig(UARTx, UART_INTCFG_RLS, ENABLE);
|
116
|
110
|
|
117
|
|
- if (UARTx == LPC_UART0)
|
118
|
|
- NVIC_EnableIRQ(UART0_IRQn);
|
119
|
|
- else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1)
|
120
|
|
- NVIC_EnableIRQ(UART1_IRQn);
|
121
|
|
- else if (UARTx == LPC_UART2)
|
122
|
|
- NVIC_EnableIRQ(UART2_IRQn);
|
123
|
|
- else if (UARTx == LPC_UART3)
|
124
|
|
- NVIC_EnableIRQ(UART3_IRQn);
|
|
111
|
+ if (UARTx == LPC_UART0) NVIC_EnableIRQ(UART0_IRQn);
|
|
112
|
+ else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) NVIC_EnableIRQ(UART1_IRQn);
|
|
113
|
+ else if (UARTx == LPC_UART2) NVIC_EnableIRQ(UART2_IRQn);
|
|
114
|
+ else if (UARTx == LPC_UART3) NVIC_EnableIRQ(UART3_IRQn);
|
125
|
115
|
|
126
|
116
|
RxQueueWritePos = RxQueueReadPos = 0;
|
127
|
117
|
#if TX_BUFFER_SIZE > 0
|
|
@@ -132,14 +122,14 @@ void HardwareSerial::begin(uint32_t baudrate) {
|
132
|
122
|
int HardwareSerial::peek() {
|
133
|
123
|
int byte = -1;
|
134
|
124
|
|
135
|
|
- /* Temporarily lock out UART receive interrupts during this read so the UART receive
|
136
|
|
- interrupt won't cause problems with the index values */
|
|
125
|
+ // Temporarily lock out UART receive interrupts during this read so the UART receive
|
|
126
|
+ // interrupt won't cause problems with the index values
|
137
|
127
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, DISABLE);
|
138
|
128
|
|
139
|
129
|
if (RxQueueReadPos != RxQueueWritePos)
|
140
|
130
|
byte = RxBuffer[RxQueueReadPos];
|
141
|
131
|
|
142
|
|
- /* Re-enable UART interrupts */
|
|
132
|
+ // Re-enable UART interrupts
|
143
|
133
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, ENABLE);
|
144
|
134
|
|
145
|
135
|
return byte;
|
|
@@ -148,8 +138,8 @@ int HardwareSerial::peek() {
|
148
|
138
|
int HardwareSerial::read() {
|
149
|
139
|
int byte = -1;
|
150
|
140
|
|
151
|
|
- /* Temporarily lock out UART receive interrupts during this read so the UART receive
|
152
|
|
- interrupt won't cause problems with the index values */
|
|
141
|
+ // Temporarily lock out UART receive interrupts during this read so the UART receive
|
|
142
|
+ // interrupt won't cause problems with the index values
|
153
|
143
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, DISABLE);
|
154
|
144
|
|
155
|
145
|
if (RxQueueReadPos != RxQueueWritePos) {
|
|
@@ -157,53 +147,52 @@ int HardwareSerial::read() {
|
157
|
147
|
RxQueueReadPos = (RxQueueReadPos + 1) % RX_BUFFER_SIZE;
|
158
|
148
|
}
|
159
|
149
|
|
160
|
|
- /* Re-enable UART interrupts */
|
|
150
|
+ // Re-enable UART interrupts
|
161
|
151
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, ENABLE);
|
162
|
152
|
|
163
|
153
|
return byte;
|
164
|
154
|
}
|
165
|
155
|
|
166
|
156
|
size_t HardwareSerial::write(uint8_t send) {
|
167
|
|
- #if TX_BUFFER_SIZE > 0
|
168
|
|
- size_t bytes = 0;
|
169
|
|
- uint32_t fifolvl = 0;
|
|
157
|
+#if TX_BUFFER_SIZE > 0
|
|
158
|
+ size_t bytes = 0;
|
|
159
|
+ uint32_t fifolvl = 0;
|
170
|
160
|
|
171
|
|
- /* If the Tx Buffer is full, wait for space to clear */
|
172
|
|
- if ((TxQueueWritePos+1) % TX_BUFFER_SIZE == TxQueueReadPos) flushTX();
|
|
161
|
+ // If the Tx Buffer is full, wait for space to clear
|
|
162
|
+ if ((TxQueueWritePos+1) % TX_BUFFER_SIZE == TxQueueReadPos) flushTX();
|
173
|
163
|
|
174
|
|
- /* Temporarily lock out UART transmit interrupts during this read so the UART transmit interrupt won't
|
175
|
|
- cause problems with the index values */
|
176
|
|
- UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
|
164
|
+ // Temporarily lock out UART transmit interrupts during this read so the UART transmit interrupt won't
|
|
165
|
+ // cause problems with the index values
|
|
166
|
+ UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
177
|
167
|
|
178
|
|
- /* LPC17xx.h incorrectly defines FIFOLVL as a uint8_t, when it's actually a 32-bit register */
|
179
|
|
- if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1)
|
180
|
|
- fifolvl = *(reinterpret_cast<volatile uint32_t *>(&((LPC_UART1_TypeDef *) UARTx)->FIFOLVL));
|
181
|
|
- else
|
182
|
|
- fifolvl = *(reinterpret_cast<volatile uint32_t *>(&UARTx->FIFOLVL));
|
|
168
|
+ // LPC17xx.h incorrectly defines FIFOLVL as a uint8_t, when it's actually a 32-bit register
|
|
169
|
+ if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
|
|
170
|
+ fifolvl = *(reinterpret_cast<volatile uint32_t *>(&((LPC_UART1_TypeDef *) UARTx)->FIFOLVL));
|
|
171
|
+ } else fifolvl = *(reinterpret_cast<volatile uint32_t *>(&UARTx->FIFOLVL));
|
183
|
172
|
|
184
|
|
- /* If the queue is empty and there's space in the FIFO, immediately send the byte */
|
185
|
|
- if (TxQueueWritePos == TxQueueReadPos && fifolvl < UART_TX_FIFO_SIZE) {
|
186
|
|
- bytes = UART_Send(UARTx, &send, 1, BLOCKING);
|
187
|
|
- }
|
188
|
|
- /* Otherwiise, write the byte to the transmit buffer */
|
189
|
|
- else if ((TxQueueWritePos+1) % TX_BUFFER_SIZE != TxQueueReadPos) {
|
190
|
|
- TxBuffer[TxQueueWritePos] = send;
|
191
|
|
- TxQueueWritePos = (TxQueueWritePos+1) % TX_BUFFER_SIZE;
|
192
|
|
- bytes++;
|
193
|
|
- }
|
|
173
|
+ // If the queue is empty and there's space in the FIFO, immediately send the byte
|
|
174
|
+ if (TxQueueWritePos == TxQueueReadPos && fifolvl < UART_TX_FIFO_SIZE) {
|
|
175
|
+ bytes = UART_Send(UARTx, &send, 1, BLOCKING);
|
|
176
|
+ }
|
|
177
|
+ // Otherwiise, write the byte to the transmit buffer
|
|
178
|
+ else if ((TxQueueWritePos+1) % TX_BUFFER_SIZE != TxQueueReadPos) {
|
|
179
|
+ TxBuffer[TxQueueWritePos] = send;
|
|
180
|
+ TxQueueWritePos = (TxQueueWritePos+1) % TX_BUFFER_SIZE;
|
|
181
|
+ bytes++;
|
|
182
|
+ }
|
194
|
183
|
|
195
|
|
- /* Re-enable the TX Interrupt */
|
196
|
|
- UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
|
|
184
|
+ // Re-enable the TX Interrupt
|
|
185
|
+ UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
|
197
|
186
|
|
198
|
|
- return bytes;
|
199
|
|
- #else
|
200
|
|
- return UART_Send(UARTx, &send, 1, BLOCKING);
|
201
|
|
- #endif
|
|
187
|
+ return bytes;
|
|
188
|
+#else
|
|
189
|
+ return UART_Send(UARTx, &send, 1, BLOCKING);
|
|
190
|
+#endif
|
202
|
191
|
}
|
203
|
192
|
|
204
|
193
|
#if TX_BUFFER_SIZE > 0
|
205
|
194
|
void HardwareSerial::flushTX() {
|
206
|
|
- /* Wait for the tx buffer and FIFO to drain */
|
|
195
|
+ // Wait for the tx buffer and FIFO to drain
|
207
|
196
|
while (TxQueueWritePos != TxQueueReadPos && UART_CheckBusy(UARTx) == SET);
|
208
|
197
|
}
|
209
|
198
|
#endif
|
|
@@ -234,65 +223,58 @@ void HardwareSerial::IRQHandler() {
|
234
|
223
|
uint8_t LSRValue, byte;
|
235
|
224
|
|
236
|
225
|
IIRValue = UART_GetIntId(UARTx);
|
237
|
|
- IIRValue &= UART_IIR_INTID_MASK; /* check bit 1~3, interrupt identification */
|
|
226
|
+ IIRValue &= UART_IIR_INTID_MASK; // check bit 1~3, interrupt identification
|
238
|
227
|
|
239
|
|
- if ( IIRValue == UART_IIR_INTID_RLS ) /* Receive Line Status */
|
240
|
|
- {
|
|
228
|
+ // Receive Line Status
|
|
229
|
+ if (IIRValue == UART_IIR_INTID_RLS) {
|
241
|
230
|
LSRValue = UART_GetLineStatus(UARTx);
|
242
|
231
|
|
243
|
|
- /* Receive Line Status */
|
244
|
|
- if ( LSRValue & (UART_LSR_OE|UART_LSR_PE|UART_LSR_FE|UART_LSR_RXFE|UART_LSR_BI) )
|
245
|
|
- {
|
246
|
|
- /* There are errors or break interrupt */
|
247
|
|
- /* Read LSR will clear the interrupt */
|
|
232
|
+ // Receive Line Status
|
|
233
|
+ if (LSRValue & (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE | UART_LSR_RXFE | UART_LSR_BI)) {
|
|
234
|
+ // There are errors or break interrupt
|
|
235
|
+ // Read LSR will clear the interrupt
|
248
|
236
|
Status = LSRValue;
|
249
|
|
- byte = UART_ReceiveByte(UARTx); /* Dummy read on RX to clear
|
250
|
|
- interrupt, then bail out */
|
|
237
|
+ byte = UART_ReceiveByte(UARTx); // Dummy read on RX to clear interrupt, then bail out
|
251
|
238
|
return;
|
252
|
239
|
}
|
253
|
240
|
}
|
254
|
241
|
|
255
|
|
- if ( IIRValue == UART_IIR_INTID_RDA ) /* Receive Data Available */
|
256
|
|
- {
|
257
|
|
- /* Clear the FIFO */
|
258
|
|
- while ( UART_Receive(UARTx, &byte, 1, NONE_BLOCKING) ) {
|
259
|
|
- if ((RxQueueWritePos+1) % RX_BUFFER_SIZE != RxQueueReadPos)
|
260
|
|
- {
|
|
242
|
+ // Receive Data Available
|
|
243
|
+ if (IIRValue == UART_IIR_INTID_RDA) {
|
|
244
|
+ // Clear the FIFO
|
|
245
|
+ while (UART_Receive(UARTx, &byte, 1, NONE_BLOCKING)) {
|
|
246
|
+ if ((RxQueueWritePos + 1) % RX_BUFFER_SIZE != RxQueueReadPos) {
|
261
|
247
|
RxBuffer[RxQueueWritePos] = byte;
|
262
|
|
- RxQueueWritePos = (RxQueueWritePos+1) % RX_BUFFER_SIZE;
|
263
|
|
- }
|
264
|
|
- else
|
|
248
|
+ RxQueueWritePos = (RxQueueWritePos + 1) % RX_BUFFER_SIZE;
|
|
249
|
+ } else
|
265
|
250
|
break;
|
266
|
251
|
}
|
267
|
|
- }
|
268
|
|
- else if ( IIRValue == UART_IIR_INTID_CTI ) /* Character timeout indicator */
|
269
|
|
- {
|
270
|
|
- /* Character Time-out indicator */
|
271
|
|
- Status |= 0x100; /* Bit 9 as the CTI error */
|
|
252
|
+ // Character timeout indicator
|
|
253
|
+ } else if (IIRValue == UART_IIR_INTID_CTI) {
|
|
254
|
+ // Character Time-out indicator
|
|
255
|
+ Status |= 0x100; // Bit 9 as the CTI error
|
272
|
256
|
}
|
273
|
257
|
|
274
|
258
|
#if TX_BUFFER_SIZE > 0
|
275
|
259
|
if (IIRValue == UART_IIR_INTID_THRE) {
|
276
|
|
- /* Disable THRE interrupt */
|
|
260
|
+ // Disable THRE interrupt
|
277
|
261
|
UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
278
|
262
|
|
279
|
|
- /* Wait for FIFO buffer empty */
|
|
263
|
+ // Wait for FIFO buffer empty
|
280
|
264
|
while (UART_CheckBusy(UARTx) == SET);
|
281
|
265
|
|
282
|
|
- /* Transfer up to UART_TX_FIFO_SIZE bytes of data */
|
|
266
|
+ // Transfer up to UART_TX_FIFO_SIZE bytes of data
|
283
|
267
|
for (int i = 0; i < UART_TX_FIFO_SIZE && TxQueueWritePos != TxQueueReadPos; i++) {
|
284
|
|
- /* Move a piece of data into the transmit FIFO */
|
285
|
|
- if (UART_Send(UARTx, &TxBuffer[TxQueueReadPos], 1, NONE_BLOCKING))
|
|
268
|
+ // Move a piece of data into the transmit FIFO
|
|
269
|
+ if (UART_Send(UARTx, &TxBuffer[TxQueueReadPos], 1, NONE_BLOCKING)) {
|
286
|
270
|
TxQueueReadPos = (TxQueueReadPos+1) % TX_BUFFER_SIZE;
|
287
|
|
- else
|
288
|
|
- break;
|
|
271
|
+ } else break;
|
289
|
272
|
}
|
290
|
273
|
|
291
|
|
- /* If there is no more data to send, disable the transmit interrupt - else enable it or keep it enabled */
|
292
|
|
- if (TxQueueWritePos == TxQueueReadPos)
|
|
274
|
+ // If there is no more data to send, disable the transmit interrupt - else enable it or keep it enabled
|
|
275
|
+ if (TxQueueWritePos == TxQueueReadPos) {
|
293
|
276
|
UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
294
|
|
- else
|
295
|
|
- UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
|
|
277
|
+ } else UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
|
296
|
278
|
}
|
297
|
279
|
#endif
|
298
|
280
|
}
|
|
@@ -301,60 +283,28 @@ void HardwareSerial::IRQHandler() {
|
301
|
283
|
extern "C" {
|
302
|
284
|
#endif
|
303
|
285
|
|
304
|
|
-/*****************************************************************************
|
305
|
|
-** Function name: UART0_IRQHandler
|
306
|
|
-**
|
307
|
|
-** Descriptions: UART0 interrupt handler
|
308
|
|
-**
|
309
|
|
-** parameters: None
|
310
|
|
-** Returned value: None
|
311
|
|
-**
|
312
|
|
-*****************************************************************************/
|
313
|
|
-void UART0_IRQHandler (void)
|
314
|
|
-{
|
315
|
|
- Serial.IRQHandler();
|
|
286
|
+void UART0_IRQHandler(void) {
|
|
287
|
+ #if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
|
|
288
|
+ Serial.IRQHandler();
|
|
289
|
+ #endif
|
316
|
290
|
}
|
317
|
291
|
|
318
|
|
-/*****************************************************************************
|
319
|
|
-** Function name: UART1_IRQHandler
|
320
|
|
-**
|
321
|
|
-** Descriptions: UART1 interrupt handler
|
322
|
|
-**
|
323
|
|
-** parameters: None
|
324
|
|
-** Returned value: None
|
325
|
|
-**
|
326
|
|
-*****************************************************************************/
|
327
|
|
-void UART1_IRQHandler (void)
|
328
|
|
-{
|
329
|
|
- Serial1.IRQHandler();
|
|
292
|
+void UART1_IRQHandler(void) {
|
|
293
|
+ #if SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
|
|
294
|
+ Serial1.IRQHandler();
|
|
295
|
+ #endif
|
330
|
296
|
}
|
331
|
297
|
|
332
|
|
-/*****************************************************************************
|
333
|
|
-** Function name: UART2_IRQHandler
|
334
|
|
-**
|
335
|
|
-** Descriptions: UART2 interrupt handler
|
336
|
|
-**
|
337
|
|
-** parameters: None
|
338
|
|
-** Returned value: None
|
339
|
|
-**
|
340
|
|
-*****************************************************************************/
|
341
|
|
-void UART2_IRQHandler (void)
|
342
|
|
-{
|
343
|
|
- Serial2.IRQHandler();
|
|
298
|
+void UART2_IRQHandler(void) {
|
|
299
|
+ #if SERIAL_PORT == 2 || SERIAL_PORT_2 == 2
|
|
300
|
+ Serial2.IRQHandler();
|
|
301
|
+ #endif
|
344
|
302
|
}
|
345
|
303
|
|
346
|
|
-/*****************************************************************************
|
347
|
|
-** Function name: UART3_IRQHandler
|
348
|
|
-**
|
349
|
|
-** Descriptions: UART3 interrupt handler
|
350
|
|
-**
|
351
|
|
-** parameters: None
|
352
|
|
-** Returned value: None
|
353
|
|
-**
|
354
|
|
-*****************************************************************************/
|
355
|
|
-void UART3_IRQHandler (void)
|
356
|
|
-{
|
357
|
|
- Serial3.IRQHandler();
|
|
304
|
+void UART3_IRQHandler(void) {
|
|
305
|
+ #if SERIAL_PORT == 3 || SERIAL_PORT_2 == 3
|
|
306
|
+ Serial3.IRQHandler();
|
|
307
|
+ #endif
|
358
|
308
|
}
|
359
|
309
|
|
360
|
310
|
#ifdef __cplusplus
|