Skip to content

Commit a38ac7e

Browse files
authored
Merge pull request #274 from adafruit/develop
more with usb
2 parents a00ab2e + 217264b commit a38ac7e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+340
-511
lines changed

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_TinyUSB_Core.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ void Adafruit_TinyUSB_Core_init(void)
9494
xTaskCreate( usb_device_task, "usbd", USBD_STACK_SZ, NULL, TASK_PRIO_HIGH, NULL);
9595
}
9696

97+
void Adafruit_TinyUSB_Core_touch1200(void)
98+
{
99+
enterSerialDfu();
100+
}
101+
97102
uint8_t load_serial_number(uint16_t* serial_str)
98103
{
99104
// Serial is 64-bit DeviceID -> 16 chars len

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_TinyUSB_Core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@
3939
// Called by main.cpp to initialize usb device typically with CDC device for Serial
4040
void Adafruit_TinyUSB_Core_init(void);
4141

42+
// Invoked when host disconnects cdc at baud 1200, usually touch feature to go into DFU mode
43+
void Adafruit_TinyUSB_Core_touch1200(void);
44+
4245
#endif /* ADAFRUIT_TINYUSB_CORE_H_ */

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
131131
cdc_line_coding_t coding;
132132
tud_cdc_get_line_coding(&coding);
133133

134-
if ( coding.bit_rate == 1200 ) enterSerialDfu();
134+
if ( coding.bit_rate == 1200 ) Adafruit_TinyUSB_Core_touch1200();
135135
}
136136
}
137137

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.cpp

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,57 +36,6 @@
3636

3737
extern uint8_t load_serial_number(uint16_t* serial_str);
3838

39-
extern "C"
40-
{
41-
42-
// tud_desc_set is required by tinyusb stack
43-
tud_desc_set_t tud_desc_set =
44-
{
45-
.device = NULL, // update later
46-
.config = NULL, // update later
47-
.hid_report = NULL // update later
48-
};
49-
50-
// Invoked when received GET_STRING_DESC request
51-
// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
52-
// Return number of characters. Note usb string is in 16-bits unicode format
53-
uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
54-
{
55-
switch (index)
56-
{
57-
case 0:
58-
// language = English
59-
desc[0] = 0x0409;
60-
return 1;
61-
62-
case 1: // Manufacturer
63-
case 2: // Product
64-
{
65-
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
66-
67-
// cap at max char
68-
uint8_t count = strlen(str);
69-
if ( count > max_char ) count = max_char;
70-
71-
for(uint8_t i=0; i<count; i++)
72-
{
73-
*desc++ = str[i];
74-
}
75-
return count;
76-
}
77-
break;
78-
79-
case 3:
80-
return load_serial_number(desc);
81-
82-
default: return 0;
83-
}
84-
85-
return 0;
86-
}
87-
88-
} // extern C
89-
9039
Adafruit_USBD_Device USBDevice;
9140

9241
Adafruit_USBD_Device::Adafruit_USBD_Device(void)
@@ -97,17 +46,11 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
9746
.bDescriptorType = TUSB_DESC_DEVICE,
9847
.bcdUSB = 0x0200,
9948

100-
#if CFG_TUD_CDC
10149
// Use Interface Association Descriptor (IAD) for CDC
10250
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
10351
.bDeviceClass = TUSB_CLASS_MISC,
10452
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
10553
.bDeviceProtocol = MISC_PROTOCOL_IAD,
106-
#else
107-
.bDeviceClass = 0x00,
108-
.bDeviceSubClass = 0x00,
109-
.bDeviceProtocol = 0x00,
110-
#endif
11154

11255
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
11356

@@ -144,9 +87,6 @@ Adafruit_USBD_Device::Adafruit_USBD_Device(void)
14487
_desc_cfglen = sizeof(tusb_desc_configuration_t);
14588
_itf_count = 0;
14689
_epin_count = _epout_count = 1;
147-
148-
tud_desc_set.config = _desc_cfg;
149-
tud_desc_set.device = &_desc_device;
15090
}
15191

15292
// Add interface descriptor
@@ -206,4 +146,70 @@ bool Adafruit_USBD_Device::begin(void)
206146
return true;
207147
}
208148

