Skip to content

Commit 5746de8

Browse files
Move statics into SerialUSB object
About a wash for code and RAM, just cleaner
1 parent 06ce4a4 commit 5746de8

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

cores/rp2040/SerialUSB.cpp

Lines changed: 20 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,32 @@ 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, p_line_coding);
226+
}
227+
228+
void SerialUSB::tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding) {
225229
(void) itf;
226-
_bps = p_line_coding->bit_rate;
227-
CheckSerialReset();
230+
_ss.bps = p_line_coding->bit_rate;
231+
checkSerialReset();
228232
}
229233

230234
SerialUSB Serial;

cores/rp2040/SerialUSB.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <Arduino.h>
2424
#include "api/HardwareSerial.h"
2525
#include <stdarg.h>
26+
#include <tusb.h>
2627

2728
class SerialUSB : public arduino::HardwareSerial {
2829
public:
@@ -53,12 +54,27 @@ class SerialUSB : public arduino::HardwareSerial {
5354
(void) unused;
5455
}
5556

57+
// TUSB callbacks
58+
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts);
59+
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding);
60+
5661
private:
5762
bool _running = false;
58-
bool _ignoreFlowControl = false;
5963
uint8_t _id;
6064
uint8_t _epIn;
6165
uint8_t _epOut;
66+
67+
typedef struct {
68+
unsigned int rebooting : 1;
69+
unsigned int ignoreFlowControl : 1;
70+
unsigned int dtr : 1;
71+
unsigned int rts : 1;
72+
unsigned int bps : 28;
73+
} SyntheticState;
74+
SyntheticState _ss = { 0, 0, 0, 0, 115200};
75+
76+
void checkSerialReset();
77+
6278
};
6379

6480
extern SerialUSB Serial;

0 commit comments

Comments
 (0)