Skip to content

Commit 8ac2905

Browse files
giulcioffifacchinm
authored andcommitted
Rename HardwareSerial classes with UART
1 parent 0f3d4da commit 8ac2905

File tree

7 files changed

+83
-55
lines changed

7 files changed

+83
-55
lines changed

cores/arduino/HardwareSerial.cpp renamed to cores/arduino/UART.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HardwareSerial.cpp - Hardware serial library for Wiring
2+
UART.cpp - Hardware serial library for Wiring
33
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -27,20 +27,21 @@
2727
#include <string.h>
2828
#include <inttypes.h>
2929
#include <util/atomic.h>
30+
#include <avr/io.h>
3031
#include "Arduino.h"
3132

32-
#include "HardwareSerial.h"
33-
#include "HardwareSerial_private.h"
33+
#include "UART.h"
34+
#include "UART_private.h"
3435

35-
// this next line disables the entire HardwareSerial.cpp,
36+
// this next line disables the entire UART.cpp,
3637
// this is so I can support Attiny series and any other chip without a uart
3738
#if defined(HAVE_HWSERIAL0) || defined(HAVE_HWSERIAL1) || defined(HAVE_HWSERIAL2) || defined(HAVE_HWSERIAL3)
3839

3940
// SerialEvent functions are weak, so when the user doesn't define them,
4041
// the linker just sets their address to 0 (which is checked below).
4142
// The Serialx_available is just a wrapper around Serialx.available(),
4243
// but we can refer to it weakly so we don't pull in the entire
43-
// HardwareSerial instance if the user doesn't also refer to it.
44+
// UART instance if the user doesn't also refer to it.
4445
#if defined(HAVE_HWSERIAL0)
4546
void serialEvent() __attribute__((weak));
4647
bool Serial0_available() __attribute__((weak));
@@ -78,15 +79,15 @@ void serialEventRun(void)
7879
}
7980

8081
// macro to guard critical sections when needed for large TX buffer sizes
81-
#if (SERIAL_TX_BUFFER_SIZE>256)
82+
#if (SERIAL_TX_BUFFER_SIZE > 256)
8283
#define TX_BUFFER_ATOMIC ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
8384
#else
8485
#define TX_BUFFER_ATOMIC
8586
#endif
8687

8788
// Actual interrupt handlers //////////////////////////////////////////////////////////////
8889

89-
void HardwareSerial::_tx_udr_empty_irq(void)
90+
void UartClass::_tx_udr_empty_irq(void)
9091
{
9192
// If interrupts are enabled, there must be more data in the output
9293
// buffer. Send the next byte
@@ -114,7 +115,7 @@ void HardwareSerial::_tx_udr_empty_irq(void)
114115

115116
// Public Methods //////////////////////////////////////////////////////////////
116117

117-
void HardwareSerial::begin(unsigned long baud, byte config)
118+
void UartClass::begin(unsigned long baud, uint16_t config)
118119
{
119120
// Try u2x mode first
120121
uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2;
@@ -149,7 +150,7 @@ void HardwareSerial::begin(unsigned long baud, byte config)
149150
cbi(*_ucsrb, UDRIE0);
150151
}
151152

152-
void HardwareSerial::end()
153+
void UartClass::end()
153154
{
154155
// wait for transmission of outgoing data
155156
flush();
@@ -163,12 +164,12 @@ void HardwareSerial::end()
163164
_rx_buffer_head = _rx_buffer_tail;
164165
}
165166

166-
int HardwareSerial::available(void)
167+
int UartClass::available(void)
167168
{
168169
return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _rx_buffer_head - _rx_buffer_tail)) % SERIAL_RX_BUFFER_SIZE;
169170
}
170171

171-
int HardwareSerial::peek(void)
172+
int UartClass::peek(void)
172173
{
173174
if (_rx_buffer_head == _rx_buffer_tail) {
174175
return -1;
@@ -177,7 +178,7 @@ int HardwareSerial::peek(void)
177178
}
178179
}
179180

