Skip to content

Commit 5654035

Browse files
committed
USBHOST: dynamically change device port if connected through hub
1 parent 6ff51cb commit 5654035

File tree

10 files changed

+155
-69
lines changed

10 files changed

+155
-69
lines changed

libraries/USBHOST/examples/Shell/Shell.ino

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ static tusbh_root_hub_t root_fs;
7474
static tusbh_root_hub_t root_hs;
7575
static const tusbh_boot_key_class_t cls_boot_key = {
7676
.backend = &tusbh_boot_keyboard_backend,
77-
.on_key = process_key
77+
//.on_key = process_key
7878
};
7979

8080
static const tusbh_boot_mouse_class_t cls_boot_mouse = {
@@ -313,17 +313,22 @@ void setup()
313313
hs = 0;
314314
#endif
315315

316+
//usb_fakeirq.start(mbed::callback(usb_irq_thread));
317+
318+
//NVIC_DisableIRQ(OTG_HS_IRQn);
319+
316320
cmd_len = 0;
317321
}
318322

319323
bool once = true;
320324

321325
void loop() {
322326
if (millis() > 5000 && once) {
323-
cmd_lsusb(NULL, 0);
327+
//cmd_lsusb(NULL, 0);
324328
once = false;
325329
}
326330
//command_loop();
331+
327332
// there is only one message q for every thing
328333
tusbh_msg_loop(mq);
329334
}
@@ -347,6 +352,7 @@ static const char tabB[] = "\t _+{}|~:\"~<>?";
347352
// route the key event to stdin
348353
static int process_key(tusbh_ep_info_t* ep, const uint8_t* keys)
349354
{
355+
printf("\n");
350356
uint8_t modify = keys[0];
351357
uint8_t key = keys[2];
352358
uint8_t last_leds = key_leds;
@@ -386,8 +392,9 @@ static int process_key(tusbh_ep_info_t* ep, const uint8_t* keys)
386392

387393

388394

395+
#if 1
389396

390-
#ifdef DEBUG
397+
extern "C" {
391398

392399
#define LOG_SIZE 1024
393400
static int hc_log_index;
@@ -410,6 +417,12 @@ void hc_log_begin(tusb_host_t* host, uint8_t hc_num)
410417
hc_info.HCTSIZ = HC->HCTSIZ;
411418
hc_info.HCDMA = HC->HCDMA;
412419
hc_log_index = 0;
420+
//printf("hc_no %x\n", hc_num);
421+
//printf("hc_info.HCCHAR %x\n", HC->HCCHAR);
422+
//printf("hc_info.HCSPLT %x\n", HC->HCSPLT);
423+
//printf("hc_info.HCINTMSK %x\n", HC->HCINTMSK);
424+
//printf("hc_info.HCTSIZ %x\n", HC->HCTSIZ);
425+
//printf("hc_info.HCDMA %x\n", HC->HCDMA);
413426
}
414427

415428
void hc_log_data(tusb_host_t* host, uint8_t hc_num, uint32_t data)
@@ -424,4 +437,5 @@ void hc_log_end(tusb_host_t* host, uint8_t hc_num)
424437
{
425438
}
426439

440+
}
427441
#endif

libraries/USBHOST/src/USBHOST.h

Whitespace-only changes.

libraries/USBHOST/src/class/host/tusbh.c

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void tusb_host_port_changed(tusb_host_t* host, uint8_t port, host_port_state_t n
6262
POST_MESSAGE(root->mq, tusbh_msg_root_disconnected, 0, host, 0);
6363
}else{
6464
root->en_mask &= ~(1<<0);
65-
POST_MESSAGE(root->mq, tusbh_msg_root_disable, 0, host, 0);
65+
POST_MESSAGE(root->mq, tusbh_msg_root_disable, 0, host, new_state);
6666
}
6767
}
6868

@@ -152,7 +152,6 @@ void tusbh_close_pipe(tusbh_device_t* dev, int pipe_num)
152152
tusb_pipe_close(&pipe);
153153
}
154154

