Browse Source

Add serial formats and 9 bit modes (disabled by default) to HardwareSerial

main
PaulStoffregen 11 years ago
parent
commit
29168b784d
4 changed files with 208 additions and 33 deletions
  1. +79
    -12
      teensy3/HardwareSerial.h
  2. +41
    -7
      teensy3/serial1.c
  3. +47
    -7
      teensy3/serial2.c
  4. +41
    -7
      teensy3/serial3.c

+ 79
- 12
teensy3/HardwareSerial.h View File

#include "mk20dx128.h" #include "mk20dx128.h"
#include <inttypes.h> #include <inttypes.h>


// uncomment to enable 9 bit formats
//#define SERIAL_9BIT_SUPPORT


#define SERIAL_7E1 0x02
#define SERIAL_7O1 0x03
#define SERIAL_8N1 0x00
#define SERIAL_8N2 0x04
#define SERIAL_8E1 0x06
#define SERIAL_8O1 0x07
#define SERIAL_7E1_RXINV 0x12
#define SERIAL_7O1_RXINV 0x13
#define SERIAL_8N1_RXINV 0x10
#define SERIAL_8N2_RXINV 0x14
#define SERIAL_8E1_RXINV 0x16
#define SERIAL_8O1_RXINV 0x17
#define SERIAL_7E1_TXINV 0x22
#define SERIAL_7O1_TXINV 0x23
#define SERIAL_8N1_TXINV 0x20
#define SERIAL_8N2_TXINV 0x24
#define SERIAL_8E1_TXINV 0x26
#define SERIAL_8O1_TXINV 0x27
#define SERIAL_7E1_RXINV_TXINV 0x32
#define SERIAL_7O1_RXINV_TXINV 0x33
#define SERIAL_8N1_RXINV_TXINV 0x30
#define SERIAL_8N2_RXINV_TXINV 0x34
#define SERIAL_8E1_RXINV_TXINV 0x36
#define SERIAL_8O1_RXINV_TXINV 0x37
#ifdef SERIAL_9BIT_SUPPORT
#define SERIAL_9N1 0x84
#define SERIAL_9E1 0x8E
#define SERIAL_9O1 0x8F
#define SERIAL_9N1_RXINV 0x94
#define SERIAL_9E1_RXINV 0x9E
#define SERIAL_9O1_RXINV 0x9F
#define SERIAL_9N1_TXINV 0xA4
#define SERIAL_9E1_TXINV 0xAE
#define SERIAL_9O1_TXINV 0xAF
#define SERIAL_9N1_RXINV_TXINV 0xB4
#define SERIAL_9E1_RXINV_TXINV 0xBE
#define SERIAL_9O1_RXINV_TXINV 0xBF
#endif
// bit0: parity, 0=even, 1=odd
// bit1: parity, 0=disable, 1=enable
// bit2: mode, 1=9bit, 0=8bit
// bit3: mode10: 1=10bit, 0=8bit
// bit4: rxinv, 0=normal, 1=inverted
// bit5: txinv, 0=normal, 1=inverted
// bit6: unused
// bit7: actual data goes into 9th bit


#define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud)) #define BAUD2DIV(baud) (((F_CPU * 2) + ((baud) >> 1)) / (baud))
#define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud)) #define BAUD2DIV3(baud) (((F_BUS * 2) + ((baud) >> 1)) / (baud))


