<?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>UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/101163/uart-async-api-with-fifo-workqueue</link><description>Hi 
 I&amp;#39;m using the UART async API and want to pass the received data to a fifo or workqueue to them process this received data. 
 
 
 There does not seem to be any examples of doing this with the async API, so where can i find something ? 
 
 KR</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 28 Jun 2023 08:56:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/101163/uart-async-api-with-fifo-workqueue" /><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433399?ContentTypeID=1</link><pubDate>Wed, 28 Jun 2023 08:56:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d595954e-7a43-4c0e-bf96-bd562545e689</guid><dc:creator>sleeper</dc:creator><description>&lt;p&gt;yes you can close this, thankyou.&lt;/p&gt;
&lt;p&gt;PS i have a new case here&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/101301/using-nrf-spi-flash-driver"&gt;devzone.nordicsemi.com/.../using-nrf-spi-flash-driver&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433394?ContentTypeID=1</link><pubDate>Wed, 28 Jun 2023 08:46:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b992d08f-47fd-4c31-981c-526afb5a20e9</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Good.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Don&amp;#39;t hesitate to create a new ticket if you are facing any problems.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Is it okay to you to close this case now?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433384?ContentTypeID=1</link><pubDate>Wed, 28 Jun 2023 08:17:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b0ed95a9-e9be-48f3-bd3d-be623af4ce0e</guid><dc:creator>sleeper</dc:creator><description>&lt;p&gt;gnss_init() is called from C main.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433364?ContentTypeID=1</link><pubDate>Wed, 28 Jun 2023 07:00:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d11c89e-fdfd-4c10-a014-37a174f19b24</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It looks fine to me other then I can&amp;#39;t see that you are calling your init functions anywhere.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433262?ContentTypeID=1</link><pubDate>Tue, 27 Jun 2023 13:20:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:356bafb7-315b-4cdf-8ba7-f2d875710d2c</guid><dc:creator>sleeper</dc:creator><description>&lt;p&gt;this is my code, it seems to work, can you check to see if you think i&amp;#39;m missing anything ?&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/drivers/uart.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;zephyr/sys/ring_buffer.h&amp;gt;

#include &amp;lt;zephyr/usb/usb_device.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;

#include &amp;lt;zephyr/drivers/gpio.h&amp;gt;
#include &amp;lt;zephyr/sys/util.h&amp;gt;

//https://github.com/too1/ncs-uart-async-count-rx/blob/master/src/main.c

#define UART_BUF_SIZE			500
//#define UART_TX_TIMEOUT_MS		100
#define UART_RX_TIMEOUT_MS		100*1000
#define UART_RX_MSG_QUEUE_SIZE	8

#define GNSS_THREAD_PRIORITY 	2
#define GNSS_THREAD_STACK_SIZE 	1024

LOG_MODULE_REGISTER(gnss, LOG_LEVEL_DBG);

static const struct device *uart = 
	DEVICE_DT_GET(DT_NODELABEL(uart0));

static uint8_t uart_double_buffer[2][UART_BUF_SIZE];
static uint8_t *uart_buf_next = uart_double_buffer[0];
static uint8_t *uart_buf_prev = uart_double_buffer[1];

struct uart_msg_queue_item {
	uint8_t bytes[UART_BUF_SIZE];
	uint32_t length;
};

const struct uart_config uart_cfg = {
	.baudrate = 9600,
	.parity = UART_CFG_PARITY_NONE,
	.stop_bits = UART_CFG_STOP_BITS_1,
	.data_bits = UART_CFG_DATA_BITS_8,
	.flow_ctrl = UART_CFG_FLOW_CTRL_NONE
};

static void gnss_thread(void *unused1, void *unused2, void *unused3);
static void uart_callback_handler(const struct device *dev, struct uart_event *evt, void *user_data);

//K_SEM_DEFINE(rx_disabled, 0, 1);
K_MSGQ_DEFINE(uart_rx_msgq, sizeof(struct uart_msg_queue_item), UART_RX_MSG_QUEUE_SIZE, 4);
K_THREAD_DEFINE(gnss_thread_id, GNSS_THREAD_STACK_SIZE, gnss_thread, NULL, NULL, NULL, GNSS_THREAD_PRIORITY, 0, 0);

void gns_init(void)
{
int result;


	if (!device_is_ready(uart)) 
	{
		LOG_DBG(&amp;quot;UART device not ready&amp;quot;);

		return;
	}

	result = uart_configure(uart, &amp;amp;uart_cfg);

	if (result != 0) 
	{
		LOG_DBG(&amp;quot;UART configuration error %i&amp;quot;, result);

		return;
	}

	uart_callback_set(uart, uart_callback_handler, NULL);
	uart_rx_enable(uart, uart_double_buffer[0], UART_BUF_SIZE, UART_RX_TIMEOUT_MS);
}

