<?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>Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/70030/missing-uart-bytes-during-transmission</link><description>I&amp;#39;m having an rather cryptic problem where around 0.01% bytes are simply not transmitted via UART for some reason. This error happens independently from the chosen baudrate, parity bytes, or control lines setting. I&amp;#39;ve verified it happens on the transmit</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 05 Jan 2021 18:08:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/70030/missing-uart-bytes-during-transmission" /><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287569?ContentTypeID=1</link><pubDate>Tue, 05 Jan 2021 18:08:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8580d559-76c5-41ea-a236-94dd32dcbfa2</guid><dc:creator>critbit</dc:creator><description>&lt;p&gt;I&amp;#39;ve resolved the issue in the previous thread.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287568?ContentTypeID=1</link><pubDate>Tue, 05 Jan 2021 18:07:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:657e5813-5df4-42e2-9926-e8d3457634f1</guid><dc:creator>critbit</dc:creator><description>&lt;p&gt;So I&amp;#39;ve done as I said, now the data to be broadcasted via UART is passed to the main() context via nrf_queue. This has solved the problem of missing bytes.&lt;br /&gt;&lt;br /&gt;I still don&amp;#39;t know the cause of the missing bytes, I guess the lesson for today is to never cut corners in such multi-context applications.&lt;br /&gt;&lt;br /&gt;Thanks for your cooperation.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287530?ContentTypeID=1</link><pubDate>Tue, 05 Jan 2021 13:47:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:222acb54-963d-4414-b224-db3b47ae6b26</guid><dc:creator>critbit</dc:creator><description>&lt;p&gt;Hey,&lt;br /&gt;&lt;br /&gt;The project is quite fresh, I&amp;#39;m using 17.0.2. I could migrate to anything at this point.&lt;br /&gt;&lt;br /&gt;I&amp;#39;m using the crystal, bit errors happen so infrequently they could be caused by cosmic rays at this point. Bytes not sent at all happen way more often.&lt;br /&gt;&lt;br /&gt;Thanks for the info about libuarte, this appears more familiar, when compared to STM32 ecosystem. However, it requires quite some refactoring from me.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ll post the results later, and whether I managed to sucesfully switch to libuarte.&lt;br /&gt;&lt;br /&gt;Best regards,&lt;br /&gt;Jakub&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287413?ContentTypeID=1</link><pubDate>Tue, 05 Jan 2021 08:26:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96be7bd0-0cbc-451d-9df1-f6b74da6adb3</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;Which SDK version are you using?&lt;/p&gt;
&lt;p&gt;The best UART driver in later versions of the SDK is the nrf_libuarte driver, which is designed to take full advantage of the EasyDMA feature of the UARTE driver.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Also,&amp;nbsp;unless you are requesting the external HF crystal to be used it could be that the UART baudrate deviates quite a bit between the nRF device and the receiver. Normally this would lead to bit errors rather than dropped bytes, but it is worth checking out.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287319?ContentTypeID=1</link><pubDate>Mon, 04 Jan 2021 16:44:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6834ef7-3860-4945-9da9-fe49e85e0cd3</guid><dc:creator>critbit</dc:creator><description>&lt;p&gt;Hey, thanks much for your time. &lt;br /&gt;&lt;br /&gt;I tried to find an correlation with amout of data sent. There are a little more characters lost when interface is more busy. But there seem to be to correlation with TX_FIFO size.&lt;br /&gt;&lt;br /&gt;The &amp;quot;counter&amp;quot; counts chars sucesfully sent between errors. Example log dump:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;17:11:22 ERROR lbga_serial.c:335: counter: 73724
17:11:26 ERROR lbga_serial.c:335: counter: 2419
17:12:33 ERROR lbga_serial.c:335: counter: 48811
17:13:43 ERROR lbga_serial.c:335: counter: 47623
17:14:32 ERROR lbga_serial.c:335: counter: 35296&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Regarding your idea about fault counting, I&amp;#39;m not using the uart_event_handle() at all.&lt;br /&gt;&lt;br /&gt;After having another look at app_uart_fifo.c I think I&amp;#39;ll just reimplement the TX fifo my way, or maybe implement message-based communication, so there&amp;#39;s no context mishmash, and everything is sent from main() context.&lt;br /&gt;&lt;br /&gt;I&amp;#39;ll check-in with the results here.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287083?ContentTypeID=1</link><pubDate>Sat, 02 Jan 2021 23:03:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e7878b86-2c69-4416-a8f5-fb21f281d4bc</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Oh, one other issue which is more&amp;nbsp;relevant.&amp;nbsp;&lt;em&gt;app_uart_put()&lt;/em&gt; is in a different thread and priority level (BLE SoftDevice) to&amp;nbsp;&lt;em&gt;app_uart_get()&lt;/em&gt; (user code serial interrupt handler). Now I recall you mentioned that &lt;em&gt;app_uart_put()&lt;/em&gt;&lt;span&gt;&amp;nbsp;is also invoked from the user code uart handler.&amp;nbsp;&lt;/span&gt;This introduces a race hazard since there is no locking and the code is not safely re-entrant, which may be more plausible than the end-of-buffer issue. I had the exact same issue now I recall; the answer is to only &lt;em&gt;put&lt;/em&gt; and &lt;em&gt;get&lt;/em&gt; from within the &lt;em&gt;main()&lt;/em&gt;&amp;nbsp;or other single context; incoming BLE packets are better buffered in an intermediate buffer before collecting and processing that buffer from the main context. I think this is your solution, and of course confirms your original suspicion.&lt;/p&gt;
&lt;p&gt;You can prove this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;in main.c:
volatile bool SignalAlreadyInInterrupt = false;
volatile uint32_t FaultCount = 0UL;

