<?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>[S110][SDK10][PCA10028] Calling uart function inside timer event handler</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/12285/s110-sdk10-pca10028-calling-uart-function-inside-timer-event-handler</link><description>Hi,
I&amp;#39;m using a timer to send message over uart.
Here is the initialization code: 
 err_code = app_timer_create(&amp;amp;m_uart_timeout_timer, APP_TIMER_MODE_SINGLE_SHOT, uart_timeout_handler);
APP_ERROR_CHECK(err_code);

err_code = app_timer_create(&amp;amp;m_cmd_timer</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 04 Mar 2016 16:09:49 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/12285/s110-sdk10-pca10028-calling-uart-function-inside-timer-event-handler" /><item><title>RE: [S110][SDK10][PCA10028] Calling uart function inside timer event handler</title><link>https://devzone.nordicsemi.com/thread/46438?ContentTypeID=1</link><pubDate>Fri, 04 Mar 2016 16:09:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f41a5642-4e33-4e40-8b91-ee884911bd7e</guid><dc:creator>Lalit Kumar</dc:creator><description>&lt;p&gt;Your edit explains it well. Thanks for help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [S110][SDK10][PCA10028] Calling uart function inside timer event handler</title><link>https://devzone.nordicsemi.com/thread/46437?ContentTypeID=1</link><pubDate>Thu, 03 Mar 2016 06:35:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f99e4f4b-9110-4a78-af12-4b88a27990b6</guid><dc:creator>Lalit Kumar</dc:creator><description>&lt;p&gt;Hi,
I did according to what you told and it is working fine.
Also I tried a bit different code that is working as well. Here is the snippet:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void send_cmd_handler(void *p_context)
{
uint32_t err_code;

txd_data = (uint8_t*)calloc(3, sizeof(uint8_t));
uint8_t cmd[] = &amp;quot;abc&amp;quot;;

memcpy(&amp;amp;txd_data[0], cmd, strlen(cmd));

err_code = nrf_drv_uart_tx(txd_data, 3);
APP_ERROR_CHECK(err_code);

err_code = app_timer_start(m_uart_timeout_timer, UART_TIMEOUT, NULL);
APP_ERROR_CHECK(err_code);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I have defined txd_data as a global pointer. Is there any reason why this is working and the previous one wasn&amp;#39;t?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: [S110][SDK10][PCA10028] Calling uart function inside timer event handler</title><link>https://devzone.nordicsemi.com/thread/46436?ContentTypeID=1</link><pubDate>Wed, 02 Mar 2016 15:42:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71a061b8-fb39-4a5b-9625-2f5ec4254d93</guid><dc:creator>MartinBL</dc:creator><description>&lt;p&gt;Hi&lt;/p&gt;
&lt;p&gt;You should avoid starting a UART TX inside a timer callback. Try to make your callback routines as short as possible. Inside &lt;code&gt;nrf_drv_uart_tx()&lt;/code&gt; there is a lot of code, while() loops, and invocations of more callbacks. Try to set a flag in the timer callback instead. Then do your thing in the main while() loop when the flag is set. You can also look into more advanced solutions like the &lt;a href="https://devzone.nordicsemi.com/tutorials/23/"&gt;scheduler&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;EDIT: I might have phrased my self a little inaccurate in my initial answer. The problem in your first code is that you define &lt;code&gt;tx_data&lt;/code&gt; locally as an automatic variable. Hence the data is stored on the stack. Once the &lt;code&gt;send_cmd_handler()&lt;/code&gt; handler is completed, the UART might still be reading from your buffer on the stack. The image below shows a logic trace of what happens.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Channel 3: Clears once your application enters &lt;code&gt;send_cmd_handler()&lt;/code&gt;and goes high again when it exits.&lt;/li&gt;
&lt;li&gt;Channel 7: Shows that the UART is busy transmitting for a long time after the handler is completely executed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/8875.Untitled.png" alt="image description" /&gt;&lt;/p&gt;
&lt;p&gt;So the problem occurs when the &lt;code&gt;send_cmd_handler()&lt;/code&gt; is completely executed, because then the CPU thinks that all the memory on the stack is free and starts to store other application data on it. It might even overwrite your buffer which the UART is still busy reading from. What happens when you declare your buffer globally (as in the code in your comment below) is that the buffer is allocated some other place in memory (or the heep) where it is safe, even after the handler is complete.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>