<?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>Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/50057/ring-buffer-or-queue-for-different-datatype</link><description>Hi, 
 I want to sample ADC signals and send data when requested via BLE or UART. I set a timer that triggers every 100 ms and gets ADC data. To store the data, I would like to use some large buffer that will store incoming data. If the buffer reaches</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 20 Aug 2019 10:58:36 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/50057/ring-buffer-or-queue-for-different-datatype" /><item><title>RE: Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/thread/204993?ContentTypeID=1</link><pubDate>Tue, 20 Aug 2019 10:58:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4fbd02f7-30da-41d3-9065-57f29b4b41e3</guid><dc:creator>haakonsh</dc:creator><description>[quote user="Ali Rumane"]Is there a way I can get available length before copying so that I don&amp;#39;t have to remove half written data or copy half data so that I can free (nrf_ringbuf_free) some data and then copy the data.[/quote]
&lt;p&gt;There&amp;#39;s no such functionality, you&amp;#39;ll have to track the amount of data stored yourself.&amp;nbsp;&lt;br /&gt;As each call to&amp;nbsp;&lt;a title="Function for copying data directly into the ring buffer." href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.3.0/group__nrf__ringbuf.html#ga8e6a3154911c158fc03174a5203d6ee6"&gt;nrf_ringbuf_cpy_put&lt;/a&gt;&amp;nbsp;will return the length of written data you can increment a global counter variable&amp;nbsp;when you write and decrement when you free. That way you can compare the counter variable to the ring buffer size and know how much space is free.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/thread/204744?ContentTypeID=1</link><pubDate>Mon, 19 Aug 2019 12:04:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1200a733-2d56-4568-8173-f057cec5e37c</guid><dc:creator>Ali Rumane</dc:creator><description>&lt;p&gt;HI &lt;a href="https://devzone.nordicsemi.com/members/haakonsh"&gt;haakonsh&lt;/a&gt;,&lt;/p&gt;
&lt;p&gt;Thank you for your help, I managed to implement it correctly.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;NRF_RINGBUF_DEF(m_ringbuf, 8);

