Skip to content

Commit 4bfd54a

Browse files
committed
improve device & cdc
1 parent 46b4133 commit 4bfd54a

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed

src/Adafruit_USBD_CDC.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface
3939
virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize);
4040

4141
void setPins(uint8_t pin_rx, uint8_t pin_tx) { (void) pin_rx; (void) pin_tx; }
42-
void begin(uint32_t baud_count);
42+
void begin(uint32_t baud);
4343
void begin(uint32_t baud, uint8_t config);
4444
void end(void);
4545

@@ -65,6 +65,10 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface
6565
virtual int availableForWrite(void);
6666
using Print::write; // pull in write(str) from Print
6767
operator bool();
68+
69+
private:
70+
bool _begun;
71+
uint8_t _itf;
6872
};
6973

7074
extern Adafruit_USBD_CDC Serial;

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@
2525
#ifdef USE_TINYUSB
2626

2727
#include "Arduino.h"
28-
#include "tusb.h"
28+
#include "Adafruit_USBD_Device.h"
2929
#include "Adafruit_USBD_CDC.h"
30-
3130
#include "arduino/ports/Adafruit_TinyUSB_Port.h"
3231

32+
// TODO Multiple instances supports
33+
// static uint8_t _itf_count;
34+
// static Adafruit_USBD_CDC* _itf_arr[]
35+
3336
#define EPOUT 0x00
3437
#define EPIN 0x80
3538

3639
Adafruit_USBD_CDC Serial;
3740

3841
Adafruit_USBD_CDC::Adafruit_USBD_CDC(void)
3942
{
40-
43+
_begun = false;
44+
_itf = 0;
4145
}
4246

4347
uint16_t Adafruit_USBD_CDC::getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize)
@@ -57,12 +61,18 @@ uint16_t Adafruit_USBD_CDC::getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t
5761
void Adafruit_USBD_CDC::begin (uint32_t baud)
5862
{
5963
(void) baud;
64+
65+
if (_begun) return;
66+
_begun = true;
67+
68+
Serial.setStringDescriptor("TinyUSB Serial");
69+
USBDevice.addInterface(Serial);
6070
}
6171

6272
void Adafruit_USBD_CDC::begin (uint32_t baud, uint8_t config)
6373
{
64-
(void) baud;
6574
(void) config;
75+
this->begin(baud);
6676
}
6777

6878
void Adafruit_USBD_CDC::end(void)

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ enum
5555
Adafruit_USBD_Device USBDevice;
5656

5757
Adafruit_USBD_Device::Adafruit_USBD_Device(void)
58+
{
59+
60+
}
61+
62+
void Adafruit_USBD_Device::clearConfiguration(void)
5863
{
5964
tusb_desc_device_t const desc_dev =
6065
{
@@ -81,21 +86,14 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
8186

8287
memcpy(_desc_device, &desc_dev, sizeof(tusb_desc_device_t));
8388

84-
tusb_desc_configuration_t const dev_cfg =
89+
uint8_t const dev_cfg [sizeof(tusb_desc_configuration_t)] =
8590
{
86-
.bLength = sizeof(tusb_desc_configuration_t),
87-
.bDescriptorType = TUSB_DESC_CONFIGURATION,
88-
91+
// Config number, interface count, string index, total length, attribute (bit 7 set to 1), power in mA
8992
// Total Length & Interface Number will be updated later
90-
.wTotalLength = 0,
91-
.bNumInterfaces = 0,
92-
.bConfigurationValue = 1,
93-
.iConfiguration = 0x00,
94-
.bmAttributes = TU_BIT(7) | TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP,
95-
.bMaxPower = TUSB_DESC_CONFIG_POWER_MA(USB_CONFIG_POWER)
93+
TUD_CONFIG_DESCRIPTOR(1, 0, 0, sizeof(tusb_desc_configuration_t), TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP | TU_BIT(7), 100),
9694
};
9795

98-
memcpy(_desc_cfg_buffer, &dev_cfg, sizeof(tusb_desc_configuration_t));
96+
memcpy(_desc_cfg_buffer, dev_cfg, sizeof(tusb_desc_configuration_t));
9997
_desc_cfg = _desc_cfg_buffer;
10098
_desc_cfg_maxlen = sizeof(_desc_cfg_buffer);
10199
_desc_cfg_len = sizeof(tusb_desc_configuration_t);
@@ -166,10 +164,9 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface& itf)
166164
return true;
167165
}
168166