180-
int HardwareSerial::read(void)
181+
int UartClass::read(void)
181182
{
182183
// if the head isn't ahead of the tail, we don't have any characters
183184
if (_rx_buffer_head == _rx_buffer_tail) {
@@ -189,7 +190,7 @@ int HardwareSerial::read(void)
189190
}
190191
}
191192

192-
int HardwareSerial::availableForWrite(void)
193+
int UartClass::availableForWrite(void)
193194
{
194195
tx_buffer_index_t head;
195196
tx_buffer_index_t tail;
@@ -202,7 +203,7 @@ int HardwareSerial::availableForWrite(void)
202203
return tail - head - 1;
203204
}
204205

205-
void HardwareSerial::flush()
206+
void UartClass::flush()
206207
{
207208
// If we have never written a byte, no need to flush. This special
208209
// case is needed since there is no way to force the TXC (transmit
@@ -222,7 +223,7 @@ void HardwareSerial::flush()
222223
// the hardware finished tranmission (TXC is set).
223224
}
224225

225-
size_t HardwareSerial::write(uint8_t c)
226+
size_t UartClass::write(uint8_t c)
226227
{
227228
_written = true;
228229
// If the buffer and the data register is empty, just write the byte

cores/arduino/HardwareSerial.h renamed to cores/arduino/UART.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HardwareSerial.h - Hardware serial library for Wiring
2+
UART.h - Hardware serial library for Wiring
33
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -21,12 +21,16 @@
2121
Modified 3 December 2013 by Matthijs Kooijman
2222
*/
2323

24-
#ifndef HardwareSerial_h
25-
#define HardwareSerial_h
24+
#ifndef _UART_H_
25+
#define _UART_H_
2626

27+
#include "Arduino.h"
2728
#include <inttypes.h>
29+
#include "api/HardwareSerial.h"
2830

29-
#include "Stream.h"
31+
#if ARDUINO_API_VERSION > 10000
32+
using namespace arduino;
33+
#endif
3034

3135
// Define constants and variables for buffering incoming serial data. We're
3236
// using a ring buffer (I think), in which head is the index of the location
@@ -65,6 +69,31 @@ typedef uint8_t rx_buffer_index_t;
6569
#endif
6670

6771
// Define config for Serial.begin(baud, config);
72+
#undef SERIAL_5N1
73+
#undef SERIAL_6N1
74+
#undef SERIAL_7N1
75+
#undef SERIAL_8N1
76+
#undef SERIAL_5N2
77+
#undef SERIAL_6N2
78+
#undef SERIAL_7N2
79+
#undef SERIAL_8N2
80+
#undef SERIAL_5E1
81+
#undef SERIAL_6E1
82+
#undef SERIAL_7E1
83+
#undef SERIAL_8E1
84+
#undef SERIAL_5E2
85+
#undef SERIAL_6E2
86+
#undef SERIAL_7E2
87+
#undef SERIAL_8E2
88+
#undef SERIAL_5O1
89+
#undef SERIAL_6O1
90+
#undef SERIAL_7O1
91+
#undef SERIAL_8O1
92+
#undef SERIAL_5O2
93+
#undef SERIAL_6O2
94+
#undef SERIAL_7O2
95+
#undef SERIAL_8O2
96+
6897
#define SERIAL_5N1 0x00
6998
#define SERIAL_6N1 0x02
7099
#define SERIAL_7N1 0x04
@@ -90,7 +119,7 @@ typedef uint8_t rx_buffer_index_t;
90119
#define SERIAL_7O2 0x3C
91120
#define SERIAL_8O2 0x3E
92121

93-
class HardwareSerial : public Stream
122+
class UartClass : public HardwareSerial
94123
{
95124
protected:
96125
volatile uint8_t * const _ubrrh;
@@ -114,12 +143,12 @@ class HardwareSerial : public Stream
114143
unsigned char _tx_buffer[SERIAL_TX_BUFFER_SIZE];
115144

116145
public:
117-
inline HardwareSerial(
146+
inline UartClass(
118147
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
119148
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
120149
volatile uint8_t *ucsrc, volatile uint8_t *udr);
121150
void begin(unsigned long baud) { begin(baud, SERIAL_8N1); }
122-
void begin(unsigned long, uint8_t);
151+
void begin(unsigned long, uint16_t);
123152
void end();
124153
virtual int available(void);
125154
virtual int peek(void);
@@ -140,22 +169,20 @@ class HardwareSerial : public Stream
140169
};
141170

142171
#if defined(UBRRH) || defined(UBRR0H)
143-
extern HardwareSerial Serial;
172+
extern UartClass Serial;
144173
#define HAVE_HWSERIAL0
145174
#endif
146175
#if defined(UBRR1H)
147-
extern HardwareSerial Serial1;
176+
extern UartClass Serial1;
148177
#define HAVE_HWSERIAL1
149178
#endif
150179
#if defined(UBRR2H)
151-
extern HardwareSerial Serial2;
180+
extern UartClass Serial2;
152181
#define HAVE_HWSERIAL2
153182
#endif
154183
#if defined(UBRR3H)
155-
extern HardwareSerial Serial3;
184+
extern UartClass Serial3;
156185
#define HAVE_HWSERIAL3
157186
#endif
158187

159-
extern void serialEventRun(void) __attribute__((weak));
160-
161188
#endif

cores/arduino/HardwareSerial0.cpp renamed to cores/arduino/UART0.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HardwareSerial0.cpp - Hardware serial library for Wiring
2+
UART0.cpp - Hardware serial library for Wiring
33
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
2323
*/
2424

2525
#include "Arduino.h"
26-
#include "HardwareSerial.h"
27-
#include "HardwareSerial_private.h"
26+
#include "UART.h"
27+
#include "UART_private.h"
2828

29-
// Each HardwareSerial is defined in its own file, since the linker pulls
29+
// Each UartClass is defined in its own file, since the linker pulls
3030
// in the entire file when any element inside is used. --gc-sections can
3131
// additionally cause unused symbols to be dropped, but ISRs have the
3232
// "used" attribute so are never dropped and they keep the
33-
// HardwareSerial instance in as well. Putting each instance in its own
33+
// UartClass instance in as well. Putting each instance in its own
3434
// file prevents the linker from pulling in any unused instances in the
3535
// first place.
3636

@@ -65,9 +65,9 @@ ISR(USART_UDRE_vect)
6565
}
6666

6767
#if defined(UBRRH) && defined(UBRRL)
68-
HardwareSerial Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
68+
UartClass Serial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
6969
#else
70-
HardwareSerial Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
70+
UartClass Serial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
7171
#endif
7272

7373
// Function that can be weakly referenced by serialEventRun to prevent

cores/arduino/HardwareSerial1.cpp renamed to cores/arduino/UART1.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HardwareSerial1.cpp - Hardware serial library for Wiring
2+
UART1.cpp - Hardware serial library for Wiring
33
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
2323
*/
2424

2525
#include "Arduino.h"
26-
#include "HardwareSerial.h"
27-
#include "HardwareSerial_private.h"
26+
#include "UART.h"
27+
#include "UART_private.h"
2828

29-
// Each HardwareSerial is defined in its own file, since the linker pulls
29+
// Each UartClass is defined in its own file, since the linker pulls
3030
// in the entire file when any element inside is used. --gc-sections can
3131
// additionally cause unused symbols to be dropped, but ISRs have the
3232
// "used" attribute so are never dropped and they keep the
33-
// HardwareSerial instance in as well. Putting each instance in its own
33+
// UartClass instance in as well. Putting each instance in its own
3434
// file prevents the linker from pulling in any unused instances in the
3535
// first place.
3636

@@ -58,7 +58,7 @@ ISR(USART1_UDRE_vect)
5858
Serial1._tx_udr_empty_irq();
5959
}
6060

