<?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>HardFault_Handler() called when NRF_LOG_ENABLED is disabled</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/20791/hardfault_handler-called-when-nrf_log_enabled-is-disabled</link><description>In my application when I disable NRF_LOG_ENABLED , HardFault_Handler() is getting called. To debug this issue I defined a HardFault_Handler() in my code and inserted a break point in it. Below is the backtrace 
 #0 HardFault_Handler () at main.c:165</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 28 Mar 2017 07:56:02 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/20791/hardfault_handler-called-when-nrf_log_enabled-is-disabled" /><item><title>RE: HardFault_Handler() called when NRF_LOG_ENABLED is disabled</title><link>https://devzone.nordicsemi.com/thread/81194?ContentTypeID=1</link><pubDate>Tue, 28 Mar 2017 07:56:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e4101fd0-b32b-4553-a6d5-0fe5a79a7875</guid><dc:creator>Prasad</dc:creator><description>&lt;h2&gt;Short answer&lt;/h2&gt;
&lt;p&gt;This is a memory alignment issue and it got fixed by adding &lt;code&gt;. = ALIGN(4)&lt;/code&gt; in my linker script.&lt;/p&gt;
&lt;h2&gt;Long answer&lt;/h2&gt;
&lt;p&gt;I debugged the code in GDB. Problem gets manifested in the below snippet from fstorage.c&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for (uint32_t i = 0; i &amp;lt; total_users; i++)
{
    fs_config_t const * const p_config = FS_SECTION_VARS_GET(i);

    ==&amp;gt;if ((p_config-&amp;gt;p_start_addr != NULL) &amp;amp;&amp;amp;
        (p_config-&amp;gt;p_end_addr   != NULL))
    {
        configs_to_init--;
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When I step over from this point I end up in the &lt;code&gt;HardFault_Handler()&lt;/code&gt;. After investigation, I found that &lt;code&gt;FS_SECTION_VARS_GET(i)&lt;/code&gt; is not returning the correct address of &lt;code&gt;fs_config&lt;/code&gt;. Then with objdum I checked the addresses of &lt;code&gt;__start_fs_data&lt;/code&gt; and &lt;code&gt;fs_config&lt;/code&gt; in the object file.&lt;/p&gt;
&lt;p&gt;When &lt;code&gt;NRF_LOG_ENABLED = 1&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;200029e4 g       .data  00000000 __start_fs_data
200029e4 l     O .data  00000010 fs_config
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When &lt;code&gt;NRF_LOG_ENABLED = 0&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;2000202a g       .data  00000000 __start_fs_data
2000202c l     O .data  00000010 fs_config
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see that when &lt;code&gt;NRF_LOG_ENABLED = 0&lt;/code&gt;, &lt;code&gt;__start_fs_data&lt;/code&gt; is not aligned with &lt;code&gt;fs_config&lt;/code&gt; and hence the macro &lt;code&gt;FS_SECTION_VARS_GET(i)&lt;/code&gt; returns an address where valid data doesn’t exist, causing a hardware fault.&lt;/p&gt;
&lt;p&gt;Resolution is to add &lt;code&gt;. = ALIGN(4)&lt;/code&gt; in your linker file as show below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;.data : AT(_sidata)
{
	. = ALIGN(4);
	_sdata = .;

	PROVIDE(__data_start__ = _sdata);
	*(.data)
	*(.data*)

==&amp;gt;	. = ALIGN(4);

	PROVIDE(__start_fs_data = .);
	KEEP(*(.fs_data))
	PROVIDE(__stop_fs_data = .);
	
	. = ALIGN(4);
	_edata = .;

	PROVIDE(__data_end__ = _edata);
} &amp;gt; SRAM
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I chose to fix it this way, if anyone knows of a better solution then please let me know.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: HardFault_Handler() called when NRF_LOG_ENABLED is disabled</title><link>https://devzone.nordicsemi.com/thread/81191?ContentTypeID=1</link><pubDate>Mon, 27 Mar 2017 12:17:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d3527b92-583b-46fc-87f2-85c9a03a7117</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Which SDK version are you using? Which example for the SDK are your application built on? Could you try debugging using &lt;a href="https://devzone.nordicsemi.com/question/60125/my-device-is-freezing-and-restarting/"&gt;this method&lt;/a&gt;, to check if you get any error codes, or which call is fausing the hardfault?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: HardFault_Handler() called when NRF_LOG_ENABLED is disabled</title><link>https://devzone.nordicsemi.com/thread/81193?ContentTypeID=1</link><pubDate>Mon, 27 Mar 2017 10:20:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:73ac4fb4-389f-4cac-90fb-930f897810df</guid><dc:creator>Prasad</dc:creator><description>&lt;p&gt;This doesn&amp;#39;t seem to be the case as I checked it with a clean build.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: HardFault_Handler() called when NRF_LOG_ENABLED is disabled</title><link>https://devzone.nordicsemi.com/thread/81192?ContentTypeID=1</link><pubDate>Sat, 25 Mar 2017 20:30:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c418ced8-05cc-4c48-81e4-422bf687db1c</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;Clean the project and recompile, you may have mixed old and new compiled object files.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>