in ble_nus_c_evt_handler():
        case BLE_NUS_C_EVT_NUS_TX_EVT:
            // Check if interrupt was interrupted
            if (SignalAlreadyInInterrupt) FaultCount++;
            ble_nus_chars_received_uart_print(p_ble_nus_evt-&amp;gt;p_data, p_ble_nus_evt-&amp;gt;data_len);
            break;

in uart_event_handle()
{
        /**@snippet [Handling data from UART] */
        case APP_UART_DATA_READY:
            if (sending anything to uart) SignalAlreadyInInterrupt = true;
            Blah Blah
            SignalAlreadyInInterrupt = false;
            
in other uart transmits
        similat to above&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m spending time on this since it potentially affects medical devices I have designed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287081?ContentTypeID=1</link><pubDate>Sat, 02 Jan 2021 20:49:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ec0ba47d-57d9-42b5-a8b5-5823806f392d</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;&amp;gt;4k means 4096 I take it, assuming you are using the FIFO which requires a power of 2 buffer size. Since 1/4096 = .02% a single byte error on FIFO buffer wrap would be roughly the same order as the observed error rate (about 0.01%) which raises suspicions of a bug in the library code. Perhaps you could try changing the buffer size to see if it affects the error rate?&lt;/p&gt;
&lt;p&gt;Looking at the fifo code I see this dubious code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Put one byte to the FIFO. */
static __INLINE void fifo_put(app_fifo_t * p_fifo, uint8_t byte)
{
    p_fifo-&amp;gt;p_buf[p_fifo-&amp;gt;write_pos &amp;amp; p_fifo-&amp;gt;buf_size_mask] = byte;
    p_fifo-&amp;gt;write_pos++;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Notice how&amp;nbsp;&lt;em&gt;p_fifo-&amp;gt;write_pos&lt;/em&gt; is not checked for overflow; does it matter? Maybe not if always used with the mask, but I would describe that as unsafe code and now&amp;nbsp;&lt;em&gt;p_fifo-&amp;gt;write_pos&lt;/em&gt; will wrap at 2^32 instead of TX_BUFFER_SIZE.&amp;nbsp;&lt;em&gt;p_fifo-&amp;gt;write_pos&lt;/em&gt; is 32-bit,&amp;nbsp;&lt;em&gt;p_fifo-&amp;gt;buf_size_mask&lt;/em&gt; is 16-bit. Does it work? Probably, but dodgy code methinks&lt;/p&gt;
&lt;p&gt;Look at this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;static __INLINE uint32_t fifo_length(app_fifo_t * p_fifo)
{
    uint32_t tmp = p_fifo-&amp;gt;read_pos;
    return p_fifo-&amp;gt;write_pos - tmp;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;No sign of the mask being used here; does it matter? Hmm .. differencing two indices which are both beyond the end of the buffer they refer to is not good practice; when write wraps at 2^32-1 before read, what then? Maybe (with mask) 1 character is lost every 4096&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@brief Look at one byte in the FIFO. */
static __INLINE void fifo_peek(app_fifo_t * p_fifo, uint16_t index, uint8_t * p_byte)
{
    *p_byte = p_fifo-&amp;gt;p_buf[(p_fifo-&amp;gt;read_pos + index) &amp;amp; p_fifo-&amp;gt;buf_size_mask];
}


/**@brief Get one byte from the FIFO. */
static __INLINE void fifo_get(app_fifo_t * p_fifo, uint8_t * p_byte)
{
    fifo_peek(p_fifo, 0, p_byte);
    p_fifo-&amp;gt;read_pos++;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;peek uses mask, get does not, so no correct wrap on read.&lt;/p&gt;
&lt;p&gt;So, maybe try reducing the buffer size which should increase the error rate in proportion. Or I&amp;#39;m wrong :-)&lt;/p&gt;
&lt;p&gt;&lt;span&gt;My notes above were for SDK v15.3.0, but I see the FIFO handling was changed in SDK v17.0.2 so maybe just try that; it wasn&amp;#39;t clear from the post which version was used originally.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287079?ContentTypeID=1</link><pubDate>Sat, 02 Jan 2021 11:23:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dabe0efa-ce48-4899-ba58-ebca3f703736</guid><dc:creator>critbit</dc:creator><description>&lt;p&gt;I forgot to mention, the &lt;em&gt;UART_TX_BUF_SIZE&lt;/em&gt; is &amp;gt;4k&lt;br /&gt;&lt;br /&gt;While I agree that your scheme of transmission might work better in production, that doesn&amp;#39;t change anything in my case, as app_uart_put() always returns NRF_SUCCESS for me.&lt;br /&gt;&lt;br /&gt;Just for the reference, i checked it, doesn&amp;#39;t seem to fix the issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Missing UART bytes during transmission</title><link>https://devzone.nordicsemi.com/thread/287076?ContentTypeID=1</link><pubDate>Fri, 01 Jan 2021 23:31:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d2fc963a-0c86-4921-9548-4acd277d7dc2</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;Try replacing:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;        APP_ERROR_CHECK(app_uart_put(c));&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;with this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;         do
         {
            err_code = app_uart_put(c);
            if ((err_code != NRF_SUCCESS) &amp;amp;&amp;amp; (err_code != NRF_ERROR_BUSY))
            {
               // NRF_LOG_ERROR(&amp;quot;Failed Tx Uart message. Error 0x%x. \r\n&amp;quot;, err_code);
               // APP_ERROR_CHECK(err_code);
            }
         } while (err_code == NRF_ERROR_BUSY);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Also make&amp;nbsp;&lt;em&gt;UART_TX_BUF_SIZE&lt;/em&gt; big, say 2048&lt;/p&gt;
&lt;p&gt;I&amp;#39;ve used the above BLE-to-uart at both 230,400 and 1MBaud on large data transfers with no issues&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>