Skip to content

Commit 15ced09

Browse files
committed
add tuh_configuration_set()
1 parent 2bdf4d8 commit 15ced09

File tree

2 files changed

+86
-82
lines changed

2 files changed

+86
-82
lines changed

src/host/usbh.c

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,26 @@ bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t* vid, uint16_t* pid)
257257
return true;
258258
}
259259

260+
tusb_speed_t tuh_speed_get (uint8_t dev_addr)
261+
{
262+
return (tusb_speed_t) (dev_addr ? get_device(dev_addr)->speed : _dev0.speed);
263+
}
260264

261-
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index,
262-
void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb)
265+
#if CFG_TUSB_OS == OPT_OS_NONE
266+
void osal_task_delay(uint32_t msec)
267+
{
268+
(void) msec;
269+
270+
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
271+
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
272+
}
273+
#endif
274+
275+
//--------------------------------------------------------------------+
276+
// Descriptors
277+
//--------------------------------------------------------------------+
278+
279+
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb)
263280
{
264281
tusb_control_request_t const request =
265282
{
@@ -272,7 +289,7 @@ bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index,
272289
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
273290
.wValue = tu_htole16( TU_U16(type, index) ),
274291
.wIndex = 0,
275-
.wLength = len
292+
.wLength = tu_htole16(len)
276293
};
277294

278295
TU_ASSERT( tuh_control_xfer(daddr, &request, buffer, complete_cb) );
@@ -290,6 +307,27 @@ bool tuh_descriptor_configuration_get(uint8_t daddr, uint8_t index, void* buffer
290307
return tuh_descriptor_get(daddr, TUSB_DESC_CONFIGURATION, index, buffer, len, complete_cb);
291308
}
292309

310+
bool tuh_descriptor_string_get(uint8_t daddr, uint16_t language_id, uint8_t index,
311+
void* buf, uint16_t len, tuh_control_complete_cb_t complete_cb)
312+
{
313+
tusb_control_request_t const request =
314+
{
315+
.bmRequestType_bit =
316+
{
317+
.recipient = TUSB_REQ_RCPT_DEVICE,
318+
.type = TUSB_REQ_TYPE_STANDARD,
319+
.direction = TUSB_DIR_IN
320+
},
321+
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
322+
.wValue = tu_htole16( TU_U16(TUSB_DESC_STRING, index) ),
323+
.wIndex = tu_htole16(language_id),
324+
.wLength = tu_htole16(len)
325+
};
326+
327+
TU_ASSERT( tuh_control_xfer(daddr, &request, buf, complete_cb) );
328+
return true;
329+
}
330+
293331
uint8_t tuh_i_manufacturer_get(uint8_t dev_addr) {
294332
TU_VERIFY(tuh_mounted(dev_addr));
295333
usbh_device_t const* dev = get_device(dev_addr);
@@ -311,54 +349,26 @@ uint8_t tuh_i_product_get(uint8_t dev_addr) {
311349
return dev->i_product;
312350
}
313351

314-
static tuh_complete_cb_t string_get_cb;
315-
316-
static bool string_get_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) {
317-
(void) dev_addr;
318-
(void) request;
319-
if (string_get_cb != NULL) {
320-
string_get_cb(result);
321-
}
322-
string_get_cb = NULL;
323-
return true;
324-
}
325-
326-
bool tuh_string_get(uint8_t dev_addr, uint8_t string_index, uint16_t* buf, size_t len, tuh_complete_cb_t complete_cb) {
327-
if (string_get_cb != NULL) {
328-
return false;
329-
}
352+
bool tuh_configuration_set(uint8_t daddr, uint8_t config_num, tuh_control_complete_cb_t complete_cb)
353+
{
354+
TU_LOG2("Set Configuration = %d\r\n", config_num);
330355
tusb_control_request_t const request =
331356
{
332357
.bmRequestType_bit =
333358
{
334359
.recipient = TUSB_REQ_RCPT_DEVICE,
335360
.type = TUSB_REQ_TYPE_STANDARD,
336-
.direction = TUSB_DIR_IN
361+
.direction = TUSB_DIR_OUT
337362
},
338-
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
339-
.wValue = TUSB_DESC_STRING << 8 | string_index,
363+
.bRequest = TUSB_REQ_SET_CONFIGURATION,
364+
.wValue = tu_htole16(config_num),
340365
.wIndex = 0,
341-
.wLength = len * sizeof(uint16_t)
366+
.wLength = 0
342367
};
343-
string_get_cb = complete_cb;
344-
TU_ASSERT( tuh_control_xfer(dev_addr, &request, buf, string_get_complete) );
345-
return true;
346-
}
347-
348-
tusb_speed_t tuh_speed_get (uint8_t dev_addr)
349-
{
350-
return (tusb_speed_t) (dev_addr ? get_device(dev_addr)->speed : _dev0.speed);
351-
}
352-
353-
#if CFG_TUSB_OS == OPT_OS_NONE
354-
void osal_task_delay(uint32_t msec)
355-
{
356-
(void) msec;
357368

358-
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
359-
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
369+
TU_ASSERT( tuh_control_xfer(daddr, &request, NULL, complete_cb) );
370+
return true;
360371
}
361-
#endif
362372

363373
//--------------------------------------------------------------------+
364374
// CLASS-USBD API (don't require to verify parameters)
@@ -903,7 +913,7 @@ static bool enum_request_set_addr(void)
903913
.direction = TUSB_DIR_OUT
904914
},
905915
.bRequest = TUSB_REQ_SET_ADDRESS,
906-
.wValue = new_addr,
916+
.wValue = tu_htole16(new_addr),
907917
.wIndex = 0,
908918
.wLength = 0
909919
};
@@ -954,8 +964,9 @@ static bool enum_get_device_desc_complete(uint8_t dev_addr, tusb_control_request
954964
// if (tuh_attach_cb) tuh_attach_cb((tusb_desc_device_t*) _usbh_ctrl_buf);
955965

956966
// Get 9-byte for total length
967+
uint8_t const config_idx = CONFIG_NUM - 1;
957968
TU_LOG2("Get Configuration[0] Descriptor (9 bytes)\r\n");
958-
TU_ASSERT( tuh_descriptor_configuration_get(dev_addr, 0, _usbh_ctrl_buf, 9, enum_get_9byte_config_desc_complete) );
969+
TU_ASSERT( tuh_descriptor_configuration_get(dev_addr, config_idx, _usbh_ctrl_buf, 9, enum_get_9byte_config_desc_complete) );
959970
return true;
960971
}
961972

