<?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>nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/3650/nrf51822---exception-on-stack-overflow</link><description>Using: 
 
 nRF51822 
 GCC 4.8.4 
 Nordic Sdk 5.2.0 
 pure-gcc setup (Ole Morten) 
 newlib-nano and -flto 
 
 See also this question . 
 I&amp;#39;d defined some stack size within startup_nrf51.s and want to be informed if the stackpointer passes __StackLimit</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 28 Jan 2015 17:47:22 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/3650/nrf51822---exception-on-stack-overflow" /><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13255?ContentTypeID=1</link><pubDate>Wed, 28 Jan 2015 17:47:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ed7a8358-87bd-4f87-b9fb-a580b8343209</guid><dc:creator>los</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Nice topic.
In order to detect stack overflow or stack usage more dynamically, stack can be regularly filled with a given pattern for example in application loop, then it can be checked in next loop iteration.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void MemoryWatcher::paintStackNow(void)
{
    uint32_t *p = (uint32_t *) GET_SP();

    while(p &amp;gt;= &amp;amp;__StackLimit)
    {
        *p = FREE_MEM_PATTERN;
        p--;
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="https://github.com/Lahorde/memory_watcher"&gt;github.com/.../memory_watcher&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13252?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 09:31:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7fcc0a26-b111-4650-85a4-1b1b5c5742af</guid><dc:creator>Joe Merten</dc:creator><description>&lt;p&gt;I&amp;#39;d just tried it out but stuck (see Edit 2014-08-28 in my question).
Could you help me to find the right value for &lt;code&gt;NRF_MPU-&amp;gt;RLENR0&lt;/code&gt; and how to write it into?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13251?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 06:48:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65ad0942-e652-4fd0-be8e-148b0e38ddff</guid><dc:creator>Joe Merten</dc:creator><description>&lt;p&gt;Very thanks for that information. I&amp;#39;ll give it a try now.&lt;/p&gt;
&lt;p&gt;May that approach charged by a software patent (&lt;a href="http://www.google.com/patents/US20140006692"&gt;see here&lt;/a&gt;)? I think no, because the patent text sounds more like the separating from softdevice and application.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13254?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 06:00:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f70b29df-afc4-43c7-b100-24331e58c72d</guid><dc:creator>Joe Merten</dc:creator><description>&lt;p&gt;That&amp;#39;s the way I currently also do it (and I also filling the heap with a different pattern).
Actually, I&amp;#39;m far away from having stack usage problems. But If one occours, I think by producing a hardfault, there might be a way to identify a bit of callstack.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13253?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 05:39:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:418f2c73-6794-43c1-b7a5-6a59be64b7e5</guid><dc:creator>Nikita</dc:creator><description>&lt;p&gt;I don&amp;#39;t know how to do it with generating hardfault, but I just periodically check whether the stack overflowed or not and generate error if it is.&lt;/p&gt;
&lt;p&gt;For this I fill stack with 0xDEADBEEF in startup file and periodically check maximum stack usage.&lt;/p&gt;
&lt;p&gt;In reset handler:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Reset_Handler:
    .fnstart

/* Make sure ALL RAM banks are powered on */
    LDR     R0, =NRF_POWER_RAMON_ADDRESS
    LDR     R2, [R0]
    MOVS    R1, #NRF_POWER_RAMON_RAMxON_ONMODE_Msk
    ORRS    R2, R1
    STR     R2, [R0]

                 LDR     R0, =__StackTop
                 LDR     R1, =Stack_Size
                 LDR     R2, =0xDEADBEEF
.Fill:
                 SUBS    R0,#4
                 STR     R2, [R0]
                 SUBS    R1,#4
                 BNE     .Fill
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Get maximum stack usage:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;extern uint32_t __StackTop;
extern uint32_t __StackLimit;
uint32_t stack_overflow_debug(void)
{
    uint32_t stack_usage = 0;
    uint32_t offset = 0;
    uint32_t * value_addr = (uint32_t *) &amp;amp;__StackLimit;

    for (; offset &amp;lt;  ((uint32_t)&amp;amp;__StackTop - (uint32_t)&amp;amp;__StackLimit); offset=offset+4)
    {
        uint32_t new_val = *(value_addr + offset);
        if (new_val != 0xDEADBEEF )
        {
            break;
        }
    }
    stack_usage = ((uint32_t)&amp;amp;__StackTop - (uint32_t)&amp;amp;__StackLimit) - offset;

    return stack_usage;
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13250?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 05:35:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b3384f5d-105d-46cc-a6f9-9b38978138a1</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;Softdevice doesn&amp;#39;t touch them, CLENR0 is written as part of the softdevice upload procedure (or not even at all in recent softdevices) and I&amp;#39;ve never seen anything write RLENR0 ever. I&amp;#39;d stick it in the hex file upload and do it there.&lt;/p&gt;
&lt;p&gt;This is partly why I haven&amp;#39;t bothered.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13249?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 05:31:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:183d541d-1de1-48c1-b3b5-73588ee2a505</guid><dc:creator>Nikita</dc:creator><description>&lt;p&gt;Is it possible to change RLENR0/CLENR0? Isn&amp;#39;t softdevice changing this registers on start and block access to them (I can&amp;#39;t found any softdevice call to change them and in S110_SoftDevice_Specification_v1.3A.pdf it&amp;#39;s written that if softrevice is running access to MPU is restricted)?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - Exception on Stack Overflow</title><link>https://devzone.nordicsemi.com/thread/13248?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 02:46:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41ffb0af-0436-4d9d-a6d0-5726a244fc85</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;If you were feeling particularly clever you could try getting your linker map to organise your code so it looks like this ...&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;FLASH: [ Softdevice.. ][Your code ][ empty flash + pstorage ]&lt;/p&gt;
&lt;p&gt;RAM: [ Softdevice RAM ][ Thread Stack ][ ... empty ...][ Process Stack ][Heap]&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Then you set RLENR0 in the memory module to just before the Process Stack and make sure CLENR0 is set correctly too.&lt;/p&gt;
&lt;p&gt;Malloc - assuming you find out why yours appears not to be working properly, will then fail if you over allocate heap and return 0 and I believe that any code running in region 1 (above CLENR0, i.e. your code) will fail if it tries to write into R0 RAM, that should include pushing too much on the stack and descending it below RLENR0. According to section 8.1.2 of the manual, that should cause a hard fault. ( don&amp;#39;t know why nothing seems to generate MemManage faults on this chip but there you go).&lt;/p&gt;
&lt;p&gt;That also nicely decouples your stack from the softdevice one.&lt;/p&gt;
&lt;p&gt;I keep meaning to set CrosssWorks up to do this, it has all the symbols in the thumb_crt0.s file to set up the second stack pointer but just sort of can&amp;#39;t quite be bothered.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>