@@ -66,17 +66,9 @@ int main(void)
66
66
#define LANGUAGE_ID 0x0409
67
67
68
68
//uint8_t usb_buf[256] TU_ATTR_ALIGNED(4);
69
+ TU_ATTR_ALIGNED (4 )
69
70
tusb_desc_device_t desc_device ;
70
71
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
-
80
72
static void _convert_utf16le_to_utf8 (const uint16_t * utf16 , size_t utf16_len , uint8_t * utf8 , size_t utf8_len ) {
81
73
// TODO: Check for runover.
82
74
(void )utf8_len ;
@@ -116,23 +108,16 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
116
108
return total_bytes ;
117
109
}
118
110
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 ) {
127
112
size_t utf16_len = ((temp_buf [0 ] & 0xff ) - 2 ) / sizeof (uint16_t );
128
113
size_t utf8_len = _count_utf8_bytes (temp_buf + 1 , utf16_len );
129
114
_convert_utf16le_to_utf8 (temp_buf + 1 , utf16_len , (uint8_t * ) temp_buf , sizeof (uint16_t ) * buf_len );
130
115
((uint8_t * ) temp_buf )[utf8_len ] = '\0' ;
131
116
}
132
117
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 )
134
119
{
135
- (void ) request ;
120
+ (void ) xfer ;
136
121
137
122
if ( XFER_RESULT_SUCCESS != result )
138
123
{
@@ -153,31 +138,29 @@ bool print_device_descriptor(uint8_t daddr, tusb_control_request_t const * reque
153
138
printf (" idProduct 0x%04x\r\n" , desc_device .idProduct );
154
139
printf (" bcdDevice %04x\r\n" , desc_device .bcdDevice );
155
140
156
- _get_string_result = 0xff ;
141
+ uint32_t timeout_ms = 10 ;
157
142
uint16_t temp_buf [128 ];
158
143
159
144
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 ));
163
148
printf ((const char * ) temp_buf );
164
149
}
165
150
printf ("\r\n" );
166
151
167
152
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 ));
172
156
printf ((const char * ) temp_buf );
173
157
}
174
158
printf ("\r\n" );
175
159
176
160
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 ));
181
164
printf ((const char * ) temp_buf );
182
165
}
183
166
printf ("\r\n" );
@@ -192,8 +175,8 @@ void tuh_mount_cb (uint8_t daddr)
192
175
{
193
176
printf ("Device attached, address = %d\r\n" , daddr );
194
177
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 );
197
180
}
198
181
199
182
/// Invoked when device is unmounted (bus reset/unplugged)
0 commit comments