@@ -973,8 +984,9 @@ static bool enum_get_9byte_config_desc_complete(uint8_t dev_addr, tusb_control_r
973984
TU_ASSERT(total_len <= CFG_TUH_ENUMERATION_BUFSIZE);
974985

975986
// Get full configuration descriptor
987+
uint8_t const config_idx = CONFIG_NUM - 1;
976988
TU_LOG2("Get Configuration[0] Descriptor\r\n");
977-
TU_ASSERT( tuh_descriptor_configuration_get(dev_addr, 0, _usbh_ctrl_buf, total_len, enum_get_config_desc_complete) );
989+
TU_ASSERT( tuh_descriptor_configuration_get(dev_addr, config_idx, _usbh_ctrl_buf, total_len, enum_get_config_desc_complete) );
978990
return true;
979991
}
980992

@@ -987,23 +999,7 @@ static bool enum_get_config_desc_complete(uint8_t dev_addr, tusb_control_request
987999
// Driver open aren't allowed to make any usb transfer yet
9881000
TU_ASSERT( parse_configuration_descriptor(dev_addr, (tusb_desc_configuration_t*) _usbh_ctrl_buf) );
9891001

990-
TU_LOG2("Set Configuration = %d\r\n", CONFIG_NUM);
991-
tusb_control_request_t const new_request =
992-
{
993-
.bmRequestType_bit =
994-
{
995-
.recipient = TUSB_REQ_RCPT_DEVICE,
996-
.type = TUSB_REQ_TYPE_STANDARD,
997-
.direction = TUSB_DIR_OUT
998-
},
999-
.bRequest = TUSB_REQ_SET_CONFIGURATION,
1000-
.wValue = CONFIG_NUM,
1001-
.wIndex = 0,
1002-
.wLength = 0
1003-
};
1004-
1005-
TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, enum_set_config_complete) );
1006-
1002+
TU_ASSERT( tuh_configuration_set(dev_addr, CONFIG_NUM, enum_set_config_complete) );
10071003
return true;
10081004
}
10091005

