Skip to content

Commit 1915d69

Browse files
authored
Merge pull request #1397 from hathach/rework-host-control-xfer
Rework host control xfer
2 parents 228e185 + 2929afe commit 1915d69

File tree

12 files changed

+812
-570
lines changed

12 files changed

+812
-570
lines changed

examples/host/bare_api/src/main.c

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,9 @@ int main(void)
6666
#define LANGUAGE_ID 0x0409
6767

6868
//uint8_t usb_buf[256] TU_ATTR_ALIGNED(4);
69+
TU_ATTR_ALIGNED(4)
6970
tusb_desc_device_t desc_device;
7071

71-
static volatile xfer_result_t _get_string_result;
72-
73-
static bool _transfer_done_cb(uint8_t daddr, tusb_control_request_t const *request, xfer_result_t result) {
74-
(void)daddr;
75-
(void)request;
76-
_get_string_result = result;
77-
return true;
78-
}
79-
8072
static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
8173
// TODO: Check for runover.
8274
(void)utf8_len;
@@ -116,23 +108,16 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
116108
return total_bytes;
117109
}
118110

119-
static void _wait_and_convert(uint16_t *temp_buf, size_t buf_len) {
120-
while (_get_string_result == 0xff) {
121-
tuh_task();
122-
}
123-
if (_get_string_result != XFER_RESULT_SUCCESS) {
124-
temp_buf[0] = 0;
125-
return;
126-
}
111+
static void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len) {
127112
size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
128113
size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len);
129114
_convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);
130115
((uint8_t*) temp_buf)[utf8_len] = '\0';
131116
}
132117

133-
bool print_device_descriptor(uint8_t daddr, tusb_control_request_t const * request, xfer_result_t result)
118+
bool print_device_descriptor(uint8_t daddr, tuh_control_xfer_t const * xfer, xfer_result_t result)
134119
{
135-
(void) request;
120+
(void) xfer;
136121

137122
if ( XFER_RESULT_SUCCESS != result )
138123
{
@@ -153,31 +138,29 @@ bool print_device_descriptor(uint8_t daddr, tusb_control_request_t const * reque
153138
printf(" idProduct 0x%04x\r\n" , desc_device.idProduct);
154139
printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
155140

156-
_get_string_result = 0xff;
141+
uint32_t timeout_ms = 10;
157142
uint16_t temp_buf[128];
158143

159144
printf(" iManufacturer %u " , desc_device.iManufacturer);
160-
temp_buf[0] = 0;
161-
if (tuh_descriptor_get_manufacturer_string(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf), _transfer_done_cb)) {
162-
_wait_and_convert(temp_buf, TU_ARRAY_SIZE(temp_buf));
145+
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf), timeout_ms) )
146+
{
147+
utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
163148
printf((const char*) temp_buf);
164149
}
165150
printf("\r\n");
166151

167152
printf(" iProduct %u " , desc_device.iProduct);
168-
_get_string_result = 0xff;
169-
temp_buf[0] = 0;
170-
if (tuh_descriptor_get_product_string(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf), _transfer_done_cb)) {
171-
_wait_and_convert(temp_buf, TU_ARRAY_SIZE(temp_buf));
153+
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf), timeout_ms))
154+
{
155+
utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
172156
printf((const char*) temp_buf);
173157
}
174158
printf("\r\n");
175159

176160
printf(" iSerialNumber %u " , desc_device.iSerialNumber);
177-
_get_string_result = 0xff;
178-
temp_buf[0] = 0;
179-
if (tuh_descriptor_get_serial_string(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf), _transfer_done_cb)) {
180-
_wait_and_convert(temp_buf, TU_ARRAY_SIZE(temp_buf));
161+
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, temp_buf, TU_ARRAY_SIZE(temp_buf), timeout_ms))
162+
{
163+
utf16_to_utf8(temp_buf, TU_ARRAY_SIZE(temp_buf));
181164
printf((const char*) temp_buf);
182165
}
183166
printf("\r\n");
@@ -192,8 +175,8 @@ void tuh_mount_cb (uint8_t daddr)
192175
{
193176
printf("Device attached, address = %d\r\n", daddr);
194177

195-
// Get Device Descriptor
196-
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor);
178+
// Get Device Descriptor using asynchronous API
179+
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
197180
}
198181