61-
HardwareSerial Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
61+
UartClass Serial1(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
6262

6363
// Function that can be weakly referenced by serialEventRun to prevent
6464
// pulling in this file if it's not otherwise used.

cores/arduino/HardwareSerial2.cpp renamed to cores/arduino/UART2.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HardwareSerial2.cpp - Hardware serial library for Wiring
2+
UartClass2.cpp - Hardware serial library for Wiring
33
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
2323
*/
2424

2525
#include "Arduino.h"
26-
#include "HardwareSerial.h"
27-
#include "HardwareSerial_private.h"
26+
#include "UART.h"
27+
#include "UART_private.h"
2828

29-
// Each HardwareSerial is defined in its own file, since the linker pulls
29+
// Each UartClass is defined in its own file, since the linker pulls
3030
// in the entire file when any element inside is used. --gc-sections can
3131
// additionally cause unused symbols to be dropped, but ISRs have the
3232
// "used" attribute so are never dropped and they keep the
33-
// HardwareSerial instance in as well. Putting each instance in its own
33+
// UartClass instance in as well. Putting each instance in its own
3434
// file prevents the linker from pulling in any unused instances in the
3535
// first place.
3636

@@ -46,7 +46,7 @@ ISR(USART2_UDRE_vect)
4646
Serial2._tx_udr_empty_irq();
4747
}
4848