149+
extern "C"
150+
{
151+
152+
// Invoked when received GET DEVICE DESCRIPTOR
153+
// Application return pointer to descriptor
154+
uint8_t const * tud_descriptor_device_cb(void)
155+
{
156+
return (uint8_t const *) &USBDevice._desc_device;
157+
}
158+
159+
// Invoked when received GET CONFIGURATION DESCRIPTOR
160+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
161+
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
162+
{
163+
(void) index; // for multiple configurations
164+
return USBDevice._desc_cfg;
165+
}
166+
167+
static uint16_t _desc_str[32];
168+
169+
// Invoked when received GET STRING DESCRIPTOR request
170+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
171+
uint16_t const* tud_descriptor_string_cb(uint8_t index)
172+
{
173+
uint8_t chr_count;
174+
175+
switch (index)
176+
{
177+
case 0:
178+
// language = English
179+
_desc_str[1] = 0x0409;
180+
chr_count = 1;
181+
break;
182+
183+
case 1: // Manufacturer
184+
case 2: // Product
185+
{
186+
char const * str = (index == 1) ? USB_MANUFACTURER : USB_PRODUCT;
187+
188+
// cap at max char
189+
chr_count = strlen(str);
190+
if ( chr_count > 31 ) chr_count = 31;
191+
192+
for(uint8_t i=0; i<chr_count; i++)
193+
{
194+
_desc_str[1+i] = str[i];
195+
}
196+
}
197+
break;
198+
199+
case 3:
200+
// serial Number
201+
chr_count = load_serial_number(_desc_str+1);
202+
break;
203+
204+
default: return NULL;
205+
}
206+
207+
// first byte is len, second byte is string type
208+
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
209+
210+
return _desc_str;
211+
}
212+
213+
} // extern C
214+
209215
#endif // USE_TINYUSB

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_Device.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Adafruit_USBD_Device
5858
bool suspended(void) { return tud_suspended(); }
5959
bool ready(void) { return tud_ready(); }
6060
bool remoteWakeup(void) { return tud_remote_wakeup(); }
61+
62+
friend uint8_t const * tud_descriptor_device_cb(void);
63+
friend uint8_t const * tud_descriptor_configuration_cb(uint8_t index);
6164
};
6265

6366
extern Adafruit_USBD_Device USBDevice;

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/audio/audio.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2018, hathach (tinyusb.org)
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2018, hathach (tinyusb.org)
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc_device.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2018, hathach (tinyusb.org)
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -99,7 +99,7 @@ static void _prep_out_transaction (uint8_t itf)
9999
bool tud_cdc_n_connected(uint8_t itf)
100100
{
101101
// DTR (bit 0) active is considered as connected
102-
return tud_ready() && TU_BIT_TEST(_cdcd_itf[itf].line_state, 0);
102+
return tud_ready() && tu_bit_test(_cdcd_itf[itf].line_state, 0);
103103
}
104104

105105
uint8_t tud_cdc_n_get_line_state (uint8_t itf)
@@ -126,9 +126,9 @@ uint32_t tud_cdc_n_available(uint8_t itf)
126126
return tu_fifo_count(&_cdcd_itf[itf].rx_ff);
127127
}
128128

129-
char tud_cdc_n_read_char(uint8_t itf)
129+
signed char tud_cdc_n_read_char(uint8_t itf)
130130
{
131-
char ch;
131+
signed char ch;
132132
return tud_cdc_n_read(itf, &ch, 1) ? ch : (-1);
133133
}
134134

@@ -139,9 +139,9 @@ uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
139139
return num_read;
140140
}
141141

142-
char tud_cdc_n_peek(uint8_t itf, int pos)
142+
signed char tud_cdc_n_peek(uint8_t itf, int pos)
143143
{
144-
char ch;
144+
signed char ch;
145145
return tu_fifo_peek_at(&_cdcd_itf[itf].rx_ff, pos, &ch) ? ch : (-1);
146146
}
147147

@@ -358,7 +358,7 @@ bool cdcd_control_request(uint8_t rhport, tusb_control_request_t const * request
358358
usbd_control_status(rhport, request);
359359

360360
// Invoke callback
361-
if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, TU_BIT_TEST(request->wValue, 0), TU_BIT_TEST(request->wValue, 1));
361+
if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, tu_bit_test(request->wValue, 0), tu_bit_test(request->wValue, 1));
362362
break;
363363