extern "C" { extern "C" {
#endif #endif
void serial_begin(uint32_t divisor); void serial_begin(uint32_t divisor);
void serial_format(uint32_t format);
void serial_end(void); void serial_end(void);
void serial_putchar(uint8_t c);
void serial_putchar(uint32_t c);
void serial_write(const void *buf, unsigned int count); void serial_write(const void *buf, unsigned int count);
void serial_flush(void); void serial_flush(void);
int serial_available(void); int serial_available(void);
void serial_phex32(uint32_t n); void serial_phex32(uint32_t n);


void serial2_begin(uint32_t divisor); void serial2_begin(uint32_t divisor);
void serial2_format(uint32_t format);
void serial2_end(void); void serial2_end(void);
void serial2_putchar(uint8_t c);
void serial2_putchar(uint32_t c);
void serial2_write(const void *buf, unsigned int count); void serial2_write(const void *buf, unsigned int count);
void serial2_flush(void); void serial2_flush(void);
int serial2_available(void); int serial2_available(void);
void serial2_clear(void); void serial2_clear(void);


void serial3_begin(uint32_t divisor); void serial3_begin(uint32_t divisor);
void serial3_format(uint32_t format);
void serial3_end(void); void serial3_end(void);
void serial3_putchar(uint8_t c);
void serial3_putchar(uint32_t c);
void serial3_write(const void *buf, unsigned int count); void serial3_write(const void *buf, unsigned int count);
void serial3_flush(void); void serial3_flush(void);
int serial3_available(void); int serial3_available(void);
class HardwareSerial : public Stream class HardwareSerial : public Stream
{ {
public: public:
void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
void end(void) { serial_end(); }
virtual void begin(uint32_t baud) { serial_begin(BAUD2DIV(baud)); }
virtual void begin(uint32_t baud, uint32_t format) {
serial_begin(BAUD2DIV(baud));
serial_format(format); }
virtual void end(void) { serial_end(); }
virtual int available(void) { return serial_available(); } virtual int available(void) { return serial_available(); }
virtual int peek(void) { return serial_peek(); } virtual int peek(void) { return serial_peek(); }
virtual int read(void) { return serial_getchar(); } virtual int read(void) { return serial_getchar(); }
virtual void flush(void) { serial_flush(); } virtual void flush(void) { serial_flush(); }
void clear(void) { serial_clear(); }
virtual void clear(void) { serial_clear(); }
virtual size_t write(uint8_t c) { serial_putchar(c); return 1; } virtual size_t write(uint8_t c) { serial_putchar(c); return 1; }
size_t write(unsigned long n) { return write((uint8_t)n); } size_t write(unsigned long n) { return write((uint8_t)n); }
size_t write(long n) { return write((uint8_t)n); } size_t write(long n) { return write((uint8_t)n); }
size_t write(const char *str) { size_t len = strlen(str); size_t write(const char *str) { size_t len = strlen(str);
serial_write((const uint8_t *)str, len); serial_write((const uint8_t *)str, len);
return len; } return len; }
size_t write9bit(uint32_t c) { serial_putchar(c); return 1; }
}; };
extern HardwareSerial Serial1; extern HardwareSerial Serial1;


class HardwareSerial2 : public HardwareSerial class HardwareSerial2 : public HardwareSerial
{ {
public: public:
void begin(uint32_t baud) { serial2_begin(BAUD2DIV(baud)); }
void end(void) { serial2_end(); }
virtual void begin(uint32_t baud) { serial2_begin(BAUD2DIV(baud)); }
virtual void begin(uint32_t baud, uint32_t format) {
serial2_begin(BAUD2DIV(baud));
serial2_format(format); }
virtual void end(void) { serial2_end(); }
virtual int available(void) { return serial2_available(); } virtual int available(void) { return serial2_available(); }
virtual int peek(void) { return serial2_peek(); } virtual int peek(void) { return serial2_peek(); }
virtual int read(void) { return serial2_getchar(); } virtual int read(void) { return serial2_getchar(); }
virtual void flush(void) { serial2_flush(); } virtual void flush(void) { serial2_flush(); }
void clear(void) { serial2_clear(); }
virtual void clear(void) { serial2_clear(); }
virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; } virtual size_t write(uint8_t c) { serial2_putchar(c); return 1; }
size_t write(unsigned long n) { return write((uint8_t)n); } size_t write(unsigned long n) { return write((uint8_t)n); }
size_t write(long n) { return write((uint8_t)n); } size_t write(long n) { return write((uint8_t)n); }
size_t write(const char *str) { size_t len = strlen(str); size_t write(const char *str) { size_t len = strlen(str);
serial2_write((const uint8_t *)str, len); serial2_write((const uint8_t *)str, len);
return len; } return len; }
size_t write9bit(uint32_t c) { serial2_putchar(c); return 1; }
}; };
extern HardwareSerial2 Serial2; extern HardwareSerial2 Serial2;


class HardwareSerial3 : public HardwareSerial class HardwareSerial3 : public HardwareSerial
{ {
public: public:
void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
void end(void) { serial3_end(); }
virtual void begin(uint32_t baud) { serial3_begin(BAUD2DIV3(baud)); }
virtual void begin(uint32_t baud, uint32_t format) {
serial3_begin(BAUD2DIV3(baud));
serial3_format(format); }
virtual void end(void) { serial3_end(); }
virtual int available(void) { return serial3_available(); } virtual int available(void) { return serial3_available(); }
virtual int peek(void) { return serial3_peek(); } virtual int peek(void) { return serial3_peek(); }
virtual int read(void) { return serial3_getchar(); } virtual int read(void) { return serial3_getchar(); }
virtual void flush(void) { serial3_flush(); } virtual void flush(void) { serial3_flush(); }
void clear(void) { serial3_clear(); }
virtual void clear(void) { serial3_clear(); }
virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; } virtual size_t write(uint8_t c) { serial3_putchar(c); return 1; }
size_t write(unsigned long n) { return write((uint8_t)n); } size_t write(unsigned long n) { return write((uint8_t)n); }
size_t write(long n) { return write((uint8_t)n); } size_t write(long n) { return write((uint8_t)n); }
size_t write(const char *str) { size_t len = strlen(str); size_t write(const char *str) { size_t len = strlen(str);
serial3_write((const uint8_t *)str, len); serial3_write((const uint8_t *)str, len);
return len; } return len; }
size_t write9bit(uint32_t c) { serial3_putchar(c); return 1; }
}; };
extern HardwareSerial3 Serial3; extern HardwareSerial3 Serial3;



