Browse Source

Add transmitterEnable on Serial2 & Serial3

teensy4-core
PaulStoffregen 10 years ago
parent
commit
19ee965759
3 changed files with 27 additions and 6 deletions
  1. +4
    -0
      teensy3/HardwareSerial.h
  2. +12
    -0
      teensy3/serial2.c
  3. +11
    -6
      teensy3/serial3.c

+ 4
- 0
teensy3/HardwareSerial.h View File

void serial2_begin(uint32_t divisor); void serial2_begin(uint32_t divisor);
void serial2_format(uint32_t format); void serial2_format(uint32_t format);
void serial2_end(void); void serial2_end(void);
void serial2_set_transmit_pin(uint8_t pin);
void serial2_putchar(uint32_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);
void serial3_begin(uint32_t divisor); void serial3_begin(uint32_t divisor);
void serial3_format(uint32_t format); void serial3_format(uint32_t format);
void serial3_end(void); void serial3_end(void);
void serial3_set_transmit_pin(uint8_t pin);
void serial3_putchar(uint32_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);
serial2_begin(BAUD2DIV(baud)); serial2_begin(BAUD2DIV(baud));
serial2_format(format); } serial2_format(format); }
virtual void end(void) { serial2_end(); } virtual void end(void) { serial2_end(); }
virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); }
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(); }
serial3_begin(BAUD2DIV3(baud)); serial3_begin(BAUD2DIV3(baud));
serial3_format(format); } serial3_format(format); }
virtual void end(void) { serial3_end(); } virtual void end(void) { serial3_end(); }
virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); }
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(); }

+ 12
- 0
teensy3/serial2.c View File

static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE]; static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE]; static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
static volatile uint8_t transmitting = 0; static volatile uint8_t transmitting = 0;
static volatile uint8_t *transmit_pin=NULL;
#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;
static volatile uint16_t tx_buffer_tail = 0; static volatile uint16_t tx_buffer_tail = 0;
rx_buffer_tail = 0; rx_buffer_tail = 0;
} }


void serial2_set_transmit_pin(uint8_t pin)
{
while (transmitting) ;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
transmit_pin = portOutputRegister(pin);
}

void serial2_putchar(uint32_t c) void serial2_putchar(uint32_t c)
{ {
uint32_t head, n; uint32_t head, n;


if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
if (transmit_pin) *transmit_pin = 1;
head = tx_buffer_head; head = tx_buffer_head;
if (++head >= TX_BUFFER_SIZE) head = 0; if (++head >= TX_BUFFER_SIZE) head = 0;
while (tx_buffer_tail == head) { while (tx_buffer_tail == head) {
uint32_t head, n; uint32_t head, n;


if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART1)) return;
if (transmit_pin) *transmit_pin = 1;
while (p < end) { while (p < end) {
head = tx_buffer_head; head = tx_buffer_head;
if (++head >= TX_BUFFER_SIZE) head = 0; if (++head >= TX_BUFFER_SIZE) head = 0;
#endif #endif
if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) { if ((c & UART_C2_TCIE) && (UART1_S1 & UART_S1_TC)) {
transmitting = 0; transmitting = 0;
if (transmit_pin) *transmit_pin = 0;
UART1_C2 = C2_TX_INACTIVE; UART1_C2 = C2_TX_INACTIVE;
} }
} }

+ 11
- 6
teensy3/serial3.c View File

static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE]; static volatile BUFTYPE tx_buffer[TX_BUFFER_SIZE];
static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE]; static volatile BUFTYPE rx_buffer[RX_BUFFER_SIZE];
static volatile uint8_t transmitting = 0; static volatile uint8_t transmitting = 0;
static volatile uint8_t *transmit_pin=NULL;
#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;
static volatile uint16_t tx_buffer_tail = 0; static volatile uint16_t tx_buffer_tail = 0;
rx_buffer_tail = 0; rx_buffer_tail = 0;
} }


void serial3_set_transmit_pin(uint8_t pin)
{
while (transmitting) ;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
transmit_pin = portOutputRegister(pin);
}

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


if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART2)) return;
if (transmit_pin) *transmit_pin = 1;
head = tx_buffer_head; head = tx_buffer_head;
if (++head >= TX_BUFFER_SIZE) head = 0; if (++head >= TX_BUFFER_SIZE) head = 0;
while (tx_buffer_tail == head) { while (tx_buffer_tail == head) {
uint32_t head, tail, n; uint32_t head, tail, n;
uint8_t c; uint8_t c;


//digitalWriteFast(4, HIGH);
if (UART2_S1 & UART_S1_RDRF) { if (UART2_S1 & UART_S1_RDRF) {
//digitalWriteFast(5, HIGH);
n = UART2_D; n = UART2_D;
if (use9Bits && (UART2_C3 & 0x80)) n |= 0x100; if (use9Bits && (UART2_C3 & 0x80)) n |= 0x100;
head = rx_buffer_head + 1; head = rx_buffer_head + 1;
rx_buffer[head] = n; rx_buffer[head] = n;
rx_buffer_head = head; rx_buffer_head = head;
} }
//digitalWriteFast(5, LOW);
} }
c = UART2_C2; c = UART2_C2;
if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) { if ((c & UART_C2_TIE) && (UART2_S1 & UART_S1_TDRE)) {
//digitalWriteFast(5, HIGH);
head = tx_buffer_head; head = tx_buffer_head;
tail = tx_buffer_tail; tail = tx_buffer_tail;
if (head == tail) { if (head == tail) {
UART2_D = n; UART2_D = n;
tx_buffer_tail = tail; tx_buffer_tail = tail;
} }
//digitalWriteFast(5, LOW);
} }
if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) { if ((c & UART_C2_TCIE) && (UART2_S1 & UART_S1_TC)) {
transmitting = 0; transmitting = 0;
if (transmit_pin) *transmit_pin = 0;
UART2_C2 = C2_TX_INACTIVE; UART2_C2 = C2_TX_INACTIVE;
} }
//digitalWriteFast(4, LOW);
} }





Loading…
Cancel
Save