<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/68136/where-does-usb-cdc-acm-data-get-read</link><description>Hi All, 
 trying to understand the USB CDC ACM example for the Nordic 52840 using SDK17. I believe I&amp;#39;ve got it working, but there&amp;#39;s one thing I&amp;#39;m not quite understanding. I&amp;#39;m using the example&amp;#39;s event handler to notify me when data has been received,</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 16 Nov 2020 11:13:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/68136/where-does-usb-cdc-acm-data-get-read" /><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/280074?ContentTypeID=1</link><pubDate>Mon, 16 Nov 2020 11:13:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5f8ced4b-ceb7-4e65-8501-6650d87896c1</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;The&amp;nbsp;app_usbd_cdc_acm_read() will only return success if the buffer is filled, while the&amp;nbsp;app_usbd_cdc_acm_rx_size() will return number of bytes received even if it&amp;#39;s not enough to fill the buffer. You may consider using&amp;nbsp;app_usbd_cdc_acm_read_any().&lt;/p&gt;
&lt;p&gt;Best regards,&lt;br /&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/279923?ContentTypeID=1</link><pubDate>Fri, 13 Nov 2020 13:32:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aff4979f-ccb9-4efa-8eb4-b7af8294ebd3</guid><dc:creator>lorenzoFIL</dc:creator><description>&lt;p&gt;Alright, I think I&amp;#39;ve almost got it. Just curious though about the function&amp;nbsp;&lt;strong&gt;app_usbd_cdc_arm_rx_size()&lt;/strong&gt;. 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&amp;nbsp;&lt;strong&gt;app_usbd_cdc_arm_rx_size()&lt;/strong&gt; after the loop, it doesn&amp;#39;t show me a value of zero?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt; case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
        {
            ret_code_t ret;
            NRF_LOG_INFO(&amp;quot;Bytes waiting: %d&amp;quot;, 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(&amp;quot;RX: size: %lu char: %c&amp;quot;, size, m_rx_buffer[0]);

                /* Fetch data until internal buffer is empty */
                ret = app_usbd_cdc_acm_read(&amp;amp;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;
        }&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/279439?ContentTypeID=1</link><pubDate>Wed, 11 Nov 2020 09:52:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c699aa6b-a728-413a-8d59-92e4ab1fc787</guid><dc:creator>Kenneth</dc:creator><description>[quote user="lorenzoFIL"]If at this time there was a sufficient amount of data in my internal buffer, the function would return NRF_SUCCESS and then the buffer would also be updated accordingly?&amp;nbsp;[/quote]
&lt;p&gt;&amp;nbsp;Yes.&amp;nbsp;&lt;/p&gt;
[quote user="lorenzoFIL"]Also, am I right when I say that the size of the internal buffer is 64 bytes?&amp;nbsp;[/quote]
&lt;p&gt;&amp;nbsp;The max endpoint size is 64bytes yes (NRFX_USBD_FEEDER_BUFFER_SIZE).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/279361?ContentTypeID=1</link><pubDate>Tue, 10 Nov 2020 19:45:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d31d6d36-4ffe-4f81-80bd-e246d1339925</guid><dc:creator>lorenzoFIL</dc:creator><description>&lt;p&gt;Alright, I think it&amp;#39;s becoming more clear now. So the function&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt; actually sets up the user buffer, and does not actually do the transfer of data from internal buffer to user at the time of invocation.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;So if my understanding is correct, then on the first run of &lt;strong&gt;app_usbd_cdc_acm_read()&amp;nbsp;&lt;/strong&gt;, the return should be NRF_ERROR_IO_PENDING since (in my case) there has been no data received. After the USB receives as much data as specified in the first call to&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt;, then&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&amp;nbsp;&lt;/strong&gt;event will occur. At which point I can then setup the buffer again for the next transfer by using&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt;.&amp;nbsp;If at this time there was a sufficient amount of data in my internal buffer, the function would return NRF_SUCCESS and then the buffer would also be updated accordingly?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Also, am I right when I say that the size of the internal buffer is 64 bytes?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/279326?ContentTypeID=1</link><pubDate>Tue, 10 Nov 2020 15:24:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:44da9c55-442e-402e-bde6-4e06385abd81</guid><dc:creator>Kenneth</dc:creator><description>[quote user="lorenzoFIL"]I do notice that in the&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN&amp;nbsp;&lt;/strong&gt;event, the comment before calling&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt; says &amp;quot;Setup first transfer&amp;quot;. I&amp;#39;m wondering if that first call to&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt; creates some link to my buffer initially...?&amp;nbsp;[/quote]
&lt;p&gt;Looks like you are right, I can see from the declaration of app_usbd_cdc_acm_read():&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * @brief Reads data from CDC ACM serial port.
 *
 * This function uses internal buffer and double buffering for continuous transmission.
 *
 * If there is enough data in internal buffer to fill user buffer, NRF_SUCCESS is
 * returned and data is immediately available in the user buffer.
 *
 * If not, up to two user buffers can be scheduled, function returns NRF_ERROR_IO_PENDING
 * when first buffer is filled and @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event is generated.
 *
 * @sa app_usbd_cdc_acm_read_any
 * @sa app_usbd_cdc_acm_rx_size
 *
 * @param[in]  p_cdc_acm CDC ACM class instance (defined by @ref APP_USBD_CDC_ACM_GLOBAL_DEF).
 * @param[out] p_buf     Output buffer.
 * @param[in]  length    Number of bytes to read.
 *
 * @retval NRF_SUCCESS          Data is stored into user buffer.
 * @retval NRF_ERROR_IO_PENDING Awaiting transmission, when data is stored into user buffer,
 *                              @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event will be raised.
 * @retval NRF_ERROR_BUSY       There are already 2 buffers queued for transfers.
 * @retval other                Standard error code.
 */
ret_code_t app_usbd_cdc_acm_read(app_usbd_cdc_acm_t const * p_cdc_acm,
                                 void *                     p_buf,
                                 size_t                     length);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;In specific:&lt;/p&gt;
&lt;p&gt;* @retval NRF_ERROR_IO_PENDING &lt;span style="text-decoration:underline;"&gt;Awaiting transmission, when data is stored into user buffer&lt;/span&gt;,&lt;br /&gt; * @ref APP_USBD_CDC_ACM_USER_EVT_RX_DONE event will be raised.&lt;/p&gt;
&lt;p&gt;You may consider using&amp;nbsp;app_usbd_cdc_acm_read_any() instead.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/279260?ContentTypeID=1</link><pubDate>Tue, 10 Nov 2020 13:26:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0da75c2e-2def-4d4b-843a-f5a88a186f3e</guid><dc:creator>lorenzoFIL</dc:creator><description>&lt;p&gt;The buffer is exactly as it is in the example, so yes, a global static in my main.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;static char m_rx_buffer[READ_SIZE]&lt;/p&gt;
&lt;p&gt;The function&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&amp;nbsp;&lt;/strong&gt;is only used in two locations! Once for the event&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN&amp;nbsp;&lt;/strong&gt;and then in&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&lt;/strong&gt; it is quite peculiar. There are no modifications to m_rx_buffer otherwise.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;At first I thought maybe the&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN&amp;nbsp;&amp;nbsp;&lt;/strong&gt;was getting triggered along with&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&lt;/strong&gt;, but if I set breakpoints accordingly, when I send a character via terminal, only&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&amp;nbsp;&lt;/strong&gt;gets called. And then as I stated, even before invoking&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read&lt;/strong&gt;&lt;strong&gt;()&lt;/strong&gt;, my buffer has already been updated.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I do notice that in the&amp;nbsp;&lt;strong&gt;APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN&amp;nbsp;&lt;/strong&gt;event, the comment before calling&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt; says &amp;quot;Setup first transfer&amp;quot;. I&amp;#39;m wondering if that first call to&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt; creates some link to my buffer initially...?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Where does USB CDC ACM data get read?</title><link>https://devzone.nordicsemi.com/thread/279144?ContentTypeID=1</link><pubDate>Tue, 10 Nov 2020 08:46:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:19d67879-40a1-4bba-91a5-913b6478fcde</guid><dc:creator>Kenneth</dc:creator><description>[quote user=""]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.[/quote]
&lt;p&gt;I agree with that, the second (buffer) and third (length) parameter in&amp;nbsp;&lt;strong&gt;app_usbd_cdc_acm_read()&lt;/strong&gt;&amp;nbsp;control where to copy the data. How have you initialized the buffer (is is a global static)? Is there any other block of code that may be modifying the buffer?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>