364364
default: return false; // stall unsupported request

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc_device.h

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2018, hathach (tinyusb.org)
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -51,40 +51,40 @@
5151
// APPLICATION API (Multiple Interfaces)
5252
// CFG_TUD_CDC > 1
5353
//--------------------------------------------------------------------+
54-
bool tud_cdc_n_connected (uint8_t itf);
55-
uint8_t tud_cdc_n_get_line_state (uint8_t itf);
56-
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding);
57-
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);
58-
59-
uint32_t tud_cdc_n_available (uint8_t itf);
60-
char tud_cdc_n_read_char (uint8_t itf);
61-
uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
62-
void tud_cdc_n_read_flush (uint8_t itf);
63-
char tud_cdc_n_peek (uint8_t itf, int pos);
64-
65-
uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
66-
uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
67-
uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);
68-
bool tud_cdc_n_write_flush (uint8_t itf);
54+
bool tud_cdc_n_connected (uint8_t itf);
55+
uint8_t tud_cdc_n_get_line_state (uint8_t itf);
56+
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding);
57+
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted);
58+
59+
uint32_t tud_cdc_n_available (uint8_t itf);
60+
signed char tud_cdc_n_read_char (uint8_t itf);
61+
uint32_t tud_cdc_n_read (uint8_t itf, void* buffer, uint32_t bufsize);
62+
void tud_cdc_n_read_flush (uint8_t itf);
63+
signed char tud_cdc_n_peek (uint8_t itf, int pos);
64+
65+
uint32_t tud_cdc_n_write_char (uint8_t itf, char ch);
66+
uint32_t tud_cdc_n_write (uint8_t itf, void const* buffer, uint32_t bufsize);
67+
uint32_t tud_cdc_n_write_str (uint8_t itf, char const* str);
68+
bool tud_cdc_n_write_flush (uint8_t itf);
6969

7070
//--------------------------------------------------------------------+
7171
// APPLICATION API (Interface0)
7272
//--------------------------------------------------------------------+
73-
static inline bool tud_cdc_connected (void) { return tud_cdc_n_connected(0); }
74-
static inline uint8_t tud_cdc_get_line_state (void) { return tud_cdc_n_get_line_state(0); }
75-
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding) { return tud_cdc_n_get_line_coding(0, coding);}
76-
static inline void tud_cdc_set_wanted_char (char wanted) { tud_cdc_n_set_wanted_char(0, wanted); }
77-
78-
static inline uint32_t tud_cdc_available (void) { return tud_cdc_n_available(0); }
79-
static inline char tud_cdc_read_char (void) { return tud_cdc_n_read_char(0); }
80-
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize) { return tud_cdc_n_read(0, buffer, bufsize); }
81-
static inline void tud_cdc_read_flush (void) { tud_cdc_n_read_flush(0); }
82-
static inline char tud_cdc_peek (int pos) { return tud_cdc_n_peek(0, pos); }
83-
84-
static inline uint32_t tud_cdc_write_char (char ch) { return tud_cdc_n_write_char(0, ch); }
85-
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) { return tud_cdc_n_write(0, buffer, bufsize); }
86-
static inline uint32_t tud_cdc_write_str (char const* str) { return tud_cdc_n_write_str(0, str); }
87-
static inline bool tud_cdc_write_flush (void) { return tud_cdc_n_write_flush(0); }
73+
static inline bool tud_cdc_connected (void) { return tud_cdc_n_connected(0); }
74+
static inline uint8_t tud_cdc_get_line_state (void) { return tud_cdc_n_get_line_state(0); }
75+
static inline void tud_cdc_get_line_coding (cdc_line_coding_t* coding) { return tud_cdc_n_get_line_coding(0, coding);}
76+
static inline void tud_cdc_set_wanted_char (char wanted) { tud_cdc_n_set_wanted_char(0, wanted); }
77+
78+
static inline uint32_t tud_cdc_available (void) { return tud_cdc_n_available(0); }
79+
static inline signed char tud_cdc_read_char (void) { return tud_cdc_n_read_char(0); }
80+
static inline uint32_t tud_cdc_read (void* buffer, uint32_t bufsize) { return tud_cdc_n_read(0, buffer, bufsize); }
81+
static inline void tud_cdc_read_flush (void) { tud_cdc_n_read_flush(0); }
82+
static inline signed char tud_cdc_peek (int pos) { return tud_cdc_n_peek(0, pos); }
83+
84+
static inline uint32_t tud_cdc_write_char (char ch) { return tud_cdc_n_write_char(0, ch); }
85+
static inline uint32_t tud_cdc_write (void const* buffer, uint32_t bufsize) { return tud_cdc_n_write(0, buffer, bufsize); }
86+
static inline uint32_t tud_cdc_write_str (char const* str) { return tud_cdc_n_write_str(0, str); }
87+
static inline bool tud_cdc_write_flush (void) { return tud_cdc_n_write_flush(0); }
8888

8989
//--------------------------------------------------------------------+
9090
// APPLICATION CALLBACK API (WEAK is optional)

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/custom/custom_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2018, hathach (tinyusb.org)
4+
* Copyright (c) 2019 Ha Thach (tinyusb.org)
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal

0 commit comments

Comments
 (0)