Skip to content

Commit d84677a

Browse files
Move statics into SerialUSB object (#3150)
About a wash for code and RAM, just cleaner organization
1 parent 7a4017b commit d84677a

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

cores/rp2040/SerialUSB.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ size_t SerialUSB::write(const uint8_t *buf, size_t length) {
139139

140140
static uint64_t last_avail_time;
141141
int written = 0;
142-
if (tud_cdc_connected() || _ignoreFlowControl) {
142+
if (tud_cdc_connected() || _ss.ignoreFlowControl) {
143143
for (size_t i = 0; i < length;) {
144144
int n = length - i;
145145
int avail = tud_cdc_write_available();
@@ -181,19 +181,15 @@ SerialUSB::operator bool() {
181181
}
182182

183183
void SerialUSB::ignoreFlowControl(bool ignore) {
184-
_ignoreFlowControl = ignore;
184+
_ss.ignoreFlowControl = ignore;
185185
}
186186

187-
static bool _dtr = false;
188-
static bool _rts = false;
189-
static int _bps = 115200;
190-
static bool _rebooting = false;
191-
static void CheckSerialReset() {
192-
if (!_rebooting && (_bps == 1200) && (!_dtr)) {
187+
void SerialUSB::checkSerialReset() {
188+
if (!_ss.rebooting && (_ss.bps == 1200) && (!_ss.dtr)) {
193189
#ifdef __FREERTOS
194190
__freertos_idle_other_core();
195191
#endif
196-
_rebooting = true;
192+
_ss.rebooting = true;
197193
// Disable NVIC IRQ, so that we don't get bothered anymore
198194
irq_set_enabled(USBCTRL_IRQ, false);
199195
// Reset the whole USB hardware block
@@ -207,24 +203,33 @@ static void CheckSerialReset() {
207203
}
208204

209205
bool SerialUSB::dtr() {
210-
return _dtr;
206+
return _ss.dtr;
211207
}
212208

213209
bool SerialUSB::rts() {
214-
return _rts;
210+
return _ss.rts;
215211
}
216212

217213
extern "C" void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
214+
Serial.tud_cdc_line_state_cb(itf, dtr, rts);
215+
}
216+
217+
void SerialUSB::tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
218218
(void) itf;
219-
_dtr = dtr ? true : false;
220-
_rts = rts ? true : false;
221-
CheckSerialReset();
219+
_ss.dtr = dtr ? 1 : 0;
220+
_ss.rts = rts ? 1 : 0;
221+
checkSerialReset();
222222
}
223223

224224
extern "C" void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding) {
225+
Serial.tud_cdc_line_coding_cb(itf, (void const *)p_line_coding);
226+
}
227+
228+
void SerialUSB::tud_cdc_line_coding_cb(uint8_t itf, void const *p) {
225229
(void) itf;
226-
_bps = p_line_coding->bit_rate;
227-
CheckSerialReset();
230+
cdc_line_coding_t const* p_line_coding = (cdc_line_coding_t const*)p;
231+
_ss.bps = p_line_coding->bit_rate;
232+
checkSerialReset();
228233
}
229234

230235
SerialUSB Serial;

cores/rp2040/SerialUSB.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,26 @@ class SerialUSB : public arduino::HardwareSerial {
5353
(void) unused;
5454
}
5555

56+
// TUSB callbacks
57+
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
58+
void tud_cdc_line_coding_cb(uint8_t itf, void const *p_line_coding); // Can't use cdc_line_coding_t const* p_line_coding, TinyUSB and BTStack conflict when we include tusb.h + BTStack.h
59+
5660
private:
5761
bool _running = false;
58-
bool _ignoreFlowControl = false;
5962
uint8_t _id;
6063
uint8_t _epIn;
6164
uint8_t _epOut;
65+
66+
typedef struct {
67+
unsigned int rebooting : 1;
68+
unsigned int ignoreFlowControl : 1;
69+
unsigned int dtr : 1;
70+
unsigned int rts : 1;
71+
unsigned int bps : 28;
72+
} SyntheticState;
73+
SyntheticState _ss = { 0, 0, 0, 0, 115200};
74+
75+
void checkSerialReset();
6276
};
6377

6478
extern SerialUSB Serial;

0 commit comments

Comments
 (0)