This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

USB missing byte

Hi,

I am developing my own vendor specific USB device with 2 interfaces (data and debug) and 2 endpoints(in and out) each. I based my class on the cdc_acm class.

So far most of it works. The only problem is, when I send more than 1 byte at a time to the device. Then the first byte is missing and the last one is repeated.

E.g. I'm sending "0x0F 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x7B 0x02" but I'm receiving "0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x7B 0x02 0x02".

My RX_DONE event looks like this:

do{
    ret = app_usbd_dev_read_data(&m_app_dev, m_data_buf, 1);
    NRF_LOG_INFO("data read: 0x%X: 0x%02X", ret, *m_data_buf);
    pu8Data[u16Length] = *m_data_buf;
    u16Length++;
}while(ret == NRF_SUCCESS);

If I do it like in the cdc_acm example with

if(ret == NRF_SUCCESS)
    u16Length++;

the received data looks like this: "0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x7B 0x02". So the first byte is missing and I'm also not able to receive just one byte. That's because the function returns NRF_ERROR_IO_PENDING.

Does anyone have an idea what the problem could be?

I am using the nRF5 SDK 17.1.0 and an nRF52833DK.

Kind regards,
Johannes

Parents
  • Hi Johannes, 

    If you search for "1 byte missing usb devzone"  you can find many cases on devzone asked about this. It's a bit confusing in the way the function app_usbd_cdc_acm_read() was designed. 
    I would suggest to have a look here.

    As I explained in the case, quoted:
    ===========

    As Dmitry already explained to you app_usbd_cdc_acm_read() doesn't read the byte(s) immediately, it only provide a buffer for the next "byte" (or bytes depends on the buffer size you provided) to come. 

    So in your case you can call app_usbd_cdc_acm_read() with the buffer size of 64 bytes. See APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN event in the cdc example. 

    When you receive APP_USBD_CDC_ACM_USER_EVT_RX_DONE event this meant that there are 64 bytes already in the buffer and you can start using it. You don't need to call app_usbd_cdc_acm_read() again to read them. You call app_usbd_cdc_acm_read() again to prepare the buffer for the next 64 bytes. 

    And since app_usbd_cdc_acm_read() can handle double buffer, you can call app_usbd_cdc_acm_read() two times with two different buffers in APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN event. This way you have double buffer and can handle if there are more than 64 bytes comes before you can process them. 

    ============

    I would need to see your full code and what inside your app_usbd_dev_read_data() but as mentioned above when you call app_usbd_cdc_acm_read() it's for the next byte to come, the current byte is already in the buffer when you receive APP_USBD_CDC_ACM_USER_EVT_RX_DONE event.

  • Hi Hung Bui,

    thanks for the fast reply.

    I found the error yesterday evening and it is exactly the same error you mentioned.
    It's funny. I searched for "1 byte missing usb" but didn't find the case you posted and all the other cases I found were similar but ultimately different.

    Kind regards
    Johannes

Reply
  • Hi Hung Bui,

    thanks for the fast reply.

    I found the error yesterday evening and it is exactly the same error you mentioned.
    It's funny. I searched for "1 byte missing usb" but didn't find the case you posted and all the other cases I found were similar but ultimately different.

    Kind regards
    Johannes

Children
No Data
Related