clean up HardwareSerial code
Remove unused variables, sanitize declarations and apply uncrust.
This commit is contained in:
@@ -49,8 +49,7 @@ millis_t previous_cmd_ms = 0;
|
|||||||
#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;
|
||||||
@@ -61,16 +60,14 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -82,27 +79,20 @@ 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
|
|
||||||
{
|
|
||||||
unsigned char c = UDR3;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
#endif
|
#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
|
// There is more data in the output buffer. Send the next byte
|
||||||
unsigned char c = tx_buffer_ajg.buffer[tx_buffer_ajg.tail];
|
unsigned char c = tx_buffer_ajg.buffer[tx_buffer_ajg.tail];
|
||||||
tx_buffer_ajg.tail = (tx_buffer_ajg.tail + 1) % SERIAL_BUFFER_SIZE;
|
tx_buffer_ajg.tail = (tx_buffer_ajg.tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
@@ -117,8 +107,7 @@ HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx
|
|||||||
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;
|
||||||
@@ -136,33 +125,28 @@ HardwareSerialClass::HardwareSerialClass(ring_buffer *rx_buffer, ring_buffer *tx
|
|||||||
|
|
||||||
// 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;
|
*_ucsra = 0;
|
||||||
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
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;
|
||||||
}
|
}
|
||||||
@@ -179,34 +163,28 @@ try_again:
|
|||||||
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;
|
||||||
uint8_t current_config;
|
|
||||||
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;
|
*_ucsra = 0;
|
||||||
baud_setting = (F_CPU / 8 / baud - 1) / 2;
|
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;
|
||||||
}
|
}
|
||||||
@@ -227,11 +205,9 @@ try_again:
|
|||||||
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);
|
||||||
@@ -242,55 +218,42 @@ void HardwareSerialClass::end()
|
|||||||
_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;
|
return -1;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
return _rx_buffer->buffer[_rx_buffer->tail];
|
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
|
else {
|
||||||
{
|
|
||||||
unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
|
unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
|
||||||
_rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
|
_rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
|
||||||
return c;
|
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;
|
||||||
@@ -303,8 +266,7 @@ size_t HardwareSerialClass::write(uint8_t c)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
HardwareSerialClass::operator bool()
|
HardwareSerialClass::operator bool() {
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,6 +274,6 @@ HardwareSerialClass::operator bool()
|
|||||||
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
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user