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

Where does USB CDC ACM data get read?

Hi All,

trying to understand the USB CDC ACM example for the Nordic 52840 using SDK17. I believe I've got it working, but there's one thing I'm not quite understanding. I'm using the example's event handler to notify me when data has been received, but when I'm in debug mode, I'm noticing that my data buffer is getting updated even before I make a call to app_usbd_cdc_acm_read() . Could anyone explain to me how this is happening? I was under the impression that I have to use the aforementioned function in order for my application to know where to put the received data. Thanks!

  • Alright, I think I've almost got it. Just curious though about the function app_usbd_cdc_arm_rx_size(). From my understanding this function should let me know how much data has been transferred from the internal buffer to the user buffer. The loop breaks when there is not enough data in the internal buffer to transfer into the user buffer. Why is it then that when I check app_usbd_cdc_arm_rx_size() after the loop, it doesn't show me a value of zero? 

     case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
            {
                ret_code_t ret;
                NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
                do
                {
                    /*Get amount of data transfered*/
                    size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
                    NRF_LOG_INFO("RX: size: %lu char: %c", size, m_rx_buffer[0]);
    
                    /* Fetch data until internal buffer is empty */
                    ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
                                                m_rx_buffer,
                                                READ_SIZE);
                } while (ret == NRF_SUCCESS);
                
                size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
    
                bsp_board_led_invert(LED_CDC_ACM_RX);
                break;
            }

  • The app_usbd_cdc_acm_read() will only return success if the buffer is filled, while the app_usbd_cdc_acm_rx_size() will return number of bytes received even if it's not enough to fill the buffer. You may consider using app_usbd_cdc_acm_read_any().

    Best regards,
    Kenneth

Related