+ 41
- 7
teensy3/serial1.c View File

// changes not recommended below this point.... // changes not recommended below this point....
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////


static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
#ifdef SERIAL_9BIT_SUPPORT
static uint8_t use9Bits = 0;
#define BUFTYPE uint16_t
#else
#define BUFTYPE uint8_t
#define use9Bits 0
#endif

static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
static volatile uint8_t transmitting = 0; static volatile uint8_t transmitting = 0;
#if TX_BUFFER_SIZE > 255 #if TX_BUFFER_SIZE > 255
static volatile uint16_t tx_buffer_head = 0; static volatile uint16_t tx_buffer_head = 0;
NVIC_ENABLE_IRQ(IRQ_UART0_STATUS); NVIC_ENABLE_IRQ(IRQ_UART0_STATUS);
} }


void serial_format(uint32_t format)
{
uint8_t c;

c = UART0_C1;
c = (c & ~0x13) | (format & 0x03); // configure parity
if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
UART0_C1 = c;
if ((format & 0x0F) == 0x04) UART0_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
c = UART0_S2 & ~0x10;
if (format & 0x10) c |= 0x10; // rx invert
UART0_S2 = c;
c = UART0_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART0_C3 = c;
#ifdef SERIAL_9BIT_SUPPORT
c = UART0_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
UART0_C4 = c;
use9Bits = format & 0x80;
#endif
}

void serial_end(void) void serial_end(void)
{ {
if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
} }