155-
#if 1
156155
static channel_state_t tusbh_pipe_xfer_and_wait(tusbh_device_t* dev, int pipe_num, uint8_t is_data, void* data, uint16_t len, uint32_t timeout)
157156
{
158157
void* bak = dev->host->hc[pipe_num].user_data;
@@ -168,49 +167,24 @@ static channel_state_t tusbh_pipe_xfer_and_wait(tusbh_device_t* dev, int pipe_nu
168167
channel_state_t s;
169168

170169
do{
171-
tusb_host_xfer_data(dev->host, pipe_num, is_data, p, remain);
170+
tusb_host_xfer_data(dev->host, pipe_num, is_data, p, remain, dev->hub_port);
172171
tusbh_evt_wait(dev->xfer_evt.event, timeout);
173172
tusb_hc_data_t* hc = &dev->host->hc[pipe_num];
174173
s = (channel_state_t)hc->state;
175-
if (s != TUSB_CS_TRANSFER_CSPLIT) {
176-
if(s != TUSB_CS_TRANSFER_COMPLETE){
177-
res = -(int)s;
178-
goto error;
179-
}
180-
remain -= hc->count;
181-
p += hc->count;
174+
if(s != TUSB_CS_TRANSFER_COMPLETE){
175+
res = -(int)s;
176+
goto error;
182177
}
183-
}while((remain && len) || (s == TUSB_CS_TRANSFER_CSPLIT));
178+
remain -= hc->count;
179+
p += hc->count;
180+
}while((remain && len));
184181
res = p - ((uint8_t*)data);
185182
error:
186183
dev->host->hc[pipe_num].user_data = bak;
187184
r = (channel_state_t)dev->host->hc[pipe_num].state;
188185
return r;
189186
}
190187

191-
#else
192-
193-
static channel_state_t tusbh_pipe_xfer_and_wait(tusbh_device_t* dev, int pipe_num, uint8_t is_data, void* data, uint16_t len, uint32_t timeout)
194-
{
195-
void* bak = dev->host->hc[pipe_num].user_data;
196-
dev->host->hc[pipe_num].user_data = &dev->xfer_evt;
197-
198-
tusb_host_xfer_data(dev->host, pipe_num, is_data, data, len);
199-
200-
channel_state_t r = TUSB_CS_UNKNOWN_ERROR;
201-
202-
//int res =
203-
tusbh_evt_wait(dev->xfer_evt.event, timeout);
204-
205-
//if(res == 0){
206-
r = (channel_state_t)dev->host->hc[pipe_num].state;
207-
//}
208-
dev->host->hc[pipe_num].user_data = bak;
209-
210-
return r;
211-
}
212-
#endif
213-
214188
int tusbh_ep_xfer(tusbh_ep_info_t* ep, void* data, uint16_t len, uint32_t timeout)
215189
{
216190
return tusbh_ep_xfer_with_event(ep, data, len, &ep_device(ep)->xfer_evt, timeout);
@@ -223,15 +197,17 @@ static int tusbh_ep_in_xfer_with_event(tusbh_ep_info_t* ep, void* data, uint16_t
223197
uint16_t remain = len;
224198
uint8_t* p = (uint8_t*)data;
225199
int res = 0;
200+
201+
channel_state_t s;
226202
do{
227203
uint16_t xfer_len = EP_MPS(ep->desc);
228204
if(xfer_len > remain){
229205
xfer_len = remain;
230206
}
231-
tusb_host_xfer_data(ep_host(ep), ep->pipe_num, 1, p, xfer_len);
207+
tusb_host_xfer_data(ep_host(ep), ep->pipe_num, 1, p, xfer_len, ep_device(ep)->hub_port);
232208
tusbh_evt_wait(action->event, timeout);
233209
tusb_hc_data_t* hc = &ep_device(ep)->host->hc[ep->pipe_num];
234-
channel_state_t s = (channel_state_t)hc->state;
210+
s = (channel_state_t)hc->state;
235211
if(s != TUSB_CS_TRANSFER_COMPLETE){
236212
res = -(int)s;
237213
goto error;
@@ -273,7 +249,7 @@ static int tusbh_ep_out_xfer_with_event(tusbh_ep_info_t* ep, void* data, uint16_
273249
}else{
274250
xfer_len = remain;
275251
}
276-
tusb_host_xfer_data(ep_host(ep), ep->pipe_num, 1, p, xfer_len);
252+
tusb_host_xfer_data(ep_host(ep), ep->pipe_num, 1, p, xfer_len, ep_device(ep)->hub_port);
277253
tusbh_evt_wait(action->event, timeout);
278254
tusb_hc_data_t* hc = &ep_device(ep)->host->hc[ep->pipe_num];
279255
channel_state_t s = (channel_state_t)hc->state;
@@ -876,7 +852,7 @@ static void tusbh_msg_root_disable(tusbh_message_t* msg)
876852
tusbh_root_hub_t* root = (tusbh_root_hub_t*)host->user_data;
877853
(void)port;
878854
(void)root;
879-
TUSB_ROOT_INFO("Disable\n");
855+
TUSB_ROOT_INFO("Disable %d\n", msg->len);
880856
}
881857

882858
static void tusbh_ep_data_xfered(tusbh_message_t* msg)
@@ -949,7 +925,7 @@ static void start_period_in(tusbh_device_t* dev, tusbh_ep_info_t* ep)
949925
if(ep->pipe_num >= 0){
950926
uint16_t mps = EP_MPS(ep->desc);
951927
ep->xfer_in_progress = 1;
952-
tusb_host_xfer_data(dev->host, ep->pipe_num, 1, ep->data, mps);
928+
tusb_host_xfer_data(dev->host, ep->pipe_num, 1, ep->data, mps, dev->hub_port);
953929
}
954930
}
955931

libraries/USBHOST/src/class/host/tusbh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ struct _tusbh_device{
172172
uint8_t speed; /**< device speed */
173173
uint8_t config; /**< device current configuartion */
174174
uint8_t interface_num; /**< Interface number, interfaces under IAD treat as 1 interface */
175+
uint8_t hub_port; /**< If connected through a hub */
175176
};
176177

177178
#define dev_root(dev) ((tusbh_root_hub_t*)(dev->host->user_data))

libraries/USBHOST/src/class/host/tusbh_hid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ int tusbh_hid_send_data(tusbh_ep_info_t* ep, void* data, uint32_t len)
324324
if( pipe_num < 0 ){
325325
return pipe_num;
326326
}
327-
tusb_host_xfer_data(ep_host(ep), pipe_num, 1, data, len);
327+
tusb_host_xfer_data(ep_host(ep), pipe_num, 1, data, len, ep_device(ep)->hub_port);
328328
return 0;
329329
}
330330

libraries/USBHOST/src/class/host/tusbh_hub.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ static int hub_data_xfered(tusbh_ep_info_t* ep)
298298
goto error;
299299
}
300300
TUSB_HUB_INFO("HUB port state = %02x\n", port_state);
301-
for(;port_state; port_state>>=1, port++){
301+
for(;port_state; port_state>>=1, port++) {
302302
if( (port_state & 1) == 0 ) continue;
303303
usb_hub_port_status_t status;
304304
TUSB_HUB_INFO("HUB get port %d status\n", port);
@@ -337,6 +337,7 @@ static int hub_data_xfered(tusbh_ep_info_t* ep)
337337
child->speed = PORT_SPEED_FULL;
338338
if(status.wPortStatus.PORT_HIGH_SPEED){
339339
child->speed = PORT_SPEED_HIGH;
340+
child->hub_port = 0;
340341
}
341342
if(status.wPortStatus.PORT_LOW_SPEED){
342343
child->speed = PORT_SPEED_LOW;
@@ -369,6 +370,7 @@ static int hub_data_xfered(tusbh_ep_info_t* ep)
369370
dev->children[port-1] = 0;
370371
}
371372
child = tusbh_new_device();
373+
child->hub_port = port;
372374
dev->children[port-1] = child;
373375
tusbh_set_hub_port_feature(dev, port, HUB_FEATURE_SEL_PORT_RESET);
374376
}

libraries/USBHOST/src/class/host/tusbh_vendor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ int tusbh_vendor_xfer_data(tusbh_ep_info_t* ep, void* data, uint32_t len)
117117
if( pipe_num < 0 ){
118118
return pipe_num;
119119
}
120-
tusb_host_xfer_data(ep_host(ep), pipe_num, 1, data, len);
120+
tusb_host_xfer_data(ep_host(ep), pipe_num, 1, data, len, ep_device(ep)->hub_port);
121121
return 0;
122122
}
123123

libraries/USBHOST/src/stm32_otg_init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ static void tusb_otg_core_init(tusb_core_t* core)
386386

387387
NVIC_SetPriority(OTG_HS_IRQn, 0);
388388
NVIC_EnableIRQ(OTG_HS_IRQn);
389+
//NVIC_DisableIRQ(OTG_HS_IRQn);
389390

390391
NVIC_SetVector(OTG_HS_IRQn, (uint32_t)&OTG_HS_IRQHandler);
391392

libraries/USBHOST/src/teeny_usb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ int tusb_pipe_cancel(tusb_pipe_t* pipe);
606606
* \param[in] data data buffer for read/write
607607
* \param[in] len data length / data buffer size
608608
*/
609-
uint32_t tusb_otg_host_xfer_data(tusb_host_t* host, uint8_t hc_num, uint8_t is_data, uint8_t* data, uint32_t len);
609+
uint32_t tusb_otg_host_xfer_data(tusb_host_t* host, uint8_t hc_num, uint8_t is_data, uint8_t* data, uint32_t len, uint8_t port);
610610
#define tusb_host_xfer_data tusb_otg_host_xfer_data
611611

612612
/** Send a setup packet

0 commit comments

Comments
 (0)