Skip to content

Commit 49d0cd4

Browse files
Default USB Serial to non blocking etc
1 parent 91e0062 commit 49d0cd4

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

STM32F1/cores/maple/usb_serial.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static void ifaceSetupHook(unsigned, void*);
5656
#define USB_TIMEOUT 50
5757
#if BOARD_HAVE_SERIALUSB
5858
bool USBSerial::_hasBegun = false;
59+
bool USBSerial::_isBlocking = false;
5960
#endif
6061

6162
USBSerial::USBSerial(void) {
@@ -105,30 +106,37 @@ void USBSerial::end(void) {
105106
}
106107

107108
size_t USBSerial::write(uint8 ch) {
108-
size_t n = 0;
109-
this->write(&ch, 1);
110-
return n;
109+
110+
return this->write(&ch, 1);
111111
}
112112

113113
size_t USBSerial::write(const char *str) {
114-
size_t n = 0;
115-
this->write((const uint8*)str, strlen(str));
116-
return n;
114+
return this->write((const uint8*)str, strlen(str));
117115
}
118116

119117
size_t USBSerial::write(const uint8 *buf, uint32 len)
120118
{
121-
size_t n = 0;
122-
if (!(bool) *this || !buf) {
119+
#ifdef USB_SERIAL_REQUIRE_DTR
120+
if (!(bool) *this || !buf) {
121+
return 0;
122+
}
123+
#else
124+
if (!buf || !(usb_is_connected(USBLIB) && usb_is_configured(USBLIB))) {
123125
return 0;
124126
}
127+
#endif
125128

126129
uint32 txed = 0;
127-
while (txed < len) {
128-
txed += usb_cdcacm_tx((const uint8*)buf + txed, len - txed);
129-
}
130+
if (!_isBlocking) {
131+
txed = usb_cdcacm_tx((const uint8*)buf + txed, len - txed);
132+
}
133+
else {
134+
while (txed < len) {
135+
txed += usb_cdcacm_tx((const uint8*)buf + txed, len - txed);
136+
}
137+
}
130138

131-
return n;
139+
return txed;
132140
}
133141

134142
int USBSerial::available(void) {
@@ -186,10 +194,6 @@ size_t USBSerial::readBytes(char *buf, const size_t& len)
186194
/* Blocks forever until 1 byte is received */
187195
int USBSerial::read(void) {
188196
uint8 b;
189-
/*
190-
this->read(&b, 1);
191-
return b;
192-
*/
193197

194198
if (usb_cdcacm_rx(&b, 1)==0)
195199
{
@@ -217,6 +221,16 @@ USBSerial::operator bool() {
217221
return usb_is_connected(USBLIB) && usb_is_configured(USBLIB) && usb_cdcacm_get_dtr();
218222
}
219223

224+
void USBSerial::enableBlockingTx(void)
225+
{
226+
_isBlocking=true;
227+
}
228+
void USBSerial::disableBlockingTx(void)
229+
{
230+
_isBlocking=false;
231+
}
232+
233+
220234
#if BOARD_HAVE_SERIALUSB
221235
#ifdef SERIAL_USB
222236
USBSerial Serial;
@@ -265,12 +279,7 @@ static void ifaceSetupHook(unsigned hook __attribute__((unused)), void *requestv
265279
break;
266280
}
267281
#endif
268-
#if false
269-
if ((usb_cdcacm_get_baud() == 1200) && (reset_state == DTR_NEGEDGE)) {
270-
iwdg_init(IWDG_PRE_4, 10);
271-
while (1);
272-
}
273-
#endif
282+
274283
}
275284

276285
#define RESET_DELAY 100000

STM32F1/cores/maple/usb_serial.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class USBSerial : public Stream {
6969
uint8 getRTS();
7070
uint8 getDTR();
7171
uint8 pending();
72+
73+
74+
void enableBlockingTx(void);
75+
void disableBlockingTx(void);
7276

7377
/* SukkoPera: This is the Arduino way to check if an USB CDC serial
7478
* connection is open.
@@ -85,6 +89,7 @@ class USBSerial : public Stream {
8589

8690
protected:
8791
static bool _hasBegun;
92+
static bool _isBlocking;
8893
};
8994

9095
#ifdef SERIAL_USB

0 commit comments

Comments
 (0)