From 6368552cedef94c1c20e5536038fb7ced6115ea2 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sat, 3 Sep 2022 09:27:41 +0200 Subject: [PATCH] clean up HardwareSerial code Remove unused variables, sanitize declarations and apply uncrust. --- Marlin/src/lcd/HardwareSerial.cpp | 420 ++++++++++++++---------------- Marlin/src/lcd/HardwareSerial.h | 73 +++--- 2 files changed, 227 insertions(+), 266 deletions(-) diff --git a/Marlin/src/lcd/HardwareSerial.cpp b/Marlin/src/lcd/HardwareSerial.cpp index c0179fd1..49fc08f0 100755 --- a/Marlin/src/lcd/HardwareSerial.cpp +++ b/Marlin/src/lcd/HardwareSerial.cpp @@ -33,285 +33,247 @@ // this next line disables the entire HardwareSerial.cpp, // 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 // 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 // location from which to read. -#if (RAMEND < 1000) - #define SERIAL_BUFFER_SIZE 16 -#else - #define SERIAL_BUFFER_SIZE 64 -#endif + #if (RAMEND < 1000) + #define SERIAL_BUFFER_SIZE 16 + #else + #define SERIAL_BUFFER_SIZE 64 + #endif -struct ring_buffer -{ - unsigned char buffer[SERIAL_BUFFER_SIZE]; - volatile unsigned int head; - volatile unsigned int tail; -}; + struct ring_buffer { + unsigned char buffer[SERIAL_BUFFER_SIZE]; + volatile unsigned int head; + volatile unsigned int tail; + }; -#if defined(UBRR3H) -ring_buffer rx_buffer_ajg = {{0}, 0, 0}; -ring_buffer tx_buffer_ajg = {{0}, 0, 0}; -#endif + #if defined(UBRR3H) + ring_buffer rx_buffer_ajg = {{0}, 0, 0}; + ring_buffer tx_buffer_ajg = {{0}, 0, 0}; + #endif -inline void store_char(unsigned char c, ring_buffer *buffer) -{ - unsigned int i = ((unsigned int)(buffer->head + 1)) % SERIAL_BUFFER_SIZE; + inline void store_char(unsigned char c, ring_buffer *buffer) { + unsigned int i = ((unsigned int)(buffer->head + 1)) % SERIAL_BUFFER_SIZE; - // if we should be storing the received character into the location - // just before the tail (meaning that the head would advance to the - // current location of the tail), we're about to overflow the buffer - // and so we don't write the character or advance the head. - if (i != buffer->tail) - { - buffer->buffer[buffer->head] = c; - buffer->head = i; - } -} + // if we should be storing the received character into the location + // just before the tail (meaning that the head would advance to the + // current location of the tail), we're about to overflow the buffer + // and so we don't write the character or advance the head. + if (i != buffer->tail) { + buffer->buffer[buffer->head] = c; + buffer->head = i; + } + } -#if defined(USART3_RX_vect) && defined(UDR3) -void serialEvent3() __attribute__((weak)); -void serialEvent3() {} -#define serialEvent3_implemented -ISR(USART3_RX_vect) -{ - if (bit_is_clear(UCSR3A, UPE3)) - { - unsigned char c = UDR3; - store_char(c, &rx_buffer_ajg); - } - else - { - unsigned char c = UDR3; - }; -} -#endif + #if defined(USART3_RX_vect) && defined(UDR3) + void serialEvent3() __attribute__((weak)); + void serialEvent3() {} + #define serialEvent3_implemented + ISR(USART3_RX_vect) + { + if (bit_is_clear(UCSR3A, UPE3)) { + unsigned char c = UDR3; + store_char(c, &rx_buffer_ajg); + } + } + #endif -#ifdef USART3_UDRE_vect -ISR(USART3_UDRE_vect) -{ - if (tx_buffer_ajg.head == tx_buffer_ajg.tail) - { - cbi(UCSR3B, UDRIE3); - } - else - { - // There is more data in the output buffer. Send the next byte - unsigned char c = tx_buffer_ajg.buffer[tx_buffer_ajg.tail]; - tx_buffer_ajg.tail = (tx_buffer_ajg.tail + 1) % SERIAL_BUFFER_SIZE; + #ifdef USART3_UDRE_vect + ISR(USART3_UDRE_vect) + { + if (tx_buffer_ajg.head == tx_buffer_ajg.tail) { + cbi(UCSR3B, UDRIE3); + } + else { + // There is more data in the output buffer. Send the next byte + 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; - } -} -#endif + UDR3 = c; + } + } + #endif -HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, - volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, - volatile uint8_t *ucsra, volatile uint8_t *ucsrb, - volatile uint8_t *ucsrc, volatile uint8_t *udr, - uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x) -{ - _rx_buffer = rx_buffer; - _tx_buffer = tx_buffer; - _ubrrh = ubrrh; - _ubrrl = ubrrl; - _ucsra = ucsra; - _ucsrb = ucsrb; - _ucsrc = ucsrc; - _udr = udr; - _rxen = rxen; - _txen = txen; - _rxcie = rxcie; - _udrie = udrie; - _u2x = u2x; -} + HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, + volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, + volatile uint8_t *ucsra, volatile uint8_t *ucsrb, + volatile uint8_t *ucsrc, volatile uint8_t *udr, + uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x) { + _rx_buffer = rx_buffer; + _tx_buffer = tx_buffer; + _ubrrh = ubrrh; + _ubrrl = ubrrl; + _ucsra = ucsra; + _ucsrb = ucsrb; + _ucsrc = ucsrc; + _udr = udr; + _rxen = rxen; + _txen = txen; + _rxcie = rxcie; + _udrie = udrie; + _u2x = u2x; + } // Public Methods ////////////////////////////////////////////////////////////// -void HardwareSerialClass::begin(unsigned long baud) -{ - uint16_t baud_setting; - bool use_u2x = true; + void HardwareSerialClass::begin(unsigned long baud) { + uint16_t baud_setting; + bool use_u2x = true; -#if F_CPU == 16000000UL - if (baud == 57600) - { - use_u2x = false; - } -#endif + #if F_CPU == 16000000UL + if (baud == 57600) + use_u2x = false; + + #endif try_again: - if (use_u2x) - { - *_ucsra = 1 << _u2x; - baud_setting = (F_CPU / 4 / baud - 1) / 2; - } - else - { - *_ucsra = 0; - baud_setting = (F_CPU / 8 / baud - 1) / 2; - } + if (use_u2x) { + *_ucsra = 1 << _u2x; + baud_setting = (F_CPU / 4 / baud - 1) / 2; + } + else { + *_ucsra = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } - if ((baud_setting > 4095) && use_u2x) - { - use_u2x = false; - goto try_again; - } + if ((baud_setting > 4095) && use_u2x) { + use_u2x = false; + goto try_again; + } - // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) - *_ubrrh = baud_setting >> 8; - *_ubrrl = baud_setting; + // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) + *_ubrrh = baud_setting >> 8; + *_ubrrl = baud_setting; - transmitting = false; + transmitting = false; - sbi(*_ucsrb, _rxen); - sbi(*_ucsrb, _txen); - sbi(*_ucsrb, _rxcie); - cbi(*_ucsrb, _udrie); -} + sbi(*_ucsrb, _rxen); + sbi(*_ucsrb, _txen); + sbi(*_ucsrb, _rxcie); + cbi(*_ucsrb, _udrie); + } -void HardwareSerialClass::begin(unsigned long baud, byte config) -{ - uint16_t baud_setting; - uint8_t current_config; - bool use_u2x = true; + void HardwareSerialClass::begin(unsigned long baud, byte config) { + uint16_t baud_setting; + bool use_u2x = true; -#if F_CPU == 16000000UL - if (baud == 57600) - { - use_u2x = false; - } -#endif + #if F_CPU == 16000000UL + if (baud == 57600) + use_u2x = false; + + #endif try_again: - if (use_u2x) - { - *_ucsra = 1 << _u2x; - baud_setting = (F_CPU / 4 / baud - 1) / 2; - } - else - { - *_ucsra = 0; - baud_setting = (F_CPU / 8 / baud - 1) / 2; - } + if (use_u2x) { + *_ucsra = 1 << _u2x; + baud_setting = (F_CPU / 4 / baud - 1) / 2; + } + else { + *_ucsra = 0; + baud_setting = (F_CPU / 8 / baud - 1) / 2; + } - if ((baud_setting > 4095) && use_u2x) - { - use_u2x = false; - goto try_again; - } + if ((baud_setting > 4095) && use_u2x) { + use_u2x = false; + goto try_again; + } - // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) - *_ubrrh = baud_setting >> 8; - *_ubrrl = baud_setting; + // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register) + *_ubrrh = baud_setting >> 8; + *_ubrrl = baud_setting; - //set the data bits, parity, and stop bits -#if defined(__AVR_ATmega8__) - config |= 0x80; // select UCSRC register (shared with UBRRH) -#endif - *_ucsrc = config; + // set the data bits, parity, and stop bits + #if defined(__AVR_ATmega8__) + config |= 0x80; // select UCSRC register (shared with UBRRH) + #endif + *_ucsrc = config; - sbi(*_ucsrb, _rxen); - sbi(*_ucsrb, _txen); - sbi(*_ucsrb, _rxcie); - cbi(*_ucsrb, _udrie); -} + sbi(*_ucsrb, _rxen); + sbi(*_ucsrb, _txen); + sbi(*_ucsrb, _rxcie); + cbi(*_ucsrb, _udrie); + } -void HardwareSerialClass::end() -{ - // wait for transmission of outgoing data - while (_tx_buffer->head != _tx_buffer->tail) - ; + void HardwareSerialClass::end() { + // wait for transmission of outgoing data + while (_tx_buffer->head != _tx_buffer->tail); - cbi(*_ucsrb, _rxen); - cbi(*_ucsrb, _txen); - cbi(*_ucsrb, _rxcie); - cbi(*_ucsrb, _udrie); + cbi(*_ucsrb, _rxen); + cbi(*_ucsrb, _txen); + cbi(*_ucsrb, _rxcie); + cbi(*_ucsrb, _udrie); - // clear any received data - _rx_buffer->head = _rx_buffer->tail; -} + // clear any received data + _rx_buffer->head = _rx_buffer->tail; + } -int HardwareSerialClass::available(void) -{ - return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE; -} + int HardwareSerialClass::available(void) { + return (int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE; + } -int HardwareSerialClass::peek(void) -{ - if (_rx_buffer->head == _rx_buffer->tail) - { - return -1; - } - else - { - return _rx_buffer->buffer[_rx_buffer->tail]; - } -} + int HardwareSerialClass::peek(void) { + if (_rx_buffer->head == _rx_buffer->tail) + return -1; + else + return _rx_buffer->buffer[_rx_buffer->tail]; + } -int HardwareSerialClass::read(void) -{ - // if the head isn't ahead of the tail, we don't have any characters - if (_rx_buffer->head == _rx_buffer->tail) - { - return -1; - } - else - { - unsigned char c = _rx_buffer->buffer[_rx_buffer->tail]; - _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE; - return c; - } -} + int HardwareSerialClass::read(void) { + // if the head isn't ahead of the tail, we don't have any characters + if (_rx_buffer->head == _rx_buffer->tail) { + return -1; + } + else { + 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() -{ - // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT - while (transmitting && !(*_ucsra & _BV(TXC0))) - ; - transmitting = false; -} + void HardwareSerialClass::flush() { + // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT + while (transmitting && !(*_ucsra & _BV(TXC0))); + transmitting = false; + } -size_t HardwareSerialClass::write(uint8_t c) -{ - int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; + size_t HardwareSerialClass::write(uint8_t c) { + unsigned int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; - // If the output buffer is full, there's nothing for it other than to - // wait for the interrupt handler to empty it a bit - // ???: return 0 here instead? - while (i == _tx_buffer->tail) - ; + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + // ???: return 0 here instead? + while (i == _tx_buffer->tail); - _tx_buffer->buffer[_tx_buffer->head] = c; - _tx_buffer->head = i; + _tx_buffer->buffer[_tx_buffer->head] = c; + _tx_buffer->head = i; - sbi(*_ucsrb, _udrie); - // clear the TXC bit -- "can be cleared by writing a one to its bit location" - transmitting = true; - sbi(*_ucsra, TXC0); + sbi(*_ucsrb, _udrie); + // clear the TXC bit -- "can be cleared by writing a one to its bit location" + transmitting = true; + sbi(*_ucsra, TXC0); - return 1; -} + return 1; + } -HardwareSerialClass::operator bool() -{ - return true; -} + HardwareSerialClass::operator bool() { + return true; + } -#if defined(UBRR3H) -HardwareSerialClass HardwareSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3); -#endif + #if defined(UBRR3H) + HardwareSerialClass HardwareSerial(&rx_buffer_ajg, &tx_buffer_ajg, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3); + #endif -#endif + #endif // if defined(UBRR3H) #endif // whole file diff --git a/Marlin/src/lcd/HardwareSerial.h b/Marlin/src/lcd/HardwareSerial.h index b8048ed7..11a5b2f1 100755 --- a/Marlin/src/lcd/HardwareSerial.h +++ b/Marlin/src/lcd/HardwareSerial.h @@ -35,41 +35,41 @@ struct ring_buffer; class HardwareSerialClass : public Stream { private: - ring_buffer *_rx_buffer; - ring_buffer *_tx_buffer; - volatile uint8_t *_ubrrh; - volatile uint8_t *_ubrrl; - volatile uint8_t *_ucsra; - volatile uint8_t *_ucsrb; - volatile uint8_t *_ucsrc; - volatile uint8_t *_udr; - uint8_t _rxen; - uint8_t _txen; - uint8_t _rxcie; - uint8_t _udrie; - uint8_t _u2x; - bool transmitting; +ring_buffer *_rx_buffer; +ring_buffer *_tx_buffer; +volatile uint8_t *_ubrrh; +volatile uint8_t *_ubrrl; +volatile uint8_t *_ucsra; +volatile uint8_t *_ucsrb; +volatile uint8_t *_ucsrc; +volatile uint8_t *_udr; +uint8_t _rxen; +uint8_t _txen; +uint8_t _rxcie; +uint8_t _udrie; +uint8_t _u2x; +bool transmitting; public: - HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, - volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, - volatile uint8_t *ucsra, volatile uint8_t *ucsrb, - volatile uint8_t *ucsrc, volatile uint8_t *udr, - uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x); - void begin(unsigned long); - void begin(unsigned long, uint8_t); - void end(); - virtual int available(void); - virtual int peek(void); - virtual int read(void); - virtual void flush(void); - virtual size_t write(uint8_t); - 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(unsigned 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 - operator bool(); +HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx_buffer, + volatile uint8_t *ubrrh, volatile uint8_t *ubrrl, + volatile uint8_t *ucsra, volatile uint8_t *ucsrb, + volatile uint8_t *ucsrc, volatile uint8_t *udr, + uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x); +void begin(unsigned long); +void begin(unsigned long, uint8_t); +void end(); +virtual int available(void); +virtual int peek(void); +virtual int read(void); +virtual void flush(void); +virtual size_t write(uint8_t); +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(unsigned 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 +operator bool(); }; // Define config for Serial.begin(baud, config); @@ -99,7 +99,7 @@ public: #define SERIAL_8O2 0x3E #if defined(UBRR3H) -extern HardwareSerialClass HardwareSerial; + extern HardwareSerialClass HardwareSerial; #endif 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_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); while (ch) { @@ -141,4 +140,4 @@ FORCE_INLINE void HardwareSerialprintPGM(const char *str) } } -#endif +#endif // ifndef hardwareserial_h