Skip to content

Commit

Permalink
Fix 7-bit support for teensy3
Browse files Browse the repository at this point in the history
  • Loading branch information
ssilverman committed Nov 18, 2022
1 parent 5454196 commit 4a23693
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
15 changes: 13 additions & 2 deletions teensy3/serial1.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ static uint8_t tx_pin_num = 1;
static uint8_t half_duplex_mode = 0;
#endif

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -202,6 +205,14 @@ void serial_format(uint32_t format)
c = UART0_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART0_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#ifdef SERIAL_9BIT_SUPPORT
c = UART0_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -628,7 +639,7 @@ void uart0_status_isr(void)
if (use9Bits && (UART0_C3 & 0x80)) {
n = UART0_D | 0x100;
} else {
n = UART0_D;
n = UART0_D & data_mask;
}
newhead = head + 1;
if (newhead >= rx_buffer_total_size_) newhead = 0;
Expand Down Expand Up @@ -674,7 +685,7 @@ void uart0_status_isr(void)
if (use9Bits && (UART0_C3 & 0x80)) {
n = UART0_D | 0x100;
} else {
n = UART0_D;
n = UART0_D & data_mask;
}
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
Expand Down
19 changes: 17 additions & 2 deletions teensy3/serial1_doughboy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ static volatile uint8_t rx_buffer_head = 0;
static volatile uint8_t rx_buffer_tail = 0;
#endif

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -140,6 +143,14 @@ void serial_format(uint32_t format)
c = UART0_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART0_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#ifdef SERIAL_9BIT_SUPPORT
c = UART0_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -404,7 +415,7 @@ void uart0_status_isr(void)
if (use9Bits && (UART0_C3 & 0x80)) {
n = UART0_D | 0x100;
} else {
n = UART0_D;
n = UART0_D & data_mask;
}
head = newhead;
rx_buffer[head] = n;
Expand Down Expand Up @@ -442,7 +453,11 @@ void uart0_status_isr(void)
}
}
n = UART0_D;
if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
if (use9Bits && (UART0_C3 & 0x80)) {
n |= 0x100;
} else {
n &= data_mask;
}
rx_buffer[head] = n;
rx_buffer_head = head;
break;
Expand Down
15 changes: 13 additions & 2 deletions teensy3/serial2.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ static uint8_t tx_pin_num = 10;
static uint8_t half_duplex_mode = 0;
#endif

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -201,6 +204,14 @@ void serial2_format(uint32_t format)
c = UART1_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART1_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#if defined(SERIAL_9BIT_SUPPORT) && !defined(KINETISL)
c = UART1_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -613,7 +624,7 @@ void uart1_status_isr(void)
if (use9Bits && (UART1_C3 & 0x80)) {
n = UART1_D | 0x100;
} else {
n = UART1_D;
n = UART1_D & data_mask;
}
newhead = head + 1;
if (newhead >= rx_buffer_total_size_) newhead = 0;
Expand Down Expand Up @@ -659,7 +670,7 @@ void uart1_status_isr(void)
if (use9Bits && (UART1_C3 & 0x80)) {
n = UART1_D | 0x100;
} else {
n = UART1_D;
n = UART1_D & data_mask;
}
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
Expand Down
13 changes: 12 additions & 1 deletion teensy3/serial3.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ static uint8_t tx_pin_num = 8;
static uint8_t half_duplex_mode = 0;
#endif

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -184,6 +187,14 @@ void serial3_format(uint32_t format)
c = UART2_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART2_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#if defined(SERIAL_9BIT_SUPPORT) && !defined(KINETISL)
c = UART2_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -492,7 +503,7 @@ void uart2_status_isr(void)
if (use9Bits && (UART2_C3 & 0x80)) {
n = UART2_D | 0x100;
} else {
n = UART2_D;
n = UART2_D & data_mask;
}
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
Expand Down
13 changes: 12 additions & 1 deletion teensy3/serial4.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ static volatile uint8_t rx_buffer_tail = 0;
static uint8_t rx_pin_num = 31;
static uint8_t tx_pin_num = 32;

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -157,6 +160,14 @@ void serial4_format(uint32_t format)
c = UART3_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART3_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#ifdef SERIAL_9BIT_SUPPORT
c = UART3_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -412,7 +423,7 @@ void uart3_status_isr(void)
if (use9Bits && (UART3_C3 & 0x80)) {
n = UART3_D | 0x100;
} else {
n = UART3_D;
n = UART3_D & data_mask;
}
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
Expand Down
13 changes: 12 additions & 1 deletion teensy3/serial5.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ static volatile uint8_t rx_buffer_tail = 0;

static uint8_t tx_pin_num = 33;

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -151,6 +154,14 @@ void serial5_format(uint32_t format)
c = UART4_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART4_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#ifdef SERIAL_9BIT_SUPPORT
c = UART4_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -383,7 +394,7 @@ void uart4_status_isr(void)
if (use9Bits && (UART4_C3 & 0x80)) {
n = UART4_D | 0x100;
} else {
n = UART4_D;
n = UART4_D & data_mask;
}
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
Expand Down
13 changes: 12 additions & 1 deletion teensy3/serial6.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ static volatile uint8_t rx_buffer_tail = 0;

static uint8_t tx_pin_num = 48;

// 7-bit mode support
static uint8_t data_mask = 0xff; // Use all bits by default

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS
// UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer

Expand Down Expand Up @@ -151,6 +154,14 @@ void serial6_format(uint32_t format)
c = UART5_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART5_C3 = c;

// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0xff; // Use all bits
}

#ifdef SERIAL_9BIT_SUPPORT
c = UART5_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
Expand Down Expand Up @@ -382,7 +393,7 @@ void uart5_status_isr(void)
if (use9Bits && (UART5_C3 & 0x80)) {
n = UART5_D | 0x100;
} else {
n = UART5_D;
n = UART5_D & data_mask;
}
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
Expand Down
16 changes: 13 additions & 3 deletions teensy3/serial6_lpuart.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ static volatile uint8_t rx_buffer_tail = 0;

static uint8_t tx_pin_num = 48;

// 7-bit mode support
static uint32_t data_mask = 0x3ff; // Use only the 10 data bits by default


void serial6_begin(uint32_t desiredBaudRate)
{
Expand Down Expand Up @@ -224,7 +227,14 @@ void serial6_format(uint32_t format)
// Bit 3 10 bit - Will assume that begin already cleared it.
if (format & 0x08)
LPUART0_BAUD |= LPUART_BAUD_M10;


// 7-bit support
if ((format & 0x0E) == 0x02) {
data_mask = 0x7f; // Use only 7 bits of data
} else {
data_mask = 0x3ff; // Use only the 10 data bits
}

// Bit 4 RXINVERT
c = LPUART0_STAT & ~LPUART_STAT_RXINV;
if (format & 0x10) c |= LPUART_STAT_RXINV; // rx invert
Expand Down Expand Up @@ -456,9 +466,9 @@ void lpuart0_status_isr(void)
// if (use9Bits && (UART5_C3 & 0x80)) {
// n = UART5_D | 0x100;
// } else {
// n = UART5_D;
// n = UART5_D & data_mask;
// }
n = LPUART0_DATA & 0x3ff; // use only the 10 data bits
n = LPUART0_DATA & data_mask;
head = rx_buffer_head + 1;
if (head >= rx_buffer_total_size_) head = 0;
if (head != rx_buffer_tail) {
Expand Down

0 comments on commit 4a23693

Please sign in to comment.