<?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>Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/38837/softdevice-hardfault-when-calling-setenv</link><description>My application needs to keep time and support local time as well. I have implemented a service that allows setting the epoch seconds as well as a local timezone string. With these two items, I can update the epoch seconds and determine local using the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 08 Oct 2018 20:18:29 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/38837/softdevice-hardfault-when-calling-setenv" /><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/152120?ContentTypeID=1</link><pubDate>Mon, 08 Oct 2018 20:18:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:569f8bc0-5771-410d-ba20-967693a57b27</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Sounds good Mark, please let me know about the results.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/152117?ContentTypeID=1</link><pubDate>Mon, 08 Oct 2018 18:29:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:81825fc0-c90b-406f-a268-e566cbe10e15</guid><dc:creator>Mark Bianchi</dc:creator><description>&lt;p&gt;Aryan, thanks for the help thus far. I have been curious if it is a stack overflow memory corruption, as that seems to be a typical memory corruption problem with RTOS use (since it is not obvious what the appropriate stack size should be for each task). I will probably try bumping the stack size at some point to determine if&amp;nbsp;that was the problem.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/151764?ContentTypeID=1</link><pubDate>Fri, 05 Oct 2018 07:30:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:855620b2-5eb1-40f7-b40c-8c8fb5dae2ca</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;is it possible that you just ran out of configured max stack space? Sometimes it happens with some RTOS (which asks you to preconfigure the max stack space) that just one call is enough to cross the boundary. I would say that there is a good chance that this memory corruption problem will be fixed if you increased the task stack space. Atleast worth a try if you are still curious.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/151538?ContentTypeID=1</link><pubDate>Wed, 03 Oct 2018 17:04:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8cb3401a-45d2-4615-a8df-29cd1911be40</guid><dc:creator>Mark Bianchi</dc:creator><description>&lt;p&gt;Well as it turns out, the product definition has just changed so I no longer need to keep track of local timezone.&lt;/p&gt;
&lt;p&gt;However just as some follow-up, while trying to figure this problem out (still not solved) it appears that I am getting memory corruption and that is the source of the hardfault. I haven&amp;#39;t been able to isolate what exactly is going on, however it seems to be caused by setenv(), as I can comment out the call to setenv() and my app runs fine for a long time. If I keep the call to setenv() I consistently get a hardfault, however after much more code beyond setenv() has run (once the corrupted memory somehow comes into play).&lt;/p&gt;
&lt;p&gt;I was only able to determine that memory corruption is occurring due to strange output shown in the OpenRTOS Viewer Task Table window. This corruption is still a curiosity, however since timezones are no longer needed, I guess it will remain a curiosity. Thanks for your interest/help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/151403?ContentTypeID=1</link><pubDate>Wed, 03 Oct 2018 11:42:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01b72688-4679-40d9-b262-845aedb25872</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;sorry for the late answer.&lt;/p&gt;
&lt;p&gt;This is strange that hardfault occurs even if there is no SVC calls (which is the reason most of the times here). Maybe there is some memory access violation, hard to say.&lt;/p&gt;
&lt;p&gt;Can you help me reproduce this issue?&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/150779?ContentTypeID=1</link><pubDate>Thu, 27 Sep 2018 23:05:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f5c0d3d0-52cd-4e44-a7fd-a4816a57202e</guid><dc:creator>Mark Bianchi</dc:creator><description>&lt;p&gt;Well I don&amp;#39;t think there is any other SVC call involved, but I don&amp;#39;t know what setenv() does other than malloc&amp;#39;ing, which I guess might disable interrupts or some other locking mechanism like that.&lt;/p&gt;
&lt;p&gt;The context I am executing setenv() in is as I said above, the BLE event handler registered with NRF_SDH_BLE_OBSERVER() with an observer priority of 2. The code that executes on the characteristic write event is basically as follows:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// A snippet of BLE event handler in my timekeeping service
case BLE_GATTS_EVT_WRITE:
    ble_gatts_evt_write_t const * p_evt_write = &amp;amp;p_ble_evt-&amp;gt;evt.gatts_evt.params.write;
    if (p_evt_write-&amp;gt;handle == m_dcs.timezone.value_handle) {
        if (p_evt_write-&amp;gt;len &amp;lt;= 12) {
            set_local_timezone(p_evt_write-&amp;gt;data, p_evt_write-&amp;gt;len);
        } else {
            NRF_LOG_INFO(&amp;quot;write to TIMEZONE too long: %d&amp;quot;, p_evt_write-&amp;gt;len);
        }
    }
    break;
    
// The set_local_timezone() function contained in a separate file is basically:
static char m_timezone_str[13];

void set_local_timezone(char const * tz, size_t len) {
    memset(m_timezone_str, 0, sizeof(m_timezone_str));
    len = MIN(len, sizeof(m_timezone_str));
    strncpy(m_timezone_str, tz, len);
    setenv(&amp;quot;TZ&amp;quot;, m_timezone_str, 1);
    NRF_LOG_DEBUG(&amp;quot;TZ set: %s&amp;quot;, getenv(&amp;quot;TZ&amp;quot;));
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p class="p1"&gt;So I can&amp;#39;t see where another SVC call is at play here, unless I am missing something.&lt;/p&gt;
&lt;p class="p1"&gt;I have tried a few other ways around this problem to no avail. In the first attempt, instead of calling set_local_timezone() directly in the BLE event handler, I sent a message containing the written timezone char array to a task that is largely sitting idle. This task is blocked&amp;nbsp;on its message queue and when it receives the timezone update message, it executes set_local_timezone() it its task context. I was pretty confident that this solution would work, but it hardfaults as well. (I didn&amp;#39;t capture the CPU registers and fault address in this case, but I could and post it if it is useful to solving the problem).&lt;/p&gt;
&lt;p class="p1"&gt;I then tried setting a flag and&amp;nbsp;saving&amp;nbsp;the written timezone array of chars and upon disconnect (again handled by the BLE event handler on BLE_GAP_EVT_DISCONNECTED), if the flag was set I would call set_local_timezone() directly in the event handler, similar to the code above. This likewise causes a hardfault. Also tried this scheme of setting the timezone upon disconnection, but this time by sending a message to the other task, extremely similar to what I did above. This too ended in a hardfault.&lt;/p&gt;
&lt;p class="p1"&gt;In all of these cases, the call to setenv() seems to complete and some time later the hardfault occurs (RTT logging shows the setenv() completing). If I simply comment out the actual call to setenv() then I don&amp;#39;t ever see a hardfault. I&amp;nbsp;could post more code, but I think it will rapidly get unwieldy. The fact that I can execute set_local_timezone() from my console command line interface and never have it fail has me baffled.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Softdevice hardfault when calling setenv()</title><link>https://devzone.nordicsemi.com/thread/150278?ContentTypeID=1</link><pubDate>Tue, 25 Sep 2018 13:17:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2430ac45-4997-4b5c-bd89-4870c1fbf7a8</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I think the problem is the context from which you are calling setenv. If this in turn calls any other SVC call which is lower in priority than the context you are calling in. Then this would trigger a hardfault as per ARM Cortex M rules. It hard to say when there is not much code reference to look into.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>