浏览代码

Add Serial1.transmitterEnable(pin) for RS-485

teensy4-core
PaulStoffregen 11 年前
父节点
当前提交
bff3488fd1
共有 4 个文件被更改,包括 34 次插入7 次删除
  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 查看文件

@@ -73,6 +73,14 @@ void HardwareSerial::end(void)
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)
{
uint8_t head, tail;

+ 1
- 0
teensy/HardwareSerial.h 查看文件

@@ -12,6 +12,7 @@ public:
}
void _begin(uint16_t baud_count, uint8_t pin);
void end(void);
void transmitterEnable(uint8_t pin);
virtual int available(void);
virtual int peek(void);
virtual int read(void);

+ 5
- 3
teensy3/HardwareSerial.h 查看文件

@@ -97,6 +97,7 @@ extern "C" {
void serial_begin(uint32_t divisor);
void serial_format(uint32_t format);
void serial_end(void);
void serial_set_transmit_pin(uint8_t pin);
void serial_putchar(uint32_t c);
void serial_write(const void *buf, unsigned int count);
void serial_flush(void);
@@ -148,11 +149,12 @@ public:
serial_begin(BAUD2DIV(baud));
serial_format(format); }
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 peek(void) { return serial_peek(); }
virtual int read(void) { return serial_getchar(); }
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; }
size_t write(unsigned long n) { return write((uint8_t)n); }
size_t write(long n) { return write((uint8_t)n); }
@@ -179,7 +181,7 @@ public:
virtual int peek(void) { return serial2_peek(); }
virtual int read(void) { return serial2_getchar(); }
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; }
size_t write(unsigned long n) { return write((uint8_t)n); }
size_t write(long n) { return write((uint8_t)n); }
@@ -206,7 +208,7 @@ public:
virtual int peek(void) { return serial3_peek(); }
virtual int read(void) { return serial3_getchar(); }
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; }
size_t write(unsigned long n) { return write((uint8_t)n); }
size_t write(long n) { return write((uint8_t)n); }

+ 20
- 4
teensy3/serial1.c 查看文件

@@ -56,6 +56,7 @@ static uint8_t use9Bits = 0;
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 *transmit_pin=NULL;
#if TX_BUFFER_SIZE > 255
static volatile uint16_t tx_buffer_head = 0;
static volatile uint16_t tx_buffer_tail = 0;
@@ -137,11 +138,20 @@ void serial_end(void)
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)
{
uint32_t head;
uint32_t head, n;

if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
if (transmit_pin) *transmit_pin = 1;
head = tx_buffer_head;
if (++head >= TX_BUFFER_SIZE) head = 0;
while (tx_buffer_tail == head) {
@@ -150,7 +160,9 @@ void serial_putchar(uint32_t c)
if ((UART0_S1 & UART_S1_TDRE)) {
uint32_t tail = tx_buffer_tail;
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;
}
} else if (priority >= 256) {
@@ -167,9 +179,10 @@ void serial_write(const void *buf, unsigned int count)
{
const uint8_t *p = (const uint8_t *)buf;
const uint8_t *end = p + count;
uint32_t head;
uint32_t head, n;

if (!(SIM_SCGC4 & SIM_SCGC4_UART0)) return;
if (transmit_pin) *transmit_pin = 1;
while (p < end) {
head = tx_buffer_head;
if (++head >= TX_BUFFER_SIZE) head = 0;
@@ -181,7 +194,9 @@ void serial_write(const void *buf, unsigned int count)
if ((UART0_S1 & UART_S1_TDRE)) {
uint32_t tail = tx_buffer_tail;
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;
}
} else if (priority >= 256) {
@@ -314,6 +329,7 @@ void uart0_status_isr(void)
}
if ((c & UART_C2_TCIE) && (UART0_S1 & UART_S1_TC)) {
transmitting = 0;
if (transmit_pin) *transmit_pin = 0;
UART0_C2 = C2_TX_INACTIVE;
}
}

正在加载...
取消
保存