static void uart_callback_handler(const struct device *dev, struct uart_event *evt, void *user_data)
{
int result;	
static int toggle = 0;
static struct uart_msg_queue_item new_message;


	ARG_UNUSED(dev);

	switch (evt-&amp;gt;type) 
	{
		case UART_TX_DONE:
			LOG_DBG(&amp;quot;tx done&amp;quot;);
			break;

		case UART_TX_ABORTED:
			LOG_DBG(&amp;quot;tx aborted&amp;quot;);
			break;

		case UART_RX_STOPPED:
			LOG_DBG(&amp;quot;rx stopped&amp;quot;);
			break;
			
		case UART_RX_DISABLED:
//			k_sem_give(&amp;amp;rx_disabled);

			LOG_DBG(&amp;quot;rx disabled&amp;quot;);
			break;

		default:
			LOG_ERR(&amp;quot;unhandled case!&amp;quot;);
			break;

		case UART_RX_BUF_RELEASED:
			uart_buf_prev = evt-&amp;gt;data.rx_buf.buf;

			LOG_DBG(&amp;quot;rx buffer release @ 0x%p size %i&amp;quot;, 
				   (void *)&amp;amp;uart_buf_prev, UART_BUF_SIZE);
			break;			

		case UART_RX_BUF_REQUEST:
			toggle = (toggle ^ 1);

			uart_buf_next = uart_double_buffer[toggle];

			LOG_DBG(&amp;quot;rx buffer request @ 0x%p size %i&amp;quot;, 
				   (void *)&amp;amp;uart_buf_next, UART_BUF_SIZE);

			uart_rx_buf_rsp(uart, uart_buf_next, UART_BUF_SIZE);
			break;

		case UART_RX_RDY:
			new_message.length = evt-&amp;gt;data.rx.len;

			memcpy(new_message.bytes, 
				  (evt-&amp;gt;data.rx.buf + evt-&amp;gt;data.rx.offset), 
				   evt-&amp;gt;data.rx.len);

			LOG_DBG(&amp;quot;rx ready %i byte(s) offset %i&amp;quot;,
                    evt-&amp;gt;data.rx.len, evt-&amp;gt;data.rx.offset);

			result = k_msgq_put(&amp;amp;uart_rx_msgq, &amp;amp;new_message, K_NO_WAIT);

			if(result != 0)
			{
				LOG_ERR(&amp;quot;rx message queue full!&amp;quot;);
			}
			break;
	}
}

void gnss_thread(void *unused1, void *unused2, void *unused3)
{
int result;
struct uart_msg_queue_item rx_message;


    ARG_UNUSED(unused1);
    ARG_UNUSED(unused2);
    ARG_UNUSED(unused3);

	//k_sem_take(&amp;amp;ble_init_ok, K_FOREVER);

	while(1)
	{
//		k_sleep(K_MSEC(2000));
//		LOG_DBG(&amp;quot;this is gnss_thread!&amp;quot;);

		result = k_msgq_get(&amp;amp;uart_rx_msgq, &amp;amp;rx_message, K_FOREVER);

		if(result == 0)
		{
			
			LOG_DBG(&amp;quot;reading %i byte(s) from buffer&amp;quot;, rx_message.length);

			LOG_HEXDUMP_DBG(&amp;amp;rx_message.bytes[0], rx_message.length, &amp;quot;&amp;quot;);
		}
	}
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433257?ContentTypeID=1</link><pubDate>Tue, 27 Jun 2023 13:12:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:03f536d8-79b5-4462-a2b4-748c08c2d850</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;From the top of my head, the include paths is wrong. I would recommend comparing them to one of the examples I have linked you. In NCS 2.0 Zephyr 3.0 was introduced and with it:&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;quot;All Zephyr public headers have been moved to include/zephyr, meaning they must be prefixed with &amp;lt;zephyr/...&amp;gt; when included. Because this change can potentially break many applications or libraries, CONFIG_LEGACY_INCLUDE_PATH is provided to allow using the old include path.&amp;quot;&lt;/p&gt;
&lt;p&gt;You might be lucky and that is the only thing you have to care about.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433251?ContentTypeID=1</link><pubDate>Tue, 27 Jun 2023 12:58:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:deb24d10-43f1-4357-bd2d-fe852df6f364</guid><dc:creator>sleeper</dc:creator><description>&lt;p&gt;What needs to be changed ?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433249?ContentTypeID=1</link><pubDate>Tue, 27 Jun 2023 12:55:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:82729910-7b37-444c-922d-327eeb31cb71</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Just remember is based on an old version of NCS so it will not work out of the box&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433247?ContentTypeID=1</link><pubDate>Tue, 27 Jun 2023 12:51:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5805859-483b-4c14-bbe4-8734ad036871</guid><dc:creator>sleeper</dc:creator><description>&lt;p&gt;i found this, much easier example, might be of interest to others.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;&lt;a href="https://github.com/too1/ncs-uart-async-count-rx/blob/master/src/main.c"&gt;github.com/.../main.c&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433236?ContentTypeID=1</link><pubDate>Tue, 27 Jun 2023 12:28:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8962dfcb-1c1e-4546-8897-5a906446f4b5</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can also check out the &lt;a href="https://github.com/nrfconnect/sdk-nrf/tree/main/samples/nrf5340/remote_shell"&gt;remote shell example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/433005?ContentTypeID=1</link><pubDate>Mon, 26 Jun 2023 13:58:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f5773f9-5418-44cc-a1e4-7ae0ed3445f5</guid><dc:creator>sleeper</dc:creator><description>&lt;p&gt;do you not have a simpler example ?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;i only need to worry about receive data. This data is from a GPS receiver, data should be out in fifo and then processed is worker/separate thread.&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: UART async API with fifo / workqueue</title><link>https://devzone.nordicsemi.com/thread/432936?ContentTypeID=1</link><pubDate>Mon, 26 Jun 2023 11:23:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7284c7f7-8eb7-459f-baf8-6ee6b12ac346</guid><dc:creator>runsiv</dc:creator><description>&lt;p&gt;Hi Lee,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;You can have a look at our &lt;a href="https://github.com/nrfconnect/sdk-nrf/tree/main/samples/bluetooth/peripheral_uart"&gt;peripheral_uart exampel&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here you can see we how it is putting the received datainto a&amp;nbsp;FIFO&amp;nbsp;&lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/main/samples/bluetooth/peripheral_uart/src/main.c#L182"&gt;https://github.com/nrfconnect/sdk-nrf/blob/main/samples/bluetooth/peripheral_uart/src/main.c#L182&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;
&lt;p&gt;Runar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>