void serial_putchar(uint8_t c)
void serial_putchar(uint32_t c)
{ {
uint32_t head; uint32_t head;




void uart0_status_isr(void) void uart0_status_isr(void)
{ {
uint32_t head, newhead, tail;
uint32_t head, newhead, tail, n;
uint8_t avail, c; uint8_t avail, c;


if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) { if (UART0_S1 & (UART_S1_RDRF | UART_S1_IDLE)) {
head = rx_buffer_head; head = rx_buffer_head;
tail = rx_buffer_tail; tail = rx_buffer_tail;
do { do {
c = UART0_D;
n = UART0_D;
if (use9Bits && (UART0_C3 & 0x80)) n |= 0x100;
newhead = head + 1; newhead = head + 1;
if (newhead >= RX_BUFFER_SIZE) newhead = 0; if (newhead >= RX_BUFFER_SIZE) newhead = 0;
if (newhead != tail) { if (newhead != tail) {
head = newhead; head = newhead;
rx_buffer[head] = c;
rx_buffer[head] = n;
} }
} while (--avail > 0); } while (--avail > 0);
rx_buffer_head = head; rx_buffer_head = head;
if (tail == head) break; if (tail == head) break;
if (++tail >= TX_BUFFER_SIZE) tail = 0; if (++tail >= TX_BUFFER_SIZE) tail = 0;
avail = UART0_S1; avail = UART0_S1;
UART0_D = tx_buffer[tail];
n = tx_buffer[tail];
if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
UART0_D = n;
} while (UART0_TCFIFO < 8); } while (UART0_TCFIFO < 8);
tx_buffer_tail = tail; tx_buffer_tail = tail;
if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING; if (UART0_S1 & UART_S1_TDRE) UART0_C2 = C2_TX_COMPLETING;

+ 47
- 7
teensy3/serial2.c View File

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS // 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 // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer


#ifdef SERIAL_9BIT_SUPPORT
static uint8_t use9Bits = 0;
#define BUFTYPE uint16_t
#else
#define BUFTYPE uint8_t
#define use9Bits 0
#endif

#define TX_BUFFER_SIZE 40 #define TX_BUFFER_SIZE 40
static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
static volatile uint8_t transmitting = 0; static volatile uint8_t transmitting = 0;
#if TX_BUFFER_SIZE > 255 #if TX_BUFFER_SIZE > 255
static volatile uint16_t tx_buffer_head = 0; static volatile uint16_t tx_buffer_head = 0;
#endif #endif


#define RX_BUFFER_SIZE 64 #define RX_BUFFER_SIZE 64
static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
static volatile uint8_t rx_buffer_head = 0; static volatile uint8_t rx_buffer_head = 0;
static volatile uint8_t rx_buffer_tail = 0; static volatile uint8_t rx_buffer_tail = 0;


NVIC_ENABLE_IRQ(IRQ_UART1_STATUS); NVIC_ENABLE_IRQ(IRQ_UART1_STATUS);
} }


void serial2_format(uint32_t format)
{
uint8_t c;

c = UART1_C1;
c = (c & ~0x13) | (format & 0x03); // configure parity
if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
UART1_C1 = c;
if ((format & 0x0F) == 0x04) UART1_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
c = UART1_S2 & ~0x10;
if (format & 0x10) c |= 0x10; // rx invert
UART1_S2 = c;
c = UART1_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART1_C3 = c;
#ifdef SERIAL_9BIT_SUPPORT
c = UART1_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
UART1_C4 = c;
use9Bits = format & 0x80;
#endif
// UART1_C1.0 = parity, 0=even, 1=odd
// UART1_C1.1 = parity, 0=disable, 1=enable
// UART1_C1.4 = mode, 1=9bit, 0=8bit
// UART1_C4.5 = mode, 1=10bit, 0=8bit
// UART1_C3.4 = txinv, 0=normal, 1=inverted
// UART1_S2.4 = rxinv, 0=normal, 1=inverted
}

void serial2_end(void) void serial2_end(void)
{ {
if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
rx_buffer_tail = 0; rx_buffer_tail = 0;
} }


void serial2_putchar(uint8_t c)
void serial2_putchar(uint32_t c)
{ {
uint32_t head; uint32_t head;




void uart1_status_isr(void) void uart1_status_isr(void)
{ {
uint32_t head, tail;
uint32_t head, tail, n;
uint8_t c; uint8_t c;


//digitalWriteFast(4, HIGH); //digitalWriteFast(4, HIGH);
if (UART1_S1 & UART_S1_RDRF) { if (UART1_S1 & UART_S1_RDRF) {
//digitalWriteFast(5, HIGH); //digitalWriteFast(5, HIGH);
c = UART1_D;
n = UART1_D;
if (use9Bits && (UART1_C3 & 0x80)) n |= 0x100;
head = rx_buffer_head + 1; head = rx_buffer_head + 1;
if (head >= RX_BUFFER_SIZE) head = 0; if (head >= RX_BUFFER_SIZE) head = 0;
if (head != rx_buffer_tail) { if (head != rx_buffer_tail) {
rx_buffer[head] = c;
rx_buffer[head] = n;
rx_buffer_head = head; rx_buffer_head = head;
} }
//digitalWriteFast(5, LOW); //digitalWriteFast(5, LOW);
UART1_C2 = C2_TX_COMPLETING; UART1_C2 = C2_TX_COMPLETING;
} else { } else {
if (++tail >= TX_BUFFER_SIZE) tail = 0; if (++tail >= TX_BUFFER_SIZE) tail = 0;
UART1_D = tx_buffer[tail];
n = tx_buffer[tail];
if (use9Bits) UART1_C3 = (UART1_C3 & ~0x40) | ((n & 0x100) >> 2);
UART1_D = n;
tx_buffer_tail = tail; tx_buffer_tail = tail;
} }
//digitalWriteFast(5, LOW); //digitalWriteFast(5, LOW);

+ 41
- 7
teensy3/serial3.c View File

// UART0 and UART1 are clocked by F_CPU, UART2 is clocked by F_BUS // 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 // UART0 has 8 byte fifo, UART1 and UART2 have 1 byte buffer


#ifdef SERIAL_9BIT_SUPPORT
static uint8_t use9Bits = 0;
#define BUFTYPE uint16_t
#else
#define BUFTYPE uint8_t
#define use9Bits 0
#endif

#define TX_BUFFER_SIZE 40 #define TX_BUFFER_SIZE 40
static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
static volatile uint8_t transmitting = 0; static volatile uint8_t transmitting = 0;
#if TX_BUFFER_SIZE > 255 #if TX_BUFFER_SIZE > 255
static volatile uint16_t tx_buffer_head = 0; static volatile uint16_t tx_buffer_head = 0;
#endif #endif


#define RX_BUFFER_SIZE 64 #define RX_BUFFER_SIZE 64
static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
static volatile uint8_t rx_buffer_head = 0; static volatile uint8_t rx_buffer_head = 0;
static volatile uint8_t rx_buffer_tail = 0; static volatile uint8_t rx_buffer_tail = 0;


NVIC_ENABLE_IRQ(IRQ_UART2_STATUS); NVIC_ENABLE_IRQ(IRQ_UART2_STATUS);
} }


void serial3_format(uint32_t format)
{
uint8_t c;

c = UART2_C1;
c = (c & ~0x13) | (format & 0x03); // configure parity
if (format & 0x04) c |= 0x10; // 9 bits (might include parity)
UART2_C1 = c;
if ((format & 0x0F) == 0x04) UART2_C3 |= 0x40; // 8N2 is 9 bit with 9th bit always 1
c = UART2_S2 & ~0x10;
if (format & 0x10) c |= 0x10; // rx invert
UART2_S2 = c;
c = UART2_C3 & ~0x10;
if (format & 0x20) c |= 0x10; // tx invert
UART2_C3 = c;
#ifdef SERIAL_9BIT_SUPPORT
c = UART2_C4 & 0x1F;
if (format & 0x08) c |= 0x20; // 9 bit mode with parity (requires 10 bits)
UART2_C4 = c;
use9Bits = format & 0x80;
#endif
}

void serial3_end(void) void serial3_end(void)
{ {
if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
rx_buffer_tail = 0; rx_buffer_tail = 0;
} }


void serial3_putchar(uint8_t c)
void serial3_putchar(uint32_t c)
{ {
uint32_t head; uint32_t head;




void uart2_status_isr(void) void uart2_status_isr(void)
{ {
uint32_t head, tail;
uint32_t head, tail, n;
uint8_t c; uint8_t c;


//digitalWriteFast(4, HIGH); //digitalWriteFast(4, HIGH);
if (UART2_S1 & UART_S1_RDRF) { if (UART2_S1 & UART_S1_RDRF) {
//digitalWriteFast(5, HIGH); //digitalWriteFast(5, HIGH);
c = UART2_D;
n = UART2_D;
if (use9Bits && (UART2_C3 & 0x80)) n |= 0x100;
head = rx_buffer_head + 1; head = rx_buffer_head + 1;
if (head >= RX_BUFFER_SIZE) head = 0; if (head >= RX_BUFFER_SIZE) head = 0;
if (head != rx_buffer_tail) { if (head != rx_buffer_tail) {
rx_buffer[head] = c;
rx_buffer[head] = n;
rx_buffer_head = head; rx_buffer_head = head;
} }
//digitalWriteFast(5, LOW); //digitalWriteFast(5, LOW);
UART2_C2 = C2_TX_COMPLETING; UART2_C2 = C2_TX_COMPLETING;
} else { } else {
if (++tail >= TX_BUFFER_SIZE) tail = 0; if (++tail >= TX_BUFFER_SIZE) tail = 0;
UART2_D = tx_buffer[tail];
n = tx_buffer[tail];
if (use9Bits) UART2_C3 = (UART2_C3 & ~0x40) | ((n & 0x100) >> 2);
UART2_D = n;
tx_buffer_tail = tail; tx_buffer_tail = tail;
} }
//digitalWriteFast(5, LOW); //digitalWriteFast(5, LOW);

Loading…
Cancel
Save