<?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>NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/16356/nvic-does-not-tail-chain-to-pendsv-isr</link><description>I&amp;#39;m trying to implement context switching from handler mode to thread mode using PendSV. I set the PendSV pending bit inside an ISR with SCB-&amp;gt;ICSR = SCB-&amp;gt;ICSR | SCB_ICSR_PENDSVSET_Msk . After setting the PendSV pending bit, I can see that the PendSV exception</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 15 Sep 2016 21:11:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/16356/nvic-does-not-tail-chain-to-pendsv-isr" /><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62556?ContentTypeID=1</link><pubDate>Thu, 15 Sep 2016 21:11:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4f9218e5-74bf-4f72-831f-c60e83187371</guid><dc:creator>fluent</dc:creator><description>&lt;p&gt;Great, thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62555?ContentTypeID=1</link><pubDate>Thu, 15 Sep 2016 13:30:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f9ad5139-3cb9-498e-9df6-9ea3a7a745c8</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;This is how i verified it.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;   void PendSV_Handler(void)
    {
          volatile register uint32_t r0 __asm(&amp;quot;r0&amp;quot;);
          volatile register uint32_t r1 __asm(&amp;quot;r1&amp;quot;);
          printf(&amp;quot;PendSV %x\n\r&amp;quot;,r0);
          printf(&amp;quot;PendSV %x\n\r&amp;quot;,r1);
    }

