modifly to use multi usb(not tested)

This commit is contained in:
LeeChunHei 2021-01-23 12:32:47 +08:00 committed by LeeChunHei
parent 75a4efc406
commit fa5c8e1502
5 changed files with 34 additions and 25 deletions

View File

@ -590,7 +590,7 @@ int imxrt_usbh_register(void)
return -RT_ERROR; return -RT_ERROR;
} }
rt_usb_host_init(); rt_usb_host_init(usb_host_obj->name);
#endif #endif
#ifdef BSP_USB1_HOST #ifdef BSP_USB1_HOST
usb_host_obj = &(imxrt_usb_host_obj[USBH1_INDEX]); usb_host_obj = &(imxrt_usb_host_obj[USBH1_INDEX]);

View File

@ -137,6 +137,7 @@ struct uhcd
uhcd_ops_t ops; uhcd_ops_t ops;
rt_uint8_t num_ports; rt_uint8_t num_ports;
uhub_t roothub; uhub_t roothub;
struct rt_messagequeue *usb_mq;
}; };
typedef struct uhcd* uhcd_t; typedef struct uhcd* uhcd_t;
@ -163,7 +164,7 @@ struct uhost_msg
typedef struct uhost_msg* uhost_msg_t; typedef struct uhost_msg* uhost_msg_t;
/* usb host system interface */ /* 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); void rt_usbh_hub_init(struct uhcd *hcd);
/* usb host core interface */ /* 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_err_t rt_usbh_hub_set_port_feature(uhub_t uhub, rt_uint16_t port,
rt_uint16_t feature); rt_uint16_t feature);
rt_err_t rt_usbh_hub_reset_port(uhub_t uhub, rt_uint16_t port); 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); void rt_usbh_root_hub_connect_handler(struct uhcd *hcd, rt_uint8_t port, rt_bool_t isHS);

View File

@ -13,6 +13,7 @@
#include <drivers/usb_host.h> #include <drivers/usb_host.h>
static rt_list_t _driver_list; 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, * 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_err_t rt_usbh_class_driver_init(void)
{ {
if (_driver_list_created == RT_FALSE)
{
rt_list_init(&_driver_list); rt_list_init(&_driver_list);
_driver_list_created = RT_TRUE;
}
return RT_EOK; 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; if (drv == RT_NULL) return -RT_ERROR;
if (rt_usbh_class_driver_find(drv->class_code, drv->subclass_code) == RT_NULL)
{
/* insert class driver into driver list */ /* insert class driver into driver list */
rt_list_insert_after(&_driver_list, &(drv->list)); rt_list_insert_after(&_driver_list, &(drv->list));
}
return RT_EOK; return RT_EOK;
} }

View File

@ -13,9 +13,9 @@
#define USB_THREAD_STACK_SIZE 4096 #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 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) 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; 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) 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; msg.content.hub = hcd->roothub;
hcd->roothub->port_status[port - 1] |= PORT_CCSC; hcd->roothub->port_status[port - 1] |= PORT_CCSC;
hcd->roothub->port_status[port - 1] &= ~PORT_CCS; 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) static void rt_usbh_hub_thread_entry(void* parameter)
{ {
uhcd_t hcd = (uhcd_t)parameter;
while(1) while(1)
{ {
struct uhost_msg msg; struct uhost_msg msg;
/* receive message */ /* 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_EOK ) continue;
//RT_DEBUG_LOG(RT_DEBUG_USB, ("msg type %d\n", msg.type)); //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. * @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); RT_ASSERT(msg != RT_NULL);
/* send message to usb message queue */ /* 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; 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) void rt_usbh_hub_init(uhcd_t hcd)
{ {
rt_thread_t thread; rt_thread_t thread;
/* link root hub to hcd */ /* create root hub for hcd */
root_hub.is_roothub = RT_TRUE; hcd->roothub = rt_malloc(sizeof(struct uhub));
hcd->roothub = &root_hub; hcd->roothub->is_roothub = RT_TRUE;
root_hub.hcd = hcd; hcd->roothub->hcd = hcd;
root_hub.num_ports = hcd->num_ports; hcd->roothub->num_ports = hcd->num_ports;
/* create usb message queue */ /* 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 */ /* 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); USB_THREAD_STACK_SIZE, 8, 20);
if(thread != RT_NULL) if(thread != RT_NULL)
{ {

View File

@ -22,15 +22,15 @@
* *
* @return none. * @return none.
*/ */
rt_err_t rt_usb_host_init(void) rt_err_t rt_usb_host_init(const char *name)
{ {
ucd_t drv; ucd_t drv;
rt_device_t uhc; rt_device_t uhc;
uhc = rt_device_find(USB_HOST_CONTROLLER_NAME); uhc = rt_device_find(name);
if(uhc == RT_NULL) 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; return -RT_ERROR;
} }