<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://test-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>nrf9160 uart0</title><link>https://test-devzone.nordicsemi.com/f/nordic-q-a/88657/nrf9160-uart0</link><description>Hi: 
 I tested that uart0 can&amp;#39;t be used. 
 I want to use uart_tx( uart, tx, 10, 0); to print &amp;quot;1234567890&amp;quot;. 
 But UART0 is not output.Shown below:</description><dc:language>en-US</dc:language><generator>Telligent Community 13 Non-Production</generator><lastBuildDate>Thu, 09 Jun 2022 14:52:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://test-devzone.nordicsemi.com/f/nordic-q-a/88657/nrf9160-uart0" /><item><title>RE: nrf9160 uart0</title><link>https://test-devzone.nordicsemi.com/thread/371707?ContentTypeID=1</link><pubDate>Thu, 09 Jun 2022 14:52:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fb5b8e8a-a4ce-402c-8ecc-2ba8c95ec7ec</guid><dc:creator>user72692</dc:creator><description>&lt;p&gt;&lt;a href="https://test-devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/uart_5F00_sample_5F00_v2.zip"&gt;test-devzone.nordicsemi.com/.../uart_5F00_sample_5F00_v2.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Try the sample above. I tested it and the data did not get garbled. If I commented out&amp;nbsp;k_sem_take() in uart_example() the data got garbled however.&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 uart0</title><link>https://test-devzone.nordicsemi.com/thread/371534?ContentTypeID=1</link><pubDate>Thu, 09 Jun 2022 05:41:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0ba0aeb4-1aeb-42cd-98d2-2234cfb54acb</guid><dc:creator>user73478</dc:creator><description>&lt;p&gt;hi&amp;nbsp;&lt;span&gt;Simon:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I tried to use thread, but it was still garbled.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;#define STACKSIZE 1024

#define THREAD0_PRIORITY 7
#define THREAD1_PRIORITY 7

void thread0(void)
{
	const struct device *dev;
	bool led_is_on = true;
	int ret;

	dev = device_get_binding(LED0);
	if (dev == NULL) {
		return;
	}

	ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
	if (ret &amp;lt; 0) {
		return;
	}

	while (1) {
		gpio_pin_set(dev, PIN, (int)led_is_on);
		led_is_on = !led_is_on;
		k_msleep(SLEEP_TIME_MS);
	}
}

void thread1(void)
{
	uart_init();
	uart_example();
	while (1) {
		k_msleep(SLEEP_TIME_MS);
	}
}

/* STEP 4 - Define and initialize the two threads */
K_THREAD_DEFINE(thread0_id, STACKSIZE, thread0, NULL, NULL, NULL,
		THREAD0_PRIORITY, 0, 0);
K_THREAD_DEFINE(thread1_id, STACKSIZE, thread1, NULL, NULL, NULL,
		THREAD1_PRIORITY, 0, 0);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I tried again semaphore,&amp;nbsp;but it was still garbled.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;static K_SEM_DEFINE(uarte_tx_finished, 0, 1);

static uint8_t uart_rx_buf[1024];

static void uart_callback(const struct device *dev,
			  struct uart_event *evt,
			  void *user_data)
{
	uint8_t *p;
	int len;
	switch (evt-&amp;gt;type) 
	{
	case UART_TX_DONE:
		k_sem_give(&amp;amp;uarte_tx_finished);
		break;
	case UART_TX_ABORTED:
		k_sem_give(&amp;amp;uarte_tx_finished);
		break;	
	case UART_RX_RDY:
		len = evt-&amp;gt;data.rx.len;
		p = evt-&amp;gt;data.rx.buf + evt-&amp;gt;data.rx.offset;
		break;	
	case UART_RX_BUF_REQUEST:
		break;			
	case UART_RX_BUF_RELEASED:
		break;
	case UART_RX_DISABLED:
		break;	
	case UART_RX_STOPPED:
		break;
	}
}

static int user_uart_rx_enable(void)
{
	int err;
	err = uart_callback_set(uart, uart_callback, (void *)NULL);
	if (err) {
	}

	err = uart_rx_enable(uart, uart_rx_buf, sizeof(uart_rx_buf),
			     5 * USEC_PER_MSEC);
	if (err) {
	} else {
	}
	return err;
}

void uart_init(void)
{
	uart = device_get_binding(&amp;quot;UART_0&amp;quot;);
	k_sem_give(&amp;amp;uarte_tx_finished);
	user_uart_rx_enable();
}

