<?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>Buffering UART data</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/64143/buffering-uart-data</link><description>I know this has been asked a couple of times, but I haven&amp;#39;t been able to find a straight forward answer. 
 I have a GPS module that spits out data via UART (no flow control). I would like to have the data stored into a buffer until I am ready to ready</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 13 Aug 2020 15:30:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/64143/buffering-uart-data" /><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/264520?ContentTypeID=1</link><pubDate>Thu, 13 Aug 2020 15:30:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:08409bcb-87e9-4705-ae09-deed1d4c6aeb</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;I ended up making my parser smarter so that it doesn&amp;#39;t care if the data starts at the beginning or not. So this doesn&amp;#39;t matter anymore. Thanks for all the help!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/264393?ContentTypeID=1</link><pubDate>Thu, 13 Aug 2020 08:55:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d33e2f9-8041-4898-be8f-ed7a4f312385</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;There are some misunderstandings here.&lt;/p&gt;
[quote user="ftjandra"]nrfx_uarte_rx() says that the receive buffer is double-buffered so that it can receive data continuously.[/quote]
&lt;p&gt;I am not sure we are talking about the same thing. The UART&lt;strong&gt;E &lt;/strong&gt;peripheral does not have a double buffered receive buffer in the same way as the UART, as it just copies data into RAM. The&amp;nbsp;RXD.PTR register is double-buffered though, but this just holds the address of the receive buffer in RAM.&lt;/p&gt;
[quote user="ftjandra"]3. Receives 17 bytes and&amp;nbsp;generates&amp;nbsp;NRFX_UARTE_EVT_RX_DONE event. I process the data and call&amp;nbsp;&lt;span&gt;nrfx_uarte_rx() to receive more. But at this point 11 bytes of the 17 bytes has overflowed and is already occupying the &amp;#39;second&amp;#39; buffer.&lt;/span&gt;[/quote]
&lt;p&gt;If that is the case, then you should perhaps use longer buffers, switch buffers immediately (this is why the PTR registers are double-buffered), or use flow control (which is usually a good idea anyway).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/264327?ContentTypeID=1</link><pubDate>Wed, 12 Aug 2020 15:45:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dd574c8a-1c80-42f3-b0ce-b6ddfc3f406f</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;That did not work. I don&amp;#39;t think it&amp;#39;s that simple.&amp;nbsp;nrfx_uarte_rx() says that the receive buffer is double-buffered so that it can receive data continuously.&lt;/p&gt;
&lt;p&gt;The data I want to receive comes in different lengths. Lets say 15, 17, and then 21 bytes. And I know that it will never be longer than 21 bytes. So I set the receiver buffer size to 21 bytes. The way I understand it:&lt;/p&gt;
&lt;p&gt;1. I call nrfx_uarte_init(), then nrfx_uarte_rx(), and it waits to receive data.&lt;/p&gt;
&lt;p&gt;2. Receives 15 bytes and puts it into buffer.&lt;/p&gt;
&lt;p&gt;3. Receives 17 bytes and&amp;nbsp;generates&amp;nbsp;NRFX_UARTE_EVT_RX_DONE event. I process the data and call&amp;nbsp;&lt;span&gt;nrfx_uarte_rx() to receive more. But at this point 11 bytes of the 17 bytes has overflowed and is already occupying the &amp;#39;second&amp;#39; buffer.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;4. Receives 21 bytes into &amp;#39;second&amp;#39; buffer and puts the data after the 11 bytes that are already there. Generates&amp;nbsp;NRFX_UARTE_EVT_RX_DONE&amp;nbsp;event.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;What I want is to start&amp;nbsp;nrfx_uarte_rx() &amp;#39;fresh&amp;#39; every time. I understand that the 17 byte packet could be lost if it gets sent before I call&amp;nbsp;nrfx_uarte_rx() again. Basically I don&amp;#39;t need the double-buffering.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Edit: I did try the following when receiving the NRFX_UARTE_EVT_RX_DONE event:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;nrfx_uarte_uninit(&amp;amp;m_uarte)
memset(uart_rx_buffer, 0, sizeof(uart_rx_buffer))
nrfx_uarte_init(&amp;amp;m_uarte, &amp;amp;config, uart_event_handler)
nrfx_uarte_rx(&amp;amp;m_uarte, uart_rx_buffer, sizeof(uart_rx_buffer))&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;But after I did that, I stopped receiving&amp;nbsp;NRFX_UARTE_EVT_RX_DONE events&amp;nbsp;all together.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/264182?ContentTypeID=1</link><pubDate>Wed, 12 Aug 2020 08:30:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:447a62b5-c83f-4515-bd25-81fdfeb3ce76</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;In this case, the Rx data is copied into normal RAM, so I assume that is what you refer to as the RX buffer in this case? If so, and you want to initialize it, you would just need to use memset() on the buffer before calling&amp;nbsp;nrfx_uart_rx().&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/264137?ContentTypeID=1</link><pubDate>Tue, 11 Aug 2020 23:56:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cdadfe2d-7b8b-425e-9386-75a9b4ab3a6b</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;Still trying to figure out how to clear the RX buffer when using&amp;nbsp;nrfx_uarte so that it starts with an empty buffer on calling&amp;nbsp;&lt;span&gt;nrfx_uart_rx().&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/263669?ContentTypeID=1</link><pubDate>Fri, 07 Aug 2020 15:33:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:788f73c3-a82e-4eec-8127-3113fad80e32</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;Oops, I mistyped. I am using&amp;nbsp;nrfx_uarte_init() and&amp;nbsp;nrfx_uarte_rx().&lt;/p&gt;
&lt;p&gt;And, I was on the wrong infocenter page when I was reading about&amp;nbsp;&lt;span&gt;nrfx_uart_rx_enable(), which I see has nothing to do when using UARTE.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;Thanks.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/263653?ContentTypeID=1</link><pubDate>Fri, 07 Aug 2020 14:34:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0fc299f2-df3c-4f25-9e1c-9eff15e85fd8</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I am sorry for the late reply. The UART Rx is enabled when you call&amp;nbsp;nrfx_uart_rx().It is not possible to clear the buffer in the UART peripheral. (If you used UARTE as you mentioned before that would be different since the peripheral copies data into RAM using DMA).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/262874?ContentTypeID=1</link><pubDate>Mon, 03 Aug 2020 23:39:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:095a54fd-6eb2-4f17-8e7f-f9ffb0402871</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;Ok, I think I got it working. How do I clear/reset the RX buffer before doing another call to&amp;nbsp;nrfx_uart_rx()?&lt;/p&gt;
&lt;p&gt;I did not call&amp;nbsp;nrfx_uart_rx_enable().&lt;/p&gt;
&lt;p&gt;I only did&amp;nbsp;nrfx_uart_init() followed by&amp;nbsp;&lt;span&gt;nrfx_uart_rx(). Then when I get the&amp;nbsp;NRFX_UARTE_EVT_RX_DONE event I process the data in buffer and then call&amp;nbsp;nrfx_uart_rx() again. But at this point it looks like the &amp;#39;overflow&amp;#39; from the past is already partially in the RX buffer. I want to start with an empty buffer.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/262680?ContentTypeID=1</link><pubDate>Sun, 02 Aug 2020 17:46:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e02e9339-1b7e-4336-bd81-9794493d6872</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;There are no official examples, but you can refer to other SDK modules that use it, for instance. You can also just refer to &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.0/hardware_driver_uart.html"&gt;the documentation&lt;/a&gt;, which covers most aspects (though itis slime on actual code examples).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/262621?ContentTypeID=1</link><pubDate>Fri, 31 Jul 2020 20:25:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e0d39590-eae3-4e07-be9f-a665b9dd8819</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;Are there any examples showing the use of either the nrf_drv_uart driver or nrfx_uarte directly?&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/262179?ContentTypeID=1</link><pubDate>Wed, 29 Jul 2020 12:14:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:104609b1-2592-4628-8c70-1b4633fe4fe2</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you are happy to receive a fixed size buffer over DMA every time, then I suggest you just use a UARTE driver. Either using the &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.0/hardware_driver_uart.html"&gt;nrf_drv_uart&lt;/a&gt;, or &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.0/group__nrfx__uart.html"&gt;nrfx_uarte&lt;/a&gt; (which is used by&amp;nbsp; nrf_drv_uart when DMA is enabled in the driver configuration).&lt;/p&gt;
&lt;p&gt;(Regarding the serial port library, this has been removed from SDK 17, but it is present in SDK 16).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/262066?ContentTypeID=1</link><pubDate>Tue, 28 Jul 2020 18:49:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5813a8d3-3bcc-4df5-aa8c-168a92a969e4</guid><dc:creator>ftjandra</dc:creator><description>&lt;p&gt;I think using the &amp;quot;UARTE peripheral which uses DMA (so that SW gets an event after receiving the specified amount of bytes)&amp;quot; would work fine. I&amp;#39;ll just size the buffer to fit at least one full transmission so that on every &amp;#39;buffer full&amp;#39; event I know that valid data is there.&lt;/p&gt;
&lt;p&gt;What driver or library would be best for this? I read that app_uart doesn&amp;#39;t really use DMA since it&amp;#39;s only doing a single byte at a time. Is this correct? What about the serial port library?&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Buffering UART data</title><link>https://devzone.nordicsemi.com/thread/262033?ContentTypeID=1</link><pubDate>Tue, 28 Jul 2020 14:39:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b72ab417-45f5-49d0-9968-b5b7254e28fa</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I want to start with sorting out a few things first.&lt;/p&gt;
&lt;p&gt;1. There are several SW libraries for handling UART communication. For instance, the app_uart library handles the buffering of arbitrary sized UART buffers. This may be relevant to you.&lt;/p&gt;
&lt;p&gt;2. There are two types of UART peripherals: The UART peripheral, which does not use DMA (so SW must handle every incoming byte), and the UARTE peripheral which uses DMA (so that SW gets an event after receiving the specified amount of bytes). You are correct though, that the UARTE peripheral has a limitation that it will not let you know how many bytes are received, so it is difficult to read half-full buffers. Therefor an option is to use &lt;a href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.0.0/lib_libuarte.html"&gt;Libuarte&lt;/a&gt;,&amp;nbsp;but this tends to complicate things a bit, and is heavy on PPI resources. The libuarte library is essentially a library that works around the limitation of reading less than full UARTE DMA buff&lt;/p&gt;
&lt;p&gt;What to use depends on your requirements. If you just receive single bytes and are happy with the driver doing a bit of work now and then behind the scenes, you can use app_uarte and get the data whenever you want. Seen from you application logic you will not need to care about the fact that some short interrupts are handled upon every received byte. But if that is a problem for you, then the libuarte is perhaps better.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>