void TIMER1_IRQHandler(void)
{
     volatile register uint32_t r0 __asm(&amp;quot;r0&amp;quot;);
     volatile register uint32_t r1 __asm(&amp;quot;r1&amp;quot;);
    
    printf(&amp;quot;Timer1 %x\n\r&amp;quot;,r0);
    printf(&amp;quot;Timer1 %x\n\r&amp;quot;,r1);
    
    
    NRF_TIMER1-&amp;gt;INTENSET = 0;
    NVIC_DisableIRQ(TIMER1_IRQn);
    NVIC_ClearPendingIRQ(TIMER1_IRQn);

    SCB-&amp;gt;ICSR = SCB-&amp;gt;ICSR | SCB_ICSR_PENDSVSET_Msk;

    int i = 0xFFFF;
    while(i--);  

    __asm(&amp;quot;mov r0, #0xbeef&amp;quot;);
    __asm(&amp;quot;mov r1, #0xdead&amp;quot;);
}



/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err_code;

    simple_uart_config(0, TX_PIN_NUMBER, 0, RTS_PIN_NUMBER, false); // Hardware flow control not used in this example.
      
    printf(&amp;quot;\n\rStart: \n\r&amp;quot;);

    NVIC_SetPriority(TIMER1_IRQn, 2);
    NVIC_EnableIRQ(TIMER1_IRQn);
    NVIC_SetPriority(PendSV_IRQn, 0xFF);
    NVIC_EnableIRQ(PendSV_IRQn);
    NVIC_SetPendingIRQ(TIMER1_IRQn);
      
    while(1);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I set 0xdeadbeef in R0 and R1 in the end of Timer1 ISR and i see that they retain on entry to PendSV, this can only happen with tail chaining&lt;/p&gt;
&lt;h2&gt;OUTPUT&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;Start:
Timer1 0
Timer1 e000e200
PendSV beef
PendSV dead
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62554?ContentTypeID=1</link><pubDate>Thu, 15 Sep 2016 08:18:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b9acb7e-bcc6-4a30-a5bf-3e7d28016e1c</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;yes, one more time , mislead by the debugger&amp;#39;s inability to show us exactly what is happening under the hood.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62553?ContentTypeID=1</link><pubDate>Thu, 15 Sep 2016 00:45:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:023df7e2-c838-4c31-9064-93f27591a1ba</guid><dc:creator>fluent</dc:creator><description>&lt;p&gt;Thanks again, Aryan! I&amp;#39;m still having problems completing my context switch without hard-faulting, but it does look like tail-chaining is fine and my problems lie somewhere else.&lt;/p&gt;
&lt;p&gt;Still interested in hearing about how you were able to confirm tail-chaining on your end.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62552?ContentTypeID=1</link><pubDate>Thu, 15 Sep 2016 00:43:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5ba2a1d0-be97-427a-98d4-96e372e0809c</guid><dc:creator>fluent</dc:creator><description>&lt;p&gt;Then I rebuild the program and in the PendSV exception handler I &lt;code&gt;PUSH {R0, LR}&lt;/code&gt; then call a function which also enters a &lt;code&gt;while (true)&lt;/code&gt; loop. And the stack looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;0x2000fe88: 0x2000feb8  0x0002e10d  0x00000038  0xfffffff9
0x2000fe98: 0x20007899  0x00000000  0x00000000  0x20007899
0x2000fea8: 0x000007ff  0x000266bf  0x00026404  0x21000000
0x2000feb8: 0x00000000  0x20007b40  0x00000000  0x00000000
0x2000fec8: 0x20007bbc  0x0000eb04  0x00000001  0x00000000
0x2000fed8: 0x0000eb04  0x00000001  0x20007b98  0x20007c3c
0x2000fee8: 0x2000ff08  0x00024b0d  0x20007b98  0x00000000
0x2000fef8: 0x20007898  0x20007899  0x00000001  0x00000002
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now those two words at the top of the previous stack have been replaced by four words - 2 from the PendSV handler and 2 from my C function.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62551?ContentTypeID=1</link><pubDate>Thu, 15 Sep 2016 00:40:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5eb0b039-863b-4673-bd42-cafc67788505</guid><dc:creator>fluent</dc:creator><description>&lt;p&gt;I think you&amp;#39;re right. I placed a &lt;code&gt;while (true) continue&lt;/code&gt; in the first exception handler and this is what the stack looks like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;0x2000fe90: 0x2000feb8  0xfffffff9  0x20007899  0x00000000
0x2000fea0: 0x00000000  0x20007899  0x000007ff  0x000266bf
0x2000feb0: 0x00026404  0x21000000  0x00000000  0x20007b40
0x2000fec0: 0x00000000  0x00000000  0x20007bbc  0x0000eb04
0x2000fed0: 0x00000001  0x00000000  0x0000eb04  0x00000001
0x2000fee0: 0x20007b98  0x20007c3c  0x2000ff08  0x00024b0d
0x2000fef0: 0x20007b98  0x00000000  0x20007898  0x20007899
0x2000ff00: 0x00000001  0x00000002  0x00000000  0x20007798
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The two words at the top of the stack were pushed by the exception handler.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62549?ContentTypeID=1</link><pubDate>Wed, 14 Sep 2016 15:14:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:25d744bb-4c3a-411d-b47e-7f3bc967fb78</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;There is tail chaining happening.  Debugger breakpoints is just messing up the timings. I&amp;#39;ll show you tomorrow how i verified,  now im already home and typing on phone is not very effective.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62550?ContentTypeID=1</link><pubDate>Wed, 14 Sep 2016 01:34:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:16a65e8d-e095-4800-a95a-659011c7a20e</guid><dc:creator>fluent</dc:creator><description>&lt;p&gt;Thanks, Aryan, for replicating the problem and responding here. I was wondering the same thing, if the debugger (I&amp;#39;m using GDB) is unable to monitor tail-chaining as it happens. I was thinking of setting a breakpoint in the PendSV handler, running to the breakpoint and comparing the stack to what I think it should be. Alternatively, I could try printing and comparing the value of the stack pointer (MSP) in both exception handlers. I&amp;#39;ll let you know what I find.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: NVIC does not tail-chain to PendSV ISR</title><link>https://devzone.nordicsemi.com/thread/62548?ContentTypeID=1</link><pubDate>Tue, 13 Sep 2016 12:45:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7b349fc2-adde-4928-a16d-9dcd65ea49de</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I am confused here, i tested this and also found that tail chaining is not happening not only with PendSV but with any interrupt... but this is a ARM Cortex feature right. You are writing directly to SCB block and not using any of the NRF features. I will have to look more into this tomorrow but I am not sure how much i should trust the debugger on this. Is there any other way you verify this without using debugger/breakpoints&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>