<?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>MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/101556/mpu-fault-stack-guard-and-hard-faults</link><description>Hello, I&amp;#39;m using the nrf v2.3.0 SDK on a 5340 and 7200 platform. I&amp;#39;m trying to get hooks into some of the lower level fault handling. 
 Reading through dev zone tickets, it looks like STACK_SENTINEL is not needed if MPU is configured to be enabled. 
</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 17 Jul 2023 02:19:10 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/101556/mpu-fault-stack-guard-and-hard-faults" /><item><title>RE: MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/thread/436609?ContentTypeID=1</link><pubDate>Mon, 17 Jul 2023 02:19:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11ee527d-beb8-4a3d-a549-0b87bea29ff9</guid><dc:creator>wallyb</dc:creator><description>&lt;p&gt;thanks. &amp;nbsp;I was able to use some of the retained ram sample code as a start and can save fatal error information to no_init ram. &amp;nbsp; &amp;nbsp;&lt;br /&gt;I&amp;#39;m still fiddling with the task watchdog. I&amp;#39;ll send more information as I get it.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/thread/435683?ContentTypeID=1</link><pubDate>Tue, 11 Jul 2023 11:00:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e9e5dff7-f345-4386-8f2b-ff0793a3fc3a</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi Wally,&lt;/p&gt;
&lt;p&gt;Depending on the source of the error and where it occurred, the error handler may be invoked in an interrupt context. For instance, the hardfault IRQ&amp;nbsp;which will block all other interrupts in your system. There is also a note about not relying on RTOS primitives in the memfault implementation, which can be found here: &lt;a href="https://github.com/nrfconnect/sdk-nrf/blob/main/modules/memfault-firmware-sdk/memfault_flash_coredump_storage.c#L15"&gt;https://github.com/nrfconnect/sdk-nrf/blob/main/modules/memfault-firmware-sdk/memfault_flash_coredump_storage.c#L15&lt;/a&gt;.&amp;nbsp;RTOS primitives are probably being used in your serial driver.&lt;/p&gt;
&lt;p&gt;I believe it&amp;#39;s safest to store the error information in RAM if you have spare memory available, as I previously suggested in this post:&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/101071/saving-coredumps-to-external-flash/432299"&gt;RE: Saving coredumps to external flash&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/thread/435601?ContentTypeID=1</link><pubDate>Tue, 11 Jul 2023 03:44:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8cbe27b7-3093-4920-9a97-83ad5f845df4</guid><dc:creator>wallyb</dc:creator><description>&lt;p&gt;thank you. &amp;nbsp;I am able to override the fatal handler if I set&amp;nbsp;&lt;/p&gt;
&lt;div&gt;
&lt;div&gt;&lt;span&gt;CONFIG_RESET_ON_FATAL_ERROR=n so that the NCS lib does not override it.&lt;br /&gt;&lt;br /&gt;An issue that I can&amp;#39;t seem to get around is that I am only able to save assert information to my serial flash if I assert during a shell command, and I can&amp;#39;t save fatal error reasons at all. &amp;nbsp;Is this due to the context of a shell command being a more privileged mode? &amp;nbsp;&lt;br /&gt;Here is my assert_post_action function:&lt;br /&gt;&lt;pre class="ui-code" data-mode="text"&gt;void assert_post_action(const char *file, unsigned int line)
{
    struct fs_file_t assert_file;
    char path[MAX_PATH_LEN];
    char assert_msg[ASSERT_STR_LEN_MAX];

    /* Disable interrupts, this is unrecoverable */
    (void)irq_lock();

    struct fs_mount_t *mp = storage_mount_point_get();
    snprintf(path, sizeof(path), &amp;quot;%s/%s&amp;quot;, mp-&amp;gt;mnt_point, &amp;quot;assert.json&amp;quot;);
    fs_unlink(path); // delete old assert file if it exists

    // create JSON string with assert info, previous boot count, and system uptime
    sprintf(assert_msg, &amp;quot;{\&amp;quot;file\&amp;quot;:\&amp;quot;%s\&amp;quot;,&amp;quot;
                        &amp;quot;\&amp;quot;line\&amp;quot;:%d,&amp;quot;
                        &amp;quot;\&amp;quot;boot_count\&amp;quot;:%d,&amp;quot;
                        &amp;quot;\&amp;quot;uptime\&amp;quot;:%lld&amp;quot;
                        &amp;quot;}&amp;quot;,
            file,
            line,
            boot_count_get() - 1,
            k_uptime_get() / 1000);

    fs_file_t_init(&amp;amp;assert_file);
    int rc = fs_open(&amp;amp;assert_file, path, FS_O_CREATE | FS_O_RDWR);
    if (rc &amp;gt;= 0)
    {
        fs_write(&amp;amp;assert_file, assert_msg, strlen(assert_msg));
        fs_close(&amp;amp;assert_file);
    }

    /* User threads aren&amp;#39;t allowed to induce kernel panics; generate
     * an oops instead.
     */
    if (k_is_user_context())
    {
        k_oops();
    }
    else
    {
        k_panic();
    }

    // sys_reboot(SYS_REBOOT_COLD);
    CODE_UNREACHABLE;
}&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Disabling the&amp;nbsp;&lt;/span&gt;irq_lock() does not have any effect, the attempt to write to the files system does not work.&lt;br /&gt;Note, that the fatal error handler is called after this post assert function.&lt;/div&gt;
&lt;div&gt;The post assert function I wrote tries to write to a different file (fatal.txt) with no success as well, even if oops() or k_panic() is called from a shell function.&lt;/div&gt;
&lt;div&gt;&lt;pre class="ui-code" data-mode="text"&gt;static int cmd_fatal(const struct shell *shell, size_t argc, char **argv)
{
    ARG_UNUSED(argc);
    ARG_UNUSED(argv);
    shell_print(shell, &amp;quot;Device will now thow a fatal error&amp;quot;);
    k_oops();

    // Function should not return ----------------------------------------------------------------------------------

    return 0;
}



