clean up HardwareSerial code

Remove unused variables, sanitize declarations and apply uncrust.
This commit is contained in:
Stefan Kalscheuer
2022-09-03 09:27:41 +02:00
parent afee7b10c2
commit 6368552ced
2 changed files with 227 additions and 266 deletions

View File

@@ -33,285 +33,247 @@
// this next line disables the entire HardwareSerial.cpp, // this next line disables the entire HardwareSerial.cpp,
// this is so I can support Attiny series and any other chip without a uart // this is so I can support Attiny series and any other chip without a uart
#if defined(UBRR3H) #if defined(UBRR3H)
#include "HardwareSerial.h" #include "HardwareSerial.h"
millis_t previous_cmd_ms = 0; millis_t previous_cmd_ms = 0;
// Define constants and variables for buffering incoming serial data. We're // Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which head is the index of the location // using a ring buffer (I think), in which head is the index of the location
// to which to write the next incoming character and tail is the index of the // to which to write the next incoming character and tail is the index of the
// location from which to read. // location from which to read.
#if (RAMEND < 1000) #if (RAMEND < 1000)
#define SERIAL_BUFFER_SIZE 16 #define SERIAL_BUFFER_SIZE 16
#else #else
#define SERIAL_BUFFER_SIZE 64 #define SERIAL_BUFFER_SIZE 64
#endif #endif
struct ring_buffer struct ring_buffer {
{ unsigned char buffer[SERIAL_BUFFER_SIZE];
unsigned char buffer[SERIAL_BUFFER_SIZE]; volatile unsigned int head;
volatile unsigned int head; volatile unsigned int tail;
volatile unsigned int tail; };
};
#if defined(UBRR3H) #if defined(UBRR3H)
ring_buffer rx_buffer_ajg = {{0}, 0, 0}; ring_buffer rx_buffer_ajg = {{0}, 0, 0};
ring_buffer tx_buffer_ajg = {{0}, 0, 0}; ring_buffer tx_buffer_ajg = {{0}, 0, 0};
#endif #endif
inline void store_char(unsigned char c, ring_buffer *buffer) inline void store_char(unsigned char c, ring_buffer *buffer) {
{ unsigned int i = ((unsigned int)(buffer->head + 1)) % SERIAL_BUFFER_SIZE;
unsigned int i = ((unsigned int)(buffer->head + 1)) % SERIAL_BUFFER_SIZE;
// if we should be storing the received character into the location // if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the // just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer // current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head. // and so we don't write the character or advance the head.
if (i != buffer->tail) if (i != buffer->tail) {
{ buffer->buffer[buffer->head] = c;
buffer->buffer[buffer->head] = c; buffer->head = i;
buffer->head = i; }
} }
}
#if defined(USART3_RX_vect) && defined(UDR3) #if defined(USART3_RX_vect) && defined(UDR3)
void serialEvent3() __attribute__((weak)); void serialEvent3() __attribute__((weak));
void serialEvent3() {} void serialEvent3() {}
#define serialEvent3_implemented #define serialEvent3_implemented
ISR(USART3_RX_vect) ISR(USART3_RX_vect)
{ {
if (bit_is_clear(UCSR3A, UPE3)) if (bit_is_clear(UCSR3A, UPE3)) {
{ unsigned char c = UDR3;
unsigned char c = UDR3; store_char(c, &rx_buffer_ajg);
store_char(c, &rx_buffer_ajg); }
} }
else #endif
{
unsigned char c = UDR3;
};
}
#endif
#ifdef USART3_UDRE_vect #ifdef USART3_UDRE_vect
ISR(USART3_UDRE_vect) ISR(USART3_UDRE_vect)
{ {
if (tx_buffer_ajg.head == tx_buffer_ajg.tail) if (tx_buffer_ajg.head == tx_buffer_ajg.tail) {
{ cbi(UCSR3B, UDRIE3);
cbi(UCSR3B, UDRIE3); }
} else {
else // There is more data in the output buffer. Send the next byte
{ unsigned char c = tx_buffer_ajg.buffer[tx_buffer_ajg.tail];
// There is more data in the output buffer. Send the next byte tx_buffer_ajg.tail = (tx_buffer_ajg.tail + 1) % SERIAL_BUFFER_SIZE;
unsigned char c = tx_buffer_ajg.buffer[tx_buffer_ajg.tail];
tx_buffer_ajg.tail = (tx_buffer_ajg.tail + 1) % SERIAL_BUFFER_SIZE;
UDR3 = c; UDR3 = c;
} }
} }
#endif #endif
HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr, volatile uint8_t *ucsrc, volatile uint8_t *udr,
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x) uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x) {
{ _rx_buffer = rx_buffer;
_rx_buffer = rx_buffer; _tx_buffer = tx_buffer;
_tx_buffer = tx_buffer; _ubrrh = ubrrh;
_ubrrh = ubrrh; _ubrrl = ubrrl;
_ubrrl = ubrrl; _ucsra = ucsra;
_ucsra = ucsra; _ucsrb = ucsrb;
_ucsrb = ucsrb; _ucsrc = ucsrc;
_ucsrc = ucsrc; _udr = udr;
_udr = udr; _rxen = rxen;
_rxen = rxen; _txen = txen;
_txen = txen; _rxcie = rxcie;
_rxcie = rxcie; _udrie = udrie;
_udrie = udrie; _u2x = u2x;
_u2x = u2x; }
}
// Public Methods ////////////////////////////////////////////////////////////// // Public Methods //////////////////////////////////////////////////////////////
void HardwareSerialClass::begin(unsigned long baud) void HardwareSerialClass::begin(unsigned long baud) {
{ uint16_t baud_setting;
uint16_t baud_setting; bool use_u2x = true;
bool use_u2x = true;
#if F_CPU == 16000000UL #if F_CPU == 16000000UL
if (baud == 57600) if (baud == 57600)
{ use_u2x = false;
use_u2x = false;
} #endif
#endif
try_again: try_again:
if (use_u2x) if (use_u2x) {
{ *_ucsra = 1 << _u2x;
*_ucsra = 1 << _u2x; baud_setting = (F_CPU / 4 / baud - 1) / 2;
baud_setting = (F_CPU / 4 / baud - 1) / 2; }
} else {
else *_ucsra = 0;
{ baud_setting = (F_CPU / 8 / baud - 1) / 2;
*_ucsra = 0; }
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
if ((baud_setting > 4095) && use_u2x) if ((baud_setting > 4095) && use_u2x) {
{ use_u2x = false;
use_u2x = false; goto try_again;
goto try_again; }
}
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
*_ubrrh = baud_setting >> 8; *_ubrrh = baud_setting >> 8;
*_ubrrl = baud_setting; *_ubrrl = baud_setting;
transmitting = false; transmitting = false;
sbi(*_ucsrb, _rxen); sbi(*_ucsrb, _rxen);
sbi(*_ucsrb, _txen); sbi(*_ucsrb, _txen);
sbi(*_ucsrb, _rxcie); sbi(*_ucsrb, _rxcie);
cbi(*_ucsrb, _udrie); cbi(*_ucsrb, _udrie);
} }
void HardwareSerialClass::begin(unsigned long baud, byte config) void HardwareSerialClass::begin(unsigned long baud, byte config) {
{ uint16_t baud_setting;
uint16_t baud_setting; bool use_u2x = true;
uint8_t current_config;
bool use_u2x = true;
#if F_CPU == 16000000UL #if F_CPU == 16000000UL
if (baud == 57600) if (baud == 57600)
{ use_u2x = false;
use_u2x = false;
} #endif
#endif
try_again: try_again:
if (use_u2x) if (use_u2x) {
{ *_ucsra = 1 << _u2x;
*_ucsra = 1 << _u2x; baud_setting = (F_CPU / 4 / baud - 1) / 2;
baud_setting = (F_CPU / 4 / baud - 1) / 2; }
} else {
else *_ucsra = 0;
{ baud_setting = (F_CPU / 8 / baud - 1) / 2;
*_ucsra = 0; }
baud_setting = (F_CPU / 8 / baud - 1) / 2;
}
if ((baud_setting > 4095) && use_u2x) if ((baud_setting > 4095) && use_u2x) {
{ use_u2x = false;
use_u2x = false; goto try_again;
goto try_again; }
}
// assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
*_ubrrh = baud_setting >> 8; *_ubrrh = baud_setting >> 8;
*_ubrrl = baud_setting; *_ubrrl = baud_setting;
//set the data bits, parity, and stop bits // set the data bits, parity, and stop bits
#if defined(__AVR_ATmega8__) #if defined(__AVR_ATmega8__)
config |= 0x80; // select UCSRC register (shared with UBRRH) config |= 0x80; // select UCSRC register (shared with UBRRH)
#endif #endif
*_ucsrc = config; *_ucsrc = config;
sbi(*_ucsrb, _rxen); sbi(*_ucsrb, _rxen);
sbi(*_ucsrb, _txen); sbi(*_ucsrb, _txen);
sbi(*_ucsrb, _rxcie); sbi(*_ucsrb, _rxcie);
cbi(*_ucsrb, _udrie); cbi(*_ucsrb, _udrie);
} }
void HardwareSerialClass::end() void HardwareSerialClass::end() {
{ // wait for transmission of outgoing data
// wait for transmission of outgoing data while (_tx_buffer->head != _tx_buffer->tail);
while (_tx_buffer->head != _tx_buffer->tail)
;
cbi(*_ucsrb, _rxen); cbi(*_ucsrb, _rxen);
cbi(*_ucsrb, _txen); cbi(*_ucsrb, _txen);
cbi(*_ucsrb, _rxcie); cbi(*_ucsrb, _rxcie);
cbi(*_ucsrb, _udrie); cbi(*_ucsrb, _udrie);
// clear any received data // clear any received data
_rx_buffer->head = _rx_buffer->tail; _rx_buffer->head = _rx_buffer->tail;
} }
int HardwareSerialClass::available(void) int HardwareSerialClass::available(void) {
{ return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE; }
}
int HardwareSerialClass::peek(void) int HardwareSerialClass::peek(void) {
{ if (_rx_buffer->head == _rx_buffer->tail)
if (_rx_buffer->head == _rx_buffer->tail) return -1;
{ else
return -1; return _rx_buffer->buffer[_rx_buffer->tail];
} }
else
{
return _rx_buffer->buffer[_rx_buffer->tail];
}
}
int HardwareSerialClass::read(void) int HardwareSerialClass::read(void) {
{ // if the head isn't ahead of the tail, we don't have any characters
// if the head isn't ahead of the tail, we don't have any characters if (_rx_buffer->head == _rx_buffer->tail) {
if (_rx_buffer->head == _rx_buffer->tail) return -1;
{ }
return -1; else {
} unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
else _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
{ return c;
unsigned char c = _rx_buffer->buffer[_rx_buffer->tail]; }
_rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE; }
return c;
}
}
void HardwareSerialClass::flush() void HardwareSerialClass::flush() {
{ // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT while (transmitting && !(*_ucsra & _BV(TXC0)));
while (transmitting && !(*_ucsra & _BV(TXC0))) transmitting = false;
; }
transmitting = false;
}
size_t HardwareSerialClass::write(uint8_t c) size_t HardwareSerialClass::write(uint8_t c) {
{ unsigned int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to // If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit // wait for the interrupt handler to empty it a bit
// ???: return 0 here instead? // ???: return 0 here instead?
while (i == _tx_buffer->tail) while (i == _tx_buffer->tail);
;
_tx_buffer->buffer[_tx_buffer->head] = c; _tx_buffer->buffer[_tx_buffer->head] = c;
_tx_buffer->head = i; _tx_buffer->head = i;
sbi(*_ucsrb, _udrie); sbi(*_ucsrb, _udrie);
// clear the TXC bit -- "can be cleared by writing a one to its bit location" // clear the TXC bit -- "can be cleared by writing a one to its bit location"
transmitting = true; transmitting = true;
sbi(*_ucsra, TXC0); sbi(*_ucsra, TXC0);
return 1; return 1;
} }
HardwareSerialClass::operator bool() HardwareSerialClass::operator bool() {
{ return true;
return true; }
}
#if defined(UBRR3H) #if defined(UBRR3H)
HardwareSerialClass HardwareSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3); HardwareSerialClass HardwareSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
#endif #endif
#endif #endif // if defined(UBRR3H)
#endif // whole file #endif // whole file