49-
HardwareSerial Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
49+
UartClass Serial2(&UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2);
5050

5151
// Function that can be weakly referenced by serialEventRun to prevent
5252
// pulling in this file if it's not otherwise used.

cores/arduino/HardwareSerial3.cpp renamed to cores/arduino/UART3.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HardwareSerial3.cpp - Hardware serial library for Wiring
2+
UartClass3.cpp - Hardware serial library for Wiring
33
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
44
55
This library is free software; you can redistribute it and/or
@@ -23,14 +23,14 @@
2323
*/
2424

2525
#include "Arduino.h"
26-
#include "HardwareSerial.h"
27-
#include "HardwareSerial_private.h"
26+
#include "UART.h"
27+
#include "UART_private.h"
2828

29-
// Each HardwareSerial is defined in its own file, since the linker pulls
29+
// Each UartClass is defined in its own file, since the linker pulls
3030
// in the entire file when any element inside is used. --gc-sections can
3131
// additionally cause unused symbols to be dropped, but ISRs have the
3232
// "used" attribute so are never dropped and they keep the
33-
// HardwareSerial instance in as well. Putting each instance in its own
33+
// UartClass instance in as well. Putting each instance in its own
3434
// file prevents the linker from pulling in any unused instances in the
3535
// first place.
3636

@@ -46,7 +46,7 @@ ISR(USART3_UDRE_vect)
4646
Serial3._tx_udr_empty_irq();
4747
}
4848

49-
HardwareSerial Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
49+
UartClass Serial3(&UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3);
5050

5151
// Function that can be weakly referenced by serialEventRun to prevent
5252
// pulling in this file if it's not otherwise used.

cores/arduino/HardwareSerial_private.h renamed to cores/arduino/UART_private.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484

8585
// Constructors ////////////////////////////////////////////////////////////////
8686

87-
HardwareSerial::HardwareSerial(
87+
UartClass::UartClass(
8888
volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
8989
volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
9090
volatile uint8_t *ucsrc, volatile uint8_t *udr) :
@@ -98,7 +98,7 @@ HardwareSerial::HardwareSerial(
9898

9999
// Actual interrupt handlers //////////////////////////////////////////////////////////////
100100

101-
void HardwareSerial::_rx_complete_irq(void)
101+
void UartClass::_rx_complete_irq(void)
102102
{
103103
if (bit_is_clear(*_ucsra, UPE0)) {
104104
// No Parity error, read byte and store it in the buffer if there is

0 commit comments

Comments
 (0)