<?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>Use WDT to enter DFU mode</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/48842/use-wdt-to-enter-dfu-mode</link><description>Hi, 
 I&amp;#39;m working with an NRF52 Dev Kit and we want to implement OTA DFU using BLE as we have in a previous project. All our projects are worked on in Segger Embedded Studio. Recently we migrated from Nordic SDK 14.2.0 to SDK 15.3.0 and soft device 132</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 26 Jun 2019 19:26:52 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/48842/use-wdt-to-enter-dfu-mode" /><item><title>RE: Use WDT to enter DFU mode</title><link>https://devzone.nordicsemi.com/thread/194961?ContentTypeID=1</link><pubDate>Wed, 26 Jun 2019 19:26:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9b95010f-4fed-45ca-bfea-d1303136299b</guid><dc:creator>Andy Cruz</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Bj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I updated my project to include that function you suggested like so:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static bool dfu_enter_check(void)
{
    ...

    if(NRF_BL_DFU_ENTER_METHOD_WDT &amp;amp;&amp;amp;
      (NRF_POWER-&amp;gt;RESETREAS &amp;amp; POWER_RESETREAS_DOG_Msk)) //NOTE: this has been added in by me 6-18-2019
    {
        NRF_LOG_DEBUG(&amp;quot;DFU mode requested via watchdog-reset.&amp;quot;);
        nrf_dfu_bank_invalidate(&amp;amp;s_dfu_settings.bank_0);  //Device application no longer valid
        return true;
    }

    ...
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I am also resetting the RESETREAS register just as before, clearing the WDT bit, reset pin bit, etc. but it still isn&amp;#39;t working correctly. Currently I run an application that just enables a WDT and lets it timeout to reset the device and it enters into DFU mode but once the DFU inactivity timeout is hit the device resets and resumes the application just like before. I&amp;#39;m unfamiliar with &amp;quot;nrf_dfu_utils.c&amp;quot; and a lot of the specifics of how the DFU stores and checks its applications so I&amp;#39;ll read up more on these soon.&lt;/p&gt;
&lt;p&gt;If it matters, the process I use to update my Dev kit is erasing it in nRFgo Studio, uploading the bootloader (and I assume SoftDevice as well) in Segger Embedded Studio, and then uploading an application using the nRF Connect phone app.&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Andy&lt;/p&gt;
&lt;p&gt;EDIT: I got it working! It took also adding the function &amp;quot;nrf_dfu_settings_write_and_backup(NULL);&amp;quot; immediately after calling the bank invalidation function. I&amp;#39;m pretty sure what was happening was the device would reset from the WDT and enter DFU mode and invalidate the current memory for bank 0 which doesn&amp;#39;t do much since A.) we&amp;#39;re already past the check and in DFU mode and B.) this is just updating the temporary memory that gets erased on a reset. I tested around with also invalidating the other bank options (ie.&amp;nbsp;&lt;span&gt;&amp;amp;s_dfu_settings.bank_1 and&amp;nbsp;&amp;amp;s_dfu_settings.bank_current) to no avail&amp;nbsp;so I searched &amp;quot;s_dfu_settings&amp;quot; on the Infocenter and found the &amp;quot;nrf_dfu_settings.c&amp;quot; source file. These functions get called in &amp;quot;nrf_bootloader.c&amp;quot; and given the context I&amp;#39;m pretty sure they are used&amp;nbsp;for storing the DFU settings for resets, power cycles, etc.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I&amp;#39;ve modified my DFU check function again so now it is:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;static bool dfu_enter_check(void)
{
    ...

    if(NRF_BL_DFU_ENTER_METHOD_WDT &amp;amp;&amp;amp;
      (NRF_POWER-&amp;gt;RESETREAS &amp;amp; POWER_RESETREAS_DOG_Msk)) //NOTE: this has been added in by me 6-18-2019
    {
        NRF_LOG_DEBUG(&amp;quot;DFU mode requested via watchdog-reset.&amp;quot;);
        if(NRF_BL_DFU_ENTER_WDT_INVALIDATE_APP) //NOTE: this has been added in by me 6-26-2019
        {
            nrf_dfu_bank_invalidate(&amp;amp;s_dfu_settings.bank_0);  //Device application no longer valid
            nrf_dfu_settings_write_and_backup(NULL);
        }
        return true;
    }

    ...
}&lt;/pre&gt;and I made it an optional inclusion by also including in &amp;quot;sdk_config.h&amp;quot; right beside the other WDT inclusion:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="text"&gt;// &amp;lt;q&amp;gt; NRF_BL_DFU_ENTER_WDT_INVALIDATE_APP  - If entering DFU mode because of a WDT reset, invalidate the Application.


#ifndef NRF_BL_DFU_ENTER_WDT_INVALIDATE_APP
#define NRF_BL_DFU_ENTER_WDT_INVALIDATE_APP 1 //NOTE: this has been added in by me 6-26-2019
#endif&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thank you very much for pointing me in the right direction&amp;nbsp;Bj&amp;oslash;rn!&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Sincerely,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Andy&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use WDT to enter DFU mode</title><link>https://devzone.nordicsemi.com/thread/194235?ContentTypeID=1</link><pubDate>Mon, 24 Jun 2019 07:50:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e3d28f9e-8b58-4a95-887c-324516d8c5dd</guid><dc:creator>bjorn-spockeli</dc:creator><description>&lt;p&gt;Hi Andy,&lt;/p&gt;
&lt;p&gt;if you suspect that the application is fault if , then you can invalidate bank 0 in&amp;nbsp;dfu_enter_check() using nrf_dfu_bank_invalidate(&amp;amp;s_dfu_settings.bank_0) from nrf_dfu_utils.c after you have cleared RESETREAS, which will ensure that the bootloader will always enter DFU mode upon boot as there is no valid application to boot.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;/p&gt;
&lt;p&gt;Bjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Use WDT to enter DFU mode</title><link>https://devzone.nordicsemi.com/thread/194153?ContentTypeID=1</link><pubDate>Fri, 21 Jun 2019 17:06:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dc2f651e-e48d-4825-a3e6-cdaa71462b25</guid><dc:creator>Andy Cruz</dc:creator><description>&lt;p&gt;Ok in further investigation this morning I found a function in &amp;quot;nrf_bootloader.c&amp;quot; that I believe&amp;nbsp;is close to doing what I want if modified. I&amp;#39;m going to try changing the function:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**@brief Function for clearing all DFU enter flags that
 *        preserve state during reset.
 *
 * @details This is used to make sure that each of these flags
 *          is checked only once after reset.
 */
static void dfu_enter_flags_clear(void)
{
    if (NRF_BL_DFU_ENTER_METHOD_PINRESET &amp;amp;&amp;amp;
       (NRF_POWER-&amp;gt;RESETREAS &amp;amp; POWER_RESETREAS_RESETPIN_Msk))
    {
        // Clear RESETPIN flag.
        NRF_POWER-&amp;gt;RESETREAS |= POWER_RESETREAS_RESETPIN_Msk;
    }

    if (NRF_BL_DFU_ENTER_METHOD_GPREGRET &amp;amp;&amp;amp;
       ((nrf_power_gpregret_get() &amp;amp; BOOTLOADER_DFU_GPREGRET_MASK) == BOOTLOADER_DFU_GPREGRET)
            &amp;amp;&amp;amp; (nrf_power_gpregret_get() &amp;amp; BOOTLOADER_DFU_START_BIT_MASK))
    {
        // Clear DFU mark in GPREGRET register.
        nrf_power_gpregret_set(nrf_power_gpregret_get() &amp;amp; ~BOOTLOADER_DFU_START);
    }

    if (NRF_BL_DFU_ENTER_METHOD_BUTTONLESS &amp;amp;&amp;amp;
       (s_dfu_settings.enter_buttonless_dfu == 1))
    {
        // Clear DFU flag in flash settings.
        s_dfu_settings.enter_buttonless_dfu = 0;
        APP_ERROR_CHECK(nrf_dfu_settings_write(NULL));
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;to include the lines:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    if (NRF_BL_DFU_ENTER_METHOD_WDT &amp;amp;&amp;amp;
       (NRF_POWER-&amp;gt;RESETREAS &amp;amp; POWER_RESETREAS_DOG_Msk))  //NOTE: this has been added in by me 6-21-2019
    {
        // Clear DOG flag.
        NRF_POWER-&amp;gt;RESETREAS |= POWER_RESETREAS_DOG_Msk;
    }&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#39;m concerned with the comments over the function since it seems like this will lower the WDT bit after entering DFU mode the first time. If that means what I think it does then the device should:&lt;/p&gt;
&lt;p&gt;1.) Reset in the application by a WDT (ie. error of some kind; enter DFU mode to upload a new application).&lt;/p&gt;
&lt;p&gt;2.) Detect the reset was caused by a WDT and enter DFU mode.&lt;/p&gt;
&lt;p&gt;3.) Clear the WDT bit and assume the problem has been handled.&lt;/p&gt;
&lt;p&gt;4.) If there&amp;#39;s an inactivity timeout for DFU reset the device and run the old (possibly faulty) application again since the WDT bit is cleared.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not sure if this is the best course of action or if it would be preferential to continuously reset into DFU mode from inactivity timeouts instead of jumping back into the application. Is there anyway I could set the bootloader up such that it only resets the WDT bit after successfully uploading a new application through OTA DFU? Either way I&amp;#39;ll work on testing this code change to make sure it does work how I think it does thus far.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>