|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
-
-
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include "core_pins.h"
- #include "HardwareSerial.h"
- #include "wiring_private.h"
-
- #define RX_BUFFER_SIZE 64
- static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
- static volatile uint8_t rx_buffer_head = 0;
- static volatile uint8_t rx_buffer_tail = 0;
-
- #define TX_BUFFER_SIZE 40
- static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
- static volatile uint8_t tx_buffer_head = 0;
- static volatile uint8_t tx_buffer_tail = 0;
- static volatile uint8_t transmitting = 0;
- static volatile uint8_t tx_enable_pin = 255;
-
-
-
- void HardwareSerial::_begin(uint16_t baud_count, uint8_t txen_pin)
- {
- tx_enable_pin = txen_pin;
- if (txen_pin < 255) {
- pinMode(txen_pin, OUTPUT);
- digitalWrite(txen_pin, LOW);
- }
- if ((baud_count & 1) && baud_count <= 4096) {
- UCSR1A = (1<<U2X1);
- UBRR1 = baud_count - 1;
- } else {
- UCSR1A = 0;
- UBRR1 = (baud_count >> 1) - 1;
- }
- if (!(UCSR1B & (1<<TXEN1))) {
- rx_buffer_head = 0;
- rx_buffer_tail = 0;
- tx_buffer_head = 0;
- tx_buffer_tail = 0;
- transmitting = 0;
- UCSR1C = (1<<UCSZ11) | (1<<UCSZ10);
- UCSR1B = (1<<RXEN1) | (1<<TXCIE1) | (1<<TXEN1) | (1<<RXCIE1);
- }
- }
-
- void HardwareSerial::end(void)
- {
- while (transmitting) ;
- UCSR1B = 0;
- rx_buffer_head = 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)
- {
- uint8_t head, tail;
-
- head = rx_buffer_head;
- tail = rx_buffer_tail;
- if (head >= tail) return head - tail;
- return RX_BUFFER_SIZE + head - tail;
- }
-
- int HardwareSerial::availableForWrite(void)
- {
- uint8_t head, tail;
-
- head = rx_buffer_head;
- tail = rx_buffer_tail;
- if (head >= tail) return TX_BUFFER_SIZE - 1 - head + tail;
- return tail - head - 1;
- }
-
- int HardwareSerial::peek(void)
- {
- uint8_t head, tail;
-
- head = rx_buffer_head;
- tail = rx_buffer_tail;
- if (head == tail) return -1;
- if (++tail >= RX_BUFFER_SIZE) tail = 0;
- return rx_buffer[tail];
- }
-
- int HardwareSerial::read(void)
- {
- uint8_t c, i;
-
- if (rx_buffer_head == rx_buffer_tail) return -1;
- i = rx_buffer_tail + 1;
- if (i >= RX_BUFFER_SIZE) i = 0;
- c = rx_buffer[i];
- rx_buffer_tail = i;
- return c;
- }
-
- void HardwareSerial::flush()
- {
- #if ARDUINO >= 100
- while (transmitting) ;
- #else
- rx_buffer_head = rx_buffer_tail;
- #endif
- }
-
- void HardwareSerial::clear()
- {
- rx_buffer_head = rx_buffer_tail;
- }
-
- #if ARDUINO >= 100
- size_t HardwareSerial::write(uint8_t c)
- #else
- void HardwareSerial::write(uint8_t c)
- #endif
- {
- uint8_t i;
- uint8_t status;
-
- if (!(UCSR1B & (1<<TXEN1))) {
- #if ARDUINO >= 100
- setWriteError();
- return 0;
- #else
- return;
- #endif
- }
- if (tx_enable_pin < 255 && !transmitting) {
- digitalWrite(tx_enable_pin, HIGH);
- }
-
-
-
-
- if ((tx_buffer_head == tx_buffer_tail) && (UCSR1A & (1<<UDRE1))) {
- status = SREG;
- cli();
- UDR1 = c;
- transmitting = 1;
- SREG = status;
- return 1;
- }
-
- i = tx_buffer_head + 1;
- if (i >= TX_BUFFER_SIZE) i = 0;
- while (tx_buffer_tail == i) ;
- tx_buffer[i] = c;
- transmitting = 1;
- tx_buffer_head = i;
-
- sbi(UCSR1B, UDRIE1);
-
- #if ARDUINO >= 100
- return 1;
- #endif
- }
-
- ISR(USART1_RX_vect)
- {
- uint8_t c, i;
-
- c = UDR1;
- i = rx_buffer_head + 1;
- if (i >= RX_BUFFER_SIZE) i = 0;
- if (i != rx_buffer_tail) {
- rx_buffer[i] = c;
- rx_buffer_head = i;
- }
- }
-
- ISR(USART1_UDRE_vect)
- {
- uint8_t i;
-
- if (tx_buffer_head == tx_buffer_tail) {
-
-
- cbi(UCSR1B, UDRIE1);
- } else {
- i = tx_buffer_tail + 1;
- if (i >= TX_BUFFER_SIZE) i = 0;
- UDR1 = tx_buffer[i];
- tx_buffer_tail = i;
- }
- }
-
- ISR(USART1_TX_vect)
- {
- transmitting = 0;
- if (tx_enable_pin < 255) {
- digitalWrite(tx_enable_pin, LOW);
- }
- }
-
-
-
- HardwareSerial Serial1;
-
-
-
|