<?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>async UART coming in broken packets</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/99442/async-uart-coming-in-broken-packets</link><description>Hello, 
 
 This is probably just me not knowing what I&amp;#39;m doing, but I could really use some help. I am trying to set up two UARTs on the nRF52840. We have a custom board but I am starting with the DK. I have gotten both UART0 and UART1 to work simultaneously</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 08 Nov 2023 19:55:24 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/99442/async-uart-coming-in-broken-packets" /><item><title>RE: async UART coming in broken packets</title><link>https://devzone.nordicsemi.com/thread/454780?ContentTypeID=1</link><pubDate>Wed, 08 Nov 2023 19:55:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:789a593b-309a-4c6d-8ef0-18dbc3470a05</guid><dc:creator>Nathan45</dc:creator><description>&lt;p&gt;Glad I could help!&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;God bless,&lt;/p&gt;
&lt;p&gt;Nathan&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: async UART coming in broken packets</title><link>https://devzone.nordicsemi.com/thread/454568?ContentTypeID=1</link><pubDate>Wed, 08 Nov 2023 05:05:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:467bf276-1a9e-469d-827b-01bae5513b9e</guid><dc:creator>Mike Austin (LPI)</dc:creator><description>&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/members/nathan45"&gt;Nathan45&lt;/a&gt;&amp;nbsp;- awesome! This is exactly what I&amp;#39;ve been trying to understand how to do, having stumbled across the same Github example.&amp;nbsp; You&amp;#39;ve saved me heaps of time/hassle.&lt;/p&gt;
&lt;p&gt;Not sure I fully understand what I&amp;#39;m doing, but I&amp;#39;ve got something working and so now I can play around with it and make sure I do&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Mike&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: async UART coming in broken packets</title><link>https://devzone.nordicsemi.com/thread/426163?ContentTypeID=1</link><pubDate>Thu, 18 May 2023 16:27:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:110cd711-033f-4ded-82e2-7865d9cd104a</guid><dc:creator>Nathan45</dc:creator><description>&lt;p&gt;For anyone with a similar issue, I found that working through the &lt;a href="https://github.com/too1/ncs-uart-async-count-rx"&gt;linked github example&lt;/a&gt; made UART work a LOT better and helped me to understand it better. In a nutshell:&lt;/p&gt;
&lt;p&gt;- Double buffering is important! don&amp;#39;t leave it out!&lt;/p&gt;
&lt;p&gt;- You don&amp;#39;t have to understand double buffering to implement it, as shown in the linked example it&amp;#39;s just a single line of code in the UART callback, and a few lines of setting up the buffer at the top of the file.&lt;/p&gt;
&lt;p&gt;- Message queues are important too! Those are worth understanding a bit more. I don&amp;#39;t think they are required, but they work really well in tandem with async UART, and thats what the linked example uses.&lt;/p&gt;
&lt;p&gt;- The linked example prints everything right away, but if you want the data in memory so you can perform operations on it you can start a thread that checks the message queue for new messages using the &amp;quot;k_msgq_get(...)&amp;quot; function. If this function returns true, it means that there is uart data ready to save to memory and you just save it and repeat basically. I added a basic snippet that I use (I modified the snippet from what we exactly use for intellectual property reasons, code may/maynot be broken but it should at least work as a starting point).&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/too1/ncs-uart-async-count-rx"&gt;https://github.com/too1/ncs-uart-async-count-rx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define UART0_INTERPRETER_STACKSIZE 512 //thread stuff
#define UART0_INTERPRETER_PRIORITY 7    //thread stuff

#define UART_BUF_SIZE 64

/ .
/ .
/ .

void uart_printer() {
	printk(&amp;quot;STARTING UART GRABBER THREAD&amp;quot;); 
	app_uart_send(&amp;quot;START\r\n&amp;quot;, strlen(&amp;quot;START\r\n&amp;quot;));
	int data_length;
	struct uart_msg_queue_item incoming_message;

	while (1) {
		//app_uart_send(&amp;quot;C&amp;quot;, 1);
		// This function will not return until a new message is ready
		if (k_msgq_get(&amp;amp;uart_rx_msgq, &amp;amp;incoming_message, K_NO_WAIT) == 0) {
			printk(&amp;quot;message detected\n&amp;quot;);
			uint8_t string_buffer[UART_BUF_SIZE] = &amp;quot;0&amp;quot;;
			uint8_t read_value[UART_BUF_SIZE] = &amp;quot;0&amp;quot;;
			uint8_t new_buffer[UART_BUF_SIZE] = &amp;quot;0&amp;quot;;
			data_length = 0;

			memcpy(string_buffer, incoming_message.bytes, incoming_message.length);
			data_length += incoming_message.length;
			string_buffer[data_length] = &amp;#39;\0&amp;#39;;
			// Read remaining bytes after initial read is started. append them to final string
			while (k_msgq_get(&amp;amp;uart_rx_msgq, &amp;amp;incoming_message, K_MSEC(50)) == 0) { //K_MSEC(x) time value = uart timeout sort of
				memcpy(new_buffer, incoming_message.bytes, incoming_message.length);
				//memcpy(new_buffer, &amp;quot;x&amp;quot;, 1);
				data_length += incoming_message.length;
				strcat(string_buffer, new_buffer);
				string_buffer[data_length] = &amp;#39;\0&amp;#39;;
			}
			printk(&amp;quot;message grabbed\n&amp;quot;);
			
			strncpy(read_value, string_buffer, strlen(string_buffer));
			//LOG_INF(&amp;quot;message = %s&amp;quot;, read_value);
			// HOORAY!! read value is now store in memory as read_value. Also as string_buffer.
			/////////////// PROCESS/HANDLE DATA BELOW //////////////////////
			
			/ .
			/ .
			/ .

			////////////////////////////////////////////////////////////////////////
		}
		k_msleep(10); //give other threads time
	}
}

/ .
/ .
/ .

K_THREAD_DEFINE(t_uart0_interpreter_id, UART0_INTERPRETER_STACKSIZE, uart_printer, NULL, NULL, NULL, UART0_INTERPRETER_PRIORITY, 0, 0);
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: async UART coming in broken packets</title><link>https://devzone.nordicsemi.com/thread/424281?ContentTypeID=1</link><pubDate>Mon, 08 May 2023 12:42:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0e62ac94-cf79-4cc3-a6ca-10d9ed5d65a2</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I think this for most part is &amp;quot;just&amp;quot; something you need to work through. The first I would have looked at is examples and driver test projects that use uart, but also check the documentation. For instance you are not feeding a buffer as recommended:&lt;br /&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/hardware/peripherals/uart.html#c.uart_event_type.UART_RX_BUF_REQUEST"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/hardware/peripherals/uart.html#c.uart_event_type.UART_RX_BUF_REQUEST&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Here is description of handling of events:&lt;br /&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/hardware/peripherals/uart.html#c.uart_event_type"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/hardware/peripherals/uart.html#c.uart_event_type&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>