View File

@@ -35,41 +35,41 @@ struct ring_buffer;
class HardwareSerialClass : public Stream class HardwareSerialClass : public Stream
{ {
private: private:
ring_buffer *_rx_buffer; ring_buffer *_rx_buffer;
ring_buffer *_tx_buffer; ring_buffer *_tx_buffer;
volatile uint8_t *_ubrrh; volatile uint8_t *_ubrrh;
volatile uint8_t *_ubrrl; volatile uint8_t *_ubrrl;
volatile uint8_t *_ucsra; volatile uint8_t *_ucsra;
volatile uint8_t *_ucsrb; volatile uint8_t *_ucsrb;
volatile uint8_t *_ucsrc; volatile uint8_t *_ucsrc;
volatile uint8_t *_udr; volatile uint8_t *_udr;
uint8_t _rxen; uint8_t _rxen;
uint8_t _txen; uint8_t _txen;
uint8_t _rxcie; uint8_t _rxcie;
uint8_t _udrie; uint8_t _udrie;
uint8_t _u2x; uint8_t _u2x;
bool transmitting; bool transmitting;
public: public:
HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
volatile uint8_t *ucsrc, volatile uint8_t *udr, volatile uint8_t *ucsrc, volatile uint8_t *udr,
uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x); uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
void begin(unsigned long); void begin(unsigned long);
void begin(unsigned long, uint8_t); void begin(unsigned long, uint8_t);
void end(); void end();
virtual int available(void); virtual int available(void);
virtual int peek(void); virtual int peek(void);
virtual int read(void); virtual int read(void);
virtual void flush(void); virtual void flush(void);
virtual size_t write(uint8_t); virtual size_t write(uint8_t);
inline size_t write(unsigned long n) { return write((uint8_t)n); } inline size_t write(unsigned long n) { return write((uint8_t)n); }
inline size_t write(long n) { return write((uint8_t)n); } inline size_t write(long n) { return write((uint8_t)n); }
inline size_t write(unsigned int n) { return write((uint8_t)n); } inline size_t write(unsigned int n) { return write((uint8_t)n); }
inline size_t write(int n) { return write((uint8_t)n); } inline size_t write(int n) { return write((uint8_t)n); }
using Print::write; // pull in write(str) and write(buf, size) from Print using Print::write; // pull in write(str) and write(buf, size) from Print
operator bool(); operator bool();
}; };
// Define config for Serial.begin(baud, config); // Define config for Serial.begin(baud, config);
@@ -99,7 +99,7 @@ public:
#define SERIAL_8O2 0x3E #define SERIAL_8O2 0x3E
#if defined(UBRR3H) #if defined(UBRR3H)
extern HardwareSerialClass HardwareSerial; extern HardwareSerialClass HardwareSerial;
#endif #endif
extern void serialEventRun(void) __attribute__((weak)); extern void serialEventRun(void) __attribute__((weak));
@@ -131,8 +131,7 @@ const char newSucc[] PROGMEM = "OK";
#define HARDWARE_SERIAL_ECHOPGM(x) HARDWARE_SERIAL_PROTOCOLPGM(x) #define HARDWARE_SERIAL_ECHOPGM(x) HARDWARE_SERIAL_PROTOCOLPGM(x)
#define HARDWARE_SERIAL_ECHO(x) HARDWARE_SERIAL_PROTOCOL(x) #define HARDWARE_SERIAL_ECHO(x) HARDWARE_SERIAL_PROTOCOL(x)
FORCE_INLINE void HardwareSerialprintPGM(const char *str) FORCE_INLINE void HardwareSerialprintPGM(const char *str) {
{
char ch = pgm_read_byte(str); char ch = pgm_read_byte(str);
while (ch) while (ch)
{ {
@@ -141,4 +140,4 @@ FORCE_INLINE void HardwareSerialprintPGM(const char *str)
} }
} }
#endif #endif // ifndef hardwareserial_h