<?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>Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/45148/differences-between-circular-ring-buffer-libraries-in-the-sdk</link><description>Hi, 
 I was wondering what are the main differences between the different libraries included in the SDK that are meant to be implementations of ring buffers. I&amp;#39;m talking specifically about the differences between the FIFO library, the queue library and</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 21 Mar 2019 14:22:09 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/45148/differences-between-circular-ring-buffer-libraries-in-the-sdk" /><item><title>RE: Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/thread/177618?ContentTypeID=1</link><pubDate>Thu, 21 Mar 2019 14:22:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3ec9954c-e7f8-4f69-b2bb-3b835c1f3efe</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;Ok, thanks for the help, as always! :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/thread/177599?ContentTypeID=1</link><pubDate>Thu, 21 Mar 2019 13:44:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:486a9a81-4250-4f85-a018-16bfea4a5d1c</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;[quote user="andresovela"][/quote]&lt;/p&gt;
&lt;p&gt;From what you said, the fact that the ringbuf library handles elements of arbitrary size just means that you can &amp;quot;put&amp;quot; an arbitrary amount of bytes in the buffer.&lt;/p&gt;
&lt;p&gt;Is that correct?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Yes, that is correct.&lt;/p&gt;
[quote user="andresovela"]I have just one more question: You mentioned a difference between the usage of critical sections and nrf_atomic operations. How do they compare?[/quote]
&lt;p&gt;Using critical sections disable all interrupts temporarily and re-enables them again. This is no problem as long at it is for short times, but it has a performance penalty and you block potentially unrelated high priority interrupts.&lt;/p&gt;
&lt;p&gt;The atomic operations module&amp;nbsp;use the &lt;a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dht0008a/ch01s02s01.html"&gt;LDREX/STREX instructions&lt;/a&gt;&amp;nbsp;that are present in the Cortex-M4 core. This is used to make a form of mutex to make sure that buffer is not updated by one &amp;quot;user&amp;quot; while it is being manipulated by another user without the need for critical sections.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/thread/177567?ContentTypeID=1</link><pubDate>Thu, 21 Mar 2019 12:15:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a047ef0b-1356-4fae-96d1-a615428978df</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;Ok, so take-aways from this thread:&lt;/p&gt;
&lt;p&gt;The nrf_ringbuf library is a better app_fifo, and the queue library incorporates an abstraction layer that handles things like putting structs and arrays and considers them as single elements.&lt;/p&gt;
&lt;p&gt;From what you said, the fact that the ringbuf library handles elements of arbitrary size just means that you can &amp;quot;put&amp;quot; an arbitrary amount of bytes in the buffer.&lt;/p&gt;
&lt;p&gt;Is that correct?&lt;/p&gt;
&lt;p&gt;I have just one more question: You mentioned a difference between the usage of critical sections and nrf_atomic operations. How do they compare?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/thread/177560?ContentTypeID=1</link><pubDate>Thu, 21 Mar 2019 11:54:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0594520f-82f7-4076-a86f-86522d16801b</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="andresovela"]When is thread safety important? When protecting against interrupts? Or is this an RTOS thing?[/quote]
&lt;p&gt;It is important if you are using the library from several interrupt priorities, regardless of it is using an RTOS or not. If it is possible that a fifo operation can be preempted by another fifo operation, then that is a concern.&lt;/p&gt;
[quote user="andresovela"]And about the ringbuf library: I don&amp;#39;t really get it. I read the documentation but it doesn&amp;#39;t appear to work with &amp;quot;elements&amp;quot; as you mentioned. It seems to me that it works more with raw data.[/quote]
&lt;p&gt;You are right,&amp;nbsp;I was basing this on my bad memory. The nrf_ringbuf works with raw data, same as app_fifo. But this is preferable, since it uses atomic operations to ensure thread safety.&lt;/p&gt;
[quote user="andresovela"]How does&amp;nbsp;it work for example if I want to get 5 elements and all of them are of different sizes?&amp;nbsp;As far as I can tell the library doesn&amp;#39;t support stuff like this.[/quote]
&lt;p&gt;You need to use your own scheme on top if you need it to handle different objects, for instance a TLV (type-length-value) variant.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/thread/177539?ContentTypeID=1</link><pubDate>Thu, 21 Mar 2019 10:29:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a66417e0-9204-42e0-b76b-74b3b44cd87f</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;Thanks for your answer. It&amp;#39;s very helpful :) I still have a couple of questions.&lt;/p&gt;
&lt;p&gt;When is thread safety important? When protecting against interrupts? Or is this an RTOS thing?&lt;/p&gt;
&lt;p&gt;And about the ringbuf library: I don&amp;#39;t really get it. I read the documentation but it doesn&amp;#39;t appear to work with &amp;quot;elements&amp;quot; as you mentioned. It seems to me that it works more with raw data.&lt;/p&gt;
&lt;p&gt;How does&amp;nbsp;it work for example if I want to get 5 elements and all of them are of different sizes?&amp;nbsp;As far as I can tell the library doesn&amp;#39;t support stuff like this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Differences between circular/ring buffer libraries in the SDK</title><link>https://devzone.nordicsemi.com/thread/177530?ContentTypeID=1</link><pubDate>Thu, 21 Mar 2019 10:10:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7deda796-bfd1-43df-bbdc-109dd84384e4</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The libraries have been developed over time for similar use cases. The best is to look at them and find which quits you, but here is a short list of differences:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;app_fifo does not use atomic operations or critical sections, so it is not thread safe.&lt;/li&gt;
&lt;li&gt;nrf_queue and nrf_ringbuf handle this, but do it differently.
&lt;ul&gt;
&lt;li&gt;nrf_ringbuf use atomic operations using nrf_atomic, which uses&amp;nbsp;LDREX/STREX. this is probably the best for most use cases. It uses arbitrary sized elements / objects.&lt;/li&gt;
&lt;li&gt;nrf_queue si a older library, that is extensively used in SDK libraries and examples. It uses critical sections. It uses fixed size elements.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Usage:
&lt;ul&gt;
&lt;li&gt;nrf_queue use fixed size elements.&lt;/li&gt;
&lt;li&gt;nrf_ringbuf on the other hand can handle elements of varying size.&lt;/li&gt;
&lt;li&gt;app_fifo operate on byte elements, but provides ways to read and write chunks as well.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Which you want to use depend on your needs.&lt;/p&gt;
&lt;p&gt;(There is also a nrf_fifo, but that is specifically intended to be used in IoT context).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>