199182
/// Invoked when device is unmounted (bus reset/unplugged)

examples/host/cdc_msc_hid/src/main.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ int main(void)
7171
#if CFG_TUH_CDC
7272
CFG_TUSB_MEM_SECTION static char serial_in_buffer[64] = { 0 };
7373

74-
void tuh_mount_cb(uint8_t dev_addr)
75-
{
76-
// application set-up
77-
printf("A device with address %d is mounted\r\n", dev_addr);
78-
79-
tuh_cdc_receive(dev_addr, serial_in_buffer, sizeof(serial_in_buffer), true); // schedule first transfer
80-
}
81-
82-
void tuh_umount_cb(uint8_t dev_addr)
83-
{
84-
// application tear-down
85-
printf("A device with address %d is unmounted \r\n", dev_addr);
86-
}
87-
8874
// invoked ISR context
8975
void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes)
9076
{
@@ -109,6 +95,19 @@ void cdc_task(void)
10995
// TinyUSB Callbacks
11096
//--------------------------------------------------------------------+
11197

98+
void tuh_mount_cb(uint8_t dev_addr)
99+
{
100+
// application set-up
101+
printf("A device with address %d is mounted\r\n", dev_addr);
102+
}
103+
104+
void tuh_umount_cb(uint8_t dev_addr)
105+
{
106+
// application tear-down
107+
printf("A device with address %d is unmounted \r\n", dev_addr);
108+
}
109+
110+
112111
//--------------------------------------------------------------------+
113112
// Blinking Task
114113
//--------------------------------------------------------------------+

src/class/cdc/cdc_host.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,32 @@ bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is
120120
return usbh_edpt_xfer(dev_addr, ep_in, p_buffer, length);
121121
}
122122

123-
bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_complete_cb_t complete_cb)
123+
bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_xfer_cb_t complete_cb)
124124
{
125125
cdch_data_t const * p_cdc = get_itf(dev_addr);
126-
tusb_control_request_t const request =
126+
127+
tuh_control_xfer_t const xfer =
127128
{
128-
.bmRequestType_bit =
129+
.request =
129130
{
130-
.recipient = TUSB_REQ_RCPT_INTERFACE,
131-
.type = TUSB_REQ_TYPE_CLASS,
132-
.direction = TUSB_DIR_OUT
131+
.bmRequestType_bit =
132+
{
133+
.recipient = TUSB_REQ_RCPT_INTERFACE,
134+
.type = TUSB_REQ_TYPE_CLASS,
135+
.direction = TUSB_DIR_OUT
136+
},
137+
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
138+
.wValue = (rts ? 2 : 0) | (dtr ? 1 : 0),
139+
.wIndex = p_cdc->itf_num,
140+
.wLength = 0
133141
},
134-
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
135-
.wValue = (rts ? 2 : 0) | (dtr ? 1 : 0),
136-
.wIndex = p_cdc->itf_num,
137-
.wLength = 0
142+
143+
.buffer = NULL,
144+
.complete_cb = complete_cb,
145+
.user_arg = 0
138146
};
139147

140-
TU_ASSERT( tuh_control_xfer(dev_addr, &request, NULL, complete_cb) );
141-
return true;
148+
return tuh_control_xfer(dev_addr, &xfer);
142149
}
143150

144151
//--------------------------------------------------------------------+

src/class/cdc/cdc_host.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@
4242
* \defgroup CDC_Serial_Host Host
4343
* @{ */
4444

45-
bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_complete_cb_t complete_cb);
45+
bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_control_xfer_cb_t complete_cb);
4646

47-
static inline bool tuh_cdc_connect(uint8_t dev_addr, tuh_control_complete_cb_t complete_cb)
47+
static inline bool tuh_cdc_connect(uint8_t dev_addr, tuh_control_xfer_cb_t complete_cb)
4848
{
4949
return tuh_cdc_set_control_line_state(dev_addr, true, true, complete_cb);
5050
}
5151

52-
static inline bool tuh_cdc_disconnect(uint8_t dev_addr, tuh_control_complete_cb_t complete_cb)
52+
static inline bool tuh_cdc_disconnect(uint8_t dev_addr, tuh_control_xfer_cb_t complete_cb)
5353
{
5454
return tuh_cdc_set_control_line_state(dev_addr, false, false, complete_cb);
5555
}

0 commit comments

Comments
 (0)