// we are able to ovveride the default weak handler with this if CONFIG_RESET_ON_FATAL_ERROR=n
void k_sys_fatal_error_handler(unsigned int reason,
							   const z_arch_esf_t *esf)
{

    led0_init();
    for (int i = 0; i &amp;lt; 10; i++)
    {
        led0_set(true);
        k_busy_wait(100000);
        led0_set(false);
        k_busy_wait(100000);
    }

    /* Disable interrupts, this is unrecoverable */
    (void)irq_lock();

    // printk(&amp;quot;FATAL ERROR\n&amp;quot;);

    struct fs_file_t fatal_file;
    char path[MAX_PATH_LEN];
    char fatal_msg[128];

    struct fs_mount_t *mp = storage_mount_point_get();
    snprintf(path, sizeof(path), &amp;quot;%s/%s&amp;quot;, mp-&amp;gt;mnt_point, &amp;quot;fatal.txt&amp;quot;);
    fs_unlink(path); // delete old fatal file if it exists

    sprintf(fatal_msg, &amp;quot;FATAL ERROR reason = %d\n&amp;quot;, reason);
    fs_file_t_init(&amp;amp;fatal_file);

    int rc = fs_open(&amp;amp;fatal_file, path, FS_O_CREATE | FS_O_RDWR);
    if (rc &amp;gt;= 0)
    {
        fs_write(&amp;amp;fatal_file, fatal_msg, strlen(fatal_msg));
        fs_close(&amp;amp;fatal_file);
    }

    sys_reboot(SYS_REBOOT_COLD);
    CODE_UNREACHABLE;
}
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Do you have a explanation of why my serial flash file system writes only seem to work in my assert_post_action function if I assert in a shell command?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;br /&gt;Best regards, thanks for the help.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;Wally&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/thread/435521?ContentTypeID=1</link><pubDate>Mon, 10 Jul 2023 13:31:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0d18bfaf-3f74-48bb-a4ab-830b4c27e084</guid><dc:creator>Vidar Berg</dc:creator><description>[quote user=""]&lt;p&gt;Is there an example of how to detect when this happens, i.e. a callback that I can override to detect it and store some information in NVS?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;[/quote]
&lt;p&gt;I forgot to add that you are free to provide your own implementation of the common&amp;nbsp;&lt;a title="k_sys_fatal_error_handler" href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.4.0/zephyr/kernel/services/other/fatal.html#c.k_sys_fatal_error_handler"&gt;&lt;code&gt;&lt;span&gt;k_sys_fatal_error_handler&lt;/span&gt;&lt;/code&gt;&lt;/a&gt;&amp;nbsp;if you want to process the faults from your application. E.g. to store error information to flash.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.4.0/zephyr/kernel/services/other/fatal.html"&gt;https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.4.0/zephyr/kernel/services/other/fatal.html&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/thread/435330?ContentTypeID=1</link><pubDate>Fri, 07 Jul 2023 18:13:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aaf94fce-75f9-4298-81cf-2c25a0763def</guid><dc:creator>wallyb</dc:creator><description>&lt;p&gt;ok thanks, I will read your links.&lt;/p&gt;
&lt;p&gt;w&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: MPU FAULT, Stack Guard, and Hard Faults</title><link>https://devzone.nordicsemi.com/thread/434974?ContentTypeID=1</link><pubDate>Thu, 06 Jul 2023 12:38:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f00b0fc0-2007-44f4-9d5f-d8ff9eb7ba69</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;Yes, the posts you linked were for the nRF5 SDK. You can read about how the stack guard is implemented for Cortex devices in the Zephyr documentation here:&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.4.0/zephyr/hardware/arch/arm_cortex_m.html#mpu-assisted-stack-overflow-detection"&gt;MPU-assisted stack overflow detection&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://developer.nordicsemi.com/nRF_Connect_SDK/doc/2.4.0/zephyr/hardware/arch/arm_cortex_m.html#stack-limit-checking-arm-v8-m"&gt;Stack limit checking (Arm v8-M)&lt;/a&gt;. The latter will be&amp;nbsp;selected by default on the nRF5340 since the CPUs include this feature.&lt;/p&gt;
&lt;p&gt;With regards to storing crash information to flash, please see the discussion in this forum thread:&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/101071/saving-coredumps-to-external-flash"&gt;Saving coredumps to external flash&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>