This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to get data from pc by usb ep0

SDK ver:15.2.0

demo:usbd

i want to use the 52840 usb function ,the pc is the host ,the 52840 is device。

i cant use respond_setup_data to send data to pc。

but how to get data from pc?

source:

static void usbd_event_handler(nrf_drv_usbd_evt_t const * const p_event)
{
    switch (p_event->type)
    {
   ......
    case NRF_DRV_USBD_EVT_SETUP:
    {
        nrf_drv_usbd_setup_t setup;
        nrf_drv_usbd_setup_get(&setup);
        switch (setup.bmRequest)
        {
       ......

        case 0xFE: // send data to pc
            send_data_to_pc(&setup);    // useful
            break;

        case 0xFF: // get data from pc
            get_data_from_pc(&setup);    //unusefu
            break;

        default:
            NRF_LOG_ERROR("Unknown request: 0x%2x", setup.bmRequest);
            nrf_drv_usbd_setup_stall();
            return;
        }
        break;
    }
    default:
        break;
    }
}

static void  send_data_to_pc(nrf_drv_usbd_setup_t const * const p_setup)
{
    respond_setup_data(
        p_setup,
        get_descriptor_report_interface_0,
        p_setup->wLength);
}

static void  get_data_from_pc(nrf_drv_usbd_setup_t const * const p_setup)
{
      respond_get_data(
        p_setup,
        get_data,
        p_setup->wLength);
}

static void respond_setup_data(
    nrf_drv_usbd_setup_t const * const p_setup,
    void const * p_data, size_t size)
{
    /* Check the size against required response size */
    if (size > p_setup->wLength)
    {
        size = p_setup->wLength;
    }
    ret_code_t ret;
    nrf_drv_usbd_transfer_t transfer =
    {
        .p_data = {.tx = p_data},
        .size = size
    };
    ret = nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &transfer);
    if (ret != NRF_SUCCESS)
    {
        NRF_LOG_ERROR("Transfer starting failed: %d", (uint32_t)ret);
    }
    ASSERT(ret == NRF_SUCCESS);
    UNUSED_VARIABLE(ret);
}

static void respond_get_data(
    nrf_drv_usbd_setup_t const * const p_setup,
    void  * data, size_t size)
{
    /* Check the size against required response size */
    if (size > p_setup->wLength)
    {
        size = p_setup->wLength;
    }
    ret_code_t ret;
    nrf_drv_usbd_transfer_t transfer =
    {
        .p_data = {.rx = data},
        .size = size,
    };
    ret = nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer);
    if (ret != NRF_SUCCESS)
    {
        NRF_LOG_ERROR("Transfer starting failed: %d", (uint32_t)ret);
    }
    ASSERT(ret == NRF_SUCCESS);
    UNUSED_VARIABLE(ret);
}

pc usb drive is good ,i can use it to communicate with other evaluation boards.

in 52840DK ,send data to pc is ok, failed to get data from pc .

how to get data from pc by usb ep0?

thanks.

Parents
  • Hi,

    Could you explain what you are trying to send in the setup stage?

    What type of data are you trying to send from the pc ? What are you trying to achieve?

    If you are trying to do a data transfer, then you should use the NRF_DRV_USBD_EVT_EPTRANSFER event instead. See this and this link.

  • hi thank for you answer.

    i want to get 32 byte of data from the pc, It is then transmitted to other devices through spi.

    how to use the NRF_DRV_USBD_EVT_EPTRANSFER ?

    I can use nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPIN0, &transfer) to send data to PC,It can also trigger the

    usbd_event_handler function into the NRF_DRV_USBD_EVT_EPTRANSFER branch.

    but i can't use the nrf_drv_usbd_ep_transfer(NRF_DRV_USBD_EPOUT0, &transfer) to get data from pc ,It does not

    trigger usbd_event_handler.

    I don't understand the difference between them,Why can I send data to pc but not receive?

  • Hi,

    I believe you need to configure NRF_DRV_USBD_EPOUT1 for the data-transfer from pc to the nRF52840.

    If you are interested in using a predefined USB class, then perhaps the CDC ACM USB class is more suitable for your application. Take look at the usbd_cdc_acm example.

    (If you are creating your own custom USB class, then let me know, and I could create a simple example for that.)

Reply Children
Related