Skip to content

Commit a5500d1

Browse files
c1728p90xc0170
authored andcommitted
Cleanup USBSerial
Make the following changes -Make blocking the first parameter in the constructor -Add destructor and proper cleanup -Add ready and wait_ready functions
1 parent 334fa33 commit a5500d1

File tree

4 files changed

+125
-41
lines changed

4 files changed

+125
-41
lines changed

usb/device/USBSerial/USBCDC.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "USBCDC.h"
1919
#include "EndpointResolver.h"
2020
#include "AsyncOp.h"
21+
#include "usb_phy_api.h"
2122

2223
static const uint8_t cdc_line_coding_default[7] = {0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08};
2324

@@ -58,20 +59,31 @@ class USBCDC::AsyncRead: public AsyncOp {
5859
bool result;
5960
};
6061

61-
USBCDC::USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking)
62-
: USBDevice(vendor_id, product_id, product_release)
62+
USBCDC::USBCDC(bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
63+
: USBDevice(get_usb_phy(), vendor_id, product_id, product_release)
6364

6465
{
65-
_init(connect_blocking);
66+
_init();
67+
if (connect_blocking) {
68+
connect();
69+
wait_ready();
70+
} else {
71+
init();
72+
}
6673
}
6774

68-
USBCDC::USBCDC(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking)
75+
USBCDC::USBCDC(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
6976
: USBDevice(phy, vendor_id, product_id, product_release)
7077
{
71-
_init(connect_blocking);
78+
_init();
7279
}
7380

74-
void USBCDC::_init(bool connect_blocking)
81+
USBCDC::~USBCDC()
82+
{
83+
deinit();
84+
}
85+
86+
void USBCDC::_init()
7587
{
7688
memcpy(_cdc_line_coding, cdc_line_coding_default, sizeof(_cdc_line_coding));
7789

@@ -91,13 +103,6 @@ void USBCDC::_init(bool connect_blocking)
91103
_rx_in_progress = false;
92104
_rx_buf = _rx_buffer;
93105
_rx_size = 0;
94-
95-
init();
96-
97-
USBDevice::connect(false);
98-
if (connect_blocking) {
99-
wait_connected();
100-
}
101106
}
102107

103108
void USBCDC::callback_reset()
@@ -234,7 +239,17 @@ void USBCDC::_change_terminal_connected(bool connected)
234239
_terminal_connected = connected;
235240
}
236241

237-
void USBCDC::wait_connected()
242+
bool USBCDC::ready()
243+
{
244+
lock();
245+
246+
bool ready = _terminal_connected;
247+
248+
unlock();
249+
return ready;
250+
}
251+
252+
void USBCDC::wait_ready()
238253
{
239254
lock();
240255

usb/device/USBSerial/USBCDC.h

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,57 @@ class AsyncOp;
2929
class USBCDC: public USBDevice {
3030
public:
3131

32-
/*
33-
* Constructor
32+
/**
33+
* Basic constructor
34+
*
35+
* Construct this object optionally connecting and blocking until it is ready.
36+
*
37+
* @note Do not use this constructor in derived classes.
3438
*
39+
* @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
3540
* @param vendor_id Your vendor_id
3641
* @param product_id Your product_id
37-
* @param product_release Your preoduct_release
38-
* @param connect_blocking define if the connection must be blocked if USB not plugged in
42+
* @param product_release Your product_release
3943
*/
40-
USBCDC(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking);
44+
USBCDC(bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
4145

42-
/*
43-
* Constructor
46+
/**
47+
* Fully featured constructor
48+
*
49+
* Construct this object with the supplied USBPhy and parameters. The user
50+
* this object is responsible for calling connect() or init().
51+
*
52+
* @note Derived classes must use this constructor and call init() or
53+
* connect() themselves. Derived classes should also call deinit() in
54+
* their destructor. This ensures that no interrupts can occur when the
55+
* object is partially constructed or destroyed.
4456
*
4557
* @param phy USB phy to use
4658
* @param vendor_id Your vendor_id
4759
* @param product_id Your product_id
48-
* @param product_release Your preoduct_release
49-
* @param connect_blocking define if the connection must be blocked if USB not plugged in
60+
* @param product_release Your product_release
5061
*/
51-
USBCDC(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release, bool connect_blocking);
62+
USBCDC(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release);
63+
64+
/**
65+
* Destroy this object
66+
*
67+
* Any classes which inherit from this class must call deinit
68+
* before this destructor runs.
69+
*/
70+
virtual ~USBCDC();
71+
72+
/**
73+
* Check if this class is ready
74+
*
75+
* @return true if a terminal is connected, false otherwise
76+
*/
77+
bool ready();
5278

5379
/**
5480
* Block until the terminal is connected
5581
*/
56-
void wait_connected();
82+
void wait_ready();
5783

5884
/*
5985
* Send a buffer
@@ -162,7 +188,7 @@ class USBCDC: public USBDevice {
162188
virtual void callback_set_configuration(uint8_t configuration);
163189
virtual void callback_set_interface(uint16_t interface, uint8_t alternate);
164190

165-
void _init(bool connect_blocking);
191+
void _init();
166192

167193
void _change_terminal_connected(bool connected);
168194
void _connect_wake_all();

usb/device/USBSerial/USBSerial.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,40 @@
1616

1717
#include "stdint.h"
1818
#include "USBSerial.h"
19+
#include "usb_phy_api.h"
20+
21+
22+
USBSerial::USBSerial(bool connect_blocking, uint16_t vendor_id, uint16_t product_id, uint16_t product_release):
23+
USBCDC(get_usb_phy(), vendor_id, product_id, product_release)
24+
{
25+
_settings_changed_callback = 0;
26+
27+
if (connect_blocking) {
28+
connect();
29+
wait_ready();
30+
} else {
31+
init();
32+
}
33+
}
34+
35+
USBSerial::USBSerial(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release):
36+
USBCDC(phy, vendor_id, product_id, product_release)
37+
{
38+
_settings_changed_callback = 0;
39+
}
40+
41+
USBSerial::~USBSerial()
42+
{
43+
deinit();
44+
}
1945

2046
int USBSerial::_putc(int c)
2147
{
22-
return send((uint8_t *)&c, 1) ? 1 : 0;
48+
if (send((uint8_t *)&c, 1)) {
49+
return c;
50+
} else {
51+
return -1;
52+
}
2353
}
2454

2555
int USBSerial::_getc()

usb/device/USBSerial/USBSerial.h

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,46 @@ class USBSerial: public USBCDC, public Stream {
4545
public:
4646

4747
/**
48-
* Constructor
48+
* Basic constructor
4949
*
50+
* Construct this object optionally connecting and blocking until it is ready.
51+
*
52+
* @note Do not use this constructor in derived classes.
53+
*
54+
* @param connect_blocking true to perform a blocking connect, false to start in a disconnected state
5055
* @param vendor_id Your vendor_id (default: 0x1f00)
5156
* @param product_id Your product_id (default: 0x2012)
52-
* @param product_release Your preoduct_release (default: 0x0001)
53-
* @param connect_blocking define if the connection must be blocked if USB not plugged in
57+
* @param product_release Your product_release (default: 0x0001)
5458
*
5559
*/
56-
USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking)
57-
{
58-
_settings_changed_callback = 0;
59-
};
60+
USBSerial(bool connect_blocking=true, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, uint16_t product_release=0x0001);
6061

6162
/**
62-
* Constructor
63+
* Fully featured constructor
64+
*
65+
* Construct this object with the supplied USBPhy and parameters. The user
66+
* this object is responsible for calling connect() or init().
67+
*
68+
* @note Derived classes must use this constructor and call init() or
69+
* connect() themselves. Derived classes should also call deinit() in
70+
* their destructor. This ensures that no interrupts can occur when the
71+
* object is partially constructed or destroyed.
6372
*
6473
* @param phy USB phy to use
6574
* @param vendor_id Your vendor_id (default: 0x1f00)
6675
* @param product_id Your product_id (default: 0x2012)
67-
* @param product_release Your preoduct_release (default: 0x0001)
68-
* @param connect_blocking define if the connection must be blocked if USB not plugged in
76+
* @param product_release Your product_release (default: 0x0001)
6977
*
7078
*/
71-
USBSerial(USBPhy *phy, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(phy, vendor_id, product_id, product_release, connect_blocking)
72-
{
73-
_settings_changed_callback = 0;
74-
};
79+
USBSerial(USBPhy *phy, uint16_t vendor_id=0x1f00, uint16_t product_id=0x2012, uint16_t product_release=0x0001);
80+
81+
/**
82+
* Destroy this object
83+
*
84+
* Any classes which inherit from this class must call deinit
85+
* before this destructor runs.
86+
*/
87+
virtual ~USBSerial();
7588

7689
/**
7790
* Send a character. You can use puts, printf.

0 commit comments

Comments
 (0)