From fa5c8e15021e06ec9114e51da17b6aa8698b3ea9 Mon Sep 17 00:00:00 2001 From: LeeChunHei Date: Sat, 23 Jan 2021 12:32:47 +0800 Subject: [PATCH] modifly to use multi usb(not tested) --- bsp/imxrt/libraries/drivers/drv_usbh.c | 2 +- components/drivers/include/drivers/usb_host.h | 7 +++-- components/drivers/usb/usbhost/core/driver.c | 15 +++++++--- components/drivers/usb/usbhost/core/hub.c | 29 ++++++++++--------- components/drivers/usb/usbhost/core/usbhost.c | 6 ++-- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/bsp/imxrt/libraries/drivers/drv_usbh.c b/bsp/imxrt/libraries/drivers/drv_usbh.c index 150d87d6e0..7362847eff 100644 --- a/bsp/imxrt/libraries/drivers/drv_usbh.c +++ b/bsp/imxrt/libraries/drivers/drv_usbh.c @@ -590,7 +590,7 @@ int imxrt_usbh_register(void) return -RT_ERROR; } - rt_usb_host_init(); + rt_usb_host_init(usb_host_obj->name); #endif #ifdef BSP_USB1_HOST usb_host_obj = &(imxrt_usb_host_obj[USBH1_INDEX]); diff --git a/components/drivers/include/drivers/usb_host.h b/components/drivers/include/drivers/usb_host.h index 2a68f4edb5..bd73c7d1d7 100644 --- a/components/drivers/include/drivers/usb_host.h +++ b/components/drivers/include/drivers/usb_host.h @@ -136,7 +136,8 @@ struct uhcd struct rt_device parent; uhcd_ops_t ops; rt_uint8_t num_ports; - uhub_t roothub; + uhub_t roothub; + struct rt_messagequeue *usb_mq; }; typedef struct uhcd* uhcd_t; @@ -163,7 +164,7 @@ struct uhost_msg typedef struct uhost_msg* uhost_msg_t; /* usb host system interface */ -rt_err_t rt_usb_host_init(void); +rt_err_t rt_usb_host_init(const char *name); void rt_usbh_hub_init(struct uhcd *hcd); /* usb host core interface */ @@ -203,7 +204,7 @@ rt_err_t rt_usbh_hub_clear_port_feature(uhub_t uhub, rt_uint16_t port, rt_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port, rt_uint16_t feature); rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port); -rt_err_t rt_usbh_event_signal(struct uhost_msg* msg); +rt_err_t rt_usbh_event_signal(uhcd_t uhcd, struct uhost_msg* msg); void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS); diff --git a/components/drivers/usb/usbhost/core/driver.c b/components/drivers/usb/usbhost/core/driver.c index 37327108c1..871a7b578a 100644 --- a/components/drivers/usb/usbhost/core/driver.c +++ b/components/drivers/usb/usbhost/core/driver.c @@ -13,6 +13,7 @@ #include static rt_list_t _driver_list; +static rt_bool_t _driver_list_created = RT_FALSE; /** * This function will initilize the usb class driver related data structure, @@ -22,8 +23,11 @@ static rt_list_t _driver_list; */ rt_err_t rt_usbh_class_driver_init(void) { - rt_list_init(&_driver_list); - + if (_driver_list_created == RT_FALSE) + { + rt_list_init(&_driver_list); + _driver_list_created = RT_TRUE; + } return RT_EOK; } @@ -39,8 +43,11 @@ rt_err_t rt_usbh_class_driver_register(ucd_t drv) { if (drv == RT_NULL) return -RT_ERROR; - /* insert class driver into driver list */ - rt_list_insert_after(&_driver_list, &(drv->list)); + if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code) == RT_NULL) + { + /* insert class driver into driver list */ + rt_list_insert_after(&_driver_list, &(drv->list)); + } return RT_EOK; } diff --git a/components/drivers/usb/usbhost/core/hub.c b/components/drivers/usb/usbhost/core/hub.c index eefa571472..2a9fc20111 100644 --- a/components/drivers/usb/usbhost/core/hub.c +++ b/components/drivers/usb/usbhost/core/hub.c @@ -13,9 +13,9 @@ #define USB_THREAD_STACK_SIZE 4096 -static struct rt_messagequeue *usb_mq; +// static struct rt_messagequeue *usb_mq; static struct uclass_driver hub_driver; -static struct uhub root_hub; +// static struct uhub root_hub; static rt_err_t root_hub_ctrl(struct uhcd *hcd, rt_uint16_t port, rt_uint8_t cmd, void *args) { @@ -92,7 +92,7 @@ void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool { hcd->roothub->port_status[port - 1] |= PORT_LSDA; } - rt_usbh_event_signal(&msg); + rt_usbh_event_signal(hcd, &msg); } void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port) @@ -102,7 +102,7 @@ void rt_usbh_root_hub_disconnect_handler(struct uhcd *hcd, rt_uint8_t port) msg.content.hub = hcd->roothub; hcd->roothub->port_status[port - 1] |= PORT_CCSC; hcd->roothub->port_status[port - 1] &= ~PORT_CCS; - rt_usbh_event_signal(&msg); + rt_usbh_event_signal(hcd, &msg); } /** @@ -647,12 +647,13 @@ ucd_t rt_usbh_class_driver_hub(void) */ static void rt_usbh_hub_thread_entry(void* parameter) { + uhcd_t hcd = (uhcd_t)parameter; while(1) { struct uhost_msg msg; /* receive message */ - if(rt_mq_recv(usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER) + if(rt_mq_recv(hcd->usb_mq, &msg, sizeof(struct uhost_msg), RT_WAITING_FOREVER) != RT_EOK ) continue; //RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type)); @@ -679,12 +680,12 @@ static void rt_usbh_hub_thread_entry(void* parameter) * * @return the error code, RT_EOK on successfully. */ -rt_err_t rt_usbh_event_signal(struct uhost_msg* msg) +rt_err_t rt_usbh_event_signal(uhcd_t hcd, struct uhost_msg* msg) { RT_ASSERT(msg != RT_NULL); /* send message to usb message queue */ - rt_mq_send(usb_mq, (void*)msg, sizeof(struct uhost_msg)); + rt_mq_send(hcd->usb_mq, (void*)msg, sizeof(struct uhost_msg)); return RT_EOK; } @@ -698,16 +699,16 @@ rt_err_t rt_usbh_event_signal(struct uhost_msg* msg) void rt_usbh_hub_init(uhcd_t hcd) { rt_thread_t thread; - /* link root hub to hcd */ - root_hub.is_roothub = RT_TRUE; - hcd->roothub = &root_hub; - root_hub.hcd = hcd; - root_hub.num_ports = hcd->num_ports; + /* create root hub for hcd */ + hcd->roothub = rt_malloc(sizeof(struct uhub)); + hcd->roothub->is_roothub = RT_TRUE; + hcd->roothub->hcd = hcd; + hcd->roothub->num_ports = hcd->num_ports; /* create usb message queue */ - usb_mq = rt_mq_create("usbh", 32, 16, RT_IPC_FLAG_FIFO); + hcd->usb_mq = rt_mq_create(hcd->parent.parent.name, 32, 16, RT_IPC_FLAG_FIFO); /* create usb hub thread */ - thread = rt_thread_create("usbh", rt_usbh_hub_thread_entry, RT_NULL, + thread = rt_thread_create(hcd->parent.parent.name, rt_usbh_hub_thread_entry, hcd, USB_THREAD_STACK_SIZE, 8, 20); if(thread != RT_NULL) { diff --git a/components/drivers/usb/usbhost/core/usbhost.c b/components/drivers/usb/usbhost/core/usbhost.c index f0294238c4..b4caad710c 100644 --- a/components/drivers/usb/usbhost/core/usbhost.c +++ b/components/drivers/usb/usbhost/core/usbhost.c @@ -22,15 +22,15 @@ * * @return none. */ -rt_err_t rt_usb_host_init(void) +rt_err_t rt_usb_host_init(const char *name) { ucd_t drv; rt_device_t uhc; - uhc = rt_device_find(USB_HOST_CONTROLLER_NAME); + uhc = rt_device_find(name); if(uhc == RT_NULL) { - rt_kprintf("can't find usb host controller %s\n", USB_HOST_CONTROLLER_NAME); + rt_kprintf("can't find usb host controller %s\n", name); return -RT_ERROR; }