diff --git a/teensy4/HardwareSerial.cpp b/teensy4/HardwareSerial.cpp index 3fc9c26d9..5c936ed4d 100644 --- a/teensy4/HardwareSerial.cpp +++ b/teensy4/HardwareSerial.cpp @@ -208,6 +208,13 @@ void HardwareSerial::begin(uint32_t baud, uint16_t format) if (format & 0x04) ctrl |= LPUART_CTRL_M; // 9 bits (might include parity) if ((format & 0x0F) == 0x04) ctrl |= LPUART_CTRL_R9T8; // 8N2 is 9 bit with 9th bit always 1 + // 7-bit support + if ((format & 0x0E) == 0x02) { + data_mask_ = 0x7f; // Use only 7 bits of data + } else { + data_mask_ = 0x3ff; // Use only up to 10 bits of data + } + // Bit 5 TXINVERT if (format & 0x20) ctrl |= LPUART_CTRL_TXINV; // tx invert @@ -483,7 +490,7 @@ int HardwareSerial::peek(void) // Still empty Now check for stuff in FIFO Queue. int c = -1; // assume nothing to return if (port->WATER & 0x7000000) { - c = port->DATA & 0x3ff; // Use only up to 10 bits of data + c = port->DATA & data_mask_; // But we don't want to throw it away... // since queue is empty, just going to reset to front of queue... rx_buffer_head_ = 1; @@ -518,7 +525,7 @@ int HardwareSerial::read(void) // Still empty Now check for stuff in FIFO Queue. c = -1; // assume nothing to return if (port->WATER & 0x7000000) { - c = port->DATA & 0x3ff; // Use only up to 10 bits of data + c = port->DATA & data_mask_; } __enable_irq(); return c; @@ -621,7 +628,7 @@ void HardwareSerial::IRQHandler() head = rx_buffer_head_; tail = rx_buffer_tail_; do { - n = port->DATA & 0x3ff; // Use only up to 10 bits of data + n = port->DATA & data_mask_; newhead = head + 1; if (newhead >= rx_buffer_total_size_) newhead = 0; diff --git a/teensy4/HardwareSerial.h b/teensy4/HardwareSerial.h index b4f2d0015..5148c1f69 100644 --- a/teensy4/HardwareSerial.h +++ b/teensy4/HardwareSerial.h @@ -287,6 +287,9 @@ class HardwareSerial : public Stream uint8_t tx_pin_index_ = 0x0; uint8_t half_duplex_mode_ = 0; // are we in half duplex mode? + // 7-bit mode support + int data_mask_ = 0x3ff; // Use only up to 10 bits of data by default + volatile BUFTYPE *tx_buffer_; volatile BUFTYPE *rx_buffer_; volatile BUFTYPE *rx_buffer_storage_ = nullptr;