int main(void)
{
	ret_code_t err_code;
	
	nrf_ringbuf_init(&amp;amp;m_ringbuf);
	
	uint16_t data_in[] = {1024, 1025, 1026, 1027};
	size_t  len_in = sizeof(data_in);

	err_code = nrf_ringbuf_cpy_put(&amp;amp;m_ringbuf, (uint8_t*)data_in, &amp;amp;len_in);
	APP_ERROR_CHECK(err_code);
		
    uint16_t data_out[4];
    size_t  len_out = sizeof(data_out);

    err_code = nrf_ringbuf_cpy_get(&amp;amp;m_ringbuf, (uint8_t*)data_out, &amp;amp;len_out);
	APP_ERROR_CHECK(err_code);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am facing another issue.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s say I have got 2 bytes of memory left and would like to copy 4 bytes of data. If I use&lt;/p&gt;
&lt;p&gt;nrf_ringbuf_cpy_put, I will get available length or copied length after copying the data.&lt;/p&gt;
&lt;p&gt;Is there a way I can get available length before copying so that I don&amp;#39;t have to remove half written data or copy half data so that I can free (nrf_ringbuf_free) some data and then copy the data.&lt;/p&gt;
&lt;p&gt;e.g.&lt;/p&gt;
&lt;p&gt;NRF_RINGBUF_DEF(m_ringbuf, 8);&lt;/p&gt;
&lt;p&gt;ring_bufer(1,2,3,4,5,6)&amp;nbsp;&amp;nbsp; // Data already present in buffer&lt;/p&gt;
&lt;p&gt;{7,8,9,10}&amp;nbsp; // New data to write&lt;/p&gt;
&lt;p&gt;Since only two bytes are left, ring buffer will write 7&amp;amp;8 and return available length.&lt;/p&gt;
&lt;p&gt;Regards&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: Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/thread/202349?ContentTypeID=1</link><pubDate>Mon, 05 Aug 2019 08:52:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1262778-3d09-4b8d-bdc8-36b132c8ea3c</guid><dc:creator>Ali Rumane</dc:creator><description>&lt;p&gt;Hi,&lt;br /&gt;Thank you for your help.&lt;/p&gt;
[quote userid="13562" url="~/f/nordic-q-a/50057/ring-buffer-or-queue-for-different-datatype/199859"]&lt;br /&gt;I suggest you read up on &lt;strong&gt;Type Casting&lt;/strong&gt; and &lt;strong&gt;Pointer Type Casting&lt;/strong&gt; in C as it is fundamental in understanding your problem and its solution.[/quote]
&lt;p&gt;Sure, I will be doing the same.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/thread/199859?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2019 14:52:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5f1c3c1-9fd4-47a3-a169-72ec977058ca</guid><dc:creator>haakonsh</dc:creator><description>[quote user="Ali Rumane"]The nrf_ringbuf_cpy_put API accepts data as uint8_t, so I can&amp;#39;t put any other datatype like uint16_t or something so how will it work for my application?[/quote]
&lt;p&gt;&amp;nbsp;You store an array of data no matter what type it is. Whether you access the data as uint8 or int16 won&amp;#39;t change the data itself.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;I suggest you read up on &lt;strong&gt;Type Casting&lt;/strong&gt; and &lt;strong&gt;Pointer Type Casting&lt;/strong&gt; in C as it is fundamental in understanding your problem and its solution.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;see f.ex.&amp;nbsp;&lt;a href="https://stackoverflow.com/questions/17260527/what-are-the-rules-for-casting-pointers-in-c/17260931"&gt;https://stackoverflow.com/questions/17260527/what-are-the-rules-for-casting-pointers-in-c/17260931&lt;/a&gt;&lt;/p&gt;
[quote user="Ali Rumane"]Let&amp;#39;s say I want to store 5 bytes data so do I deliberately need to store 0 or some other value so that data becomes 6 which is multiple of 2 and ignore while retrieving it?[/quote]
&lt;p&gt;&amp;nbsp;Int16&amp;#39;s are exactly two bytes of data. If you want to store 5 uint8&amp;#39;s then you will need 5x1 bytes, but if you want to store 5 int16&amp;#39;s as uint8&amp;#39;s it will require 5x2 bytes.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/thread/199852?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2019 14:30:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b290d17e-1254-4712-9dbf-22449a511625</guid><dc:creator>Ali Rumane</dc:creator><description>&lt;p&gt;Thank you for your response.&lt;/p&gt;
&lt;p&gt;I am little confused;&lt;/p&gt;
[quote userid="13562" url="~/f/nordic-q-a/50057/ring-buffer-or-queue-for-different-datatype/199754"]data is&amp;nbsp;accessed as uint8 or int16 does not really matter[/quote]
&lt;p&gt;The nrf_ringbuf_cpy_put API accepts data as uint8_t, so I can&amp;#39;t put any other datatype like uint16_t or something so how will it work for my application?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ret_code_t nrf_ringbuf_cpy_put(nrf_ringbuf_t const * p_ringbuf,
                               uint8_t const * p_data,
                               size_t * p_length);&lt;/pre&gt;&lt;/p&gt;
[quote userid="13562" url="~/f/nordic-q-a/50057/ring-buffer-or-queue-for-different-datatype/199754"]read and write to the ring buffer in multiples of two bytes[/quote]
&lt;p&gt;Let&amp;#39;s say I want to store 5 bytes data so do I deliberately need to store 0 or some other value so that data becomes 6 which is multiple of 2 and ignore while retrieving it?&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Ring buffer or queue for different datatype</title><link>https://devzone.nordicsemi.com/thread/199754?ContentTypeID=1</link><pubDate>Mon, 22 Jul 2019 11:29:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6fb9ca41-94a0-4357-8f5a-f0c5f8909db7</guid><dc:creator>haakonsh</dc:creator><description>&lt;p&gt;From the queue buffer docs: &amp;quot;&lt;span&gt;The queue library provides interrupt secure implementation of the circular buffer to store predefined objects&amp;quot;.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;Whether your data is&amp;nbsp;accessed as uint8 or int16 does not really matter as long as you read and write to the ring buffer in multiples of two bytes, the data is still there.&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>