<?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>USBD high level API usage question</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/44271/usbd-high-level-api-usage-question</link><description>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: 
 
 NRF_SUCCESS Data was stored into user buffer</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 31 Mar 2019 17:46:19 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/44271/usbd-high-level-api-usage-question" /><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/179362?ContentTypeID=1</link><pubDate>Sun, 31 Mar 2019 17:46:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3ffe029e-ec69-49f4-8a2b-300009b247fa</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/vibe"&gt;Vidar Berg&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I&amp;nbsp;did some other testing about&amp;nbsp;the 840 USBD interface from a MACBook using ttyusbxxxx interface, with a piece of code running on MAC that read/write from/to 840.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;According to the few test results so far, &amp;nbsp;this time the result looks the same as you advised.Thanks a lot for your support.&amp;nbsp;I will do further more testings to look into the&amp;nbsp;details.&lt;/p&gt;
&lt;p&gt;BTW, I am still using timer on 840 side to fire USB_READ_ANY operations, as I assume this way CPU can share more idle time with other tasks.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/175713?ContentTypeID=1</link><pubDate>Tue, 12 Mar 2019 14:28:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d3e8396-df62-40f8-bc64-74d0e7512557</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;I&amp;#39;m not sure why you only receive one byte. Here are the changes I made in SDK 15.3.0 example:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="diff"&gt;diff --git a/examples/peripheral/usbd_cdc_acm/main.c b/examples/peripheral/usbd_cdc_acm/main.c
index 51e6a3f..a87c3cd 100644
--- a/examples/peripheral/usbd_cdc_acm/main.c
+++ b/examples/peripheral/usbd_cdc_acm/main.c
@@ -129,7 +129,7 @@ APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
                             APP_USBD_CDC_COMM_PROTOCOL_AT_V250
 );
 
-#define READ_SIZE 1
+#define READ_SIZE 256
 
 static char m_rx_buffer[READ_SIZE];
 static char m_tx_buffer[NRF_DRV_USBD_EPSIZE];
@@ -150,9 +150,9 @@ static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
             bsp_board_led_on(LED_CDC_ACM_OPEN);
 
             /*Setup first transfer*/
-            ret_code_t ret = app_usbd_cdc_acm_read(&amp;amp;m_app_cdc_acm,
-                                                   m_rx_buffer,
-                                                   READ_SIZE);
+            ret_code_t ret = app_usbd_cdc_acm_read_any(&amp;amp;m_app_cdc_acm,
+                                                       m_rx_buffer,
+                                                       READ_SIZE);
             UNUSED_VARIABLE(ret);
             break;
         }
@@ -173,9 +173,9 @@ static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
                 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);
