Skip to content
This repository was archived by the owner on May 18, 2021. It is now read-only.

Commit 2ccfcb4

Browse files
committed
moving function around, preparing for interface descriptor
1 parent 2afb232 commit 2ccfcb4

File tree

2 files changed

+88
-71
lines changed

2 files changed

+88
-71
lines changed

Adafruit_USBD_Device.cpp

Lines changed: 75 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,47 @@ bool Adafruit_USBD_Device::attach(void)
192192
return tud_connect();
193193
}
194194

195+
static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize);
196+
uint16_t const* Adafruit_USBD_Device::descriptor_string_cb(uint8_t index, uint16_t langid)
197+
{
198+
(void) langid;
199+
200+
// up to 32 unicode characters (header make it 33)
201+
static uint16_t _desc_str[33];
202+
uint8_t chr_count;
203+
204+
switch (index)
205+
{
206+
case 0:
207+
_desc_str[1] = _language_id;
208+
chr_count = 1;
209+
break;
210+
211+
case 1:
212+
chr_count = strcpy_utf16(_manufacturer, _desc_str + 1, 32);
213+
break;
214+
215+
case 2:
216+
chr_count = strcpy_utf16(_product, _desc_str + 1, 32);
217+
break;
218+
219+
case 3:
220+
// serial Number
221+
chr_count = this->getSerialDescriptor(_desc_str+1);
222+
break;
223+
224+
default: return NULL;
225+
}
226+
227+
// first byte is length (including header), second byte is string type
228+
_desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
229+
230+
return _desc_str;
231+
}
232+
233+
//--------------------------------------------------------------------+
234+
// TinyUSB stack callbacks
235+
//--------------------------------------------------------------------+
195236
extern "C"
196237
{
197238

@@ -210,6 +251,39 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
210251
return USBDevice._desc_cfg;
211252
}
212253

254+
// Invoked when received GET STRING DESCRIPTOR request
255+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
256+
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
257+
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
258+
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
259+
{
260+
return USBDevice.descriptor_string_cb(index, langid);
261+
}
262+
263+
//--------------------------------------------------------------------+
264+
// Some of TinyUSB class driver requires strong callbacks, which cause
265+
// link error if 'Adafruit_TinyUSB_Arduino' is not included, provide
266+
// weak callback here to prevent linking error
267+
//--------------------------------------------------------------------+
268+
269+
// HID
270+
TU_ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb(void) { return NULL; }
271+
TU_ATTR_WEAK uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { return 0; }
272+
TU_ATTR_WEAK void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { }
273+
274+
// MSC
275+
TU_ATTR_WEAK int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { return -1; }
276+
TU_ATTR_WEAK int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) { return -1; }
277+
TU_ATTR_WEAK void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {}
278+
TU_ATTR_WEAK bool tud_msc_test_unit_ready_cb(uint8_t lun) { return false; }
279+
TU_ATTR_WEAK void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) { }
280+
TU_ATTR_WEAK int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { return -1; }
281+
282+
} // extern C
283+
284+
//--------------------------------------------------------------------+
285+
// Helper
286+
//--------------------------------------------------------------------+
213287
constexpr static inline bool isInvalidUtf8Octet(uint8_t t) {
214288
// see bullets in https://tools.ietf.org/html/rfc3629#section-1
215289
return (t == 0xc0) || (t == 0xC1) || (t >= 0xF5);
@@ -240,7 +314,7 @@ constexpr static inline bool isInvalidUtf8Octet(uint8_t t) {
240314
// NOTE: evilUTF8 could just contain a single byte 0xF9 ....
241315
//
242316
// Attempting to decode evilUTF8 will progress to whatever is next to it on the stack.
243-
// The above should work when optimizations are turned
317+
// The above should work when optimizations are turned
244318
//
245319
static int8_t utf8Codepoint(const uint8_t *utf8, uint32_t *codepointp)
246320
{
@@ -351,67 +425,4 @@ static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize)
351425
return buflen;
352426
}
353427

354-
// up to 32 unicode characters (header make it 33)
355-
static uint16_t _desc_str[33];
356-
357-
// Invoked when received GET STRING DESCRIPTOR request
358-
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
359-
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
360-
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
361-
uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
362-
{
363-
(void) langid;
364-
365-
uint8_t chr_count;
366-
367-
switch (index)
368-
{
369-
case 0:
370-
_desc_str[1] = USBDevice.getLanguageDescriptor();
371-
chr_count = 1;
372-
break;
373-
374-
case 1:
375-
chr_count = strcpy_utf16(USBDevice.getManufacturerDescriptor(), _desc_str + 1, 32);
376-
break;
377-
378-
case 2:
379-
chr_count = strcpy_utf16(USBDevice.getProductDescriptor(), _desc_str + 1, 32);
380-
break;
381-
382-
case 3:
383-
// serial Number
384-
chr_count = USBDevice.getSerialDescriptor(_desc_str+1);
385-
break;
386-
387-
default: return NULL;
388-
}
389-
390-
// first byte is length (including header), second byte is string type
391-
_desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);
392-
393-
return _desc_str;
394-
}
395-
396-
//--------------------------------------------------------------------+
397-
// Some of TinyUSB class driver requires strong callbacks, which cause
398-
// link error if 'Adafruit_TinyUSB_Arduino' is not included, provide
399-
// weak callback here to prevent linking error
400-
//--------------------------------------------------------------------+
401-
402-
// HID
403-
TU_ATTR_WEAK uint8_t const * tud_hid_descriptor_report_cb(void) { return NULL; }
404-
TU_ATTR_WEAK uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { return 0; }
405-
TU_ATTR_WEAK void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { }
406-
407-
// MSC
408-
TU_ATTR_WEAK int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { return -1; }
409-
TU_ATTR_WEAK int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) { return -1; }
410-
TU_ATTR_WEAK void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {}
411-
TU_ATTR_WEAK bool tud_msc_test_unit_ready_cb(uint8_t lun) { return false; }
412-
TU_ATTR_WEAK void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size) { }
413-
TU_ATTR_WEAK int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) { return -1; }
414-
415-
} // extern C
416-
417428
#endif // USE_TINYUSB