src/host/usbh.h

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,15 @@ void tuh_task(void);
5858
extern void hcd_int_handler(uint8_t rhport);
5959
#define tuh_int_handler hcd_int_handler
6060

61-
//------------- descriptors -------------//
62-
63-
// Get an descriptor
64-
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index,
65-
void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
66-
67-
// Get device descriptor
68-
bool tuh_descriptor_device_get(uint8_t daddr, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
69-
70-
// Get configuration descriptor
71-
bool tuh_descriptor_configuration_get(uint8_t daddr, uint8_t index, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
72-
7361
bool tuh_vid_pid_get(uint8_t daddr, uint16_t* vid, uint16_t* pid);
7462

75-
// Gets the string indices for common device descriptor data.
76-
uint8_t tuh_i_manufacturer_get(uint8_t daddr);
77-
uint8_t tuh_i_serial_get(uint8_t daddr);
78-
uint8_t tuh_i_product_get(uint8_t daddr);
79-
80-
// Reads the string descriptor at the string index into the buffer. This is the
81-
// full response so the first entry is the length and the constant 0x03 for
82-
// string descriptor type.
83-
bool tuh_string_get(uint8_t daddr, uint8_t string_index, uint16_t* buf, size_t len, tuh_complete_cb_t complete_cb);
84-
8563
tusb_speed_t tuh_speed_get(uint8_t daddr);
8664

8765
// Check if device is connected and configured
8866
bool tuh_mounted(uint8_t daddr);
8967

9068
// Check if device is suspended
69+
TU_ATTR_ALWAYS_INLINE
9170
static inline bool tuh_suspended(uint8_t daddr)
9271
{
9372
// TODO implement suspend & resume on host
@@ -105,6 +84,35 @@ static inline bool tuh_ready(uint8_t daddr)
10584
// Carry out control transfer
10685
bool tuh_control_xfer (uint8_t daddr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb);
10786

87+
// Set Configuration
88+
// config_num = 0 will un-configure device. Note: config_num = config_descriptor_index + 1
89+
bool tuh_configuration_set(uint8_t daddr, uint8_t config_num, tuh_control_complete_cb_t complete_cb);
90+
91+
//------------- descriptors -------------//
92+
93+
// Get an descriptor
94+
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index,
95+
void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
96+
97+
// Get device descriptor
98+
bool tuh_descriptor_device_get(uint8_t daddr, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
99+
100+
// Get configuration descriptor
101+
bool tuh_descriptor_configuration_get(uint8_t daddr, uint8_t index, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
102+
103+
// Get string descriptor
104+
// Reads the string descriptor at the string index into the buffer. This is the
105+
// full response so the first entry is the length and the constant 0x03 for
106+
// string descriptor type.
107+
bool tuh_descriptor_string_get(uint8_t daddr, uint16_t language_id, uint8_t index,
108+
void* buf, uint16_t len, tuh_control_complete_cb_t complete_cb);
109+
110+
111+
// Gets the string indices for common device descriptor data.
112+
uint8_t tuh_i_manufacturer_get(uint8_t daddr);
113+
uint8_t tuh_i_serial_get(uint8_t daddr);
114+
uint8_t tuh_i_product_get(uint8_t daddr);
115+
108116
//--------------------------------------------------------------------+
109117
// APPLICATION CALLBACK
110118
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)