<?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>CONFIG_UART_ASYNC_API -&amp;gt; uart_tx() fails when call from K_THREAD</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76846/config_uart_async_api---uart_tx-fails-when-call-from-k_thread</link><description>Hello, 
 i have a function wich just send &amp;quot;hello world&amp;quot; via uart0. static otError SendUARTCommand(void) { volatile uint32_t ret; const uint8_t message[] = &amp;quot;Hello World! \n\r&amp;quot;; dk_set_led_on(DONGLE_RED_MULTI_LED); ret = uart_tx(UART0Handle, &amp;amp;message, sizeof</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 16 Jul 2021 13:00:13 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76846/config_uart_async_api---uart_tx-fails-when-call-from-k_thread" /><item><title>RE: CONFIG_UART_ASYNC_API -&gt; uart_tx() fails when call from K_THREAD</title><link>https://devzone.nordicsemi.com/thread/320435?ContentTypeID=1</link><pubDate>Fri, 16 Jul 2021 13:00:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5ff027b8-6ef8-4aca-b1a9-711628b2758f</guid><dc:creator>Hakon</dc:creator><description>[quote user="RAlexeev"]So, do I have 4 options to define a buffer variable for &lt;code&gt;uart_tx()&lt;/code&gt; function, which are listed below?[/quote]
&lt;p&gt;&amp;nbsp;Yes, all of these options are viable I believe. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: CONFIG_UART_ASYNC_API -&gt; uart_tx() fails when call from K_THREAD</title><link>https://devzone.nordicsemi.com/thread/319498?ContentTypeID=1</link><pubDate>Mon, 12 Jul 2021 00:28:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3499d116-0ef9-4572-952b-b92c9dd926ca</guid><dc:creator>RAlexeev</dc:creator><description>&lt;p&gt;Hi, Hakon! Thank you for the answer! It seems that I faced about the same problem, using &lt;code&gt;uart_tx()&lt;/code&gt; function. After making a &lt;code&gt;message&lt;/code&gt; variable as static the UART transmission started to work properly for me also. I had something like the code below. And UART (&lt;code&gt;uart_tx()&lt;/code&gt;) sent corrupted data to me until I made a variable &lt;code&gt;message&lt;/code&gt; as static.&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&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/uart.h&amp;gt;

#include &amp;lt;logging/log.h&amp;gt;
#define LOG_MODULE_NAME main
LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG);

static const struct device *uart_dev;

static void uart_cb(const struct device *dev, struct uart_event *evt, void *user_data)
{
    if (evt-&amp;gt;type == UART_TX_DONE) {
        LOG_DBG(&amp;quot;UART_TX_DONE: TX sent %d bytes&amp;quot;, evt-&amp;gt;data.tx.len);
    }
}

void main(void)
{
    int err;
    static uint8_t message[] = &amp;quot;Hello World!\r\n&amp;quot;;
    const struct uart_config uart_cfg = {
        .baudrate = 115200,
        .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
    };

    uart_dev = device_get_binding(DT_LABEL(DT_NODELABEL(uart0)));

    if (uart_dev == NULL) {
        LOG_ERR(&amp;quot;Failed to get UART device&amp;quot;);
    }

    if (uart_configure(uart_dev, &amp;amp;uart_cfg) != 0) {
        LOG_ERR(&amp;quot;Failed to configure UART&amp;quot;);
    }

    err = uart_callback_set(uart_dev, uart_cb, NULL);
    if (err &amp;lt; 0) {
        LOG_DBG(&amp;quot;Cannot set UART callback (err %d)&amp;quot;, err);
    }

    err = uart_tx(uart_dev, message, sizeof(message), SYS_FOREVER_MS);
    if (err &amp;lt; 0) {
        LOG_ERR(&amp;quot;Cannot send data over UART (err %d)&amp;quot;, err);
    } else {
        LOG_DBG(&amp;quot;Sent data&amp;quot;);
    }
}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So, do I have 4 options to define a buffer variable for &lt;code&gt;uart_tx()&lt;/code&gt; function, which are listed below?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;make it static inside the function and not const (as it is implemented in this example &lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/v1.6.0/samples/edge_impulse/data_forwarder/src/main.c#L82" rel="noopener noreferrer" target="_blank"&gt;https://github.com/nrfconnect/sdk-nrf/blob/v1.6.0/samples/edge_impulse/data_forwarder/src/main.c#L82&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;make it global and not const&lt;/li&gt;
&lt;li&gt;make it not static inside the function and not const, but wait until the end of transmission, using a semaphore, for example (as it is implemented in this example &lt;a href="https://github.com/zephyrproject-rtos/zephyr/blob/zephyr-v2.6.0/tests/drivers/uart/uart_async_api/src/test_uart_async.c#L71" rel="noopener noreferrer" target="_blank"&gt;https://github.com/zephyrproject-rtos/zephyr/blob/zephyr-v2.6.0/tests/drivers/uart/uart_async_api/src/test_uart_async.c#L71&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;allocate a buffer variable on the heap and release it in a callback function (as it is implemented in this example &lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/v1.6.0/applications/serial_lte_modem/src/slm_at_host.c#L86" rel="noopener noreferrer" target="_blank"&gt;https://github.com/nrfconnect/sdk-nrf/blob/v1.6.0/applications/serial_lte_modem/src/slm_at_host.c#L86&lt;/a&gt;)&lt;/li&gt;
&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: CONFIG_UART_ASYNC_API -&gt; uart_tx() fails when call from K_THREAD</title><link>https://devzone.nordicsemi.com/thread/318832?ContentTypeID=1</link><pubDate>Tue, 06 Jul 2021 14:57:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f5c9c5f-5eb1-427b-96d2-17184c866e61</guid><dc:creator>Hakon</dc:creator><description>[quote user="SoerenBirth"]May you can explain it to me?![/quote]
&lt;p&gt;&amp;nbsp;The array gets erased from the stack when it exits the scope, unless it is declared static. So if uart_tx is called asynchronously, then the data that was provided to uart_tx could be erased already when the uart is trying to process the data that was provided.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: CONFIG_UART_ASYNC_API -&gt; uart_tx() fails when call from K_THREAD</title><link>https://devzone.nordicsemi.com/thread/318433?ContentTypeID=1</link><pubDate>Fri, 02 Jul 2021 12:06:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1ccfc51c-e33d-440d-8cb0-97fe1ff4582c</guid><dc:creator>SoerenBirth</dc:creator><description>&lt;p&gt;No, sorry.&lt;br /&gt;&lt;br /&gt;I went on developing. &lt;br /&gt;&lt;br /&gt;Making the message &amp;quot;static&amp;quot; fixed the problem. May you can explain it to me?!&lt;/p&gt;
&lt;p&gt;This short message/ variable should not provoke a stack overflow.&lt;br /&gt;&lt;br /&gt;I ran into similar problems as i was using UDP packets. Local variables tend to be unstable? Making them static fixed it again.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: CONFIG_UART_ASYNC_API -&gt; uart_tx() fails when call from K_THREAD</title><link>https://devzone.nordicsemi.com/thread/318432?ContentTypeID=1</link><pubDate>Fri, 02 Jul 2021 11:58:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a4860186-5144-4788-9c26-3680981dec43</guid><dc:creator>Hakon</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;would it be possible to provide the full code and project files, so I can have a closer look?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>