Adafruit_USBD_Device.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class Adafruit_USBD_Device
3939
tusb_desc_device_t _desc_device;
4040

4141
uint8_t *_desc_cfg;
42-
uint16_t _desc_cfg_maxlen;
43-
uint16_t _desc_cfg_len;
4442
uint8_t _desc_cfg_buffer[256];
43+
uint16_t _desc_cfg_len;
44+
uint16_t _desc_cfg_maxlen;
4545

4646
uint8_t _itf_count;
4747

@@ -52,6 +52,8 @@ class Adafruit_USBD_Device
5252
const char *_manufacturer;
5353
const char *_product;
5454

55+
// const char*
56+
5557
public:
5658
Adafruit_USBD_Device(void);
5759

@@ -76,14 +78,18 @@ class Adafruit_USBD_Device
7678
bool suspended (void) { return tud_suspended(); }
7779
bool ready (void) { return tud_ready(); }
7880
bool remoteWakeup (void) { return tud_remote_wakeup(); }
79-
80-
friend uint8_t const * tud_descriptor_device_cb(void);
81-
friend uint8_t const * tud_descriptor_configuration_cb(uint8_t index);
81+
bool detach(void); // physical detach by disable pull-up
82+
bool attach(void); // physical attach by enable pull-up
8283

8384
//------------- Platform Dependent APIs -------------//
8485
uint8_t getSerialDescriptor(uint16_t* serial_str);
85-
bool detach(void); // physical detach by disable pull-up
86-
bool attach(void); // physical attach by enable pull-up
86+
87+
private:
88+
uint16_t const* descriptor_string_cb(uint8_t index, uint16_t langid);
89+
90+
friend uint8_t const * tud_descriptor_device_cb(void);
91+
friend uint8_t const * tud_descriptor_configuration_cb(uint8_t index);
92+
friend uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid);
8793
};
8894

8995
extern Adafruit_USBD_Device USBDevice;

0 commit comments

Comments
 (0)