<?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>Can i Use Ring Buffer from Zephyr in Uart Event Handler?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/81646/can-i-use-ring-buffer-from-zephyr-in-uart-event-handler</link><description>Hi, 
 Im using nrf9160 based custom board, nrf sdk 1.5.1. with Zephyr 
 Im using Uart in DMA mode but have small doubts in implementation 
 1. what is the max size of buffer for DMA in UART (i need at least 2048 buffer, so is that possible)? 
 2. I want</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 12 Nov 2021 09:00:11 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/81646/can-i-use-ring-buffer-from-zephyr-in-uart-event-handler" /><item><title>RE: Can i Use Ring Buffer from Zephyr in Uart Event Handler?</title><link>https://devzone.nordicsemi.com/thread/338702?ContentTypeID=1</link><pubDate>Fri, 12 Nov 2021 09:00:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:840799fd-848b-490b-8676-18e6c6035d99</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="Rajender"]That means i can use Semaphore lock/ mutex lock also in this Uart event handler right?[/quote]
&lt;p&gt;My apologies, I forgot to mention this initially;&lt;/p&gt;
&lt;p&gt;you should be very careful about locking semaphores in interrupts, as you shall always give a timeout that is not K_FOREVER:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/latest/reference/kernel/synchronization/semaphores.html#c.k_sem_take"&gt;https://docs.zephyrproject.org/latest/reference/kernel/synchronization/semaphores.html#c.k_sem_take&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you have multiple functions pushing data to this ring-buffer, you can consider moving logic from the interrupt to a workqueue item instead.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can i Use Ring Buffer from Zephyr in Uart Event Handler?</title><link>https://devzone.nordicsemi.com/thread/338699?ContentTypeID=1</link><pubDate>Fri, 12 Nov 2021 08:51:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:965ce14d-86e8-4a6d-bf2d-f4156ef038c3</guid><dc:creator>Rajender</dc:creator><description>&lt;p&gt;Hi Hakon,&lt;/p&gt;
[quote userid="2115" url="~/f/nordic-q-a/81646/can-i-use-ring-buffer-from-zephyr-in-uart-event-handler/338697#338697"]you guard the function with a mutex or similar, as described in the documentation for the function[/quote]
&lt;p&gt;That means i can use Semaphore lock/ mutex lock also in this Uart event handler right?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best Regards&lt;/p&gt;
&lt;p&gt;Rajender&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can i Use Ring Buffer from Zephyr in Uart Event Handler?</title><link>https://devzone.nordicsemi.com/thread/338697?ContentTypeID=1</link><pubDate>Fri, 12 Nov 2021 08:43:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10ca7f65-80a6-446d-a72d-e68d088ac9cc</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Rajender,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]1. what is the max size of buffer for DMA in UART (i need at least 2048 buffer, so is that possible)?[/quote]
&lt;p&gt;&amp;nbsp;The DMA count is 12 bits, so you can send/receive up to 4095 bytes:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://infocenter.nordicsemi.com/topic/ps_nrf9160/uarte.html?cp=2_0_0_5_18_8_47#register.TXD.MAXCNT"&gt;https://infocenter.nordicsemi.com/topic/ps_nrf9160/uarte.html?cp=2_0_0_5_18_8_47#register.TXD.MAXCNT&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
[quote user=""]2. I want to collect this Uart data in Ring Buffer(zephyr/..../sys/ring_buffer.h), can i directly implement ring_buf_put() in Uart Event Handler?, i have tested and its working fine, but i need confirmation that is it good practice or do we have any work arround?[/quote]
&lt;p&gt;Yes, but you have to ensure that if there&amp;#39;s multiple scenarios (ie. different functions) where you put data into the ring buffer, that you guard the function with a mutex or similar, as described in the documentation for the function:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/latest/reference/data_structures/ring_buffers.html#c.ring_buf_put"&gt;https://docs.zephyrproject.org/latest/reference/data_structures/ring_buffers.html#c.ring_buf_put&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user=""]3. can i use k_poll_signal_raise() in envent handler?[/quote]
&lt;p&gt;Yes, it should not be problematic.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kind regards,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>