Skip to content

Commit

Permalink
Fix 7-bit support in teensy4 HardwareSerial
Browse files Browse the repository at this point in the history
Don't include the parity bit for returned data.
  • Loading branch information
ssilverman committed Apr 26, 2023
1 parent 78a1d99 commit 797a1f5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions teensy4/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions teensy4/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 797a1f5

Please sign in to comment.