2017-08-22 16:30:33 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
|
|
|
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
|
|
|
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-08-26 22:25:25 +02:00
|
|
|
#ifdef TARGET_LPC1768
|
|
|
|
|
2017-08-22 16:30:33 +02:00
|
|
|
#include "HardwareSerial.h"
|
|
|
|
|
2018-04-25 13:44:26 +02:00
|
|
|
#include "../../inc/MarlinConfigPre.h"
|
|
|
|
|
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
#include "../../feature/emergency_parser.h"
|
|
|
|
#endif
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
#if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
|
|
|
|
HardwareSerial Serial = HardwareSerial(LPC_UART0);
|
|
|
|
#elif SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
|
|
|
|
HardwareSerial Serial1 = HardwareSerial((LPC_UART_TypeDef *) LPC_UART1);
|
|
|
|
#elif SERIAL_PORT == 2 || SERIAL_PORT_2 == 2
|
|
|
|
HardwareSerial Serial2 = HardwareSerial(LPC_UART2);
|
|
|
|
#elif SERIAL_PORT == 3 || SERIAL_PORT_2 == 3
|
|
|
|
HardwareSerial Serial3 = HardwareSerial(LPC_UART3);
|
|
|
|
#endif
|
2017-10-25 00:28:33 +02:00
|
|
|
|
|
|
|
void HardwareSerial::begin(uint32_t baudrate) {
|
|
|
|
|
|
|
|
UART_CFG_Type UARTConfigStruct;
|
|
|
|
PINSEL_CFG_Type PinCfg;
|
|
|
|
UART_FIFO_CFG_Type FIFOConfig;
|
|
|
|
|
2018-02-04 02:33:26 +01:00
|
|
|
if (Baudrate == baudrate) return; // No need to re-initialize
|
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
if (UARTx == LPC_UART0) {
|
2018-01-15 23:00:59 +01:00
|
|
|
// Initialize UART0 pin connect
|
2017-10-25 00:28:33 +02:00
|
|
|
PinCfg.Funcnum = 1;
|
|
|
|
PinCfg.OpenDrain = 0;
|
|
|
|
PinCfg.Pinmode = 0;
|
|
|
|
PinCfg.Pinnum = 2;
|
|
|
|
PinCfg.Portnum = 0;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
|
|
|
PinCfg.Pinnum = 3;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
2018-01-15 23:00:59 +01:00
|
|
|
} else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
|
|
|
|
// Initialize UART1 pin connect
|
2017-10-25 00:28:33 +02:00
|
|
|
PinCfg.Funcnum = 1;
|
|
|
|
PinCfg.OpenDrain = 0;
|
|
|
|
PinCfg.Pinmode = 0;
|
|
|
|
PinCfg.Pinnum = 15;
|
|
|
|
PinCfg.Portnum = 0;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
|
|
|
PinCfg.Pinnum = 16;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
2018-01-15 23:00:59 +01:00
|
|
|
} else if (UARTx == LPC_UART2) {
|
|
|
|
// Initialize UART2 pin connect
|
2017-10-25 00:28:33 +02:00
|
|
|
PinCfg.Funcnum = 1;
|
|
|
|
PinCfg.OpenDrain = 0;
|
|
|
|
PinCfg.Pinmode = 0;
|
|
|
|
PinCfg.Pinnum = 10;
|
|
|
|
PinCfg.Portnum = 0;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
|
|
|
PinCfg.Pinnum = 11;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
2018-01-15 23:00:59 +01:00
|
|
|
} else if (UARTx == LPC_UART3) {
|
|
|
|
// Initialize UART2 pin connect
|
2017-10-25 00:28:33 +02:00
|
|
|
PinCfg.Funcnum = 1;
|
|
|
|
PinCfg.OpenDrain = 0;
|
|
|
|
PinCfg.Pinmode = 0;
|
|
|
|
PinCfg.Pinnum = 0;
|
|
|
|
PinCfg.Portnum = 0;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
|
|
|
PinCfg.Pinnum = 1;
|
|
|
|
PINSEL_ConfigPin(&PinCfg);
|
2017-09-27 11:57:33 +02:00
|
|
|
}
|
2017-09-28 17:16:25 +02:00
|
|
|
|
2018-01-05 17:25:42 +01:00
|
|
|
/* Initialize UART Configuration parameter structure to default state:
|
|
|
|
* Baudrate = 9600bps
|
|
|
|
* 8 data bit
|
|
|
|
* 1 Stop bit
|
|
|
|
* None parity
|
|
|
|
*/
|
|
|
|
UART_ConfigStructInit(&UARTConfigStruct);
|
|
|
|
|
|
|
|
// Re-configure baudrate
|
|
|
|
UARTConfigStruct.Baud_rate = baudrate;
|
|
|
|
|
|
|
|
// Initialize eripheral with given to corresponding parameter
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_Init(UARTx, &UARTConfigStruct);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
// Enable and reset the TX and RX FIFOs
|
|
|
|
UART_FIFOConfigStructInit(&FIFOConfig);
|
|
|
|
UART_FIFOConfig(UARTx, &FIFOConfig);
|
|
|
|
|
2018-01-05 17:25:42 +01:00
|
|
|
// Enable UART Transmit
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_TxCmd(UARTx, ENABLE);
|
|
|
|
|
|
|
|
// Configure Interrupts
|
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, ENABLE);
|
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_RLS, ENABLE);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
if (UARTx == LPC_UART0) NVIC_EnableIRQ(UART0_IRQn);
|
|
|
|
else if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) NVIC_EnableIRQ(UART1_IRQn);
|
|
|
|
else if (UARTx == LPC_UART2) NVIC_EnableIRQ(UART2_IRQn);
|
|
|
|
else if (UARTx == LPC_UART3) NVIC_EnableIRQ(UART3_IRQn);
|
2017-10-25 00:28:33 +02:00
|
|
|
|
|
|
|
RxQueueWritePos = RxQueueReadPos = 0;
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
TxQueueWritePos = TxQueueReadPos = 0;
|
|
|
|
#endif
|
2018-02-04 02:33:26 +01:00
|
|
|
|
|
|
|
// Save the configured baudrate
|
|
|
|
Baudrate = baudrate;
|
2017-09-27 11:57:33 +02:00
|
|
|
}
|
2017-09-28 17:16:25 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
int HardwareSerial::peek() {
|
|
|
|
int byte = -1;
|
2017-08-22 16:30:33 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Temporarily lock out UART receive interrupts during this read so the UART receive
|
|
|
|
// interrupt won't cause problems with the index values
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, DISABLE);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
if (RxQueueReadPos != RxQueueWritePos)
|
|
|
|
byte = RxBuffer[RxQueueReadPos];
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Re-enable UART interrupts
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, ENABLE);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
return byte;
|
2017-09-28 17:16:25 +02:00
|
|
|
}
|
2017-10-25 00:28:33 +02:00
|
|
|
|
|
|
|
int HardwareSerial::read() {
|
|
|
|
int byte = -1;
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Temporarily lock out UART receive interrupts during this read so the UART receive
|
|
|
|
// interrupt won't cause problems with the index values
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, DISABLE);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
if (RxQueueReadPos != RxQueueWritePos) {
|
|
|
|
byte = RxBuffer[RxQueueReadPos];
|
|
|
|
RxQueueReadPos = (RxQueueReadPos + 1) % RX_BUFFER_SIZE;
|
2017-08-22 16:30:33 +02:00
|
|
|
}
|
2017-09-28 17:16:25 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Re-enable UART interrupts
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_RBR, ENABLE);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t HardwareSerial::write(uint8_t send) {
|
2018-01-15 23:00:59 +01:00
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
size_t bytes = 0;
|
|
|
|
uint32_t fifolvl = 0;
|
2017-10-25 00:28:33 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// If the Tx Buffer is full, wait for space to clear
|
|
|
|
if ((TxQueueWritePos+1) % TX_BUFFER_SIZE == TxQueueReadPos) flushTX();
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Temporarily lock out UART transmit interrupts during this read so the UART transmit interrupt won't
|
|
|
|
// cause problems with the index values
|
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
2017-10-25 00:28:33 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// LPC17xx.h incorrectly defines FIFOLVL as a uint8_t, when it's actually a 32-bit register
|
|
|
|
if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) {
|
|
|
|
fifolvl = *(reinterpret_cast<volatile uint32_t *>(&((LPC_UART1_TypeDef *) UARTx)->FIFOLVL));
|
|
|
|
} else fifolvl = *(reinterpret_cast<volatile uint32_t *>(&UARTx->FIFOLVL));
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// If the queue is empty and there's space in the FIFO, immediately send the byte
|
|
|
|
if (TxQueueWritePos == TxQueueReadPos && fifolvl < UART_TX_FIFO_SIZE) {
|
|
|
|
bytes = UART_Send(UARTx, &send, 1, BLOCKING);
|
|
|
|
}
|
|
|
|
// Otherwiise, write the byte to the transmit buffer
|
|
|
|
else if ((TxQueueWritePos+1) % TX_BUFFER_SIZE != TxQueueReadPos) {
|
|
|
|
TxBuffer[TxQueueWritePos] = send;
|
|
|
|
TxQueueWritePos = (TxQueueWritePos+1) % TX_BUFFER_SIZE;
|
|
|
|
bytes++;
|
|
|
|
}
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Re-enable the TX Interrupt
|
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
return bytes;
|
|
|
|
#else
|
|
|
|
return UART_Send(UARTx, &send, 1, BLOCKING);
|
|
|
|
#endif
|
2017-10-25 00:28:33 +02:00
|
|
|
}
|
2017-09-28 17:16:25 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
void HardwareSerial::flushTX() {
|
2018-01-15 23:00:59 +01:00
|
|
|
// Wait for the tx buffer and FIFO to drain
|
2017-10-25 00:28:33 +02:00
|
|
|
while (TxQueueWritePos != TxQueueReadPos && UART_CheckBusy(UARTx) == SET);
|
|
|
|
}
|
2017-09-28 17:16:25 +02:00
|
|
|
#endif
|
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
int HardwareSerial::available() {
|
|
|
|
return (RxQueueWritePos + RX_BUFFER_SIZE - RxQueueReadPos) % RX_BUFFER_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardwareSerial::flush() {
|
|
|
|
RxQueueWritePos = 0;
|
|
|
|
RxQueueReadPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardwareSerial::printf(const char *format, ...) {
|
|
|
|
char RxBuffer[256];
|
|
|
|
va_list vArgs;
|
|
|
|
va_start(vArgs, format);
|
|
|
|
int length = vsnprintf(RxBuffer, 256, format, vArgs);
|
|
|
|
va_end(vArgs);
|
|
|
|
if (length > 0 && length < 256) {
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
write(RxBuffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardwareSerial::IRQHandler() {
|
|
|
|
uint32_t IIRValue;
|
|
|
|
uint8_t LSRValue, byte;
|
2017-09-28 17:16:25 +02:00
|
|
|
|
2017-10-25 00:28:33 +02:00
|
|
|
IIRValue = UART_GetIntId(UARTx);
|
2018-01-15 23:00:59 +01:00
|
|
|
IIRValue &= UART_IIR_INTID_MASK; // check bit 1~3, interrupt identification
|
2017-09-28 17:16:25 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Receive Line Status
|
|
|
|
if (IIRValue == UART_IIR_INTID_RLS) {
|
2017-10-25 00:28:33 +02:00
|
|
|
LSRValue = UART_GetLineStatus(UARTx);
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Receive Line Status
|
|
|
|
if (LSRValue & (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE | UART_LSR_RXFE | UART_LSR_BI)) {
|
|
|
|
// There are errors or break interrupt
|
|
|
|
// Read LSR will clear the interrupt
|
2017-10-25 00:28:33 +02:00
|
|
|
Status = LSRValue;
|
2018-01-15 23:00:59 +01:00
|
|
|
byte = UART_ReceiveByte(UARTx); // Dummy read on RX to clear interrupt, then bail out
|
2017-09-30 23:06:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-10-25 00:28:33 +02:00
|
|
|
}
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Receive Data Available
|
|
|
|
if (IIRValue == UART_IIR_INTID_RDA) {
|
|
|
|
// Clear the FIFO
|
|
|
|
while (UART_Receive(UARTx, &byte, 1, NONE_BLOCKING)) {
|
2018-04-25 13:44:26 +02:00
|
|
|
#if ENABLED(EMERGENCY_PARSER)
|
|
|
|
emergency_parser.update(byte);
|
|
|
|
#endif
|
2018-01-15 23:00:59 +01:00
|
|
|
if ((RxQueueWritePos + 1) % RX_BUFFER_SIZE != RxQueueReadPos) {
|
2017-10-25 00:28:33 +02:00
|
|
|
RxBuffer[RxQueueWritePos] = byte;
|
2018-01-15 23:00:59 +01:00
|
|
|
RxQueueWritePos = (RxQueueWritePos + 1) % RX_BUFFER_SIZE;
|
|
|
|
} else
|
2017-10-25 00:28:33 +02:00
|
|
|
break;
|
2017-09-30 23:06:43 +02:00
|
|
|
}
|
2018-01-15 23:00:59 +01:00
|
|
|
// Character timeout indicator
|
|
|
|
} else if (IIRValue == UART_IIR_INTID_CTI) {
|
|
|
|
// Character Time-out indicator
|
|
|
|
Status |= 0x100; // Bit 9 as the CTI error
|
2017-08-22 16:30:33 +02:00
|
|
|
}
|
2017-10-25 00:28:33 +02:00
|
|
|
|
|
|
|
#if TX_BUFFER_SIZE > 0
|
|
|
|
if (IIRValue == UART_IIR_INTID_THRE) {
|
2018-01-15 23:00:59 +01:00
|
|
|
// Disable THRE interrupt
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Wait for FIFO buffer empty
|
2017-10-25 00:28:33 +02:00
|
|
|
while (UART_CheckBusy(UARTx) == SET);
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// Transfer up to UART_TX_FIFO_SIZE bytes of data
|
2017-10-25 00:28:33 +02:00
|
|
|
for (int i = 0; i < UART_TX_FIFO_SIZE && TxQueueWritePos != TxQueueReadPos; i++) {
|
2018-01-15 23:00:59 +01:00
|
|
|
// Move a piece of data into the transmit FIFO
|
|
|
|
if (UART_Send(UARTx, &TxBuffer[TxQueueReadPos], 1, NONE_BLOCKING)) {
|
2017-10-25 00:28:33 +02:00
|
|
|
TxQueueReadPos = (TxQueueReadPos+1) % TX_BUFFER_SIZE;
|
2018-01-15 23:00:59 +01:00
|
|
|
} else break;
|
2017-10-25 00:28:33 +02:00
|
|
|
}
|
2017-10-27 06:33:43 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
// If there is no more data to send, disable the transmit interrupt - else enable it or keep it enabled
|
|
|
|
if (TxQueueWritePos == TxQueueReadPos) {
|
2017-10-25 00:28:33 +02:00
|
|
|
UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
|
2018-01-15 23:00:59 +01:00
|
|
|
} else UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
|
2017-09-30 23:06:43 +02:00
|
|
|
}
|
2017-10-25 00:28:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
void UART0_IRQHandler(void) {
|
|
|
|
#if SERIAL_PORT == 0 || SERIAL_PORT_2 == 0
|
|
|
|
Serial.IRQHandler();
|
|
|
|
#endif
|
2017-08-22 16:30:33 +02:00
|
|
|
}
|
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
void UART1_IRQHandler(void) {
|
|
|
|
#if SERIAL_PORT == 1 || SERIAL_PORT_2 == 1
|
|
|
|
Serial1.IRQHandler();
|
|
|
|
#endif
|
2017-08-22 16:30:33 +02:00
|
|
|
}
|
2017-10-25 00:28:33 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
void UART2_IRQHandler(void) {
|
|
|
|
#if SERIAL_PORT == 2 || SERIAL_PORT_2 == 2
|
|
|
|
Serial2.IRQHandler();
|
|
|
|
#endif
|
2017-08-22 16:30:33 +02:00
|
|
|
}
|
2017-10-25 00:28:33 +02:00
|
|
|
|
2018-01-15 23:00:59 +01:00
|
|
|
void UART3_IRQHandler(void) {
|
|
|
|
#if SERIAL_PORT == 3 || SERIAL_PORT_2 == 3
|
|
|
|
Serial3.IRQHandler();
|
|
|
|
#endif
|
2017-09-28 17:16:25 +02:00
|
|
|
}
|
2017-08-22 16:30:33 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2017-09-28 17:16:25 +02:00
|
|
|
}
|
2017-08-22 16:30:33 +02:00
|
|
|
#endif
|
2017-08-26 22:25:25 +02:00
|
|
|
|
|
|
|
#endif // TARGET_LPC1768
|