예제 #1
0
/**
 * This function will create a cdc class instance.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
uclass_t rt_usbd_class_cdc_create(udevice_t device)
{
    uintf_t intf_comm, intf_data;
    ualtsetting_t comm_setting, data_setting;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);

    /* create a cdc class */
    cdc = rt_usbd_class_create(device, &dev_desc, &ops);

    /* create a cdc communication interface and a cdc data interface */
    intf_comm = rt_usbd_interface_create(device, _interface_handler);
    intf_data = rt_usbd_interface_create(device, _interface_handler);

    /* create a communication alternate setting and a data alternate setting */
    comm_setting = rt_usbd_altsetting_create(&comm_desc.intf_desc,
                   sizeof(struct ucdc_comm_descriptor));
    data_setting = rt_usbd_altsetting_create(&data_desc.intf_desc,
                   sizeof(struct ucdc_data_descriptor));

    /* configure the cdc interface descriptor */
    _cdc_descriptor_config(intf_comm->intf_num, intf_data->intf_num);

    /* create a bulk in and a bulk endpoint */
    ep_out = rt_usbd_endpoint_create(&data_desc.ep_out_desc, _ep_out_handler);
    ep_in = rt_usbd_endpoint_create(&data_desc.ep_in_desc, _ep_in_handler);

    /* add the bulk out and bulk in endpoints to the data alternate setting */
    rt_usbd_altsetting_add_endpoint(data_setting, ep_in);
    rt_usbd_altsetting_add_endpoint(data_setting, ep_out);

    /* add the data alternate setting to the data interface
            then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_data, data_setting);
    rt_usbd_set_altsetting(intf_data, 0);

    /* add the cdc data interface to cdc class */
    rt_usbd_class_add_interface(cdc, intf_data);

    /* create a command endpoint */
    ep_cmd = rt_usbd_endpoint_create(&comm_desc.ep_desc, _ep_cmd_handler);

    /* add the command endpoint to the cdc communication interface */
    rt_usbd_altsetting_add_endpoint(comm_setting, ep_cmd);

    /* add the communication alternate setting to the communication interface,
       then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
    rt_usbd_set_altsetting(intf_comm, 0);

    /* add the communication interface to the cdc class */
    rt_usbd_class_add_interface(cdc, intf_comm);

    return cdc;
}
/**
 * This function will create a cdc class instance.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
uclass_t rt_usbd_class_cdc_create(udevice_t device)
{
    uclass_t cdc;
    cdc_eps_t eps;
    uintf_t intf_comm, intf_data;
    ualtsetting_t comm_setting, data_setting;
    ucdc_data_desc_t data_desc;
    ucdc_comm_desc_t comm_desc;

    /* parameter check */
    RT_ASSERT(device != RT_NULL);
    
    /* create a cdc class */
    cdc = rt_usbd_class_create(device, &dev_desc, &ops);
    /* create a cdc class endpoints collection */
    eps = rt_malloc(sizeof(struct cdc_eps));
    cdc->eps = (void*)eps;

    /* create a cdc communication interface and a cdc data interface */
    intf_comm = rt_usbd_interface_create(device, _interface_handler);
    intf_data = rt_usbd_interface_create(device, _interface_handler);

    /* create a communication alternate setting and a data alternate setting */
    comm_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_comm_descriptor));     
    data_setting = rt_usbd_altsetting_create(sizeof(struct ucdc_data_descriptor)); 
    
    /* config desc in alternate setting */    
    rt_usbd_altsetting_config_descriptor(comm_setting, &_comm_desc,
        (rt_off_t)&((ucdc_comm_desc_t)0)->intf_desc);
    rt_usbd_altsetting_config_descriptor(data_setting, &_data_desc, 0);
    /* configure the cdc interface descriptor */
    _cdc_descriptor_config(comm_setting->desc, intf_comm->intf_num, data_setting->desc, intf_data->intf_num);

    /* create a bulk in and a bulk endpoint */
    data_desc = (ucdc_data_desc_t)data_setting->desc;
    eps->ep_out = rt_usbd_endpoint_create(&data_desc->ep_out_desc, _ep_out_handler);
    eps->ep_in = rt_usbd_endpoint_create(&data_desc->ep_in_desc, _ep_in_handler);

    /* add the bulk out and bulk in endpoints to the data alternate setting */
    rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_in);
    rt_usbd_altsetting_add_endpoint(data_setting, eps->ep_out);
    
    /* add the data alternate setting to the data interface
            then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_data, data_setting);
    rt_usbd_set_altsetting(intf_data, 0);

    /* add the cdc data interface to cdc class */
    rt_usbd_class_add_interface(cdc, intf_data);  

    /* create a command endpoint */
    comm_desc = (ucdc_comm_desc_t)comm_setting->desc;
    eps->ep_cmd = rt_usbd_endpoint_create(&comm_desc->ep_desc, _ep_cmd_handler);

    /* add the command endpoint to the cdc communication interface */
    rt_usbd_altsetting_add_endpoint(comm_setting, eps->ep_cmd);
    
    /* add the communication alternate setting to the communication interface,
       then set default setting of the interface */
    rt_usbd_interface_add_altsetting(intf_comm, comm_setting);
    rt_usbd_set_altsetting(intf_comm, 0);

    /* add the communication interface to the cdc class */
    rt_usbd_class_add_interface(cdc, intf_comm);    

    return cdc;
}