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

USBD high level API usage question

Hello,.

I have a question about using app_usbd_cdc_acm_read and app_usbd_cdc_acm_read_any in SDK15.2.0,

I am not sure I fully understand the description in reference of app_usbd_cdc_acm_read call:

According to this description, when there is not enough data in internal buffer can fill user buffer, the call will return a NRF_ERROR_IO_PENDING, so what will happen to the data stored in internal buffer now? will the data be copied to user buffer or just stay there waiting for enough data arrived ? if the data is copied to user buffer, then will APP_USBD_CDC_ACM_USER_EVT_RX_DONE will be raised at this point? 

However, if the data is not copied to user buffer, then what will happen if, the expected receiving size if larger than internal buffer size, say, 256bytes or even larger? won't the data in internal buffer overwritten by coming bytes?

thank your for your help!

/Roland

  • An extended question is about usage of app_usbd_cdc_acm_read_any:

    the document says it returns data as quick as any data is available, even if the given buffer was not totally full. In real, when I call this function and waiting for incoming bytes, I get a call back of APP_USBD_CDC_ACM_USER_EVT_RX_DONE on first 1 byte received.

    So I am not clear what exactly 'any data available' means? In case host sends a 64bytes packet, when APP_USBD_CDC_ACM_USER_EVT_RX_DONE will be trigged? Is it trigged on 1 byte received, or until all 64bytes received?

     

  • Hi Roland,

    According to this description, when there is not enough data in internal buffer can fill user buffer, the call will return a NRF_ERROR_IO_PENDING, so what will happen to the data stored in internal buffer now? will the data be copied to user buffer or just stay there waiting for enough data arrived ? if the data is copied to user buffer, then will APP_USBD_CDC_ACM_USER_EVT_RX_DONE will be raised at this point? 

    RX data is continuously written to the user buffer. APP_USBD_CDC_ACM_USER_EVT_RX_DONE is signaled to the application once the provided buffer(s) are full (i.e, received bytes == length). The code below will for instance trigger the RX event after receiving 256 bytes. In other words, the data will be copied to the user buffer while receiving, but RX done event will come after receiving all 256 bytes. 

    ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                      m_rx_buffer,
                                                      256);

  • Thank you Vidar.

    I am considering using wether app_usbd_cdc_acm_read or app_usbd_cdc_acm_read_any. The problem about app_usbd_cdc_acm_read is in case host activity is un-predictable then I have no idea how long I need to wait before 256 bytes buffer is filled. As you explained, it seems I have no way to poll the double buffer to access the bytes already received before APP_USBD_CDC_ACM_USER_EVT_RX_DONE is raised.

    Using  app_usbd_cdc_acm_read_any instead, however, getting out of my expectation. In my experiment I setup a timer, say , every 1 second, to fire app_usbd_cdc_acm_read_any call. Host keeps sending characters to device at baud rate 57600. So I am expecting when each timer fires app_usbd_cdc_acm_read_any will return with at least few characters received, not 1 byte. In real testing however, app_usbd_cdc_acm_read_any always return 1 byte is read. This will be no difference than using app_usbd_cdc_acm_read with buffer size of 1, which is I am trying to avoid to use. 

    I am not sure what I was doing wrong, or that is just how app_usbd_cdc_acm_read_any behave? That is why I am asking the extended question in previous post.

    /Roland

  • Hi Roland,

    I'm out of the office today, but expect to have an update for you tomorrow. Just need to verify excepted behavior on my side first.  

Related