<?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>Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/96031/flash-operations-do-not-work-in-the-hard-fault-handler-hardfault_c_handler-function</link><description>Hi, 
 I want to log the error type and program counter when a hard fault occurred. I implemented some algorithm for that, I am using fds library for saving this information to flash. I do not get an error when saving hard fault information on the flash</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 27 Jan 2023 11:37:31 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/96031/flash-operations-do-not-work-in-the-hard-fault-handler-hardfault_c_handler-function" /><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406831?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 11:37:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5756075-01e0-4a9a-987c-5e0d6a7c1826</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;With a flash write operation&amp;nbsp;you can only flip bits from &amp;#39;1&amp;#39;s to &amp;#39;0&amp;#39;s.&amp;nbsp;To get the bits back to &amp;#39;1&amp;#39; you need to erase the flash page.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406800?ContentTypeID=1</link><pubDate>Fri, 27 Jan 2023 09:14:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d825d6b4-cd3d-42ea-b105-90ef84c8944f</guid><dc:creator>ilyasbsran</dc:creator><description>&lt;p&gt;When I erase the page before writing it works. it was not about increasing or decreasing the buffer. I don&amp;#39;t understand why it is related to&amp;nbsp;erasing&amp;nbsp;the page.&lt;/p&gt;
&lt;div id="gtx-trans" style="left:181px;position:absolute;top:-6px;"&gt;
&lt;div class="gtx-trans-icon"&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406689?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 14:22:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:760fb34d-0714-4fac-ab66-55f7195500f1</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Can you erase the page and also change to:&lt;/p&gt;
&lt;p&gt;buffer[0] = buffer[0] - 1; // - instead of +&lt;/p&gt;
&lt;p&gt;And share&amp;nbsp;what you get then?&lt;/p&gt;
&lt;p&gt;Kenneth&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406660?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 13:10:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3835d209-f3ac-42b7-9b65-9044dc2c8bec</guid><dc:creator>ilyasbsran</dc:creator><description>&lt;p&gt;I see the same value every time.&lt;/p&gt;
&lt;p&gt;I erased flash using nrfjprog. then I run this code. I see the same value (it was 0 )every reset.&lt;pre class="ui-code" data-mode="c_cpp"&gt;#define ADDRESS_OFF_TEST                    0x45000

void flash_set_data(const uint8_t * src){
    nrf_nvmc_write_bytes(ADDRESS_OFF_TEST, src, sizeof(uint8_t));
}