169-
void Adafruit_USBD_Device::setDescriptorBuffer(uint8_t* buf, uint32_t buflen)
167+
void Adafruit_USBD_Device::setConfigurationBuffer(uint8_t* buf, uint32_t buflen)
170168
{
171-
if (buflen < _desc_cfg_maxlen)
172-
return;
169+
if (buflen < _desc_cfg_maxlen) return;
173170

174171
memcpy(buf, _desc_cfg, _desc_cfg_len);
175172
_desc_cfg = buf;
@@ -192,7 +189,6 @@ void Adafruit_USBD_Device::setDeviceVersion(uint16_t bcd)
192189
((tusb_desc_device_t*)_desc_device)->bcdDevice = bcd;
193190
}
194191

195-
196192
void Adafruit_USBD_Device::setLanguageDescriptor (uint16_t language_id)
197193
{
198194
_desc_str_arr[STRID_LANGUAGE] = (const char*) ((uint32_t) language_id);
@@ -233,9 +229,11 @@ uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t* serial_utf16)
233229

234230
bool Adafruit_USBD_Device::begin(uint8_t rhport)
235231
{
236-
Serial.setStringDescriptor("TinyUSB Serial");
237-
USBDevice.addInterface(Serial);
238-
USBDevice.setID(USB_VID, USB_PID);
232+
clearConfiguration();
233+
setID(USB_VID, USB_PID);
234+
235+
// Serial is always added by default
236+
Serial.begin(115200);
239237

240238
TinyUSB_Port_InitDeviceController(rhport);
241239

src/arduino/Adafruit_USBD_Device.h

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,50 @@ class Adafruit_USBD_Device
5555
public:
5656
Adafruit_USBD_Device(void);
5757

58-
// Device descriptor
58+
//------------- Device descriptor -------------//
59+
60+
// Set VID, PID
5961
void setID(uint16_t vid, uint16_t pid);
62+
63+
// Set bcdUSB version e.g 1.0, 2.0, 2.1
6064
void setVersion(uint16_t bcd);
65+
66+
// Set bcdDevice version
6167
void setDeviceVersion(uint16_t bcd);
6268

63-
// Configuration descriptor
69+
//------------- Configuration descriptor -------------//
70+
71+
// Add an new interface
6472
bool addInterface(Adafruit_USBD_Interface& itf);
65-
void setDescriptorBuffer(uint8_t* buf, uint32_t buflen);
6673

67-
// String descriptor
74+
// Clear/Reset configuration descriptor
75+
void clearConfiguration(void);
76+
77+
// Provide user buffer for configuration descriptor, needed if total length > 256
78+
void setConfigurationBuffer(uint8_t* buf, uint32_t buflen);
79+
80+
//------------- String descriptor -------------//
6881
void setLanguageDescriptor(uint16_t language_id);
6982
void setManufacturerDescriptor(const char *s);
7083
void setProductDescriptor(const char *s);
7184
uint8_t getSerialDescriptor(uint16_t* serial_utf16);
7285

73-
// Control
86+
//------------- Control -------------//
87+
7488
bool begin(uint8_t rhport=0);
7589
void task(void);
7690

77-
// status
78-
bool mounted (void);
79-
bool suspended (void);
80-
bool ready (void);
81-
bool remoteWakeup (void);
82-
8391
// physical disable/enable pull-up
8492
bool detach (void);
8593
bool attach (void);
8694

8795

96+
//------------- status -------------//
97+
bool mounted (void);
98+
bool suspended (void);
99+
bool ready (void);
100+
bool remoteWakeup (void);
101+
88102
private:
89103
uint16_t const* descriptor_string_cb(uint8_t index, uint16_t langid);
90104

src/common/tusb_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ typedef struct TU_ATTR_PACKED
309309
uint8_t bMaxPower ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA).
310310
} tusb_desc_configuration_t;
311311

312+
TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct");
313+
312314
/// USB Interface Descriptor
313315
typedef struct TU_ATTR_PACKED
314316
{

0 commit comments

Comments
 (0)