void uart_example(void)
{
	int ret;
    uint8_t tx[]=&amp;quot;1234567890&amp;quot;;
	printk(&amp;quot;UART loopback start!\n&amp;quot;);
	k_sem_take(&amp;amp;uarte_tx_finished, K_FOREVER);
    ret = uart_tx( uart, tx, 10, 0);
	if (ret) {
		k_sem_give(&amp;amp;uarte_tx_finished);
	}	
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 uart0</title><link>https://test-devzone.nordicsemi.com/thread/371377?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2022 11:36:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4d4a7dd2-45a7-48fe-8034-60af7e34a349</guid><dc:creator>user72692</dc:creator><description>&lt;p&gt;If you look at the &lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.7.99-ncs1-1/include/drivers/uart.h#L507-L508"&gt;explanatory text above uart_tx()&lt;/a&gt;&amp;nbsp;you can see that the function returns immediately.&amp;nbsp;I think the issue then is that gpio_pin_set and k_msleep is called from the same thread (main), and I think it it will interrupt and&amp;nbsp;corrupt the output. (I will take a closer look at this and get back to you)&lt;/p&gt;
&lt;p&gt;If you set up a callback, you can get notified when uart_tx is completed. Check out how it&amp;#39;s done in ppp.c for example, where a semaphore is used to make it wait until it&amp;#39;s finished:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.7.99-ncs1-1/drivers/net/ppp.c#L377"&gt;Call uart_tx() and waiting for it to get finished&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/nrfconnect/sdk-zephyr/blob/v2.7.99-ncs1-1/drivers/net/ppp.c#L125"&gt;Release the semaphore when the tx transaction is completed&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You could also try to put the uart_tx call into an own thread. Check out the DevAcademy to learn how to make your own threads:&amp;nbsp;&lt;a href="https://academy.nordicsemi.com/lessons/lesson-7-multithreaded-applications/"&gt;https://academy.nordicsemi.com/lessons/lesson-7-multithreaded-applications/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Simon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 uart0</title><link>https://test-devzone.nordicsemi.com/thread/371269?ContentTypeID=1</link><pubDate>Wed, 08 Jun 2022 06:14:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d624bf49-79f8-427e-8f79-6d6e9dd0393f</guid><dc:creator>user73478</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Simon:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Use&amp;nbsp;nrf9160dk_nrf9160_ns&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;After I modify prj.conf.It outputs garbled.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img src="https://test-devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/_014F1A4EAE5FE14F2A62FE565F00_16546603584826.png" alt=" " /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;zephyr.h&amp;gt;
#include &amp;lt;device.h&amp;gt;
#include &amp;lt;devicetree.h&amp;gt;
#include &amp;lt;drivers/gpio.h&amp;gt;
#include &amp;lt;logging/log.h&amp;gt;
#include &amp;lt;logging/log_ctrl.h&amp;gt;
#include &amp;quot;user_uart.h&amp;quot;

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   1000

/* The devicetree node identifier for the &amp;quot;led0&amp;quot; alias. */
#define LED0_NODE DT_ALIAS(led0)

#if DT_NODE_HAS_STATUS(LED0_NODE, okay)
#define LED0	DT_GPIO_LABEL(LED0_NODE, gpios)
#define PIN	DT_GPIO_PIN(LED0_NODE, gpios)
#define FLAGS	DT_GPIO_FLAGS(LED0_NODE, gpios)
#else
/* A build error here means your board isn&amp;#39;t set up to blink an LED. */
#error &amp;quot;Unsupported board: led0 devicetree alias is not defined&amp;quot;
#define LED0	&amp;quot;&amp;quot;
#define PIN	0
#define FLAGS	0
#endif

void main(void)
{

	const struct device *dev;
	bool led_is_on = true;
	int ret;

	dev = device_get_binding(LED0);
	if (dev == NULL) {
		return;
	}

	ret = gpio_pin_configure(dev, PIN, GPIO_OUTPUT_ACTIVE | FLAGS);
	if (ret &amp;lt; 0) {
		return;
	}

	uart_init();

	uart_example();
	
	while (1) {
		gpio_pin_set(dev, PIN, (int)led_is_on);
		led_is_on = !led_is_on;
		k_msleep(SLEEP_TIME_MS);
	}
}
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I found that uf the following is deleted,UART can output normally.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;		gpio_pin_set(dev, PIN, (int)led_is_on);
		led_is_on = !led_is_on;
		k_msleep(SLEEP_TIME_MS);&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;What causes this?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nrf9160 uart0</title><link>https://test-devzone.nordicsemi.com/thread/371200?ContentTypeID=1</link><pubDate>Tue, 07 Jun 2022 14:28:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0683beb9-bf0d-493e-aef8-28ac1f4cb4c4</guid><dc:creator>user72692</dc:creator><description>&lt;p&gt;I was able to make uart_tx work, check out&amp;nbsp;it out here:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://test-devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/2514.uart_5F00_sample.zip"&gt;test-devzone.nordicsemi.com/.../2514.uart_5F00_sample.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I think the issue is that you have probably set&amp;nbsp;&lt;span&gt;CONFIG_UART_INTERRUPT_DRIVEN=y, and uart_tx will not work when that is the case.&lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;span&gt;As mentioned in &lt;a href="https://test-devzone.nordicsemi.com/f/nordic-q-a/75340/sdkv1-5-1-blinky-example-with-uart/312364#312364"&gt;this reply&lt;/a&gt;, uart_tx is supported if you use&amp;nbsp;CONFIG_UART_ASYNC_API.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I have made an async uart loopback sample here (inspired by ..\zephyr\tests\drivers\uart\uart_async_api):&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;a href="https://github.com/simon-iversen/ncs_samples/tree/master/uart_async"&gt;https://github.com/simon-iversen/ncs_samples/tree/master/uart_async&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If you use nrf9160dk_nrf9160, &lt;a href="https://github.com/simon-iversen/ncs_samples/blob/eb7fa36387783f8faf94b7861f144acf9f994462/uart_async/src/test_uart.h#L22"&gt;uart1 will be used&lt;/a&gt;. Connect together tx and rx of uart1 to make it work.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Simon&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>