/**
 * This function will handle system sof event.
 *
 * @param device the usb device object.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _class_sof_handler(udevice_t device, uclass_t cls)
{
    rt_uint32_t level;
    rt_size_t size;
    static rt_uint32_t frame_count = 0;
    cdc_eps_t eps;

    if(vcom_connected != RT_TRUE) return -RT_ERROR;
    
    eps = (cdc_eps_t)cls->eps;
    if (frame_count ++ == 5)
    {
        rt_size_t mps = eps->ep_in->ep_desc->wMaxPacketSize;

        /* reset the frame counter */
        frame_count = 0;

        size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
        if(size == 0) return -RT_EFULL;

        size = size > mps ? mps : size;
        
        level = rt_hw_interrupt_disable();
        rt_ringbuffer_get(&tx_ringbuffer, eps->ep_in->buffer, size);
        rt_hw_interrupt_enable(level);                     
        
        /* send data to host */
        dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer, size);
    }

    return RT_EOK;
}
Exemplo n.º 2
0
/**
 * This function will handle get_string_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful, -RT_ERROR on invalid request.
 */
static rt_err_t _get_string_descriptor(struct udevice* device, ureq_t setup)
{
    struct ustring_descriptor str_desc;
    rt_uint8_t index, i;
    rt_uint32_t len;

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

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_string_descriptor\n"));

    str_desc.type = USB_DESC_TYPE_STRING;
    index = setup->value & 0xFF;

    if (index > USB_STRING_INTERFACE_INDEX)
    {
        rt_kprintf("unknown string index\n");
        dcd_ep_stall(device->dcd, 0);
        return -RT_ERROR;
    }
    if (index == 0)
    {
        str_desc.bLength = 4;
        str_desc.String[0] = 0x09;
        str_desc.String[1] = 0x04;
    }
    else
    {
        len = rt_strlen(device->str[index]);
        str_desc.bLength = len*2 + 2;

        for (i=0; i<len; i++)
        {
            str_desc.String[i*2] = device->str[index][i];
            str_desc.String[i*2 + 1] = 0;
        }
    }

    if (setup->length > str_desc.bLength)
        len = str_desc.bLength;
    else
        len = setup->length;

    /* send string descriptor to endpoint 0 */
    dcd_ep_write(device->dcd, 0, (rt_uint8_t*)&str_desc, len);

    return RT_EOK;
}
Exemplo n.º 3
0
/**
 * This function will handle cdc_get_line_coding request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _cdc_get_line_coding(udevice_t device, ureq_t setup)
{
    struct ucdc_line_coding data;
    rt_uint16_t size;

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(setup != RT_NULL);

    data.dwDTERate = 115200;
    data.bCharFormat = 0;
    data.bDataBits = 8;
    data.bParityType = 0;
    size = setup->length > 7 ? 7 : setup->length;
    dcd_ep_write(device->dcd, 0, (void *)&data, size);

    return RT_EOK;
}
Exemplo n.º 4
0
/**
 * This function will handle cdc bulk in endpoint request.
 *
 * @param device the usb device object.
 * @param size request size.
 *
 * @return RT_EOK.
 */
static rt_err_t _ep_in_handler(udevice_t device, rt_size_t size)
{
    rt_uint32_t level;
    rt_size_t length;

    rt_size_t mps = ep_in->ep_desc->wMaxPacketSize;
    size = RT_RINGBUFFER_SIZE(&tx_ringbuffer);
    if(size == 0) return RT_EOK;

    length = size > mps ? mps : size;

    level = rt_hw_interrupt_disable();
    rt_ringbuffer_get(&tx_ringbuffer, ep_in->buffer, length);
    rt_hw_interrupt_enable(level);

    /* send data to host */
    dcd_ep_write(device->dcd, ep_in, ep_in->buffer, length);

    return RT_EOK;
}
Exemplo n.º 5
0
/**
 * This function will handle get_config_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _get_config_descriptor(struct udevice* device, ureq_t setup)
{
    rt_size_t size;
    ucfg_desc_t cfg_desc;

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

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_config_descriptor\n"));

    cfg_desc = &device->curr_cfg->cfg_desc;
    size = (setup->length > cfg_desc->wTotalLength) ?
           cfg_desc->wTotalLength : setup->length;

    /* send configuration descriptor to endpoint 0 */
    dcd_ep_write(device->dcd, 0, (rt_uint8_t*)cfg_desc, size);

    return RT_EOK;
}
Exemplo n.º 6
0
/**
 * This function will handle get_device_descriptor request.
 *
 * @param device the usb device object.
 * @param setup the setup request.
 *
 * @return RT_EOK on successful.
 */
static rt_err_t _get_device_descriptor(struct udevice* device, ureq_t setup)
{
    rt_size_t size;

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

    RT_DEBUG_LOG(RT_DEBUG_USB, ("_get_device_descriptor\n"));

    /* device descriptor length should less than USB_DESC_LENGTH_DEVICE*/
    size = (setup->length > USB_DESC_LENGTH_DEVICE) ?
           USB_DESC_LENGTH_DEVICE : setup->length;

    /* send device descriptor to endpoint 0 */
    dcd_ep_write(device->dcd, 0, (rt_uint8_t*)&device->dev_desc,
                 size);

    return RT_EOK;
}