void flash_get_data(uint8_t * dst){
    memcpy(dst, (uint8_t*)ADDRESS_OFF_TEST, sizeof(uint8_t));
}
int main(void)
{
    nrf_delay_ms(10000);

    uint8_t  buffer[1];
    flash_get_data(buffer);

    BB_printf(LOG_FATAL, &amp;quot;Counter : %u&amp;quot;, buffer[0]);

    buffer[0] = buffer[0] + 1;
    flash_set_data(buffer);

    nrf_delay_ms(10000);

    NVIC_SystemReset();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I think my problem is not about&amp;nbsp;enabling or&amp;nbsp;disabling the soft device.&lt;/p&gt;
&lt;div&gt;
&lt;div class="content-scrollable-wrapper"&gt;
&lt;pre&gt;FDS_BACKEND &lt;span&gt;1 in sdk_config.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406658?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 12:58:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8fb59c00-7f70-4609-b3ff-03ee524e588e</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Thanks for confirming.&amp;nbsp;What do you see when you read the memory at 0x76000 afterwards?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406650?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 12:40:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7c3fc861-0d90-4af1-938b-911e4ea425e5</guid><dc:creator>ilyasbsran</dc:creator><description>&lt;p&gt;Yes, I confirmed it,&amp;nbsp;I check it by printing log via j link rtt&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406645?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 12:26:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:841eb827-0c60-4162-b32e-9ada0cc4c5e9</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Have you confirmed&amp;nbsp;through debugging&amp;nbsp;that&amp;nbsp;flash_set_data() ends up being called&amp;nbsp;in&amp;nbsp;HardFault_c_handler()?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406644?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 12:18:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:82c09275-f08f-4ced-8874-9c6d3a58e33d</guid><dc:creator>ilyasbsran</dc:creator><description>&lt;p&gt;&lt;span&gt;NRF_MWU-&amp;gt;PREGION[0].SUBS &amp;amp;= ~(MWU_PREGION_SUBS_SR30_Include &amp;lt;&amp;lt; MWU_PREGION_SUBS_SR30_Pos);&lt;/span&gt;&lt;br /&gt;&lt;span&gt;__DSB(); // barrier to ensure register is set before accessing NVMC.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I used it at starting of HardFault_c_handler() function.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406638?ContentTypeID=1</link><pubDate>Thu, 26 Jan 2023 12:07:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59dc466f-d4fd-4847-85d6-3fe11753ec1b</guid><dc:creator>ilyasbsran</dc:creator><description>&lt;p&gt;Hi Thank your for the reply,&lt;/p&gt;
&lt;p&gt;I just want to see if I can write to flash.&lt;/p&gt;
&lt;div&gt;
&lt;pre&gt;FDS_BACKEND &lt;span&gt;1 in sdk_config.&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int main(void)
{
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
    
    APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
    
    power_management_init();

    err_code = nrf_sdh_enable_request();
    APP_ERROR_CHECK(err_code);

    nrf_delay_ms(10000);

    hardfault_genhf_invalid_fp();
}

#define ADDRESS_OFF_TEST                    0x76000

void flash_set_data(const uint8_t * src){
    nrf_nvmc_write_bytes(ADDRESS_OFF_TEST, src, sizeof(uint8_t));
}

void flash_get_data(uint8_t * dst){
    memcpy(dst, (uint8_t*)ADDRESS_OFF_TEST, sizeof(uint8_t));
}

/** @breif Errors caused by Bus Fault, Memory Management Fault, or Usage Fault
 *
 * @note If you want to use custom HardFault_c_handler(), Don&amp;#39;t include hardfault_handler_gcc.c file on makefile.
 *       Don&amp;#39;t include hardfault_implementation.c file on makefile.
 *
 * @note In order to use this function, the @ref init_hardfault_storage() function must be used.
 */
void HardFault_c_handler(uint32_t * p_stack_address){
    uint8_t  buffer[1];
    flash_get_data(buffer);
    BB_printf(LOG_FATAL, &amp;quot;Counter : %u&amp;quot;, buffer[0]);
    
    buffer[0] = buffer[0] + 1;
    flash_set_data(buffer);
    
    nrf_delay_ms(1000);

    // Restart the device.
    NVIC_SystemReset();
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I can not write to flash. Wham&amp;nbsp;Am i&amp;nbsp;doing wrong?&lt;/p&gt;
&lt;div id="gtx-trans" style="left:185px;position:absolute;top:356.5px;"&gt;
&lt;div class="gtx-trans-icon"&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406516?ContentTypeID=1</link><pubDate>Wed, 25 Jan 2023 15:46:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:25ab6aea-0997-49f1-9d9e-75e5f7468178</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Also&amp;nbsp;have a look at this post if&amp;nbsp;you are&amp;nbsp;using the Softdevice in your application:&amp;nbsp;&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/53248/disable-softdevice-sandboxing-in-fault-handler/215049"&gt;RE: Disable Softdevice sandboxing in fault handler&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Flash operations do not work in the hard fault handler (HardFault_c_handler function).</title><link>https://devzone.nordicsemi.com/thread/406460?ContentTypeID=1</link><pubDate>Wed, 25 Jan 2023 13:11:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:694394e4-47df-4246-b2f7-d0d2401e4b01</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I guess there are different ways, but considering you are in the hardfault handler and you can&amp;#39;t really depend on any software modules to work, and the&amp;nbsp;interrupts are disabled (or lower priority), so you would need to use flash api that are bare metal and don&amp;#39;t rely on interrupts or other software modules. In that case I suggest to use the api in \modules\nrfx\hal\nrf_nvmc.c directly, for instance call&amp;nbsp;nrf_nvmc_page_erase() to erase the flash page you want to store the information, and then follow by one or several calls to&amp;nbsp;nrf_nvmc_write_words(), before you do a system reset to start the chip again. It may be that you need to rename the api in&amp;nbsp;&lt;span&gt;\modules\nrfx\hal\nrf_nvmc.c, because you may using a similar api in your project already that rely on other software modules.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Kenneth&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>