Skip to content

Commit

Permalink
Default USB Serial to non blocking etc
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerclarkmelbourne committed Feb 3, 2019
1 parent 91e0062 commit 49d0cd4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
53 changes: 31 additions & 22 deletions STM32F1/cores/maple/usb_serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ static void ifaceSetupHook(unsigned, void*);
#define USB_TIMEOUT 50
#if BOARD_HAVE_SERIALUSB
bool USBSerial::_hasBegun = false;
bool USBSerial::_isBlocking = false;
#endif

USBSerial::USBSerial(void) {
Expand Down Expand Up @@ -105,30 +106,37 @@ void USBSerial::end(void) {
}

size_t USBSerial::write(uint8 ch) {
size_t n = 0;
this->write(&ch, 1);
return n;

return this->write(&ch, 1);
}

size_t USBSerial::write(const char *str) {
size_t n = 0;
this->write((const uint8*)str, strlen(str));
return n;
return this->write((const uint8*)str, strlen(str));
}

size_t USBSerial::write(const uint8 *buf, uint32 len)
{
size_t n = 0;
if (!(bool) *this || !buf) {
#ifdef USB_SERIAL_REQUIRE_DTR
if (!(bool) *this || !buf) {
return 0;
}
#else
if (!buf || !(usb_is_connected(USBLIB) && usb_is_configured(USBLIB))) {
return 0;
}
#endif

uint32 txed = 0;
while (txed < len) {
txed += usb_cdcacm_tx((const uint8*)buf + txed, len - txed);
}
if (!_isBlocking) {
txed = usb_cdcacm_tx((const uint8*)buf + txed, len - txed);
}
else {
while (txed < len) {
txed += usb_cdcacm_tx((const uint8*)buf + txed, len - txed);
}
}

return n;
return txed;
}

int USBSerial::available(void) {
Expand Down Expand Up @@ -186,10 +194,6 @@ size_t USBSerial::readBytes(char *buf, const size_t& len)
/* Blocks forever until 1 byte is received */
int USBSerial::read(void) {
uint8 b;
/*
this->read(&b, 1);
return b;
*/

if (usb_cdcacm_rx(&b, 1)==0)
{
Expand Down Expand Up @@ -217,6 +221,16 @@ USBSerial::operator bool() {
return usb_is_connected(USBLIB) && usb_is_configured(USBLIB) && usb_cdcacm_get_dtr();
}

void USBSerial::enableBlockingTx(void)
{
_isBlocking=true;
}
void USBSerial::disableBlockingTx(void)
{
_isBlocking=false;
}


#if BOARD_HAVE_SERIALUSB
#ifdef SERIAL_USB
USBSerial Serial;
Expand Down Expand Up @@ -265,12 +279,7 @@ static void ifaceSetupHook(unsigned hook __attribute__((unused)), void *requestv
break;
}
#endif
#if false
if ((usb_cdcacm_get_baud() == 1200) && (reset_state == DTR_NEGEDGE)) {
iwdg_init(IWDG_PRE_4, 10);
while (1);
}
#endif

}

#define RESET_DELAY 100000
Expand Down
5 changes: 5 additions & 0 deletions STM32F1/cores/maple/usb_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class USBSerial : public Stream {
uint8 getRTS();
uint8 getDTR();
uint8 pending();


void enableBlockingTx(void);
void disableBlockingTx(void);

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

protected:
static bool _hasBegun;
static bool _isBlocking;
};

#ifdef SERIAL_USB
Expand Down

0 comments on commit 49d0cd4

Please sign in to comment.