+                ret = app_usbd_cdc_acm_read_any(&amp;amp;m_app_cdc_acm,
+                                                m_rx_buffer,
+                                                READ_SIZE);
             } while (ret == NRF_SUCCESS);
 
             bsp_board_led_invert(LED_CDC_ACM_RX);
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And here is the log output I got (notice that it receives up to 64 bytes in this case - baudrate set to 115200):&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-ec4be8dea7eb4e4fb5c0b9ab639e07a1/log.PNG" /&gt;&lt;/p&gt;
&lt;p&gt;Can you try the same?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/175101?ContentTypeID=1</link><pubDate>Fri, 08 Mar 2019 15:30:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:690d2954-4fc3-4966-b9f5-166c2cb269e3</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Thanks for the explanation Vidar.&lt;/p&gt;
&lt;p&gt;I will check my test further to see if anything wrong, as I always get 1 byte in my side.&lt;/p&gt;
&lt;p&gt;Besides, I actually more prefer timer based polling using&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read_any.&amp;nbsp;The code example you gave above keeps CPU busy receiving data. In case host is&amp;nbsp;continuously sending data to device this will occupy CPU time too long for other tasks.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;A timer based&amp;nbsp;polling mode is better as CPU can share the load between all tasks, as long as buffer&amp;nbsp;size properly selected.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Do you think&amp;nbsp;app_usbd_cdc_acm_read_any still work as you explained in timer based polling mode?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/175009?ContentTypeID=1</link><pubDate>Fri, 08 Mar 2019 12:50:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:57f26315-d945-4997-894b-a0e488823e75</guid><dc:creator>Vidar Berg</dc:creator><description>[quote userid="13823" url="~/f/nordic-q-a/44271/usbd-high-level-api-usage-question/174999"]What&amp;nbsp;I am trying to find out is&amp;nbsp;what size will&amp;nbsp;app_usbd_cdc_acm_read_any return, is it always 1 bytes?[/quote]
&lt;p&gt;In the code above, app_usbd_cdc_acm_rx_size() will return anything between 1-256 depending on how fast the data is being received. &amp;quot;Size&amp;quot; corresponds to the actual number of bytes stored to the user buffer (m_rx_buffer in this case). It will not return 1 If you received a larger chunk of data.&lt;/p&gt;
[quote userid="13823" url="~/f/nordic-q-a/44271/usbd-high-level-api-usage-question/174999"]Consider a typical setup: the device called&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read_any and wait for&amp;nbsp;&lt;/span&gt;incoming bytes, and host start to send, say, 32bytes in one packet. So will&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read_any return size of 1 as soon as&amp;nbsp;first byte is received and repeat for 32 times, &amp;nbsp;or 32 after complete packet received?&lt;/span&gt;[/quote]
&lt;p&gt;In this case,&amp;nbsp;&lt;span&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&amp;nbsp; should occur once, and&amp;nbsp;app_usbd_cdc_acm_rx_size should return 32.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/174999?ContentTypeID=1</link><pubDate>Fri, 08 Mar 2019 12:05:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:24eecd00-c8db-4a26-9b4e-7e3794afac17</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Thanks Vidar.&lt;/p&gt;
&lt;p&gt;I did know that&amp;nbsp;&lt;span&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE is trigged even buffer not full when using&amp;nbsp;app_usbd_cdc_acm_read_any.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;What&amp;nbsp;I am trying to find out is&amp;nbsp;what size will&amp;nbsp;app_usbd_cdc_acm_read_any return, is it always 1 bytes?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I do not want to receive data one bytes per&amp;nbsp;polling, as this seems quite low&amp;nbsp;&lt;/span&gt;efficient and interrupts other tasks too often.&lt;/p&gt;
&lt;p&gt;Consider a typical setup: the device called&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read_any and wait for&amp;nbsp;&lt;/span&gt;incoming bytes, and host start to send, say, 32bytes in one packet. So will&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read_any return size of 1 as soon as&amp;nbsp;first byte is received and repeat for 32 times, &amp;nbsp;or 32 after complete packet received?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/174978?ContentTypeID=1</link><pubDate>Fri, 08 Mar 2019 09:51:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6a65f804-4221-4885-81db-2e97562ad1e5</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi Roland,&lt;/p&gt;
&lt;p&gt;You will still get the&amp;nbsp;APP_USBD_CDC_ACM_USER_EVT_RX_DONE event when using *_read_any as well, so you don&amp;#39;t need a timer for polling the buffer size. The event can get triggered even if you haven&amp;#39;t filled up the buffer. E.g., you will get the event if you only send a single character.&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&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 = app_usbd_cdc_acm_rx_size(p_cdc_acm);
                NRF_LOG_INFO(&amp;quot;%d bytes written to m_rx_buffer&amp;quot;, size);

                /* Set up new buffer*/
                ret = app_usbd_cdc_acm_read_any(&amp;amp;m_app_cdc_acm,
                                                 m_rx_buffer,
                                                 256);
            } while (ret == NRF_SUCCESS);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/174717?ContentTypeID=1</link><pubDate>Thu, 07 Mar 2019 07:40:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1a28733a-7768-4f95-9c64-044354fcbcfe</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi Roland,&lt;/p&gt;
