<?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>main endless loop Timer Problem</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/8602/main-endless-loop-timer-problem</link><description>i am running this code on my nrf. 
 const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);


uint8_t flag;

/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
 //static</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 10 Aug 2015 07:42:50 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/8602/main-endless-loop-timer-problem" /><item><title>RE: main endless loop Timer Problem</title><link>https://devzone.nordicsemi.com/thread/31490?ContentTypeID=1</link><pubDate>Mon, 10 Aug 2015 07:42:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:60b4fca7-c2f4-4795-8e22-9c86bc1da68b</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;The normal start of any function (including main) starts ( with &lt;code&gt;{&lt;/code&gt; ) with a PUSH operation.
Where a bunch of register contents are pushed to the stack (to an address pointed by Stack Pointer, Register R13), then the address of next instruction to be executed is copied into link register and the start address of the function to be exxecuted is copied into PC(program counter) register, also called branch.&lt;/p&gt;
&lt;p&gt;Similarly when the function ends (with &lt;code&gt;}&lt;/code&gt; ), the compiler adds a POP instruction that would restore the PUSHED register values back to the register. This all the main registers will have the same value just as before the function was started.&lt;/p&gt;
&lt;p&gt;But main is not a normal function. If you go to arm_startup_nrf51.s (assuming you are running Keil) you see the that main is started by Reset_Handler&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Reset_Handler   PROC
                ...........
                ...........
                LDR     R0, =SystemInit
                BLX     R0
                LDR     R0, =__main
                BX      R0
                ENDP
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here there was no push operation done. This means that the Reset_handler never expects the main to return (end). If the last closing bracket &amp;quot; &lt;code&gt;}&lt;/code&gt; &amp;quot; in main is reached, then it will automatically mean a POP operation to the compiler. At this point the stack pointer is not pointing to any PUSHED registers because the &lt;code&gt;Reset_Handler&lt;/code&gt; did not perform a PUSH (because it expects main not to return). Thus all the registers get corrupted with garbage POP operation and also the PC starts to execute garbage instructions probably corrupting the memory and probably hard_faulting. That is why your timer interrupts were not called.&lt;/p&gt;
&lt;p&gt;So the end of main should have an endless loop for the main never to return.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>