Browse Source

Add Serial1.transmitterEnable(pin) for RS-485

teensy4-core
PaulStoffregen 11 years ago
parent
commit
bff3488fd1
4 changed files with 34 additions and 7 deletions
  1. +8
    -0
      teensy/HardwareSerial.cpp
  2. +1
    -0
      teensy/HardwareSerial.h
  3. +5
    -3
      teensy3/HardwareSerial.h
  4. +20
    -4
      teensy3/serial1.c

+ 8
- 0
teensy/HardwareSerial.cpp View File

rx_buffer_tail = 0; rx_buffer_tail = 0;
} }


void HardwareSerial::transmitterEnable(uint8_t pin)
{
while (transmitting) ;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
tx_enable_pin = pin;
}

int HardwareSerial::available(void) int HardwareSerial::available(void)
{ {
uint8_t head, tail; uint8_t head, tail;

+ 1
- 0
teensy/HardwareSerial.h View File

} }
void _begin(uint16_t baud_count, uint8_t pin); void _begin(uint16_t baud_count, uint8_t pin);
void end(void); void end(void);
void transmitterEnable(uint8_t pin);
virtual int available(void); virtual int available(void);
virtual int peek(void); virtual int peek(void);
virtual int read(void); virtual int read(void);

+ 5
- 3
teensy3/HardwareSerial.h View File

void serial_begin(uint32_t divisor); void serial_begin(uint32_t divisor);
void serial_format(uint32_t format); void serial_format(uint32_t format);
void serial_end(void); void serial_end(void);
void serial_set_transmit_pin(uint8_t pin);
void serial_putchar(uint32_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);
serial_begin(BAUD2DIV(baud)); serial_begin(BAUD2DIV(baud));
serial_format(format); } serial_format(format); }
virtual void end(void) { serial_end(); } virtual void end(void) { serial_end(); }
virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); }
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(); }
virtual 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); }
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(); }
virtual 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); }
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(); }
virtual 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); }

+ 20
- 4
teensy3/serial1.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 serial_set_transmit_pin(uint8_t pin)
{
while (transmitting) ;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
transmit_pin = portOutputRegister(pin);
}

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


if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) 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) {
if ((UART0_S1 & UART_S1_TDRE)) { if ((UART0_S1 & UART_S1_TDRE)) {
uint32_t tail = tx_buffer_tail; uint32_t tail = tx_buffer_tail;
if (++tail >= TX_BUFFER_SIZE) tail = 0; if (++tail >= TX_BUFFER_SIZE) tail = 0;
UART0_D = tx_buffer[tail];
n = tx_buffer[tail];
if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
UART0_D = n;
tx_buffer_tail = tail; tx_buffer_tail = tail;
} }
} else if (priority >= 256) { } else if (priority >= 256) {
{ {
const uint8_t *p = (const uint8_t *)buf; const uint8_t *p = (const uint8_t *)buf;
const uint8_t *end = p + count; const uint8_t *end = p + count;
uint32_t head;
uint32_t head, n;


if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return; if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) 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;
if ((UART0_S1 & UART_S1_TDRE)) { if ((UART0_S1 & UART_S1_TDRE)) {
uint32_t tail = tx_buffer_tail; uint32_t tail = tx_buffer_tail;
if (++tail >= TX_BUFFER_SIZE) tail = 0; if (++tail >= TX_BUFFER_SIZE) tail = 0;
UART0_D = tx_buffer[tail];
n = tx_buffer[tail];
if (use9Bits) UART0_C3 = (UART0_C3 & ~0x40) | ((n & 0x100) >> 2);
UART0_D = n;
tx_buffer_tail = tail; tx_buffer_tail = tail;
} }
} else if (priority >= 256) { } else if (priority >= 256) {
} }
if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) { if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
transmitting = 0; transmitting = 0;
if (transmit_pin) *transmit_pin = 0;
UART0_C2 = C2_TX_INACTIVE; UART0_C2 = C2_TX_INACTIVE;
} }
} }

Loading…
Cancel
Save