&lt;p&gt;I&amp;#39;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.&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/174458?ContentTypeID=1</link><pubDate>Wed, 06 Mar 2019 08:16:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ef50197-9865-4daa-9058-513c8fd2d9e2</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Thank you Vidar.&lt;/p&gt;
&lt;p&gt;I am considering using wether&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read or&amp;nbsp;app_usbd_cdc_acm_read_any. The problem about&amp;nbsp;app_usbd_cdc_acm_read is&amp;nbsp;in case host activity is un-predictable then&amp;nbsp;I have no idea how long&amp;nbsp;I need to wait before 256 bytes buffer is filled.&amp;nbsp;As you explained, it seems&amp;nbsp;I&amp;nbsp;have no way to poll the double buffer to access the bytes already received before&amp;nbsp;APP_USBD_CDC_ACM_USER_EVT_RX_DONE is raised.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Using&amp;nbsp;&amp;nbsp;app_usbd_cdc_acm_read_any instead, however, getting out of my expectation. In my experiment&amp;nbsp;I setup a timer, say ,&amp;nbsp;every 1&amp;nbsp;second, to fire app_usbd_cdc_acm_read_any call. Host keeps sending characters to device at&amp;nbsp;baud rate 57600. So&amp;nbsp;I am expecting when each timer fires&amp;nbsp;app_usbd_cdc_acm_read_any will return with at least few characters received, not 1 byte. In real testing however,&amp;nbsp;app_usbd_cdc_acm_read_any always return 1 byte is read. This&amp;nbsp;will be no&amp;nbsp;difference than using&amp;nbsp;app_usbd_cdc_acm_read with buffer size of 1, which is&amp;nbsp;I am trying to avoid to use.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I am not sure what&amp;nbsp;I was doing wrong, or that is just how&amp;nbsp;app_usbd_cdc_acm_read_any behave? That is why&amp;nbsp;I am asking the extended question in previous post.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;/Roland&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/174368?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2019 17:41:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e1736fa1-42f2-498a-ba91-e79e4245adcd</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi Roland,&lt;/p&gt;
[quote user=""]According to this description, when there is not enough data in internal buffer can fill user buffer, the call will return a&amp;nbsp;&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.2.0/group__sdk__common__errors.html#gaa77764b770e616614429200ca028a7b0"&gt;NRF_ERROR_IO_PENDING&lt;/a&gt;, 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&amp;nbsp;&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.2.0/group__app__usbd__cdc__acm.html#ggaa9ed7eb4cf5195a3b6a1a22a10ad1fbaaf1b79a0aa25f655e4c2779404fd86168"&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&lt;/a&gt;&amp;nbsp;will be raised at this point?&amp;nbsp;[/quote]
&lt;p&gt;RX data is continuously&amp;nbsp;written to the user buffer. APP_USBD_CDC_ACM_USER_EVT_RX_DONE is signaled to the application once the provided buffer(s)&amp;nbsp;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.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;ret = app_usbd_cdc_acm_read(&amp;amp;m_app_cdc_acm,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m_rx_buffer,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 256);&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/174326?ContentTypeID=1</link><pubDate>Tue, 05 Mar 2019 15:02:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:08d3e79b-fe2f-4893-85e6-c8bb50ec661c</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;Hello, anyone can help with the questions ?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: USBD high level API usage question</title><link>https://devzone.nordicsemi.com/thread/173605?ContentTypeID=1</link><pubDate>Fri, 01 Mar 2019 06:30:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f3304701-f7ac-4f01-a4fd-98ce8075553a</guid><dc:creator>rolandash</dc:creator><description>&lt;p&gt;An extended question is about usage of&amp;nbsp;&lt;span&gt;app_usbd_cdc_acm_read_any:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;the document says&amp;nbsp;it returns data as quick as any data is available, even if the given buffer was not totally full. In real, when&amp;nbsp;I call this function and waiting for incoming bytes,&amp;nbsp;I get a call back of&amp;nbsp;&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.2.0/group__app__usbd__cdc__acm.html#ggaa9ed7eb4cf5195a3b6a1a22a10ad1fbaaf1b79a0aa25f655e4c2779404fd86168"&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&lt;/a&gt;&amp;nbsp;on first 1 byte received.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;So&amp;nbsp;I am not clear what exactly &amp;#39;any data available&amp;#39; means? In case host sends a 64bytes packet, when&amp;nbsp;&lt;a href="http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.2.0/group__app__usbd__cdc__acm.html#ggaa9ed7eb4cf5195a3b6a1a22a10ad1fbaaf1b79a0aa25f655e4c2779404fd86168"&gt;APP_USBD_CDC_ACM_USER_EVT_RX_DONE&lt;/a&gt;&amp;nbsp;will be&amp;nbsp;&lt;/span&gt;trigged? Is it trigged on 1 byte received, or until all 64bytes received?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>