Skip to content

Commit 7a8d3ee

Browse files
committed
Speedup compilation by making singletons opaque
1 parent 1dcd3a3 commit 7a8d3ee

30 files changed

+257
-85
lines changed

cores/arduino/Arduino.h

+14-39
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,23 @@
2323
#if !defined(Arduino_h) && !defined(ARDUINO_LIB_DISCOVERY_PHASE)
2424
#define Arduino_h
2525

26-
#if defined(__cplusplus)
2726
#if !defined(ARDUINO_AS_MBED_LIBRARY)
28-
2927
#include "pinmode_arduino.h"
30-
31-
#ifdef F
32-
#define Arduino_F F
33-
#undef F
34-
#endif // F (mbed included after arduino.h)
35-
#define F Mbed_F
36-
#endif // !ARDUINO_AS_MBED_LIBRARY
37-
#include "mbed_config.h"
38-
#include "drivers/InterruptIn.h"
39-
#include "drivers/PwmOut.h"
40-
#include "drivers/AnalogIn.h"
41-
#include "drivers/DigitalInOut.h"
42-
#include "mbed.h"
43-
#undef F
44-
#endif //__cplusplus
45-
46-
#if defined(ARDUINO_AS_MBED_LIBRARY)
47-
#define PinMode ArduinoPinMode
48-
#define Arduino_F F
4928
#endif
5029

5130
#include "api/ArduinoAPI.h"
5231

5332
#if defined(__cplusplus)
33+
34+
#undef F
35+
// C++11 F replacement declaration
36+
template <typename T1>
37+
auto F(T1&& A)
38+
-> const arduino::__FlashStringHelper*
39+
{
40+
return (const arduino::__FlashStringHelper*)A;
41+
}
42+
5443
#if !defined(ARDUINO_AS_MBED_LIBRARY)
5544
using namespace arduino;
5645
#endif
@@ -91,28 +80,15 @@ void analogWriteResolution(int bits);
9180

9281
#ifdef __cplusplus
9382
// Types used for the table below
94-
typedef struct _PinDescription
95-
{
96-
PinName name;
97-
mbed::InterruptIn* irq;
98-
mbed::PwmOut* pwm;
99-
mbed::DigitalInOut* gpio;
100-
} PinDescription ;
101-
102-
typedef struct _AnalogPinDescription
103-
{
104-
PinName name;
105-
mbed::AnalogIn* adc;
106-
} AnalogPinDescription ;
107-
108-
int PinNameToIndex(PinName P);
83+
typedef struct _PinDescription PinDescription;
84+
typedef struct _AnalogPinDescription AnalogPinDescription;
10985

11086
// Pins table to be instantiated into variant.cpp
11187
extern PinDescription g_APinDescription[];
11288
extern AnalogPinDescription g_AAnalogPinDescription[];
11389

11490
#ifdef ANALOG_CONFIG
115-
91+
#include "hal/analogin_api.h"
11692
typedef enum _AnalogReferenceMode AnalogReferenceMode;
11793
void analogReference(uint8_t mode);
11894
/* nRF specific function to change analog acquisition time */
@@ -128,8 +104,7 @@ extern analogin_config_t adcCurrentConfig;
128104

129105
#include "Serial.h"
130106
#if defined(SERIAL_CDC)
131-
#include "USB/PluggableUSBSerial.h"
132-
#define Serial SerialUSB
107+
#define Serial _UART_USB_
133108
#else
134109
#define Serial _UART1_
135110
#endif

cores/arduino/Interrupts.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "Arduino.h"
20+
#include "pinDefinitions.h"
2021

2122
void detachInterrupt(PinName interruptNum) {
2223
pin_size_t idx = PinNameToIndex(interruptNum);

cores/arduino/Serial.cpp

+125-17
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,30 @@
2121
*/
2222

2323
#include "Arduino.h"
24+
#include "pinDefinitions.h"
25+
#include "Serial.h"
26+
#include "drivers/UnbufferedSerial.h"
27+
#if defined(SERIAL_CDC)
28+
#include "USB/PluggableUSBSerial.h"
29+
#endif
2430

2531
#ifdef Serial
2632
#undef Serial
2733
#endif
2834

2935
using namespace arduino;
3036

37+
struct _mbed_serial {
38+
mbed::UnbufferedSerial* obj;
39+
};
40+
3141
void UART::begin(unsigned long baudrate, uint16_t config) {
42+
43+
#if defined(SERIAL_CDC)
44+
if (is_usb) {
45+
return;
46+
}
47+
#endif
3248
begin(baudrate);
3349
int bits = 8;
3450
mbed::SerialBase::Parity parity = mbed::SerialBase::None;
@@ -69,47 +85,81 @@ void UART::begin(unsigned long baudrate, uint16_t config) {
6985
break;
7086
}
7187

72-
_serial->format(bits, parity, stop_bits);
88+
_serial->obj->format(bits, parity, stop_bits);
7389
}
7490

7591
void UART::begin(unsigned long baudrate) {
92+
#if defined(SERIAL_CDC)
93+
if (is_usb) {
94+
return;
95+
}
96+
#endif
7697
if (_serial == NULL) {
77-
_serial = new mbed::UnbufferedSerial(tx, rx, baudrate);
98+
_serial = new mbed_serial;
99+
_serial->obj = NULL;
100+
}
101+
if (_serial->obj == NULL) {
102+
_serial->obj = new mbed::UnbufferedSerial(_tx, _rx, baudrate);
78103
} else {
79-
_serial->baud(baudrate);
104+
_serial->obj->baud(baudrate);
80105
}
81-
if (rts != NC) {
82-
_serial->set_flow_control(mbed::SerialBase::Flow::RTSCTS, rts, cts);
106+
if (_rts != NC) {
107+
_serial->obj->set_flow_control(mbed::SerialBase::Flow::RTSCTS, _rts, _cts);
83108
}
84-
if (_serial != NULL) {
85-
_serial->attach(mbed::callback(this, &UART::on_rx), mbed::SerialBase::RxIrq);
109+
if (_serial->obj != NULL) {
110+
_serial->obj->attach(mbed::callback(this, &UART::on_rx), mbed::SerialBase::RxIrq);
86111
}
87112
}
88113

89114
void UART::on_rx() {
90-
while(_serial->readable()) {
115+
#if defined(SERIAL_CDC)
116+
if (is_usb) {
117+
return;
118+
}
119+
#endif
120+
while(_serial->obj->readable()) {
91121
char c;
92-
_serial->read(&c, 1);
122+
_serial->obj->read(&c, 1);
93123
rx_buffer.store_char(c);
94124
}
95125
}
96126

97127
void UART::end() {
98-
if (_serial != NULL) {
99-
delete _serial;
100-
_serial = NULL;
128+
#if defined(SERIAL_CDC)
129+
if (is_usb) {
130+
return SerialUSB.end();
131+
}
132+
#endif
133+
if (_serial->obj != NULL) {
134+
delete _serial->obj;
135+
_serial->obj = NULL;
101136
}
102137
}
103138

104139
int UART::available() {
140+
#if defined(SERIAL_CDC)
141+
if (is_usb) {
142+
return SerialUSB.available();
143+
}
144+
#endif
105145
return rx_buffer.available();
106146
}
107147

108148
int UART::peek() {
149+
#if defined(SERIAL_CDC)
150+
if (is_usb) {
151+
return SerialUSB.peek();
152+
}
153+
#endif
109154
return rx_buffer.peek();
110155
}
111156

112157
int UART::read() {
158+
#if defined(SERIAL_CDC)
159+
if (is_usb) {
160+
return SerialUSB.read();
161+
}
162+
#endif
113163
return rx_buffer.read_char();
114164
}
115165

@@ -118,15 +168,25 @@ void UART::flush() {
118168
}
119169

120170
size_t UART::write(uint8_t c) {
121-
while (!_serial->writeable()) {}
122-
int ret = _serial->write(&c, 1);
171+
#if defined(SERIAL_CDC)
172+
if (is_usb) {
173+
return SerialUSB.write(c);
174+
}
175+
#endif
176+
while (!_serial->obj->writeable()) {}
177+
int ret = _serial->obj->write(&c, 1);
123178
return ret == -1 ? 0 : 1;
124179
}
125180

126181
size_t UART::write(const uint8_t* c, size_t len) {
127-
while (!_serial->writeable()) {}
128-
_serial->set_blocking(true);
129-
int ret = _serial->write(c, len);
182+
#if defined(SERIAL_CDC)
183+
if (is_usb) {
184+
return SerialUSB.write(c, len);
185+
}
186+
#endif
187+
while (!_serial->obj->writeable()) {}
188+
_serial->obj->set_blocking(true);
189+
int ret = _serial->obj->write(c, len);
130190
return ret == -1 ? 0 : len;
131191
}
132192

@@ -135,9 +195,57 @@ void UART::block_tx(int _a) {
135195
}
136196

137197
UART::operator bool() {
198+
#if defined(SERIAL_CDC)
199+
if (is_usb) {
200+
return SerialUSB;
201+
}
202+
#endif
138203
return 1;
139204
}
140205

206+
#if defined(SERIAL_CDC)
207+
uint32_t UART::baud() {
208+
if (is_usb) {
209+
return SerialUSB.baud();
210+
}
211+
return 0;
212+
}
213+
uint8_t UART::stopbits() {
214+
if (is_usb) {
215+
return SerialUSB.stopbits();
216+
}
217+
return 0;
218+
}
219+
uint8_t UART::paritytype() {
220+
if (is_usb) {
221+
return SerialUSB.paritytype();
222+
}
223+
return 0;
224+
}
225+
uint8_t UART::numbits() {
226+
if (is_usb) {
227+
return SerialUSB.numbits();
228+
}
229+
return 0;
230+
}
231+
bool UART::dtr() {
232+
if (is_usb) {
233+
return SerialUSB.dtr();
234+
}
235+
return false;
236+
}
237+
bool UART::rts() {
238+
if (is_usb) {
239+
return SerialUSB.rts();
240+
}
241+
return false;
242+
}
243+
#endif
244+
245+
#if defined(SERIAL_CDC)
246+
UART _UART_USB_;
247+
#endif
248+
141249
#if SERIAL_HOWMANY > 0
142250

143251
#ifdef SERIAL1_RTS

cores/arduino/Serial.h

+22-4
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,24 @@
2323
#include "api/RingBuffer.h"
2424
#include "Arduino.h"
2525
#include "api/HardwareSerial.h"
26-
#include "drivers/UnbufferedSerial.h"
26+
#include "PinNames.h"
2727

2828
#ifdef __cplusplus
2929

3030
#ifndef __ARDUINO_UART_IMPLEMENTATION__
3131
#define __ARDUINO_UART_IMPLEMENTATION__
3232

33+
typedef struct _mbed_serial mbed_serial;
34+
typedef struct _mbed_usb_serial mbed_usb_serial;
35+
3336
namespace arduino {
3437

3538
class UART : public HardwareSerial {
3639
public:
37-
UART(int _tx, int _rx, int _rts, int _cts) : tx((PinName)_tx), rx((PinName)_rx), rts((PinName)_rts), cts((PinName)_cts) {};
40+
UART(int tx, int rx, int rts, int cts) : _tx((PinName)tx), _rx((PinName)rx), _rts((PinName)rts), _cts((PinName)cts) {}
41+
UART() {
42+
is_usb = true;
43+
}
3844
void begin(unsigned long);
3945
void begin(unsigned long baudrate, uint16_t config);
4046
void end();
@@ -47,23 +53,35 @@ class UART : public HardwareSerial {
4753
using Print::write; // pull in write(str) and write(buf, size) from Print
4854
operator bool();
4955

56+
#if defined(SERIAL_CDC)
57+
uint32_t baud();
58+
uint8_t stopbits();
59+
uint8_t paritytype();
60+
uint8_t numbits();
61+
bool dtr();
62+
bool rts();
63+
#endif
64+
5065
private:
5166
void on_rx();
5267
void block_tx(int);
5368
bool _block;
5469
// See https://github.com/ARMmbed/mbed-os/blob/f5b5989fc81c36233dbefffa1d023d1942468d42/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/serial_api.c#L76
5570
const size_t WRITE_BUFF_SZ = 32;
56-
mbed::UnbufferedSerial* _serial = NULL;
57-
PinName tx, rx, rts, cts;
71+
mbed_serial* _serial = NULL;
72+
mbed_usb_serial* _usb_serial = NULL;
73+
PinName _tx, _rx, _rts, _cts;
5874
RingBufferN<256> rx_buffer;
5975
uint8_t intermediate_buf[4];
76+
bool is_usb = false;
6077
};
6178
}
6279

6380
extern arduino::UART _UART1_;
6481
extern arduino::UART _UART2_;
6582
extern arduino::UART _UART3_;
6683
extern arduino::UART _UART4_;
84+
extern arduino::UART _UART_USB_;
6785

6886
#endif
6987
#endif

cores/arduino/Tone.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Arduino.h"
2+
#include "pinDefinitions.h"
23
#include "mbed.h"
34

45
using namespace std::chrono_literals;

cores/arduino/USB/USBCDC.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818

1919
#include "Arduino.h"
20-
#include "PeripheralPins.h"
2120

2221
#if DEVICE_USBDEVICE && defined(SERIAL_CDC)
2322

0 commit comments

Comments
 (0)